
/*
    Search by address
*/
function SearchByAddress(theForm) {

    var va = new Validator();
    var er = new ErrorRenderer();

    address = new String(document.getElementById(""+CONSTANT_ADDRESS+"").value);
    city    = new String(document.getElementById(""+CONSTANT_CITY+"").value);

    if (address.length == 0 && city.length < 2) {
        var addressErr = new Error(document.getElementById(""+CONSTANT_ADDRESS+""),'You must enter at least one character here');
        va.addError(addressErr);

        var cityErr = new Error(document.getElementById(""+CONSTANT_CITY+""),'You must enter at least two characters here');
        va.addError(cityErr);
    }

    if (address.length > 0 && address.match(/[?\"'%"]/)) {
        var addressErr = new Error(document.getElementById(""+CONSTANT_ADDRESS+""),"The characters ? \" ' % can't be used here");
        va.addError(addressErr);
    }
    if (city.length >= 2 && city.match(/[?\"'%"]/)) {
        var cityErr = new Error(document.getElementById(""+CONSTANT_CITY+""),"The characters ? \" ' % can't be used here");
        va.addError(cityErr);
    }
    er.setHeader("One or more details are incorrect in the form below.");
    return er.displayErrors(theForm,va.getErrors());
}

/*
    Search by zipcode
*/
function SearchByZipCode (theForm) {

    var va = new Validator();
    var er = new ErrorRenderer();

    zipcode = new String(document.getElementById(""+CONSTANT_ZIPCODE+"").value);
    zipcode = zipcode.replace(/(\s)/g,"");

    if (zipcode.length == 0){
        var err = new Error(document.getElementById(""+CONSTANT_ZIPCODE+""),'You must enter at least three characters here');
        va.addError(err);
    } else if (zipcode.match(/[^[0-9]/g)) {
        var err = new Error(document.getElementById(""+CONSTANT_ZIPCODE+""),'The code contains invalid characters (non-numeric)');
        va.addError(err);
    } else if (zipcode.length < 3) {
        var err = new Error(document.getElementById(""+CONSTANT_ZIPCODE+""),'You must enter at least three characters here');
        va.addError(err);
    } else if (zipcode.length > 5 && !va.checkZipcode(zipcode)) {
        var err = new Error(document.getElementById(""+CONSTANT_ZIPCODE+""),'Postal codes may only contain numbers and blank spaces');
        va.addError(err);
    } else if (zipcode.length == 5) {
        zipcode = zipcode.substring(0,3) + " " + zipcode.substring(3,5);
    }

    document.getElementById(""+CONSTANT_ZIPCODE+"").value = zipcode;

    er.setHeader("One or more details are incorrect in the form below.");
    return er.displayErrors(theForm,va.getErrors());
}

/*
    Search at Hitta.se
*/
function openHitta(theForm) {
    var er = new ErrorRenderer();
    var va = new Validator();



    var url = new String();
    action = theForm.action;
    url = action + "?TextBoxWho=" + theForm.TextBoxWho.value;
    url += "&TextBoxWhere=" + theForm.TextBoxWhere.value;
    url += "&SearchType=" + theForm.SearchType.value;

    er.setHeader("One or more details are incorrect in the form below.");
    if (er.displayErrors(theForm,va.getErrors())) {
        window.open(url,'','width=800,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,directories=yes,status=yes');
    }
    return false; // Form should never be submitted
}


/*
    Open international sites
*/
function openInternationalSites(theForm){   
    var theField = theForm.iljlist;
    var va = new Validator();
    var er = new ErrorRenderer();

    if (!va.isListSelected(theField)) {
        var err = new Error(theField,'Please choose a country');
        va.addError(err);
    }
    er.setHeader("One or more details are incorrect in the form below.");
    if (er.displayErrors(theForm,va.getErrors())) {
        window.open(va.getListValue(),'internationalpostalsites','width=800,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,directories=yes,status=yes');
    }
    return false; // Form should never be submitted
}


/*
    Download zipcodes
*/
function downLoadZipCodes (theForm) {

    var va = new Validator();
    var er = new ErrorRenderer();

    if (!va.isRadioSelected(theForm.fileversion)) {
        var err = new Error(theForm.fileversion,'Please choose a version before downloading');
        va.addError(err);
    } else {
        if (va.getRadioValue() == 'windows') {
            document.location = 'bin/pguide.exe';
        } else if (va.getRadioValue() == 'mac') {
            document.location = 'bin/pguide_mac.sit';
        }
    }

    er.setHeader("One or more details are incorrect in the form below.");
    er.displayErrors(theForm,va.getErrors());

    return false; // Form should never be submitted
}

