//Copyright RealGo, Inc. 2002 All Rights Reserved
    var schoolData;

    function getSchools(advanced) {
        var request = RGXMLHttp.create();
        request.open("GET", "/Listings/index.cfm?Action=GetSchools", false);
        request.send();
        schoolData = eval(request.responseText);
        updateSchools(advanced);
    }
    function updateSchools(advanced) {
        if(document.getElementById("elemSchoolContainer")) {
            var cityList = getSelections("cities");

            function buildSchoolSelect(schoolType, displayType, oldSchool) {
                var schoolList = schoolData[schoolType + "SchoolList"];
                var schoolSelect = "";
                var selectName = schoolType + "School";
                if (cityList.length > 0 && schoolList.length > 0) {
                    for (var i = 0; i < schoolList.length; i++) {
                        var schoolId = schoolList[i].SchoolId;
                        // if the current array element's city is in the city list, add the value and text
                        if (schoolList[i].CityId >= 0 && arrayFind(cityList, schoolList[i].CityId)) {
                            schoolSelect += '<option value=' + schoolId +
                                (advanced && schoolId == oldSchool ? ' selected="selected"' : '') +
                                '>' +  schoolList[i].Name + '</option>';
                        }
                    }
                }
                document.getElementById(schoolType + "SchoolContainer").innerHTML = '<select name="' + selectName + '" id="' + selectName + '"><option value="0">No ' +
                    (schoolSelect.length == 0
                        ? displayType + ' schools available.</option>'
                        : 'Preference</option>' + schoolSelect        ) + '</select>';
            }
            buildSchoolSelect("elem",   "elementary", "");
            buildSchoolSelect("middle", "middle",     "");
            buildSchoolSelect("high",   "high",       "");
        }
    }


        function showAdvancedOptions() {
//            getSchools(false);
            document.getElementById("advancedOptionsButton").style.display="none";
            classNames = document.getElementById("criteriaTable").className;
            classNames = classNames.replace(/HideAdvanced/,"ShowAdvanced");
            document.getElementById("criteriaTable").className = "SearchTable CriteriaContainer ShowAdvanced";
            document.getElementById("showAdvanced").value = "1";
        }


    function submitThat(criteria) {
        var errors = new Array();
        errors = checkField(errors, criteria.TotalSQFTMin, "Minimum Total Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.TotalSQFTMax, "Maximum Total Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.PriceMin, "Minimum Price must be a whole dollar amount.", isPrice);
        errors = checkField(errors, criteria.PriceMax, "Maximum Price must be a whole dollar amount.", isPrice);
        errors = checkField(errors, criteria.YearBuiltMin, "Beginning Year Built must be a four digit year.", isInteger, 1000, 9999);
        errors = checkField(errors, criteria.YearBuiltMax, "Ending Year Built must be a four digit year.", isInteger, 1000, 9999);
        errors = checkField(errors, criteria.BuildingSQFTMin, "Minimum Building Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.BuildingSQFTMax, "Maximum Building Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.LeaseSQFTMin, "Minimum Lease Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.LeaseSQFTMax, "Maximum Lease Sqft must be an integer.", isInteger);
        errors = checkField(errors, criteria.Subdivision, "At least 2 letters needed for a Subdivision search.",isString,2);
        if (errors.length > 0) {
            alert(errors.join('\n'));
            return false; //failure
        }
        return true; //success
    }


function isMLSNumber(number) {
    return (isInteger(number, 100000, 999999) || isInteger(number, 1000, 9999));
}


function isPrice(value) {
    if (value == null) return false;
    var testChar;
    for (var i=0; i<value.length; i++) {
        testChar = value.charAt(i);
        if ( ! (isDigit(testChar) || testChar == '$' || testChar == ',') ) {
            return false;
        }
    }
    return true;
}

function isInteger(number, minVal, maxVal) {
    if (number == null) return false;
    for (var i=0; i<number.length; i++) {
        if ( ! isDigit(number.charAt(i)) ) {
            return false;
        }
    }
    if (arguments.length >= 2 && number < minVal) {
        return false;
    }

    if (arguments.length >= 3 && number > maxVal) {
        return false;
    }

    return true;
}

function isString(testStr, minLen, maxLen) {
    if (testStr == null) return false;
    var len = trim(testStr).length;
    if (len <= 0) return false;

    if (arguments.length >= 2 && len < minLen) {
        return false;
    }

    if (arguments.length >= 3 && len > maxLen) {
        return false;
    }

    return true;
}

function isWhiteSpace(c) {
    if (c == ' ' || c == '\t' || c == '\n') {
        return true;
    }
    return false;
}

function isDigit(n) {
    if (n == '0' || n == '1' || n == '2' || n == '3' || n == '4' ||
        n == '5' || n == '6' || n == '7' || n == '8' || n == '9' ) {
        return true;
    }

    return false;
}

function ltrim(s) {
    var len = s.length;
    for (var i=0; i<len; i++) {
        if (!isWhiteSpace(s.charAt(i))) {
            return s.substring(i,len);
        }
    }
    return "";
}

function rtrim(s) {
    var len = s.length;
    for (var i=len-1; i>=0; i--) {
        if ( !isWhiteSpace(s.charAt(i)) ) {
            return s.substring(0,i+1);
        }
    }
    return "";
}

function trim(s) {
    return rtrim(ltrim(s));
}

function selectedCount(selectRef) {
    var count = 0;
    for (var i=0;i<selectRef.length;i++) {
        if (selectRef.options[i].selected) {
            count++;
        }
    }
    return count > 0;
}

function checkValidDate(year,month,day,dateString)
{
    month = month - 1; /*goofy javascript thing*/
    var test = new Date(year,month,day);

    if ( (test.getFullYear() == year) && (month == test.getMonth()) && (day == test.getDate()) ) {
        return true;
    } else {
        alert(dateString + " is not a valid date!");
        return false;
    }
 }

/*
 Check that a field's value validates against a function fct.
 Extra parameters to fct can be supplied as arguments after the fct pointer.
 Returns error as passed in with new errors added.
*/
function checkField(errors, field, errMsg, fct) {
    if (field == null) return errors; //Ignore null fields, it probably isn't defined for this form

    if (trim(field.value) != 0) {
        var args = "";
        if (arguments.length > 4 ) {
            args = buildArgs(arguments, 4);
        }

        if ( eval("fct(field.value" + args + ")") == false) {
            return errors.concat(errMsg);
        }
    } else {
        //Don't enforce required here
        return errors;
    }

    return errors;
}

function checkRequiredField(errors, field, errMsg, fct) {
    if (field == null) return errors; //Ignore null fields, it probably isn't defined for this form

    if (trim(field.value) != 0) {
        var args = "";
        if (arguments.length > 4 ) {
            args = buildArgs(arguments, 4);
        }

        if ( eval("fct(field.value" + args + ")") == false) {
            return errors.concat(errMsg);
        }
    } else {
        //Required
        return errors.concat(errMsg);
    }

    return errors;
}

function buildArgs(args, startArg) {
    var returnArgs = "";
    for(var i=startArg; i<args.length; i++) {
        returnArgs += ', ' + args[i];
    }
    return returnArgs;
}
