var page = 1;
var perpage = 4;
var totalpage = 0;
var totalnum = 0;
var categoryId = "";
var searchKey = "";
var productId = Request['ProductId'];

//获取搜索关键字，搜索方式
if ( Request['SearchKey'] != null &&  Request['SearchKey'] != undefined && Request['CategoryId'] != null &&  Request['CategoryId'] != undefined ){
	searchKey = Request['SearchKey'];
	if ( searchKey == "Enter keyword or item" ||  searchKey == "Enter keyword or item #" ||  searchKey == "Enter keyword or item ") searchKey = "";
}
if ( Request['CategoryId'] != null &&  Request['CategoryId'] != undefined && Request['CategoryId'] != null &&  Request['CategoryId'] != undefined){
	categoryId = Request['CategoryId'];
}

function SetPerPage(v){
	perpage = v;
	return true;
}
/*onLoad*/
function OnLoad(){
	if ( Request['SearchKey'] != null &&  Request['SearchKey'] != undefined ){
		findObj('SearchKey').value = searchKey == "" ? "Enter keyword or item #" : Request['SearchKey'];
	}
	if (Url == "category-mainpage.htm" || Url == "category-mainpage-all.htm"){
		AddHtml("ad",'<img src="images/cosmetics.jpg">');
	}
	if (Url == "login.htm"){
		findObj("login_form").action = ServerUrl + "eboss3_5/system/Login.asp";
		findObj("url").value = ClientUrl + "login.htm";
		findObj("ClientUrl").value = ClientUrl + "login.htm";
		switch(Request['type']){
			case "myfavorite":
				findObj("url").value = ClientUrl + "myaccount-my_favorite.htm";
				break;
			case "orderhistory":
				findObj("url").value = ClientUrl + "myaccount-view_order_history.htm";
				break;
			case "checkout":
				findObj("url").value = SLLHttp + "myaccount-order_save.asp?log1=entry&file=" + SLLHttp + "myaccount-edit-information.asp&file2=" + SLLHttp + "myaccount-ship_info.asp";
				ShowD("NoLoginCheckOutMsg");
				break;
			case "information":
				findObj("url").value = ServerUrl + "eboss3_5/system/myaccount-edit-information.asp?url_login=" + ClientUrl + "login.htm&url=" + SLLHttp + 'myaccount-edit-information.asp&cid=' + ClientId;
				break;
			case "err":
				alert("your email or password err!! please try again");
				break;
		}
		findObj("cid").value = ClientId;
	}
	if (Url == "register.htm"){
		findObj("register_form").action = ServerUrl + "eboss3_5/system/register.asp";
		var returnUrl = ServerUrl + "eboss3_5/system/myaccount-edit-information.asp?url_login=" + ClientUrl + "login.htm&url=" + SLLHttp + 'myaccount-edit-information.asp&cid=' + ClientId;
		if ( Request['type'] == "checkout" ) {
			//type = checkout
			returnUrl = SLLHttp + "myaccount-order_save.asp?log1=entry&file=" + SLLHttp + "myaccount-edit-information.asp&file2=" + SLLHttp + "myaccount-ship_info.asp";
		}
		findObj("url").value = returnUrl
		findObj("cid").value = ClientId
		if (Request['type'] == "err"){
			alert("The email already exists,please try again!");
		}
	}
}
/*Search Function*/
//废除！！：获得第几页
if ( Request['Page'] != null &&  Request['Page'] != undefined ){
	page = Number(Request['Page']);
}
//搜索函数;search-result.htm;product_all_.js;
var myFilterFunc = function(dataSet, row, rowNumber)
{
	//alert(rowNumber);
	if ( (row['ProductName'].toLowerCase().search(searchKey.toLowerCase()) != -1 || row['Unitprice'].search(searchKey) != -1) && (row['CategoryId'].search(categoryId + ",") != -1 ) ){
		return row;                    // Return the row to keep it in the data set.
	}
	return null;                        // Return null to remove the row from the data set.
}
function SetPerPage(v){
	perpage = v;
	return true;
}
//页数生成
var CreatePage = function(v){
	//var ci = findObj("SearchWay").value;
	//var sk = findObj("SearchKey").value;
	//var links = "CategoryId="+ ci+ "&SearchKey="+ sk;
	//links = "search-result.htm?"+ links + "&Page=";
	
	tlinks = page == 1 ? 1 : page - 1;
	var s = '<a href="' + 'javascript:UpdatePage('+tlinks+');' + '" class="next">&lt; Previous</a>. ';
	
	totalnum = v;
	t = totalnum / perpage;
	totalpage = Math.floor(t) < t ? Math.floor(t) + 1 : Math.floor(t) ;
	
	for (var i = 1 ; i <= totalpage ; i++ ){
		s += page == i ? '<span class="pagenumberon">'+i+'</span> .' : '<a href="'+ 'javascript:UpdatePage('+i+');' +'">'+i+'</a>.';
		if (i/23 == Math.floor(i/23)) s += "<br>";
	}
	
	tlinks = page + 1 > totalpage ? totalpage : page + 1;
	s += '<a href="'+ 'javascript:UpdatePage('+tlinks+');' +'">Next &gt;</a>';
	AddHtml("pagenumber", s);
	AddHtml("totalProduct", totalnum);
	return false;
}
//换页时更新页面
function UpdatePage(v){
	page = v==undefined ? page : v;
	Spry_product_all.filter(myFilterFunc);
	if (findObj("sortby") != null) DoSort(findObj("sortby").value);
}
//排列方式
function DoSort(v)
{
	var sortway = v == "ProductId" ? "descending" : "ascending";
	Spry_product_all.sort(v, sortway);
	//Spry_product_all.sort(["firstname", "lastname"], "toggle");
	//Spry_product_all.sort(["firstname", "lastname", "@id"], "toggle");
	return true;
}
//跳转到产品细节
function GotoProductDetail(v){
	var productId = v;	
	var s = "";
	var cid = Request['CategoryId'] == undefined ? 0 : Request['CategoryId'];
	s = "category-product-detail.htm?CategoryId="+cid+"&ProductId="+productId;
	if ( Request['SearchKey'] != null &&  Request['SearchKey'] != undefined ){
		s = "category-product-detail.htm?CategoryId="+cid+"&ProductId="+productId+"&SearchKey="+Request['SearchKey'];
	}
	location = s;
}
//跳转到首页
function GotoIndex(){
	location = ClientUrl+ "index.htm";
}
//搜索跳转
function Search(){
	var ci = findObj("SearchWay").value;
	var sk = findObj("SearchKey").value;
	var s = "CategoryId="+ ci+ "&SearchKey="+ sk;
	var ls = "";
	if ( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") ls = "category-mainpage.htm?"
		else ls = "search-result.htm?";
	if (( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") &&  ci == "0") location = ClientUrl + "search-result.htm?" + s;
		else location = ClientUrl + ls + s;
}
//Path 跳转
function GotoCategoryMainpage(v){
//	var sk = findObj("SearchKey").value;
//	var s = "CategoryId="+ v+ "&SearchKey="+ sk;
//	if ( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") 
//		else location = ClientUrl + "category-mainpage.htm?" + s
	location = ClientUrl + "search-result.htm"
	location = ClientUrl + "category-mainpage.htm?CategoryId=" + v;
}
//显示某一类产品的所有产品 跳转
function GotoProductAll(){
	s = categoryId == "" ? 0 : categoryId ;
	location = ClientUrl + "category-mainpage-all.htm?CategoryId=" + s;
}
//加入到购物车
function AddToShoppingCart(v){
	var buynum = 1 ;
	if (findObj("buynum") != null){
		if (Number(findObj("buynum").value) > 0 ) buynum = Math.floor(Number(findObj("buynum").value));
	}
	location = ClientUrl + "myaccount-shopping_cart.htm?quantity="+buynum+"&productid="+v+"&type=add";
}
//更新购物车数量和优惠券号码
function UpdataShoppingCart(){
	var quantity = "@";
	var productid = "@";
	for (var s in findObj("cartsThings")){	
		if ( s.search("productNumber") != -1 ) {
			quantity += "," + findObj(s).value;
			productid += "," + s.replace("productNumber","");
		}			   
	}	
	quantity = quantity.replace("@,","");
	productid = productid.replace("@,","");
	if (productid == "@") return;
	location = ClientUrl + "myaccount-shopping_cart.htm?quantity="+quantity+"&productid="+productid+"&couponcode="+findObj('couponcode').value+"&type=updata";
}
//删除购物车
function DelShoppingCart(){
	var productid = "@";
	for (var s in findObj("cartsThings")){	
		if ( s.search("removeProductId") != -1 ) {
			if ( findObj(s).checked == true ) productid += "," + findObj(s).value;
		}			   
	}	
	productid = productid.replace("@,","");
	if (productid == "@") return;
	location = ClientUrl + "myaccount-shopping_cart.htm?productid="+productid+"&type=del";
}
//注册
 function Register_Check(){
	if (findObj("firstname").value == ""){alert("FirstName 项目必须填写！");return;}
	if (findObj("lastname").value == ""){alert("LastName 项目必须填写！");return;}
	if (findObj("email").value == ""){alert("email 项目必须填写！");return;}
	if (findObj("password").value == ""){alert("password 项目必须填写！");return;}
	if (findObj("email").value != findObj("email2").value){alert("两次填写的email不一致，请确认！");return;}
	if (findObj("password").value != findObj("password2").value){alert("两次填写的password不一致，请确认！");return;}
	findObj("register_form").submit();
} 
//不注册直接购买
function NoLoginCheckOut(){
	location.replace(SLLHttp+'myaccount-new.asp?act=next&file='+SLLHttp+'myaccount-ship_info.asp');
}
//
function CheckOutNow(){
	var s = ServerUrl + "eboss3_5/system/check_out.asp?sslurl=" + SLLHttp + "&url_login=" + ClientUrl + "login.htm&cid=" + ClientId;
	location.replace(s);
}
//注销
function LoginOut(){
	location.replace(ServerUrl + "eboss3_5/system/LoginOut.asp?url=" + ClientUrl + "index.html");
}
//更改用户资料
function EditInfo(){
	var s = ServerUrl + "eboss3_5/system/myaccount-edit-information.asp?url_login=" + ClientUrl + "login.htm&url=" + SLLHttp + 'myaccount-edit-information.asp&cid=' + ClientId;
	location.replace(s);
}
//加入收藏
function AddToMyFavorite(v){
	location = ClientUrl + "myaccount-my_favorite.htm?" + "type=add&productid="+v;
}
//删除收藏
function DelMyFavorite(){
	var productid = "@";
	for (var s in findObj("cartsThings")){	
		if ( s.search("removeProductId") != -1 ) {
			if ( findObj(s).checked == true ) productid += "," + findObj(s).value;
		}			   
	}	
	productid = productid.replace("@,","");
	if (productid == "@") return;
	location = ClientUrl + "myaccount-my_favorite.htm?productid="+productid+"&type=del";
}
//显示历史定单详细内容
function ShowOrderListDetail(v){
	window.open("myaccount-view_order_history-order_detail.htm?ordernumber="+v,"_blank");
}
//显示大图片
function ShowBigPic(_url){
	if (_url.indexOf("default-productimage") == -1) 
		window.open("show_big_pic.htm?PicUrl="+_url,'picwindow','height=50,width=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}
//是否显示buy按钮和价格
function IsShow(v){
	var price = Number(v);
	if (price > 0) return true
		else return false;
}
