﻿String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, '').replace(/{;}/g, ''); };
String.prototype.myParseFloat = function() { return parseFloat(this.replace(',', '')); };
Number.prototype.myParseFloat = function() { return parseFloat(this); };

var changesMade = false;
var secondPrompt = false;
var confirmExitMessage = "You have not Save your work. Your changes made within current session will be lost.";
window.onbeforeunload = confirmExit;
function confirmExit() {
    try {
        if ((changesMade) && (secondPrompt == false)) {
            secondPrompt = true;
            setTimeout("secondPrompt = false;", 10);
            return confirmExitMessage;
        }
    } catch (e) { alert(e.message); }
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function roundVal(val, _5senRound) { //pending work
    var dec = 2;
    var result = Math.round(val.myParseFloat() * Math.pow(10, dec)) / Math.pow(10, dec);
    result = result + "";
    var i = result.indexOf(".");
    if (i < 0) {
        result += ".";
        dec = -2;
    } else {
        dec = result.length - i - 3;
        if ((dec == 0) && (_5senRound)) {
            var lastDigit = result.substr(result.length - 1, 1);
            if (parseInt(lastDigit, 10) < 3) lastDigit = parseInt(lastDigit, 10) * -1;
            else if (parseInt(lastDigit, 10) > 7) lastDigit = 10 - parseInt(lastDigit, 10);
            else if (parseInt(lastDigit, 10) == 5) lastDigit = 0;
            else lastDigit = 5 - parseInt(lastDigit, 10);
            result = result.myParseFloat() + (lastDigit.myParseFloat() / 100);
        }
        result = Math.round(result.myParseFloat() * Math.pow(10, 2)) / Math.pow(10, 2);
        result = result + "";
        i = result.indexOf(".");
        dec = result.length - i - 3;
    }
    for (var j = dec; j < 0; j++) {
        result += "0";
    }
    return addCommas(result);
}

function getSelectionStart(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate();
        r.moveEnd('character', o.value.length);
        if (r.text == '') return o.value.length;
        return o.value.lastIndexOf(r.text);
    } else return o.selectionStart;
}

function getSelectionEnd(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate();
        r.moveStart('character', -o.value.length);
        return r.text.length;
    } else return o.selectionEnd;
}

function showdeadcenterobj(objid) {
    var o = document.getElementById(objid);
    if (o) {
        o.style.display = "block";
        var Xwidth = o.clientWidth; //400;
        var Yheight = o.clientHeight; //213;
        // First, determine how much the visitor has scrolled
        var scrolledX, scrolledY;
        if (self.pageYOffset) {
            scrolledX = self.pageXOffset;
            scrolledY = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            scrolledX = document.documentElement.scrollLeft;
            scrolledY = document.documentElement.scrollTop;
        } else if (document.body) {
            scrolledX = document.body.scrollLeft;
            scrolledY = document.body.scrollTop;
        }

        // Next, determine the coordinates of the center of browser's window
        var centerX, centerY;
        if (self.innerHeight) {
            centerX = self.innerWidth;
            centerY = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            centerX = document.documentElement.clientWidth;
            centerY = document.documentElement.clientHeight;
        } else if (document.body) {
            centerX = document.body.clientWidth;
            centerY = document.body.clientHeight;
        }

        // Xwidth is the width of the div, Yheight is the height of the
        // div passed as arguments to the function:
        var leftOffset = scrolledX + (centerX - Xwidth) / 2;
        var topOffset = scrolledY + (centerY - Yheight) / 2;
        // The initial width and height of the div can be set in the
        // style sheet with display:none; objid is passed as an argument to // the function
        var r = o.style;
        r.top = topOffset + 'px';
        r.left = leftOffset + 'px';
        r.display = "block";
    }
}

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]; }
}

checked = false;
function checkedAll() {
    var aa = document.forms[0];
    if (checked == false) {
        checked = true;
    }
    else {
        checked = false;
    }
    for (var i = 0; i < aa.elements.length; i++) {
        if (aa.elements[i].checked != checked) {
            if (aa.elements[i].click) aa.elements[i].click();
            else aa.elements[i].checked = checked;
        }
    }
}

function GetElementTop(eSrc) {
    var iTop = 0;
    var eParent;
    eParent = eSrc;
    while (eParent.tagName.toUpperCase() != "BODY") {
        iTop += eParent.offsetTop;
        if (eParent.style.position == "absolute") break;
        eParent = eParent.offsetParent;
        if (eParent == null) break;
    }
    return iTop;
}
function GetElementLeft(eSrc) {
    var iLeft = 0;
    try {
        var eParent;
        eParent = eSrc;
        while (eParent.tagName.toUpperCase() != "BODY") {
            iLeft += eParent.offsetLeft;
            if (eParent.style.position == "absolute") break;
            eParent = eParent.offsetParent;
        }
    } catch (e) { }
    return iLeft;
}

