var aryRequired = new Array();
var intArrayCount = 0;

function defineRequired(strRule,strArgs){ 
    var objRequired = new Object();	
		
	objRequired.Rule = strRule;
	objRequired.Args = strArgs;
	             
	aryRequired[intArrayCount] = objRequired;
	intArrayCount++;	
}

function checkForm(objForm){
	for (var i = 0; i < aryRequired.length; i++) {

		//rule 1 - text field validation
		if(aryRequired[i].Rule == '1'){

			var sArgs = aryRequired[i].Args.split('|');

			var ElementName = sArgs[0];			
			var IsRequired = sArgs[1];
			var ValidationRule = sArgs[2];	

			var objElement = eval("objForm." + ElementName);
			
			if(!objElement) continue;		

			if(IsRequired == 'T'){
				if (objElement.value == '') {
					alert(getLabel('Field_Required'));
					objElement.focus();
					return false;
				}
			}

			if(ValidationRule != ''){
				if (objElement.value != '') {			
					switch (ValidationRule) {
						case "alphanumeric":													
							if(!check_alphanumeric(objElement.value)){
								alert(getLabel('alphanumeric'));
								objElement.focus();
								objElement.select();
								return false;
							}
							break;

						case "alphanumericspace":													
							if(!check_alphanumericspace(objElement.value)){
								alert(getLabel('alphanumericspace'));
								objElement.focus();
								objElement.select();
								return false;
							}
							break;

						case "alphanumericspacehyphen":
							if(!check_alphanumericspacehyphen(objElement.value)){
								alert(getLabel('alphanumericspacehyphen'));
								objElement.focus();
								objElement.select();
								return false;
							}
							break;

						case "alphanumericunderscore":													
							if(!check_alphanumericunderscore(objElement.value)){
								alert(getLabel('alphanumericunderscore'));
								objElement.focus();
								objElement.select();
								return false;
							}
							break;

						case "alphanumericcomma":													
							if(!check_alphanumericcomma(objElement.value)){
								alert(getLabel('alphanumericcomma'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "numeric":													
							if(!check_numeric(objElement.value)){
								alert(getLabel('numeric'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "numericdecimal":													
							if(!check_numericdecimal(objElement.value)){
								alert(getLabel('numericdecimal'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "email":													
							if(!check_email(objElement.value)){
								alert(getLabel('email'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "phonenumber":													
							if(!check_phonenumber(objElement.value)){
								alert(getLabel('phonenumber'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "datemmddyyyy":													
							if(!check_datemmddyyyy(objElement)){
								alert(getLabel('datemmddyyyy'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "trailingbackslashreq":
							if(!check_trailingbackslashreq(objElement.value)){
								alert(getLabel('trailingbackslashreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "trailingbackslashorfrontslashreq":
							if(!check_trailingbackslashfrontorslashreq(objElement.value)){
								alert(getLabel('trailingbackslashfrontorslashreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "trailingbackslashnotreq":
							if(!check_trailingbackslashnotreq(objElement.value)){
									alert(getLabel('trailingbackslashnotreq'));
									objElement.focus();
									objElement.select();
									return false;
							}						
							break;

						case "trailingfrontslashreq":
							if(!check_trailingfrontslashreq(objElement.value)){
								alert(getLabel('trailingfrontslashreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "trailingfrontslashnotreq":
							if(!check_trailingfrontslashreq(objElement.value)){
								alert(getLabel('trailingfrontslashreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "httprequired":
							if(!check_httprequired(objElement.value)){
								alert(getLabel('httprequired'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "httpnotrequired":
							if(!check_httpnotrequired(objElement.value)){
								alert(getLabel('httpnotrequired'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "leadingfrontslashreq":
							if(!check_leadingfrontslashreq(objElement.value)){
								alert(getLabel('leadingfrontslashreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

						case "leadingfrontslashnotreq":
							if(!check_leadingfrontslashreq(objElement.value)){
								alert(getLabel('leadingfrontslashnotreq'));
								objElement.focus();
								objElement.select();
								return false;
							}						
							break;

					}					
				}
			}
						
		}//rule 1		

		//dependent fields
		if(aryRequired[i].Rule == '2'){
		
			var sArgs = aryRequired[i].Args.split('|');

			var ElementName = sArgs[0];			
			var IsRequired = sArgs[1];
			var DependentField = sArgs[2];
			var DependentFieldRule = sArgs[3];	
			
			var objElement1 = eval("objForm." + ElementName);
			var objElement2 = eval("objForm." + DependentField);										

			if(IsRequired =='T'){ 
				if (objElement1.value == '') {
					alert(getLabel('Enter_A_Value'));
					objElement1.focus();
					objElement1.select();					
					return false;
				}
			}

			if(DependentFieldRule != ''){
				switch (DependentFieldRule) {
					case "numericgreaterthanorequal":													
						if(!check_numericgreaterthanorequal(objElement1.value,objElement2.value)){
							alert(getLabel('numericgreaterthanorequal'));								
							objElement1.focus();
							objElement1.select();
							return false;
						}
						break;

					case "passwordmatch":													
						if(!check_passwordmatch(objElement1.value,objElement2.value)){
							alert(getLabel('passwordmatch'));								
							objElement1.focus();
							objElement1.select();
							return false;
						}
						break;

				}									
			}
						
		}//rule 2		
			
	}//for
	
	return true;

}

function check_alphanumeric(strVal){
	var sRegExp = /[^a-z\d]/i;

	if(sRegExp.test(strVal) == true){		
		return false;
	}						
	return true;

}

function check_alphanumericperiod(strVal){
	var sRegExp = /[^a-z\d.]/i;

	if(sRegExp.test(strVal) == true){		
		return false;
	}						
	return true;

}

function check_alphanumericspace(strVal){
	var sRegExp = /[^a-z\d ]/i;

	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_alphanumericspacehyphen(strVal){
	var sRegExp = /[^a-z\d\- ]/i;

	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_alphanumericunderscore(strVal){
	var sRegExp = /[^a-z\d\_]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_alphanumericcomma(strVal){
	var sRegExp = /[^a-z\d,]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_numeric(strVal){
	var sRegExp = /[^\d]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_numericdecimal(strVal){
	var sRegExp = /[^\d\.]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;

}

function check_email(strVal){
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";

	var regex = new RegExp(emailReg);
    return regex.test(strVal);
}

function check_phonenumber(strVal) {
	var sRegExp = /[^\d\-()\. ]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;
}

function check_fileorfoldername(strVal) {
	var sRegExp = /[^a-z\d\-\_. ]/i;
							
	if(sRegExp.test(strVal) == true){		
		return false;
	}						

	return true;
}


function check_datemmddyyyy(objField) {
  //if(strValue.length != 10){
  // 	   return false; 
  //}

  var strValue = objField.value;
  
  strValue = xreplace(strValue, '-', '/');
  
  var reg = /^\d{1,2}(\/)\d{1,2}(\/)\d{2,4}$/

  var objRegExp = new RegExp(reg);

  //check to see if in correct format
  if(!objRegExp.test(strValue)){
   	   return false; 
  }
  else{        
	
    var strSeparator='/';

    var arrayDate = strValue.split(strSeparator);
	
	if(arrayDate[0].length == 1){
		arrayDate[0] = '0' + arrayDate[0];
	} 
	
	if(arrayDate[1].length == 1){
		arrayDate[1] = '0' + arrayDate[1];
	} 
	
	if(arrayDate[2].length == 2){
		arrayDate[2] = '20' + arrayDate[2];
	} 
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31};
						
    var intDay = parseInt(arrayDate[1],10);

	var intMonth = parseInt(arrayDate[0],10);
	
	//alert(intDay);
	
	//leap year stuff
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
	   
	     objField.value = arrayDate[0] + '/' + arrayDate[1] + '/' + arrayDate[2];   	
          return true; 
    }
	else{
	    //all other months
	    if(arrayLookup[arrayDate[0]] != null) {
	      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0){
		  
		  	objField.value = arrayDate[0] + '/' + arrayDate[1] + '/' + arrayDate[2];		  	
	        return true; 
		  }		
	    }
	}	      
  	
  }  
  return false; 
}

function check_numericgreaterthanorequal(str1,str2) {
	if(!check_numeric(str1)){
		return false;
	}	

	if(!check_numeric(str2)){
		return false;
	}	

	if (str1 > str2){
		return false;	
	}
	return true;
}

function check_passwordmatch(str1,str2) {
	if (str1 == str2){
		return true;	
	}
	return false;
}

function check_trailingbackslashreq(strVal) {
	if (iqRight(strVal,1) == '\\'){
		return true;	
	}
	
	return false;
}

function check_trailingbackslashfrontorslashreq(strVal) {
	if (iqRight(strVal,1) == '\\' || iqRight(strVal,1) == '/'){
		return true;	
	}
	
	return false;
}
function check_trailingbackslashnotreq(strVal) {
	if (iqRight(strVal,1) == '\\'){
		return false;	
	}
	
	return true;
}

function check_trailingfrontslashreq(strVal) {
	if (iqRight(strVal,1) == '/'){
		return false;	
	}
	
	return true;
}

function check_trailingfrontslashnotreq(strVal){
	if (iqRight(strVal,1) == '/'){
		return false;	
	}
	
	return true;
}

function check_httprequired(strVal) {
	if (iqLeft(strVal,7) == 'http://'){
		return true;	
	}
	
	return false;
}

function check_httpnotrequired(strVal) {
	if (iqLeft(strVal,4) == 'http://'){
		return false;	
	}
	
	return true;
}


function check_leadingfrontslashreq(strVal) {
	if (iqLeft(strVal,1) == '/'){
		return true;	
	}
	
	return false;
}

function check_leadingfrontslashnotreq(strVal) {
	if (iqLeft(strVal,1) == '/'){
		return false;	
	}
	
	return true;
}



//js replace only does first occurence -
// use this to do all
function xreplace(checkMe,toberep,repwith){
	var temp = checkMe;
	
	var i = temp.indexOf(toberep);
	
	while(i > -1)	
	{
	
		temp = temp.replace(toberep, repwith);
		
		i = temp.indexOf(toberep, i + repwith.length + 1);
		
	}
	
	return temp;

}

//use this to limit length of text area
//you then need to add: maxlength="512" onkeyup="return ismaxlength(this)"
// to the text area itself (even though there is no 'maxlength'
function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

//prevent enter key submitting a form when the focus
// is on a textbox
function noenter() {
  return !(window.event && window.event.keyCode == 13); 
 }