/*----------------------------------------------------------------------------
 (c) 2003 JVolution Ltd. All rights reserved. 
 $Revision: 1.1 $
<%--
 Program:   Cablinks

 File : $Header: /jvolution/cablinks/scripts/scriptlib.js,v 1.1 2003/01/24 17:19:34 john Exp $

  Last Modified:  $Date: 2003/01/24 17:19:34 $
             By:  $Author: john $                 

 History :
 ------------------
   $Log: scriptlib.js,v $
   Revision 1.1  2003/01/24 17:19:34  john
   Initially added to CVS .JBW.
  --%>
-----------------------------------------------------------------------------*/
var imagecache = new Array();
var imagename = new Array();
var testundefined ;
var isNav4, isIE4, isNav6, isMac = false, isMac50 = false, isSafari ;

function trimString (str) 
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
String.prototype.trim = trimString;

if (parseInt(navigator.appVersion.charAt(0)) >= 4) 
{
  isSafari = navigator.userAgent.indexOf("Safari")!=-1?true:false;
  isNav6 = navigator.userAgent.indexOf("Gecko")!=-1?true:false;
  isNav4 = (navigator.appName == "Netscape") ? true && !isNav6: false;
  isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
  if (isIE4)
  {
    isMac = navigator.userAgent.indexOf("Mac")!=-1?true:false;
    if (isMac)
  	  	isMac50 = navigator.userAgent.indexOf("5.0")!=-1?true:false;
    isIE50 = navigator.userAgent.indexOf("MSIE 5.0")!=-1?true:false;
    if (false == isIE50)
  	  	isIE50 = navigator.userAgent.indexOf("MSIE 4.0")!=-1?true:false;
  }
	// Mozilla is like Safari but not like NS6 and NS7
	if (isNav6 == true && (navigator.userAgent.indexOf("Netscape/7")==-1 && navigator.userAgent.indexOf("Netscape/6")==-1))
	{
		isSafari = true ;
		isNav6 = false ;
	}
}

