﻿isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}
jQuery.extend(jQuery.browser, { MobileSafari: (navigator.userAgent.toLowerCase().match(/iP(hone|ad|od)/i) != null) });


function PreloadImage(path) {
    arrImages[arrImages.Length] = new Image();
    arrImages[arrImages.Length].src = path;
}

function CheckLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
    }
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}

function trim(stringToTrim) {
    if (stringToTrim != undefined) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    }
    else
        return "";
}
function ltrim(stringToTrim) {
    if (stringToTrim != undefined)
        return stringToTrim.replace(/^\s+/, "");
    else return "";
}
function rtrim(stringToTrim) {
    if (stringToTrim != undefined)
        return stringToTrim.replace(/\s+$/, "");
    else return "";
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

var inlineWindows = new Array();

function OpenInline(id, width, height, url, innerText, title, level) {
    $("#divItemContentLong object").hide();

    // register the open window

    winIndex = inlineWindows.indexOf(id);

    //    if (winIndex > -1)
    //    {
    //            $("#" + id).fadeOut("fast", function() {
    //            $("#" + id).remove();
    //            $('#divBodyFade').hide();
    //            });

    //            inlineWindows.splice(winIndex, 1);
    //    }

    var actionClose = "'HideInline(\"" + id + "\");'";

    if (id == 'alertBox')
        actionClose = "$('#btnOk').click()";

    var lvl = (level + 1) * 10; var baseLvl = 100; var curLvl = baseLvl + lvl;
    strWindowContent = "<div id='" + id + "' class='inlineWindow' name='inlineWindow' style='display:none; height:" + (height + 70) + "px; width:" + (width + 72 + (($.browser == "msie" && $.browser.version == "6.0") ? 10 : 0)) + "px; z-index:" + curLvl + "'>";
    strWindowContent += "<div class='t-l'></div><div class='t' style='width:" + width + "px'><div class='TopTitle'>" + title + "</div></div><div class='t-r' id='btnClose' style='cursor:pointer' onclick=" + actionClose + "></div>";
    strWindowContent += "<div class='m-l' id='m-l' style='height:" + height + "px'></div><div class='m' id='m' style='width:" + width + "px; height:" + height + "px'>";

    strWindowContent += "<div style='width:" + (width - 20) + "px; padding:10px;float:left'>";
    title = "";
    //    if (title!="")
    //        strWindowContent += "<div style='width" + (width - 20) + "px; border-bottom:solid 1px #AAAAAA; padding-bottom: 10px; margin-bottom:10px; float:left'><div style='font:bold 16px Arial;width:" + (width - 120) + "px;float:left'>" + title + "</div><div style='width:100px;float:left; text-align:right'><img class='NoFloat' src='//educationalwarehouse.com/assets/layout/Window/close.gif' style='cursor:pointer' onclick='HideInline(\"" + id + "\");' /></div></div>"

    strWindowContent += "<div style='float:left; overflow:auto; height: " + (height - 30) + "px; width:" + (width - 20) + "px' id='" + id + "_Content'></div></div>";

    strWindowContent += "</div><div class='m-r' id='m-r' style='height:" + height + "px'></div>";
    strWindowContent += "<div class='b-l'></div><div class='b' style='width:" + width + "px'></div><div class='b-r'></div>";
    strWindowContent += "</div>";
    $('#divBodyFade').css({ "width": $(document).width() });
    $('#divBodyFade').css({ "height": $(document).height() });
    $('#divBodyFade').css({ "z-index": baseLvl + lvl - 5 });
    $("#divBodyFade").show();
    $("body").append(strWindowContent);


    if (innerText != "")
        $("#" + id + "_Content").html(innerText);
    else {
        $("#" + id + "_Content").html("<img src='//ninashoes.com/assets/layout/window/loading.gif' style='padding-top:" + (height - 16) / 2 + "px; padding-left:" + (width - 16) / 2 + "px' />");
        $.get(url, function(data) {
            $("#" + id + "_Content").html(data);
        });
    }

    $("#" + id).fadeIn("fast");

    var scrollCompensation = ($.browser.MobileSafari ? $(document).scrollTop() : 0);
    $("#" + id).css({
        top: ($(window).height() - height - 45) / 2 + scrollCompensation,
        left: ($(window).width() - width - 45) / 2
    });

    if ($.browser.MobileSafari)
        $(window).scroll(function() {
            var zoom = document.documentElement.clientWidth / window.innerWidth;
            if (document.documentElement.clientWidth == window.innerWidth || document.documentElement.clientWidth == window.innerWidth - 1) // because iPad bug on screen rotation :)
                $("#" + id).css({ top: ($(window).height() - height - 45) / 2 + $(document).scrollTop() });
        });

    inlineWindows[inlineWindows.length] = id;

    if (isIE6)
        resizeWindowPopupOnScrollOrResize();

    $("#" + id).supersleight();


    $(document).keydown(function(event) {
        if (event.keyCode == 13 && level == 0) {
            if (id == 'alertBox') {
                $("#btnOk").click();
            }
        }
    });
    // close window when escape key is pressed
    $(document).keydown(function(event) {
        if (event.keyCode == 27) {
            if (id != 'alertBox' && id != 'Register' && level == 0) {
                HideInline(inlineWindows[inlineWindows.length - 1]);
            }
            else
                $("#btnOk").click();
        }
    });
}


function HideInline(id) {

    $("#divFormError").hide();

    $("#" + id).fadeOut("fast", function() {
        $("#" + id).remove();
        inlineWindows.splice(inlineWindows.indexOf(id), 1);
        if (inlineWindows.length == 0)
            $('#divBodyFade').hide();
        else {
            var fadeIndex = $('#divBodyFade').css("z-index");
            fadeIndex = fadeIndex - inlineWindows.length * 10;
            $('#divBodyFade').css({ "z-index": fadeIndex });
        }
    });


}

function HideInlineWithCallback(id, callback) {

    $("#divFormError").hide();

    $("#" + id).fadeOut("fast", function() {
        $("#" + id).remove();
        inlineWindows.splice(inlineWindows.indexOf(id), 1);
        if (inlineWindows.length == 0)
            $('#divBodyFade').hide();
        else {
            var fadeIndex = $('#divBodyFade').css("z-index");
            fadeIndex = fadeIndex - inlineWindows.length * 10;
            $('#divBodyFade').css({ "z-index": fadeIndex });
        }
        callback();
    });


}

function InitializeWindowHeight(id) {

    return $("#" + id).height();
}

function ResizeWindowHeight(id, errorContentId, height) {

    var errorHeight = $("#" + errorContentId).height();

    var popupH = (isIE6) ? errorHeight + height : errorHeight + height + 20;
    var contentH = (isIE6) ? errorHeight + height - 50 : errorHeight + height - 20;
    var marginH = popupH - 32;
    if (isIE6) marginH += 21;


    $("#" + id).height(popupH);
    $("#" + id + "_Content").height(contentH);
    $("#m").height(marginH);
    $("#m-r").height(marginH);
    $("#m-l").height(marginH);
}

function HideAllInline() {
    for (i = 0; i < inlineWindows.length; i++) {
        HideInline(inlineWindows[i]);
    }
}

function ShowAlert(title, text, callback) {
    OpenInline('alertBox', 400, 170, '', "<div style='float:left; width:370px;margin-top:30px; text-align:center;'>" + text + "<br><br><br><img class='NoFloat' id='btnOk' src='//educationalwarehouse.com/assets/layout/ok.gif' width='100px' height='26px' onclick='HideInline(\"alertBox\"); " + callback + "' style='cursor:pointer' /></div>", title, 0, '');
}

function ShowAlertWindow(title, text, callback, lvl) {
    OpenInline('alertBox', 400, 170, '', "<div style='float:left; width:370px;margin-top:30px; text-align:center;'>" + text + "<br><br><br><img class='NoFloat' id='btnOk' src='//educationalwarehouse.com/assets/layout/ok.gif' width='100px' height='26px' onclick='HideInline(\"alertBox\"); " + callback + "' style='cursor:pointer' /></div>", title, lvl, '');
}

function HandleErrors(container, errorMessages) {
    if (errorMessages) {
        if (errorMessages.length > 0) {
            container.html("");
            for (i = 0; i < errorMessages.length; i++) {
                container.append("* " + errorMessages[i] + "<br />");
            }

            container.slideDown("fast");
        }

        return errorMessages.length > 0;
    }
    return false;
}

function ParseResponse(response) {
    errorMessages = new Array();

    if (response.indexOf("ERROR:") >= 0) {
        errorMessages = response.substring(6, response.length).split("|");
    }

    return errorMessages;
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
    }

    function toggledisplay(el) {
        $("#" + el).toggle();
    }    

