/*
 @Name: $RCSfile: howToSellList.js,v $
 @Version: $Revision: 1.10 $
 @Date: $Date: 2012/01/30 21:38:25 $

 Copyright (C) 2008 Copart, Inc. All rights reserved.
 */

// BEGIN: VALIDATION FUNCTIONS

Validation.addAllThese([
    ['copart-required-name', 'Please enter a valid Name.', function(v, elm){
    	return validateName(v, elm);
	}
	],
	['copart-phone-required', 'Phone error.', function(v, elm){
	    return validatePhoneNumbers(v, elm);
	}
	],

	['copart-miles-required', 'Confirm miles error', function(v, elm){
	    return validateMiles(v, elm);
	}
	],
	['copart-required-worth', 'Confirmworth error', function(v, elm){
	    return validateWorth(v, elm);
	}
	],
	['copart-ReferralCode-required', 'Confirm ReferralCode error', function(v, elm){
	    return validateReferralCode(v, elm);
	}
	],
	['copart-confirm-email', 'Confirm email error', function(v, elm){
	    return validateConfirmEmailAddress(v, elm);
	}
	],
	['copart-zip', 'Confirm Zip error', function(v, elm){
	    return validateZip(v, elm);
	}
	],
    ['copart-ReferralCode', 'Please enter a valid Name.', function(v, elm){
    	return validateReferralCode(v, elm);
	}
	],
	['copart-year', 'Please select a Year.', function(v, elm){
	    return validateDropDown(v, elm);
	}
	],
	['copart-loan', 'Please select Loan Information.', function(v, elm){
	    return validateDropDown(v, elm);
	}
	],
	['copart-make', 'Please select a Make.', function(v, elm){
	    return validateDropDown(v, elm);
	}
	],
	['copart-model', 'Please select a Model.', function(v, elm){
	    return validateDropDown(v, elm);
	}
	]
]);

function validateName(v, elm) {
	var isValid = false;
	isValid = Validation.get('required').test(v);
	if(isValid) {
		isValid = Validation.get('validate-name').test(v);
	}
	return showErrorImage(elm.id, isValid);
}

function validateReferralCode(v, elm) {
	var isValid = true;
	isValid = Validation.get('required').test(v);
	if(v.length > 0) {
		isValid = Validation.get('validate-name').test(v);
	}
	return showErrorImage(elm.id, isValid);
}

function validateConfirmEmailAddress(v, elm){
   var isValid = false;

    isValid = Validation.get('required').test(v);

    if (isValid) {
        isValid = Validation.get('validate-email').test(v);
    }

    return showErrorImage(elm.id, isValid);
}



function validatePhoneNumbers(v, elm){
    var phone1Valid = false;
    var phone2Valid = false;
    var phone3Valid = false;
    var phoneErrorImageName = 'phone';

	var phone1 = $F('phoneAreaCode');
    var phone2 = $F('phonePrefix');
    var phone3 = $F('phoneLineNumber');

    // Required Check
    phone1Valid = Validation.get('required').test(phone1);
    phone2Valid = Validation.get('required').test(phone2);
    phone3Valid = Validation.get('required').test(phone3);

    if (phone1Valid && phone2Valid && phone3Valid) {
        phone1Valid = validateMinLength(phone1, 3);
        phone2Valid = validateMinLength(phone2, 3);
        phone3Valid = validateMinLength(phone3, 4);
        if (phone1Valid && phone2Valid && phone3Valid) {
            // Special Characters Check
            phone1Valid = checkSpecialCharsWithSpace(phone1, null)
            phone2Valid = checkSpecialCharsWithSpace(phone2, null)
            phone3Valid = checkSpecialCharsWithSpace(phone3, null)
            if (phone1Valid && phone2Valid && phone3Valid) {
                // Numeric Check
                phone1Valid = Validation.get('validate-number').test(phone1);
                phone2Valid = Validation.get('validate-number').test(phone2);
                phone3Valid = Validation.get('validate-number').test(phone3);
                if (phone1Valid && phone2Valid && phone3Valid) {
                    return showErrorImage(phoneErrorImageName, true);
                }
            }
        }
    }
    return showErrorImage(phoneErrorImageName, false);
}



function validateMiles(v, elm) {
	var isValid = true;
	isValid = Validation.get('required').test(v);
	if(isValid) {
		isValid = Validation.get('validate-digitsWithComma').test(v);
	}

	return showErrorImage(elm.id, isValid);
}
function validateWorth(v, elm) {
	var isValid = true;
	isValid = Validation.get('required').test(v);
	if(isValid) {
		isValid = Validation.get('validate-digitsWithComma').test(v);
	}

	return showErrorImage(elm.id, isValid);
}

function validateReferralCode(v, elm) {
	var isValid = true;

	if(v != ""){
		isValid = Validation.get('validate-digitsAndNumbers').test(v);
	}

	return showErrorImage(elm.id, isValid);
}

function validateDropDown(v, elm){

	var isValid = elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
	if(elm.options.length == 1){
		isValid = true;
	}

	return showErrorImage(elm.id, isValid);
}

