﻿var search = function() {

    this.myjsonservice = new JSONService();

    this.fetchIndustries = function() {
        this.myjsonservice.loadScript("searchIndustriesfetch_0001", "services/industries.aspx?action=fetch&callback=mysearch.holdIndustriesData&expires=0");
    };

    this.fetchpartnershipFocus = function() {
        this.myjsonservice.loadScript("searchIndustriesfetch_0001", "services/partnershipsearch.aspx?action=fetchfocus&callback=mysearch.holdPartnershipFocus&expires=0");
    };

    this.fetchBorough = function() {

        this.myjsonservice.loadScript("boroughFetch_1", "services/boroughs.aspx?action=fetch&callback=mysearch.holdBoroughData&expires=5");

    };

    this.fetchSchoolType = function() {

        this.myjsonservice.loadScript("schoolTypefetch_0001", "services/schooltype.aspx?action=fetch&callback=mysearch.holdSchooltype&expires=5");
    }




    this.isAdvanceSearch = false;

    this.industriesData = null;

    this.PartnershipFocus = null;

    this.boroughData = null;

    this.holdIndustriesData = function(jsonData) {
        try {

            this.industriesData = eval('(' + jsonData + ')');
            this.fetchpartnershipFocus();
        }
        catch (ex) {
        }
    };

    this.holdPartnershipFocus = function(jsonData) {

        try {

            this.PartnershipFocus = eval('(' + jsonData + ')');
            this.fetchBorough();

        }
        catch (ex) {
        }
    };

    this.holdBoroughData = function(jsonData) {

        try {

            this.boroughData = eval('(' + jsonData + ')');
            this.fetchSchoolType();
        }
        catch (ex) {
        }
    };

    this.schoolData = null;
    this.holdSchooltype = function(jsonData) {

        try {

            this.schoolData = eval('(' + jsonData + ')');
            this.render();
        }
        catch (ex) {
        }
    };

    this.render = function() {
        var strHtml = [];
        var divSearch = document.getElementById("search1");
        strHtml[strHtml.length] = '<table cellspacing="5" cellpadding="0" border="0" width="100%">';
        strHtml[strHtml.length] = '    <tr><td colspan="2"><div class="portlet-title">Search Other Partnerships</div></td></tr>';
        strHtml[strHtml.length] = '    <tr>';
        strHtml[strHtml.length] = '        <td width="100%"><input id="txtpartnershipSearch" type="text"  style="width:195px;height:21px;" /></td>';
        strHtml[strHtml.length] = '        <td align="right"><img src="assets/go.gif" alt="GO" onclick="mysearch.partnershipSearch(event);" /></td>';
        strHtml[strHtml.length] = '    </tr>';

        strHtml[strHtml.length] = '    <tr>';
        strHtml[strHtml.length] = '    <td colspan="2" style="font-family:arial,verdana;font-size:11px;color:gray;">';
        strHtml[strHtml.length] = '    Note: Use quotation marks to search for exact matches.';
        strHtml[strHtml.length] = '    <br />Ex: “John Smith” or “career awareness” or “PS 101”';
        strHtml[strHtml.length] = '    </td>';
        strHtml[strHtml.length] = '    </tr>';
        
        strHtml[strHtml.length] = '    <tr><td colspan="2"><a id="linkadvanced1" href="javascript:mysearch.toggle(linkadvanced1);">Expand Search Options</a></td></tr>';
        strHtml[strHtml.length] = '    <tr id="searchadvanced1" style="display:none">';
        strHtml[strHtml.length] = '        <td colspan="2">';
        strHtml[strHtml.length] = '            <div style="width:100%;background:#e7eaf9;border:solid 1px #404040;">';
        strHtml[strHtml.length] = '                <table cellpadding="0" cellspacing="5" width="100%">';
        strHtml[strHtml.length] = '                    <tr><td>Partnership Goal Area</td></tr>';
        strHtml[strHtml.length] = '                    <tr><td><select style="width:100%;font-size:10px;" id="ddPFocus" onkeypress="mysearch.partnershipSearch(event);"><option value="">-Select-</option></select></td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>School Location</td></tr>';
        strHtml[strHtml.length] = '                    <tr><td><select style="width:100%;font-size:10px;" id="ddSBorough" onkeypress="mysearch.partnershipSearch(event);"><option value="">-Select-</option></select>';
        strHtml[strHtml.length] = '                    </td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>School Level</td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>';
        strHtml[strHtml.length] = '                    <select  onkeypress="mysearch.partnershipSearch(event);" style="width:100%;font-size:10px;" id="ddSSchoolType">';
        strHtml[strHtml.length] = '                    <option value="">-Select-</option>';
        strHtml[strHtml.length] = '                    </select>';
        strHtml[strHtml.length] = '                    </td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>Principal OR School Name OR District No.</td></tr>';
        strHtml[strHtml.length] = '                    <tr><td><input type="text" style="width:100%;font-size:10px;" onkeypress="mysearch.partnershipSearch(event);" id="txtPSDname" /></td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>Business Partner Name OR Company</td></tr>';
        strHtml[strHtml.length] = '                    <tr><td><input type="text" style="width:100%;font-size:10px;" onkeypress="mysearch.partnershipSearch(event);" id="txtBPCName" /></td></tr>';
        strHtml[strHtml.length] = '                    <tr><td>Business Industry OR Profession</td></tr>';

        strHtml[strHtml.length] = '                    <tr><td>';
        strHtml[strHtml.length] = '                    <select onkeypress="mysearch.partnershipSearch(event);" style="width:100%;font-size:10px;" id="ddSIndustry">';
        strHtml[strHtml.length] = '                    <option value="">-Select-</option>'

        if (this.industriesData != null) {
            for (var x = 0, xlen = this.industriesData.rows.length; x < xlen; x++) {
                strHtml[strHtml.length] = '            <option value="' + this.industriesData.rows[x].id + '" >' + this.industriesData.rows[x].industry + '</option>';
            }

        }

        strHtml[strHtml.length] = '                    </select>'
        strHtml[strHtml.length] = '                    </td></tr>';

        strHtml[strHtml.length] = '                </table>';
        strHtml[strHtml.length] = '            </div>';
        strHtml[strHtml.length] = '        </td>';
        strHtml[strHtml.length] = '    </tr>';
        
        

        strHtml[strHtml.length] = '</table>';

        divSearch.innerHTML = strHtml.join('');



        if (this.PartnershipFocus) {
            var objPFocus = this.PartnershipFocus.rows;
            var replaceText = "";
            var compareText = ["Building School Capacity - ", "Developing Strong Leaders - ", "Enhancing the Learning Experience - ", "Other - "];
            var collaborationName = "";
            var objSelect = document.getElementById("ddPFocus");
            var objOption = null;
            for (var intCount = 0; intCount < objPFocus.length; intCount++) {
                var index = -1;
                index = this.arrayIndex(compareText, objPFocus[intCount].collaboration);
                if (index != -1) {
                    collaborationName = objPFocus[intCount].collaboration.replace(compareText[index], '>>');
                }
                else {
                    collaborationName = objPFocus[intCount].collaboration
                }
                if (navigator.appName === "Microsoft Internet Explorer") {
                    var objOption = document.createElement("OPTION");
                    objOption.value = objPFocus[intCount].id;
                    objOption.innerText = collaborationName;
                    objSelect.appendChild(objOption);
                }
                else {
                    objSelect.options[objSelect.options.length] = new Option(collaborationName, objPFocus[intCount].id);
                }
            }
        }

        if (this.boroughData) {
            var objBorough = this.boroughData.rows;
            var objBSelect = document.getElementById("ddSBorough");
            var objOption = null;
            for (var intCount = 0; intCount < objBorough.length; intCount++) {
                if (navigator.appName === "Microsoft Internet Explorer") {
                    var objOption = document.createElement("OPTION");
                    objOption.value = objBorough[intCount].id;
                    objOption.innerText = objBorough[intCount].title;
                    objBSelect.appendChild(objOption);
                }
                else {
                    objBSelect.options[objBSelect.options.length] = new Option(objBorough[intCount].title, objBorough[intCount].id);
                }

            }
        }

        if (this.schoolData) {
            var objStype = this.schoolData.rows;
            var objSSelect = document.getElementById("ddSSchoolType");
            var objOption = null;
            for (var intCount = 0; intCount < objStype.length; intCount++) {
                if (navigator.appName === "Microsoft Internet Explorer") {
                    var objOption = document.createElement("OPTION");
                    objOption.value = objStype[intCount].id;
                    objOption.innerText = objStype[intCount].school_type;
                    objSSelect.appendChild(objOption);
                }
                else {
                    objSSelect.options[objSSelect.options.length] = new Option(objStype[intCount].school_type, objStype[intCount].id);
                }
            }
        }



        this.loadViewState();

        //$("input").keypress(function(e) { mysearch.partnershipSearch(e); });
        $("input[id='txtpartnershipSearch']").keypress(function(e) { mysearch.partnershipSearch(e); });
        $("input[id='txtPSDname']").keypress(function(e) { mysearch.partnershipSearch(e); });
        $("input[id='txtBPCName']").keypress(function(e) { mysearch.partnershipSearch(e); });

    };

    this.arrayIndex = function(/*Array*/arr, /*Array Element*/obj) {


        var index = -1;

        for (var i = 0; i < arr.length; i++) {
            if (obj.indexOf(arr[i]) != -1) {
                index = i;
                break;
            }

        }

        return index;

    };



    this.loadViewState = function() {


        var ddPFocus = document.getElementById('ddPFocus'),
                ddSBorough = document.getElementById('ddSBorough'),
                ddSSchoolType = document.getElementById('ddSSchoolType'),
                txtPSDname = document.getElementById('txtPSDname'),
                txtBPCName = document.getElementById('txtBPCName'),
                ddSIndustry = document.getElementById('ddSIndustry'),
                txtpartnershipSearch = document.getElementById('txtpartnershipSearch'),
                pfocus = $.query.get('pfocus') || 0,
                bo = $.query.get('bo') || 0,
                st = $.query.get('st') || 0,
                pn = $.query.get('pn') || 0,
                bn = $.query.get('bn') || 0,
                ind = $.query.get('ind') || 0,
                type = $.query.get('type') || 0,
                keyword = $.query.get('keyword') || 0;




        if (pfocus != 0)
            ddPFocus.value = pfocus;
        if (bo != 0)
            ddSBorough.value = bo;
        if (st != 0)
            ddSSchoolType.value = st;

        if (pn != 0)
            txtPSDname.value = pn;
        if (bn != 0)
            txtBPCName.value = bn;
        if (ind != 0)
            ddSIndustry.value = ind;
        if (keyword != 0)
            txtpartnershipSearch.value = keyword;

        if (type != 0) {
            mysearch.toggle(document.getElementById('linkadvanced1'));
        }

    };



    this.toggle = function(element) {

        var divAdvanced = document.getElementById("searchadvanced1");
        divAdvanced.style.display = (divAdvanced.style.display == "block") ? "none" : "block";

        if (divAdvanced.style.display == "block") {
            this.isAdvanceSearch = true;
            element.innerText = "Hide Search Options";

        }
        else {
            this.isAdvanceSearch = false;
            element.innerText = "Expand Search Options";

        }


    };





    this.partnershipSearch = function(ev) {
        var keyword = "";
        
        if (ev.which == 13 || ev.type == "click") {
            keyword = document.getElementById("txtpartnershipSearch").value.replace(/^\s|\s$/g, "");

            if (!this.isAdvanceSearch) {
                document.location = "partnershipsearch.htm?keyword=" + document.getElementById("txtpartnershipSearch").value;
            }
            else {
                var params = [],
                        ddPFocus = document.getElementById('ddPFocus'),
                        ddSBorough = document.getElementById('ddSBorough'),
                        ddSSchoolType = document.getElementById('ddSSchoolType'),
                        txtPSDname = document.getElementById('txtPSDname'),
                        txtBPCName = document.getElementById('txtBPCName'),
                        ddSIndustry = document.getElementById('ddSIndustry'),
                        keyword = document.getElementById("txtpartnershipSearch");

                params[params.length] = "type=ad"

                if (ddPFocus.value != "")
                    params[params.length] = "&pfocus=" + ddPFocus.value;
                if (ddSBorough.value != "")
                    params[params.length] = "&bo=" + ddSBorough.value;
                if (ddSSchoolType.value != "")
                    params[params.length] = "&st=" + ddSSchoolType.value;
                if (txtPSDname.value != "")
                    params[params.length] = "&pn=" + txtPSDname.value;
                if (txtBPCName.value != "")
                    params[params.length] = "&bn=" + txtBPCName.value;
                if (ddSIndustry.value != "")
                    params[params.length] = "&ind=" + ddSIndustry.value;
                if (keyword.value != "")
                    params[params.length] = "&keyword=" + keyword.value;




                document.location = "partnershipsearch.htm?" + params.join('');

            }

        }
        
    };

}
var mysearch = new search();
mysearch.fetchIndustries();
