// JavaScript Document
function ChangeState(focusfield,disfirstfield,dissecfield){
 	if(focusfield.disabled="disabled"){
		focusfield.disabled="";
   	}
   	disfirstfield.disabled="disabled";
   	dissecfield.disabled="disabled";
   	focusfield.focus();	 
}
 

function Formvalidation(frmObj,fieldId,isNull,fieldValidaion,lengthvalidation,Msg){
   	var controlObj,flag,rt;  
   	controlObj=getObject(fieldId);   
   	//Null validation 
	if(isNull=="no"){
		flag=checkNull(controlObj,Msg);
		if((flag=="false")||(flag==false)){
			controlObj.focus();
		 	return false;		   
		}
	}
	// length Validation
  	if(lengthvalidation!=""){
	   	flag=checkLength(lengthvalidation,controlObj,Msg)
	   	if((flag=="false")||(flag==false)){
	 		controlObj.focus();
			return false;
	   	}
   	}
	// other fieldvalidation
	if (fieldValidaion!=""){
  		flag=validateValue(fieldValidaion,controlObj,Msg)
	 	if((flag=="false")||(flag==false)){
			controlObj.focus();
			return false;
	 	}
	}
	return true;
}

function checkLength(length,controlObj,Msg){
	if (controlObj.value.length > length){
		alert(Msg +" Value Should not More then "+length )
	   	return false;
   	}
}
function checkNull(field,alerttxt){
// with (field)
//alert(field.name);
val=removeEnter(field.value);
//if (field.value==null || trim(field.value," ")=="" || trim(field.value,"\n")==""){
if (val==null || trim(val," ")==""){
alert("Please Fill "+alerttxt);
var aa;
aa="false";
return aa;
}
}

function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function removeEnter(val) {
return val.replace(/\r\n|\n|\r/g, " ");
}


function getObject(fieldId){
	var cntrlobj
	cntrlobj=document.getElementById(fieldId)
	return cntrlobj;
}


function validateValue(fieldValidaion,controlObj,Msg){
	switch(fieldValidaion){
		case "char":
	    	flag=checkchar(controlObj,Msg)
			if((flag=="false")||(flag==false))
	 			return false;
			break; 
	 	case "email":
	    	flag=validate_email(controlObj,Msg)
			if((flag=="false")||(flag==false))
	 			return false;
			break;	
		case "numeric":
	    	flag=checknumeric(controlObj,Msg)
			if((flag=="false")||(flag==false))
            	return false;
       		break;
	 	case "date":
			 flag=isDate1(controlObj)
				 if((flag=="false")||(flag==false))
				 {
					return false;
					Msg="Invlaid Data Value"
				 }
				break;					
	}
}

function validate_email(field,alerttxt){
	with (field){
		//var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		var ok = "1234567890qwertyuiopasdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for(i=0; i < value.length ;i++)
		{
			if(ok.indexOf(value.charAt(i))<0)
			{
				alert("Not a valid e-mail address!");
				return false;
			}
		}
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2){
			alert("Not a valid e-mail address!");
			return false;
		}
	}
}

function checknumeric(field,alerttxt){
	if(isNaN(field.value)){
	    alert(alerttxt+" Must be Numeric");
		return false;
   	}
	else{
		if(field.value>0)
		{
			return true;
		}
		else
		{
			alert(alerttxt+" Must be greater than zero");
			return false;
		}
	}
			  
}

function checkchar(field,alerttxt){
	if(!(isNaN(field.value))){
		alert(alerttxt+" Should not Numeric");
	  	return false;
  	}  
}

//function submitForm(formObj,formAction,formMethod,Haction){ 
//	var formObje
//	formObje=document.getElementById(formObj)
//	//alert(formObje)
//	formObje.Haction.value=Haction
//	alert(Haction + " " + formObje.Haction.value);
//	formObje.action=formAction;
//	formObje.method=formMethod;
//	// alert(formObj.method)
//	formObje.submit(); 
//}

function getLength(objId,count){
	var userInput;
  	userInput=trim(objId.value);
  	if (userInput.length > count){
		alert("Can't be more than "+ count + " character ");
		objId.value=objId.value.substring(0,count)
		return false;
	}
	else
	{
		//objId.value=objId.value.substring(0,userInput.length);
		return true;
	}
}

function setOtherValue(obj,label){
	var org;
	if(obj.value.toLowerCase()=="other")
	{
		org=prompt("Please Specify the Name Of "+label+ ' State','');
		if(org==null || org=="" || org.length <1)
		{
			obj.options[0].selected=true
		}
		else
		{
			obj.options[0].text=org;
			obj.options[0].value=org;
			obj.options[0].selected=true;
		}
	}
}

var keybDecimal = new keybEdit('01234567890.','Decimal input only.');
var keybDecimalNM = new keybEdit('01234567890.');
var keybNumeric = new keybEdit('01234567890','Numeric input only.');
var keybNumericNM = new keybEdit('01234567890');
var keybDecimalMinus = new keybEdit('01234567890.-+','Allowed only Signed Numeric input.');

