﻿

var http;
function createHTTP()
{
    var browser=navigator.appName;
    if(browser=="Microsoft Internet Explorer")
    {
        var arrVersions=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
        for(var i=0;i<arrVersions.length;i++)
        {
            try
            {
                http=new ActiveXObject(arrVersions[i]);
            }
            catch(exception)
            {
            
            }
        }
    }
    else
    {
        http=new XMLHttpRequest();
    }
}
function startHTTP(cid)
{
	createHTTP();
    http.onreadystatechange=dome;
    http.open("GET","List.aspx?CID="+cid+"&t="+Math.random(),true);
    http.send(null);
}
function dome()
{
    if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
            b=b.substring(b.indexOf("(listBegin)")-4,b.indexOf("(listEnd)")+10);
            document.getElementById("CategoryInfo").innerHTML=b;
        }
    }
}
//增加记录到小购物车
function setCart(ProductID)
{
	$('#addtocart_' + ProductID).hide();
	$('#addtocartbeifen_' + ProductID).show();
	$('#btnaddtocart').attr("disabled",true); 
    createHTTP();
	http.onreadystatechange=addSmallCart;
    http.open("GET","ShoppingCart.aspx?ProductID="+ProductID+"&t="+Math.random(),true);
    http.send(null);
	var items = 0;
	$('[id^=Qty]').each(function(){
		items += Number($(this).val());
	});
	items += 1;
	document.getElementById("CTIValue").innerHTML=items.toString();	
}
//删除小购物车的记录
function remove(ProductID)
{
	createHTTP();
	http.onreadystatechange=deleteSmallCart;
    http.open("GET","ShoppingCart.aspx?ProID="+ProductID+"&Type=Remove&t="+Math.random(),true);
    http.send(null);
}
//改变小购物车的数量
function changeCart(ProductID,RecordID,quantity)
{
	createHTTP();
	http.onreadystatechange=changeSmallCart;
    http.open("GET","ShoppingCart.aspx?RecordID="+RecordID+"&ProID="+ProductID+"&Type=Change&quantity="+quantity+"&t="+Math.random(),true);
    http.send(null);
	var items = 0;
	$('[id^=Qty]').each(function(){
		items += Number($(this).val());
	});
	document.getElementById("CTIValue").innerHTML=items.toString();
}
function changeSmallCart()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;	
			if(b=="success")
			{
				//计算总价格
				var Price=0.0;
				var CartDIV=document.getElementById("CartItems").getElementsByTagName("div");
				var ValueDIV;
				for(var i=0;i<CartDIV.length;i++)
				{
					if(CartDIV[i].className=="CPValue")	
					{
						ValueDIV=CartDIV[i].childNodes;
						ValueDIV[2].innerHTML=(parseFloat(parseFloat(ValueDIV[0].innerHTML)*parseFloat(ValueDIV[1].getElementsByTagName("input")[0].value))).toFixed(2);
						Price+=parseFloat(ValueDIV[2].innerHTML);
					}
				}
				document.getElementById("CTIPriceValue").innerHTML=Price.toFixed(2);
			}
			else
			{
				alert(b);
			}
		}
	}
}
function deleteSmallCart()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
			if(b!="0")
			{
				var deleteDIV=document.getElementById("Card"+b);
				if(deleteDIV!=null)
				{
					document.getElementById("CTIPriceValue").innerHTML=(parseFloat(document.getElementById("CTIPriceValue").innerHTML)-parseFloat(document.getElementById("Price"+b).innerHTML)).toFixed(2);
					//document.getElementById("CTIValue").innerHTML=parseInt(document.getElementById("CTIValue").innerHTML)-1;
					document.getElementById("CartItems").removeChild(deleteDIV);
					
					var items = 0;
					$('[id^=Qty]').each(function(){
						items += Number($(this).val());
					});;
					document.getElementById("CTIValue").innerHTML=items.toString();
				}
			}
		}
	}
}
//绑定小购物车
function bind()
{
	createHTTP();
	http.onreadystatechange=bindSmallCart;
    http.open("GET","ShoppingCart.aspx?Type=Bind&t="+Math.random(),true);
    http.send(null);
}

