window.onload = function() {

    //clear default values in select fields

    var purchasePrice = document.getElementById("purchasePrice");
    var loanAmount = document.getElementById("loanAmount");
    var propertyZIP = document.getElementById("propertyZIP");
    var creditScore = document.getElementById("creditScore");
    var grossIncome = document.getElementById("grossIncome");
    var monthlyDebt = document.getElementById("monthlyDebt");
    var email = document.getElementById("email");
    var faqPopUp = document.getElementById("faqs");


    prepareFields1();
    prepareFields2();
    toggleTabs();

    //FAQ page pop-up window code 5/11/09
    if (faqPopUp != null) {
        faqPopUp.onclick = function() {
            mywindow = window.open("/faqs", "mywindow", "location=0,status=0,scrollbars=1,width=660,height=500");
            mywindow.moveTo(10, 10);
            return false;
        }
    }


    //workouts

    //		var workout1 = document.getElementById("WO1");
    //		var workout1Row = document.getElementById("workout1Row");

    //		workout1.onclick = function() {
    //			workout1Row.style.display = 'none';
    //		}


    function prepareFields1() {

        if (!document.getElementsByTagName) return false;
        if (!document.getElementById) return false;
        if (!document.getElementById("purchase_tab")) return false;

        var area = document.getElementById("purchase_tab");
        var fields = document.getElementsByTagName("input");

        for (var i = 0; i < fields.length; i++) {
            if (fields[i].className == 'pre-filled')
                continue;
            fields[i].onfocus = function() {
                //alert(this.id);
                highLightField(this);
            }
        }

        for (var i = 0; i < fields.length; i++) {
            if (fields[i].className == 'pre-filled')
                continue;
            fields[i].onblur = function() {
                revertField(this);
            }
        }

    }

    function prepareFields2() {

        if (!document.getElementsByTagName) return false;
        if (!document.getElementById) return false;
        if (!document.getElementById("refinance_tab")) return false;

        var area = document.getElementById("refinance_tab");
        var fields = document.getElementsByTagName("input");

        var debug = "";

        for (var i = 0; i < fields.length; i++) {
            if (fields[i].className == 'pre-filled')
                continue;
            fields[i].onfocus = function() {
                //alert(this.id);
                highLightField(this);
            }
        }

        for (var i = 0; i < fields.length; i++) {
            if (fields[i].className == 'pre-filled')
                continue;
            fields[i].onblur = function() {
                revertField(this);
            }
        }

    }

    function highLightField(objectID) {

        //alert(objectID);
        objectID.style.color = '#000';
        objectID.style.backgroundColor = '#ffc';
        if (objectID.defaultValue == objectID.value) objectID.value = '';
    }


    function revertField(objectID) {

        if (objectID.value == '') {

            objectID.value = objectID.defaultValue;
            objectID.style.color = '#999';

        }
        objectID.style.backgroundColor = '#fff';

    }


    function toggleTabs() {

        /*

         This function toggles the correct workout tab on or off and shows the correct
         accordion based on the user's selection. - Joe

         */

        var type = document.getElementById("tabByType");
        if (type == null) return;
        var importance = document.getElementById("tabByImportance");
        var ease = document.getElementById("tabByEase");

        //hide the importance and ease tabs when the page loads.
        var importanceAccordion = document.getElementById("importanceAccordion");
        if (importanceAccordion != null) importanceAccordion.style.display = 'none';
        var easeAccordion = document.getElementById("easeAccordion");
        if (easeAccordion != null) easeAccordion.style.display = 'none';


        //if the Type tab is selected

        type.onclick = function() {

            //make the selected tab active
            type.className = 'active';

            //make the other tabs inactive
            importance.className = '';
            ease.className = 'last';

            //show the relevant workouts based on the selected sorting option
            document.getElementById("typeAccordion").style.display = 'block';
            if (importanceAccordion != null) importanceAccordion.style.display = 'none';
            if (easeAccordion != null) easeAccordion.style.display = 'none';
            return false;
        }


        //if the Importance tab is selected

        importance.onclick = function() {

            //make the selected tab active
            importance.className = 'active';

            //make the other tabs inactive
            type.className = '';
            ease.className = 'last';

            //show the relevant workouts based on the selected sorting option
            document.getElementById("typeAccordion").style.display = 'none';
            if (importanceAccordion != null) importanceAccordion.style.display = 'block';
            if (easeAccordion != null) easeAccordion.style.display = 'none';
            return false;
        }

        //if the Ease tab is selected

        ease.onclick = function() {

            //make the selected tab active
            ease.className = 'active';

            //make the other tabs inactive
            type.className = 'first';
            importance.className = '';

            //show the relevant workouts based on the selected sorting option
            document.getElementById("typeAccordion").style.display = 'none';
            if (importanceAccordion != null) importanceAccordion.style.display = 'none';
            if (easeAccordion != null) easeAccordion.style.display = 'block';
            return false;
        }


    }


}