// JSFile=JSFile+"|"+"Entered Forms.js"
var FormErrors = "";
var ErrorLocation = "";
var Submitted = false;

/* Move this function to the top of the page being validated and edit as needed:

function Validate(objForm) {
   if (Submitted) {
      alert( 'Just a moment, please./n/nYour request is being processed.');
      return false;
   }
   var errfound  = false;
   var make  = objForm.make.options[objForm.make.selectedIndex].value;
   var model = objForm.model.options[objForm.model.selectedIndex].value;
   var year  = objForm.year.options[objForm.year.selectedIndex].value;
   var exterior_color  = objForm.exterior_color.options[objForm.exterior_color.selectedIndex].value;
   var VINMsg=CheckVIN(objForm);
   if (VINMsg.length > 0)
   	errfound=addError(objForm.VIN,VINMsg);
   if (make.length==0 || make.indexOf("Select") != -1)
      errfound=addError(objForm.make,"Please select a make of vehicle.");
   if (model.length==0 || model.indexOf("Select") != -1)
      errfound=addError(objForm.model,"Please select a model of vehicle.");
   if (year.length==0)
      errfound=addError(objForm.year,"Please select a year of vehicle.");
   if (objForm.mileage.value<=0)  ' this catches empty values, too
   	errfound=addError(objForm.mileage,"Mileage for this vehicle must be more than 0.");
   if (objForm.price.value <= 0)  ' this catches empty values, too
   	errfound=addError(objForm.price,"Price for this vehicle must be more than 0.");
   if (exterior_color.length==0)
      errfound=addError(objForm.exterior_color,"Please select an exterior color.");
   if (!ValidLength(objForm.name.value,6))
      errfound=addError(objForm.name,"Your name is missing.");
   if (!ValidEmail(objForm.email.value))
      errfound=addError(objForm.email, "Your email address is missing or incorrect.");
   if (!ValidLength(objForm.comments.value,6))
      errfound=addError(objForm.comments,"Please share your comments with us.");
   if (!ValidLength(objForm.company.value,2))
      errfound=addError(objForm.company,"The company name is missing.");
   if (!ValidLength(objForm.address1.value,2))
      errfound=addError(objForm.address2,"The address is missing.");
   if (!ValidLength(objForm.city.value,2))
      errfound=addError(objForm.city,"The city is missing");
   if (!ValidState(objForm.state.value))
      errfound=addError(objForm.state,"The State/Province is missing or incorrect.");
   if (!ValidLength(objForm.zip.value,5))
      errfound=addError(objForm.zip,"The ZIP/postal code is missing or incorrect.");
   if (!ValidPhone(objForm.phone.value))
      errfound=addError(objForm.phone,"The telephone number is missing or incorrect.");
	if (!ValidPassword(objForm.password.value))
		errfound=addError(objForm.password,"Passwords require at least one upper case character, one lower case character, one digit, and must be at least six characters long.");
   if (errfound==true) showerrors( "The following errors were encountered:" + FormErrors, ErrorLocation );
   FormErrors="";
   ErrorLocation="";
   if (!errfound){
      Submitted = true;
      return true;
   } else {
      return false;
   }
}  */

//Then put onSubmit="return Validate(document.formname);" in the form tag.
//For text fields needing formatting, put onChange="form.CON.value=toUpperLower(form.CON.value)"  in the Input tags.
//To format phone number fields, put onKeyDown="return PhoneMask();" in the Input tags.
//To format 9 digit zip code number fields, put onKeyDown="return ZipCodeMask();" in the Input tags.
//To format 5 digit zip code number fields, put onKeyDown="return ZipCodeMask(5);" in the Input tags.
//To format 9 digit social security number fields, put onKeyDown="return SSNMask();" in the Input tags.
//To format specific (in this case 10) digit length numeric fields, put onKeyDown="return NumericMask(10);" in the Input tags.


function addError(Location,Error)
{
  if (FormErrors == "")
  {
    FormErrors = "\n" + Error + "\n";
    ErrorLocation=Location;
  }
  else
  {
    FormErrors += Error + "\n";
  }
  return true;
}

function ValidLength(item,len) {
   return (item.length >= len);
}

function ValidEmail(item) {
   if (!ValidLength(item, 5)) return false;
   if (item.indexOf ('@', 0) == -1) return false;
   if (item.indexOf ('.', 0) == -1) return false;
   return true;
}


function ValidPhone(item) {
   newstring=FormatPhone(item);
   if (newstring.length==14){
      item=newstring;
      return true;
      }
   else {
      if (newstring.length >= 10) return true;
      }
   return false;
}


function showerrors(text,elem) {
   window.alert(text);
   if (typeof(elem)=='object') elem.focus();
}

