﻿
//RWA NOV 19
function StoreNav_OnLoad(sender, eventArgs) {
    var panelBar = sender;
    var allItems = panelBar.get_allItems();

    for (var i = 0; i < allItems.length; i++) {
        var panelItem = allItems[i];
        var imageElement = panelItem.get_imageElement();
        if (imageElement) {
            imageElement.AssociatedItem = panelItem;
            imageElement.onclick =
                    function(e) {
                        if (!e) e = window.event;
                        if (this.AssociatedItem.get_expanded()) {
                            this.AssociatedItem.collapse();
                        } else {
                            this.AssociatedItem.expand();
                        }
                        e.cancelBubble = true;
                        if (e.stopPropagation) { e.stopPropagation(); }
                        return false;
                    }
        }
    }
}

//######################## MAY 16 #######################

function CloseTooltip() {
    var controller = Telerik.Web.UI.RadToolTipController.getInstance();
    var tooltip = controller.get_activeToolTip();
    if (tooltip) {
        tooltip.hide();
    }
}


//#################################################
function URLencode(str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'

    var hash_map = {}, unicodeStr = '', hexEscStr = '';
    var ret = (str + '').toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urldecode.
    hash_map["'"] = '%27';
    hash_map['('] = '%28';
    hash_map[')'] = '%29';
    hash_map['*'] = '%2A';
    hash_map['~'] = '%7E';
    hash_map['!'] = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%" + m2.toUpperCase();
    });
}

function URLdecode(str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'

    var hash_map = {}, ret = str.toString(), unicodeStr = '', hexEscStr = '';

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urlencode.
    hash_map["'"] = '%27';
    hash_map['('] = '%28';
    hash_map[')'] = '%29';
    hash_map['*'] = '%2A';
    hash_map['~'] = '%7E';
    hash_map['!'] = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}



var curDiv;

function changeCatStatus() {
    document.getElementById('s_catStatus').value = 'Search Selected Only';
}
function changeBrandStatus() {
    document.getElementById('s_brandStatus').value = 'Search Selected Only';
}
function quickChecks(ctl) {
    var elements = document.getElementsByTagName("input");

    for (i = 0; i < elements.length; i++) {
        if (elements[i].type == "checkbox" && elements[i].parentNode.getAttribute("group") == ctl.getAttribute("group")) {
            elements[i].checked = ctl.checked;
        }
    }
}

function hideSearchDivs() {
    if (curDiv != 's_catlist' && curDiv != 's_brandlist') {
        panelHidden('s_catlist');
        panelHidden('s_brandlist');
        panelVisible('cartInfo');
    }
    else if (curDiv == 's_catlist') {
        panelHidden('s_brandlist');
        panelHidden('cartInfo');
    }
    else if (curDiv == 's_brandlist') {
        panelHidden('s_catlist');
        panelHidden('cartInfo');
    }
}

function GetCatArticles(catID, resultDiv) {
    panelVisible('articles_loading');

    params = 'GetCatArticles|' + catID + '|' + resultDiv;
    WebForm_DoCallback("__Page", params, RebuildArticles, before, null, true);
}
function RebuildArticles(result) {
    var obj = eval("(" + result + ")");
    document.getElementById(obj.resultDiv).innerHTML = obj.articles;
    panelHidden('articles_loading');
}

function switchSkuImg(skuID, ddl, imgID, url, hlID, decUrl) {

    //document.getElementById(ddl).value = skuID;
    var combo = $find(ddl);
    var item = combo.findItemByValue(skuID);
    if (item) {
        item.select();
    }
    document.getElementById(imgID).src = url;
    document.getElementById(hlID).href = decUrl;
}

function lstSku_Changed(skuID, resultDiv) {
    params = 'lstSku_Changed|' + skuID + '|' + resultDiv;
    WebForm_DoCallback('__Page', params, lstSkuChangedResut, before, null, true);
}

