// 设为首页
function setIndex(obj,url){
	var Browser = new Object();
	Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
	Browser.isIE = window.ActiveXObject ? true : false;
	Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
	Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
	
	if(Browser.isIE){obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);}
	//else if(Browser.isOpera) alert("请从工具>首选项中设置");
	else alert("请从工具>选项中设置");
}
// 加入收藏
function addFavorite(sTitle, sURL){
	if(document.all)
		window.external.AddFavorite(sURL, sTitle);
	else
		window.sidebar.addPanel(sTitle, sURL, "");
}
function isEmail(email) {
	var filter = /(\S)+[@]{1}(\S)+[.]{1}(\w)+/;
	if (!filter.test(email)) {
		return false;
	}
	return true;
}
function isTelephone(tel) {
	var reg = /^(\d{3}-\d{8}|\d{3}-\d{7}|\d{4}-\d{7}|\d{4}-\d{8})$/;
	if(!reg.test(tel))
		return false;
	else
		return true;
}
/**
  * 功能: 判断是否为数字
  * 参数: 判断数字
  * 返回: true/false
  */
function chkNumonly(textValue) {  
	var  numMark  =  '1234567890';  
	var  strPass  =  textValue;  
	var  strLength  =  strPass.length;  
	var  lchar  =  strPass.charAt((strLength)  -  1);  
	if (strPass == "")
		return false;
	if(numMark.indexOf(lchar)  ==  -1)
		return false;
	return true;
} 
/**
 * 切换大图小图
 */
var curProductView = 1;
function changeView() {
	var v1 = curProductView;
	var v2 = v1==1 ? 2 : 1;
	var html = v1==1 ? '切换到缩略图' : '切换到详细';
	$('#product_list_' + v1).hide();
	$('#product_list_' + v2).show();
	$('#product_view_btn').html(html);
	curProductView = v2;
}
function changeViewEn() {
	var v1 = curProductView;
	var v2 = v1==1 ? 2 : 1;
	var html = v1==1 ? 'Switch Thumbnails' : 'Switch Details';
	$('#product_list_' + v1).hide();
	$('#product_list_' + v2).show();
	$('#product_view_btn').html(html);
	curProductView = v2;
}

/**
 * product consult
 */
function crtConsult(id) {
	var content = jQuery.trim($('#consult_content_999').val());
	var typeStr = id==0 ? '留言' : '咨询';
	if (content=='') {
		alert('请输入' + typeStr + '内容');
		$('#consult_content_999').focus();
		return;
	}
	$.post('/product_deal.php',
		   {'content':content,'productid':id,'action':'crtconsult'},
		   function(data) {
			   if (data=='1') {
				   alert(typeStr + '成功,请等待回复');
				   $('#consult_content_999').val('');
				   return;
			   }
			   else if(data=='-2') {
				   alert('请先登陆');
				   return;
			   }
			   else {
				   alert('意外错误');
				   return;
			   }
		   }
	);
}
function getConsult(id,page) {
	var pageSize = 10;
	var typeStr = id==0 ? '留言' : '咨询';
	$('#consult_list_999').html('载入中...');
	$.getJSON('/product_deal.php?action=getconsult&productid='+id+'&page='+page+'&pagesize='+pageSize,
		function(data){
			var total = data.total;
			if (total>0) {
				$('#consult_list_999').html('');
				$.each(data.item, function(i,item){
					var html = '<div class="item">\
							<div class="user">网友：'+item.username+'<span>发布于：'+item.addtime+'</span></div>\
							<dl class="ask"><dt>'+typeStr+'内容：</dt><dd>'+item.content+'</dd></dl>\
							<dl class="answer">\
								<dt>网站回复：</dt>\
								<dd>\
									<div class="ac">'+item.reply+'</div>\
									<div class="date">'+item.replytime+'</div>\
								</dd>\
							</dl>\
							<div class="clear"></div>\
							</div>';
					$(html).appendTo('#consult_list_999');
				});
	  			// set page content
	  			setPageContent(total,page,pageSize,id);
			}
			else {
				$('#consult_list_999').html('<div class="item">无'+typeStr+'内容</div>');
			}
	});
}
/**
 * set page content
 * @int total
 * @int page
 * @int pageSize
 */