function showImage(image, generatedImage, profile, text) 
{
	image.src = null ;
	// Look for image in local cache (identified by profile name and text string)
	for (i = 0 ; i < imagename.length ; i++)
	{
		if (imagename[i] == profile + text)
		{
			// Image found in local cache - use it
			image.src = imagecache[i] ;
			return ;
		}
	}
	// This image has not been found in cache - go to TextFX to generate it
	image.src = generatedImage ;
	// Cache this image locally for next time
	i = imagename.length ;
	imagecache[i] = image.src ;
	imagename[i] == profile + text ;
}
function saveTabNo(tabNo)
{
	var element = (isIE4) ? document.all.tabno : document.getElementById("tabno") ;
	if (element != null) element.value = tabNo ;
}
function getTabNo()
{
	var element = (isIE4) ? document.all.tabno : document.getElementById("tabno") ;
	var value = "" ;
	if (element != null) value = element.value	
	return value ;
}
function upHours(fieldName)
{
	var timefield = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	var timevalue = timefield.value ;
	if (timevalue == null) return false ;
	var timefields = timevalue.split(":") ;
	if (timefields[0] == "23")
		timevalue = 0 ;
	else
		timevalue = eval(timefields[0]) + 1 ;
	if (timevalue < 10) timevalue = "0" + timevalue ;
	timefield.value = timevalue + ":" + timefields[1] ;
}
function downHours(fieldName)
{
	var timefield = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	var timevalue = timefield.value ;
	if (timevalue == null) return false ;
	var timefields = timevalue.split(":") ;
	if (timefields[0] == "00")
		timevalue = 23 ;
	else
		timevalue = eval(timefields[0]) - 1 ;
	if (timevalue < 10) timevalue = "0" + timevalue ;
	timefield.value = timevalue + ":" + timefields[1] ;
}
function upMins(fieldName)
{
	var timefield = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	var timevalue = timefield.value ;
	if (timevalue == null) return false ;
	var timefields = timevalue.split(":") ;
	if (timefields[1] == "55" 
	|| timefields[1] == "56" 
	|| timefields[1] == "57"
	|| timefields[1] == "58"
	|| timefields[1] == "59")
	{
		timevalue = 0 ;
		var hourvalue ;
		if (timefields[0] == "23")
			hourvalue = 00 ;
		else
			hourvalue = eval(timefields[0]) + 1 ;
		if (hourvalue < 10) hourvalue = "0" + hourvalue ;
		timefields[0] = hourvalue ;
	}
	else
		timevalue = eval(timefields[1]) + 5 ;
	if (timevalue < 10) timevalue = "0" + timevalue ;
	timefield.value = timefields[0] + ":" + timevalue ;
}
function downMins(fieldName)
{
	var timefield = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	var timevalue = timefield.value ;
	if (timevalue == null) return false ;
	var timefields = timevalue.split(":") ;
	if (timefields[1] == "00"
	|| timefields[1] == "01" 
	|| timefields[1] == "02"
	|| timefields[1] == "03"
	|| timefields[1] == "04")
	{
		timevalue = 55 ;
		var hourvalue ;
		if (timefields[0] == "00")
			hourvalue = 23 ;
		else
			hourvalue = eval(timefields[0]) - 1 ;
		if (hourvalue < 10) hourvalue = "0" + hourvalue ;
		timefields[0] = hourvalue ;
	}
	else
		timevalue = eval(timefields[1]) - 5 ;
	if (timevalue < 10) timevalue = "0" + timevalue ;
	timefield.value = timefields[0] + ":" + timevalue ;
}
function removeSpaces(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	fieldStr = thisField.value ;
	newStrs = fieldStr.split(" ") ;
	fieldStr = ""
	for (i = 0 ; i < newStrs.length ; i++)
	{
		fieldStr = fieldStr + newStrs[i] ;
	}
	thisField.value = fieldStr ;
}
function clearField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	thisField.value = "" ;
}
function disableField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	thisField.disabled = true ;
}
function enableField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	thisField.disabled = false ;
}
function validateEmail(fieldName) 
//Validates email address by passing the object
{
	var emailField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	if (emailField.value.length < 1) return null ;
	if (-1 == emailField.value.indexOf("@")) 
	{ 
		return "Email address must have a '@' in it." ; 
	}
	if (-1 != emailField.value.indexOf(",")) 
	{ 
		return "Email address must not have a ',' in it."; 
	}
	if (-1 != emailField.value.indexOf("#")) 
	{ 
		return "Email address must not have an '#' in it."; 
	}
	if (-1 != emailField.value.indexOf("!")) 
	{ 
		return "Email address must not have a '!' in it." ; 
	}
	if (-1 != emailField.value.indexOf(" ")) 
	{ 
		return "Email address must not have a space in it."; 
	}
	if (emailField.value.length == (emailField.value.indexOf("@")+1) ) 
	{ 
		return "Email address must have a domain name after the '@'."; 
	}
	if (emailField.value.length == 0) 
	{ 
		return "Please enter your email address."; 
	}
	return null ;
}
function checkForTelephoneNoField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;

	var fieldStr = thisField.value ;
   cmp = "0123456789 " ;
  	for (j = 0 ; j < fieldStr.length ; j++)
	{
   	tst = fieldStr.substring(j, j+1)
   	if (cmp.indexOf(tst) < 0)
		{
			return false ;
		}
	}
   return true ;
}
function checkForDateField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;

	var fieldStr = thisField.value ;
   cmp = "0123456789/" ;
  	for (j = 0 ; j < fieldStr.length ; j++)
	{
   	tst = fieldStr.substring(j, j+1)
   	if (cmp.indexOf(tst) < 0)
		{
			return false ;
		}
	}
   return true ;
}
function checkForNumericField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;

	var fieldStr = thisField.value ;
   cmp = "0123456789" ;
  	for (j = 0 ; j < fieldStr.length ; j++)
	{
   	tst = fieldStr.substring(j, j+1)
   	if (cmp.indexOf(tst) < 0)
		{
			return false ;
		}
	}
   return true ;
}
function checkForAlphabeticField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;

	var fieldStr = thisField.value ;
   cmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,-'\"`_ " ;
  	for (j = 0 ; j < fieldStr.length ; j++)
	{
   	tst = fieldStr.substring(j, j+1)
   	if (cmp.indexOf(tst) < 0)
		{
			return false  ;
		}
	}
   return true ;
}
function checkForRegistrationNoField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;

	var fieldStr = thisField.value ;
   cmp = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
  	for (j = 0 ; j < fieldStr.length ; j++)
	{
   	tst = fieldStr.substring(j, j+1)
   	if (cmp.indexOf(tst) < 0)
		{
			return false ;
		}
	}
   return true ;
}
function focusOnField(fieldName)
{
	var thisField = (isIE4) ? eval("document.all." + fieldName) : document.getElementById(fieldName) ;
	if (thisField == null) return ;
	thisField.select();
	thisField.focus() ;
}
function checkFieldsCompleted(fieldNames, fieldDescriptors)
{
	var fieldNameArr = fieldNames.split(",") ;
	if (fieldDescriptors == null) fieldDescriptors = fieldNames ;
	var fieldDescriptorArr = fieldDescriptors.split(",") ;
	var i ;
	for (i = 0 ; i < fieldNameArr.length ; i++)
	{
		var fldArr = new Array() ;
		fldArr[0] = fieldNameArr[i] ;
		fldArr[1] = fieldDescriptorArr[i] ;
		var thisField = (isIE4) ? eval("document.all." + fieldNameArr[i]) : document.getElementById(fieldNameArr[i]) ;
		if (thisField == null)
		{
			alert("Cannot find field " + fieldNameArr[i]) ;
			continue ;
		}
		if (thisField.type == "text" 
		|| thisField.type == "input" 
		|| thisField.type == "hidden"
		|| thisField.type == "password"
		|| thisField.type == "textarea" 
		|| thisField.type == "radio")
		{
			if (thisField.value.trim().length < 1) return fldArr ;
		}
		else
		if (thisField.type == "checkbox")
		{
			if (thisField.checked == false) fldArr ; 
		}
		else
		if (thisField.type == "select-one")
		{
			if (thisField.options.selectedIndex < 0
			|| thisField.options[thisField.options.selectedIndex].text.trim().length < 1)  return fldArr ;
		}
	}
	return null ;
}
function capitaliseAll(thisobj)
{
return ;
	if (thisobj.value.length < 0) return ;
	var newvalue = "" ;
	var i ;
	for (i = 0 ; i < thisobj.value.length ; i++)
	{
		var charvalue = thisobj.value.charAt(i) ;
		newvalue = newvalue + charvalue.toUpperCase();
	}
	thisobj.value = newvalue ;
}
function capitaliseWords(thisobj)
{
return ;
	if (thisobj.value.length < 0) return ;
	var newvalue = "" ;
	var i ;
	for (i = 0 ; i < thisobj.value.length ; i++)
	{
		var charvalue = thisobj.value.charAt(i) ;
		if (i == 0)
			newvalue = newvalue + charvalue.toUpperCase();
		else
		{
			var prevvalue = thisobj.value.charAt(i-1) ;
			if (prevvalue == " "
			 || prevvalue == ","
			 || prevvalue == ".")
				newvalue = newvalue + charvalue.toUpperCase();
			else
				newvalue = newvalue + charvalue ;
		}
	}
	thisobj.value = newvalue ;
}
function capitaliseSentances(thisobj)
{
return ;
	if (thisobj.value.length < 0) return ;
	var newvalue = "" ;
	var i ;
	for (i = 0 ; i < thisobj.value.length ; i++)
	{
		var charvalue = thisobj.value.charAt(i) ;
		if (i == 0)
			newvalue = newvalue + charvalue.toUpperCase();
		else
		{
			if (i > 2 
				&& thisobj.value.charAt(i-1) == " "
			   && thisobj.value.charAt(i-2) == ".")
				newvalue = newvalue + charvalue.toUpperCase();
			else
				newvalue = newvalue + charvalue ;
		}
	}
	thisobj.value = newvalue ;
}
function showDocument(docname)
{
	window.open(docname, '', 'height=500,width=700,top=10,left=10,toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes')
}
function printCurrentPage() 
{
   if (navigator.appName.indexOf("Microsoft") > -1 
   && navigator.appVersion.indexOf("5.") == -1) 
   { 
   // IE4 
   OLECMDID_PRINT = 6; 
   OLECMDEXECOPT_DONTPROMPTUSER = 2; 
   OLECMDEXECOPT_PROMPTUSER = 1; 
   WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
   document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
   WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER); 
   WebBrowser1.outerHTML = ""; 
   } 
   else 
   { 
   // N4 IE5 
   window.print(); 
   }
   return false ;
} 

var foundElement;
function getObj(name)
{
	foundElement = null ;
  if (document.getElementById)
  {
  	foundElement = document.getElementById(name);
  }
  else if (document.all)
  {
	foundElement = document.all[name];
  }
  else if (document.layers)
  {
	getObjNN4(document.forms[0],name);
  }
  return foundElement ;
}
function getObjNN4(obj,name)
{
	if (document.forms[0].name == name)
	{
		foundElement = document.forms[0];
		return ;
	}
	var x = obj.elements;
	for (var i=0;i<x.length;i++)
	{
		if (x[i] == null) continue ;
		if (x[i].name == name)
		 	foundElement = x[i];
	}
}