/*
**  Returns the caret (cursor) position of the specified text field.
**  Return value range is 0-oField.length.
*/
function doGetCaretPosition(oField) {

    // Initialize
    var iCaretPos = 0;

    // IE Support
    if (document.selection) {

        // Set focus on the element
        oField.focus();

        // To get cursor position, get empty selection range
        var oSel = document.selection.createRange();

        // Move selection start to 0 position
        oSel.moveStart('character', -oField.value.length);

        // The caret position is selection length
        iCaretPos = oSel.text.length;
    }

    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0')
        iCaretPos = oField.selectionStart;

    // Return results
    return (iCaretPos);
}


/*
**  Sets the caret (cursor) position of the specified text field.
**  Valid positions are 0-oField.length.
*/
function doSetCaretPosition(oField, iCaretPos) {

    // IE Support
    if (document.selection) {

        // Set focus on the element
        oField.focus();

        // Create empty selection range
        var oSel = document.selection.createRange();

        // Move selection start and end to 0 position
        oSel.moveStart('character', -oField.value.length);

        // Move selection start and end to desired position
        oSel.moveStart('character', iCaretPos);
        oSel.moveEnd('character', 0);
        oSel.select();
    }

    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0') {
        oField.selectionStart = iCaretPos;
        oField.selectionEnd = iCaretPos;
        oField.focus();
    }
}

function openprintdialog(url) {
    window.open(url, "printpop", "width=1,height=1,left=0,top=0,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0");
}

function findPos(obj) {
    var curleft = curtop = 0;
    do {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
    } while ((obj = obj.offsetParent) && (obj.tagName.toUpperCase() != "BODY"));
    return curleft + "," + curtop;
}

function XmlDecode(orival) {
    return orival.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/, "\"").replace(/&apos;/g, "'").replace(/&amp;/g, "&");
}

animating = false;
function animHide(objId, vdirection, currval, minsize) {
    animating = true;
    var obj = document.getElementById(objId);
    obj.style.overflow = "hidden";
    obj.parentNode.style.overflow = "hidden";
    var localcontrolsize = 10;
    if (minsize != null && minsize != undefined) {
        currval -= Math.abs(Math.ceil((parseInt(currval, 10) - parseInt(minsize, 10)) / 2));
        localcontrolsize = minsize;
    } else currval /= 2;
    if (currval <= localcontrolsize) {
        if (minsize == null) {
            obj.style.visibility = "hidden";
            obj.parentNode.style.display = "none";
        } else {
            obj.style.width = minsize + "px";
            obj.parentNode.style.width = minsize + "px";
        }
        animating = false;
    } else {
        if (vdirection) {
            obj.style.width = currval + "px";
            obj.parentNode.style.width = currval + "px";
        } else {
            obj.style.height = currval + "px";
            obj.parentNode.style.height = currval + "px";
        }
        setTimeout("animHide( \"" + objId + "\", " + vdirection + ", " + currval + ", " + minsize + " );", 100);
    }
}

function animShow(objId, vdirection, fullsize, locksizeflag) {
    animating = true;
    var obj = document.getElementById(objId);
    if (vdirection) {
        var widthval = fullsize;
        var currval = 0;
        if (obj.parentNode.style.display != "none") {
            currval = obj.parentNode.clientWidth;
        } else {
            obj.parentNode.style.display = "";
            obj.style.visibility = "visible";
        }
        widthval -= currval;
        widthval /= 2;
        if (widthval < 10) {
            obj.style.width = fullsize + "px";
            obj.parentNode.style.width = fullsize + "px";
            obj.style.overflow = "visible";
            obj.parentNode.style.overflow = "visible";
            if (!locksizeflag) {
                obj.style.width = "auto";
                obj.parentNode.style.width = "auto";
            }
            animating = false;
        } else {
            obj.style.width = (currval + widthval) + "px";
            obj.parentNode.style.width = (currval + widthval) + "px";
            setTimeout("animShow( \"" + objId + "\", true, " + fullsize + "," + locksizeflag + " );", 100);
        }
    } else {
        var heightval = fullsize;
        var currval = 0;
        if (obj.parentNode.style.display != "none") {
            currval = obj.parentNode.clientHeight;
        } else {
            obj.parentNode.style.display = "";
            obj.style.visibility = "visible";
        }
        heightval -= currval;
        heightval /= 2; //alert(heightval);
        if (heightval < 10) {
            obj.style.height = fullsize + "px";
            obj.parentNode.style.height = fullsize + "px";
            obj.style.overflow = "visible";
            obj.parentNode.style.overflow = "visible";
            if (!locksizeflag) {
                obj.style.height = "auto";
                obj.parentNode.style.height = "auto";
            }
            animating = false;
        } else {
            obj.style.height = (currval + heightval) + "px";
            obj.parentNode.style.height = (currval + heightval) + "px";
            setTimeout("animShow( \"" + objId + "\", false, " + fullsize + ", " + locksizeflag + " );", 100);
        }
    }
}

