//Checks whether the form input element is empty
//paramValue-Value of the Form Item
function isEmpty(paramValue)
{
	var expIsEmpty=/[^ ^\r^\n ]/;
	return  ! expIsEmpty.test(paramValue);
}

//Function checks for the Special Characters
function isValidEntity(paramValue)
{
	var expIsValidEntity=/[^a-z0-9A-Z \#\,\'\-\(\)\.\_\$\&\/]/;
  	return ! expIsValidEntity.test(paramValue);
}

//Function checks for the Spaces
function hasSpace(paramValue)
{
	var expIsValidEntity=/[ ]/;
  	return expIsValidEntity.test(paramValue);
}

//Checks whether the form input element is numeric
//paramValue-Value of the Form Item
function isNumeric(paramValue)
{
  	var expIsNumeric=/[^0-9]/;
  	return ! expIsNumeric.test(paramValue);
}

//Checks whether the form input element is float
//paramValue-Value of the Form Item
function isFloat(paramValue)
{
	var expIsFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	return expIsFloat.test(paramValue);
}

//Checks whether the form input element is a String
//paramValue-Value of the Form Item
function isChar(paramValue)
{
  	var expIsChar=/[^a-zA-Z ]/;
  	return ! expIsChar.test(paramValue);
}

//Checks whether the form input element is a valid AlphaNumeric
function isValidAlphaNumeric(paramValue)
{
  	var expisValidAlphaNumeric = /[^a-zA-Z0-9]/;
  	return ! expisValidAlphaNumeric.test(paramValue);
}

//Checks whether the form input element is a Valid Domain
//domainIn-Value of the Email Domain
function  isStandardDomain(domainIn)  
{
	/// The value to return, start out assuming invalid domain.
	var  isStandardReturn = false;

	/// Holds the last 4 characters of domain name.
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );

	/// Holds the last 3 characters of domain name.
	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );

	/// Uppercase it for comparison purposes.
	last4chars = last4chars.toUpperCase();

	/// A regular expression pattern to match country code domains.
	///  In otherwords a Dot character followed by two alphabetic characters.
	/// NOTE:  This line doesn't work at all in Opera3.5 and prevents the
	///  entire script from running!!!  BUMMER!!!
	///  It also seems to not work in Opera4.02 but only prevents
	///  the 2 letter codes from working but doesn't crash the script.
	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;


	if      ( last4chars == ".COM" ) isStandardReturn = true;
	else if ( last4chars == ".EDU" ) isStandardReturn = true;
	else if ( last4chars == ".GOV" ) isStandardReturn = true;
	else if ( last4chars == ".NET" ) isStandardReturn = true;
	else if ( last4chars == ".MIL" ) isStandardReturn = true;
	else if ( last4chars == ".ORG" ) isStandardReturn = true;

	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		isStandardReturn = true;
	return  isStandardReturn;
} 

//Checks whether the form input element is a Valid Email
//emailID-Value of the Form Item
function  isValidEmail(emailID)  
{
	//var expIsSpecialChar=/[\&\;\"\*\~\|\$\%\^\#\!\`\<\>\\\+\=\?]/
	var expIsSpecialChar=/[^a-zA-Z0-9_@.]/   			
	var expFirstCharOfEmail = /[^0-9a-zA-Z]/

	if(expIsSpecialChar.test(emailID))
	{
		return false;
	}

	/// Number of '@' chars present in input string.
	var  numAtChars;

	/// The part of input address before the '@' character.
	var  userNameIn;

	/// The part of input address after the '@' character.
	var  domainNameIn;

	/// Holds the fields of the entered address, delimitted by '@' chars.
	var  addressFields = new Array();

	/// Divide the input email address into fields.
	///  Note that the array "addressFields" will have one more element
	///  than the number of '@' signs in the input string.
	/// IE4 handles this OK with '@' as last character but NS4,4.5 and Opera 3.5 don't.
	
	addressFields = emailID.split('@');
	numAtChars = addressFields.length - 1;

	// Has to be added in the email validation 
	// This checks the dot at the last of the user name 
	// Also  this checks for the first entered character 
	// of email is a charactre or not
	//============================
	var dotPosition;
	dotPosition = addressFields[0].length-1;			
	if(addressFields[0].lastIndexOf(".") == dotPosition) 
		return false;	
	if(expFirstCharOfEmail.test(addressFields[0].substring(0,1)))	
		return false;	
	//===============================
	

	if (emailID == "") 	 		//  is EMPTY
		return false;

	else if (numAtChars  ==  0) 		//  contains no '@' character 
		return false;

	else if (numAtChars  >  1)		//  more than one '@' characters 
		return false;

	else if (addressFields[0] == "")	// has no Username before the '@' character 
		return false;

	else if (addressFields[1] == "")	// has no Domain Name after the '@' character
		return false;

	else
	{
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if (userNameIn.indexOf( " " ) != -1)		// has one or more Spaces in the Username before the '@'  					
			return false;
		else if (domainNameIn.indexOf( " " ) != -1) 	// has one or more Spaces in the Domain Name after the '@' character
			return false;
		else if (isStandardDomain( domainNameIn ) == false) 	//does not end with a '.com' style domain or two letter country code domain
			return false;

	}
	return true;           
}