function lstSkuChangedResut(result) {
    var obj = eval("(" + result + ")");

    document.getElementById(obj.imgID).src = obj.imgUrl;
    document.getElementById(obj.hlID).href = obj.decodedUrl;


    if (obj.noStock) {
        document.getElementById(obj.addCartButton).style.display = 'none';
        panelVisible('noStock');
        panelHidden('volDisc');
    }
    else {
        resultPnl = document.getElementById(obj.resultDiv);
        //debugger;
        if (resultPnl) {
            resultPnl.innerHTML = obj.discRows;
            panelVisible('volDisc');
        }

        panelVisible(obj.addCartButton);
        panelHidden('noStock');
    }
}



function RemoveImg(path, imgContainer, thisID) {
    params = 'RemoveImg|' + path + '|' + document.getElementById(imgContainer).value;
    WebForm_DoCallback(thisID, params, RebuildSelectedImages, before, null, true);
}

function RebuildSelectedImages(result) {
    var obj = eval("(" + result + ")");
    document.getElementById(obj.resultDiv).innerHTML = obj.images;
    document.getElementById(obj.hImages).value = obj.hImagesContent;
    //alert(obj.images);
}




function doneTyping(ctl, scr) {
    var oldVal = ctl.value;
    setTimeout(checkIfDone, 2000);

    function checkIfDone() {
        var newVal = ctl.value;
        if (newVal == oldVal) {
            //start XMLHttpRequest
            setTimeout(scr, 1);
        }
    }
}

function decreaseQty_Click(ctl, scr) {
    if (ctl.value > 1) {
        ctl.value--;
    }
    else {
        ctl.value = 1;
    }
    var oldVal = ctl.value;

    setTimeout(checkIfDone, 1000);

    function checkIfDone() {
        var newVal = ctl.value;
        newQty = newVal;
        if (newVal == oldVal) {
            //start XMLHttpRequest
            setTimeout(scr, 1);
        }
    }
}
function increaseQty_Click(ctl, scr) {
    ctl.value++;
    var oldVal = ctl.value;

    setTimeout(checkIfDone, 1000);

    function checkIfDone() {
        var newVal = ctl.value;
        newQty = newVal;
        if (newVal == oldVal) {
            //start XMLHttpRequest
            setTimeout(scr, 1);
        }
    }
}

function updateQty(qty, orig, id, resultDiv) {
    if (qty != orig) {
        qty = parseInt(qty);
        panelVisible('progressBar');
        params = 'UpdateQty|' + id + '|' + qty + '|' + resultDiv;
        WebForm_DoCallback("__Page", params, updateQtyResult, before, null, true);
    }
}


function updateQtyResult(result) {

    var obj = eval("(" + result + ")");
    document.getElementById(obj.totalDiv).innerHTML = obj.cartTotal;
    document.getElementById(obj.resultDiv).innerHTML = obj.Rows;
    //    document.getElementById('volPrice'+obj.itemID).innerHTML = obj.price;
    //    document.getElementById('totalPrice'+obj.itemID).innerHTML = obj.itemTotal;

    panelHidden('progressBar');
    if (obj.isRemoved == true) {
        //        getCartRelated();
        rebuildCartRelated(obj.relatedItems)
    }
}

function getCartRelated() {
    params = 'GetRelated';
    WebForm_DoCallback("__Page", params, rebuildCartRelated, before, null, true);
}

function rebuildCartRelated(result) {
    if (result.length > 0) {
        panelVisible('cartRelated');
        document.getElementById('divCartRelated').innerHTML = result;
    }
    else {
        panelHidden('cartRelated');
    }
}

function joinList(email, resultDiv) {
    if (email.length > 0) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(email)) {
            params = 'JoinList|' + email + '|' + resultDiv;
            WebForm_DoCallback(masterID, params, joinResult, before, null, true);
        }
        else {
            document.getElementById(resultDiv).className = 'statusLabel';
            document.getElementById(resultDiv).innerHTML = 'Invalid Address<br />';
        }
    }
}
function joinResult(result) {
    var obj = eval("(" + result + ")");
    var div = document.getElementById(obj.resultDiv);
    if (obj.isInserted == 'True') {
        div.className = 'greenLabel';
    }
    else {
        div.className = 'statusLabel';
    }
    div.innerHTML = obj.resultMsg;

}



