
/******************************************************************************************
Function Name: IsEmpty
Parameters: pszStringtoCheck
Purpose: Check whether string s is empty and returns true if the string is empty.
only.
*******************************************************************************************/
function IsEmpty ( pszStringtoCheck ) {
	return ( ( pszStringtoCheck == null ) || ( pszStringtoCheck.length == 0 ) )
}
		

/******************************************************************************************
Function Name: IsWhitespace
Parameters: pszStringtoCheck
Purpose: Returns true if string 'pszStringtoCheck' is empty or whitespace characters 
only.
*******************************************************************************************/
function IsWhitespace ( pszStringtoCheck )
{
	var reWhitespace = /^\s+$/
	return ( IsEmpty ( pszStringtoCheck ) || reWhitespace.test ( pszStringtoCheck ) );
}
		

/******************************************************************************************
Function Name: TrimTheString
Parameters: pszStringtoTrim
Purpose: Returns the string after triming it.
*******************************************************************************************/
function TrimTheString ( pszStringtoTrim )
{
	var bflag = true;
	var i = 0;
	if ( IsWhitespace ( pszStringtoTrim ) == true )
		return "";
	while ( ( i < pszStringtoTrim.length ) && ( bflag ) )
	{
		retChar = pszStringtoTrim.charAt ( i++ );
		if ( retChar != " " ) bflag = false;
	}
	if ( bflag ) return "";
	var j = pszStringtoTrim.length-1;
	bflag = true;
	while ( ( j >= 0 ) && ( bflag ) ) 
	{
		retChar = pszStringtoTrim.charAt ( j-- );
		if ( retChar != " " ) bflag = false;
	}
	if ( bflag ) return "";
	pszStringtoTrim = pszStringtoTrim.substring ( i-1 ,j+2 );
	return pszStringtoTrim;
}
		
/******************************************************************************************
Function Name: IsDate
Parameters: pszFieldObj, objArgs
Purpose: Verifies if the value in the text object passed is a proper date or not. It 
validates format matching either mm-dd-yyyy or mm/dd/yyyy. Then it checks to 
make sure the month has the proper number of days, based on which month it is.
*******************************************************************************************/

function IsDate (pszFieldObj,objArgs)
		{
			var indate=objArgs.Value;
			var szTheDate = TrimTheString(indate)
			var reTheRegExp = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
			var aMatchArray = szTheDate.match(reTheRegExp); // is the format ok?
			if (aMatchArray == null) 
			{
				//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
				objArgs.IsValid = false;
				return ;
			}

			nMonth = aMatchArray[1]; // parse date into variables
			nDay = aMatchArray[3];
			nYear = aMatchArray[5];
			
			if ( nYear < 1900 )
			{
				//alert("Year must be greater than 1900.");
				objArgs.IsValid = false;
				return ;
			}

			if ( nMonth < 1 || nMonth > 12 ) // check month range
			{
				//alert("Month must be between 1 and 12.");
				objArgs.IsValid = false;
				return ;
			}

			if ( nDay < 1 || nDay > 31 ) 
			{
				//alert("Day must be between 1 and 31.");
				objArgs.IsValid = false;
				return;
			}

			if ( ( nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) && nDay == 31 ) 
			{
				//alert("Month " + nMonth + " does not have 31 days!")
				objArgs.IsValid = false;
				return;
			}

			if ( parseInt ( nMonth ) == 2) // check for february 29th
			{
				var isleap = ( nYear % 4 == 0 && ( nYear % 100 != 0 || nYear % 400 == 0 ) );
				if ( parseInt ( nDay ) > 29 || ( parseInt ( nDay ) == 29 && !isleap ) ) 
				{
					//alert("February " + nYear + " doesn't have " + nDay + " days!");
					objArgs.IsValid = false;
					return;
				}
			}
			objArgs.IsValid = true;
			return ; // date is valid
		}
		
		