//Checks whether the form input element is a Valid Date (mm/dd/yy)
//dateStr-Value of the Form Item
function isValidDate(dateStr) 
{	/*
	var expIsChar=/^[0-9]\/$/;
	alert(dateStr);
	alert(expIsChar.test(dateStr));
	if(expIsChar.test(dateStr))
	  	return ! expIsChar.test(dateStr);*/

	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/; // requires 4 digit year
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		//alert(dateStr + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (year <= 1753)
	{
		//alert("year must be greater than 1753.");
		return false;
	}	

	if (month < 1 || month > 12)
	{
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) 
	{
		//alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		//alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) 
	{	 // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	
	return true;
}

function checkDateDifference(strDate1, strDate2)
{	
	var strDate1Array =  strDate1.split("/");
	var dtDate1 = new Date(strDate1Array[2], strDate1Array[0] -1, strDate1Array[1]);
	var strDate2Array =  strDate2.split("/");
	var dtDate2 = new Date(strDate2Array[2], strDate2Array[0] -1, strDate2Array[1]);
	if(dtDate1 >= dtDate2)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//Checks the Date and returns the boolean value
//Checks whether the Given Date is not greater than the Current Date
//paramValue-Value of the Form Item
function checkDate(paramValue)
{
	var curDate=new Date();
	var dtToCompare=new Date(paramValue);
	if(dtToCompare>curDate)
	{
		return false;
	}
	return true;
}

//Decodes the Special Characters in the Query String Passed to CallInfo.asp which is then
//decoded in that file.
function encode(str)
{
	var dest="";
	var len=str.length;
   	var index=0;
   	var code=null;

   	for(var i=0;i<len;i++)
   	{
      		var ch=str.charAt(i);
      		if(ch==" ") code="%20";	
      		else if(ch=="%") code="%25";
      		else if(ch==",") code="%2C";
      		else if(ch==";") code="%3B";
      		else if(ch=="\b") code="%08";
      		else if(ch=="\t") code="%09";
      		else if(ch=="\n") code="%0A";
      		else if(ch=="\f") code="%0C";
      		else if(ch=="\r") code="%0D";
    		if(code!=null)
		{
       			dest+=str.substring(index,i) + code;
		       	index=i+1;
		       	code=null;
	       	}
   	}
	 if(index<len) dest+=str.substring(index,len);
		 return dest;
}


//Initialize the Global variable 
var checkFlag = 0;

function checkControls(oFormObj)
{
	if(checkFlag == 0)
	{
		checkAll(oFormObj);
		checkFlag = 1;
	}
	else
	{
		clearAll(oFormObj);
		checkFlag = 0;
	}		
}

function clearAll(oFormObj)
{
	for(ctr = 0;ctr< oFormObj.elements.length;ctr++)
	{
		if(oFormObj[ctr].type == 'checkbox')
		{
			oFormObj[ctr].checked = false;
		}
	}
}

function checkAll(oFormObj)
{
	for(ctr = 0;ctr< oFormObj.elements.length;ctr++)
	{
		if(oFormObj[ctr].type == 'checkbox')
		{
			oFormObj[ctr].checked = true;
		}
	}
}


function checkObjSel(oFormObj)
{
	var checked = false;

	for(ctr = 0; ctr< oFormObj.elements.length; ctr++)
	{
		if(oFormObj[ctr].type == 'checkbox')
		{
			if(oFormObj[ctr].value != -1)
			{
				if(oFormObj[ctr].checked)
					checked = true;
			}
		}
	}
		
	return checked;
}

function getCheckedControlValues(oFormObj)
{
		var strIDList = '';
		var objChk = '';
		
		for(ctr = 0;ctr< oFormObj.elements.length;ctr++)
		{
			objChk = oFormObj[ctr];

			if(objChk.type == 'checkbox')
			{
				if(objChk.checked)
				{
					if(objChk.value != -1)
					{
						strIDList = strIDList + objChk.value + ",";	
					}
				}
			}
		}

		strIDList = strIDList.substring(0, (strIDList.length -1))
		return strIDList;
}


function checkTime(stime, etime) 
{

	var stimeAM = stime.indexOf("A");
	var stimePM = stime.indexOf("P");
	var etimeAM = etime.indexOf("A");
	var etimePM = etime.indexOf("P");
	var st;
	var et;

	if( (stimeAM != undefined && stimeAM > 0 ) && (etimeAM != undefined && etimeAM > 0 )) 
	{
	   stime = stime.substring(0,stimeAM);
	   etime = etime.substring(0,etimeAM);
	   stime = stime.replace(":","");
	   etime = etime.replace(":","");


	   if( parseInt(etime, 10) > parseInt(stime, 10) ) 
	   {
		return true;
	   } 
	   else 
	   {
		return false;
	   }
 
 	 }
	 else if((stimeAM != undefined && stimeAM > 0 ) && (etimePM != undefined && etimePM > 0 ) ) 
	 {
	   	return true;
	 }
	 else if((stimePM != undefined && stimePM > 0 ) && (etimeAM != undefined && etimeAM > 0 ) ) 
	 {
	   	return false;
	 }
	 else if((stimePM != undefined && stimePM > 0 ) && (etimePM != undefined && etimePM > 0 ) ) 
	 {
	   	stime = stime.substring( 0, stimePM );
	   	etime = etime.substring( 0, etimePM );
	   	stime = stime.replace( ":", "" );
	   	etime = etime.replace( ":", "" );

		if(parseInt(stime, 10) >= 1200)
		{
			stime = parseInt(stime, 10) - 1200;
		}

		if(parseInt(etime, 10) >= 1200)
		{
			etime = parseInt(etime, 10) - 1200;
		}

	  	if( parseInt(etime, 10) > parseInt(stime, 10) ) 
		{
	    		return true;
	  	} 
	  	else 
 		{
			return false;
		}
	} 
	else if( (stimeAM > 0 || stimePM > 0 ) && (etimeAM != undefined && etimePM != undefined ) ) 
	{   
   		stime = stime.replace( ":", "" );
   		etime = etime.replace( ":", "" );
   		st = parseInt(stime,10);
   		if( st < parseInt( etime, 10 ) ) 
		{
    			return true;
   		}
		else 
		{
    			return false;
   		}
	}
	else if( (stimeAM != undefined && stimePM != undefined ) && (etimeAM > 0 || etimePM > 0) ) 
	{   
   		stime = stime.replace( ":", "" );
   		etime = etime.replace( ":", "" );
   		et = parseInt(etime,10);
   		if( et > parseInt( stime, 10) ) 
		{
    			return true;
   		}
		else 
		{
    			return false;
   		}
 	 }
	 else if( (stimeAM != undefined && stimePM != undefined ) && (etimeAM != undefined && etimePM != undefined ) ) 
	 {   
   		stime = stime.replace( ":", "" );
   		etime = etime.replace( ":", "" );

   		st = parseInt(stime, 10);
   		et = parseInt(etime, 10);

   		if( et < st ) 
		{
    			return false;
   		}
		else 
		{
    			return true;
   		}
  	}
}

function noLetters(e) 
{
	var myChar;
	var srcControl;

	if (document.all) 
	{
		e = window.event;
		srcControl = e.srcElement;

		myChar = e.keyCode;
	}
	else
	{
		if (document.layers)
		{
			srcControl = e.target;
 			myChar = e.which;
		}
	}

	if(srcControl.type == "text" || srcControl.type == "password")
	{
		if(myChar == 13)
		{
			if (document.layers)
			{
				return false;
			}
			else if (document.all)
			{
				e.returnValue = false;
			}
		}
	}

	myChar = String.fromCharCode(myChar);

	if (myChar == '=' || myChar == '~' || myChar == '%' || myChar == '<' 
		|| myChar == '>' || myChar == '#' || myChar == '$' || myChar == '^' 
		|| myChar == '&' || myChar == '(' || myChar == ')' || myChar == '\\' || myChar == '|' 
		|| myChar == '\'')  
	{
		if (document.layers)
		{
			return false;
		}
		else if (document.all)
		{
			e.returnValue = false;
		}
	}
}

function checkObjOneSel(oFormObj)
{
var checked = false;

for(ctr = 0; ctr< oFormObj.elements.length; ctr++)
{
	if(oFormObj[ctr].type == 'checkbox')
	{
		if(oFormObj[ctr].value != -1)
		{
			if(oFormObj[ctr].checked)
			{
				if(checked == true)
				{
					return !checked;
				}
				checked = true;
			}
		}
	}
}
	
return checked;
}

function sendMessage(strURL)
{
	OpenNewWindow(encode(strURL), 530, 360);
}

function isValidPasswords(paramValue)
{
	var x1 = /^[a-z\d]{6,25}$/i // only alphanumerics, and length 6-25
	var x2 = /[a-z]/i           // a letter present
	var x3 = /\d/               // a digit present	

	return x1.test(paramValue) && x2.test(paramValue) && x3.test(paramValue)  
} 

function nocontextmenu()
{
	event.cancelBubble = true, event.returnValue = false;
	return false;
}

function norightclick(e)
{
	if (window.Event)
    {
			if (e.which > 1) return false;
	}
	else if (event.button > 1)
    {
		event.cancelBubble = true, event.returnValue = false;
		return false;
	}
}

function disableRightClick()
{
	if (document.layers)
	{
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown = norightclick;
	}
	else
	{
		document.oncontextmenu = nocontextmenu;
	}
}


function noLetters1(e) 
{
	var myChar;
	var srcControl;

	if (document.all) 
	{
		e = window.event;
		srcControl = e.srcElement;

		myChar = e.keyCode;
	}
	else
	{
		if (document.layers)
		{
			srcControl = e.target;
 			myChar = e.which;
		}
	}

	if(srcControl.type == "text" || srcControl.type == "password")
	{
		if(myChar == 13)
		{
			if (document.layers)
			{
				return false;
			}
			else if (document.all)
			{
				e.returnValue = false;
			}
		}
	}

	myChar = String.fromCharCode(myChar);

	if (myChar == '~' || myChar == '%' || myChar == '<' 
		|| myChar == '>' 
		|| myChar == '\\')  
	{
		if (document.layers)
		{
			return false;
		}
		else if (document.all)
		{
			e.returnValue = false;
		}
	}
}

function isValidPhoneFax(x)
{
	var i;
	i=x.value;
	i=i.length;
	if(i<12)
	{
		return false;
	} 
	j=x.value;
	j=j.substring(0,3)
	if(!IsNumeric(j))
	{
	  	return false;
	}
	// alert(j);
	j=x.value;
	j=j.substring(4,7);
	if(!IsNumeric(j))
	{
	  	return false;
	}
	//alert(j);	
	if (i==4  || i==8)
	{	  
	  i=x.value;
	  j=i.length;
	  j=i.substring(j-1);
	  if (j!='-')
	  {
		return false;
	  }
	 }
	j=x.value;
	j=j.substring(8,12);
	if(!IsNumeric(j))
	{
	  	return false;
	}
	//alert(j);	
	 return true;
}


var NewWindow = "";
function OpenNewWindow(url, width, height, name)
{
	
	//opens a 640 x 480 window, centered..
	var window_width = width;
	var window_height = height;

	if(window_width == null)
	{
		window_width = 640;
	}
	if(window_height == null)
	{
		window_height = 480;
	}
	if(name == null)
	{
		name = "";
	}

	var window_top = (screen.height / 2) - (window_height / 2);
	var window_left = (screen.width / 2) - (window_width / 2);
	
	NewWindow = ""
	NewWindow = window.open(url,name,'status=no,width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',resizable=yes,scrollbars=yes');
	NewWindow.focus();

	return NewWindow;
}
