/*
 * Check and Format a phone number (North American format)
 * Call using the onchange() event
 */
function phoneFilter(input) {
	s = input.value;

	var filteredString = "";

	// characters to be stripped out
	allowedValues = "1234567890";
	filteredString = filterString(input, allowedValues, 1);

	// check returnString for a blank value, 10-digit number or 1 before area code
	if (filteredString == "") {
		input.value = filteredString;
		alert("Please enter a Phone Number in the following format: (nnn) nnn-nnnn");
		input.focus();
	} else if (filteredString.length < 10) {
		alert("Please enter a Phone Number in the following format: (nnn) nnn-nnnn");
		input.focus();
	} else if (filteredString.substring(0,1) == 1) {
		alert("Please do not add a 1 before your Area Code. Use the following format: (nnn) nnn-nnnn");
		input.focus();
	} else {
		input.value = "(" + filteredString.substring(0,3) + ") " + filteredString.substring(3,6) + "-" + filteredString.substring(6,10);
		return true;
	}

	return false;
}

// Deprecated function. Use numericFilter.
function commaFilter (input) {
	numericFilter(input);
}

// Filter string, return numeric characters only
function numericFilter (input) {
	allowedValues = "1234567890";
	filterString (input, allowedValues);
}

// Filter string, return alphabetic characters only
function alphaFilter (input) {
	allowedValues = "abcdefghijklmnopqrstuvwxyz";
  filterString (input, allowedValues);
}

// Filter string, return alphabetic and numeric characters only
function alphaNumericFilter (input) {
  allowedValues = "abcdefghijklmnopqrstuvwxyz1234567890";
  filterString (input, allowedValues);
}

// Filter string using custom allowedValues; this is really just a wrapper function
function customFilter (input, allowedValues) {
  filterString (input, allowedValues);
}

function filterString (input, allowedValues, returnFilteredString) {
  var returnFilteredString = (returnFilteredString == null) ? 0 : returnFilteredString; // set default value to 0
  s = input.value;

	var i;
	var filteredString = "";

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (allowedValues.indexOf(c)>=0) filteredString += c;
	}

	if(returnFilteredString)
	{
	  return filteredString;
	}
	else
	{
	  input.value = filteredString;
	}
}

function checkEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid E-mail")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid E-mail")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid E-mail")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid E-mail")
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    alert("Invalid E-mail")
	    return false
	 }

		 return true
}

 // Check for required values
function checkrequired(which) {
        var pass=true;
        if (document.images) {
                for (i=0;i<which.length;i++) {
                        var tempobj=which.elements[i];
                        if (tempobj.name.substring(0,9)=="required_") {
                                if (((tempobj.type=="text"||tempobj.type=="textarea")&&
                                tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
                                tempobj.selectedIndex==0)) {
                                        pass=false;
                                        break;
                                }
                        }
                }
        }
        if (!pass) {
                shortFieldName=tempobj.name.substring(9,30).toUpperCase();
                alert("Please make sure the "+shortFieldName+" field was properly completed.");
                return false;
        }
}

 // Check for Confirmation checkbox value
function checkConfirmation(url) {
	if(window.document.forms[0].Confirmation.checked) {
		return true;
	} else {
		window.open(url, 'confirmation', 'width=400,height=200,');
		window.document.forms[0].action = "#";
		submit.form();
	}
}

// Check for a valid Custom Year value
function checkCustomYear(current_year,input) {
	s = input.value;
	year = eval(current_year - s);

	if (year < 15) {
		alert("The model year must be at least 15 years old.");
		input.value = "";
		input.focus();
	} else {
		return true;
	}
}

// Check for a valid Silver-A Year value
function checkSilverAYear(current_year,input) {
	s = input.value;
	year = eval(current_year - s);

	if (s == "") {
		alert("Please enter a model year.");
		input.focus();
	} else if (year < 15) {
		alert("The model year must be at least 15 years old.");
		input.value = "";
		input.focus();
	} else if (year > 24) {
		alert("Please enter this vehicle in the 25 year old vehicles and over section below.");
		input.value = "";
		input.focus();
	} else {
		return true;
	}
}

// Check for a valid Silver-B Year value
function checkSilverBYear(current_year,input) {
	s = input.value;
	year = eval(current_year - s);

	if (s == "") {
		alert("Please enter a model year.");
		input.focus();
	} else if (year < 15) {
		alert("The model year must be at least 25 years old.");
		input.value = "";
		input.focus();
	} else if (year >= 15 && year < 25) {
		alert("Please enter this vehicle in the 15 - 24 year old vehicles section above.");
		input.value = "";
		input.focus();
	} else {
		return true;
	}
}

// Check for a previous blank vehicle value
function checkSequence(prior_value,input) {
	if (input.value != "") {
		if (prior_value.value == ""){
			alert("Please complete the vehicle information in sequence.");
			input.value = "";
			prior_value.focus();
		}
	} else {
		return true;
	}
}

// Check for Silver Vehicle Values and Redirect Form Submit function
function checkSilverValues(url,v1A,v2A,v3A,v4A,v5A,v6A,v1B,v2B,v3B,v4B,v5B,v6B) {
	if ((v1A.value == "") && (v2A.value == "") && (v3A.value == "") && (v4A.value == "") && (v5A.value == "") && (v6A.value == "") && (v1B.value == "") && (v2B.value == "") && (v3B.value == "") && (v4B.value == "") && (v5B.value == "") && (v6B.value == "")) {
		alert("To submit a valid Application Form, please fill in details for at least one vehicle.");
		return false;
	} else {
		return checkrequired(this);
		document.forms[0].action = url;
		document.forms[0].submit();
	}
}

// Check for Custom Vehicle Values and Redirect Form Submit function
function checkCustomValues(url,v1,v2,v3,v4,v5,v6) {
	if ((v1.value == "") && (v2.value == "") && (v3.value == "") && (v4.value == "") && (v5.value == "") && (v6.value == "")) {
		alert("To submit a valid Application Form, please fill in details for at least one vehicle.");
		return false;
	} else {
		return checkrequired(this);
		document.forms[0].action = url;
		document.forms[0].submit();
	}
}

// Change the form destination
function changeForm(url) {
	document.forms[0].action = url;
}


