function $(id){return document.getElementById(id);}

var User_id;
var Product_id;
var vip;

window.onload=function(){
    User_id=$("hf_userid").value;
    Product_id=$("hf_productid").value;
    vip=$("hf_vip").value;
   
    setTimeout(function(){if(vip=="1"){
        $("price1").className="price";
        $("price2").className="priceb";
        $("price3").className="price";
        $("vip").style.display="";
    }},500);
    

    switch($("hf_shipment").value)
    {
        case "HK Post":
            {
                $("shipment").src="/images/ship_hkpost.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.hongkongpost.com/eng/index.htm');};
                break;
            }
        case "EMS":
            {
                $("shipment").src="/images/ship_ems.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.ems.com.cn/english-main.jsp');};
                break;
            }
       case "DHL":
            {
                $("shipment").src="/images/ship_dhl.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.dhl-usa.com/home/home.asp');};
                break;
            }
        case "UPS":
            {
                $("shipment").src="/images/ship_ups.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.ups.com/content/us/en/index.jsx');};
                break;
            }
        case "SF":
            {
                $("shipment").src="/images/ship_sf.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.sf-express.com/tabid/41/Default.aspx');};
                break;
            }
        default:
            {
                break;
            }
    }
}

var userAgent = navigator.userAgent.toLowerCase();
var isFireFox=/mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent);
if(isFireFox)
{
    window.showModelessDialog=function (url)
    {
        var windowName=(arguments[1]==null?"":arguments[1].toString());
        var feature=(arguments[2]==null?"":arguments[2].toString());
        var OpenedWindow=window.open(url,windowName,feature);
        window.addEventListener('click',function (){OpenedWindow.focus();},false);
        return OpenedWindow;
    }
}
else
{
    //子窗口中调用父窗口
    //IE中用window.parent.document
    //FF中用window.opener.document
    //下面的代码将 作用于IE '重载' window.showModelessDialog 方法 统一用 window.opener访问父窗口
    var originFn=window.showModelessDialog;
    window.showModelessDialog=function (url)
    {
        var OpenedWindow= originFn(url,arguments[1],arguments[2]);
        OpenedWindow.opener=window;
    }
}

function OpenWindow(url,w,h)
{
    var OpenedWindow=window.showModelessDialog(url,'',"width="+w+",height="+h);
    //showModelessDialog(url,window, "dialogWidth:"+width+"px;dialogHeight:"+height+"px;center:1;scroll:yes;help:0;status:0");	
}
function checkSend()
{
    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
    var to=$("tb_tomail").value;
    var name=$("tb_name").value;
    var from=$("tb_frommail").value;
    var subject=$("tb_subject").value;
    var message=$("tb_message").value;
    if(!sReg.test(to))
    {
        alert("Email format error.");
        $("tb_tomail").focus();
        return false;
    }
    else if(name=="")
    {
        alert("Your Name disallow null.");
        $("tb_name").focus();
        return false;
    }
    else if(!sReg.test(from))
    {
        alert("Your Email format error.");
        $("tb_frommail").focus();
        return false;
    }
    else if(subject=="")
    {
        alert("subject disallow null.");
        return false;
    }
    else
    {
        return true;
    }
}

