/*
 * validate.js   functions for input field validation on forms
 * 
 * include this file into any html file that has forms, by using statement:
 *    <script type="text/javascript" src="http://www.korteweb.com/js/validate.js"></script>
 *
 * also include directly IN the html file, a function similar to the model below:
 * function CheckThenSubmit(form) {
 * 	var stat1 = validateLength(2,25,form["fFull"], document.getElementById('help1'));
 * 	var stat2 = validateRegEx(/^[0-9]$/,form["fCode"], document.getElementById('help2'), 'Please enter XX');
 * 	if ( stat1 && stat2 )  {
 *		form.submit();
 * 	} else {
 * 		alert("Please make corrections as noted in red messages");
 * 	}
 * }
 * the stylesheet should have a definition for span.help  (exists in common_style.css)
*/

function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

function validateNonEmpty(inputField, helpSpot) {
	if (inputField.value.length == 0) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "required field";
		return false;
	}
	else {
		// the data is OK, so clear the help message
		if (helpSpot != null)
			helpSpot.innerHTML = "";
		return true;
	}
}

function validateLength(minLength,maxLength,inputField, helpSpot,setProperCase) {
   if (typeof setProperCase === 'undefined') setProperCase=false;
	// see if the input contains at least minLength but no more than maxLength chars
	if (inputField.value.length < minLength || inputField.value.length > maxLength) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "needs " + minLength + "-" + maxLength + " chars";
		return false;
	}
	else {
		// the data is OK, so clear the help message
		if (helpSpot != null)
			helpSpot.innerHTML = "";
	   if (setProperCase) {
   		var temp = toProperCase(inputField.value);
   		inputField.value = temp;
      }
		return true;
	}
}

function validateRegEx(regex,inputField, helpSpot, helpMessage) {
	//if (!validateNonEmpty(inputField, helpSpot))
		//return false;		
	var inputStr = inputField.value;		
	// see if the inputStr validates OK
	if (!regex.test(inputStr)) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = helpMessage;
		return false;
	}
	else {
		// the data is OK, so clear the help message
		if (helpSpot != null)
			helpSpot.innerHTML = "";
		return true;
	}
}

function validateLettersOnly(minLength, maxLength, inputField, helpSpot,setProperCase) {
   if ((minLength < 1) &&( inputField.value.length < 1)) return true;
   if (typeof setProperCase === 'undefined') setProperCase=false;
   var pattern = /^[A-Za-z ]+$/;   
	// see if the input contains at least minLength but no more than maxLength chars
	if (inputField.value.length < minLength || inputField.value.length > maxLength) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "needs " + minLength + " to " + maxLength + " chars";
		return false;
	}
   if (setProperCase) {
		var temp = toProperCase(inputField.value);
		inputField.value = temp;
   }
   return validateRegEx(pattern,inputField,helpSpot,'Only letters allowed');
}

function validateLettersOrDigits(minLength, maxLength, inputField, helpSpot,setProperCase) {
   if ((minLength < 1) &&( inputField.value.length < 1)) return true;
   if (typeof setProperCase === 'undefined') setProperCase=false;
   var pattern = /^[A-Za-z0-9 ]+$/;   
	// see if the input contains at least minLength but no more than maxLength chars
	if (inputField.value.length < minLength || inputField.value.length > maxLength) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "needs " + minLength + " to " + maxLength + " chars";
		return false;
	}
   if (setProperCase) {
		var temp = toProperCase(inputField.value);
		inputField.value = temp;
   }
   return validateRegEx(pattern,inputField,helpSpot,'Only letters or digits');
}

