Validation.addAllThese([['copart-email-required', 'Email error.', function(v, elm){
    return validateEmailAddress(v, elm);
}
], ['copart-confirm-email-required', 'Confirm email error', function(v, elm){
    return validateConfirmEmailAddress(v, elm);
}
], ['copart-name-required', 'Name error.', function(v, elm){
    return validateName(v, elm);
}
], ['copart-alphaNumSpace', 'Alphanum error.', function(v, elm){
    return validateAlphaNumericSpace(v, elm);
}
], ['copart-alphaNumSpace-modified', 'Alphanum error.', function(v, elm){
    return validateAlphaNumericSpaceModified(v, elm);
}
], ['copart-required-alphaNumSpace', 'Required Alphanum error.', function(v, elm){
    return validateRequiredAlphaNumericSpace(v, elm);
}
], ['copart-required-alphaNum', 'Required Alphanum error.', function(v, elm){
    return validateRequiredAlphaNumeric(v, elm);
}
], ['copart-required', 'Required error.', function(v, elm){
    return validateCopartRequired(v, elm);
}
], ['copart-phone-required', 'Phone error.', function(v, elm){
    return validatePhoneNumbers(v, elm);
}
], ['copart-checkbox-required', 'Checkbox Required error.', function(v, elm){
    return validateCheckboxRequired(v, elm);
}
], ['copart-required-hearAbout', 'Required error.', function(v, elm){
    return validateCopartRequiredHearAbout(v, elm);
}
], ['copart-phone', 'Valid Phone.', function(v, elm){
    return validatePhoneNumbersNotRequired(v, elm);
}
], ['copart-international-phone-required', 'Valid Phone.', function(v, elm){
	return validateInternationPhone(v, elm, true);
}
], ['copart-international-phone', 'Valid Phone.', function(v, elm){
	return validateInternationPhone(v, elm, false);
}
]
]);


function checkIfSpecialCharacterEntered(){
	var defaultAllow = "- , .";
    if (!checkSpecialChars($F("firstName"), defaultAllow)) {
        $("noSpecialCharss").style.display = "block";
    }
    else 
        if (!checkSpecialChars($F("lastName"), defaultAllow)) {
            $("noSpecialCharss").style.display = "block";
        }
        else 
            if (!checkSpecialChars($F("company"), defaultAllow)) {
                $("noSpecialCharss").style.display = "block";
            }
            else 
                if (!checkSpecialChars($F("physicalAddress.line1"), defaultAllow)) {
                    $("noSpecialCharss").style.display = "block";
                }
                else 
                    if (!checkSpecialChars($F("physicalAddress.line2"), defaultAllow)) {
                        $("noSpecialCharss").style.display = "block";
                    }
                    else 
                        if (!checkSpecialChars($F("physicalAddress.city"), defaultAllow)) {
                            $("noSpecialCharss").style.display = "block";
                        }
                        else 
                            if (!checkSpecialChars($F("physicalAddress.zip.zip1"), null)) {
                                $("noSpecialCharss").style.display = "block";
                            }
                            else 
                                if (!checkSpecialChars($F("physicalAddress.zip.zip2"), null)) {
                                    $("noSpecialCharss").style.display = "block";
                                }
                                else 
                                    if (!checkSpecialChars($F("hearAboutOtherValue"), defaultAllow)) {
                                        $("noSpecialCharss").style.display = "block";
                                    }
                                    else {
                                        $("noSpecialCharss").style.display = "none";
                                    }
}

/**
 * This method will check if special characters were entered into a field
 * and present our special character message. We can pass space separated
 * exclusions per field that we will remove from our regex.
 * @param {Object} data - field value to test
 * @param {Object} exclusions - space delimited characters to ignore as special.
 */
function checkSpecialChars(data, exclusions){
    var exclude = "";
    if (exclusions != null) {
        exclude = exclusions.split(" ");
    }
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";
    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;
}

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;
}

function isNotNumeric(data) {
	var regEx = /[^0-9]/;
	return data.match(regEx);
}

function onHearChange(){
    if ("009:Other" == $F("hearAboutText")) {
        $("hearAboutSpan").show();
    }
    else {
        $("hearAboutSpan").hide();
    }
    
}

function brfValidate(valid, formName) {
    //checkIfSpecialCharacterEntered();
    valid.reset();
    hideErrorImages();
    if (valid.validate()) {
	$(formName).submit();
    }
}


function submitBuyerRegForm(formName,action){
	$(formName).destination.value=action;
	$(formName).submit();
}

