// JavaScript Document
function $(id) 
{
	return document.getElementById(id);
}

function Trim(str)
{
	return str.replace(/(^\s*)|(\s*$)/g,"");
}  

function getMsg()
{
	try
	{
		divT = parseInt($("eMeng").style.top,10); //层top位置
		divL = parseInt($("eMeng").style.left,10); //层left位置
		divH = parseInt($("eMeng").offsetHeight,10);//层宽
		divW = parseInt($("eMeng").offsetWidth,10);//层高
		docW = document.documentElement.clientWidth;//窗口宽
		docH = document.documentElement.clientHeight;//窗口高
		docST=document.documentElement.scrollTop;
		docSL=document.documentElement.scrollLeft;
		$("eMeng").style.top = parseInt(docST,10) + docH + 10+"px";
		$("eMeng").style.left = parseInt(docSL,10) + docW - divW+"px";
		$("eMeng").style.visibility="visible";
		objTimer = setInterval("moveDiv()",10);
	}
	catch(e)
	{}
}

function resizeDiv()
{
	try
	{
		divH = parseInt($("eMeng").offsetHeight,10);
		divW = parseInt($("eMeng").offsetWidth,10);
		docW = document.documentElement.clientWidth;
		docH = document.documentElement.clientHeight;
		$("eMeng").style.top = docH - divH + parseInt(document.documentElement.scrollTop,10)+"px";
		$("eMeng").style.left = docW - divW + parseInt(document.documentElement.scrollLeft,10)+"px";
	}
	catch(e)
	{}
}
function moveDiv()
{
	try
	{
		if(parseInt($("eMeng").style.top,10) <= (docH - divH + parseInt(document.documentElement.scrollTop,10)))
		{
			window.clearInterval(objTimer)
			objTimer = setInterval("resizeDiv()",1)
		}
		divT = parseInt($("eMeng").style.top,10);
		$("eMeng").style.top = divT - 1+"px";
	}
	catch(e)
	{}
}
function closeDiv()
{
	$('eMeng').style.visibility='hidden';
	if(objTimer) window.clearInterval(objTimer)
}

function searchDeater()
{
	if(document.all("loc_com"))
	{
		if(document.all("loc_com").value!="")
		{
			document.form2.submit();
		}
		else
		{
			alert("Please input search keyword!");
		}
	}
}

function productSearch()
{
	if(document.all("search_words"))
	{
		if(document.all("search_words").value=="")
			alert("Please input search condition!");
		else
		{
			//alert("["+document.all("search_words").value+"]");
			document.forms[0].submit();
		}
	}
}

function ClearCache(url)
{
    if(url.indexOf("?")!=-1)
    {
       return url+"&ram="+String(Math.random());
    }
    else
    {
       return url+"?ram="+String(Math.random());
    }
}


function InitAjax()
{
     var ajax=false;
     try {
		 ajax = new ActiveXObject("Msxml2.XMLHTTP");
		 }
	 catch (e){
		 try {
			  ajax = new ActiveXObject("Microsoft.XMLHTTP");
			  }
		 catch (E) {
			  ajax = false;
              }
         }
     if (!ajax && typeof XMLHttpRequest!="undefined") {
		 ajax = new XMLHttpRequest();
		 }
	 return ajax;
}

function getNews(url,msg)
{
   var show = $(msg);
   var ajax = InitAjax();
   ajax.open("GET", ClearCache(url), true);
   ajax.onreadystatechange = function() {
        if (ajax.readyState == 4 && ajax.status == 200) {
			show.innerHTML = ajax.responseText;
			}
      }
   ajax.setRequestHeader("If-Modified-Since","0");
   ajax.send(null);
}

