function parseURL(url) {
 var a = document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':', ''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function () {
                var ret = {},
                seg = a.search.replace(/^\?/, '').split('&'),
                len = seg.length, i = 0, s;
                for (; i < len; i++) {
                    if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
            hash: a.hash.replace('#', ''),
            path: a.pathname.replace(/^([^\/])/, '/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
            segments: a.pathname.replace(/^\//, '').split('/')
        };
}


function BuyProductWithQuantity(idProduct, prefix, URL) {
    BuyProductWithQuantity(idProduct, prefix, URL, null);
}
function BuyProductWithQuantity(idProduct, prefix, URL, countItems) {
    BuyProductWithQuantity(idProduct, prefix, URL, countItems, null);
}
function BuyProductWithQuantity(idProduct, prefix, URL, countItems, additionalUrlParams) {
    //var QuantityObjectId = "ItemQuantity_" + idProduct;
    var QuantityObjectId = prefix + idProduct;

    if (URL.length == 0) {
        URL = "ps_additem.aspx?idProduct=";
    }

    var QuantityObject = document.getElementById(QuantityObjectId);

    if (QuantityObject != null) {
        var QuantityObjectValue = document.getElementById(QuantityObjectId).value;

        QuantityInt = parseInt(QuantityObjectValue);

        if (QuantityInt < 1 || QuantityInt > 100 || isNaN(QuantityInt))
            QuantityInt = 1;
    }
    else {
        QuantityInt = parseInt(countItems);
        if (QuantityInt < 1 || QuantityInt > 100 || isNaN(QuantityInt))
            QuantityInt = 1;
    }

    URL = URL + idProduct + "&quantity=" + QuantityInt;

    var SelectedOptions = document.getElementById("SelectedOptions");
    if (SelectedOptions != null) {
        var SelectedOptionsValue = document.getElementById("SelectedOptions").value;
        URL = URL + "&options=";
        URL = URL + SelectedOptionsValue;
    }
    var query = window.location.search.substring(1);
    if (query.indexOf("idCartRow") != -1) {
        URL = URL + "&modify=1";
    }

    if (additionalUrlParams) {
        URL = URL + "&" + additionalUrlParams;
    }

    //alert(URL);
    window.location = URL


}

function BuyProductQty(idProduct, prefix, URL, evObj) {
    if (evObj.keyCode != 13) {
        return false;
    }
    else {
        BuyProductWithQuantity(idProduct, prefix, URL)
    }
}


function StartSearchMainControl() {
    var SearchTextObject = document.getElementById("SearchTextMainControl");

    //alert (SearchTextObject.value);
    if (SearchTextObject.value.length > 0) {
        URL = "ps_searchItem.aspx?SearchText=" + SearchTextObject.value;
        window.location = URL

    }

}

function OpenWindow(URL, WindowType) {

    var wWidth = 600;
    var wHeight = 480;

    if (WindowType == "TopBanner") {
        wWidth = 600;
        wHeight = 500;
    }

    if (WindowType == "LeftBanner") {
        wWidth = 600;
        wHeight = 450;
    }

    if (WindowType == "EditProduct") {
        wWidth = 480;
        wHeight = 320;
    }


    var poz_left = screen.availWidth / 2 - wWidth / 2;
    var poz_top = screen.availHeight / 2 - wHeight / 2;
    var params = "left=" + poz_left + ",top=" + poz_top + ",width=" + wWidth + ",height=" + wHeight + ",toolbar=0,resizable=1";

    window.open(URL, 'petstore', params);
}

function EnterClickEvent(event, ButtonPrefix, idProduct) {
    event = event || window.event;
    if (event.keyCode == 13) {
        src = event.target;
        if (__nonMSDOMBrowser) {
            src = event.target;
        }
        else {
            src = event.srcElement;
        }
        tagName = src.tagName.toLowerCase();
        if ((tagName != "a") && ((tagName != "input") || ((src.type != "submit") && (src.type != "reset") && (src.type != "image") && (src.type != "file")))) {

            var ButtonTextId = ButtonPrefix + idProduct;
            document.getElementById(ButtonTextId).click();
        }
        return false;
    }

    return true;
}

function goCSRSearch(e, GetValue) {
    e = e || window.event;
    //alert(this.location )
    var URLpath = window.location.href;

    if (e.keyCode == "13") {
        SearchText = GetValue;
        if (SearchText != null && SearchText != '') {
            re = /[^\w '\-`]/g;
            SearchText = SearchText.replace(re, ' ');

        }
        if (SearchText != '') {
            location.href = 'ps_CSRsearchItem.aspx?SearchText=' + SearchText;
        }
        return false;
    }
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

function checkCSRSearch(e, value) {
    e = e || window.event;

    if (e.keyCode == "13") {
        if (value == "") {
            alert("Please input a search string!");
            return false;
        }
    }
    return true;
}

// SearchCtrl.


function CheckSearchString(idControl) {

    var SearchText = "";
    if (idControl) {
        SearchText = document.getElementById(idControl).value;
    }

    SearchText = Trim(SearchText);

    if (SearchText != null && SearchText != '') {
        re = /[^\w '\-`]/g;
        SearchText = SearchText.replace(re, ' ');
    }

    if (SearchText == "") {
        alert("Please input a correct search string or choose a category.\nCharacters %, # , < , > , ', / and - cannot be used in your search string.");
        return false;

    }
    return true;
}

function CheckSearchForm() {
    var SearchText = document.getElementById(get_SearchTextMainControlClientID()).value;

    SearchText = Trim(SearchText);

    if (SearchText != null && SearchText != '') {
        re = /[^\w '\-`]/g;
        SearchText = SearchText.replace(re, ' ');
    }

    var CaterogyParam = document.getElementById("idCategoryParam").value;

    if (SearchText == "" && CaterogyParam == "") {
        alert("Please input a correct search string or choose a category.\nCharacters %, # , < , > , ', / and - cannot be used in your search string.");
        return false;

    }
    return true;
}

function SearchBtn_onclick() {
    if (!CheckSearchForm()) {
        return false;
    }

    var searchurl = "http://" + window.location.host + "/";
    searchurl = searchurl + "ps_searchItem.aspx?";
    searchurl = searchurl + "SearchText=";
    SearchText = document.getElementById(get_SearchTextMainControlClientID()).value;
    if (SearchText != null && SearchText != '') {
        re = /[^\w '\-`]/g;
        SearchText = SearchText.replace(re, ' ');
    }
    searchurl = searchurl + SearchText;

    var categParam = document.getElementById("idCategoryParam").value;
    if (categParam) {
        var categName = "";

        switch (categParam) {
            case "DG": categName = "Dog_Suppies";
                break;
            case "CT": categName = "Cat_Supplies";
                break;
            case "AQ": categName = "Aquarium_Supplies";
                break;
            case "RP": categName = "Reptile_Supplies";
                break;
            case "BD": categName = "Bird_Supplies";
                break;
            case "SP": categName = "Small_Animal_Pet_Supplies";
                break;
            case "PD": categName = "Pond_Supplies";
                break;
            default: ;
        }
        searchurl = searchurl + "&Category=";
        searchurl = searchurl + categName;
        searchurl = searchurl + "&IdCategory=";
        searchurl = searchurl + categParam;
    }

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0) {
        window.location.href = searchurl;
    }
    else {
        window.open(searchurl, target = "_self");
    }
    return false;
}


function RemoveCount() {
    var SearchText = document.getElementById(get_SearchTextMainControlClientID()).value;
    var CorrectText = SearchText;

    if (SearchText != null && SearchText != '' && SearchText.length > 0) {
        var start = SearchText.indexOf('(') - 1;
        if (start > 0) {
            CorrectText = SearchText.substring(0, start);
            document.getElementById(get_SearchTextMainControlClientID()).value = CorrectText;
        }
    }
}

var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
function KeyDownOnSearch(event) {
    if (event.keyCode == 13) {

        src = event.target;
        if (__nonMSDOMBrowser) {
            src = event.target;
        }
        else {
            src = event.srcElement;
        }
        tagName = src.tagName.toLowerCase();

        if ((tagName != "a") && ((tagName != "input") || ((src.type != "submit") && (src.type != "reset") && (src.type != "image") && (src.type != "file")))) {

            SearchBtn_onclick();
        }
        return false;
    }
    return true;
}

//////////////////////////////////////////////////////
/////////////////// Trim functions ///////////////////
//////////////////////////////////////////////////////
function Trim(TRIM_VALUE) {
    if (TRIM_VALUE == null || TRIM_VALUE.length < 1) {
        return "";
    }

    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if (TRIM_VALUE == "") {
        return "";
    }
    else {
        return TRIM_VALUE;
    }
}

function RTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if (v_length < 0) {
        return "";
    }
    var iTemp = v_length - 1;

    while (iTemp > -1) {
        if (VALUE.charAt(iTemp) == w_space)
        { }
        else {
            strTemp = VALUE.substring(0, iTemp + 1);
            break;
        }

        iTemp = iTemp - 1;

    } //End While
    return strTemp;

}

function LTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    if (v_length < 1) {
        return "";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while (iTemp < v_length) {
        if (VALUE.charAt(iTemp) == w_space)
        { }
        else {
            strTemp = VALUE.substring(iTemp, v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
}
//.

function KeyDownOnFreeNewsletter(event, element) {
    if (event.keyCode == 13) {
        if (element.value == '' || element.value == 'Enter Email Here') {
            alert("Please enter email address!");
            return false;
        }
        window.location = 'ps_optNewsletterAddEmail.aspx?smd=1&email=' + escape(element.value);
        return false;
    }
    return true;
}

function SubscribeClick(elemId) {
    var email = document.getElementById(elemId).value;
    if (email == '' || email == 'Enter Email Here') {
        alert("Please enter email address!");
        return false;
    }
    window.location = 'ps_optNewsletterAddEmail.aspx?smd=1&email=' + escape(email);
    return false;    
}
function IsValidEmail(sText) {
    var objRegExp =
     /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

    return objRegExp.test(sText);
}

function IsValidNumeric(strValue) {
    var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
    return objRegExp.test(strValue);
}



function GetElement(hiddenFieldValue) {
    var hiddenElem = document.getElementById(hiddenFieldValue);
    if (hiddenElem == null) {
        return null;
    }
    return document.getElementById(hiddenElem.value);
}

function EnterKeyDownEventHandler(e, buttonId) {
    var charCode = (e.which) ? e.which : event.keyCode;
    if (charCode == 13) {
        var btn = document.getElementById(buttonId);
        if (btn) {
            btn.click();
            return false;
        }
    }
}

function DisableEnterKey() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("firefox") != -1) {
        return;
    }

    if (event && event.srcElement && event.srcElement.type == "textarea") {
        return true;
    }

    if (event.keyCode == 13) {
        return false;
    }
    else {
        return true;
    }
}


