
function CheckSearch(){
    var success = true;
    if(!validateFields()){
        success = false;
    }
    
    // Check to see if the user has not sneakily combined a name search with the 
    // other types of searches
    if ((document.searchForm.company_name.value.length != 0) && (document.searchForm.company_name.value!='TYPE COMPANY NAME')){
        if (checkConflictFields()) {
            resetNameSearch();
            success = false;
        }
    }
    return success;
    
}


/**
 * A user can either search for a company name in the free text field OR search
 * by any of the other option but not both. This functions checks to see if any
 * of the other fields are populated when the company name field has been filled
 */
function checkConflictFields() {
    var f = document.searchForm;
    var conflicts = false;
    if (f.catid.selectedIndex     != 0) { conflicts = true; }
    if (f.tradeid.selectedIndex          != 0) { conflicts = true; }
    if (f.countyid.selectedIndex              != 0) { conflicts = true; }
    return conflicts;
}


function validateFields(){
    if ((document.searchForm.company_name.value.length != 0) && (document.searchForm.company_name.value!='TYPE COMPANY NAME')){
        return validateName();
    } else if (document.searchForm.catid.selectedIndex != 0) {
        return true;
    } else {
        alert("Please enter either a company name or select a trade.");
        return false;
    }
    return true;
}


function validateName() {
    str   = document.searchForm.company_name.value;
    feeld = "Company Name";
    if (checkNameLength()) {
        if (isAlpha(str, feeld)) {
            return true;
        }
    } else {
        return false;
    }
}


function isAlpha( str, feeld ) {
    // Loop through string one character at time,  breaking out of for
    // loop when an non Alpha character is found.
    /*for (i = 0; i < str.length; i++) {
        // Alpha must be between "A"-"Z", or "a"-"z"
        if (!(((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
                ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))||
                ((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))||
                (str.charAt(i) == " ") || (str.charAt(i) == ".") ||
				(str.charAt(i) == "(") || (str.charAt(i) == ")") ||
                (str.charAt(i) == "-") || (str.charAt(i) == "&") || (str.charAt(i) == "'"))) {
            alert('Please do not enter symbols into the ' + feeld + ' field.');
            return false;
        }
    }*/

    return true;
}

function checkNameLength() {
    if (document.searchForm.company_name.value.length > 0) {
        if (document.searchForm.company_name.value.length < 3) {
            alert("Please enter at least 3 characters for the Company Name");
            return false;
        }
    }

    return true;
}


function StripNonNumeric( str ) {
    var resultStr = "";

    // Return immediately if an invalid value was passed in
    if (str + "" == "undefined" || str == null) {
        return null;
    }

    // Make sure the argument is a string
    str += "";

    // Loop through entire string, adding each character from the original
    // string if it is a number
    for (var i=0; i < str.length; i++) {
        if ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) {
            resultStr = resultStr + str.charAt(i);
        }
    }

    return resultStr;
}

function IsNum(numstr, loc) {
    // Return immediately if an invalid value was passed in
    if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "") {
        return true;
    }

    var isValid  = true;
    var decCount = 0;        // number of decimal points in the string

    // convert to a string for performing string comparisons.
    numstr += "";

    // Loop through string and test each character. If any
    // character is not a number, return a false result.
    // Include special cases for negative numbers (first char == '-')
    // and a single decimal point (any one char in string == '.').
    for (i = 0; i < numstr.length; i++) {
        // track number of decimal points
        if (numstr.charAt(i) == ".") {
            decCount++;
        }

        if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") ||
                (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
            alert('Please enter only numbers into the job value field.')
            isValid = false;
            break;
        } else if ((numstr.charAt(i) == "-" && i != 0) ||
                (numstr.charAt(i) == "." && numstr.length == 1) ||
                (numstr.charAt(i) == "." && decCount > 1)) {
            alert('Please use only one decimal point')
            isValid = false;
            break;
        }
    }

    return isValid;
}


function resetSelfSearch() {
    var catid = document.searchForm.catid;

    if (catid.selectedIndex == 0) {
        alert('You must make a Trade selection before choosing any of these options.');
			cleanAllFields();
    }

	resetTopSearch();
}

function CheckSubCat() {
    var classification = document.searchForm.catid;
    if (classification.selectedIndex == 0) {
        document.searchForm.subcatid.selectedIndex = 0;
        alert('You must make a Trade selection before choosing any of these options.');
    }
}

function countyidChanged() {
    if (document.searchForm.countyid.selectedIndex != 0) {
    }
    
    resetNameSearch();
}



function resetNameSearch() {
    var name  = document.searchForm.company_name;

    if ( name.value > "" ) {
        alert('You cannot combine this search with a Company Name search')
        document.searchForm.countyid.selectedIndex    = 0;

    }
}


function cleanNonReg() {
    var name = document.searchForm.company_name;
    if (name.value > "") {
        name.value = "";
        cleanAllFields();
    }
}


function cleanAllFields() {
    document.searchForm.catid.selectedIndex     = 0;
    document.searchForm.tradeid.selectedIndex  = 0;
    document.searchForm.countyid.selectedIndex  = 0;
}

function fixSub() {
    var theSelect = document.searchForm.subcatid.options;

    theSelect.length = 0;

    for (var i = 0; i < subClassList[0].length; i++) {
        var option = new Option(subClassList[0][i]);
        eval("document.searchForm.subcatid.options[i] = option");
        if (navigator.appName == 'Netscape' &&
                (navigator.appVersion.indexOf('Win') != -1 ||
                navigator.appVersion.indexOf('Mac') != -1)) {
            history.go(0);
        }
    }
}

function stateChanged() {
    if (document.searchForm.countyid.selectedIndex != 0) 
    resetNameSearch();
}