function openApplicationForm(){
	var winHandle = window.open('buyerReg', 'Buyer Application Form', 'width=700,height=700,resizable=yes,scrollbars=1');
	winHandle.focus();
}



/* Form submission to testcopart.com - NOT USED */

function submitBuyerRegFormToNetData(formName) {
	var formData = getFormVariables(formName);
	var url = 'http://testcopart.co.uk/cgi-bin/public/buyer/signup/c2index.dtw/dealer';
	var method = 'POST';
	var data = '?';
	
	// build key=value string using target form element names
	data += 'cfname='+escape(formData.get('firstName'));
	data += '&clname='+escape(formData.get('lastName'));
	data += '&cname='+escape(formData.get('company'));
	data += '&baddr1='+escape(formData.get('address.line1'));
	data += '&baddr2='+escape(formData.get('address.line2'));
	data += '&bcity='+escape(formData.get('address.city'));
	data += '&bcountry='+escape(formData.get('address.country'));
	data += '&bzip1='+escape(formData.get('address.zip.zip1'));
	data += '&bzip2='+escape(formData.get('address.zip.zip2'));
	data += '&email='+escape(formData.get('email'));
	data += '&mailZip='+escape(formData.get('mailZip'));
	data += '&phZip='+escape(formData.get('phZip'));
	data += '&email2='+escape(formData.get('confirmEmail'));
//	data += '&vb2terms='+escape(formData.get('privacyPolicy'));
	data += '&phone1='+escape(formData.get('primaryPhone.areaCode'))+
		escape(formData.get('primaryPhone.prefix'))+escape(formData.get('primaryPhone.line'));
	data += '&phone2='+escape(formData.get('altPhone.areaCode'))+
		escape(formData.get('altPhone.prefix'))+escape(formData.get('altPhone.line'));
	data += '&mailsame=true'
	
	openNetDataWindow(url, data);
}

//function open window
function openNetDataWindow(url, data) {
	var winHandle = window.open(url+data, 'netdata', 'width=700,height=700,scrollbars=1');
	winHandle.focus();
}

//function getFormVariables
function getFormVariables(formName){
    var formvars= new Hash();
    var inputElements=document.getElementById(formName).getElementsByTagName('input');

    for(var i=0;i<inputElements.length;i++){
   		formvars.set(inputElements[i].name, inputElements[i].value);
    }

    var selectElements=document.getElementById(formName).getElementsByTagName('select');
    for(var i=0;i<selectElements.length;i++){
    	var index = selectElements[i].selectedIndex;
    	var text = selectElements[i][index].text; 
   		formvars.set(selectElements[i].name, text);
    }
    
    return formvars;
}

/* End form submission to testcopart functions */

/* Validation Functions */
function validateEmailAddress(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 validateConfirmEmailAddress(v, elm){
    var isValid = false;
    
	v = trimWS(v);
	var regEmail = $F('email');
	regEmail = trimWS(regEmail);
	$('email').value = regEmail;
	$(elm).value = v;
	
    isValid = Validation.get('required').test(v);
    
    if (isValid) {
        isValid = Validation.get('validate-email').test(v);
    }
    
    if (isValid) {
        isValid = (v == regEmail);
    }
    
    return showErrorImage(elm.id, isValid);
}

function validatePhoneNumbers(v, elm){
    var whichPhone = elm.id.substring(0, elm.id.indexOf('.'));
    var phone1Valid = false;
    var phone2Valid = false;
    var phone3Valid = false;
    var phoneErrorImageName = 'PhoneNumber';
    
    if (whichPhone != 'primaryPhone') {
        phoneErrorImageName = 'altPhone.number';
    }
	
	var phone1 = $(whichPhone + '.areaCode').value;
    var phone2 = $(whichPhone + '.prefix').value;
    var phone3 = $(whichPhone + '.line').value;
    
    // Required Check
    phone1Valid = Validation.get('required').test($F(whichPhone + '.areaCode'));
    phone2Valid = Validation.get('required').test($F(whichPhone + '.prefix'));
    phone3Valid = Validation.get('required').test($F(whichPhone + '.line'));
    
    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($F(whichPhone + '.areaCode'), null)
            phone2Valid = checkSpecialCharsWithSpace($F(whichPhone + '.prefix'), null)
            phone3Valid = checkSpecialCharsWithSpace($F(whichPhone + '.line'), null)
            if (phone1Valid && phone2Valid && phone3Valid) {
                // Numeric Check
                phone1Valid = Validation.get('validate-number').test($F(whichPhone + '.areaCode'));
                phone2Valid = Validation.get('validate-number').test($F(whichPhone + '.prefix'));
                phone3Valid = Validation.get('validate-number').test($F(whichPhone + '.line'));
                if (phone1Valid && phone2Valid && phone3Valid) {
                    return showErrorImage(phoneErrorImageName, true);
                }
            }
        }
    }
    return showErrorImage(phoneErrorImageName, false);
}

