﻿function AutoLogin()
{
    createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "/Ajax/common.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("action=logininfo");
	function handleStateChange()
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
			    
				var result = xmlHttp.responseText;
				if(result != "false")
				{
					$("#loginInfo").html(result);
				}
				$("#award").change();
			}
		}
	}
}

var shoppings_index;
var sizes_index;
var works_id;
function Check_Cart()
{
	if($("#List_Shopping")[0] == null)
	{
		alert("购物车是空的哦！");
		return false;
	}
	else
	{
		return null;
	}
}

function Sum_Price(shopping_index,size_index,work_id,obj,maxquantity)
{
	var quantity = parseInt(obj.value);
    shoppings_index = shopping_index;
	sizes_index = size_index;
    var reg = /^-?\d+$/;
	if(reg.test(quantity) == false || quantity <= 0)
	{
	    quantity = 1;
		obj.value = 1;	
	}
	var sumquantity = 0;
	var ctlName = "List_Shopping_ctl01_List_Size_ctl00_txt_Unit";
	ctlName = "List_Shopping_ctl" + (shopping_index < 10 ? "0" + shopping_index : shopping_index) + "_List_Size_ctl";
	var ctlIndex = 0;
	while(ctlIndex > -1 && ctlIndex < 20)
	{
		var ctlUnit = $("#" + ctlName + (ctlIndex < 10 ? "0" + ctlIndex : ctlIndex) + "_txt_Unit")[0];
		if(ctlUnit != null && ctlUnit != undefined)
		{
			var tb = $("#List_Shopping_ctl" + (shopping_index < 10 ? "0" + shopping_index : shopping_index) + "_List_Size")[0];
			if(tb.childNodes[ctlIndex].style.display != "none")
			{
			    sumquantity = sumquantity + parseInt(ctlUnit.value);
			}
		}
		else
		{
			break;
		}
		ctlIndex ++;
	}
	if(sumquantity > maxquantity)
	{
		alert("对不起，此作品的最大购买不得超过 " + maxquantity);
		obj.value = parseInt(obj.value) - (sumquantity - maxquantity);
	}
	quantity = parseInt(obj.value);
	works_id = work_id;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "Ajax_Sum_Price.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("size_index=" + size_index + "&id=" + works_id + "&quantity=" + quantity);
    function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				 var result = xmlHttp.responseText;
				 $("#price_" + works_id).html(result);
				 $("#loading").css("display","none");
				 Sum_money();
			}
		}
		else
		{
			$("#loading").css("display","block");	
		}
	}
}

function Sum_Price_(shopping_index,size_index,work_id,quantity)
{
    shoppings_index = shopping_index;
	sizes_index = size_index;
	works_id = work_id;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "Ajax_Sum_Price.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("size_index=" + size_index + "&id=" + works_id + "&quantity=" + quantity);
    function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				 var result = xmlHttp.responseText;
				 $("#price_" + works_id).html(result);
				 $("#loading").css("display","none");
				 Sum_money();
			}
		}
		else
		{
			$("#loading").css("display","block");	
		}
	}
}

function Get_UnitPrice(shopping_index,size_index,work_id,size)
{
    shoppings_index = shopping_index;
	sizes_index = size_index;
	works_id = work_id;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "Ajax_Unit_Price.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("size_index=" + size_index + "&id=" + works_id + "&newsize=" + size);
    function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				 var result = xmlHttp.responseText;
				 $("#List_Shopping_ctl0" + shoppings_index + "_List_Size_ctl0" + sizes_index +"_unitprice").html(result);
				 $("#loading").css("display","none");
				 Sum_Price(shoppings_index,sizes_index,works_id,$("#List_Shopping_ctl0" + shoppings_index +"_List_Size_ctl0" + sizes_index + "_txt_Unit"), 999999);
			}
		}
		else
		{
			$("#loading").css("display","block");	
		}
	}
}

function Sum_Unit()
{
    createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "/ajax/cart.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("action=sumunit");
	function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				 var result = xmlHttp.responseText;
				 $("#lb_sumUnit").html(result);
				 $("#loading").css("display","none");
			}
		}
		else
		{
			$("#loading").css("display","block");	
		}
	}
}

function Sum_money()
{
    createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "/ajax/cart.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("action=summoney");
	function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				 var result = xmlHttp.responseText;
				 $("#lb_sumMoney").html(result);
				 $("#loading").css("display","none");
				 $("#award")[0].value = result.replace("￥","");
				 $("#award").change();
			}
		}
		else
		{
			$("#loading").css("display","block");	
		}
	}
}