function keybEdit(strValid, strMsg) {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to be a constructor for
						the keybEdit object.  keybEdit objects are used by the
						function editKeyBoard to determine which keystrokes are
						valid for form objects.  In addition, if an error occurs,
						they provide the error message.
						
						Please note that the strValid is converted to both
						upper and lower case by this constructor.  Also, that
						the error message is prefixed with 'Error:'.
						
						The properties for this object are the following:
							valid	=	Valid input characters
							message	=	Error message
							
						The methods for this object are the following:
							getValid()	=	Returns a string containing valid
											characters.
							getMessage()=	Returns a string containing the
											error message.

		Update Date:	Programmer:			Description:
	*/

	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getValid method
						for the keybEdit object.  Please note that most of the
						following logic is for handling numeric keypad input.

		Update Date:		Programmer:			Description:
	*/

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getMessage method
						for the keybEdit object.

		Update Date:	Programmer:			Description:
	*/
	
		return this.message;
	}
}

function editKeyBoard(objForm, objKeyb,evn) {
/*	Function:		editKeyBoard
	Creation Date:	October 11, 2001
	Programmer:		Edmond Woychowsky
	Purpose:		The purpose of this function is to edit edit keyboard input
					to determine if the keystrokes are valid.

	Update Date:		Programmer:			Description:
*/
	var keyC
	//alert(navigator.appName)
	
	if (!evn.keyCode){keyC=evn.which}
	else{keyC=evn.keyCode}
	if (evn.charCode==0){return true;}
	strWork = objKeyb.getValid();
	strMsg = '';							// Error message
	blnValidChar = false;					// Valid character flag
	// Part 1: Validate input
	if(!blnValidChar)
		for(i=0;i < strWork.length;i++)
			if(keyC == strWork.charCodeAt(i)) {
				blnValidChar = true;
				break;
			}
	blnOccurAgain=scount(objForm.value,".");
	if(keyC == strWork.charCodeAt(11))
		blnOccurAgain++;
		
	// Part 2: Build error message
	if(!blnValidChar ||  blnOccurAgain>1) 
	{
		if(objKeyb.getMessage().toString().length != 0)
			alert('Error: ' + objKeyb.getMessage());
		return  false;		// Clear invalid character
		objForm.focus();						// Set focus
	}
}
function scount(str,strfind){
	var c = 0;
	for (var i=0;i<str.length;i++) {
	if (strfind == str.substr(i,strfind.length))
		c++;
	}
	return c; 
}
var stateValues = new Array();	

function CheckCountryOnload(objValue,stcomboname)
{
	var bExistOther=false;
	var i,j;
	comboobj=document.getElementById(stcomboname);
	if (objValue.toLowerCase()!="us")
	{		
		for(i=comboobj.options.length-1,j=0;i>0;i--){
			if(comboobj.options[i].text.toLowerCase()!="other" || comboobj.options[i].text.toLowerCase()!="select")
			{
				stateValues[j]=comboobj.options[i].text + "|" + comboobj.options[i].value;
			}
			comboobj.remove(i);
			j++;
		}		
		addOption(comboobj,"Other","Other");
		comboobj.options[0].selected=true;
	}
}
function CheckCountry(objValue,comboobj)
{
	var i,j;
	if (objValue.toLowerCase()!="us")
	{
		for(i=comboobj.options.length-1,j=0;i>=0;i--)
		{
			if(comboobj.options[i].text.toLowerCase()!="other" || comboobj.options[i].text.toLowerCase()!="select")
			{
				stateValues[j]=comboobj.options[i].text + "|" + comboobj.options[i].value;
			}
			comboobj.remove(i);
			j++;
		}
		
/*		for(i=comboobj.options.length-1,j=0;i>=1;i--)
		{
			if(comboobj.options[i].text.toLowerCase()!="other")
			{
				stateValues[j]=comboobj.options[i].text + "|" + comboobj.options[i].value
				comboobj.remove(i);
				j++;
			}
		}
*/
		addOption(comboobj,"Select","");
		addOption(comboobj,"Other","Other");
		comboobj.options[comboobj.options.length-1].selected=true;
		setOtherValue(comboobj,"Other");
	}
	else
	{
		for(i=comboobj.options.length-1;i>=0;i--)
			comboobj.remove(i);
		addOption(comboobj,"Select","");
		for(i=stateValues.length-1;i>=1;i--)
		{
			var comt=new Array();				
			comt=stateValues[i].split("|")			
			if (comt[0].toLowerCase()!="select"){addOption(comboobj,comt[0],comt[1]);}
		}	
	}
}

function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
function isDate(DateField) 
{
	var dateStr = DateField.value;
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) 
	{
	alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	DateField.focus();
    DateField.select();
	return false;
	}
	
	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
	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; // date is valid
}

function ValidateDate(CtrlSDate,CtrlEDate)
{
	var SDate = document.getElementById(CtrlSDate).value;    	
	var EDate =  document.getElementById(CtrlEDate).value;
	//alert(SDate);   
	//alert(EDate);			  
	var alertReason1 =  'To Date must be greater than or equal to  From Date.' 
	var alertReason2 =  'To Date can not be less than Current Date.';
	
	var endDate = new Date(EDate);    	
	var startDate= new Date(SDate);
	 
	if(SDate != '' && EDate != '' && startDate > endDate)
	{
		alert(alertReason1);
		//document.getElementById(CtrlEDate).value = "";
		return false;
	}
	else if(SDate == '')	
	{
		alert("Please enter From Date");
		return false;
	}
	else if(EDate == '')	
	{
		alert("Please enter To Date");
		return false;
	}	    
	else
	{
		return true;
	}
}
