// JavaScript Document

//===================================================================================
//Jump out from outer Frame
if (!FrameAllowed){
        if (top.location != self.location) {
                top.location = self.location
        }
}
//===================================================================================

//===================================================================================
//Common functions
//===================================================================================
/*************************************
Description:        verify News letter sign up
Author:                        Joshua Chen
Date:                        2004/10/15
Parameters:                f        (form name)
return:                        boolean
*************************************/
function verifyNewsletterSingup(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
    var error_msg = "";

        f = eval('document.' + f);

        if ((f.LoginName.value == null) || (f.LoginName.value == "") || isblank(f.LoginName.value))
        {
                error_msg = "- Your email field is empty";
        empty_fields += "\n      " + error_msg;
    }


    else if (f.LoginName.value.indexOf("@")==-1)
    {
                error_msg = "- Your email address is invalid";
        empty_fields += "\n      " + error_msg;
    }

    if ((!f.Promotions.checked) && (!f.Newsletter.checked))
    {
                error_msg = "- You didn't specify your desired newsletter option(s).";
        empty_fields += "\n      " + error_msg;
    }

    if (!empty_fields && !errors)
    {
                return true;
        }
    msg  = "______________________________________________________\n\n"
    msg += "Your request cannot continue because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit:\n";
    msg += "______________________________________________________\n"

    if (empty_fields) {
        msg += empty_fields + "\n";
    }
    alert(msg);
    return false;
}

// Commom popup window function
function PopupWin(vName,vLink,vWidth,vHeight,vLeft,vTop,vScrollbar,vResizable)
{
        var para = '';
        if(vWidth!=0)
        {para += ',width=' + vWidth;}

        if(vHeight!=0)
        {para += ',height=' + vHeight;}

        if(vLeft!=0)
        {para += ',left=' + vLeft;}

        if(vTop!=0)
        {para += ',top=' + vTop;}

        if(vScrollbar!=0)
        {para += ',scrollbars=' + vScrollbar;}

        if(vResizable!=0)
        {para += ',resizable=' + vResizable;}

        javascript:window.open(vLink ,vName,'toolbar=0,menubar=0,location=0,directories=0,status=0' + para)
}

/*************************************
Description:        check string is blank
Author:                        Newegg.com
Date:                        2004/04/14
Parameters:                s        (string)
Return:                        boolean
*************************************/
function isblank(s)
{
        for(var i = 0; i < s.length; i++) {
                var c = s.charAt(i);
                if ((c != ' ')&&(c != '\n')&&(c != '\t')) return false;
        }
        return true;
}

/*************************************
Description:        go search, submit form
Author:                        Joshua Chen
Date:                        2004/04/14
Parameters:                sFormName,iLenth
*************************************/
function goSearch(sFormName,iLength)
{
        var objForm;
        objForm = eval('document.' + sFormName);
        if (iLength >= 1)
        {
                objForm.submit();
        }
}

/*************************************
Description:        Expand Menu, change element's style.
Parameters:                item        (item id)
Author:                        Joshua Chen
Date:                        2004/04/14
*************************************/
function ExpandItem(item)
{
        obj = document.getElementById(item);
        visible = (obj.style.display != 'none');
        key = document.getElementById('x' + item);

        if (visible)
        {
                obj.style.display = 'none';
                 key.style.fontWeight = 'normal';
        }
        else
        {
                obj.style.display = 'block';
                key.style.fontWeight = 'bold';
        }
}
//===================================================================================



//===================================================================================
//Rollover menu functions
//===================================================================================
var LAST_SIZE = 0;
var LAST_LEVEL = 0;
var PRODUCT_CELL_ID = 0;
var CLIENT_BROWSER_ID = 0;
var IFRAME_SUPPORT = false;
var DEBUG_MODE = false;

