﻿var clientid;
var CountryRegion;
var BookstoreUpdatePanel;
var LibraryUpdatePanel;
var CountryListId;
var RegionListId;
var validFormats = [1, 25, 30, 35, 50, 302, 410, 425, 450, 810, 900];

function fnSetFocus(txtClientId) {
    clientid = txtClientId;
    setTimeout("fnFocus()", 300);
}

function fnFocus() {
    eval("document.getElementById('" + clientid + "').focus()");
}

function fnResetAdvancedSearch() {
    $('#modalPopupAdvSearch input:text').attr('value', '');
    $('#modalPopupAdvSearch select').attr('selectedIndex', 0);
}

function fnPopulateCountriesByTitle(ddlCountryId, ReserveId, FormatId) {
    clientid = ddlCountryId;
    PageMethods.GetCountryRegionListByTitle(ReserveId, FormatId, PopulateCountries, ReportError, 'countries');
    return false;
}

function fnPopulateRegionsByTitle(ReserveId, FormatId) {
    PageMethods.GetCountryRegionListByTitle(ReserveId, FormatId, storeCountryRegion, ReportError, 'countries');
    return false;
}


function fnPopulateRegions(ddlCountryId, ddlRegionId) {
    var ddlCountry = $('#' + ddlCountryId);
    var ddlRegion = $('#' + ddlRegionId);
    clientid = ddlRegionId;
    if (ddlCountry != null) {
        if (ddlCountry[0].selectedIndex > 0) {
            //PageMethods.GetRegionList(ddlCountry[0].options[ddlCountry[0].selectedIndex].text, PopulateRegions, ReportError, 'regions');
            PageMethods.GetRegionList(ddlCountry[0].options[ddlCountry[0].selectedIndex].value, PopulateRegions, ReportError, 'regions');
            ddlRegion.attr('style', 'width:150px');
        } else {
            ddlRegion.attr('style', 'display:none;');
        }
    }
    return false;
}

function fnValidateKeywordSearch(txtClientId) {       
    clientid = txtClientId;
    var txtKeyword = $('#' + txtClientId);
   
    if (txtKeyword != null) {
        
        var KeywordValue = txtKeyword[0].value.trim();
        var KeywordTest = true;
        var AllWhitespace = /^(\s+)$/;
        if (KeywordValue == "Enter keyword, title, author, or ISBN")
            KeywordTest = false;
        else if (KeywordValue.match(AllWhitespace))
            KeywordTest = false;
        else if (KeywordValue.length <= 1)
            KeywordTest = false;
        if (!KeywordTest) {
            alert("Please enter a keyword, title, author, or ISBN with at least 2 characters.");
            txtKeyword.focus();
        }
        
        return KeywordTest;
    }

}