function toUpperLower(item) {
   newstring="";
   firstone=true;
   for (i=0; i < item.length;i++)
      {
      thisone=item.substring(i,i+1);
      if (thisone==" " || thisone.charCodeAt(0)==10 || thisone.charCodeAt(0)==9) {
         newstring=newstring+thisone;
         firstone=true;
         continue;
         }
      if (firstone && thisone.toLowerCase()==thisone){
         thisone=thisone.toUpperCase()
         newstring=newstring+thisone;
         firstone=false;
         }
      else
         {
         firstone=false;
         newstring=newstring+thisone;
         }
      }
   return newstring;
}

function clean(string) {
   outstring="";
   LenStr=string.length+1;
   n=0;
   NumStr="0123456789"
   for (n=0;n<LenStr;n++) {
      if (NumStr.indexOf(string.substring(n,n+1))>-1)
         {
         outstring=outstring+string.substring(n,n+1);
         }
      }
   if (outstring.length==0) {
      outstring=0;
      }
   return outstring;
}


function IsNumber(string) {
   LenStr=string.length+1;
   n=0;
   NumStr="0123456789"
   for (n=0;n<LenStr;n++) {
      if (NumStr.indexOf(string.substring(n,n+1))==-1)
         {
         return false;
         }
   }
   return true;
}

function IsAlpha(string) {
   LenStr=string.length+1;
   n=0;
   AlphaStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   for (n=0;n<LenStr;n++) {
      if (AlphaStr.indexOf(string.substring(n,n+1).toUpperCase())==-1)
         {
         return false;
         }
   }
   return true;
}


function FormatPhone(item) {
	//Formating a phone number on the fly:
	//Usage: <input type="text" name="phone" onKeyUp="OrderForm.phone.value=FormatPhone(OrderForm.phone.value)">
   newstring="(";
   digitstr="0123456789"
   for (i=0; i < item.length;i++)
      {
      if (digitstr.indexOf(item.substring(i,i+1),0)!=-1)
      	{
         newstring=newstring+item.substring(i,i+1);
         if (newstring.length==4) newstring=newstring+") ";
         if (newstring.length==9) newstring=newstring+"-";
         }
      }
   if (newstring.length!=14)  // don't mess with foreign phone numbers...
      {
      return item;
      }
   else
      {
      return newstring;
      }
}


function checkCR(evt) {
  // To prevent the enter key from submitting a form when hit by accident in a text box.
  // This kills the enter key in text fields only.
  var evt  = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;


function PhoneMask(){
	// usage: onKeyDown="return PhoneMask();"
	return FilterIt("(###) ###-####");
}

function SSNMask(){
	// usage: onKeyDown="return SSNMask();"
	return FilterIt("###-##-####");
}


function NumericMask(NumDigits){
	// usage: onKeyDown="return NumericMask(5);"
	var Mask="";
   for (FilterStep = 0; FilterStep < NumDigits; FilterStep++){
		Mask=Mask + "#";
	}
	return FilterIt(Mask);
}


function ZipCodeMask(NumDigits){
	// usage: onKeyDown="return ZipCodeMask();"
	// defaults to 9 digit zips unless parameter passed
	// of 5 for 5 digit zips (ie, onKeyDown="return ZipCodeMask(5);")
	if (NumDigits==5){
		return FilterIt("#####");
	}else{
		return FilterIt("#####-####");
	}
}

// Text input numeric masking
// usage: onKeyDown="return FilterIt('###-##-####');"

function FilterIt(FilterMask){
		key=event.keyCode;
	   textbox = (event.target) ? event.target : ((event.srcElement) ? event.srcElement : null);

		FilterNum = FilterStrip(textbox.value, FilterMask);

		if (key==9)
		{
		    return true;
		}
		else if (key==8&&FilterNum.length!=0)
		{
		 	 	FilterNum = FilterNum.substring(0,FilterNum.length-1);
		}
 	  else if ( ((key>47&&key<58)||(key>95&&key<106)) && FilterNum.length<FilterMax(FilterMask) )
		{
        if (key>95) key=key-48;  // number pad keys
        FilterNum=FilterNum+String.fromCharCode(key);
		}
		else if ((key==12)||(key==45)||(key>32&&key<41)) // If numlock is turned off...
		{
			if (key==45) key=48;
			if (key==35) key=49;
			if (key==40) key=50;
			if (key==34) key=51;
			if (key==37) key=52;
			if (key==12) key=53;
			if (key==39) key=54;
			if (key==36) key=55;
			if (key==38) key=56;
			if (key==33) key=57;
      	FilterNum=FilterNum+String.fromCharCode(key);
      }
		var FilterFinal='';
    for (FilterStep = 0; FilterStep < FilterMask.length; FilterStep++)
		{
        if (FilterMask.charAt(FilterStep)=='#')
				{
					  if (FilterNum.length!=0)
					  {
				        FilterFinal = FilterFinal + FilterNum.charAt(0);
					      FilterNum = FilterNum.substring(1,FilterNum.length);
					  }
				    else
				    {
				        FilterFinal = FilterFinal + "";
				    }
				}
		 		else if (FilterMask.charAt(FilterStep)!='#')
				{
				    FilterFinal = FilterFinal + FilterMask.charAt(FilterStep);
				}
		}
		var IsNumeric=false;
		var strNumbers="0123456789";
	 	for (FilterStep=0; FilterStep < FilterFinal.length; FilterStep++) {
	 		if (strNumbers.indexOf(FilterFinal.charAt(FilterStep))!=-1) IsNumeric=true;
	 	}
	 	if	(IsNumeric){
	 		textbox.value = FilterFinal;
	 	}else{
	 		textbox.value="";
	 	}
    return false;
}

function FilterStrip (FilterTemp, FilterMask)
{
    FilterMask = replace(FilterMask,'#','');
    for (var FilterStep = 0; FilterStep < FilterMask.length++; FilterStep++)
		{
		    FilterTemp = replace(FilterTemp,FilterMask.substring(FilterStep,FilterStep+1),'');
		}
		return FilterTemp;
}

function FilterMax (FilterMask)
{
	FilterTemp = FilterMask;
   for (var FilterStep = 0; FilterStep < (FilterMask.length+1); FilterStep++)
		{
		 		if (FilterMask.charAt(FilterStep)!='#')
				{
		        FilterTemp = replace(FilterTemp,FilterMask.charAt(FilterStep),'');
				}
		}
		return FilterTemp.length;
}


function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;
    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;
    var newstr = fullString.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);
    return newstr;
}


