function validate(form)
{
var response = "All fields are required. Please complete the entire form and click Submit.";
if (!validateFirstName(form.firstname,response)) return false;
if (!validateLastName(form.lastname,response)) return false;
if (!validateEmail(form.email,response)) return false;
if (!validatePhone(form.phone,response)) return false;
if (!validateSelectList(form.unsecureddebt,response)) return false;
return true;
}

function validateFirstName(field, message)
{
     var val = stripSpaces(field.value);
     var msg = (message != null && message != "") ? message : "Please enter your first name.";
     if( val == "" ||
         val.length < 1 ||
         val.search(/\d/g) > -1 )
     {
       alert(msg);
       field.select();
       field.focus();
       return false;
     }
     return true;
}

function validateLastName(field, message)
{
    var val = stripSpaces(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your last name.";
    if( val == "" ||
        val.length < 3 ||
        val.search(/\d/g) > -1 )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function validateStreetAddress(field, message)
{
	if(field == undefined)
		return true;

    var val = stripSpaces(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your street address.";
    if( val == "" ||
        val.length < 5 )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function validateCity(field, message)
{
	if(field == undefined)
		return true;

    var val = stripSpaces(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your city.";
    if( val == "" ||
        val.length < 3 )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;				
}

function validateState(field, message)
{
	if(field == undefined)
		return true;

    var msg = (message != null && message != "") ? message : "Please select your state.";
    if( field.value == "unspecified" || field.value == "")
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;
}

function validateZipCode(field, message)
{
	if(field == undefined)
		return true;

    var val = stripSpaces(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your zip code.\nA zip code can either be five numbers like '94103' or \nnine numbers seperated by a '-' like '94103-2002'";
    if( val.length < 5 ||
       (val.length == 5 && val.search(/\d{5}/g) != 0) ||
       (val.length > 5 && val.search(/\d{5}-\d{4}/g) != 0) )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function validateEmail(field, message)
{
    var val = stripSpaces(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your valid email address.";
    if( val == "" ||
        val.indexOf("@") < 0 ||
        val.indexOf(".") < 0 ||
        val.length < 8 )
    {
	  msg = "Please enter your valid email address.";
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function oldValidatePhone(field, message)
{
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;
     var msg = (message != null && message != "") ? message : "Please enter a phone number in the format (555) 555-5555";
     // Check that the phone number only contains
     // the characters "() -0-9" and contains the right
     // number of digits total

     for (var i=0; i < field.value.length; i++) {
        var theChar = field.value.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
        phoneOK = false;
     }

     // The string is OK if it contains only the allowed
     // characters and the number of digits in the
     // phone number is 10 total (area code + number)

     phoneOK = phoneOK && (digits == 10);
     if (!phoneOK) {
      alert(msg);
      field.select();
      field.focus();
      return false;
     }

     return true;
}

function validatePhone(field, message)
{
    var phoneOK = true;
	var strPhone = field.value;
    var msg = (message != null && message != "") ? message : "Please enter a phone number in the format (555) 555-5555";
	if (strPhone == "")
	{
		phoneOK = false;
	}
	var stripped = strPhone.replace(/[\(\)\.\-\ ]/g, '');
	if (isNaN(parseInt(stripped)))
	{
		phoneOK = false;
	}
	if (stripped.length != 10)
	{
		phoneOK = false;
	}
	for (i=0; i<10; i++)
	{
		var reg = new RegExp('[' + i + "]{4}");
		if (stripped.search(reg) > -1)
		{
			phoneOK = false;
			msg = "Phone number appears to be fake (too many repeating digits). Please enter a valid phone number.";
		}
	} 
    if (!phoneOK) {
	  msg = "Please enter a valid 10 digit phone number.";
      alert(msg);
      field.select();
      field.focus();
      return false;
     }
	return phoneOK;
}

function validateHomePhoneAreaCode(field, message)
{
    var val = stripSpacesAndAlpha(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your phone number's area code (3 digits) .";
    if( val.length != 3 ||
        val.charAt(0) == "0" )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function validateHomePhonePrefix(field, message)
{
    var val = stripSpacesAndAlpha(field.value);
    var msg = (message != null && message != "") ? message : "Please enter your phone number's prefix code (3 digits) .";
    if( val.length != 3 ||
        val.charAt(0) == "0" )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;
}

function validateHomePhoneSuffix(field, message)
{
    var msg = (message != null && message != "") ? message : "Please enter your phone number's suffix code (last 4 digits) .";
    if( stripSpacesAndAlpha(field.value).length != 4 )
    {
      alert(msg);
      field.select();
      field.focus();
      return false;
    }
    return true;			    
}

function validateBestCallTime(field, message)
{
    var msg = (message != null && message != "") ? message : "Please enter the best time that you prefer us to contact you.";
    if( field.value == "unspecified" )
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;	      
}

function validateRentOwn(field, message)
{
    var msg = (message != null && message != "") ? message : "Please enter the home type you are looking to install a system for.";
    if( field.value == "unspecified" )
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;
}

function validateCheckBox(field, message)
{
	if(field == undefined)
		return true;

	var msg = (message != null && message != "") ? message : "Please check an option from the option fields";
	var radio_choice = false;
	if(field.length == undefined) 
	{	
		if(field.checked) radio_choice = true;	
		
	}
	for (counter = 0; counter < field.length; counter++)
  {
		if (field[counter].checked)
		radio_choice = true; 
	}
	if (!radio_choice)
	{
		alert(msg);
		return false;
	}
	return true;
}	

function validateSelectList(field, message)
{
	if(field == undefined)
		return true;

    var msg = (message != null && message != "") ? message : "Please select an option from the selection list.";
    if( field.value == "unspecified" || field.value == "" )
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;
}

function validateTextField(field, message)
{
		var msg = (message != null && message != "") ? message : "Please enter a value in the empty field.";
    if( field.value == "" )
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;
}

function validateInteger(field, message)
{
		var msg = (message != null && message != "") ? message : "Please enter an integer value.";
    if( parseInt(field.value) != field.value )
    {
      alert(msg);
      field.focus();
      return false;
    }
    return true;
}

function stripForZip(field)
{
    return stripSpaces(field).replace(/^\d{5}-\d{4}/g,"");
}

function stripSpaces(field)
{
    return field.replace(/\s/g,"");
}

function stripSpacesAndAlpha(field)
{
    return stripSpaces(field.replace(/\D/g,""));
}

function sampleData()
{
	var today_date= new Date()
	var month=today_date.getMonth()+1
	var today=today_date.getDate()
	var h=today_date.getHours()
	var m=today_date.getMinutes()
	var s=today_date.getSeconds()

	document.myForm.firstname.value = "System";
	document.myForm.lastname.value = "Tester";
	document.myForm.email.value = "Tst" + month + today + h + m + s + "@systemcheck.com";
	document.myForm.phone.value = "4054783377";
	if (document.myForm.unsecureddebt) 
		document.myForm.unsecureddebt.value = "42500";
}