function PreviewCoupon(couponCode, lbl) {
    if (couponCode != '') {
        panelVisible('coupon_progess');
        params = 'ValidateCoupon|' + couponCode + '|' + lbl;
        WebForm_DoCallback('__Page', params, ShowCoupon, before, null, true);
    }
}
function ShowCoupon(result) {
    var obj = eval("(" + result + ")");
    document.getElementById(obj.resultLbl).innerHTML = obj.msg;

    if (obj.cartDiscount != null) {
        document.getElementById('couponTotalAdjust').innerHTML = obj.cartDiscount;
    }
    else {
        document.getElementById('couponTotalAdjust').innerHTML = '';
    }

    if (obj.cartAddAmount != null) {
        document.getElementById('couponAddToTotal').innerHTML = obj.cartAddAmount;
    }
    else {
        document.getElementById('couponAddToTotal').innerHTML = '';
    }

    if (obj.isFreeShip == 'true') {
        document.getElementById('shippingCost').style.textDecoration = 'line-through';
        document.getElementById('shippingCost').className = 'statusLabel';
    }
    else {
        document.getElementById('shippingCost').style.textDecoration = 'none';
        document.getElementById('shippingCost').className = '';
    }

    if (obj.newCartTotal != null) {
        document.getElementById('orderTotalSpan').innerHTML = obj.newCartTotal;
        document.getElementById('orderTotalSpan').className = 'statusLabel';
    }
    else {
        document.getElementById('orderTotalSpan').className = '';
    }
    if (obj.totalTax != null) {
        document.getElementById('orderTaxSpan').innerHTML = obj.totalTax;
        document.getElementById('orderTaxSpan').className = 'statusLabel';
    }
    else {
        document.getElementById('orderTaxSpan').className = '';
    }

    panelHidden('coupon_progess');
}

/*
Inputted by RWA
*/

var ValidUserNameObj;
var ValidEmailObj;
var Label;

function CheckUsername(name, lbl) {
    Label = lbl
    SongWriters.Services.WebCall.CheckUserName(name, OnUsernameChecked);
}

function OnUsernameChecked(result) {

    ValidUserNameObj = result;
    
    if (result.IsValid == false)
        $get(Label).innerHTML = "<br />" + result.Message;
    else
        $get(Label).innerHTML = '';

    return result.IsValid;
}

function CheckEmail(email, lbl) {
    Label = lbl;
    SongWriters.Services.WebCall.EmailExists(email, OnEmailChecked);
}

function OnEmailChecked(result) {
    ValidEmailObj = result;
    if (result.IsValid) {
        $get(Label).innerHTML = '';
    } else {
        $get(Label).innerHTML = "<br />" + result.Message;
    }
}

function UniqueUsernameValidation(oSrouce, args) {
    while(SongWriters.Services.WebCall.CheckUserName(args.Value, OnUsernameChecked))
    {
    }
    
    if (ValidUserNameObj.IsValid) {
        args.IsValid = true;
    } else {
        args.IsValid = false;
    }
}

function UniqueEmailValidation(oSrouce, args) {
    if (ValidEmailObj.IsValid) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}
/*
END
*/

function checkUsername(username, lbl) {
    if (username.length >= 5) {
        params = 'CheckUsername|' + username + '|' + lbl;
        WebForm_DoCallback('__Page', params, ShowAvail, before, null, true);
    }
    else {
        document.getElementById(lbl).innerHTML = '';
    }
}
function ShowAvail(result) {
    var obj = eval("(" + result + ")");
    document.getElementById(obj.resultLbl).innerHTML = obj.msg;
}

function SelectState(stateCode, hID) {
    document.getElementById(hID).value = stateCode;
}

function GetStates(countryID, hID) {
    params = 'GetStates|' + countryID + '|' + hID;
    WebForm_DoCallback('__Page', params, BuildStateList, before, null, true);
}
function BuildStateList(result) {
    var obj = eval("(" + result + ")");
    if (obj.showText) {
        panelVisible(obj.txtOther);
        panelHidden(obj.resultDiv);
    }
    else {
        panelVisible(obj.resultDiv);
        panelHidden(obj.txtOther);
        document.getElementById(obj.resultDiv).innerHTML = obj.ddl;
    }
}