function LimitedTextArea(FieldPrompt,FieldName,Columns,Rows,MaxLength){
	document.write('<table border=0><tr><td>'+FieldPrompt);
	document.write('<br><textarea class=form name=\"'+FieldName+'\" wrap=\"physical\" cols=\"'+Columns+'\" rows=\"'+Rows+'\" onKeyUp="TextCount(this,this.form.'+FieldName+'CharRem,'+MaxLength+');"></textarea><br><center>')
	if(navigator.appName == "Netscape"){
		document.write('\(Maximum Characters: '+MaxLength+'\)');
	}else{
		document.write('\(Characters Remaining: <input class=form readonly name='+FieldName+'CharRem type=text size=4 maxlength=4 value='+MaxLength+'> \)');
	}
	document.write('</center></td></tr></table>');
}


function TextCount(TextField, Kounter, MaxLength) {
	if (TextField.value.length > MaxLength){
		TextField.value = TextField.value.substring(0, MaxLength);
	}else{
		Kounter.value = MaxLength - TextField.value.length;
	}
}


function ValidPassword(inPassword){
   // requires at least one upper case, one lower case, one digit, and at least 6 characters long
   // which is 13,537,086,000 variations (26+26+10 to the sixth power)
	var re = /^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/
	if (!re.test(inPassword)){
		return false;
	}else{
	   if (inPassword.length < 6){
	   	return false;
	   } else {
			return true;
		}
	}
}


function ValidDate(inDate,nYearLen){
// checks for format of mm/dd/yyyy or mm/dd/yy and makes sure it's a valid date
	if (inDate.indexOf("/") == -1) return false;
	dt1 = inDate.split("/");
	if (dt1[2].length !=nYearLen) return false;
	mm1 = parseInt(dt1[0]);
	dd1 = parseInt(dt1[1]);
	yy1 = parseInt(dt1[2]);
	if (isNaN(dd1) || isNaN(mm1) || isNaN(yy1)) return false;
	dt2 = new Date(mm1+'/'+dd1+'/'+yy1);
	dd2 = dt2.getDate();
	mm2 = dt2.getMonth()+1;
	yy2 = dt2.getFullYear();
	if (dd1!=dd2 || mm1!=mm2 || yy1!=yy2) return false;
	return true;
}



function ValidRadio(item) {
	for (i=item.length-1; i > -1; i--) {
		if (item[i].checked) {
			return true;
		}
	}
	return false;
}

function ValidCheckbox(item){
	return (item.checked)
}

function CheckTextLen(TextField,MaxLength,FieldName){
   if (TextField.value.length > MaxLength) {
   	TextField.value = TextField.value.substring(0, MaxLength);
      alert("The maximum length for this field is " + MaxLength +".");
      return false;
   }
   if (document.getElementById(FieldName + "CharKount")){
   	document.getElementById(FieldName + "CharKount").innerHTML=TextField.value.length;
   }
   return true;
}

//JSFile=JSFile+"|"+"Exited Forms.js"