// this function is same as validateLength except that it removes commas,
// always sets proper case, then force-capitalizes any 2-letter sequences surrounded by spaces.
// it does not verify that a statecode exists or that a zipcode exists 
//  (althouth the commented out pattern would force that, if used)
function validateLengthFmtCityStateZip(minLength, maxLength, inputField, helpSpot) {
   if ((minLength < 1) &&( inputField.value.length < 1)) return true;
   var pattern = /^[A-Za-z0-9\- ]*$/;   
   //var pattern = /^[A-Za-z]+\s[A-Za-z]{2,3}\s[A-Za-z0-9]+$/;
	// see if the input contains at least minLength but no more than maxLength chars
	if (inputField.value.length < minLength || inputField.value.length > maxLength) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "needs " + minLength + " to " + maxLength + " chars";
		return false;
	}   
	var temp = toProperCase(inputField.value);		
   temp = temp.replace(',',' ');		
   temp = temp.replace('.',' ');	
   //temp = temp.replace(/ ([a-zA-Z]{2}) /, ' '+$1.toUpperCase()+' ');	
   temp = temp.replace(/ ([a-zA-Z]{2}) /, function(m) { return ' '+m.toUpperCase()+' '; });	
   temp = temp.replace(/\s+/g,' ');	  
	inputField.value = temp;
   return validateRegEx(pattern,inputField,helpSpot,'no commas, max '+maxLength+' chars');
}

function validateSlashDate(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^\d{1,2}\/\d{1,2}(\/(\d{2}|\d{4}))?$/;
   } else {
      var pattern = /^$|^\d{1,2}\/\d{1,2}(\/(\d{2}|\d{4}))?$/;
   }    
   return validateRegEx(pattern,inputField,helpSpot,'like 3/15 or 10/29/83 or 12/25/2009');
}

function validateSlashDate4(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^\d{1,2}\/\d{1,2}(\/(\d{4}))?$/;
   } else {
      var pattern = /^$|^\d{1,2}\/\d{1,2}(\/(\d{4}))?$/;
   }    
   return validateRegEx(pattern,inputField,helpSpot,'needs 4-digit year, like 12/25/2009');
}

function validatePostZip(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^\d{5}(-\d{4})*$/;
   } else {
      var pattern = /^$|^\d{5}(-\d{4})*$/;
   }   
   return validateRegEx(pattern,inputField,helpSpot,'needs ZIP like 12345 or 12345-1234');
}

function validatePostCan(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]$/;
   } else {
      var pattern = /^$|^[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]$/;
   }    
   return validateRegEx(pattern,inputField,helpSpot,'like K2N 3D5');
}

function validatePhoneUS(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
   } else {
      var pattern = /^$|^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
   } 
   return validateRegEx(pattern,inputField,helpSpot,'like 800-555-1212 or (800) 555-1212');
}

function validateEmail(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
   } else {
      var pattern = /^$|^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
   }    
   return validateRegEx(pattern,inputField,helpSpot,'like don.smith@hiya.com');
}

function validateSignedInt(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^[-+]?\d+$/;
   } else {
      var pattern = /^$|^[-+]?\d+$/;
   }  
   return validateRegEx(pattern,inputField,helpSpot,'needs number (+/- sign opt)');
}

function validateUnSignedInt(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^\d+$/;
   } else {
      var pattern = /^$|^\d+$/;
   }  
   return validateRegEx(pattern,inputField,helpSpot,'needs unsigned number');
}

function validateFloat(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^[-+]?[0-9]*\.?[0-9]+$/;
   } else {
      var pattern = /^$|^[-+]?[0-9]*\.?[0-9]+$/;
   } 
   return validateRegEx(pattern,inputField,helpSpot,'needs number');
}

/*
 * validates unsigned integer in specified range (inclusive) 
 */
function validateIntRange(minallowed, maxallowed, inputField, helpSpot) {
   var pattern = /^[-+]?\d+$/;
   if (!validateRegEx(pattern,inputField,helpSpot,'needs number (+/- sign opt)')) {
      return false;
   }
   var x = inputField.value;
   if ((x < minallowed) || (x > maxallowed) ) { 
			helpSpot.innerHTML = "number must be between " + minallowed + " and " + maxallowed;
			return false;
   }
   return true;
}

/*
 * for URLs, protocol (ie http://) is optional
 */
