//登录
function Login(formname){
	if(typeof(formname) == 'undefined') var formname = document.loginform;
	with(formname) {
	var e = trim(cer_email.value);
	var p = trim(cer_password.value);
	if(e == "" ){
		alert('Please enter email.');
		cer_email.focus();
		return false;
	}
	if(!isEmail(e)) {
		cer_email.focus();
		return false;
	}
	if(p == "" ){
		alert('Please enter password.');
		cer_password.focus();
		return false;
	}
	}
	var str='email='+e+'&password='+p;
	if (formname.returnurl) str += '&returnurl='+ formname.returnurl.value;
	formname.login.value = 'Login...';
	formname.login.disabled = true;
	AjaxServer('/login/login.php','POST',str);
}
//搜索
function search_tab(n){
	for (a = 1; a <= 3; a++) {
		obj = document.getElementById("search_t"+a);
		obj.style.fontWeight = ( a == n ) ? "bold" : "normal";
		obj.style.cursor = "pointer";
	}
	obj = document.getElementById("searchform");
	objq = document.getElementById("q");
	switch (n) {
		case 2:
			obj.action = "/search.php?t=2";
			obj.method = "post";
		break;
		case 3:
			obj.action = "/search.php?t=3";
			obj.method = "post";
		break;
		default :
			obj.action = "http://www.google.com/search";
			obj.method = "get";
	}
	objq.focus();
}
//邮箱检测
function isEmail(emailStr){
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|mobi)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var emailPat=/^(.+)@(.+)$/;
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null){
		alert("Email Error.");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++){
		if (user.charCodeAt(i)>127)	{
			alert("Email Error.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++)	{
		if (domain.charCodeAt(i)>127){
			alert("Email Error.");
			return false;
		}
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++)	{
		if (domArr[i].search(atomPat)==-1){
			alert("Email Error.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)	{
		alert("Email Error.");
		return false;
	}
	if (len<2){
		alert("Email Error.");
		return false;
	}
	return true;
}
//去除字符串的两侧的空格
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

//去除字符串的左侧的空格
function ltrim(str){
    return str.replace(/(^\s*)/g, "");
}

//去除字符串的右侧的空格
function rtrim(str){
    return str.replace(/(\s*$)/g, "");
}
//去除字符串的中间连续多余空格
function ctrim(str){
	while (str.indexOf("  ") != -1) {
		str = str.substring(0, str.indexOf("  ")) + str.substring(str.indexOf("  ")+1, str.length);
	}
	return str;
}
//检测日期格式
function chkdate(datestr){
	var lthdatestr
	lthdatestr= (datestr != "") ? datestr.length : 0;
	var tmpy=tmpm=tmpd="";
	var status;
	status=0;
	if (lthdatestr==0){
		alert('Please enter date!');
		return false;
	}
	for (i=0;i<lthdatestr;i++){
		if (datestr.charAt(i)== '-') status++;
		if (status>2){
			alert("Invalid format of date!");
			return false;
		}
		if ((status==0) && (datestr.charAt(i)!='-')) tmpy=tmpy+datestr.charAt(i);
		if ((status==1) && (datestr.charAt(i)!='-')) tmpm=tmpm+datestr.charAt(i);
		if ((status==2) && (datestr.charAt(i)!='-')) tmpd=tmpd+datestr.charAt(i);
	}
	year=new String (tmpy);
	month=new String (tmpm);
	day=new String (tmpd)
	if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2)){
		alert("Invalid format of date!");
		return false;
	}
	if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) ){
		alert ("Invalid month or day!");
		return false;
	}
	if (!((year % 4)==0) && (month==2) && (day==29)){
		alert ("This is not a leap year!");
		return false;
	}
	if ((month<=7) && ((month % 2)==0) && (day>=31)){
		alert ("This month is a small month!");
		return false;
	}
	if ((month>=8) && ((month % 2)==1) && (day>=31)){
		alert ("This month is a small month!");
		return false;
	}
	if ((month==2) && (day==30)){
		alert("The Febryary never has this day!");
		return false;
	}
	return true;
}
//判断是否为价钱格式
function isMoney(m){
	strRef = "1234567890.";
	for (i=0; i<m.length; i++) {
		tempChar= m.substring(i,i+1);
 		if(strRef.indexOf(tempChar,0)==-1) {
			alert('Invalid format of price!');
			return false;
		}
		else{
			tempLen=m.indexOf(".");
			if(tempLen!=-1){
				strLen=m.substring(tempLen+1,m.length);
				if(strLen.length>2){
					alert('Invalid format of price!');
					return false;
				}
			}
		}
	}
	return true;
}
/*==================================================================
get_cookie:取得cookie值
==================================================================*/
function get_cookie(name_to_get) {
    var cookie_pair
    var cookie_name
    var cookie_value
    var cookie_array = document.cookie.split("; ")
    for (counter = 0; counter < cookie_array.length; counter++) {
        cookie_pair = cookie_array[counter].split("=")
        cookie_name = cookie_pair[0]
        cookie_value = cookie_pair[1]
        if (cookie_name == name_to_get)  return unescape(cookie_value);
    }
    return null
}
/*==================================================================
set_cookie:设置cookie值
==================================================================*/
function set_cookie(cookie_name, cookie_value, cookie_expire, cookie_path, cookie_domain, cookie_secure) {
    var cookie_string = cookie_name + "=" + cookie_value
    if (cookie_expire) {
        var expire_date = new Date()
        var ms_from_now = cookie_expire * 24 * 60 * 60 * 1000
        expire_date.setTime(expire_date.getTime() + ms_from_now)
        var expire_string = expire_date.toGMTString()
        cookie_string += "; expires=" + expire_string
    }
    if (cookie_path) cookie_string += "; path=" + cookie_path;
    if (cookie_domain) cookie_string += "; domain=" + cookie_domain;
    if (cookie_secure) cookie_string += "; true";
    document.cookie = cookie_string
}
// 检查Cookie是否存在
function checkCookieExist(name){
  if (get_cookie(name))
      return true;
  else
      return false;
}
// 删除Cookie
function deleteCookie(name, path, domain){
	var strCookie;
	if (checkCookieExist(name)){
		strCookie = name + "="; 
		strCookie += (path) ? "; path=" + path : "";
		strCookie += (domain) ? "; domain=" + domain : "";
		strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		document.cookie = strCookie;
	}
}
//复选框高亮
function CheckBox(a) {
	var b = a.id + "_label";
	b = document.getElementById(b);
	b.className = (a.checked) ? "checkboxhigh" : "";
}

function AutoDigital(obj,def){
	if(typeof(def)=='undefined') var def=1;
	var v='';
	var str=obj.value;
	for (var i=0;i<str.length;i++) {
		if(isDigital(str.charAt(i))) v+=str.charAt(i);
	}
	v = (v.length==0) ? def : parseInt(v)+0;
	obj.value=v;
}
function isDigital(text) {
	v = true;
	var checkOK = "0123456789";
	for (var i=0;i<text.length;i++) {
		var t=checkOK.indexOf(text.charAt(i));
		if(t==-1) {
			v=false;
			break;							
		} else {
			v=true;
		}
	}
	return v;
}
function objExists(w){	
	try{
		obj = eval(w);
	}
	catch(obj){
		return false;
	}
	if(typeof(obj)=="object"){
		return true;
	}
	return false;
}