function setPageContent(total,page,pageSize,id) {
	var html = '';
	var totalPage = Math.ceil(total/pageSize);
	var startPage = 1;
	var defaultPageViewNum = 10;
	var halfPage = defaultPageViewNum/2;
	if (page<1 || page>totalPage) {
		html = 'over page';
	}
	else {
		html = '共'+total+'条 '
		if (page>1) {
			html += '<a onclick="javascript:getConsult(' + id + ',1);" >首页</a><a onclick="javascript:getConsult(' + id + ',' + (page-1) +');">上一页</a>';
		}
		if (page > halfPage) {
			startPage = page - halfPage +1;
		}
		for (var i=startPage,j=0;i<=totalPage && j<=defaultPageViewNum;i++,j++) {
			if (i==page)
				html += '<span class="current">' + i + '</span>' ;
			else
				html += ' <a onclick="javascript:getConsult(' + id + ',' + i +');">' + i + '</a>';
		}
		html += '</span>';
		if (page<totalPage) {
			html += '<a onclick="javascript:getConsult(' + id + ',' + (page+1) + ');" >下一页</a><a onclick="javascript:getConsult(' + id + ',' + totalPage + ');">尾页</a>';
		}
	}
	$('#consult_page_999').html(html);
}

// 搜索
function proSearch() {
	var f = document.getElementById('headerSearch');
	var q = jQuery.trim(f.q.value);
	if (q=='' || q=='输入关键字') {
		alert('请输入关键字');
		f.q.focus();
		return false;
	}
	window.location = 'product.php?q=' + encodeURIComponent(q);
}
// change city
var tmpAreaOption = null;
function changeCity(cityType,s) {
	if (cityType==1)
		tmpAreaOption = document.getElementById('area_city');
	else
		tmpAreaOption = document.getElementById('area');
	$.getJSON('/area_deal.php?action=getarea&areaidp=' + s.value,changeSubCity);
}
function changeSubCity(data){ 
	// clear options
	var opts = tmpAreaOption.options;
	for (var i=0;i<opts.length;i++) {
		tmpAreaOption.remove(i);
		i--;
	}
	// set options
	if (data.length > 0) { 
		$.each(data, function(i,item){
			tmpAreaOption.options[i] = new Option(item.areaname,item.areaid);
		});
	}
	else {
		tmpAreaOption.options[0] = new Option('未设置下属城市',0);
	}
	tmpAreaOption.options[0].selected=true;
	tmpAreaOption.onchange();
} 
/**
 * 注册
 */
