	function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
	}

	function Right(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}


	function validateInteger( strValue ) {
	/************************************************
	DESCRIPTION: Validates that a string contains only
		valid integer number.
	
	PARAMETERS:
	   strValue - String to be tested for validity
	
	RETURNS:
	   True if valid, otherwise false.
	******************************************************************************/
	  var objRegExp  = /(^-?\d\d*$)/;
	
	  //check for integer characters
	  return objRegExp.test(strValue);
	}

	 
	function isEmailAddr(email){
		  var result = false
		  var theStr = new String(email)
		  var index = theStr.indexOf("@");
		  if (index > 0){
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
		  }
		  return result;
	}
	
	function EmailValidator(theForm){

		  if (theForm.new_email.value == "")  {
			alert("Please enter a value for the \"email\" field.");
			theForm.new_email.focus();
			return (false);
		  }
		
		  if (!isEmailAddr(theForm.new_email.value))  {
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			theForm.new_email.focus();
			return (false);
		  }
		   
		  if (theForm.new_email.value.length < 3)  {
			alert("Please enter at least 3 characters in the \"email\" field.");
			theForm.new_email.focus();
			return (false);
		  }
 
		  return (true);
	}
	
		function FormValidator(theForm){
  		  
		  var strName,ps1,ps2
		  
		  strName=theForm.new_name.value;
		  
		  if (strName.indexOf(" ")<1)  {
			alert("Please enter your first and last name in the \"Name\" field.");
			theForm.new_name.focus();
			return (false);
		  }
		  
		  if (theForm.new_address.value == "")  {
			alert("Please enter a value for the \"Address\" field.");
			theForm.new_address.focus();
			return (false);
		  }
		  
		  if (theForm.new_city.value == "")  {
			alert("Please enter a value for the \"City\" field.");
			theForm.new_city.focus();
			return (false);
		  }
		  
		  if (theForm.new_state.value == "")  {
			alert("Please enter a value for the \"State\" field.");
			theForm.new_state.focus();
			return (false);
		  }
		  
		  if (theForm.new_zip.value == "")  {
			alert("Please enter a value for the \"Zip Code\" field.");
			theForm.new_zip.focus();
			return (false);
		  }
		  
		  //if (theForm.new_zip.value.length != 5)  {
			//alert("The \"Zip Code\" field must contain 5 intergers.");
			//theForm.new_zip.focus();
			//return (false);
		  //}
		  
		  if (theForm.new_phone.value == "")  {
			alert("Please enter a value for the \"Phone\" field.");
			theForm.new_phone.focus();
			return (false);
		  }
		  
		  if (isNaN(theForm.new_phone.value))  {
			alert("The \"Phone\" field must contain only numbers.\nNo spaces or special characters are allowed.");
			theForm.new_phone.focus();
			return (false);
		  }
		  
		  if (theForm.new_phone.value.length != 10)  {
			alert("The \"Phone\" field must contain 10 intergers.\nYour area code followed by your seven digit phone number.");
			theForm.new_phone.focus();
			return (false);
		  }	
		  
		  if(theForm.name=="form22"){
			  ps1=theForm.new_pass.value;
			  ps2=theForm.new_pass2.value;
			  if (ps1.length<5 || ps1.length>15 || ps2.length<5 || ps2.length>15 || ps1!=ps2)  {
				alert("Passwords Invalid.");
				theForm.new_pass.focus();
				return (false);
			  }	
		  }
		  
		  return (true);
	}
	
	
	function dateValidator(theForm){
		  if (theForm.startDate.value == "" && theForm.endDate.value == "")  {
			return (true);
		  }
		  
		  if (theForm.startDate.value == "")  {
			alert("Please enter a start date.");
			theForm.startDate.focus();
			return (false);
		  }
		  
		  if (theForm.endDate.value == "")  {
			alert("Please enter an end date.");
			theForm.endDate.focus();
			return (false);
		  }
		  
		  if(isDate(theForm.endDate.value)){
			  if(isDate(theForm.startDate.value)){
				  if(Date.parse(theForm.endDate.value)<Date.parse(theForm.startDate.value)){
					alert("The start date must be prior to the end date.");
					theForm.endDate.focus();
					return (false);
				  }				  
			  }else{
				  alert("Start date is not provided in a recognized date format.");
				  return (false);
			  }
		  }else{
			  alert("End date is not provided in a recognized date format.");
			  return (false);
		  }
	}
	
	function isDate(dateStr) {

		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.");
		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 viewCartValidate(theForm){
	   var tagPrefix,tagPost

	   for(i=0; i<theForm.elements.length; i++){
			if(Left(theForm.elements[i].name,5)=="quant" || theForm.elements[i].name=="addQuantity"){
				if(theForm.elements[i].value<1 || !validateInteger(theForm.elements[i].value)){
					alert('Quantity must be an integer\nand cannot be 0 or left blank.');
					theForm.elements[i].select()
					return false;
				}
			}

			tagPrefix=theForm.elements[i].name.substr(0,7);
			tagPost=theForm.elements[i].name.substring(7);
   			if(tagPrefix=='OPTION_' && theForm.elements[i].value==""){
				alert("Please select a valid " + tagPost);
				theForm.elements[i].focus();
				return false;
			}			
	   }
	   return true;
	}