function getVolDiscs(skuID, resultDiv) {
    params = 'volDiscs|' + skuID + '|' + resultDiv;
    WebForm_DoCallback('__Page', params, BuildDiscList, before, null, true);
}

function BuildDiscList(result) {
    var obj = eval("(" + result + ")");
    if (obj.noStock) {
        cartBtn = document.getElementById(obj.addCartButton);
        if (cartBtn) { cartBtn.style.display = 'none'; }
        panelVisible('noStock');
        panelHidden('volDisc');
    }
    else {
        resultPnl = document.getElementById(obj.resultDiv);
        //debugger;
        if (resultPnl) {
            resultPnl.innerHTML = obj.discRows;
            panelVisible('volDisc');
        }

        panelVisible(obj.addCartButton);
        panelHidden('noStock');
    }
}

function enablePriceOverride(ctl, txtID) {
    if (ctl.checked) {
        panelVisible(txtID);
    }
    else {
        panelHidden(txtID);
    }
}





function changeImg(imgID, url, hID) {
    var esUrl = URLencode(url);
    document.getElementById(imgID).src = '../thumbGen.ashx?' + esUrl + '/100/100/w';
    document.getElementById(hID).value = url;
}

function switchImg(myID, imgID) {
    //    var esUrl = URLencode(url);
    //    img = new Image();
    //    img.src = 'thumbGen.ashx?'+esUrl+'/250/250/w';
    document.getElementById(imgID).src = img[myID].src; //'thumbGen.ashx?'+url+'/250/250/w';  
}

function before() {
}

function showDialog(pnl) {
    panel = document.getElementById(pnl);

    if (panel) {
        panel.style.display = '';
    }
    var sels = document.getElementsByTagName("select");
    for (i = 0; i < sels.length; i++) {
        sels[i].style.width = '0px';
    }

}

function hideDialog(pnl) {
    panel = document.getElementById(pnl);
    if (panel) {
        panel.style.display = 'none';
    }
    var sels = document.getElementsByTagName("select");
    for (i = 0; i < sels.length; i++) {
        sels[i].style.width = '';
    }
}


function showPanel(pnl) {
    ctl = document.getElementById(pnl);
    if (ctl.style.display == '') {
        document.getElementById(pnl).style.display = 'none';
    }
    else {
        document.getElementById(pnl).style.display = '';
    }
}

function panelVisible(pnl) {
    panel = document.getElementById(pnl);
    if (panel) {
        panel.style.display = '';
    }
}
function panelHidden(pnl) {
    panel = document.getElementById(pnl);
    if (panel) {
        panel.style.display = 'none';
    }
}


// called by RadTabStrip OnClientTabSelected will store a cookie 
// to maintain the selected tab
function onSearchTabSelected(sender, args) {
    document.cookie = "searchTab" + "=" + escape(args.get_tab().get_index()) + "; expires=; path=/;";
}

function brandsCombo_Focused(sender, args) {
    sender.set_text('');
    sender.showDropDown();
}

function closeEditToolTop(sender, args) {
    location.reload(true);
}

function hideFlash() {
    var ems = document.getElementsByTagName("embed");
    var objs = document.getElementsByTagName("object");
    for (var i = 0; i < ems.length; i++) {
        ems[i].style.display = 'none';
    }
    for (var i = 0; i < objs.length; i++) {
        objs[i].style.display = 'none';
    }

}
function StartRotator(rotator, direction) {
    if (rotator == null) return; 

    if (!rotator.autoIntervalID) {
        rotator.autoIntervalID = window.setInterval(function() { rotator.showNext(direction); }, rotator.get_frameDuration());
    }
}

function StopRotator(rotator) {
    if (rotator.autoIntervalID) {
        window.clearInterval(rotator.autoIntervalID);
        rotator.autoIntervalID = null;
    }
}

 