function registUser() {
	var username = $('#username').val();
	var password = $('#password').val();
	var password2 = $('#password2').val();
	var truename = $('#truename').val();
	var gender = $("input[name=gender]:checked").val();
	var email = $('#email').val();
	var telephone = $('#telephone').val();
	var postcode = $('#postcode').val();
	var area = $('#area_city').val();
	var region = $('#region').val();
	var address = $('#address').val();
	
	$('#username_error').hide();
	$('#username_error2').hide();
	$('#password_error').hide();
	$('#password2_error').hide();
	$('#truename_error').hide();
	$('#email_error').hide();
	$('#telephone_error').hide();
	$('#postcode_error').hide();
	$('#area_error').hide();
	$('#region_error').hide();
	$('#address_error').hide();
	var usernameLength = username.replace(/[^\x00-\xff]/g,"xx").length;
	if (usernameLength<4 || usernameLength>20) {
		$('#username').focus();
		$('#username_error').show();
		return;
	}
	if (password.length<6 || password.length>16) {
		$('#password').focus();
		$('#password_error').show();
		return;
	}
	if (password.length<6 || password.length>16) {
		$('#password').focus();
		$('#password_error').show();
		return;
	}
	if (password2 != password) {
		$('#password2').focus();
		$('#password2_error').show();
		return;
	}
	if (truename.length<2 || truename.length>10) {
		$('#truename').focus();
		$('#truename_error').show();
		return;
	}
	if (!isEmail(email)) {
		$('#email').focus();
		$('#email_error').show();
		return;
	}
	if (telephone.length<6) {
		$('#telephone').focus();
		$('#telephone_error').show();
		return;
	}
	if (postcode.length!=6) {
		$('#postcode').focus();
		$('#postcode_error').show();
		return;
	}
	if (area==0) {
		$('#area').focus();
		$('#area_error').show();
		return;
	}
	if (region == '') {
		$('#region').focus();
		$('#region_error').show();
		return;
	}
	if (address == '') {
		$('#address').focus();
		$('#address_error').show();
		return;
	}
	$('#regist_img').hide();
	$('#regist_txt').show();
	$.post('ajax.php',{action:'regist',username:username,password:password,truename:truename,gender:gender,email:email,telephone:telephone,postcode:postcode,area:area,region:region,address:address},
		   function(data) {
			   switch(data) {
				   case '1':
				   		window.location = 'regist.php?action=success_user';
						return;
				   case '-1':
				   		alert('参数错误');
						return;
					case '-2':
						$('#username_error2').show();
						$('#username').focus();
						$('#regist_txt').hide();
						$('#regist_img').show();
						return;
					case '-3':
						$('#email_error').html('该Email已经存在，请选用其他Email');
						$('#email_error').show();
						$('#email').focus();
						$('#regist_txt').hide();
						$('#regist_img').show();
						return;
					case '-4':
						alert('意外错误');
						return;
			   }
		   }
	);
}
function registCompany() {
	var username = $('#username').val();
	var password = $('#password').val();
	var password2 = $('#password2').val();
	var c_company = $('#c_company').val();
	var c_telephone = $('#c_telephone').val();
	var c_fax = $('#c_fax').val();
	var email = $('#email').val();
	var c_sitecode = $('#c_sitecode').val();
	var truename = $('#truename').val();
	var telephone = $('#telephone').val();
	var postcode = $('#postcode').val();
	var area = $('#area_city').val();
	var region = $('#region').val();
	var address = $('#address').val();
	
	$('#username_error').hide();
	$('#username_error2').hide();
	$('#password_error').hide();
	$('#password2_error').hide();
	$('#c_company_error').hide();
	$('#c_telephone_error').hide();
	$('#c_fax_error').hide();
	$('#email_error').hide();
	$('#c_sitecode_error').hide();
	$('#truename_error').hide();
	$('#telephone_error').hide();
	$('#postcode_error').hide();
	$('#area_error').hide();
	$('#region_error').hide();
	$('#address_error').hide();
	var usernameLength = username.replace(/[^\x00-\xff]/g,"xx").length;
	if (usernameLength<4 || usernameLength>20) {
		$('#username').focus();
		$('#username_error').show();
		return;
	}
	/*
	if (username.length<4 || username.length>20) {
		$('#username').focus();
		$('#username_error').show();
		return;
	}
	*/
	if (password.length<6 || password.length>16) {
		$('#password').focus();
		$('#password_error').show();
		return;
	}
	if (password.length<6 || password.length>16) {
		$('#password').focus();
		$('#password_error').show();
		return;
	}
	if (password2 != password) {
		$('#password2').focus();
		$('#password2_error').show();
		return;
	}
	if (c_company.length=='') {
		$('#c_company').focus();
		$('#c_company_error').show();
		return;
	}
	if (c_telephone.length=='') {
		$('#c_telephone').focus();
		$('#c_telephone_error').show();
		return;
	}
	if (c_fax.length=='') {
		$('#c_fax').focus();
		$('#c_fax_error').show();
		return;
	}
	if (!isEmail(email)) {
		$('#email').focus();
		$('#email_error').show();
		return;
	}
	if (c_sitecode.length=='') {
		$('#c_sitecode').focus();
		$('#c_sitecode_error').show();
		return;
	}
	if (truename.length<2 || truename.length>10) {
		$('#truename').focus();
		$('#truename_error').show();
		return;
	}
	if (telephone.length<6) {
		$('#telephone').focus();
		$('#telephone_error').show();
		return;
	}
	if (postcode.length!=6) {
		$('#postcode').focus();
		$('#postcode_error').show();
		return;
	}
	if (area==0) {
		$('#area').focus();
		$('#area_error').show();
		return;
	}
	if (region == '') {
		$('#region').focus();
		$('#region_error').show();
		return;
	}
	if (address == '') {
		$('#address').focus();
		$('#address_error').show();
		return;
	}
	$('#regist_img').hide();
	$('#regist_txt').show();
	$.post('ajax.php',{action:'registcompany',username:username,password:password,c_company:c_company,c_telephone:c_telephone,c_fax:c_fax,email:email,c_sitecode:c_sitecode,truename:truename,telephone:telephone,postcode:postcode,area:area,address:address},
		   function(data) {
			   switch(data) {
				   case '1':
				   		window.location = 'regist.php?action=success_company';
						return;
				   case '-1':
				   		alert('参数错误');
						return;
					case '-2':
						$('#username_error2').show();
						$('#username').focus();
						$('#regist_txt').hide();
						$('#regist_img').show();
						return;
					case '-3':
						$('#email_error').html('该Email已经存在，请选用其他Email');
						$('#email_error').show();
						$('#email').focus();
						$('#regist_txt').hide();
						$('#regist_img').show();
						return;
					case '-4':
						alert('意外错误');
						return;
			   }
		   }
	);
}
function checkRegistUser() {
	var username = $('#username').val();
	$('#username_error').hide();
	$('#username_error2').hide();
	var usernameLength = username.replace(/[^\x00-\xff]/g,"xx").length;
	if (usernameLength<4 || usernameLength>20) {
		$('#username').focus();
		$('#username_error').show();
		return;
	}
	$.post('ajax.php',{action:'checkregistuser',username:username},
		   function(data) {
			   if (data!='1') {
				   $('#username_error2').show();
				   $('#username').focus();
			   }
		   }
	);
}
/*---------------------------------------登陆------------------------------------------*/
function login(usertype) {
	var username = $('#login_username').val();
	var password = $('#login_password').val();
	if (username=='') {
		alert('请输入用户名');
		$('#login_username').focus();
		return false;
	}
	if (password=='') {
		alert('请输入密码');
		$('#login_password').focus();
		return false;
	}
	$.post('ajax.php',{action:'login',username:username,password:password,usertype:usertype},
		function(data) {
			if (data.substr(0,1)=='/') {
				window.location = data;
			}
			else if (data=='1') {
				if (typeof cburl != 'undefined' && cburl!='')
					window.location = cburl;
				else
					window.location = '/my/';
			}
			else if (data=='-3')
				alert('您的账户不属于企业账户');
			else
				alert('用户名或密码错误');
		}
	);
	return false;
}
function logout() {
	$.post('/ajax.php',{action:'logout'},
		   function(data) {
			   window.location = '/';
		   }
	);
}
/*---------------------------------------收藏商品------------------------------------------*/
function addFav(id) {
	if (id<0) return;
	$.get('/ajax.php?action=addfav&id='+id,
		function(data) {
			var info = null
			switch(data) {
				case '1':
					info = '收藏成功';
					break;
				case '-1':
					info = '请先登陆';
					break;
				case '-2':
					info = '参数错误';
					break;
				case '-3':
					info = '您已收藏了此商品，请到个人中心查看';
					break;
				default:
					info = '意外错误';
			}
			alert(info);
		}
	);
				
}
// 删除收藏
function delFav(id) {
	$.get('/ajax.php?action=delfav&id='+id,
		function(data) {
			var info = null
			switch(data) {
				case '1':
					info = '删除成功';
					$('#fav_'+id).css('display','none');
					//var total = (parseInt($('#totalFav').html())-1).toString();
					//$('#totalFav').html(total);
					break;
				case '-1':
					info = '请先登陆';
					break;
				case '-2':
					info = '参数错误';
					break;
				default:
					info = '意外错误';
			}
			alert(info);
		}
	);
}