function DIVLogin(s){//弹出式登录对话框

      var msgw,msgh,bordercolor;
      msgw=350;//提示窗口的宽度
      titleheight=38; //提示窗口标题高度
      bordercolor="#336699";//提示窗口的边框颜色
      titlecolor="#336699";//提示窗口的标题颜色
      
      var sWidth,sHeight;
      sWidth=(document.body.offsetWidth > window.screen.width) ? document.body.offsetWidth : window.screen.width-20;
      sHeight=document.body.offsetHeight;
      var bgObj=document.createElement("div");
      bgObj.setAttribute('id','bgDiv');
      bgObj.style.position="absolute";
      bgObj.style.top="0";
      bgObj.style.background="#777";
      bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75);";
      bgObj.style.opacity="0.6";
      bgObj.style.left="0";
      bgObj.style.width=sWidth + "px";
      bgObj.style.height=sHeight + "px";
      bgObj.style.zIndex = "10000";
      document.body.appendChild(bgObj);
      
      var msgObj=document.createElement("div")
      msgObj.setAttribute("id","msgDiv");
      msgObj.setAttribute("align","center");
      msgObj.style.position = "absolute";
      msgObj.style.left = "50%";
      msgObj.style.top = "50%";
      msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
      msgObj.style.marginLeft = "-200px" ;
      msgObj.style.marginTop = -80+document.documentElement.scrollTop+"px";
      msgObj.style.width = msgw + "px";
      msgObj.style.height ="auto";
      msgObj.style.textAlign = "center";
      msgObj.style.lineHeight ="25px";
      msgObj.style.zIndex = "10001";
   
        var title=document.createElement("div");
        title.setAttribute("id","msgTitle");
        title.style.cursor="pointer";
		title.style.background="#eee";
        title.innerHTML='<div><strong>User Log In</strong></div>';
        document.body.appendChild(msgObj);
        $("msgDiv").appendChild(title);
        var txt=document.createElement("div");
        txt.style.background="#fff";
        txt.setAttribute("id","msgTxt");
        txt.innerHTML = '<br><div><p style="line-height:18px;">&nbsp;&nbsp;&nbsp;&nbsp;E-mail:&nbsp;<input id=\"logmail\" type=\"text\" style=\"width:140px;color:#333;\" /></p><p style="line-height:18px;">Password:&nbsp;<input id=\"logpwd\" type=\"password\" style=\"width:140px;color:#333;\" /></p></div>';
        txt.innerHTML+='<div style="clear:both;"><input id="OKBtn" type="button" value="LOGIN" style="border:1px #999 solid; width:60px;" onclick="userlogin()" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="CANCEL" style="border:1px #999 solid; width:60px;" id="CancelBtn" /></div><br>';
		if(s!=null)
		{
			txt.innerHTML+='<font color=red>'+s+'</font><br>';
		}
            $("msgDiv").appendChild(txt);
            $("OKBtn").onclick=function(){//定义关闭窗口函数
					var mail=Trim($("logmail").value);
					var pass=Trim($("logpwd").value);
					if(mail==""||pass=="")
					{
						alert("Please input your email and password!");
						if(mail=="")
						{
							$("logmail").focus();
						}
						else
						{
							$("logpwd").focus();
						}
					}
					else
					{
						jsLogin(mail,pass,txt,bgObj,title,msgObj);
					}
            }
            $("CancelBtn").onclick=function(){//定义关闭窗口函数
                   document.body.removeChild(bgObj);
                   $("msgDiv").removeChild(title);
                   document.body.removeChild(msgObj);
            }
}

function showLogForm()
{
	DIVLogin();
}

function jsLogin(logmail,logpwd,txt,bgObj,title,msgObj)
{
	txt.innerHTML="Log in now, please wait...";
	var url="/user/login.asp?logmail="+logmail+"&logpwd="+logpwd;
	var ajax = InitAjax();
	ajax.open("GET", ClearCache(url), true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) 
		{
			document.body.removeChild(bgObj);
			$("msgDiv").removeChild(title);
			document.body.removeChild(msgObj);
			if(ajax.responseText!="")
			{
				$("dvdlog").innerHTML=ajax.responseText;
			}
			else
			{
				DIVLogin("Email or password error!")
			}
		 }
	  }
	ajax.setRequestHeader("If-Modified-Since","0");
	ajax.send(null);
}

function userlogout()
{
	var url="/user/logout.asp";
	getNews(url,"dvdlog");
}