function bindSmallCart()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
			var cartItem="";
			var Price=0.0;
	 		if(b!="ProductInfo=]")
			{
				eval(b);
				for(var i=0;i<ProductInfo.length;i++)
				{
					Price+=parseFloat(ProductInfo[i].Quantity)*parseFloat(ProductInfo[i].MarketPrice);
					cartItem+="<div id=\"Card"+ProductInfo[i].ProductID+"\" class=\"CartItem\">"+
						"<div class=\"CPName\">"+
							"<span class=\"CPNLeft\">["+ProductInfo[i].CategoryName+"]</span>"+
							"<span class=\"CPNRight\">"+
								ProductInfo[i].Name+
							"</span>"+
						"</div>"+
						"<div class=\"CPItems\">"+
							"<div class=\"CPUnitName\">Unite Price:</div>"+
							"<div class=\"CPQtyName\">Qty:</div>"+
							"<div class=\"CPPriceName\">Price:</div>"+
							"<div class=\"CPIRemove\"><a href=\"javascript:void(0);\" onclick=\"remove("+ProductInfo[i].ProductID+");\">Remove</a></div>"+
						"</div>"+
						"<div class=\"CPValue\">"+
							"<div id=\"Unit"+ProductInfo[i].ProductID+"\" class=\"CPUnitValue\">"+ProductInfo[i].MarketPrice+"</div>"+
							"<div class=\"CPQtyValue\"><input id=\"Qty"+ProductInfo[i].ProductID+"\" type=\"text\" onkeyup=\"changeSmallShoppingCart("+ProductInfo[i].ProductID+","+ProductInfo[i].RecordID+",this);\" value=\""+ProductInfo[i].Quantity+"\" style=\"width:28px; text-align:center; font-weight:bold;\"/></div>"+
							"<div id=\"Price"+ProductInfo[i].ProductID+"\" class=\"CPPriceValue\">"+(parseFloat(ProductInfo[i].Quantity)*parseFloat(ProductInfo[i].MarketPrice)).toFixed(2)+"</div>"+
						"<div style=\"clear:both\"></div></div>"+
					"</div>";
				}
				document.getElementById("CartItems").innerHTML=cartItem;
				document.getElementById("CTIPriceValue").innerHTML=Price.toFixed(2).toString();
				var items = 0;
				$('[id^=Qty]').each(function(){
					items += Number($(this).val());
				});
				//document.getElementById("CTIValue").innerHTML=ProductInfo.length.toString();
				document.getElementById("CTIValue").innerHTML=items.toString();
				getText("smallCart");
			}
			else
			{
				document.getElementById("CartItems").innerHTML="";
				document.getElementById("CTIPriceValue").innerHTML="0.00";
				document.getElementById("CTIValue").innerHTML="0";
			}
		}
	}
}
function addSmallCart()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
			if(b.substring(0,5)=="false")
			{
				alert(b.substring(5,b.length));	
			}
			else if(b.substring(0, 7)=="success")
			{
				eval(b.substring(7,b.length));
				if(document.getElementById("Card"+ProductInfo[0].ProductID))
				{
				    document.getElementById("Unit"+ProductInfo[0].ProductID).innerHTML=	ProductInfo[0].MarketPrice;
					document.getElementById("Qty"+ProductInfo[0].ProductID).value=ProductInfo[0].Quantity;
					document.getElementById("Price"+ProductInfo[0].ProductID).innerHTML=(parseInt(document.getElementById("Qty"+ProductInfo[0].ProductID).value)*parseFloat(ProductInfo[0].MarketPrice)).toFixed(2);
					document.getElementById("CTIPriceValue").innerHTML=(parseFloat(document.getElementById("CTIPriceValue").innerHTML)+parseFloat(ProductInfo[0].MarketPrice)).toFixed(2);
				}
				else
				{
					var cartItem="<div id=\"Card"+ProductInfo[0].ProductID+"\" class=\"CartItem\">"+
						"<div class=\"CPName\">"+
							"<span class=\"CPNLeft\">["+ProductInfo[0].CategoryName+"]</span>"+
							"<span class=\"CPNRight\">"+
								ProductInfo[0].ProductName+
							"</span>"+
						"</div>"+
						"<div class=\"CPItems\">"+
							"<div class=\"CPUnitName\">Unite Price:</div>"+
							"<div class=\"CPQtyName\">Qty:</div>"+
							"<div class=\"CPPriceName\">Price:</div>"+
							"<div class=\"CPIRemove\"><a href=\"javascript:void(0);\" onclick=\"remove("+ProductInfo[0].ProductID+")\">Remove</a></div>"+
						"</div>"+
						"<div class=\"CPValue\">"+
							"<div id=\"Unit"+ProductInfo[0].ProductID+"\" class=\"CPUnitValue\">"+ProductInfo[0].MarketPrice+"</div>"+
							"<div class=\"CPQtyValue\"><input id=\"Qty"+ProductInfo[0].ProductID+"\" onkeyup=\"changeSmallShoppingCart("+ProductInfo[0].ProductID+","+ProductInfo[0].RecordID+",this);\" type=\"text\" value=\""+ProductInfo[0].Quantity+"\" style=\"width:28px; text-align:center; font-weight:bold;\"/></div>"+
							"<div id=\"Price"+ProductInfo[0].ProductID+"\" class=\"CPPriceValue\">"+(parseFloat(ProductInfo[0].Quantity)*parseFloat(ProductInfo[0].MarketPrice)).toFixed(2)+"</div>"+
						"<div style=\"clear:both\"></div></div>"+
					"</div>";
					//alert(document.getElementById("CartItems").innerHTML + cartItem);
					$('#CartItems').append(cartItem);
					//document.getElementById("CartItems").innerHTML+=cartItem;
					document.getElementById("CTIPriceValue").innerHTML=(parseFloat(document.getElementById("CTIPriceValue").innerHTML)+(parseFloat(ProductInfo[0].Quantity)*parseFloat(ProductInfo[0].MarketPrice))).toFixed(2);
					//document.getElementById("CTIValue").innerHTML=parseInt(document.getElementById("CTIValue").innerHTML)+1;
					getText("smallCart");
				}				
				$('#addtocartbeifen_' + ProductInfo[0].ProductID).hide();
				$('#addtocart_' + ProductInfo[0].ProductID).show();
				$('#btnaddtocart').attr("disabled",false); 
			}
			else
			{
				alert(b);	
			}
          	//Eval(b);

        }
    }
}
function changeSmallShoppingCart(ProductID,RecordID,obj)
{
	 var re = /^[1-9]+[0-9]*]*$/; ///^[0-9]+.?[0-9]*$/;   //判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/   
   if (re.test(obj.value))
	 {
		 changeCart(ProductID,RecordID,obj.value);
	 }
	 else
	 {
		 alert("not a integer number!");	 
	 }
}
//大购物车页面
function changeShoppingCart(obj)
{
	 var re = /^[1-9]+[0-9]*]*$/; ///^[0-9]+.?[0-9]*$/;   //判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/   
     if (re.test(obj.value))
	 {
		 var q=obj.parentNode.parentNode.parentNode.parentNode;
		 var p=document.getElementById("dlShoppingCart").getElementsByTagName("table");
		 var index=-1;
		 for(var i=0;i<p.length;i++)
		 {
			if(p[i].getAttribute("name")=="ShoppingCartItem")
			{
				index++;
				if(p[i]==q)
				{
					var itemPrice=q.getElementsByTagName("span");
					var itemQuantity=q.getElementsByTagName("input");
					var itemAllPrice=q.getElementsByTagName("div");
					itemAllPrice[0].innerHTML=(parseFloat(itemPrice[0].innerHTML)*parseFloat(itemQuantity[0].value)).toFixed(2).toString();
					break;
				}
			}
		 }
		 changeBigCart(index,obj.value);
	 }
	 else
	 {
		 alert("not a integer number!");	 
	 }
}
function deleteShoppingCart(pid,items,obj)
{
	 var q=obj.parentNode.parentNode.parentNode.parentNode;
	 var p=document.getElementById("dlShoppingCart").getElementsByTagName("table");
	 var index=-1;
	 for(var i=0;i<p.length;i++)
	 {
		index++;
		if(p[i].getAttribute("name")=="ShoppingCartItem")
		{
			if(p[i]==q)
			{
				break;	
			}
		}
	 }
	 deleteBigCart(pid,items,index)
	
}
function clearBigCart()
{
	createHTTP();
	http.onreadystatechange=clearBig;
    http.open("GET","ShoppingCart.aspx?Type=EmptyBig&t="+Math.random(),true);
    http.send(null);
}
function clearBig()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			 var p=document.getElementById("DIVShoppingCart");
			 p.innerHTML="";
			document.getElementById("PTotal").innerHTML="0.00";
			document.getElementById("PWeight").innerHTML=0;
			document.getElementById("PQuantity").innerHTML=0;
			if(window.opener!=null&&window.opener.location.toString().indexOf("List.aspx")>0)
			{
				window.opener.bind();
			}
		}
	}
}
function deleteBigCart(pid,items,index)
{
	createHTTP();
	http.onreadystatechange=deleteBig;
    http.open("GET","ShoppingCart.aspx?Type=DeleteBig&ProID="+pid+"&items="+items+"&index="+index+"&t="+Math.random(),true);
    http.send(null);
}
function deleteBig()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
			eval(b);
			var p=document.getElementById("dlShoppingCart").getElementsByTagName("table");
			var index=ProductInfo.Index; 
			p[index].parentNode.removeChild(p[index]);
			p[index-1].parentNode.removeChild(p[index-1]);
			//document.getElementById("dlShoppingCart").removeChild(p[index]);
			//document.getElementById("dlShoppingCart").removeChild(p[index-1]);
			document.getElementById("PTotal").innerHTML=ProductInfo.Total;
			document.getElementById("PWeight").innerHTML=ProductInfo.Weight;
			document.getElementById("PQuantity").innerHTML=ProductInfo.Quantity;
			if(window.opener!=null&&window.opener.location.toString().indexOf("List.aspx")>0)
			{
				window.opener.bind();
			}
		}
	}
}
function changeBigCart(index,value)
{
	createHTTP();
	http.onreadystatechange=changeBig;
    http.open("GET","ShoppingCart.aspx?Type=ChangeBig&Itmes="+index+"&Value="+value+"&t="+Math.random(),true);
    http.send(null);
}
function changeBig()
{
	if(http.readyState==4)
    {
        if(http.status==200)
        {
			var b=http.responseText;
			eval(b);
			document.getElementById("PTotal").innerHTML=ProductInfo.Total;
			document.getElementById("PWeight").innerHTML=ProductInfo.Weight;
			document.getElementById("PQuantity").innerHTML=ProductInfo.Quantity;
			if(window.opener!=null&&window.opener.location.toString().indexOf("List.aspx")>0)
			{
				window.opener.bind();
			}
		}
	}
}
//获得不同语言的文本
function getText(name)
{
	createHTTP();
	http.onreadystatechange=function()
	{
		if(http.readyState==4)
		{
			if(http.status==200)
			{
				var b=http.responseText;
				eval(b);
				//removeNull(document.getElementById("CartItems"));
				var div=document.getElementById("CartItems").getElementsByTagName("div");
				for(var i=0;i<div.length;i++)
				{
					switch(div[i].className)
					{
						case "CPUnitName":div[i].innerHTML=Text.UnitePrice; break;
						case "CPQtyName":div[i].innerHTML=Text.Qty; break;
						case "CPPriceName":div[i].innerHTML=Text.Price; break;
						case "CPIRemove":div[i].childNodes[0].innerHTML=Text.Remove; break;
					}
				}
			}
		}
	}
    http.open("GET","ShoppingCart.aspx?Type=GetText&name="+name+"&t="+Math.random(),true);
    http.send(null);
}
//Checkout.aspx
function checkTime(currentTime)
{
	createHTTP();
	http.onreadystatechange=function()
	{
		if(http.readyState==4)
    	{
        	if(http.status==200)
        	{
				var b=http.responseText;
				if(b!="false")
				{
					alert(b);
				}
			}
		}
	}
    http.open("GET","Checkout.aspx?Type=time&CurrentTime="+currentTime+"&t="+Math.random(),true);
    http.send(null);
}
//头部的Header.ascx
function changeCategoryColor(CategoryID)
{
	var isIE=navigator.appName.indexOf("Internet Explorer")>0?true:false;
	removeNull(document.getElementById("indexItems"));
	var div=document.getElementById("indexItems").childNodes;
	var currentDIV=document.getElementById("Index"+CategoryID);
	var index=0;
	var marginLeft=0;
	for(var i=0;i<div.length;i++)
	{
		removeNull(div[i]);
		if(div[i]==currentDIV)
			index=i;
		div[i].childNodes[0].style.backgroundImage="url(Template/elders_EN/images/Menu_bg.gif)";
	}
	removeNull(document.getElementById("showImg"));
	document.getElementById("showImg").childNodes[0].style.display="none";
//	currentDIV.childNodes[0].style.backgroundColor="red";
	currentDIV.childNodes[0].style.backgroundImage="url(Template/elders_EN/images/Menu_bg_A.gif)";
	if(currentDIV.name!="")
	{
		document.getElementById("showImg").childNodes[0].src="Upload/picture/"+currentDIV.name;
		document.getElementById("showImg").childNodes[0].style.width=getStyleValue(1,"#indexItems .subIndexItems","width");
		document.getElementById("showImg").childNodes[0].style.display="block";
		if(isIE)
		{
			marginLeft=(index*parseInt(getStyleValue(1,"#indexItems .subIndexItems","width")))==0?-50:(index*parseInt(getStyleValue(1,"#indexItems .subIndexItems","width")));	
		}
		else
		{
			marginLeft=(index*parseInt(getStyleValue(1,"#indexItems .subIndexItems","width")));
		}
		document.getElementById("showImg").childNodes[0].style.marginLeft=marginLeft;
	}
	getCategoryTwo(CategoryID);
}

