

//JAVASCRIPT VALIDATION FUNCTION!

// USAGE:
// <form onsubmit="
//	this.ADDRESS1.required=true;
//	this.FULL_NAME.required=true;
//	this.FULL_NAME.displayName='Full Name';
//	this.CITY.required=true;
//	this.CITY.displayName='City';
//	this.STATE.required=true;
//	this.STATE.displayName='State';
//	this.EMAIL.required=true; 
//	this.EMAIL.formatcheck='email'; 
//	this.EMAIL.displayName='Email Address';
//	this.ZIP5.required=true;
//	this.ZIP5.displayName='First 5 digits of your zip code';
//	this.ZIP5.numeric=true; 
//	this.ZIP5.minLength=5; 
//	this.ZIP5.maxLength=5;
//	return validate(this);">
//
//
// <input type=submit onclick="commitValidate=true;">

//Globals
var submsg;
var commitValidate=false;

function delCheck(error_txt)
  {
	
  	if (confirm('Are you sure you want to delete ' + error_txt + '?'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isEmpty(s){

	for(var i=0;i<s.length;i++){
		var c = s.charAt(i);
		if((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		return true;
}

function setErrorMsg(txt){
submsg=txt;
} 




function dateValidate(dateValue){
	var pattern = /[0-1]{0,1}\d\/[0-3]{0,1}\d\/20[0-9][0-9]/;
	
	// [0-1]{0,1} : 0 or 1  - match 0 or 1 occurences
	// \d 				 : any digit [0-9]
	// \/   			 : literal '/'
	// [0-3]{0,1} : 0 to 3  - match 0 or 1 occurences
	//  \d         : any digit [0-9]
	// etc etc

  if (pattern.test(dateVal)) {
	 return true;
	}else{
	 return false;
	}

}

function validate(f){

	//This is how we control whether the validate is actually done in story server
	if(commitValidate == true){
		commitValidate = false;


	var msg = "";
	var show_name;
	var empty = "";
	var errors = "";
	
	//Set up associative array objects for month names
	var monthName= new Object();
	monthName.jan=1;
	monthName.january=1;
	monthName.feb=2;
	monthName.february=2;
	monthName.mar=3;
	monthName.march=3;
	monthName.apr=4;
	monthName.april=4;
	monthName.may=5;
	monthName.jun=6;
	monthName.june=6;
	monthName.jul=7;
	monthName.july=7;
	monthName.aug=8;
	monthName.august=8;
	monthName.sept=9;
	monthName.september=9;
	monthName.oct=10;
	monthName.october=10;
	monthName.nov=11;
	monthName.november=11;
	monthName.dec=12;
	monthName.december=12;

	//Loop through all elements checking if validation variable are set and
	//performing the appropriate validations.

	for(var c=0;c<f.length;c++){
		var e = f.elements[c];

			//use display name if available
			if(e.displayName != null){
				show_name=e.displayName;
				}else{
				show_name=e.name;
				}
				

		//handle text, textarea, and password elements
		if((e.type=="text")||(e.type=="textarea")||(e.type=="password")||(e.type=="hidden")){
			//if required check for input
			if(e.required){
				if((e.value==null)||(e.value=="")||isEmpty(e.value)){
					empty+="\n      "+show_name;
					continue;
					}}

			//handle other errors
			if(e.minLength != null || (e.maxLength != null)){
				var l=e.value.length;
				if(((e.minLength != null) && (l < e.minLength)) ||
					((e.maxLength != null  && e.maxLength != -1) && (l > e.maxLength))) {
					errors += "- The field "+show_name+" must have a length";
					if(e.minLength != null)
						errors += " of at least "+e.minLength;
					if(e.maxLength != null && e.minLength != null)
						errors += " but less than "+e.maxLength;
					else if (e.maxLength != null && e.maxLength != -1)
						errors += " that is less than "+e.maxLength;
					errors += " characters.\n";

					}

			}




			
			if((e.numeric || (e.min != null) || (e.max != null))&&
			(e.value != null && e.value !="")){
				var v = parseFloat(e.value);
				if(isNaN(v) ||
				((e.min != null) && (v < e.min)) ||
				((e.max != null) && (v > e.max))) {
				errors += "- The field "+show_name+" must be a number";
				if(e.min != null)
					errors += " that is greater than "+e.min;
				if(e.max != null && e.min !=null)
					errors += " and less than "+e.max;
				else if (e.max != null)
					errors += " that is less than "+e.max;
				errors +=".\n";
				}
			}

			if(e.type == "password" && e.confirm != null){
			var ne = f.elements[c+1];
				  
			  if(e.value != ne.value) errors +="- The password and confirm password do not match.\n";
			  
			  
			
			}
			
			if(e.formatcheck != null && (e.value != null) && (e.value != "") && (e.type != "password")){
				
				// Email format checks for valid email address
				// checks for "@" and "." and their positioning
				if(e.formatcheck.toLowerCase() == "email"){
					var at = e.value.indexOf("@");
					if(at > 0) var dot = e.value.lastIndexOf(".");
					
					if(at<1 || (dot<=at+1) || (dot>=(e.value.length-2))){
					errors += "- The field "+show_name+" is an email field and must be in the format (JSmith@Provider.com).\n";
					}
					}
				//Zipany format = NNNNN or NNNNN-NNNN	
				if(e.formatcheck.toLowerCase() == "zipany"){
					var num;
					var err = false;
					var punct = e.value.indexOf("-");
					if(punct != 5 && punct>0) err=true;
					if(!err){
						if(punct==5) num = parseInt(e.value.substr(0,5) + e.value.substr(6,4));
						if(punct<0) num =parseInt(e.value);
						if(num.toString().length != 5 && num.toString().length != 9) err=true;
					}
					if(err){
					errors += "- The field "+show_name+" is a Zip Code field and must be in the format (NNNNN or NNNNN-NNNN).\n";
					}
				
				}
				
				//Zip4 format = NNNNN-NNNN
				if(e.formatcheck.toLowerCase() == "zip4"){
					var num;
					var err = false;
					var punct = e.value.indexOf("-");
					if(punct != 5 && punct>0) err=true;
					if(!err){
						if(punct==5) num = parseInt(e.value.substr(0,5) + e.value.substr(6,4));
						if(punct<0) num =parseInt(e.value);
						if(num.toString().length != 9) err=true;
					}
					if(err){
					errors += "- The field "+show_name+" is a Zip+4 field and must be in the format (NNNNN-NNNN).\n";
					}
				}
				// Zipshort format = NNNNN
				if(e.formatcheck.toLowerCase() == "zipshort"){
					var num;
					var err = false;
					var punct = e.value.indexOf("-");
					if(punct>0) err=true;
					if(!err){
						num =parseInt(e.value);
						if(num.toString().length != 5) err=true;
					}
					if(err){
					errors += "- The field "+show_name+" is a Zip Code field and must be in the format (NNNNN).\n";
					}			
				}
				//Validates standard phone formats
				// (NNN)NNN-NNNN or NNN-NNN-NNNN or NNNNNNNNNN or +NNNNNNNNNNNNNNNNNN..
				if(e.formatcheck.toLowerCase() == "phone"){
					// no Reg Exps were used to keep the validation compliant
					// with older browsers
					
					var err=false;
					var temp="";
					var phone = e.value;
					
					//get locations of any possible punctuation and space
					var ip = e.value.indexOf("+");
					var op = e.value.indexOf("(");
					var cp = e.value.indexOf(")");
					var fd = e.value.indexOf("-");
					var ld = e.value.lastIndexOf("-");
					var sp = e.value.indexOf(" ");
					//strip out numbers
					
					for(var i=0;i<phone.length;i++){
					  if(!(isNaN(parseInt(phone.charAt(i))))){
					    temp+=phone.charAt(i);
					  }
					}
					
					//error logic
					//these conditions check for every possible positional error
					if(temp.length != 10 && ip == null) err=true;
					if(op>cp || (op <0 && cp>=0) || (cp<0 && op>=0) || 
						(op != 0 && op >=0) || (cp !=4 && cp >=0)) err=true;
					
					if(fd == ld) fd=null;
					if((op >= 0 && fd != null) || (sp >= 0 && fd != null) ||
					(sp >= 0 && ld != 9 && ld >= 0) || 
					(sp <0 && ld != 8 && ld >= 0 && fd == null)||
					(fd != null && ld != 7) || (fd != null && fd !=3)||
					(sp >=0 && cp<0)) err=true;
					
					if(err){
					errors += "- The field "+show_name+" should be in the format with *no* spaces: (NNN)NNN-NNNN or NNN-NNN-NNNN or +NNNNN..(international).\n";
					}
				}
			//Date format = MM/DD/YYYY	
			if(e.formatcheck.toLowerCase() == "date"){
					var dt = e.value;
					var fsl=e.value.indexOf("/");
					var lsl=e.value.lastIndexOf("/");
					//strip out date elements
					var month = dt.substring(0,fsl);
					var day = dt.substring(fsl+1,lsl);
					var year = dt.substr(lsl+1);
					
					if(fsl == lsl || month.length>2 || year.length!=4 || day.length>2)
						errors += "- The field "+show_name+" should be in the format (MM/DD/YYYY).\n";
					if(parseInt(month)>12 || parseInt(month)<0)
						errors += "- The field "+show_name+" has an invalid month ("+parseInt(month)+") range.\n";
					if(parseInt(day)>31 || parseInt(day)<0)
						errors += "- The field "+show_name+" has an invalid day range.\n";
					
				}
			//Date format = MM/DD/YY	
			if(e.formatcheck.toLowerCase() == "number"){
					
			}
			
			if(e.formatcheck.toLowerCase() == "date2"){
					var dt = e.value;
					var fsl=e.value.indexOf("/");
					var lsl=e.value.lastIndexOf("/");
					//strip out date elements
					var month = dt.substring(0,fsl);
					var day = dt.substring(fsl+1,lsl);
					var year = dt.substr(lsl+1);
					
					if(fsl == lsl || month.length>2 || year.length!=2 || day.length>2)
						errors += "- The field "+show_name+" should be in the format (MM/DD/YY).\n";
					if(parseInt(month)>12 || parseInt(month)<1)
						errors += "- The field "+show_name+" has an invalid month range.\n";
					if(parseInt(day)>31 || parseInt(day)<1)
						errors += "- The field "+show_name+" has an invalid day range.\n";
									
				}
				
			//Date format = Jan 14,2000 or January 14,2000	
			if(e.formatcheck.toLowerCase() == "date3"){
					var err=false;
					var dt = e.value;
					var sp=e.value.indexOf(" ");
					var cm=e.value.lastIndexOf(",");
					//strip out date elements
					var month = dt.substring(0,sp);
					var day = dt.substring(sp+1,cm);
					var year = dt.substr(cm+1);
					
					if(year.length!=4 || day.length>2 ||
					!(isNaN(parseInt(month)))){
						errors += "- The field "+show_name+" should be in the format (JAN 14,2000 or January 14,2000).\n";
						err=true;
						}
					if(monthName[month.toLowerCase()] == null && !err)
						errors += "- The field "+show_name+" has an invalid month.\n";
					if(parseInt(day)>31 || parseInt(day)<1)
						errors += "- The field "+show_name+" has an invalid day range.\n";
											
				}
				
			//Date format = JAN-14-2000	
			if(e.formatcheck.toLowerCase() == "date4"){
					var err=false;
					var dt = e.value;
					var fsl=e.value.indexOf("-");
					var lsl=e.value.lastIndexOf("-");
					//strip out date elements
					var month = dt.substring(0,fsl);
					var day = dt.substring(fsl+1,lsl);
					var year = dt.substr(lsl+1);
					
					if(fsl == lsl || month.length !=3 || year.length!=4 || day.length>2 ||
					!(isNaN(parseInt(month)))){
						errors += "- The field "+show_name+" should be in the format (JAN-14-2000).\n";
						err=true;
						}
					if(monthName[month.toLowerCase()] == null && !err)
						errors += "- The field "+show_name+" has an invalid month.\n";
					if(parseInt(day)>31 || parseInt(day)<1)
						errors += "- The field "+show_name+" has an invalid day range.\n";
														
				}
				
				
			}


			}


		

		if(e.type=="radio"){
			if(e.oneSelected != null){
				var sel = false;
					for(var i=0;i<f[e.name].length;i++){
					 if(f[e.name][i].checked == true){
						sel = true;
					 	break;
												 	}
										 }
						 if(!(sel)){
				 errors+= "- The field "+show_name+" must have one selection checked.\n";
				 }
			}


		}

		if(e.type=="checkbox"){
			
			if(e.oneSelected != null && (e.minSelected == null) && (f[e.name].length>1)){
			
				var sel = false;
					for(var i=0;i<f[e.name].length;i++){
					 if(f[e.name][i].checked == true){
					 	sel = true;
					 	break;
								 	}
						 }
						 if(!(sel)){
						 errors += "- The field "+show_name+" must have at least one selection checked.\n";
				 }
			}
			
			if(e.minSelected != null || e.maxSelected != null && (f[e.name].length>1)){
							
				var sel=0;
				for(var i=0;i<f[e.name].length;i++){
					 if(f[e.name][i].checked == true){
					 	sel += 1;
					 	
				}
				}
				if(((e.minSelected != null) && (sel < e.minSelected))||
				((e.maxSelected != null) && (sel > e.maxSelected))){
					if(e.minSelected == null && (e.oneSeleted != null))
						e.minSelected=1;
					errors += "- The field "+show_name;
				if(e.minSelected != null)
					errors += " must have a minimum of "+e.minSelected;
				if(e.maxSelected != null && e.minSelected != null)
					errors += " and a maximum of "+e.maxSelected;
				else if (e.maxSelected != null)
					errors += " can only have a maximum of "+e.maxSelected;
				errors += " selection(s) checked.\n";
				}
							
							
			
			}	


		}

		if(e.type=="select-one" || e.type=="select-multiple"){
			if(e.oneSelected != null &&(e.minSelected == null)){
				
				var sel = false;
				for(var i=0;i<e.options.length;i++){
				 if(e.options[i].selected == true && (e.options[i].value != null) &&
				 (e.options[i].value != "") && (e.options[i].value.toLowerCase != "null")){
				 	sel = true;
				 	break;
				}
				}
					
				if(!(sel)){
				errors += "- The field "+show_name+" must have at least one item selected.\n";
				}

			}
			
			if(e.minSelected != null || e.maxSelected != null){
				
				var sel=0;
				for(var i=0;i<e.options.length;i++){
				 if(e.options[i].selected == true && (e.options[i].value != null) &&
				 (e.options[i].value != "") && (e.options[i].value.toLowerCase != "null")){
				 	sel += 1;
				 	
				}
				}
				if(((e.minSelected != null) && (sel < e.minSelected))||
				((e.maxSelected != null) && (sel > e.maxSelected))){
					if(e.minSelected == null && (e.oneSeleted != null))
						e.minSelected=1;
					errors += "- The field "+show_name;
				if(e.minSelected != null)
					errors += " must have a minimum of "+e.minSelected;
				if(e.maxSelected != null && e.minSelected != null)
					errors += " and a maximum of "+e.maxSelected;
				else if (e.maxSelected != null)
					errors += " can only have a maximum of "+e.maxSelected;
				errors += " selection(s) chosen.\n";
				}
				
				

			}	

		}



} //end for loop

//If no errors then return true so the form will continue the submit process else
// display the error messages and return false to stop the submit process

if(!empty && !errors){
	if(f.confirmSubmit == 1){
		if(confirm(f.confirmMsg)){
		 return true;
		}else{return false;}
	}else{
		return true;
	}
}	

msg = "___________________________________________________________________\n\n";
if(submsg != null){
	msg += submsg;
}else{
	//default error message
	msg +="The form was not submitted because of the following error(s).\n";
	msg +="If you need assistance, please call us at <%=GLOBAL_SITE_CONTACT_TOLLFREE%> <%=CalculateExtension(dist_id)%>.\n";
	msg +="Please correct these error(s) and re-submit.\n";
}
msg += "___________________________________________________________________\n\n";

if(empty){
	msg += "- The following required field(s) are empty:"+empty+"\n";
	if(errors) msg += "\n";
}	
 msg += errors;
 alert(msg);

 return false;
 
}else{
	if(f.confirmSubmit == 1){
		if(confirm(f.confirmMsg)){
			return true;
		}else{
			return false;
		}	
	}else{
		return true;	
	}
}
} 