/******************************************************************************************
Function Name: ClearAllControls
Parameters: none
Purpose: Clears all the textbox controls in the current form.
only.
*******************************************************************************************/

		function ClearAllControls()
		{
			for(var Count = 0;Count<document.forms(0).elements.length;Count++)
			{
			if(document.forms(0).elements(Count).type == 'text' || document.forms(0).elements(Count).type == 'password')
				document.forms(0).elements(Count).value = ''; 
			}
			
		}
/******************************************************************************************
Function Name: change
Parameters: color
Purpose: Change the color of the button object with the color code passed.
only.
*******************************************************************************************/	
			function change(color)
			{
				//var el = event.srcElement
				//alert(el.);
				//if (el.tagName == "INPUT" && (el.type == "button")
				event.srcElement.style.backgroundColor = color;
			}

/******************************************************************************************
Function Name: IsValidPhoneNo
Parameters: color
Purpose: Change the color of the button object with the color code passed.
only.
*******************************************************************************************/	

function IsValidPhoneNo ( objSource , objArgs )
{

	var pszValue = objArgs.Value;
	var szStripped = pszValue.replace(/[\(\)\.\-\ ]/g, '');
	if ( ( szStripped.length != 10)) 
	{
		 objArgs.IsValid = false ;
		return objArgs;
	}
	 objArgs.IsValid = true ;
}

/******************************************************************************************
Function Name: IsCurrencyFloatSign
Purpose: Checks for numeric value and allows decimal depending on the 'pnPrecision' 
parameter. Also allows + or - sign at the beginning depending on the second 
parameter, 'pbAllowSign'.
*******************************************************************************************/	
function IsFloat ( objSource , objArgs )
{
	//var szFieldValue = eval( pszFieldObj ).value
	var szFieldValue = objArgs.Value
	var pbAllowSign  = false;
	var pszCurrency = "";
	var pnPrecision = "0"
	
	if ( pbAllowSign )
	{
		if ( TrimTheString ( pszCurrency ) == "$" )
			reTheRegExp = /^(((\+|-)?\$?\d+(\.\d*)?)|((\+|-)?\$?(\d*\.)?\d+))$/
		else if ( TrimTheString ( pszCurrency ) == "£" )
			reTheRegExp = /^(((\+|-)?\£?\d+(\.\d*)?)|((\+|-)?\£?(\d*\.)?\d+))$/
		else if ( TrimTheString ( pszCurrency ) == "¥" )
			reTheRegExp = /^(((\+|-)?\¥?\d+(\.\d*)?)|((\+|-)?\¥?(\d*\.)?\d+))$/
		else
			reTheRegExp = /^(((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))$/
	}
	else
	{
		if ( TrimTheString ( pszCurrency ) == "$" )
			reTheRegExp = /^((\$?\d+(\.\d*)?)|((\$?\d*\.)?\d+))$/
		else if ( TrimTheString ( pszCurrency ) == "£" )
			reTheRegExp = /^((\£?\d+(\.\d*)?)|((\£?\d*\.)?\d+))$/
		else if ( TrimTheString ( pszCurrency ) == "¥" )
			reTheRegExp = /^((\¥?\d+(\.\d*)?)|((\¥?\d*\.)?\d+))$/
		else
			reTheRegExp = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
	}

	if ( reTheRegExp.test( TrimTheString ( szFieldValue ) ) == false )
	{
		if ( szFieldValue.substr ( 0, 1 ) == "$" || szFieldValue.substr ( 0, 1 ) == "£" || szFieldValue.substr ( 0, 1 ) == "¥" )
		{
			if ( szFieldValue.substr ( 0, 1 ) != pszCurrency )
			{
				alert("The Currency symbol does not match.");
				eval( pszFieldObj ).focus();
				objArgs.IsValid = false ;
				return;
			}
		}
		else
		{
			/*alert("Field should contain only numeric value");
			eval( pszFieldObj ).focus();*/
			objArgs.IsValid = false ;
			return;
		}
	}
	pnPrecision = "" + pnPrecision
	
	if ( TrimTheString ( pnPrecision ) != "" )
	{
		if ( ( pnPrecision >= ( ( TrimTheString ( szFieldValue ).length - 1 ) - TrimTheString ( szFieldValue ).indexOf ( "." ) ) ) || ( TrimTheString ( szFieldValue ).indexOf ( "." ) == -1 ) )
		{
			objArgs.IsValid = true ;
			return;
		}
		else
		{
			/*alert( "Field should contain only " + pnPrecision + " decimal place(s)." );
			eval( pszFieldObj ).focus();*/
			objArgs.IsValid = false;
			return;
		}
	}
	else
	{
		objArgs.IsValid = true;
		return;
	}
}

/******************************************************************************************
Function Name: MM_swapImgRestore, MM_preloadImages, MM_findObj, MM_swapImage
Purpose: Functions for swapping of images for mouseover and mouse out events
parameter. N/A
*******************************************************************************************/	
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}		

