// Declaring variables for some of the validation functions

var defaultEmptyOK = false;
var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var whitespace = " \t\n\r";
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var mPrefix = "You did not enter a value into the ";
var mSuffix = " field. This is a required field. Please enter it now.";
var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now.";
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now.";
var iEmail = "This field must be a valid email address (like foo@bar.com). Please reenter it now.";
var sStateCode = "State Code";
var sZIPCode = "ZIP Code";
var sPhone = "Phone Number";
var sEmail = "Email";

//*********************************************************************

function validate_email(theControl) {

	if ( theControl.value == "" || theControl.value.length <= 0 )   {
	   alert("I'm sorry. This email address must be filled in correct to send the form to our server. Please"

   		+" check the prefix and '@' sign.");
	   theControl.focus()

	   return false;

    }

    var reEmail = /^.+\@.+\..+$/
    var holderValue;
    var thisValue = theControl.value;

    // Check for e-mail addresses from ISPs and other sources that have been consistently
    // entered incorrectly.  If detected, correct the situation.
    if  (

            (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@aol' ||
            (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@msn' ||
            (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@yahoo' ||
            (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@lycos' ||
            (thisValue.substring(thisValue.length - 7, thisValue.length).toLowerCase()) == '@excite' ||
            (thisValue.substring(thisValue.length - 10, thisValue.length).toLowerCase()) == '@altavista' ||
            (thisValue.substring(thisValue.length - 11, thisValue.length).toLowerCase()) == '@compuserve' ||
            (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@prodigy' ||
            (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@hotmail' ||
            (thisValue.substring(thisValue.length - 9, thisValue.length).toLowerCase()) == '@netscape'
        )

        {

            holderValue = thisValue.concat('.com');
            thisValue = holderValue;
            theControl.value = thisValue;

        }

    if  (

            (thisValue.substring(thisValue.length - 5, thisValue.length).toLowerCase()) == '@home'

        )

        {

            holderValue = thisValue.concat('.net');
            thisValue = holderValue;
            theControl.value = thisValue;

        }

    // Now check the actual value of the e-mail address for validity.

    var flagFirstCheck = (theControl.value.length < 6) ||
        (thisValue.indexOf('@') == -1) ||
        (thisValue.indexOf('.') == -1) ||
        (thisValue.indexOf('@',(thisValue.indexOf('@')+1)) != -1) ||
        ((thisValue.indexOf('.')+1) == thisValue.length) ||
        ((thisValue.indexOf('@')+1) == thisValue.length)

    var flagSecondCheck = reEmail.test(thisValue)
    if ( flagFirstCheck || !flagSecondCheck)
    {

   alert("I'm sorry. This email address seems to be incorrect. Please"
   +" check the prefix and '@' sign.");
   theControl.focus()

        return false;

    }

    else {
        return true;

    }

}
function validate_vin(theControl) {
// if ( theControl.value.length > 16 && theControl.value.length < 18 )

 if (theControl.value.length != 17)

{

alert("I'm sorry. The VIN must be filled in correct to send the form to our server. Please"
   +" enter 17 digits.");
   theControl.focus()
        return false;
          }
    else {
        return true;
    }
}

function ValidateControl(control, prompt) {
  if (control.value=="") {
    alert("The " + prompt +" field is a required field, and it must be filled in before your form can be sent to our server.")
    control.focus()
    return false }
  return true
}

function LitSubmitValidation(form) {
		var brands_cnt = form.brands.length;
		var verify_cnt = 0;	
		form.division.value = "";
		
		// Iterate through checkbox array to see if any are checked. Then grab the value.
		for (var i=0; i < brands_cnt; i ++) {
			if (form.brands[i].checked) {
				form.division.value += form.brands[i].value + ",";
				verify_cnt++;
			}
		}
		
		// Double check if any of the checkbox are selected. If not then empty division field.
		if (verify_cnt == 0) {
			form.division.value = "";
		}
		
		// Alert the user to select a brochure if division field is empty		
		if (form.division.value == "") {
			alert("You must choose at least one brochure to proceed !");
            form.zipcode.focus();
            return false;
		} else if(form.division.value.slice(-1) == ",") {	
			form.division.value = form.division.value.slice(0,-1);
		}
		
		// Regular form validation
        if (!ValidateControl(form.realname,'Name')) return false;
        if (!ValidateControl(form.address,'Address')) return false;
        if (!ValidateControl(form.city,'City')) return false;
        if (!ValidateControl(form.state,'State')) return false;
        if (!ValidateControl(form.zipcode,'Zip')) return false;

        // It everything checks out, then proceed to submit the form.
		return true;
}

function SpecialsSubmitValidation(form) {

        if (!validate_email(form.email, 'Your Email')) return false
        if (!ValidateControl(form.your_realname,'Your Name')) return false
        if (!ValidateControl(form.address,'Your Address')) return false
        if (!ValidateControl(form.city,'Your City')) return false
        if (!ValidateControl(form.state,'Your State')) return false 
        if (!ValidateControl(form.zip,'Your Zip')) return false      
        if (!ValidateControl(form.phone,'Your Phone')) return false        

        return true

}

function SalesSubmitValidation2(form) {
        var count = 0;
        if (!ValidateControl(form.realname,'Name')) return false
        if (!ValidateControl(form.Address,'Address')) return false
        if (!ValidateControl(form.City,'City')) return false
        if (!ValidateControl(form.State,'State')) return false
        if (!ValidateControl(form.ZipCode,'Zip')) return false
        return true
}

function MailSubmitValidation(form) {
        if (!ValidateControl(form.realname,'Name')) return false
        if (!ValidateControl(form.address,'Address')) return false
        if (!ValidateControl(form.city,'City')) return false
        if (!ValidateControl(form.state,'State')) return false
        if (!ValidateControl(form.zipcode,'Zip')) return false
        if (!ValidateControl(form.country,'country')) return false        
        if (form.division.value == "0" || form.division.value == "") {
              alert("You must choose a product to proceed !");
              form.division.focus();
              return false   }
        return true
}

function DownloadSubmitValidation(form) {
        if (!ValidateControl(form.realname,'Name')) return false
   	    if (!ValidateControl(form.ZipCode,'Zip')) return false
        if (form.division.value == "0" || form.division.value == "") {
              alert("You must choose a product to proceed !");
       	      form.division.focus();
              return false   }
        return true
}
function DivDownloadSubmitValidation(form) {
        if (!ValidateControl(form.realname,'Name')) return false
   	    if (!ValidateControl(form.ZipCode,'Zip')) return false
        return true
}


function FriendSubmitValidation(form) {
        if (!validate_email(form.friend_email, 'Your Friends email')) return false
   	    if (!validate_email(form.your_email, 'Your email')) return false
        if (!ValidateControl(form.your_realname,'Your Name')) return false
        if (!ValidateControl(form.friend_realname,'Friends Name')) return false
        return true
}

function SpecialsSubmitValidation(form) {

        if (!validate_email(form.email, 'Your Email')) return false
        if (!ValidateControl(form.your_realname,'Your Name')) return false
        if (!ValidateControl(form.address,'Address')) return false
        if (!ValidateControl(form.city,'City')) return false
        if (!ValidateControl(form.state,'State')) return false 
        if (!ValidateControl(form.zip,'Zip')) return false      
        if (!ValidateControl(form.phone,'Phone')) return false        

        return true

}

function WarrantySubmitValidation(form) {
        if (!validate_email(form.email)) return false
        if (!ValidateControl(form.realname,'Name')) return false
        if (!ValidateControl(form.Address,'Address')) return false
        if (!ValidateControl(form.City,'City')) return false
        if (!ValidateControl(form.State,'State')) return false
        if (!ValidateControl(form.ZipCode,'Zip')) return false
        if (!ValidateControl(form.PhoneNumber,'Phone Number')) return false
        if (form.division.value == "0") {
      	      alert("You must select a division to proceed !");
              form.division.focus();
              return false   }

        if (!ValidateControl(form.SerialNumber,'Serial Number')) return false
        if (!validate_vin(form.VINNumber)) return false
        if (!ValidateControl(form.Comments,'Comments')) return false

        return true
}

function generalSubmitValidation(form) {

        if (!validate_email(form.email)) return false
        if (!ValidateControl(form.realname,'Name')) return false
        if (!ValidateControl(form.Address,'Address')) return false
        if (!ValidateControl(form.City,'City')) return false
        if (!ValidateControl(form.State,'State')) return false
        if (!ValidateControl(form.ZipCode,'Zip')) return false
        if (!ValidateControl(form.PhoneNumber,'Phone Number')) return false
        if (!ValidateControl(form.Comments,'Comments')) return false

        return true

}

function webmasterSubmitValidation(form) {
        if (!validate_email(form.email)) return false
        if (!ValidateControl(form.realname,'Name')) return false
        if (!ValidateControl(form.Comments,'Comments')) return false

        return true
}

function dealerSubmitValidation(form) {

        if (!ValidateControl(form.DealerNumber,'Dealer Number')) return false
        if (!ValidateControl(form.DealerName,'Dealer Name')) return false
        if (!ValidateControl(form.Address,'Address')) return false
        if (!ValidateControl(form.City,'City')) return false
        if (!ValidateControl(form.State,'State')) return false
        if (!ValidateControl(form.ZipCode,'Zip')) return false
        if (!validate_email(form.Email)) return false
        if (!ValidateControl(form.PhoneNumber,'Phone Number')) return false

        return true
}

function mediaValidation(form) {

		if (!ValidateControl(form.fullname,'Full Name')) return false
     	if (!ValidateControl(form.email,'Email')) return false
        if (!ValidateControl(form.comments,'Comments')) return false
        return true

}

function PhoneFormat(evt) {
	IE = document.all;
        if (IE) {
        var keycode = window.event.keyCode;
        var shift   = window.event.shiftKey;
        var ctrl    = window.event.ctrlKey;
        var alt     = window.event.altKey;
        var pos = this.value.length + 1;
        var lparen  = (shift  && !ctrl && !alt && keycode == 40);
        var rparen  = (shift  && !ctrl && !alt && keycode == 41);
        var space   = (!shift && !ctrl && !alt && keycode == 32);
        var dash    = (!shift && !ctrl && !alt && keycode == 45);
        var slash   = (!shift && !ctrl && !alt && keycode == 47);
        var digit   = (!shift && !ctrl && !alt && keycode >= 48 && keycode <= 57);


                if (!lparen && !rparen && !space && !dash && !digit) return false;
                if (pos == 1  && lparen) return true;
                if (pos == 1  && digit) { this.value = '('; return true; }
                if (pos == 2  && digit) return true;
                if (pos == 3  && digit) return true;
                if (pos == 4  && digit) return true;
                if (pos == 5  && rparen) { this.value += ') '; return false; }
                if (pos == 5  && dash)   { this.value += ') '; return false; }
                if (pos == 5  && slash)  { this.value += ') '; return false; }
                if (pos == 5  && space)  { this.value += ') '; return false; }
                if (pos == 5  && digit)  { this.value += ') '; return true; }
                if (pos == 6  && space) return true;
                if (pos == 6  && digit) { this.value += ' '; return true; }
                if (pos == 7  && digit) return true;
                if (pos == 8  && digit) return true;
                if (pos == 9  && digit) return true;
                if (pos == 10 && dash) return true;
                if (pos == 10 && space) { this.value += '-'; return false; }
                if (pos == 10 && digit) { this.value += '-'; return true; }
                if (pos == 11 && digit) return true;
                if (pos == 12 && digit) return true;
                if (pos == 13 && digit) return true;
                if (pos == 14 && digit) return true;
        } else {
        keycode = evt.which;
        var bs = String.fromCharCode(evt.which);
                if (keycode >= 48 && keycode <= 57) return true;
                if (keycode == 0) return true;
                if ((keycode == 32) || (keycode >= 40 && keycode <= 41) || (keycode >= 45 && keycode <= 46)) return true;
                if (bs == "\b") return true;
                if (keycode == 13) wr4_hitReturn(); return false;
        }
return false;
}

function validateFeedback(form) {
	
	if (!ValidateControl(form.name,'Full Name')) return false
	if (!ValidateControl(form.email,'Email')) return false
	if (!ValidateControl(form.comments,'Comments')) return false
        
	return true	
	
}

function FSubmitValidation(form) {

   if ( form.division.value == "") {
	  alert('You MUST select a division');
	  return false
   }

   if ( (form.zip.value == "") && (form.city.value == "" || form.state.value == "") ) {
	  alert("You must enter EITHER a zip code or city/state to proceed !");
	  form.zip.focus();
	  return false   }

   return true }


function check_division() {
  var i, ok = false;
  for (i=0; i<document.f.elements.length; i++) {
	if (document.f.elements[i].name == 'class_id[]' && document.f.elements[i].checked) {
	  ok = true;
	  break;
	}
  }
  return ok;
}

// Additional Validation functions
//****************************************************************************
function isRadioButtonChecked (form)
{
	var radio_elem = document.step2.whichfloorplan;		   
	var cnt        = radio_elem.length;
	
	for (var i=0; i<cnt; i++) {   
		if (radio_elem[i].checked) {
			return true; 			
		}
		else {
			alert("You need to select a floorplan to continue.");	
    		return false;
		}	
	}
	
}

function isOptionSelected (theForm)
{
	var cntOpt = theForm.length;
	var allvalid = true;
	var alertstr = "";
	var checkbox_selected = false;
	
	for (var i=0; i<cntOpt; i++) {
		var theElement = theForm.elements[i];
		var element_type = theElement.type;

// Check Checkboxes ...
		if (element_type == "checkbox") {
			if (theElement.checked == true) {
				checkbox_selected = true;
				validstr += "From form element '" + element_name + "' you selected the \"" + element_value + "\" checkbox.\n\n";
			}
		}
	}
// .... End of loop through form elements ....
	
	if (checkbox_selected == false) {
		alertstr += "There are no options selected. Please select a package and/or option to continue.\n\n";
		allvalid = false;
	}
	
	if (allvalid) {
		alert (validstr);
		return false;
	} else {
		alert (alertstr);
		return false;
	}
	
}


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}


function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}


function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}


function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}


function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}


function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return warnInvalid (theField, iUSPhone);
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}


function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      if (!isZIPCode(normalizedZIP, false)) 
         return warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         // theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}


function dealerlocValidation(form) {
		
		if (!ValidateControl(form.product,'Product')) return false
        if (!ValidateControl(form.zip,'Zip')) return false       

        return true

}