/*************************************
Description:        Javascript Settings Initialization
Parameters:                1). DebugModeID                int
                                2). BrowserID                int
                                3). IFrameSupportID        int
                                4). ProductCellID        int

Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
function JS_Initialize(DebugModeID, BrowserID, IFrameSupportID, ProductCellID, Counttdtd){
        if (IFrameSupportID == 1){IFRAME_SUPPORT = true;}
        if (DebugModeID == 1){DEBUG_MODE = true;}
        CLIENT_BROWSER_ID = BrowserID;
        PRODUCT_CELL_ID = ProductCellID;
        Counttd_td = Counttdtd;

        switch (BrowserID)
        {
                case 1:
                        window.onload = RolloverMenuRunning;
                        var pID = window.setInterval("AutoFontSizeForIE()", 200);
                        break;
                case 2:
                        window.onload = AutoWidthProductCell;
                        var pID = window.setInterval("AutoFontSizeForFF()", 200);
                        break;
                default:
        }
        window.onresize = AutoWidthProductCell;
}

/*************************************
Description:        Rollover Menu Running
Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
RolloverMenuRunning = function(){
        if (document.all && document.getElementById){
                if (document.getElementById("nav")){
                        var navRoot = document.getElementById("nav");
                        var node;
                        for (i=0; i<navRoot.childNodes.length; i++){
                                node = navRoot.childNodes[i];
                                node.onmouseover = function() {
                                        for (var j=0; j<this.childNodes.length; j++){
                                                if (this.childNodes[j].nodeName == "UL"){
                                                        this.childNodes[j].className += " over";
                                                        if (IFRAME_SUPPORT && this.childNodes[j].childNodes[0].style.width == "0px" && this.childNodes[j].childNodes[0].style.height == "0px"){
                                                                var menuWidth = this.childNodes[j].offsetWidth;
                                                                var menuHeight = this.childNodes[j].offsetHeight;
                                                                if (typeof(menuWidth) == "number" && typeof(menuHeight) == "number"){
                                                                        this.childNodes[j].childNodes[0].style.width = menuWidth;
                                                                        this.childNodes[j].childNodes[0].style.height = menuHeight;
                                                                }
                                                        }
                                                break;
                                                }
                                        }
                                }
                                node.onmouseout = function(){
                                        for (var j=0; j<this.childNodes.length; j++){
                                                if (this.childNodes[j].nodeName == "UL"){
                                                        this.childNodes[j].className = this.childNodes[j].className.replace(" over", "");
                                                break;
                                                }
                                        }
                                }
                        }
                }

                if (document.getElementById("navSpecial")){
                        node = document.getElementById("navSpecial");
                        node.onmouseover = function(){
                                for (var j=0; j<this.childNodes.length; j++){
                                        if (this.childNodes[j].nodeName == "UL"){
                                                this.childNodes[j].className += " over";
                                                if (IFRAME_SUPPORT && this.childNodes[j].childNodes[0].style.width == "0px" && this.childNodes[j].childNodes[0].style.height == "0px"){
                                                        var menuWidth = this.childNodes[j].offsetWidth;
                                                        var menuHeight = this.childNodes[j].offsetHeight;
                                                        if (typeof(menuWidth) == "number" && typeof(menuHeight) == "number"){
                                                                this.childNodes[j].childNodes[0].style.width = menuWidth;
                                                                this.childNodes[j].childNodes[0].style.height = menuHeight;
                                                        }
                                                }
                                                break;
                                        }
                                }
                        }
                        node.onmouseout = function(){
                                for (var j=0; j<this.childNodes.length; j++){
                                        if (this.childNodes[j].nodeName == "UL"){
                                                this.childNodes[j].className = this.childNodes[j].className.replace(" over", "");
                                                break;
                                        }
                                }
                        }
                }
        }
}

/*************************************
Description:        Set Style
Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
function SetStyle(id,css,type)
{
        if (!document.getElementById(id)){return;}
        switch (type){
                case 0:
                        document.getElementById(id).className = css;
                        break;

                case 1:
                        document.getElementById(id).className += " "+css;
                        break;

                default:
                        break;
        }
}

/*************************************
Description:        Clear Style
Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
function ClearStyle(id,css,type)
{
        if (!document.getElementById(id)){return;}
        switch (type){
                case 0:
                        document.getElementById(id).className = "";
                        break;

                case 1:
                        document.getElementById(id).className = document.getElementById(id).className.replace(" "+css,"");
                        break;

                default:
                        break;
        }

}

/*************************************
Description:        Auto Change Firefox,Mozilla,Opera,NetScape Font Size
Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
function AutoFontSizeForFF(){
        var dBody = document.defaultView.getComputedStyle(document.body,null);
        var dSize = dBody.getPropertyValue("font-size");
        dSize = parseInt(dSize.replace("/[^\d]/",""));
        if (dSize == LAST_SIZE){return;}

        if (dSize > 16){
                if (dSize < 24){
                        SetStyle("TopNavigation","FontSizeLarger",0);
                        SetStyle("leftNav","FontSizeLarger",0);
                        SetStyle("Search-6","FontSizeLarger",0);
                        SetStyle("centerArea","FontSizeLarger",0);
                        SetStyle("prdTopNav","prdTopNavSize1",0);

                }else{
                        SetStyle("TopNavigation","FontSizeLargest",0);
                        SetStyle("leftNav","FontSizeLargest",0);
                        SetStyle("Search-6","FontSizeLargest",0)
                        SetStyle("centerArea","FontSizeLargest",0);
                        SetStyle("prdTopNav","prdTopNavSize1",0);
                }
        }else{
                ClearStyle("TopNavigation","",0);
                ClearStyle("leftNav","",0);
                ClearStyle("Search-6","",0);
                ClearStyle("centerArea","",0);
                ClearStyle("prdTopNav","",0);
        }

        if (document.getElementById("BigFontWarring")){
                if (dSize > 24){
                        document.getElementById("BigFontWarring").style.display = "block";
                }else{
                        document.getElementById("BigFontWarring").style.display = "none";
                }
        }
        LAST_SIZE = dSize;
        AutoWidthProductCell();
}

/*************************************
Description:        Auto Change IE Font Size
Author:                        Joshua Chen
Date:                        2004/07/14
*************************************/
function AutoFontSizeForIE(){
        if (!window.readyState == "complete"){return;}
        if (!document.getElementById("IEFontCheck")){return;}
        var dSize = document.getElementById("IEFontCheck").offsetWidth;
        var action = 1;
        if (dSize == LAST_SIZE){return;}

        if (dSize <= 8){
                action = 1;
        }else if(dSize <= 10){
                action = 2;
        }
        else{
                action = 3;
        }
        SetActivedCSS(action);
        LAST_SIZE = dSize;
        AutoWidthProductCell();
}

