/**
 * Westwood Core JS functions
 */

(function($) {

    var campusSelect;
    var programSelect;

    // Keep ONE doc.ready
    $(document).ready(function() {

        // Initiate Carousels
        $(".carousel-module .carousel").after('<div class="pager">').cycle({
            fx: 'fade',
            timeout: 3000,
            pager: $(this).find(".pager")
        });

        campusSelect = $(".campusSelect select");
        programSelect = $(".programSelect select");
        if (campusSelect.length > 0 && programSelect.length > 0) {
            campusSelect.change(function() {
                setSelect($(this));
            });
            
            //run on page load to handle case of previously selected dropdown
            setSelect(campusSelect);
        }

        $("#main #content .sidebar .sidebar_middle .bar:first-child").attr("class", "solidbar");

        // ROUNDED CORNERS
        /*
        if ($("#content_body .scfForm .scfSectionContent").length > 0) {
            $("#content_body .scfForm").append("<span class='corner tl'></span><span class='corner bl'></span><span class='corner tr'></span><span class='corner br'></span>");
        }*/

        $("#tabbed_content_bottom .sidebar_middle .bar:first-child").addClass("first");


        //logic is done on blur of zip code field
        $(".scfForm span.scfCheckbox input").attr('checked', 'checked');
        $('.scfForm .zipcode input').blur(function() {
            //retrieve value of zip code field
            var zipCodeValue = $('.scfForm .zipcode input').val();
            var zipCodeValue = jQuery.trim(zipCodeValue);

            var test = jQuery.get("/zips.csv", function(data) {
                array = jQuery.csv()(data);
                for (i = 0; i < array.length; i++) {
                    if (array[i].indexOf(zipCodeValue) != -1) {
                        var foundValue = true;
                    }
                }
                if (foundValue) {
                    $(".scfForm span.scfCheckbox").attr('style', 'display:inline');
                    $(".scfForm span.scfCheckboxUsefulInfo").attr('style', 'display:inline');
                    $(".scfForm span.scfCheckbox input").attr('checked', null);
                }
                else {
                    $(".scfForm span.scfCheckbox").attr('style', 'display:none');
                    $(".scfForm span.scfCheckboxUsefulInfo").attr('style', 'display:none');
                    $(".scfForm span.scfCheckbox input").attr('checked', 'checked');
                }
            });
        });

        $('.scfForm input[type=submit]').click(function(e) {
            if ($(".scfForm span.scfCheckbox input").length > 0 && $(".scfForm span.scfCheckbox input").attr('checked') != "checked") {
                alert("Please check the box to consent to be called by Westwood College at the phone number provided above to discuss educational opportunities.");
                e.preventDefault();
            }

            //check for invalid zip code/campus combinations
            collection = new Object();
            //add entry for each campus with array of zip code prefixes to look for
            collection["19"] = new Array(350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 307, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 723, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 411, 412, 413, 414, 415, 416, 417, 418, 420, 421, 422, 423, 425, 426, 427, 471, 197, 198, 199);

            isValid = true;

            campusSelect = jQuery(".campusSelect select");

            for (campus in collection) {
                if (campusSelect.val() == campus) {
                    for (i = 0; i < collection[campus].length; i++) {
                        if (jQuery(".scfForm .zipcode input").val().slice(0, 3) == collection[campus][i]) {
                            isValid = false;
                        }
                    }
                }
            }

            if (!isValid) {
                alert("Westwood College's online campus is not currently accepting residents from your state for its programs. If you are interested in attending Westwood College please select one of our ground campuses. ");
                e.preventDefault();
            }
            
        });

    })  // doc.ready

    $(window).load(function() {

        //alert form error for future year
        errmsg = $(".scfForm .futureyear");
        if (errmsg.length > 0)
            alert(errmsg.text());
    });

    // Move UTIL functions out of doc.ready
    function setSelect(arg) {
        $.ajax({
            url: "/campuses.xml",
            datatype: "xml",
            success: function(data) {
                //alert(data.getElementsByTagName("campus").length);
                campus = $(data).find("campus[id=" + arg.val() + "]");

                programSelect[0].options.length = 0;
                addOption("Select Program", "");

                campus.find("program").each(function() {
                    addOption($(this).attr("name"), $(this).attr("id"));
                });
            }
        });
    }

    function addOption(text, val) {
        var opt = new Option(text, val);
        $(opt).html(text); //doing this to get it to work in ie 7/8            
        programSelect.append(opt);
    }

})(jQuery);



