﻿var downarrow = 'notpressed';

jQuery(document).ready(function () {

    setSearchCategoriesSelectedValue();

    jQuery("#btnSearchGSA").click(function () {
        submitSearch();
    });
})

function submitonEnter(evt) {

    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode == "40") {
        //down arrow pressed - takes care of autocomplete
        evt.returnValue = false;
        evt.cancel = true;
        downarrow = 'pressed';
    }

    if (charCode == "13") {
        evt.returnValue = false;
        evt.cancel = true;
        if (downarrow == 'notpressed') {
            submitSearch();
        }
        else {
            downarrow = 'notpressed';
        }
    }
}


function submitSearch() {

    if ($('#linkGSASearch').val() == undefined) {
        return false;
    }

    var search = $("#searchGSA").val();
    var hint = $("#typeTextGSA").val();

    if (!CheckInput(search, hint)) {
        return false;
    }

    var headerId;
    if ($('#headerIdGSA') != undefined && $('#headerIdGSA') != null) {
        headerId = $('#headerIdGSA').attr('value');
    }

    var searchurl;
    if ($('#listCount').val() > 1) {
        searchurl = $('#linkGSASearch').val() + "?searchText=" + jQuery("#searchGSA").val() + "&qs=" + jQuery("#searchcategory").val() + "&catTxt=" + ($('#catTxt').val() != '' ? $('#catTxt').val() : 'All') + "&headerId=" + headerId;
    }
    else {
        searchurl = $('#linkGSASearch').val() + "?searchText=" + jQuery("#searchGSA").val() + "&qs=" + jQuery("#seachCategoryWithoutDropDown").val() + "&catTxt=" + $('#catTxtWithoutDropDown').val() + "&headerId=" + headerId;
    }

    location.href = searchurl;
    return true;
}

function CheckInput(searchText, typeKeyword) {
    if (TrimText(searchText) == TrimText(typeKeyword) || TrimText(searchText) == "") {
        return false;
    }
    return true;
}

function setSearchCategoriesSelectedValue() {
    try {
        var selectedCategory = getParameterByName("qs");
        if (selectedCategory == "" || selectedCategory == undefined)
            jQuery("#searchcategory option:first").attr('selected', 'selected');
        else {
            jQuery("#searchcategory").val(selectedCategory);
            jQuery("#catTxt").val(jQuery("#searchcategory option:selected").text());
            jQuery("#searchcategory").getSetSSValue(selectedCategory);
        }
    }
    catch (err) { }
};

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) {
        return "";
    }
    else {
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
};

function HideHint(input, hint) {
    hint = TrimText(hint);
    var text = TrimText(input.value);

    if (text == hint) {
        input.value = '';
    }
}

function ShowHint(input, hint) {
    hint = TrimText(hint);
    var text = TrimText(input.value);

    if (text == '') {
        input.value = hint;
    }
}

function TrimText(text) {
    text = text.replace(/\r\n/g, '');
    //text = text.replace(/\s+$/, '');
    return text.trim();
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}

function selectGSATab(id) {

    buildUrl(id, window.location.href);

    $('.gsc-tabhActive').addClass('gsc-tabhInactive');
    $('.gsc-tabhActive').removeClass('gsc-tabhActive');

    $('#tabGSA' + id).removeClass('gsc-tabhInactive');
    $('#tabGSA' + id).addClass('gsc-tabhActive');
}

function buildUrl(id, url) {

    var index = url.indexOf('qs=');
    var newUrl = '';

    if (index != -1) {
        newUrl = url.substring(0, index);
    }

    var catText = $('#hdnGSACatText' + id).val();
    var qs = $('#hdnGSAQs' + id).val();

    if (newUrl != null && qs != null && catText != null) {

        var browserName = navigator.appName;
        if (browserName == "Microsoft Internet Explorer") {
            window.setTimeout("window.location.reload()", 500, "javascript");
        }
        else {
            window.location.reload();
        }

        var headerId;
        if ($('#headerIdGSA') != undefined && $('#headerIdGSA') != null) {
            headerId = $('#headerIdGSA').attr('value');
        }

        document.location.href = newUrl + "qs=" + qs + "&catTxt=" + catText + "&headerId=" + headerId;
    }
}


function searchTabGSA() {

    if ($('#txtTabGSA') != null && $('#searchGSA') != null) {
        $('#searchGSA').val($('#txtTabGSA').val());
    }

    var url = window.location.href;
    var indexSearchText = url.indexOf('searchText=');
    var newUr;

    var indexQs = url.indexOf('qs=');

    if (indexSearchText != -1 && indexQs != -1) {

        newUrl = url.substring(0, indexSearchText) + "searchText=" + $('#txtTabGSA').val() + "&" + url.substring(indexQs, url.length);
    }

    //build the URL
    var divId = $('.gsc-tabhActive').attr('id');
    var index;

    if (divId != null) {
        index = divId.substring(6, divId.length);
    }

    buildUrl(index, newUrl);
}


function SearchGSAGo() {

    if (document.getElementById('searchcategory') != undefined && document.getElementById('searchcategory') != null) {

        var index = document.getElementById('searchcategory').selectedIndex;

        if (index != undefined && index >= 0) {

            $('.gsc-tabhActive').addClass('gsc-tabhInactive');
            $('.gsc-tabhActive').removeClass('gsc-tabhActive');

            $('#tabGSA' + index).removeClass('gsc-tabhInactive');
            $('#tabGSA' + index).addClass('gsc-tabhActive');
        }
    }
}