function getCategoryTwo(CategoryID)
{
	createHTTP(CategoryID);
	http.onreadystatechange=function()
	{
		if(http.readyState==4)
    	{
        	if(http.status==200)
        	{
				var b=http.responseText;
				if(b!="Category=]")
				{
					var buttomItem="";
					eval(b);
					for(var i=0;i<Category.length;i++)
					{
						buttomItem+="<a id=\"Sub"+Category[i].CategoryID+"\" name=\""+Category[i].GrayPic+"\" href=\""+(Category[i].link==""?"List.aspx?CID="+Category[i].CategoryID+"&FID="+Category[i].ParentID+"&SID="+Category[i].CategoryID:Category[i].link)+"\" target=\""+(Category[i].isOpen=="1"?"_self":"_blank")+"\" onmouseover=\"subChangeCategoryColor(this);\">"+
										"<div class=\"subButtomItems\">"+"&nbsp;&nbsp;&nbsp;"+
											Category[i].Name+
										"</div>"+
									"</a>"
					}
					document.getElementById("buttomItems").innerHTML=buttomItem;
					document.getElementById("buttomItems").style.display="block";
				}
				else
				{
					document.getElementById("buttomItems").style.display="none";
					document.getElementById("buttomItems").innerHTML="";
				}
			}
		}
	}
	var isIE=navigator.appName.indexOf("Internet Explorer")>0?true:false;
	if(isIE)
    http.open("GET","List.aspx?CateID="+CategoryID+"&t="+Math.random(),false);
  else
  	http.open("GET","List.aspx?CateID="+CategoryID+"&t="+Math.random(),true);
    http.send(null);
}
function subChangeCategoryColor(obj)
{
	removeNull(document.getElementById("buttomItems"));
	var div=document.getElementById("buttomItems").childNodes;
	for(var i=0;i<div.length;i++)
	{
		if(div[i]==obj)
		{
//			div[i].childNodes[0].style.backgroundColor="red";
			div[i].childNodes[0].style.backgroundImage="url(Template/elders_EN/images/Menu_bg_A.gif)";
			document.getElementById("showImg").childNodes[0].style.display="none";
			if(div[i].name!="")
			{
				document.getElementById("showImg").childNodes[0].src="Upload/picture/" + div[i].name;
				//document.getElementById("showImg").childNodes[0].src="http://www.elders.com.cn/en_newstyle/Upload/picture/gray_2010-05-23-07-45-10.jpg";
				//alert("/" + folder + "Upload/picture/" + div[i].name);
				document.getElementById("showImg").childNodes[0].style.width=getStyleValue(1,"#header #headerNavigator #buttomItems .subButtomItems","width")//div[i].childNodes[0].style.width;
				document.getElementById("showImg").childNodes[0].style.marginLeft=i*(parseInt(getStyleValue(1,"#header #headerNavigator #buttomItems .subButtomItems","width")) + 2);//i*parseInt(div[i].childNodes[0].style.width);
				document.getElementById("showImg").childNodes[0].style.display="block";
			}
		}
		else
		{
			div[i].childNodes[0].style.backgroundImage="url(Template/elders_EN/images/Menu_bg.gif)";
		}
	}
}

