﻿function initxmlhttp() {
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefiend') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

// 收藏
function shoucang(pid) {
	xmlhttp = initxmlhttp();
	var url = 'isonline.aspx';
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var ss= xmlhttp.responseText;				
		if(ss!=""){
		favorite(pid);		
		}else{
		 alert('请登录');
		}
	 }		
	}
	xmlhttp.send(null);
}
//先判断 库存是否为空
function isonline(id) {
	xmlhttp = initxmlhttp();
	var url = 'kucun.aspx?pid=' +id;
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var ss= xmlhttp.responseText;		
			if(ss== 0){
			alert('该商品库存不足，请稍后再购！');
			}else
			{
			isonline2(id);
			}
		}		
	}
	xmlhttp.send(null);
}
// 添加到购物车
function isonline2(id) {
	xmlhttp = initxmlhttp();
	var url = 'orderconfirm.aspx?pid=' +id ;
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
	
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
		    var ss= xmlhttp.responseText;		
		    alert('已放入购物车');
		}		
		else
		{
		    alert('Ready State:' + xmlhttp.readyState + '\r\n Status:'+ xmlhttp.status);
        }
	}
	xmlhttp.send(null);
}

//先判断秒杀 库存是否为空，秒杀是否有效。
function SeckillIsOnline(id,SeckillID) {
	xmlhttp = initxmlhttp();
	var url = 'kucun.aspx?pid=' +id + '&SeckillID=' + SeckillID;
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		    var ss= xmlhttp.responseText;		
	        if(ss=="NoSeckill")
	        {
		        alert('该秒杀未到期或已过期！');
		    }
		    else
		    {
                if(ss=="OrderLimit")
                {
    		        alert('本商品购买量达到了秒杀上限，目前不能再订购！');
                }
                else
                {
                    if(ss== 0)
	    	        {
			            alert('该商品库存不足，请稍后再购！');
			        }
			        else
			        {
			            SeckillIsOnline2(id,SeckillID);
			        }
			    }
		    }
		}		
	}
	xmlhttp.send(null);
}
// 添加到购物车
function SeckillIsOnline2(id,SeckillID) {
	xmlhttp = initxmlhttp();
	var url = 'orderconfirm.aspx?pid=' +id +'&SeckillID=' + SeckillID;
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var ss= xmlhttp.responseText;		
		alert('已放入购物车');
		}		
	}
	xmlhttp.send(null);
}
// 添加到收藏
function favorite(id) {
	xmlhttp = initxmlhttp();
	var url = 'favorite.aspx?pid=' +id;
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var ss= xmlhttp.responseText;
		alert('已收藏');
		}		
	}
	xmlhttp.send(null);
}

//取得购物车内商品数量
function GetCartCount(pLabel) {
	xmlhttp = initxmlhttp();
	var url = 'kucun.aspx?Mode=CartCount';
	var strCartCount = new Object();   
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function () {RereshCartCount(pLabel);}
	xmlhttp.send(null);
}

function RereshCartCount(pCartCount){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
    {
	    pCartCount.innerHTML = xmlhttp.responseText;		
	}		
}

function chkformsearch()
  {
     if(document.getElementById("Shop_menu1_txtSearch").value=="")
     {
	    return false;
    }      
     if(document.getElementById("Shop_menu1_txtSearch").value=="请输入商品关键字")
     {
	    return false;
    }   
    
    return true; 
 }
 //无法查找图片时，显示指定的图片
function imgErr(img)
{
    img.src='/images/Default.jpg';
}