function validateURL(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^(((http\:|https\:|ftp\:|HTTP\:|HTTPS\:|FTP\:)\/\/)?[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_~\-]+)*(?:\/[a-zA-Z0-9_~\-]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_~]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/;
   } else {
      var pattern = /(^$)|(^(((http\:|https\:|ftp\:|HTTP\:|HTTPS\:|FTP\:)\/\/)?[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_~\-]+)*(?:\/[a-zA-Z0-9_~\-]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_~]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$)/;
   } 
   return validateRegEx(pattern,inputField,helpSpot,'needs URL like www.google.com (http:// opt)');
}

/*
 * state code must be 2 character us postal code; 
 * note that this function capitalizes content of form field in-place
 */
function validateStateCode(inputField, helpSpot, emptyallowed) {
   if (typeof emptyallowed === 'undefined') emptyallowed=false;
   if (!emptyallowed) { 
      var pattern = /^(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)$/i;
   } else {
      var pattern = /(^$)|(^(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)$)/i;
   } 
   inputField.value = inputField.value.toUpperCase();
   return validateRegEx(pattern,inputField,helpSpot,'needs 2-letter US State Code');
}

/*
 * club name must have only alpha letters 
 * note that this function replaces content of form field in-place,
 * replacing Figure Skating Club with FSC, 
 * replacing Skating Club and Skate Club with SC,
 * replacing Figure Skating Academy with FSA
 * replacing Skating Academy with SA
 */
function validateFSCname(minLength, maxLength, inputField, helpSpot) {
   if ((minLength < 1) &&( inputField.value.length < 1)) return true;
   var pattern = /^[A-Za-z0-9\-\/ ]+$/;   
	// first  make some handy simplifications		
	var temp = inputField.value
	temp = toProperCase(temp);
   temp = temp.replace(/^ /g,'');
   temp = temp.replace(/ $/g,'');	
   temp = temp.replace('.',' ');			
   temp = temp.replace(',',' ');		
   temp = temp.replace(/\s+/g,' ');
	temp = temp.replace('Figure Skating Club','FSC');
   temp = temp.replace('Skating Club','SC');
   temp = temp.replace('Skate Club','SC');
   temp = temp.replace('Figure Skating Academy','FSA');
   temp = temp.replace('Skating Academy','SA');
   temp = temp.replace('Learn To Skate','LTS');
   temp = temp.replace(/^Saint /,'St ');	
   temp = temp.replace(/^Mount /,'Mt ');	
   temp = temp.replace(/^Pt /,'Port ');	
   temp = temp.replace(/^Sc /,'SC ');	
   temp = temp.replace(/ Sc$/,' SC');	
   temp = temp.replace(/ Sc /,' SC ');	
   temp = temp.replace(/^Fsc /,'FSC ');	
   temp = temp.replace(/ Fsc$/,' FSC');	
   temp = temp.replace(/^Sa /,'SA ');	
   temp = temp.replace(/ Sa$/,' SA');	
   temp = temp.replace(/ Sa /,' SA ');	
   temp = temp.replace(/^Fsa /,'FSA ');	
   temp = temp.replace(/ Fsa$/,' FSA');	
   temp = temp.replace(/ Lts$/,' LTS');	
   temp = temp.replace(/ Of /,' of ');
   temp = temp.replace(/\s+/g,' ');	   
   if ((temp.indexOf("ndiana") != -1) && (temp.indexOf("orld") != -1)) {
      temp = "Indiana/World SA";
   }   
   inputField.value = temp;
	// then see if the input contains at least minLength but no more than maxLength chars
	if (inputField.value.length < minLength || inputField.value.length > maxLength) {
		// the data is invalid, so set the help message
		if (helpSpot != null) 
			helpSpot.innerHTML = "allowed " + minLength + "-" + maxLength + " chars (letters or digits)";
		return false;
	}
   return validateRegEx(pattern,inputField,helpSpot,'Only letters and digits allowed');
}