function Load_Size(Color_id,work_id)
{
    works_id = work_id;
    createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("post", "Ajax_Load_Size.aspx", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("id=" + Color_id);
	function handleStateChange() 
	{
		if(xmlHttp.readyState == 4) 
		{
			if(xmlHttp.status == 200) 
			{
				var drop_Size = $("#" + works_id.replace("Drop_Color","") + "Drop_Size")[0];
				drop_Size.options.length = 0;
				
				var result = xmlHttp.responseText.split("$");
				var index = 0;
				while(index < result.length - 1)
				{
				   var itemSize = new Option(result[index + 1],result[index]);
				   drop_Size.options.add(itemSize);
				   index += 2;
				}				
				$("loding").style.display = "none";
			}
		}
		else
		{
			$("#loading").style.display = "block";	
		}
	}
}



function add_size(shopping_Index,work_Id,maxQuantity)
{
   var tb = $("#List_Shopping_ctl0" + shopping_Index + "_List_Size")[0];
   var size_Index = tb.childNodes.length;
   if(size_Index > 9)
   {
	   return;
   }
   var html = tb.childNodes[0].childNodes[0].nodeType == 1 ? tb.childNodes[0].childNodes[0].innerHTML : tb.childNodes[0].childNodes[1].innerHTML;

   html = html.replace("List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl00_Select_Size","List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_Select_Size");
      
   html = html.replace("List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl00_unitprice","List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_unitprice");
   
   html = html.replace("List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl00_txt_Unit","List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_txt_Unit");
   html = html.replace("Get_UnitPrice(" + shopping_Index + ",0," + work_Id +",this.value)","Get_UnitPrice(" + shopping_Index + "," + size_Index + "," + work_Id + ",this.value)");
   html = html.replace("Sum_Price(" + shopping_Index + ",0," + work_Id +",this," + maxQuantity + ")","Sum_Price(" + shopping_Index + "," + size_Index + "," + work_Id + ",this," + maxQuantity + ")");
   html = html.replace("function_size(" + shopping_Index + ",0," + work_Id + "," + maxQuantity + ")","function_size(" + shopping_Index + "," + size_Index + "," + work_Id + "," + maxQuantity + ")");
   html = html.replace("添加尺寸","删除尺寸");
   var newSize = document.createElement("span");
   newSize.innerHTML = html;
   tb.appendChild(newSize);
   var select_size = $("#List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_Select_Size")[0];
   $("#List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_txt_Unit")[0].value = 1;
   $("#List_Shopping_ctl0" + shopping_Index + "_List_Size_ctl0" + size_Index + "_unitprice").html("￥0.00");
   select_size.options[0].selected = true;
   Get_UnitPrice(shopping_Index,size_Index,work_Id,select_size.value)
}

function remove_size(shopping_Index,size_Index,work_Id)
{
   if(confirm("您确认要删除改尺寸吗？") == false)
   {
      return;
   }
   var tb = $("#List_Shopping_ctl0" + shopping_Index + "_List_Size")[0];
   tb.childNodes[size_Index].style.display="none";
   Sum_Price_(shopping_Index,size_Index,work_Id,0);
}

function function_size(shopping_Index,size_Index,work_Id,maxQuantity)
{
    if(size_Index == 0)
    {
		var sumquantity = 0;
		var ctlName = "List_Shopping_ctl01_List_Size_ctl00_txt_Unit";
		ctlName = "List_Shopping_ctl" + (shopping_Index < 10 ? "0" + shopping_Index : shopping_Index) + "_List_Size_ctl";
		var ctlIndex = 0;
		while(ctlIndex > -1)
		{
			var ctlUnit = $("#" + ctlName + (ctlIndex < 10 ? "0" + ctlIndex : ctlIndex) + "_txt_Unit")[0];
			if(ctlUnit != null && ctlUnit != undefined)
			{
				var tb = $("#List_Shopping_ctl" + (shopping_Index < 10 ? "0" + shopping_Index : shopping_Index) + "_List_Size")[0];
				if(tb.childNodes[ctlIndex].style.display != "none")
				{
					sumquantity = sumquantity + parseInt(ctlUnit.value);
				}
			}
			else
			{
				break;
			}
			ctlIndex ++;
		}
		if(sumquantity == maxQuantity)
		{
			alert("对不起，此作品的最大购买不得超过 " + maxQuantity);
			return;
		}
        add_size(shopping_Index,work_Id,maxQuantity);
    }
    else
    {
        remove_size(shopping_Index,size_Index,work_Id);
    }
}

function orderinfo_doClear(i,refer,node)
{
    var check = orderinfo_CheckForm(i,refer,node);
	if(i==0 && check == false)
	{
		refer.value = "";
		ShowTitle(refer,'',node);
	}
	if(i==1 && check == false)
	{
		refer.value = "";
		ShowTitle(refer,"",node);
	}
	if(i==2 && check == false)
	{
		refer.value = "";
		ShowTitle(refer,"",node);
	}
	if(i==3 && check == false)
	{
		refer.value = "";
		ShowTitle(refer,"",node);
	}
	if(i==4 && check == false)
	{
		refer.value = "";
		ShowTitle(refer,"",node);
	}
}
	
function orderinfo_CheckForm(i,refer,node)
{ 
	if(i==0)
	{
		if(refer.value=="")
		{
			ShowError(refer,"请填写收货人！",node);
			return false;
		}		
		return true;
	}
	if(i==1)
	{
	   if(refer.value == "")
	   {
	        ShowError(refer,"请填写收获地址！",node);
		    return false;
	   }
	   return true;
	}
	if(i==2)
	{
		if(refer.value=="")
		{
		    ShowError(refer,"请填写邮政编码！",node);
			return false;
		}
		var reg = /^[1-9]\d{5}$/;
		if(reg.test(refer.value) == false)
		{
		    ShowError(refer,"邮编格式有误！",node);
		    return false;
		}
		return true;
	}
	if(i==3)
	{
		if(refer.value=="")
		{
			return true;
		}
		var reg=/^\d{10,13}$/;
		if(reg.test(refer.value) == false)
		{
		    ShowError(refer,"号码格式错误！",node); 
		    return false;
		}
		return true;
	}
	if(i==4)
	{
		if(refer.value=="")
		{
			return true;
		}
		var reg=/^\d{10,12}$/;
	    if(reg.test(refer.value) == false)
	    {
		    ShowError(refer,"号码格式有误！",node); 
		    return false;
	    }
		return true;

	}
	if(i==5)
	{
	    if($("#txt_Mobie")[0].value == "" && refer.value == "")
		{
		   ShowError(refer,"请至少填写移动电话或固定电话当中的一项！",node); 
		   return false;
		}
		return true;
	}
}

function validate_recieve()
{
	var check = true;
	if(orderinfo_CheckForm(0,$("#txt_Recieve")[0],'msg_Recieve') == false)
	{
		check = false;
	}
	if(orderinfo_CheckForm(1,$("#txt_Addr")[0],'msg_Addr') == false)
	{
		check = false;
	}
	if(orderinfo_CheckForm(2,$("#txt_PostCode")[0],'msg_PostCode') == false)
	{
		check = false;
	}
	if(orderinfo_CheckForm(3,$("#txt_Mobie")[0],'msg_Mobie') == false)
	{
		check = false;
	}
	if(orderinfo_CheckForm(4,$("#txt_Phone")[0],'msg_Phone') == false)
	{
		check = false;
	}
	if(orderinfo_CheckForm(5,$("#txt_Phone")[0],'msg_Phone') == false)
	{
		check = false;
	}
	return check;
}

function orderinfo_validate()
{
	return validate_recieve();
}

function switch_div(div_1,div_2)
{
   $("#" + div_1).css("display",$("#" + div_1).css("display") == 'none' ? 'block' : 'none');
   $("#" + div_2).css("display",$("#" + div_2).css("display") =='none' ? 'block' : 'none');
}

function checkCoupons(ref)
{
	var i = 1;
	while(true)
	{
		var radio = $("#List_Coupons_ctl0" + i + "_radio")[0];
		if(radio == null || radio == undefined)
		{
			break;
		}
		else if(radio != ref)
		{
		    radio.checked = false;
		}
		i ++;
	}
}

function orderpay_CheckForm(i,refer,node)
{ 
	if(i==0)
	{
		if(refer.value=="")
		{
			ShowError(refer,"请填写支付金额！",node);
			return false;
		}
		var reg = /^\d+(\.\d+)?$/;
		if(reg.test(refer.value) == false)
		{
			ShowError(refer,"金额应用数字表示！",node);
			return false;
		}
		if(refer.value * 1 > $("#valibel_money")[0].value * 1)
		{
			refer.value = $("#valibel_money")[0].value;
		}
		if(refer.value * 1 > $("#balance_pay")[0].value * 1)
		{
			refer.value = $("#balance_pay")[0].value;
		}
		return true;
	}
	if(i==1)
	{
		if(refer.value == "")
		{
		ShowError(refer,"请输入礼券号码！",node);
		return false;
		}
		return true;
	}
	if(i==2)
	{
		if(refer.value=="")
		{
			ShowError(refer,"请输入礼券密码！",node);
			return false;
		}
		return true;
	}
}


	