//删除所有空白节点
function removeAllNull(n)
{
	if(n.nodeType==1)
	{
		removeNull(n.parentNode);
	}
	var child = n.childNodes; 
	for(var i = 0; i < child.length; i++)
	{
		removeAllNull(child[i]); 
	}
}

function removeNull(element)
{
	for(var i=0;i<element.childNodes.length;i++)
	{
		var node=element.childNodes[i];
		if(node.nodeType==3&&!/\S/.test(node.nodeValue))
		{
			node.parentNode.removeChild(node);
		}
	}
}
//获得样式里的属性值
function getStyleValue(obj,labname,param)
{
	var tar;
	var rss;
	var style;
	var value;

	tar = tar = document.styleSheets[obj];;
	rss = tar.cssRules?tar.cssRules:tar.rules;
	for(i=0;rss.length;i++)
	{
		style = rss[i];
		try
		{
			if(style.selectorText.toLowerCase() == labname.toLowerCase())
			{
				value = style.style[param];
				break;
			}
		}catch(exception)
		{
			//alert(rss.length);	
		}
	}
	return value;
}

$(document).ready(function()
{
	if($('#span-' + CID).attr('flag') == '1')
	{
		$('#span-' + CID).css('background-image','url(Images/light_' + $('#span-' + CID).attr('flag') + '.jpg)');
	}
	
	if($('#span-' + CID).attr('flag') == '4' || $('#span-' + CID).attr('flag') == '5')
	{
		$('#span-' + CID).css('color','#ff1200');
	}
	
	$('#index span[@flag]').each(function(i){
		var selfid = $(this).attr('id');
		var nextid = $(this).parent().next().attr('id');
		if($('#' + nextid + ':has(#span-' + CID + ')').length > 0 && $(this).attr('flag') == '1')
		{
			$(this).css('background-image','url(Images/light_' + $(this).attr('flag') + '.jpg)');
			if($(this).hasClass("noPic"))
			{
				$(this).css('background-image','url(Images/l_light_' + $(this).attr('flag') + '.jpg)');
			}
		}
		
	});
});

