window.onerror = errorHandler;

function errorHandler(message, url, line) 
{
    var xhr;
    if (window.ActiveXObject)
    { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
    else if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); }
    var regExRoot = /(http\:\/\/[^\/]+\/[^\/]+)/gi;
    var ar = regExRoot.exec(window.location);
    xhr.open("get", ar[1] + "/LogClientError.aspx?error=" + encodeURIComponent(message) + "&url=" + encodeURIComponent(url) + "&line=" + encodeURIComponent(line), true);
    xhr.send();
}

var currentPage = 1;
var numOfPages = 1;
var genreId = -1;
var addressStem = '';
var ticks = -1;
var contextId = -1;
var newwindow = '';


function toggleLoader(on) {
    if (on) {document.getElementById('loader').className = 'viewLoader';}
    else {document.getElementById('loader').className = 'hideLoader';}
}

function popitup(url) {
    if (!newwindow.closed && newwindow.location) { newwindow.location.href = url; }
    else { newwindow = window.open(url, 'name', 'height=400,width=400'); if (!newwindow.opener) newwindow.opener = self; }
    if (window.focus) { newwindow.focus() }
    return false;
}

function d(clientId, id){$get(clientId).value = id;__doPostBack(clientId,'');$get(clientId).value = '';}

function openTarget(url)
{
    openTarget(url, 'targetWindow');
}

function openTarget(url, windowName)
{
    var targetWindow;
    targetWindow = window.open(url, windowName,'location=no,status=no,resizable=no,scrollbars=0,width=270,height=275,toolbar=0');
    if (targetWindow){targetWindow.moveTo(0,0);}else{alert ('Please enable popups');}
}

// Purchase code
function MakePurchase(context) {
    var pp = '_pm.aspx/AddToCart';
    toggleLoader(true);
    DoAjax(pp, ["context", context], _pcd, _pee);
}

function _pcd(result) {

    if (result.d) {
        var _cc = result.d.CartCost;
        var cc = document.getElementById('CartCost');
        if (cc && _cc) {
            cc.innerText = _cc;
            cc.innerHTML = _cc;
        }
        var _cic = result.d.CartItemCount;
        var cic = document.getElementById('CartItemCount');
        if (cic && _cic) {
            cic.innerText = _cic;
            cic.innerHTML = _cic;
        }

        if (result.d.HasError) {
            alert(result.d.Message);
        }
        toggleLoader(false);
    }
}

function _pee(result) {
    if (result) {
        alert('unable to add item at this time');
    }
    toggleLoader(false);
}

// Label List code
function GetLabelList(data, as, af) {
    var pagePath = '_pm.aspx/GetLabels';
    toggleLoader(true);
    DoAjax(pagePath, data, as, af);
}

var currentLink;
var newLink;
var currentLabel;
var newLabel;

function gl(link, key) {
    newLink = link;
    GetLabelList(["key", key], _lsh, _leh);
    if (currentLink) {currentLink.className = '';}
    if (newLink) {newLink.className = 'selectedLink';}
    currentLink = newLink;
}

function createLabels(myArray) {
    var arraySize = myArray.length;
    var _he = document.getElementById('labelList');
    _he.innerHTML = '';

    for (i = 0; i < arraySize; i++) {
        var newLi = document.createElement('li');

        newLi.setAttribute('id', myArray[i].Id);

        if (i % 2 == 0) {
            newLi.setAttribute('className', 'oddBox');
        }
        var _lnf = myArray[i].Name;
        var labelName = _lnf.length > 27 ? _lnf.substring(0, 25) + '..' : _lnf;
        newLi.innerHTML = '<a title=\'' + _lnf + '\' href=\'Label.aspx?lid=' + myArray[i].Id + '\'>' + labelName + '</a>';
        _he.appendChild(newLi);
    }
}

function _lsh(result) {
    toggleLoader(false);
    if (result) {createLabels(result.d);}
}

function _leh(result) {
    toggleLoader(false);
    if (result) {alert('unable to load list at this time');}
}

function selectLabelLink(name) {
    var l = document.getElementById(name);
    if (l) {
        l.className = 'selectedLink';
        currentLink = l;
    }
}

function DoAjax(t, pa, as, af) {
    var pl = '';
    if (pa && pa.length > 0) {
        for (var i = 0; i < pa.length; i += 2) {
            if (pl.length > 0) { pl += ','; }
            pl += '"' + pa[i] + '":"' + pa[i + 1] + '"';
        }
    }
    pl = '{' + pl + '}';
    $.ajax({type: "POST", url: t, contentType: "application/json; charset=utf-8", data: pl, dataType: "json", success: as, error: af});
}

function DoAjaxWCF(t, pa, as, af) {
    var pl = "p=" + json2string(pa);
    $.ajax({ type: "POST", url: t, contentType: "application/json; charset=utf-8", data: pl, dataType: "json", success: as, error: af });
}

function json2string(strObject) {
    var c, i, l, s = '', v, p;

    switch (typeof strObject) {
        case 'object':
            if (strObject) {
                if (strObject.length && typeof strObject.length == 'number') {
                    for (i = 0; i < strObject.length; ++i) {
                        v = json2string(strObject[i]);
                        if (s) {
                            s += ',';
                        }
                        s += v;
                    }
                    return '[' + s + ']';
                } else if (typeof strObject.toString != 'undefined') {
                    for (i in strObject) {
                        v = strObject[i];
                        if (typeof v != 'undefined' && typeof v != 'function') {
                            v = json2string(v);
                            if (s) {
                                s += ',';
                            }
                            s += json2string(i) + ':' + v;
                        }
                    }
                    return '{' + s + '}';
                }
            }
            return 'null';
        case 'number':
            return isFinite(strObject) ? String(strObject) : 'null';
        case 'string':
            l = strObject.length;
            s = '"';
            for (i = 0; i < l; i += 1) {
                c = strObject.charAt(i);
                if (c >= ' ') {
                    if (c == '\\' || c == '"') {
                        s += '\\';
                    }
                    s += c;
                } else {
                    switch (c) {
                        case '\b':
                            s += '\\b';
                            break;
                        case '\f':
                            s += '\\f';
                            break;
                        case '\n':
                            s += '\\n';
                            break;
                        case '\r':
                            s += '\\r';
                            break;
                        case '\t':
                            s += '\\t';
                            break;
                        default:
                            c = c.charCodeAt();
                            s += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                    }
                }
            }
            return s + '"';
        case 'boolean':
            return String(strObject);
        default:
            return 'null';
    }
}

function validateLogin() {
    var success = true;
    var emailAddress = document.getElementById('ctl00_pc_lc_e3_txtEA');
    var password = document.getElementById('ctl00_pc_lc_e3_txtPassword');

    document.getElementById('paAst').className = 'hideError';
    document.getElementById('paMessage').className = 'hideError';
    document.getElementById('eaAst').className = 'hideError';
    document.getElementById('eaMessage').className = 'hideError';

    if (emailAddress) {
        if (emailAddress.value.length == 0) {
            success = false;
            document.getElementById('eaAst').className = 'viewError';
            document.getElementById('eaMessage').className = 'viewError';
            $("#eaAst").fadeOut("slow").fadeIn("slow");
            $("#eaMessage").fadeOut("slow").fadeIn("slow");
        }
    }

    if (password) {
        if (password.value.length == 0) {
            success = false;
            document.getElementById('paAst').className = 'viewError';
            document.getElementById('paMessage').className = 'viewError';
            $("#paAst").fadeOut("slow").fadeIn("slow");
            $("#paMessage").fadeOut("slow").fadeIn("slow");
        }
    }

    return success;
}

