function validateZip() {
    var zip = document.dealerSearch.address.value;
    if (zip.length < 5) {
        throwZipError();
        return false;
    }
    var validChars = '0123456789';
    for (var i = 0; i < zip.length; i++) {
        if (validChars.indexOf(zip.charAt(i)) == -1){
            throwZipError();
            return false;
        }
    }
    return true;
}
function throwZipError() {
    // if other zip error is displayed, get rid of it first.
    var invalidZipEle = document.getElementById('invalidZip');
    if (invalidZipEle) {
        invalidZipEle.style.display = 'none';
    }
    var zipEle = document.getElementById('noNumAddress');
    var zipLabelEle = document.getElementById('zipLabel');
    var zipErrorIconEle = document.getElementById('zipErrorIcon');
    zipEle.style.display = 'block';
    zipLabelEle.style.color = '#f00';
    zipErrorIconEle.style.display = 'block';
    document.fydSearchForm.address.focus();
}

function removeZipError() {
    var zipEle = document.getElementById('noNumAddress');
    var zipLabelEle = document.getElementById('zipLabel');
    var zipErrorIconEle = document.getElementById('zipErrorIcon');
    zipEle.style.display = 'none';
    zipLabelEle.style.color = '#fff';
    zipErrorIconEle.style.display = 'none';
}

function validMarketZip() {
    zip = document.getElementById("address").value;
    document.getElementById("address").focus();
    /*Need to have this check because we do not populate zip code if request is from VSSP*/
    if (zip != null) {
      MarketManager.getMarketByZipCode(zip, marketCallBack);
   }
}

var marketCallBack = function (data) {
    if (data.zipCode != null) {
        document.fydSearchForm.marketZipError.value = "false";
    } else {
       document.fydSearchForm.marketZipError.value = "true";
    }
}

var zipFormat = /[0-9][0-9][0-9][0-9][0-9]/;

function validateForm() {
    removeZipError();
    var form = document.fydSearchForm;
    if(!form.address.value || form.address.value.length < 5 || form.address.value == '00000' || !form.address.value.match(zipFormat) || document.getElementById("marketZipError").value == 'true') {
        throwZipError();
    } else {
        document.fydSearchForm.submit();
    }
}

function checkZipLength() {
    zip = document.getElementById("address").value;
    if (zip.length == 5) {
            document.getElementById("fydSubmitButton").focus();
    }
}

