var VALIDATIONLABELS = {
	check_file_overwrite:"The file already exists and will be overwriten.",
	field_required:"The field is required.",
	check_numericdecimal:"The field can only contain numbers and decimals.",
	check_emailto:"The field may only email addresses and semi-colon",
	check_numeric:"The field can only contain numbers.",
	check_file_exists:"The file already exists.",
	check_datemmddyyyy:"The field is not a valid date.",
	check_alphanumericcomma:"The field can only contain numbers, letters and commas.",
	check_phonenumber:"The field is not a valid phone number.",
	check_alphanumericunderscore:"The field can only contain numbers, letters and underscores.",
	check_passwordmatch:"Your passwords do not match.",
	check_numericgreaterthanorequal:"The field must be less then or equal the other field and may only contains numbers.",
	check_dir_exists:"The folder already exists.",
	time_greater_than:"The start time must be before the end time.",
	enter_a_value:"The field is required.",
	enter_a_date:"Please enter a date.",
	date_greater_than:"The start date must be before the end date.",
	check_email:"The field is not a valid email address.",
	check_alphanumeric:"The field can only contain numbers and letters.",
	check_alphanumericspace:"The field can only contain numbers, letters and spaces.",
	check_zipcode:"The field is not a valid zip code."
} 

if(typeof window['iQ'] == "undefined"){
	window['iQ'] = new Object();
}

window.iQ['Form'] = new Object();