function addtoCart()
{
    var qtystr="";
    var pidstr="";
    var qty=document.getElementsByName("qty");
    var pid=document.getElementsByName("pid");
    for(var i=0;i<qty.length;i++)
    {
		if(Trim(qty[i].value)!=null && Trim(qty[i].value)!="")
		{
			qtystr+=Trim(qty[i].value)+",";
			pidstr+=Trim(pid[i].value)+",";
			qty[i].value="";
		}
    }
	if(qtystr!="")
	{
		var url="/Cart/addToCart.asp?qty="+qtystr+"&pid="+pidstr;
		addProuctTocart(url);
	}
	else
	{
		alert("Please input quantity!")
	}
}

function addProuctTocart(url)
{
	openMessage_box()
	var ajax = InitAjax();
	ajax.open("GET", ClearCache(url), true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) 
		{
			$("massage_box").innerHTML=ajax.responseText;
		 }
	  }
	ajax.setRequestHeader("If-Modified-Since","0");
	ajax.send(null);
}

function checkinput(v)
{
   if(v.value.length!=0)
   {
		var filter=/^\s*[0-9]{0,5}\s*$/;
		if (!filter.test(v.value)||v.value=="0"||v.value.charAt(0)=="0")
		{ 
		    alert("Input Error!");
			v.focus();
			v.select();
		}
	}
}

function setMessage_boxStyle()
{ 
      var msgObj= $("massage_box");
      msgObj.setAttribute("align","center");
      msgObj.style.position = "absolute";
      msgObj.style.left = "50%";
      msgObj.style.top = "50%";
      msgObj.style.marginLeft = "-110px" ;
      msgObj.style.marginTop = -25+document.documentElement.scrollTop+"px";
      msgObj.style.width = "220px";
      msgObj.style.height ="auto";
      msgObj.style.textAlign = "left";
      msgObj.style.zIndex = "10001";
      msgObj.style.fontSize="11px";
      msgObj.style.fontFamily="Verdana";
      msgObj.style.color="#000000";
	  msgObj.style.background="#eee";
}

function openMessage_box()
{
   var dgc = $("massage_box");
   var html="";
   html = "<table style=\"border:1px solid #7eacb1;background-color:#d4e3e5;width:100%; height:53px;\"><tr><td style=\"padding-left:10px;padding-right:10px;\"><img src=\"/webpic/loading.gif\" align=\"absmiddle\">&nbsp;&nbsp;";
   html +="Processing , please wait...";
   html +="</td></tr></table>";
   dgc.innerHTML=html;
   dgc.style.visibility="visible";
   setMessage_boxStyle();
}

function closeMessage_box()
{
   var dgc = $("massage_box");
   dgc.style.visibility="hidden";
}

function checkout()
{
	window.location="/Cart/viewcart.asp";
}

function continueshop()
{
	window.location="/product/producthome.asp";
}

function DeleteCart(id,pid)
{
  if(confirm('Sure Delete '+pid+'!'))
  {
    document.cartForm.cartid.value=id;
	document.cartForm.submitmode.value="d";
	window.document.cartForm.submit();
  }
  else
   return false;
}

function clearAll()
{
  if(confirm('Sure Clear All Products from Cart!'))
  {
	document.cartForm.submitmode.value="c";
	window.document.cartForm.submit();
  }
  else
   return false;
}

function submitOrder()
{
  if (document.cartForm.Shipaddress.value=="" )
     {
      alert("Shipping Address is required!");
	  return false;
	  }
  else if (document.cartForm.cityname.value=="" )
     {
      alert("City is required!");
	  return false;
	  }
  else if (document.cartForm.statename.value=="" )
     {
      alert("State is required!");
	  return false;
	  }
  else if (document.cartForm.zipcode.value=="" )
     {
      alert("Zip Code is required!");
	  return false;
	  }
  else if (document.cartForm.contactperson.value=="" )
     {
      alert("Contact Person is required!");
	  return false;
	  }
  else if (document.cartForm.contactTel.value=="" )
     {
      alert("Phone Number is required!");
	  return false;
	  }
  else if (document.cartForm.payment.value=="" )
     {
      alert("Payment Terms is required!");
	  return false;
	  }
  else if (document.cartForm.ETAdate.value=="" )
     {
      alert("Ship Via is required!");
	  return false;
	  }
  else if(document.cartForm.LWord.value.length>800)
  {
  	alert("Notes is too long!");
	return false;
  } 
  else
  {
     document.cartForm.action="submitOrder.asp";
     window.document.cartForm.submit();
  }
}
