﻿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'];
}else{
	categoryId = 0;
}
startPage = ( Request['start'] == null ||  Request['start'] == undefined ) ? 1 : Number(Request['start']);
sortbyKey = ( Request['sortby'] == null ||  Request['sortby'] == undefined ) ? "" : Request['sortby'];
orderbyKey = ( Request['orderby'] == null ||  Request['orderby'] == undefined ) ? "" : Request['orderby'];

function SetPerPage(v){
	perpage = v;
	return true;
}
/*onLoad*/
function OnLoad(){
	//sortby和orderby转到正确的地方
	var sortbyObj = findObj("sortby");
	if ( sortbyObj != null && sortbyObj != undefined){
		for (var i = 0 ; i < sortbyObj.length ; i ++){
			sortbyObj[i].selected = false;
			if (sortbyObj[i].value == sortbyKey) sortbyObj[i].selected = true;
		}
	}
	var orderbyObj = findObj("orderby");
	if ( orderbyObj != null && orderbyObj != undefined){
		for (var i = 0 ; i < orderbyObj.length ; i ++){
			orderbyObj[i].selected = false;
			if (orderbyObj[i].value == orderbyKey) orderbyObj[i].selected = true;
		}
	}
	//
	if ( Request['SearchKey'] != null &&  Request['SearchKey'] != undefined ){
		if (findObj('SearchKey') != null) {
			findObj('SearchKey').value = searchKey == "" ? "Enter keyword or item #" : Request['SearchKey'];
		}
	}
	if (Url == "category-mainpage.htm" || Url == "category-mainpage-all.php"){
		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 "review":
				findObj("url").value = ClientUrl + "category-product-detail.php?CategoryId=0&ProductId=" + Request['ProductId'];
				break;
			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("Incorrect  e-mail address or password!! Please try again");
				break;
		}
		findObj("cid").value = ClientId;
		var p_s = '<a href="'+SLLHttp+'myaccount-ship_info_paypal.asp"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border=0 align="absmiddle" style="margin-right:7px;"></a>'
		AddHtml("paypalexpress", p_s);
	}
	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("ClientUrl").value = ClientUrl + "register.htm";
		findObj("cid").value = ClientId
		if (Request['type'] == "err"){
			alert("The e-mail address you just entered has been registered, please go to Returned customer to login or enter a different e-mail address to register,!");
		}
	}
	if (Url == "myaccount-shopping_cart.htm"){
		//var p_s = '<a href="'+SLLHttp+'myaccount-ship_info_paypal.asp"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border=0 align="absmiddle" style="margin-right:7px;"></a>'
		var p_s = '<a href="'+SLLHttp+'myaccount-ship_info_paypal.asp"><img src="images/btn_xpressCheckout.gif" border=0 align="absmiddle" style="margin-right:7px;"></a>'
		AddHtml("paypalexpress", p_s);
	}
	if (Url == "getpwd.htm"){
		if( Request['type'] == "ok" ){
			alert("Your password has been sent to your email address!");
			location = "login.htm";
		}else{
			alert("The email doesn't exist! please try again!");
		}
	}
}
//注册
var gotoRegiest = function(){
	var type = ""
	if (Request['type'] == "checkout"){
		type = "checkout"
	}
	location='register.htm' + "?type=" + type;
}
/*Search Function*/
//废除！！：获得第几页
if ( Request['Page'] != null &&  Request['Page'] != undefined ){
	page = Number(Request['Page']);
}
//搜索函数;search-result.php;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['ItemNumber'].toLowerCase().search(searchKey.toLowerCase()) != -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;
}
//产品细节显示 过滤函数 用于Color_Size
var ProductDetailFilterFunc = function(dataSet, row, rowNumber)
{
	if (row['Value'].search("cn.com.D0") != -1){
		var tarr = row['Value'].split("cn.com.D0");
		var s = '<select name="colorandsize'+row['Name']+'" id="colorandsize'+row['Name']+'">';
		for (var i = 0 ; i < tarr.length ; i ++){
			if (tarr[i] != ""){
				if ( i == 0 ){
					s += '<option value="'+tarr[i]+'" selected="selected">'+tarr[i]+'</option>';
				}else{
					s += '<option value="'+tarr[i]+'">'+tarr[i]+'</option>';
				}
			}
		}
		s += '</select>';
		row['Value'] =s;
	}
	return row;
	//return null;
}
//页数生成
var CreatePage = function(v){
	//var ci = findObj("SearchWay").value;
	//var sk = findObj("SearchKey").value;
	//var links = "CategoryId="+ ci+ "&SearchKey="+ sk;
	//links = "search-result.php?"+ 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;
}
var CreateSearchPage = function(v){
	//var ci = findObj("SearchWay").value;
	//var sk = findObj("SearchKey").value;
	//var links = "CategoryId="+ ci+ "&SearchKey="+ sk;
	//links = "search-result.php?"+ links + "&Page=";
	
	var page = Number(Request['start']) == 1;
	tlinks = startPage == 1 ? 1 : startPage - 1;
	var s = '<a href="' + 'javascript:UpdateSearchPage('+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 += startPage == i ? '<span class="pagenumberon">'+i+'</span> .' : '<a href="'+ 'javascript:UpdateSearchPage('+i+');' +'">'+i+'</a>.';
		if (i/23 == Math.floor(i/23)) s += "<br>";
	}
	
	tlinks = startPage + 1 > totalpage ? totalpage : startPage + 1;
	s += '<a href="'+ 'javascript:UpdateSearchPage('+tlinks+');' +'">Next &gt;</a>';
	AddHtml("pagenumber", s);
	AddHtml("totalProduct", totalnum);
	
	return false;
}
//换页时更新页面
function UpdateSearchPage(v){
	var page = v==undefined ? 1 : v;
	var s = Url + "?" + "CategoryId=" + categoryId +"&SearchKey=" + searchKey + "&sortby=" + findObj("sortby").value;
	s += "&start=" + page;
	s += "&orderby=" + findObj("orderby").value;
	location = s;
}
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.php?CategoryId="+cid+"&ProductId="+productId;
	if ( Request['SearchKey'] != null &&  Request['SearchKey'] != undefined ){
		s = "category-product-detail.php?CategoryId="+cid+"&ProductId="+productId+"&SearchKey="+Request['SearchKey'];
	}
	location = s;
}
//跳转到首页
function GotoIndex(){
	location = ClientUrl+ "index.php";
}
//搜索跳转
function Search(){
	var ci = findObj("SearchWay").value;
	var sk = findObj("SearchKey").value;
	var s = "CategoryId="+ ci+ "&SearchKey="+ sk;
	s += "&orderby=" + findObj("orderby").value;
	s += "&sortby=" + findObj("sortby").value;
	var ls = "";
	if ( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") ls = "category-mainpage-all.php?"
		else ls = "search-result.php?";
	if (( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") &&  ci == "0") location = ClientUrl + "search-result.php?" + s;
		else location = ClientUrl + ls + s;
}
function SortListSearch(){
	var ci = findObj("SortListSearchWay").value;
	var sk = "Enter keyword or item #";
	var s = "CategoryId="+ ci+ "&SearchKey="+ sk;
	s += "&orderby=";
	s += "&sortby=";
	var ls = "";
	if ( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") ls = "category-mainpage-all.php?"
		else ls = "search-result.php?";
	if (( sk == "Enter keyword or item" ||  sk == "Enter keyword or item #" ||  sk == "Enter keyword or item ") &&  ci == "0") location = ClientUrl + "search-result.php?" + s;
		else location = ClientUrl + ls + s;
}
//搜索Keyword输入框获得焦点
function SearchKeyOnFocus(){
	if (findObj("SearchKey").value == "Enter keyword or item #"){
		findObj("SearchKey").value = "";
	}
}
//搜索Keyword输入框失去焦点
function SearchKeyOnBlur(){
	if (findObj("SearchKey").value == ""){
		findObj("SearchKey").value = "Enter keyword or item #";
	}
}
//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.php"
	location = ClientUrl + "category-mainpage-all.php?CategoryId=" + v;
}
//显示某一类产品的所有产品 跳转
function GotoProductAll(){
	s = categoryId == "" ? 0 : categoryId ;
	location = ClientUrl + "category-mainpage-all.php?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));
	}
	var s = "&attribute=";
	for (var n in findObj("attribute")){
		if (n.search("colorandsize") != -1){
			s += n.replace("colorandsize","") + "c.c.D" + findObj("attribute")[n].value + "c.c.D";
		}
	}
	s = s.replace(/(c\.c\.D)$/,"");
	//alert(s);
	location = ClientUrl + "myaccount-shopping_cart.htm?quantity="+buynum+"&productid="+v+"&type=add"+s;
}
//更新购物车数量和优惠券号码
function UpdataShoppingCart(){
	var quantity = "@";
	var key = "@";
	for (var s in findObj("cartsThings")){	
		if ( s.search("productNumber") != -1 ) {
			quantity += "," + findObj(s).value;
			key += "," + s.replace("productNumber","");
		}			   
	}	
	quantity = quantity.replace("@,","");
	key = key.replace("@,","");
	if (key == "@") return;
	if (findObj('couponcode') != null){
		location = ClientUrl + "myaccount-shopping_cart.htm?quantity="+quantity+"&key="+key+"&couponcode="+findObj('couponcode').value+"&type=updata";
	}else{
		location = ClientUrl + "myaccount-shopping_cart.htm?quantity="+quantity+"&key="+key+"&couponcode=&type=updata";
	}
}
//删除购物车
function DelShoppingCart(){
	var key = "@";
	for (var s in findObj("cartsThings")){	
		if ( s.search("removeProductId") != -1 ) {
			if ( findObj(s).checked == true ) key += "," + findObj(s).value;
		}			   
	}	
	key = key.replace("@,","");
	if (key == "@") return;
	location = ClientUrl + "myaccount-shopping_cart.htm?key="+key+"&type=del";
}
//注册
 function Register_Check(){
	var v = findObj('email').value;
	var re = new  RegExp("\\w+([.]\\w+)*@\\w+([.]\\w+)+");
	if (findObj("firstname").value == ""){alert("Please enter your first name！");return;}
	if (findObj("lastname").value == ""){alert("Please enter your last name！");return;}
	if(!re.test(v.replace(/^\s*|\s*$/g,''))){
		alert("Incorrect  e-mail address！");
		return;
	}
	if (findObj("email").value == ""){alert("Please enter your email！");return;}
	if (findObj("password").value == ""){alert("Please enter your password！");return;}
	if (findObj("email").value != findObj("email2").value){alert("Please retype your e-mail address to confirm！");return;}
	if (findObj("password").value != findObj("password2").value){alert("Please retype your password to confirm！");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 + "redirector.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;
}
//是否显示Promotion
function ShowPromotion(v){
	//CategoryId =0 不显示promotion
	if  (Request['CategoryId'] != null &&  Request['CategoryId'] != undefined){
		if (Request['CategoryId'] == "0" ){
			HideD("featuredproduct");
			return true;
		}
	}
	if (v == 0){
		HideD("featuredproduct");
	}else{
		ShowD("featuredproduct");
	}
	return true;
}
//获取密码
function getpwd(){
	var v = findObj('youremail').value;
	var re = new  RegExp("\\w+([.]\\w+)*@\\w+([.]\\w+)+");
	if(re.test(v.replace(/^\s*|\s*$/g,''))){
		var s = ServerUrl + "eboss3_5/system/getpwd.asp?email=" + v + "&url=" + ClientUrl + "getpwd.htm&cid=" + ClientId;
		location.replace(s);
	}else{
		alert("Incorrect  e-mail address.");
	}
}
//检查email
function checkemail(v){
	var re = new  RegExp("\\w+([.]\\w+)*@\\w+([.]\\w+)+");
	if(re.test(v.replace(/^\s*|\s*$/g,''))){
		return true
	}else{
		return false;
	}
}
//按Buy不能加入到shopping cart的修正
function CheckEmtyBuy(_v){
	if (Request['type'] != undefined){
		if(Request['type'] == "add"){
			//alert(_v);
			if (_v == 0){
				//location = "redirector.html";
				writeCookie('active_server', 'true', null);
				location = 'redirector.html?url=' + document.URL.split('&').join('ccd0').split('?').join('ccD0').split('=').join('cCD0');
				//alert(document.URL);
			}
			//if (readCookie('active_server') == ''){
			//}
		}
	}
}