function fnValidateAdvancedSearch() {
   // var txt = "";
    var hasVal = false;
    var hasValidVal = false;

    if (!isNullOrWhiteSpace($("#txtTitle").val())) {
        hasVal = true;
    }
    else if (!isNullOrWhiteSpace($("#txtCreator").val())) {
        hasVal = true;
    }
    else if (!isNullOrWhiteSpace($("#txtKeywords").val())) {
        hasVal = true;
    }
    else if (!isNullOrWhiteSpace($("#txtISBN").val())) {
        hasVal = true;
    }
    // Check dropdowns
    txt = $("#ddlMediaFormat option:selected").text();
    if (txt.toLowerCase() != "view all")
        hasVal = true;
    txt = $("#ddlSubject option:selected").text();
    if (txt.toLowerCase() != "view all")
        hasVal = true;
    txt = $("#ddlPublisher option:selected").text();
    if (txt.toLowerCase() != "view all")
        hasVal = true;
    txt = $("#ddlImprint option:selected").text();
    if (txt.toLowerCase() != "view all")
        hasVal = true;
    txt = $("#ddlLanguage option:selected").text();
    if (txt.toLowerCase() != "view all")
        hasVal = true;

    if (hasVal == false) {
        alert('We’re sorry, but no search criteria was specified.');
        $("#txtTitle").focus();
        return false;
    }
    else {
        // Check that title value is a valid length
        if ((!isNullOrWhiteSpace($("#txtTitle").val())) && (trim($("#txtTitle").val()).length < 2)) {
            alert('Make sure that your Title search criteria is at least 2 characters long.');
            $("#txtTitle").focus();
            return false;
        }
        // Check that creator value is a valid length
        else if ((!isNullOrWhiteSpace($("#txtCreator").val())) && (trim($("#txtCreator").val()).length < 2)) {
            alert('Make sure that your Creator search criteria is at least 2 characters long.');
            $("#txtCreator").focus();
            return false;
        }
        // Check that keyword value is a valid length
        else if ((!isNullOrWhiteSpace($("#txtKeywords").val())) && (trim($("#txtKeywords").val()).length < 2)) {
            alert('Make sure that your Keyword search criteria is at least 2 characters long.');
            $("#txtKeywords").focus();
            return false;
        }
        // Check that isbn/doi value is a valid length
        else if ((!isNullOrWhiteSpace($("#txtISBN").val())) && (trim($("#txtISBN").val()).length < 2)) {
            alert('Make sure that your ISBN/DOI search criteria is at least 2 characters long.');
            $("#txtISBN").focus();
            return false;
        }

    }
    
    return hasVal;
}


function fnValidateLibrarySearch(txtClientId) {
    clientid = txtClientId;
    var txtKeyword = $('#' + txtClientId);
    if (txtKeyword != null) {

        var KeywordValue = txtKeyword[0].value;
        var KeywordTest = true;
        var AllWhitespace = /^(\s+)$/;
        if ((KeywordValue == "Search for a library") || (KeywordValue == "Enter a 5-digit postal code"))
            KeywordTest = false;
        else if (KeywordValue.match(AllWhitespace))
            KeywordTest = false;
        else if (KeywordValue.length <= 1)
            KeywordTest = false;
        if (!KeywordTest) {
            txtKeyword.focus();
            alert("We’re sorry, but no search criteria was specified.");
           
           
        }

        return KeywordTest;
    }

}

function fnValidateBookstoreSearch(txtClientId) {
    clientid = txtClientId;
    var txtKeyword = $('#' + txtClientId);
    
    if (txtKeyword != null) {

        var KeywordValue = txtKeyword[0].value;
        var KeywordTest = true;
        var AllWhitespace = /^(\s+)$/;
        if (KeywordValue == "Search for a bookstore")
            KeywordTest = false;
        else if (KeywordValue.match(AllWhitespace))
            KeywordTest = false;
        else if (KeywordValue.length <= 1)
            KeywordTest = false;
        if (!KeywordTest) {
            txtKeyword.focus();
            alert("We’re sorry, but no search criteria was specified.");


        }

        return KeywordTest;
    }

}

function fnValidateSearchBox(txtClientId, txtToCheck) {
    clientid = txtClientId;
    var txtKeyword = $('#' + txtClientId);

    if (txtKeyword != null) {

        var KeywordValue = txtKeyword[0].value;
        var KeywordTest = true;
        var AllWhitespace = /^(\s+)$/;
        if (KeywordValue == txtToCheck)
            KeywordTest = false;
        else if (KeywordValue.match(AllWhitespace))
            KeywordTest = false;
        else if (KeywordValue.length <= 1)
            KeywordTest = false;
        if (!KeywordTest) {
            txtKeyword.focus();
            alert("We’re sorry, but no search criteria was specified.");


        }
      
        return KeywordTest;
    }
    else {
        return false;
    }

}

function trim(valueToTrim) {
    return $.trim(valueToTrim);
}

function isNullOrWhiteSpace(valueToCheck) {
    var bIsNullOrWhiteSpace = false;
    if (valueToCheck == null)
        bIsNullOrWhiteSpace = true;
    else {
        if ($.trim(valueToCheck) == "") {
            bIsNullOrWhiteSpace = true;
        }
    }
    return bIsNullOrWhiteSpace;
}