iQ.Form.Checker = function(objFormRules){return{
	FormRules : objFormRules,
	doLoad: function(sAction){
		for(var i = 0; i<this.FormRules.length; i++){
			var currForm = this.FormRules[i].FormName;
			var currField = this.FormRules[i].FieldName;
			var curRequired = this.FormRules[i].Required

			var objLI = iQ.Utils.getFormObject(currForm,currField,'li');

			if(curRequired){
				var sStatusHTML = this.getStatusHTML('required',currField);
			}else{
				var sStatusHTML = this.getStatusHTML('',currField);
			}

			objLI.innerHTML += sStatusHTML;
		}

		return true;
	},

	doClearAllStatus: function(){
		for(var i = 0; i<this.FormRules.length; i++){
			var currForm = this.FormRules[i].FormName;
			var currField = this.FormRules[i].FieldName;
			var curRequired = this.FormRules[i].Required;
			var curRule = this.FormRules[i].Rule;
			
			if(!curRequired){
				this.setStatus(currForm,currField,'','');
			}else{
				this.setStatus(currForm,currField,'required','');
			}
		}

		return true;
	},

	doCheckAllFields : function(){
		for(var i = 0; i<this.FormRules.length; i++){
			var currForm = this.FormRules[i].FormName;
			var currField = this.FormRules[i].FieldName;
			var curRequired = this.FormRules[i].Required;
			var curRule = this.FormRules[i].Rule;

			var ret = this.doValidation(currForm,currField,curRequired,curRule);
			
			if(!ret.Passed){
				this.setStatus(currForm,currField,'incorrect',ret.Message);
				return false;
			}else{
				this.setStatus(currForm,currField,'correct','');
			}
		}
		return true;
	},

	doCheckField : function(sForm,sField){
		for(var i = 0; i<this.FormRules.length; i++){
			var currForm = this.FormRules[i].FormName;
			var currField = this.FormRules[i].FieldName;
			var curRequired = this.FormRules[i].Required;
			var curRule = this.FormRules[i].Rule;

			if(currForm == sForm && currField == sField){
				var ret = this.doValidation(currForm,currField,curRequired,curRule);
				
				if(!ret.Passed){
					this.setStatus(currForm,currField,'incorrect',ret.Message);
				}else{
					this.setStatus(currForm,currField,'correct','');
				}

				break;
			}

		}
		return true;
	},

	doValidation : function(sFormName,sFieldName,bRequired,sRule){
		var ValueToCheck = iQ.Utils.getFieldValue(sFormName,sFieldName);
		var retVal = {Passed:true, Message:''};

		if(bRequired){
			if(ValueToCheck == ''){
				retVal = {Passed:false, Message:this.getValidationLabel('field_required')};
				return retVal;
			}
		}else{                                    
			if(ValueToCheck == ''){                                                  
				return retVal;
			}
		}

		if(sRule){
			switch (sRule) {
				case "alphanumeric":
					if(!this.check_alphanumeric(ValueToCheck)){	
						retVal = {Passed:false,Message:this.getValidationLabel('check_alphanumeric')};
					}
					break;
			
				case "alphanumericspace":													
					if(!this.check_alphanumericspace(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_alphanumericspace')};
					}
					break;
			
				case "alphanumericspacehyphen":
					if(!this.check_alphanumericspacehyphen(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_alphanumericspacehyphen')};
					}
					break;
			
				case "alphanumericunderscore":													
					if(!this.check_alphanumericunderscore(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_alphanumericunderscore')};
					}
					break;
			
				case "alphanumericcomma":													
					if(!this.check_alphanumericcomma(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_alphanumericcomma')};
					}
					break;
			
				case "numeric":
					if(!this.check_numeric(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_numeric')};
					}
					break;

				case "zipcode":													
					if(!this.check_zipcode(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_zipcode')};
					}
					break;
					
				case "numericdecimal":													
					if(!this.check_numericdecimal(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_numericdecimal')};
					}
					break;
			
				case "email":													
					if(!this.check_email(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_email')};
					}
					break;
			
				case "phonenumber":													
					if(!this.check_phonenumber(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_phonenumber')};
					}
					break;

				case "datemmddyyyy":													
					if(!this.check_datemmddyyyy(objElement)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_datemmddyyyy')};
					}
					break;
			
				case "trailingbackslashreq":
					if(!this.check_trailingbackslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_trailingbackslashreq')};
					}
					break;
			
				case "trailingbackslashorfrontslashreq":
					if(!this.check_trailingbackslashfrontorslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_trailingbackslashfrontorslashreq')};
					}
					break;
			
				case "trailingbackslashnotreq":
					if(!this.check_trailingbackslashnotreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_trailingbackslashnotreq')};
					}
					break;
			
				case "trailingfrontslashreq":
					if(!this.check_trailingfrontslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_trailingfrontslashreq')};
					}
					break;
			
				case "trailingfrontslashnotreq":
					if(!this.check_trailingfrontslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_trailingfrontslashreq')};
					}
					break;
			
				case "httprequired":
					if(!this.check_httprequired(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_httprequired')};
					}
					break;
			
				case "httpnotrequired":
					if(!this.check_httpnotrequired(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_httpnotrequired')};
					}
					break;
			
				case "leadingfrontslashreq":
					if(!this.check_leadingfrontslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_leadingfrontslashreq')};
					}
					break;
			
				case "leadingfrontslashnotreq":
					if(!this.check_leadingfrontslashreq(ValueToCheck)){
						retVal = {Passed:false,Message:this.getValidationLabel('check_leadingfrontslashreq')};
					}
					break;
			}
		}

		return retVal;
	},

	check_alphanumeric : function(strVal){
		var sRegExp = /[^a-z\d]/i;
	
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
		return true;
	},
	
	check_alphanumericspace : function(strVal){
		var sRegExp = /[^a-z\d ]/i;
	
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},
	
	check_alphanumericspacehyphen : function(strVal){
		var sRegExp = /[^a-z\d\- ]/i;
	
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},
	
	check_alphanumericunderscore : function(strVal){
		var sRegExp = /[^a-z\d\_]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},
	
	check_alphanumericcomma : function(strVal){
		var sRegExp = /[^a-z\d,]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},
	
	check_numeric : function(strVal){
		var sRegExp = /[^\d]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},

	check_zipcode : function(strVal){
		var sRegExp = /[^\d]/i;
								
		if(sRegExp.test(strVal) == true || strVal.length != 5){		
			return false;
		}						

		return true;
	},

	check_numericdecimal : function(strVal){
		var sRegExp = /[^\d\.]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	
	},
	
	check_email : function(strVal){
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	
		var regex = new RegExp(emailReg);
		return regex.test(strVal);
	},
	
	check_phonenumber : function(strVal){
		var sRegExp = /[^\d\-()\. ]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	},
	
	check_fileorfoldername : function(strVal){
		var sRegExp = /[^a-z\d\-\_. ]/i;
								
		if(sRegExp.test(strVal) == true){		
			return false;
		}						
	
		return true;
	},
	
	check_datemmddyyyy : function(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; 
	},
	
	check_numericgreaterthanorequal : function(str1,str2){
		if(!this.check_numeric(str1)){
			return false;
		}	
	
		if(!this.check_numeric(str2)){
			return false;
		}	
	
		if(str1 > str2){
			return false;	
		}
		return true;
	},
	
	check_passwordmatch : function(str1,str2){
		if(str1 == str2){
			return true;	
		}
		return false;
	},
	
	check_trailingbackslashreq : function(strVal){
		if(iQ.Utils.Right(strVal,1) == '\\'){
			return true;	
		}
		
		return false;
	},
	
	check_trailingbackslashfrontorslashreq : function(strVal){
		if(iQ.Utils.Right(strVal,1) == '\\' || iQ.Utils.Right(strVal,1) == '/'){
			return true;	
		}
		
		return false;
	},
	
	check_trailingbackslashnotreq : function(strVal){
		if(iQ.Utils.Right(strVal,1) == '\\'){
			return false;	
		}
		
		return true;
	},
	
	check_trailingfrontslashreq : function(strVal){
		if(iQ.Utils.Right(strVal,1) == '/'){
			return false;	
		}
		
		return true;
	},
	
	check_trailingfrontslashnotreq : function(strVal){
		if(iQ.Utils.Right(strVal,1) == '/'){
			return false;	
		}
		
		return true;
	},
	
	check_httprequired : function(strVal){
		if(iQ.Utils.Left(strVal,7) == 'http://'){
			return true;	
		}
		
		return false;
	},
	
	check_httpnotrequired : function(strVal){
		if(iQ.Utils.Left(strVal,4) == 'http://'){
			return false;	
		}
		
		return true;
	},	
	
	check_leadingfrontslashreq : function(strVal){
		if(iQ.Utils.Left(strVal,1) == '/'){
			return true;	
		}
		
		return false;
	},
	
	check_leadingfrontslashnotreq : function(strVal){
		if(iQ.Utils.Left(strVal,1) == '/'){
			return false;	
		}
		
		return true;
	},

	getStatusHTML : function(sType,sFieldName){
		var htmlBuffer = new iQ.Utils.StringBuffer();

		if(sType.toLowerCase() == ''){
			htmlBuffer.append('<div id="' + sFieldName + '_Status" class="iQ_Form_Field_Status"><img src="/JS/iQ/assets/spacer.gif" width="16" height="16" /></div>');
		}

		if(sType.toLowerCase() == 'required'){
			htmlBuffer.append('<div id="' + sFieldName + '_Status" class="iQ_Form_Field_Status_Required"><img src="/JS/iQ/assets/spacer.gif" width="16" height="16" /></div>');
		}

		htmlBuffer.append('<div id="' + sFieldName + '_Message" class="iQ_Hidden"></div>');		

		return htmlBuffer.tostring();
	},

	setStatus : function(sFormName,sFieldName,sStatus,sMessage){
		var objStatus = iQ.Utils.getFormObject(sFormName,sFieldName + '_Status','div');
		var objMessage = iQ.Utils.getFormObject(sFormName,sFieldName + '_Message','div');

		if(sStatus == 'correct'){
			objStatus.className = 'iQ_Form_Field_Status_Correct';
		}
	
		if(sStatus == 'incorrect'){
			objStatus.className = 'iQ_Form_Field_Status_Incorrect';
		}

		if(sStatus == 'required'){
			objStatus.className = 'iQ_Form_Field_Status_Required';
		}
	
		if(sStatus == ''){
			objStatus.className = 'iQ_Form_Field_Status';
		}

		objMessage.innerHTML = sMessage;

		if(sMessage == ''){
			objMessage.className = 'iQ_Hidden';
		}else{
			objMessage.className = 'iQ_Form_Field_Status_Message';
		}	

		return true;
	},

	getValidationLabel : function(sLabel){
		if(typeof VALIDATIONLABELS == 'undefined'){
			return sLabel;
		}else{
			tLabel = 'VALIDATIONLABELS.' + sLabel.toLowerCase();
			if(typeof eval(tLabel) == 'undefined'){
				return sLabel;
			}else{
				tLabel = 'VALIDATIONLABELS.' + sLabel.toLowerCase();
				return eval(tLabel);
			}
		}
	}
}}

iQ.Form.getStatusDiv = function(sForm,sID){
	var sField =  sID + '_Status';
	var objFormElms = document.getElementsByTagName('form');

	for (var i=0; i<objFormElms.length; i++){
		var currForm = objFormElms[i];
		if(currForm.name == sForm || currForm.id == sForm){
			var test = currForm.getElementsByTagName('div')
			for (var j=0; j<test.length; j++){
				var currElm = test[j];
				if(currElm.name == sField ||currElm.id == sField){
					return currElm;
				}
			}
		}
	}
}