/******************************************************************************************
Function Name: FormatPhone
Parameters: phoneField
Purpose: Format the inputed phone number directly at the time of entry...
*******************************************************************************************/	
function FormatPhone(phoneField){
	p = phoneField.value
	if(p.length==3){
			pp=p;
			d4=p.indexOf('(')
			d5=p.indexOf(')')
			if(d4==-1){
					pp="("+pp;
			}
			if(d5==-1){
					pp=pp+") ";
			}
			phoneField.value="";
			phoneField.value=pp;
	}
	if(p.length>3){
			d1=p.indexOf('(')
			d2=p.indexOf(')')
			if (d2==-1){
					l30=p.length;
					p30=p.substring(0,4);
					p30=p30+") "
					p31=p.substring(4,l30);
					pp=p30+p31;
					phoneField.value="";
					phoneField.value=pp;
			}
	}
	if(p.length>6){
			p11=p.substring(d1+1,d2);
			if(p11.length>3){
				p12=p11;
				l12=p12.length;
				l15=p.length
				p13=p11.substring(0,3);
				p14=p11.substring(3,l12);
				p15=p.substring(d2+1,l15);
				phoneField.value="";
				pp="("+p13+") "+p14+p15;
				phoneField.value=pp;
			}
			l16=p.length;
			p16=p.substring(d2+1,l16);			
			l17=p16.length;
			if(l17>3&&p16.indexOf('-')==-1){
					p17=p.substring(d2+1,d2+5);
					p18=p.substring(d2+5,l16);
					p19=p.substring(0,d2+1);
					pp=p19+p17+"-"+p18;
					phoneField.value="";
					phoneField.value=pp;
			}
	}
}

/******************************************************************************************
Function Name: FormatCurrency
Parameters: number
Purpose: Format the inputed currency field directly at the time of entry...
*******************************************************************************************/	
function FormatCurrency(number) {
	number = number.toString().replace(/\$|\,/g,'');
	if(isNaN(number))
	number = "0";
	sign = (number == (number = Math.abs(number)));
	number = Math.floor(number*100+0.50000000001);
	cents = number%100;
	number = Math.floor(number/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
	number = number.substring(0,number.length-(4*i+3))+','+
	number.substring(number.length-(4*i+3));
	//return (((sign)?'':'-') + '$' + number + '.' + cents);
	return (((sign)?'':'-') + number + '.' + cents);
}

/******************************************************************************************
Function Name: FormatNumber
Parameters: number
Purpose: Format the inputed number field directly at the time of entry... 
		 This is essentially the same as the 'FormatCurrency' Function minus the 'currency sign'...
*******************************************************************************************/	
function FormatNumber(numberField) {
	number = numberField.value
	number = number.toString().replace(/\$|\,/g,'');
	if(isNaN(number))
	number = "0";
	sign = (number == (number = Math.abs(number)));
	number = Math.floor(number*100+0.50000000001);
	//cents = number%100;
	number = Math.floor(number/100).toString();
	//if(cents<10)
	//cents = "0" + cents;
	for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
	number = number.substring(0,number.length-(4*i+3)) + ',' + number.substring(number.length-(4*i+3));
	numberField.value = (((sign)?'':'-') + number);
	//return (((sign)?'':'-') + number + '.' + cents);
}