/********************************************
Description:        Auto Width Homepage Store Product Cell
Author:                        Joshua Chen
Date:                        2005/03/10
*******************************************/
AutoWidthProductCell = function(){
        switch (PRODUCT_CELL_ID){
                case 1: //Homepage Store Except DVD Store
                        if (!document.getElementById("centerArea")){return;}
                        if (!document.getElementById("HomepagePrd")){return;}
                        var allW = document.getElementById("centerArea").offsetWidth;
                        var itemCount = document.getElementById("HomepagePrd").className;
                        var itemEnd = itemCount.indexOf(" ");
                        if (itemEnd == -1){itemEnd = itemCount.length;}
                        itemCount = parseInt(itemCount.substring(4,itemEnd));
                        allW = parseInt(allW);
                        if (allW < 200){allW = 200;}
                        var sCount = parseInt(allW / 200);
                        if (sCount > Counttd_td){sCount = Counttd_td;}
                        if (sCount > itemCount){sCount = itemCount;}
                        var sPd = parseInt((allW - (200 * sCount)) / (sCount+sCount));
                        var hps;
                        sPd = sPd +"px";
                        for (var i=1;i<=itemCount;i++){
                                if (document.getElementById("Section"+i)){
                                        hps = document.getElementById("Section"+i);
                                        hps.style.posTop = "0px";
                                        hps.style.paddingLeft = 0;
                                        hps.style.paddingRight = 0;
                                        hps.style.paddingLeft = sPd;
                                        if ((i / sCount).toString().indexOf(".") == -1){
                                                hps.className = "SectionRt";
                                        }else{
                                                hps.className = "Section";
                                                hps.style.paddingRight = sPd;
                                        }
                                }
                        }
                        break;

                case 2: //DVD Store
                        var itemCount = new Array(3);
                        var itemName = new Array(3);
                        itemName[0] = "ne";
                        itemName[1] = "ns";
                        itemName[2] = "cs";
                        var bHaveDvd = false;
                        var tmpItemCount;
                        if (!document.getElementById("centerArea")){return;}
                        if (document.getElementById("neDVDPrd")){
                                tmpItemCount = document.getElementById("neDVDPrd").className;
                                itemCount[0] = parseInt(tmpItemCount.substring(4,tmpItemCount.length));
                                bHaveDvd = true;
                        }
                        if (document.getElementById("nsDVDPrd")){
                                tmpItemCount = document.getElementById("nsDVDPrd").className;
                                itemCount[1] = parseInt(tmpItemCount.substring(4,tmpItemCount.length));
                                bHaveDvd = true;
                        }
                        if (document.getElementById("csDVDPrd")){
                                tmpItemCount = document.getElementById("csDVDPrd").className;
                                itemCount[2] = parseInt(tmpItemCount.substring(4,tmpItemCount.length));
                                bHaveDvd = true;
                        }
                        if (!bHaveDvd){return;}

                        var allW = document.getElementById("centerArea").offsetWidth;
                        allW = parseInt(allW);

                        if (allW < 200){allW = 200;}
                        var sCount = parseInt(allW / 100);
                        if (sCount > 4){sCount = 4;}
                        var tmpsCount;
                        for (var i=0;i<itemCount.length;i++){
                                if (tmpsCount < itemCount[i]){tmpsCount = itemCount[i];}
                        }
                        if (sCount > tmpsCount){sCount = tmpsCount;}
                        var sPd = parseInt((allW - (100 * sCount)) / (2*sCount));
                        var hps;
                        sPd = sPd +"px";
                        for (var j=0;j<itemCount.length;j++){
                                for (var i=1;i<=itemCount[j];i++){
                                        if (document.getElementById("Section"+itemName[j]+i)){
                                                hps = document.getElementById("Section"+itemName[j]+i);
                                                hps.style.display = "";
                                                hps.style.posTop = "0px";
                                                hps.style.paddingLeft = sPd;
                                                hps.style.paddingRight = 0;
                                                if ((i / sCount).toString().indexOf(".") == -1){
                                                        hps.className = "SectionRt";
                                                }else{
                                                        hps.className = "Section";
                                                        hps.style.paddingRight = sPd;
                                                }
                                        }
                                }
                                if (j==0 && sCount == 3 && hps){hps.style.display = "none";}
                        }
                        break;

                default:
                        break;
        }
}

