// shoppingcart.js

var isIe=(window.ActiveXObject)?true:false;
var pageLinkUrl="";
var ajaxUrl="";

function showElementById(id)
{
   document.getElementById(id).style.display="";	   
}

function hideElementById(id)
{
   document.getElementById(id).style.display="none";
}

function showvolumediscount(pid)
{
	var theid="volumediscount";
	theid = theid + pid;
	document.getElementById(theid).style.display="";
	
	var xmlHttp=createXmlHttp();
    xmlHttp.onreadystatechange=function(){AfterProcessShowDiscount(xmlHttp, pid)};
	
	xmlHttp.open("Get","dis_show.php?productId=" + pid);
	xmlHttp.send(null);

}

function AfterProcessShowDiscount(xmlHttp, PID)
{
   if(xmlHttp.readyState==4)
   {
		if(xmlHttp.status==200)
		{
		    var backValue=xmlHttp.responseText;
			
			var start = backValue.indexOf("<body>") + 6;
			var end = backValue.indexOf("</body>");
			var textcontent = backValue.substring(start, end);
			// alert(textcontent);
		    if(!checkErrorFromBackValue(backValue))
		    {
		       return;
		    }
			
			document.getElementById("volumediscount"+PID).innerHTML=textcontent;
		}
	}
}


function hidevolumediscount(pid)
{
	var theid="volumediscount";
	theid = theid + pid;
	document.getElementById(theid).style.display="none";
}


function CookieEnable() 
{
	var result=false;
	if(navigator.cookiesEnabled)
	  return true;
	document.cookie = "testcookie=yes;";
	var cookieSet = document.cookie;
	if (cookieSet.indexOf("testcookie=yes") > -1) 
	  result=true;
	document.cookie = "";
	return result;
}

function shownews(info1)
{
	win_titleshow.style.display='block'
	win_titleshow.style.left=event.x+document.body.scrollLeft-win_titleshow.width+180;
	win_titleshow.style.top=event.y+document.body.scrollTop+20;
	title_info.innerHTML="<font color=005FA9>Description:</font>"+info1
}
function checkupdate(o)
{
	if(isNaN(document.forms[o.id].Quantity.value))
	{
		alert("Your input is not a number!");
		document.forms[o.id].Quantity.focus();
		return false;
	}
	return true;
}

function changeBar(type,UserID,ProductID,obj)
{
    var txtC=null;
    var change=0;
    if(type=='+')
    {
      txtC=obj.previousSibling.previousSibling.previousSibling.previousSibling;
      change=1;
    }
    if(type=='-')
    {
      txtC=obj.nextSibling.nextSibling;
      change=-1;
    }
    var num=parseInt(txtC.value);
    if(num+change<0)
    {
       // alert('您输入的数字已经超出的最小值');
       return;
    }
	//alert(txtC.id);
    txtC.value=num+change;
    
    changeProductQuantity(UserID, ProductID, txtC.value);
    //if(skuType=='gift')changeGiftCount(skuId,txtC);
    //if(skuType=='suit')changeSuitCount(skuId,txtC);   
}

function loadCart(userId)
{
    var xmlHttp=createXmlHttp();
    xmlHttp.onreadystatechange=function(){AfterProcess(xmlHttp)};
	
	xmlHttp.open("Get","cartajax.php?action=loadCart&UserID="+userId);
	xmlHttp.send(null);

}
function removeProduct(userId, productId)
{
    var xmlHttp=createXmlHttp();
    xmlHttp.onreadystatechange=function(){AfterProcess(xmlHttp)};
	
	xmlHttp.open("Get","cartajax.php?action=removeProduct&UserID="+userId+"&ProductID="+productId);
	xmlHttp.send(null);

}

//除去两边空格
function trim(string)
{
	return string.replace(/(^\s*)|(\s*$)/g, "");
}

function isdigit(s)
{
	var r,re;
	re = /\d*/i; //\d表示数字,*表示匹配多个数字
	r = s.match(re);
	return (r==s)?1:0;
}
//更改商品数量
function changeProductQuantity(userId, productId, number)
{
	//判断number是否数值类型
	//if(isNaN(trim(number)))
	var checkInt = isdigit(trim(number));
    if (checkInt == false)
	{
	    var xmlHttp=createXmlHttp();
		xmlHttp.onreadystatechange=function(){AfterProcess(xmlHttp,1,productId)};		
		xmlHttp.open("Get","cartajax.php?action=loadCart&UserID="+userId);
		xmlHttp.send(null);	   
	    return false;
	}
	//判断为number为空的情况，等待两秒要求用户输入
	if (!trim(number))
	{
	   window.setTimeout("WaitInput('"+userId+"','"+productId+"');", 2000);
	   return false;
	}
	//判断为0的情况,提示用户数值必须大于0
	if(parseInt(number)==0)
	{
		WaitInput(userId, productId);
		//removeProduct(userId, productId);
		return;
	}

	// obj.disabled=true;
    var xmlHttp=createXmlHttp();
    xmlHttp.onreadystatechange=function(){AfterProcess(xmlHttp,"",productId)};	
	xmlHttp.open("Get","cartajax.php?action=changeProductQuantity&UserID="+userId+"&ProductID="+productId+"&Quantity="+number);
	xmlHttp.send(null);	
}