function CheckEmail()
{

    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
    if($("TB_name").value=="")
    {
        alert("Please enter your name.")
        $("TB_name").focus();
        return false;
    }
    else if($("TB_content").value=="")
    {
        alert("Please enter your comments.");
        $("TB_content").focus();
        return false;
    }
    else
    {
        if(User_id=="")
        {
            if($("TB_mail").value=="")
            {
                alert("Please enter your Email.");
                $("TB_mail").focus();
                return false;
            }
            else if(!sReg.test($("TB_mail").value))
            {
                alert("your Email is wrong.");
                $("TB_mail").focus();
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return true;
        }
    }
}

function GiveEmail()
{
    var em=$("tb_giveemail").value;
    var saleid=$("hf_saleid").value;
    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
    if(em=="")
    {
        alert("Please enter your E-mail.");
        $("tb_giveemail").focus();
    }
    else if(!sReg.test(em))
    {
        alert("your E-mail is wrong.");
        $("tb_giveemail").focus();
    }
    else
    {
        if(AjaxProShowProduct.GiveEmail(Product_id,em,saleid,User_id).value=="1")
        {
            alert("That's ok, we'll inform you when we receive the stock.thinks!");
            $("tb_giveemail").value="";
            $("Top1_TB_keyword").focus();
        }
        else
        {
            alert("Error!!!");
        }
    }
}

function closew()
{
    $("TB_qty").disabled="";
    $("DDL_shipment").disabled="";
    $("DDL_country").disabled="";
    $("PutProduct").disabled="";
    $("waiting").style.display="none";
    $("waitings").style.display="none";
    
    if($("tb_giveemail").display=="block")
        $("tb_giveemail").focus();
}

function AddToCart()
{
    var w=getPageWidth()/2-130;
    $("waiting").style.left=w+"px";
    $("waitings").style.left=w+10+"px";
    
    $("TB_qty").disabled="disabled";
    $("DDL_shipment").disabled="disabled";
    $("DDL_country").disabled="disabled";
    $("PutProduct").disabled="disabled";
    $("waiting").style.display="block";
    $("waitings").style.display="block";
    window.setTimeout("CallPutProduct()",0);
}

function CallPutProduct()
{
    var shipment=$("DDL_shipment").value;
    var district_id=district_id=$("DDL_country").value;
    var number=$("TB_qty").value;
    var ok = AjaxProShowProduct.CallAddProductIntoCart(Product_id,shipment,district_id,number).value;
    switch(ok)
    {
        case "0":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Sorry! you must login firstly!</div><div style=margin-top:25px;><a style="font-size:14px; color:Blue; text-decoration:underline; cursor:pointer;" onclick="javascript:closew();" target="_blank" href="/login.aspx">Enter into login page</a> OR <a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                break;
            }
        case "1":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Catch Error!</div><div style=margin-top:25px;><a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                break;
            }
        case "2":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Request Error!</div><div style=margin-top:25px;><a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                break;
            }
        case "3":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Items are shortage here!<br><br>You may give your E-mail to us, we will inform you when this item receive the stock.</div><div style="margin-top:25px;"><a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                $("tb_giveemail").style.borderColor="#cc0000";
                break;
            }
        case "4":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:Green;"><b>√Items have been added to cart successfully!</b></div><div style="margin-top:25px;"><a href="/user/cart.aspx" style="width:30px;color:Blue;text-decoration:underline;" target="_blank">Go to shopping cart</a> OR <a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                break;
            }
        case "5":
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Sorry,the item is no stock now!</div><div style=margin-top:25px;><a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                $("tb_giveemail").style.borderColor="#cc0000";
                break;
            }
        default :
            {
                $("waiting").innerHTML='<div style="margin-top:55px; font-size:14px; font-weight:bold; color:#c00000;"><b>×</b>Items have been not added to cart;<br>plz try it again!!</div><div style=margin-top:25px;><a style="text-decoration:underline; color:Blue; cursor:pointer;" onclick="javascript:closew();">Close</a></div>';
                break;
            }    
    }
}

function HideWindow()
{
    window.setTimeout('$("PutProductMsg").innerHTML=""',5000);
}   

function ShowWindow(msg)
{
    str="<div id='window_login' style='margin-left:-165px;margin-top:40px; font-size:12px; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=95); background-color:#CFE6FF;width:200px;height:40px;border:solid 1px #797979;'><font color='#006699'><div style='text-align:center;'>"+msg+"</font><p><a href='/login.aspx' style='width:30px;color:#ff0000;text-decoration:underline;' target='_self'>Onclick into Login Page</a>&nbsp;Or&nbsp;<span style='cursor:pointer;color:#ff0000;' onclick='WindowClose()'>Close</a></p></div></div>";
    return str;
}

function WindowClose()
{
    $("PutProductMsg").innerHTML="";
}