//===================================================================================
//Base functions
//===================================================================================
//Request.Cookies
Request = function(){}
Request.Cookies = function(name,key){
        try
        {
                Array.prototype.Request = null;
                var rv = null;
                //Init all cookies collection
                var c = window.document.cookie;
                if (c.length == 0){return rv;}
                var cc = c.split("; ");
                var al = cc.length;
                var nb = -1;
                var cn = "";
                var cv = "";
                //Get cookie value by name
                if (name != null && name.length > 0){
                        var cre = eval("/^"+ name +"$/i;");
                        for (var i=0; i<al; i++){
                                nb = cc[i].indexOf("=");
                                if (nb != -1){
                                        cn = cc[i].substring(0,nb);
                                        cv = cc[i].substring(nb+1);
                                        if (Request.Cookies.Corrector(cn).match(cre) != null){
                                                //Get cookie value by name.key
                                                if(key != null && key.length > 0){
                                                        if (cv.length > 0 && cv.indexOf("=") != -1){
                                                                var kc = cv.split("&");
                                                                var kl = kc.length;
                                                                var kb = -1;
                                                                var kre = eval("/^"+ key +"$/i;");
                                                                //Loop cookie sub key value
                                                                for (var j=0; j<kc.length; j++){
                                                                        kb = kc[j].indexOf("=");
                                                                        if (kb != -1){
                                                                                if (Request.Cookies.Corrector(kc[j].substring(0,kb)).match (kre) != null){
                                                                                        rv = Request.Cookies.Corrector(kc[j].substring(kb+1));
                                                                                        break;
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }else{
                                                        if (cv.length > 0 && cv.indexOf("=") != -1){
                                                                rv = cv;
                                                        }else{
                                                                rv = Request.Cookies.Corrector(cv);
                                                        }
                                                }
                                                break;
                                        }
                                }
                        }
                }else{//Get all cookies
                        var ac = new Array(al*2);
                        ac.Request = "Aggregate";
                        //Loop cookie value
                        for (var i=0; i<al; i++){
                                nb = cc[i].indexOf("=");
                                if (nb != -1){
                                        cn = cc[i].substring(0,nb);
                                        cv = cc[i].substring(nb+1);
                                        ac[i] = Request.Cookies.Corrector(cn);
                                        if (cv.length > 0 && cv.indexOf("=") != -1){
                                                var kc = cv.split("&");
                                                var kl = kc.length;
                                                var ak = new Array(kl*2);
                                                ak.Request = "Aggregate";
                                                var kb = -1;
                                                //Loop cookie sub key value
                                                for (var j=0; j<kc.length; j++){
                                                        kb = kc[j].indexOf("=");
                                                        if (kb != -1){
                                                                ak[j] = Request.Cookies.Corrector(kc[j].substring(0,kb));
                                                                ak[j+kl] = Request.Cookies.Corrector(kc[j].substring(kb+1));
                                                        }
                                                }
                                                ac[i+al] = ak;
                                        }else{
                                                ac[i+al] = Request.Cookies.Corrector(cv);
                                        }
                                }else{
                                        ac[i] = Request.Cookies.Corrector(cc[i]);
                                        ac[i+al] = null;
                                }
                        }
                        rv = ac;
                }

                return rv;
        }
        catch(e)
        {
                if (DEBUG_MODE){
                        throw e;
                }else{
                        return null;
                }
        }
}
Request.Cookies.Corrector = function(v){
        if (v.length > 0){
                v = v.replace(/\+/g," ");
        }
        return unescape(v);
}


