/**
 * Common input validation code for contact forms.
 *
 * @author Pawandeep Singh(c) Versata 2006
 */

var alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var nums   = "0123456789";
var frenchChars = "ÀàÂâÄäÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜüŸÿ«»€₣";

//DONOT USE THIS VALIDATION METHOD -- ALL NEW VALIDATIONS ARE TO BE DONE USING
//THE VALIDATION METHODOLOGY FOLLOWED IN input-field-validation.js

//This code was originally from a free-use source online (URL not known anymore) 
//and then used in the FordDealerSites Trilogy project.
function checkZipCode(locationForm, errorDivName) 
{
	var field = locationForm['postalCode'].value;
	var valid = "0123456789-";
	var hyphencount = 0;
	var message = "";
	var errorDiv = document.getElementById(errorDivName);
	if (field.length!=5 && field.length!=10) 
	{
		errorDiv.innerHTML = arrStrings["apps.inputvalidation.ziperror.numdigits"];
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			errorDiv.innerHTML = arrStrings["apps.inputvalidation.ziperror.invalidchar"];
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			errorDiv.innerHTML = arrStrings["apps.inputvalidation.ziperror.hyphenformat"];
			return false;
		}
	}
	errorDiv.innerHTML = "";
	return true;
}

//DONOT USE THIS VALIDATION METHOD -- ALL NEW VALIDATIONS ARE TO BE DONE USING
//THE VALIDATION METHODOLOGY FOLLOWED IN input-field-validation.js

//Does validation for the e-mail address entered by the user
function validateEmail(locationForm, errorDivName) 
{
	var errorDiv=document.getElementById(errorDivName);
	var field = locationForm['email'].value;
	var emailRegularExpression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if(emailRegularExpression.test(field) == false)
	{
		errorDiv.innerHTML = arrStrings["apps.inputvalidation.emailerror"];
		return false;
	}
	else
	{
		errorDiv.innerHTML = "";
		return true;
	}
}
	
function goodString(OurString, goodChars)
{
	var result = true;
	for (x = 0; x < OurString.length; x++)
	{
		if(goodChars.indexOf(OurString.charAt(x), 0) == -1)
		{
			result = false;
		}
	}
	return result;
}
		
function digitCount(Phone)
{
	var result = true
	var numCount=0;
	var nums = "0123456789"; 
	for (x = 0; x < Phone.length; x++)
	{
		if(nums.indexOf(Phone.charAt(x), 0) != -1)
		{
			numCount++;
		}
	}
	return numCount;		
}
	
//DONOT USE THIS VALIDATION METHOD -- ALL NEW VALIDATIONS ARE TO BE DONE USING
//THE VALIDATION METHODOLOGY FOLLOWED IN input-field-validation.js
function validateOtherFields(objForm, errorDivName)
{
    return (validateInputFields(objForm, errorDivName) == null);
}

/**
 * This method should be used if we need obtain the exact error that was caused while filling a form.
 */
function validateInputFields(objForm, errorDivName)
{
    var FirstName       = Trim(objForm['firstName'].value);
    var LastName        = Trim(objForm['lastName'].value);
    var Phone           = Trim(objForm['phone'].value);
    var EMail           = Trim(objForm['email'].value);
    var Street          = Trim(objForm['street'].value);
    var errorDiv        = document.getElementById(errorDivName);

    if (FirstName.length < 2 || !goodString(FirstName, alphas + ".- "))
    {
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.firstname"];
        return 'first';
    }
    else if (LastName.length < 2 || !goodString(LastName, alphas + "- "))
    {  
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.lastname"];
        return 'last';
    }
    else if (Street.length < 2)
    {  
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.street"];
        return 'street';
    }
    else if (!checkZipCode(objForm, errorDivName))
    {
        return 'zip';
    }
    else if (digitCount(Phone) != 10 || !goodString(Phone, nums + "/\-() "))
    {
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.phone"];
        return 'phone';
    }
    else if (!validateEmail(objForm, errorDivName)) 
    {
        return 'email';
    }
    return null;
}

function validatePrefContactTime(objForm, errorDivName)
{
    var PrefContactTime = Trim(objForm['preferredContactTime'].value);
    var errorDiv        = document.getElementById(errorDivName);

    if((PrefContactTime != "") && !goodString(PrefContactTime, nums + ".:"))
    {
	    errorDiv.innerHTML = arrStrings["apps.inputvalidation.prefcontacttime"];
    	return false;
    }
    return true;
}

//This method ensures that the pick up date and time is <=
//the drop off date and time
function validatePickUpDropOffDates(objForm, errorDivName)
{
    var pickMonthDropDown = document.getElementById("monthDropDown"); 
    var pickDateDropDown = document.getElementById("dateDropDown");
    var pickTimeDropDown = document.getElementById("preferredTimeDropDown");
	
    var dropMonthDropDown = document.getElementById("ooMonthDropDown");
    var dropDateDropDown = document.getElementById("ooDateDropDown");
    var dropTimeDropDown = document.getElementById("preferredOtherTimeDropDown");
	
    var selectedPickDate = pickDateDropDown.selectedIndex;
    var pickMonthIndex = pickMonthDropDown.selectedIndex;	
    var dropMonthIndex = dropMonthDropDown.selectedIndex;

    var selectedDropDate  = dropDateDropDown.selectedIndex;
    var pickTimeIndex = pickTimeDropDown.selectedIndex;	
    var dropTimeIndex = dropTimeDropDown.selectedIndex;
	
    var errorDiv = document.getElementById(errorDivName);

    //Check if the drop off month is < the pick up month
    //If yes then return false
    if(dropMonthIndex < pickMonthIndex)
    {
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.pickupdatetime"];
        return false;
    }
    else if(pickMonthIndex == dropMonthIndex)
	{
        //Check if the selected drop date is < pick up date
        //If yes then return false and throw an error
        if(selectedDropDate < selectedPickDate)
        {
            errorDiv.innerHTML = arrStrings["apps.inputvalidation.pickupdatetime"];
            return false;
        }
        else if(selectedDropDate == selectedPickDate)
        {
            //Check if the selected drop time is < pick up time
            //If yes then return false and throw an error
            if(dropTimeIndex < pickTimeIndex)
            {
                errorDiv.innerHTML = arrStrings["apps.inputvalidation.pickupdatetime"];
                return false;
            }
        }		
    }
    return true;	
}				


function validateMileage(objForm, errorDivName)
{
    var errorDiv   = document.getElementById(errorDivName);
    var Mileage  = Trim(objForm['mileage'].value);
    
    if ((Mileage != "") && !goodString(Mileage, nums + "."))
    {
        errorDiv.innerHTML = arrStrings["apps.inputvalidation.mileage"];
        return false;
    }
    return true;
}
	
function addOption(selectbox,text,value)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

function hasOptions(obj) 
{
	if (obj!=null && obj.options!=null) 
	{ 
		return true; 
	}
	return false;
}

function removeAllOptions(from) 
{ 
	if (!hasOptions(from)) 
	{ 
		return; 
	}
	for (var i=(from.options.length-1); i>=0; i--) 
	{ 
		from.options[i] = null; 
	} 
	from.selectedIndex = -1; 
} 
	