/*---------------------------------------购物车------------------------------------------*/
function changeCar(act,id) {
	switch(act) {
		case 'dec':
			var buynum = parseInt($('#buynum_h_' + id).val()) - 1;
			window.location = 'car.php?action=chnum&id='+id+'&buynum='+buynum;
			break;
		case 'add':
			var buynum = parseInt($('#buynum_h_' + id).val()) + 1;
			window.location = 'car.php?action=chnum&id='+id+'&buynum='+buynum;
			break;
		case 'del':
			window.location = 'car.php?action=del&id='+id;
			break;
		case 'clear':
			window.location = 'car.php?action=clear';
			break;
		case 'chnum':
			var buynumOld = $('#buynum_h_' + id).val();
			var buynum = $('#buynum_' + id).val();
			if (chkNumonly(buynum) && buynumOld!=buynum) {
				window.location = 'car.php?action=chnum&id='+id+'&buynum='+buynum;
			}
			break;
	}	
}
// 找回密码
function findpwd() {
	var username = $('#username').val();
	var email = $('#email').val();
	if (username=='' && email=='') {
		alert('请输入用户名或Email');
		$('#username').focus();
		return false;
	}
	return true;
}
















/**
 * 留言板
 */
function checkMessage(f) {
	$('#name_error').html('');
	$('#tel_error').html('');
	$('#vcode_error').html('');
	if (f.name.value=="") {
		$('#name_error').html('请输入您的姓名');
		f.name.focus();
		return false;
	}
	if (f.tel.value=="") {
		$('#tel_error').html('请输入您的电话');
		f.tel.focus();
		return false;
	}
	if (f.content.value=="") {
		alert("请输入留言内容");
		f.content.focus();
		return false;
	}
	if (f.vcode.value=="") {
		$('#vcode_error').html('请输入验证码');
		f.vcode.focus();
		return false;
	}
	f.submit();
}
// 刷新验证码
function refreshCode(o) {
	o.src = 'verify/verifycode.php?rnd=' + parseInt(Math.random()*10000);
}