function validateZip(v, elm) {
	var isValid = false;
	if (v.length > 0) {
		isValid = Validation.get('validate-alphanum').test(v);
	}
	if(isValid && $F('zip2').length > 0)
	{
		isValid = Validation.get('validate-alphanum').test($F('zip2'));
	}
	return showErrorImage(elm.id, isValid);
}

function validateMinLength(v, minLength){
    if (v != null && minLength != null) {
        var cleanVar = trimWS(v);
        if (cleanVar.length < minLength) {
            return false;
        }
        return true;
    }
    return false;
}

function checkSpecialCharsWithSpace(data, exclusions){
    var exclude = "";
    if (exclusions != null) {
        exclude = exclusions.split(" ");
    }
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_\\s";
    if (exclude.length > 0) {
        for (var i = 0; i < exclude.length; i++) {
            iChars = iChars.replace(exclude[i], "");
        }
    }
    for (var i = 0; i < data.length; i++) {
        if (iChars.indexOf(data.charAt(i)) != -1) {
            return false;
        }
    }
    return true;
}

/**
* Show the Validation Error Image based on the form element's id
* @param {Object} errorElementId - id used to show image.
* @param {Object} noError - boolean flag denoting if error state.
*/
function showErrorImage(errorElementId, noError){
   if (noError) {
       $('errorImage-' + errorElementId).style.display = 'none';
   }
   else {
       $('errorImage-' + errorElementId).style.display = 'inline';
   }
   return noError;
}

// END: VALIDATION FUNCTIONS

function trim (str) {
	return str.replace(/^\s+|\s+$/g,"");
}


function howToSellUpdateModelDropDown(){
    //updateModelDropDown('make', 'model');
	var reqParam = "selectedMake";
    var xmlNodeName = "model";
    var xmlNodeAttribute1 = "code";
    var xmlNodeAttribute2 = "description";

	var make = $F('make');

    if ($F('make') != "*") {
        updateDropDownWith($F('make').toString(), 'model', xmlNodeName, xmlNodeAttribute1, xmlNodeAttribute2, reqParam, message.modelAjax, message.defaultModelDisplay, message.defaultModelValue);
    }else{
		$("model").selectedIndex = 0;
	}
}

var submitFlag = false;

function submitForm(valid){

	if (submitFlag){
		return
	}
	$('btnFreeEstimateSubmit').disabled=true;

	submitFlag = true;
	valid.reset();

	$("phone").value = $F("phoneAreaCode")+$F("phonePrefix")+$F("phoneLineNumber");
	$("00N60000002IFPw").value = $F("year");
	$("00N60000002IFQa").value = $("make").options[$("make").options.selectedIndex].text;
	$("00N60000002IFQk").value = $("model").options[$("model").options.selectedIndex].text;
	$("00N60000002IFV7").value= $F("No_Lien");
	// set retirect url


	$("retURL").value = message['thankYouURL']
	if ($F('zip').length > 0){
		$("retURL").value +="&slzip=" + $F('zip');
	}

	if ($F('referralCode').length > 0){
		$("retURL").value +="&slref=" + $F('referralCode');
	}

	if ($F('miles').length > 0){
		$("retURL").value +="&slmiles=" + $F('miles');
	}


	if ($F('year').length > 0){
		$("retURL").value +="&slyear=" + $F('year');
	}

	if ($F('make') != '*'){
		$("retURL").value +="&slmake=" + $F('make');
	}

	if ($F('model').length > 0){
		$("retURL").value +="&slmodel=" + $F('model');
	}
	if ($F('No_Lien').length > 0){
		$("retURL").value +="&slloan=" + $F("No_Lien");
	}
	if ($F('Desired_Amount').length > 0){
		$("retURL").value +="&slworth=" + $F('Desired_Amount');
	}

	submitFlag = false;
	if(valid.validate()) {
		var form = $('getEstimate');
		// build phone field
		form.phone.value = "(" + form.phoneAreaCode.value + ") " + form.phonePrefix.value + " - " + form.phoneLineNumber.value;

		if (($("zip2").value != null) && (trimWS($("zip2").value) !="") && ($("zip2").value.length>0)) {
			$("zip").value = $("zip1").value+"-"+$("zip2").value;
		}
		else{
			$("zip").value = $("zip1").value;
		}

		form.submit();
	}else{
		submitFlag = false;
		$('btnFreeEstimateSubmit').disabled=false;
	}
}
function validateForm(){
	var form = $(getEstimate);
	var returnValue = true;

	//First Name
	if (trim(form.firstname.value).length == 0){
		returnValue = true;
	}

	//Last Name
	if (trim(form.lastname.value).length == 0){
		returnValue = true;
	}

	//Phone
	if (trim(form.firstname.value).length == 0){
		returnValue = true;
	}

	//eMail
	if (trim(form.firstname.value).length == 0){
		returnValue = true;
	}

	//zip
	if (trim(form.firstname.value).length == 0){
		returnValue = true;
	}

}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring( 0, maxlimit );
		return false;
	}
	else {
		countfield.value = maxlimit - field.value.length;
	}
}

Event.observe(window, "load", function(){

	// set retirect url
	$("retURL").value = message['thankYouURL'] + '?transaction_id' + $F('transaction_id');

	// set page events
	Event.observe('make', 'change', howToSellUpdateModelDropDown);
});

