﻿function updateMiniCart(totaItem,sumPrice)
{
    if($('span[id*=TotalQtyLabel]') && $('span[id*=TotalAmountLabel]'))
    { 
        $('span[id*=TotalQtyLabel]').text(totaItem);
        $('span[id*=TotalAmountLabel]').text("$"+sumPrice.toFixed(2));
    } 
}

function openFixWin(address, winname, height, width, scrollbars,resizeable) {
    var iHeight = height;
    var iWidth = width;
    var iLeft = (screen.width - iWidth)/2;
    var iTop = (screen.height- iHeight)/2;
    var iScrollbars = scrollbars;
    var iResizeable = resizeable;
    if(iScrollbars == null || iScrollbars=="") {
        iScrollbars = "yes";
    } 
    var feature = "left="+iLeft+",top="+iTop+",fullscreen=0,width= " + iWidth + ",height=" + iHeight +",toolbar=no, menubar=no, scrollbars=" + iScrollbars + ", resizable=" + iResizeable+ ", location=no, status=no";
    var win =window.open(address,winname,feature);
    win.focus();
    win = null;
}

function appendRandomToUrl(url) {    
    if (url) {
        var appendParam = "random=";
        var index = url.indexOf(appendParam);
        if (index > 0) {
            url = url.substring(0, index);
        }
        var concatStr = "";
        var lastChar = url.charAt(url.length-1);
        if (lastChar != '&' && lastChar != '?') {
            if (url.indexOf('?') > 0) concatStr = "&";
            else concatStr = "?";
        }
        url += concatStr + appendParam + Math.random();        
    }
    return url;
}
function blockMultiClick(button,time)
{
    if(!time || time==""){
        time=500;
    }
    button.disabled='disabled';
    setTimeout(function(){button.disabled="";},500);
}

function updateSignName()
{
    var url = "/AjaxHandler/GetCustomerFirstName.ajax?random="+Math.random();
    var userName = ajaxSync(url);
    if(userName==""){//show sign in
        $("#signInLi").css("display","block");
        $("#signOutLi").css("display","none");
        //$("#frontHomeLinkLi").css("display","block");
        //$("#backHomeLinkLi").css("display","none");
    }else{ //show welcome and sign out
        $("#signInLi").css("display","none");
        $("#signOutLi").css("display","block");
        //$("#frontHomeLinkLi").css("display","none");
        //$("#backHomeLinkLi").css("display","block");
        $("#welcomeUserNameSpan").attr("innerHTML","Welcome "+userName);
    }
}

//var webConfig=new Object();
//webConfig.cDNModeOfHttp=true;
//webConfig.cDNModeOfHttps=true;
//webConfig.hostIndex=0;
//webConfig.hostOfHttp=new Array();
//webConfig.hostOfHttp[0]='c1';
//webConfig.hostOfHttp[1]='c2';
//webConfig.hostOfHttp[2]='c3';
//webConfig.hostOfHttps=new Array();
//webConfig.hostOfHttps[0]='c1';
//webConfig.hostOfHttps[1]='c2';
//webConfig.hostOfHttps[2]='c3';
//webConfig.domainOfHttp='diapers.com';
//webConfig.domainOfHttps='diapers.com';

var webConfig=new Object();
webConfig.cDNModeOfHttp=true; 
webConfig.hostOfHttp=new Array();
webConfig.hostOfHttp[webConfig.hostOfHttp.length]='c1';
webConfig.hostOfHttp[webConfig.hostOfHttp.length]='c2'; 
webConfig.hostOfHttp[webConfig.hostOfHttp.length]='c3';
webConfig.hostOfHttps=new Array(); 
webConfig.cDNModeOfHttps=false; 
webConfig.hostIndex=0; 
webConfig.domainOfHttp='diapers.com'; 
webConfig.domainOfHttps='diapers.com'; 
function getDomain()
{
    if(!webConfig)
    {
        return '';
    } 
    if(webConfig.cDNModeOfHttp && window.location.protocol=="http:")
    {
        webConfig.hostIndex=webConfig.hostIndex<webConfig.hostOfHttp.length?webConfig.hostIndex:0;
        return "http://"+ webConfig.hostOfHttp[webConfig.hostIndex++]+"."+webConfig.domainOfHttp;
    } 
    else if(webConfig.cDNModeOfHttps && window.location.protocol=="https:")
    {
        webConfig.hostIndex=webConfig.hostIndex<webConfig.hostOfHttps.length?webConfig.hostIndex:0;
        return "https://"+ webConfig.hostOfHttps[webConfig.hostIndex++]+"."+webConfig.domainOfHttp;
    } 
    else
    {
        return ''; 
    }  
}

function getDomainById(id)
{
    if(!webConfig)
    {
        return '';
    } 
    if(webConfig.cDNModeOfHttp && window.location.protocol=="http:")
    {
        return "http://"+ webConfig.hostOfHttp[id%webConfig.hostOfHttp.length]+"."+webConfig.domainOfHttp;
    } 
    else if(webConfig.cDNModeOfHttps && window.location.protocol=="https:")
    {
        return "https://"+ webConfig.hostOfHttps[id%webConfig.hostOfHttps.length]+"."+webConfig.domainOfHttp;
    } 
    else
    {
        return ''; 
    }  
}


function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 30; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function refreshHeader()
{
    var url = "/AjaxHandler/GetShopCartAndShipFee.ajax?random="+Math.random();
    ajaxGetFromUrl(url, bindShopCartAndFee);
}

function bindShopCartAndFee(data,status)
{ 
    var result =eval(data);    
    $(".totalQty").attr("innerHTML",result[0]);
    $(".totalAmount").attr("innerHTML",result[1]);
    $("#fastFreeShippingText").attr("innerHTML",result[2]);
}
 function trim(str)
   {
     s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
     return s;
   }

function limitTextBoxMaxLength(textBox,maxLength){
    //if(textBox && length && maxLength>0){
        //if(textBox.value.length>maxLength){
            //textBox.value = textBox.value.substring(0,maxLength);
        //}
    //}
    var leftNum = 0;
    var descText = textBox;
    if((/\r\n/g).test(descText.value))
    {
        leftNum = maxLength - descText.value.length;
    }
    else
    {
        var countLF= descText.value.match(/\n/g);
        leftNum = maxLength - descText.value.length-(countLF==null?0:countLF.length);
   }   
    if (leftNum < 0)
    {
        descText.value = descText.value.substr(0, maxLength);
        leftNum = 0;
    }
    var numSpan = document.getElementById("numSpan");
    numSpan.innerHTML = leftNum;

}