//等待输入时的处理
function WaitInput(userId, productId)
{
	document.getElementById("err"+productId).style.display='';
	var xmlHttp=createXmlHttp();
	xmlHttp.onreadystatechange=function(){AfterProcess(xmlHttp,2,productId)};	
	xmlHttp.open("Get","cartajax.php?action=loadCart&UserID="+userId);
	xmlHttp.send(null);	 	
}

function AfterProcess(xmlHttp,err,productId)
{
   if(xmlHttp.readyState==4)
   {
		if(xmlHttp.status==200)
		{
		    var backValue=xmlHttp.responseText;
		    if(!checkErrorFromBackValue(backValue))
		    {
		       return;
		    }
			// alert(backValue);
		    // Change the  contents in the cart
			document.getElementById("cart-content").innerHTML=backValue;
			//如果输入类型错误，此处红色信息提示
			if (err)
			{
				if (err==2)
				  	document.getElementById("err"+productId).innerHTML='QTY must be larger than 0';
				else
				    document.getElementById("err"+productId).innerHTML='Incorrect number'; 					
				document.getElementById("err"+productId).style.display='';
				//document.getElementById('Quantity'+productId).focus();
			}
			//保证光标在输入框内
			if (productId)
			{
			    window.setTimeout("document.getElementById('Quantity"+productId+"').focus();", 50)  ;
				window.setTimeout("KeepFocusRight('"+productId+"');", 50)  ;
			}
		}
	}
}

//保证光标总在文字右方
function KeepFocusRight(productId)
{
   document.getElementById('Quantity'+productId).value = document.getElementById('Quantity'+productId).value;
}

//ajax通用方法
function createXmlHttp(){
  var ajaxObj=null;
  if(window.ActiveXObject)
  {
     ajaxObj=new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    if(window.XMLHttpRequest){
    ajaxObj=new XMLHttpRequest();
    }
  }
  return ajaxObj;
}
function setAjax_getRes(requst,resObjId)
{
   setAjax("GET",requst,null,false,null,resObjId,null);
}
function setAjax_runCode(requst,runCode)
{
   setAjax("GET",requst,null,false,null,null,runCode);
}
function setAjax_runCodeAndBtn(requst,curBtn,runCode)
{
   setAjax("GET",requst,null,false,curBtn,null,runCode);
}
function setAjax_getResAndRunCode(requst,resObjId,runCode)
{
   setAjax("GET",requst,null,false,null,resObjId,runCode);
}
function setAjax(postType,requst,postXml,isXml,curBtn,resObjId,runCode)
{   
    setAjaxBase(postType,requst,postXml,isXml,curBtn,resObjId,runCode,null);
}
function setAjaxBase(postType,requst,postXml,isXml,curBtn,resObjId,runCode,onOverRunCode)
{   
    
    if(curBtn!=null){curBtn.disabled=true;}
    var xmlHttp=createXmlHttp();
    xmlHttp.onreadystatechange=function(){backAjaxValue(xmlHttp,curBtn,resObjId,runCode,onOverRunCode)};
    if(postType=="GET"){
		xmlHttp.open(postType,pageLinkUrl+ajaxUrl+'?roid='+Math.random()+'&'+requst);
		xmlHttp.send(null);
    }else
    {
        xmlHttp.open(postType,pageLinkUrl+ajaxUrl+'?roid='+Math.random()+'&'+requst,true);
       if(!isXml){xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");}
       xmlHttp.send(postXml);
    }
}
function backAjaxValue(xmlHttp,curBtn,resObjId,runCode,onOverRunCode)
{
 
   if(xmlHttp.readyState==4)
   {
        clearWaitInfo();
		if(curBtn!=null){curBtn.disabled=false;}
		
		if(onOverRunCode!=null)eval(onOverRunCode);
		
		if(xmlHttp.status==200)
		{
		    var backValue=xmlHttp.responseText;
		    if(!checkErrorFromBackValue(backValue))
		    {
		       return;
		    }
		    if(resObjId!=null && g(resObjId)!=null)
		    {
		        g(resObjId).innerHTML=xmlHttp.responseText;
		    }
		    if(runCode!=null)
		    {
		    var backValue=xmlHttp.responseText;
		    eval(runCode);}
		    
		}
	}
}
function checkErrorFromBackValue(bakValue)
{
   if(bakValue!=null)
   {
      if(bakValue.indexOf('error_')==0)
      {
         if(bakValue.length>6)
         {bakValue=bakValue.substr(6);}
         else{bakValue='There is an error, please try again！';}
         alert(bakValue);
         return false;
      }
       return true;
   }
   return true;
}
function showProcess()
{
   	document.getElementById("main-content").innerHTML='<form action="checkout.php" method="post" id="gocheckout"><input type="hidden" name="Paymode" value="avangate" /><div id="show-process"><div id="img-process"><img src="images/loadling.gif" alt="Order Processing" /></div><div id="text-process"><p><strong>Thank you for ordering Sothink products.</strong></p><p><strong>You are being redirected to the Secure Payment Server, please wait!</strong></p></div></div></form>';
	document.getElementById("gocheckout").submit();
}