function GetShipment()
{
    var ship=$("DDL_shipment").value;
    var qty=$("TB_qty").value;
    var country=$("DDL_country").value;
    var productid=Product_id;
    var str=AjaxProShowProduct.CountFreight(productid,qty,ship,country).value;
    var ss=str.split(',');
    $("TB_qty").value=ss[0];
    $("LB_freightage").innerHTML=ss[1];
    $("img_country").src="/images/country/"+ss[2];
    $("hf_country_id").value=ss[3];
    
    var i=parseInt(ss[0]);
    if(vip=="1")
    {
        if(i<10)
        {
            $("price1").className="price";
            $("price2").className="priceb";
            $("price3").className="price";
            $("vip").style.display="";
        }
        else if(i<100)
        {
            $("price1").className="price";
            $("price2").className="priceb";
            $("price3").className="price";
            $("vip").style.display="none";
        }
        else
        {
            $("price1").className="price";
            $("price2").className="price";
            $("price3").className="priceb";
            $("vip").style.display="none";
        }
    }
    else
    {
        if(i<10)
        {
            $("price1").className="priceb";
            $("price2").className="price";
            $("price3").className="price";
        }
        else if(i<100)
        {
            $("price1").className="price";
            $("price2").className="priceb";
            $("price3").className="price";
        }
        else
        {
            $("price1").className="price";
            $("price2").className="price";
            $("price3").className="priceb";
        }
    }
    
    var regex= /^[1-9][0-9]{0,10}$/;
    if(regex.test(qty))
    {
        if(ss[0]=="0")
        {
            alert("Items are sold out ! Quantity will be updated when they are in stock again!");
        }
        else if(qty!=ss[0])
        {
            if(parseInt(ss[0])==parseInt($("hf_purchaseless").value))
            {
                alert("The minimum purchase quantity is "+ss[0]+"pcs.");
            }
            else
            {
                alert("Sale quantity : "+ss[0]+" pcs");
            }
        }
    }
}
function CallGetList()
{
    $("LB_freightage").innerHTML="waiting...";
    setTimeout("GetList()",0);
}
function GetList()
{
    var ship=$("DDL_shipment").value;
    var country_id=$("hf_country_id").value;
    var strs=AjaxProShowProduct.GetOpotionList(ship,country_id).value.split('|');
    var Arr=strs[0].split(';');
    var a=$("DDL_country");
    a.length=0;
    for(var i = 0;i < Arr.length;i++)
    { 
        var option = document.createElement("option");   
        var ss=Arr[i].split(',');
        var option=new Option(ss[0],ss[1]);
        a.options.add(option);
    }

    if(strs[1]!="0")
    {
        $("DDL_country").value=strs[1];
    }
//    if(country_id!=strs[2])
//    {
//        alert("it can't delivery the goods to "+$("DDL_country").options[$("DDL_country").selectedIndex].text+" by "+ship+".your may select other shipment.");
//    }
    $("hf_country_id").value=strs[2];
    
    switch(ship)
    {
        case "HK Post":
            {
                $("shipment").src="/images/ship_hkpost.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.hongkongpost.com/eng/index.htm');};
                $("LB_showmsg").innerHTML="about 10-20 working days";
                break;
            }
        case "EMS":
            {
                $("shipment").src="/images/ship_ems.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.ems.com.cn/english-main.jsp');};
                $("LB_showmsg").innerHTML="about 15-30 working days";
                break;
            }
       case "DHL":
            {
                $("shipment").src="/images/ship_dhl.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.dhl-usa.com/home/home.asp');};
                $("LB_showmsg").innerHTML="about 5-7 working days";
                break;
            }
        case "UPS":
            {
                $("shipment").src="/images/ship_ups.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.ups.com/content/us/en/index.jsx');};
                $("LB_showmsg").innerHTML="about 5-7 working days";
                break;
            }
        case "SF":
            {
                $("shipment").src="/images/ship_sf.gif";
                $("shipmenturl").onclick=function(){window.open('http://www.sf-express.com/tabid/41/Default.aspx');};
                $("LB_showmsg").innerHTML="about 2-3 working days";
                break;
            }
        default:
            {
                break;
            }
    }
    CallGetShipment()
}
function CallGetShipment()
{
    $("LB_freightage").innerHTML="waiting...";
    setTimeout("GetShipment()",0);
}

function CallOver(obj)
{
    obj.style.backgroundColor="#ffffed";
    obj.style.borderColor="#999999";
}
function CallOut(obj)
{
    obj.style.backgroundColor="#ffffff";
    obj.style.borderColor="Silver";
}

function getPageWidth()
{
    var xScroll;
    if (window.innerHeight && window.scrollMaxY)
    {
        xScroll = document.body.scrollWidth;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
        xScroll = document.body.scrollWidth;
    }
    else
    {
        xScroll = document.body.offsetWidth;
    }
    
    var windowWidth;
    if (self.innerHeight)
    {
        windowWidth = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        windowWidth = document.documentElement.clientWidth;
    }
    else if (document.body)
    {
        windowWidth = document.body.clientWidth;
    } 
   
    
    if(xScroll < windowWidth)
    {
        pageWidth = windowWidth;
    }
    else
    {
        pageWidth = xScroll;
    }

    return pageWidth;
}