function togglemenubar(buttonobj, vdirection, fullsize, menuid, simulflag) {
    if (animating && !simulflag) return;
    var obj = document.getElementById(menuid);
    if (obj.style.visibility == "hidden") {
        animShow(menuid, vdirection, fullsize, true);
        buttonobj.className = "hidemenubar";
    } else {
        animHide(menuid, vdirection, fullsize);
        buttonobj.className = "showmenubar";
    }
}

function toggleVisibility(objId) {
    var obj = document.getElementById(objId);
    if (obj.style.display == "none") {
        obj.style.display = "";
    } else {
        obj.style.display = "none";
    }
}

function submitsearch() {
    document.location.href = "search.aspx?txtsearchbook=" + document.getElementById("txtsearchbook").value;
    //document.forms[0].submit();
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

var onloadtries = 0;
function onloadfunction() {
    try {
        var objs = document.getElementsByTagName("img");
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].src.indexOf("btn_buynowg.png") >= 0) {
                objs[i].parentNode.style.display = "none";
            } else if (objs[i].src.indexOf("btn_soldout.png") >= 0) {
                objs[i].parentNode.style.display = "none";
            }
        }
    } catch (ex) {
        if (++onloadtries < 20) {
            setTimeout("onloadfunction();", 500);
        }
    }
}
//if (window.attachEvent) { window.attachEvent('onload', onloadfunction); }
//else if (window.addEventListener) { window.addEventListener('load', onloadfunction, false); }
//else { document.addEventListener('load', onloadfunction, false); }

function checkEmail(inputvalue) {
    var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if (pattern.test(inputvalue)) {
        return true;
    } else {
        return false;
    }
}

function togglesearcharea() {
    if (jqnoflict("#openCloseIdentifier").is(":hidden")) {
        jqnoflict("#searchSlider").animate({
            marginTop: "-73px"
        }, 500);
        document.getElementById("topMenuImage").src = "/static-images/search/open.png";
        document.getElementById("topMenuImage").setAttribute("title", "open");
        jqnoflict("#openCloseIdentifier").show();
    } else {
        jqnoflict("#searchSlider").animate({
            marginTop: "0px"
        }, 500);
        document.getElementById("topMenuImage").src = "/static-images/search/close.png";
        document.getElementById("topMenuImage").setAttribute("title", "close");
        jqnoflict("#openCloseIdentifier").hide();
    }
}
function toggle2tboxes(obj, id2) {
    if ((obj.getAttribute("id").indexOf("dummy") < 0) && (obj.value.length > 0)) return;
    obj.style.display = 'none';
    document.getElementById(id2).style.display = '';
    if (obj.getAttribute("id").indexOf("dummy") >= 0) setTimeout("document.getElementById(\"" + id2 + "\").focus();", 100);
}

function adjustvidsize(controlW) {
    var vobj = document.getElementById("vobj");
    if (vobj) {
        vobj.innerHTML = vobj.innerHTML.replace(/\n/g,"").replace(/iframe><iframe/g,"iframe><br/><br/><iframe");
        for (var i = 0; i < vobj.childNodes.length; i++) {
            try {
                obj = vobj.childNodes[i];
                var currwidth = obj.getAttribute("width");
                if ((currwidth + "").length > 0) {
                    if (obj.tagName.toLowerCase() == "iframe") {
                        if (obj.src.indexOf("wmode=opaque") < 0) {
                            if (obj.src.indexOf("?") < 0) obj.src += "?wmode=opaque";
                            else obj.src += "&wmode=opaque";
                        }
                    }
                    currwidth = parseInt(currwidth, 10);
                    if (currwidth > controlW) {
                        obj.style.width = controlW + "px";
                        var currheight = obj.getAttribute("height");
                        if ((currheight + "").length > 0) {
                            currheight = parseInt(currheight, 10);
                            var newH = parseInt(controlW * currheight / currwidth, 10);
                            obj.style.height = newH + "px";
                        }
                    }
                }
            } catch (ex) { }
        }
    }
}