function validatePhoneNumbersNotRequired(v, elm){
    var whichPhone = elm.id.substring(0, elm.id.indexOf('.'));
    var phone1Valid = false;
    var phone2Valid = false;
    var phone3Valid = false;
    var phoneErrorImageName = 'PhoneNumber';
    
    if (whichPhone != 'primaryPhone') {
        phoneErrorImageName = 'altPhone.number';
    }
	
	var phone1 = $(whichPhone + '.areaCode').value;
    var phone2 = $(whichPhone + '.prefix').value;
    var phone3 = $(whichPhone + '.line').value;
    
    // Special Characters Check
    phone1Valid = checkSpecialCharsWithSpace($F(whichPhone + '.areaCode'), null)
    phone2Valid = checkSpecialCharsWithSpace($F(whichPhone + '.prefix'), null)
    phone3Valid = checkSpecialCharsWithSpace($F(whichPhone + '.line'), null)
    
    if (phone1Valid && phone2Valid && phone3Valid) {
        // Numeric Check
        phone1Valid = Validation.get('validate-number').test($F(whichPhone + '.areaCode'));
        phone2Valid = Validation.get('validate-number').test($F(whichPhone + '.prefix'));
        phone3Valid = Validation.get('validate-number').test($F(whichPhone + '.line'));
        if (phone1Valid && phone2Valid && phone3Valid) {
            if (phone1.length > 0 || phone2.length > 0 || phone3.length > 0) {
                phone1Valid = validateMinLength(phone1, 3);
                phone2Valid = validateMinLength(phone2, 3);
                phone3Valid = validateMinLength(phone3, 4);
                if (phone1Valid && phone2Valid && phone3Valid) {
                    return showErrorImage(phoneErrorImageName, true);
                }
                return showErrorImage(phoneErrorImageName, false);
            }
            return showErrorImage(phoneErrorImageName, true);
        }
    }
    return showErrorImage(phoneErrorImageName, false);
}

function trim(str){
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

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

function validateAlphaNumericSpace(v, elm){
    var isValid = false;
    
    if (Validation.get('IsEmpty').test(v)) {
        isValid = true;
    }
    else {
        isValid = Validation.get('validate-alphanumspace').test(v);
    }
    return showErrorImage(elm.id, isValid);
}

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

function validateCopartRequired(v, elm){
    var isValid = false;
    isValid = Validation.get('required').test(v);
    return showErrorImage(elm.id, isValid);
}

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

function validateCheckboxRequired(v, elm){
    var isValid = false;
    isValid = elm.checked;
    
    return showErrorImage(elm.id, isValid);
}

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

function validateCopartRequiredHearAbout(v, elm){
    var isValid = false;
    isValid = Validation.get('required').test(v);
    var defaultValue = "000:SELECT";
    
    if (isValid) {
        if (defaultValue == v.toUpperCase()) {
            isValid = false;
        }
    }
    
    return showErrorImage('hearAboutOtherValue', isValid);
}

function validateInternationPhone(v, elm, required) {
	 var isValid = false;
	 var errorImage = '';
	 
	 if(required) {
	 	isValid = Validation.get('required').test(v);
	 }
	 else {
	    v=trim(v);
	 	isValid = (v.length == 0 || v.length == 9 || v.length == 10 || v.length == 11) ? true : false ;	 	
	 }
	 
	 if(required){
	 	v=trim(v);
	 	isValid = v.length == 9 || v.length == 10 || v.length == 11? true : false ;
	 }
	 
	 if(elm.id.indexOf('primaryPhone') >= 0) {
	 	errorImage = 'PhoneNumber';
	 }
	 else {
	 	errorImage = 'altPhone.number';
	 }
	 
	 
	 if(isValid) {
	 	isValid = !isNotNumeric(v, null)
		if(isValid) {
	 		isValid = Validation.get('validate-number').test(v);
			if(isValid) {
				return showErrorImage(errorImage, true);
			}
	 	}
	 }
	 return showErrorImage(errorImage, false);
	 
}

/**
 * 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;
}

/**
 * Reset Validation Error Images to Hidden
 */
function hideErrorImages(){
    var images = $$('.validationErrorImage');
    
    for (var i = 0; i < images.length; i++) {
        images[i].style.display = 'none';
    }
}

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

