function getRealLength(str)	//检查实际长度，一个汉字占2字节
{
	var result = 0;
	for(var tempi=0;tempi<str.length;tempi++)
	{
		result ++ ;
		temp=str.substring(tempi,tempi+1);
		if(/[^ -}]/.test(temp))
			result++;
	}
	return result ;
}

function chkTextLength() //检查文本的长度是否合法
{
	var n=chkTextLength.arguments.length/2;
	var flag=true;
	var warn = "";
	var limit_length=2000;
	for(i=0;i<n;i++) {
		if(isNull(chkTextLength.arguments[i*2]))
		{
			if(getRealLength(chkTextLength.arguments[i*2])>limit_length)
				warn=warn+"["+chkTextLength.arguments[i*2+1]+"]长度为"+getRealLength(chkTextLength.arguments[i*2])+",不能超过"+limit_length+" \n";
		} 
	}

	if(warn=="") {
		return true;
	} else {
  		alert("以下域不能超过规定长度:\n"+warn+"\n\n注：一个汉字占2个长度");
  		return false;
	}
}

function chkNull(str,name)
{
	if(str==""||str==" ")
	{
		window.alert(name+"不能为空");
		return false;
	}
	var chars = str.split("");//creat array
	for( var i= 0;i<chars.length; i++)
	{
		if(chars[i]!=" ")
			return true;
	}
	window.alert(name+"不能为空");
	return false; 
}

function checkEmail(email)
{
	var emailPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (emailPattern.test(email)==false)
	{
		alert("非法的Email地址！");
		return false;
	}
	else
		return true;
}

function isDigital(str,name)
{
   var num="0123456789";
   var result = new String();
   var chars = str.split("");//creat array
   for( var i= 0;i<chars.length; i++)
   {
	   if( num.indexOf(chars[i])==-1)
	   {
			window.alert(name+"必须是数字");
			return false;
	   }
   }
   return true;
}

function chkType(type,str,name)
{
	if(type=="digital") //是否是数字
	{
		if(!isDigital(str,name))
			return false;
	}
	else if(type=="email") //是否是邮件
	{
		if(!checkEmail(str))
			return false;
	}
	return true; 
}


/**
* 检测是否为空(如果输入的是多个空格也算是空)
* 不对外
* 如果返回是true,说明不为空
**/
function isNull(str)
{
	if(str==""||str==" ")
		return false;
	var chars = str.split("");
	for( var i= 0;i<chars.length; i++)
	{
		if(chars[i]!=" ")
			return true;
	}
	return false; 
}
/**
* 不对外
* 检测实数
* 根据这个方法得出的长度，直接可以和数据库字段设置的长度匹配
* 参数 判断对象，小数位长度，整数位长度
**/
//----------------------,不对外----------------------------------------//
function isReal(_theStr,decLen,intLen){
	var theStr=_theStr;

	if(theStr.indexOf("-")==0) 
	{
		theStr=theStr.substring(1,theStr.length);
		if(!isNull(theStr))
			return false;
	}
	var dot1st=theStr.indexOf(".");
	var dot2nd=theStr.lastIndexOf(".");
	var OK=true;

	if(!isNull(theStr)){return true;}
	if(intLen==null) intLen=10;
	if(dot1st==-1){
		if(!isInt(theStr)){
			return false;
		}
		else if(theStr.length>intLen){
			alert("整数位太长，最多"+intLen+"位整数！");
			return false;
		}
		else{
			return true;
		}
	}
	else if(dot1st!=dot2nd){return false;}
	else if(dot1st==0){return false}
	else{
		var intPart=theStr.substring(0,dot1st);
		var decPart=theStr.substring(dot2nd+1);

		if(!isInt(intPart)||!isInt(decPart)){
			return false;
		}
		else if(intPart.length>intLen){
			alert("整数位太长，最多"+intLen+"位整数！");
			return false;
		}
		else if(decPart.length>decLen){
			alert("小数位太长，最多"+decLen+"位整数！");
			return false;
		}
		else if(decPart==""){
			return false;
		}
		else{
			return true;
		}
	}
}
/** 
* 不对外
* 检测是否位数字
**/ 
function isDigit(theNum){
	var theMask="0123456789";
	if(theNum==""){
		return true;
	}
	else if(theMask.indexOf(theNum)==-1){
		return false;
	}
	return true;
}
/** 
* 不对外
* 检测整数
**/
function isInt(theInt){

	if(theInt=="") {
		return true;
	} else {
		for(var i=0;i<theInt.length;i++){
			if(isDigit(theInt.charAt(i))==false){
				return false;
			}
		}
		return true;
	}
}

/** 
* 检测金额
* 对外使用
* 参数： rmb,判断的对象； prom,判断错误用的对象文字提示
* 返回： true,数字正确；  false,不为数字
**/
function chkMoney(strmoney,prom)
{

	if (strmoney== "")
    {
        alert("["+prom+"] 不能为空");
        return false;
    }
    else if(!(isReal(strmoney,2,7)))
    {
           alert("["+prom+"] 必须为数字,可保留两位小数");
           return false;
    }

	return true;
}

function check(isnull,type,str,len,name)
{
	if(isnull!=0) //0为允许为空 , 非0为不允许为空
	{
		if(!chkNull(str,name))
			return false;			
	}
	if(type!=0)
	{
		if(!chkType(type,str,name)) //digital 数字
			return false;
	}
	if(len!=0)
	{
		if(getRealLength(str)>len)
		{
			window.alert(name+'的长度不能超过'+len+'(一个汉字长度为2)');
			return false;
		}
	}
	return true;
}

function newWin(url,width,height)
{
	window.open(url,"","height="+height+",width="+width+",status=yes,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");

}

function parseToMoney(str)
{
	var temp = str + "";
	var dot1st=temp.indexOf(".");
	if(dot1st==-1)
		return str;
	else
	{
		if(temp.length>(dot1st+3))
		{
			return temp.substring(0,(dot1st+3));
		}
		else
			return str;
	}
	return str;
}

function checkLogin()
{
	if(!chkNull(frmlogin.username.value,"登陆用户名"))
		return false;
	else if(!chkNull(frmlogin.userpwd.value,"登陆密码"))
		return false;
	else
		return true;
}

/** 把所有的checkbox选中/不选中 如果第一个是选中的，全部取消选中。如果第一个不选中，全部选中**/
function selectAll(objname)
{
	var obj = eval("document.all."+objname+"");
	if(obj)
	{
		var len = obj.length;
		if(len)
		{
			var result=true;
			if(obj[0].checked)
				result=false;
			for(var i=0;i<len;i++)
			{
				obj[i].checked=result;
			}	
		}
		else
		{
			if(obj.checked)
				obj.checked=false;
			else 
				obj.checked=true;
		}
	}
	else
	{
		//一个都没有，不用考虑
	}
}

/** 取得所有选中的checkbox的值组合,并且给每个值加上标签[],主要用在选择出现位置 **/
function gatherCheckbox(objname)
{
	var result="";
	var obj = eval("document.all."+objname+"");
	if(obj)
	{
		var len = obj.length;
		if(len)
		{
			for(var i=0;i<len;i++)
			{
				if(obj[i].checked)
				{
					if(result!="")
						result=result+",";
					result=result+"["+obj[i].value+"]";
				}
			}	
		}
		else
		{
			if(obj.checked)
				result=obj.value;
		}
	}
	else
	{
		//一个都没有，不用考虑
	}
	return result;
}


/** 取得所有选中的checkbox的值组合 **/
function gatherCheckboxValue(objname)
{
	var result="";
	var obj = eval("document.all."+objname+"");
	if(obj)
	{
		var len = obj.length;
		if(len)
		{
			for(var i=0;i<len;i++)
			{
				if(obj[i].checked)
				{
					if(result!="")
						result=result+",";
					result=result+obj[i].value;
				}
			}	
		}
		else
		{
			if(obj.checked)
				result=obj.value;
		}
	}
	else
	{
		//一个都没有，不用考虑
	}
	return result;
}


/** 检查预订单的2个必填项目 **/
function checkOrderComm()
{
	if(!chkNull(frm.name.value,"联系人"))
	{
		return false;
	}
	else if(!chkNull(frm.phone.value,"联系电话"))
	{
		return false;
	}
	return true;
}

function checkTripOrder(id)
{
	if(!chkNull(eval("frm"+id+".name.value"),"联系人"))
	{
		return false;
	}
	else if(!chkNull(eval("frm"+id+".phone.value"),"联系电话"))
	{
		return false;
	}
	return true;
}

/**function showCtiy(province,city,p_field,f_field)
{
	if(province==-1) //初始化,需要根据city来寻找province
	{
		for(var i=0;i<cities.length;i++)
		{
			for(var j=0;j<cities[i].length;j++)
			{
				if(cities[i][j]==city)
				{
					province=i;
					break;
				}
			}
		}
	}
	else  //说明传递过来的是值，不是selectedIndex，需要进行转换
	{
		for(var i=0;i<cities.length;i++)
		{
			if(cities[i][1]==province)
			{
				province=i;
				break;
			}
		}
	}

	var oSelect = document.all(f_field);
	var strHtml = "<select id='" +f_field+ "' name='"+f_field+"'  class='input_select'></select>";
	oSelect.outerHTML = strHtml;

	for(var i=0;i<cities.length;i++)
	{
		 document.all(p_field).options[i] = new Option();
		 document.all(p_field).options[i].text = cities[i][0];
		 document.all(p_field).options[i].value = cities[i][1] ;
	}
	document.all(p_field).selectedIndex = province;

	var selectedCity=0;

		 document.all(f_field).options[0] = new Option();
		 document.all(f_field).options[0].text = "选择";
		 document.all(f_field).options[0].value = -1;

	for(var i=0;i<cities[province].length-2;i=i+2)
	{
		 document.all(f_field).options[i/2+1] = new Option();
		 document.all(f_field).options[i/2+1].text = cities[province][i+2];
		 document.all(f_field).options[i/2+1].value = cities[province][i+3];
		 if(cities[province][i+3]==city)
		 {
			 selectedCity=i/2+1;
		 }
	}
	document.all(f_field).selectedIndex = selectedCity;
	
	if(document.all.areaproid)
	{
		if(document.all.areaproid.value=="1") 
			document.all.divAreaType.style.display=""; 
		else
			document.all.divAreaType.style.display="none"; 
	}
}

function getCityNameById(id)
{
	for(var i=0;i<cities.length;i++)
	{
		for(var j=0;j<cities[i].length;j++)
		{
			if(cities[i][j]==id)
			{
				return cities[i][j+1];
			}
		}
	}
}

**/
/** 删除图片提示操作 **/
function deleteImg(contentid,imgdiv,type)
{
	var tUrl="img_do.php?contentid="+contentid+"&type="+type;
	if(window.confirm("确认要删此文件么？如果还需要上传，请直接使用上传功能会自动覆盖此文件\n此功能会弹出窗口，请关闭对此域名的禁止弹窗的限制。"))
	{
		frm["img_"+type].value="";
		document.all(imgdiv+"").style.display="none";
		window.open(tUrl,"_nWd");
	}
	
}

function ImgOver(n)
{
	window.document.getElementById(n).style.verticalAlign = 'top';
}
function ImgOut(n)
{
	document.getElementById(n).style.verticalAlign = 'bottom'; 
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


var currentDivId=new Array();
function addDiv(i,divId)
{
	currentDivId[i]=divId;
}
function changeDiv(i,d,agif,bgif)
{	
	document.getElementById('Td_'+i+"_" + currentDivId[i]).background = "../images/"+agif;
	document.getElementById('Div_'+i+"_" + currentDivId[i]).style.display = 'none';
	document.getElementById('Td_'+i+"_" + currentDivId[i]).style.fontWeight = "normal";


	document.getElementById('Td_'+i+"_" + d).background = "../images/"+bgif;
	document.getElementById('Div_'+i+"_" + d).style.display = 'block';
	document.getElementById('Td_'+i+"_" + d).style.fontWeight = "bold";

	currentDivId[i]=d;
}


//检查搜索框
function checkSearch()
{
	if(!chkNull(frmSearch.q.value,"搜索条件"))
	{
		return false;
	}
	return true;
}

//检查评论提交
function checkComment()
{
	if(!isNull(frmComment.name.value))
	{
		window.alert("必须是登陆用户才可以评论");
		return false;
	}
	else if(!isNull(frmComment.body.value))
	{
		window.alert("评论内容不可以为空");
		return false;
	}
	else if(getRealLength(frmComment.body.value)>1000)
	{
		window.alert("评论内容不可以超过500个汉字（1000个英文字母）");
		return false;
	}
	return true;
}


//根据目标跳转
var waitFlag=0;
function goUrl(url,seconds)
{
	if(waitFlag==0)
	{
		waitFlag=1;
		setTimeout("goUrl('"+url+"',"+seconds+")");
	}
	else
	{
		location.href=url;
	}
}




var showAuthimg = false;
function startComment()
{
	document.all.commentdiv.style.display="";
	if(showAuthimg==false)
	{
		showAuthimg=true;
		document.all.authimg.src="../tools/authimg.php";
	}

}
function checkComment()
{

	if(frmComment.c_userid.value==""||frmComment.c_userid.value=="0")
	{
		window.alert("只有登陆用户才可以进行留言");
		return false;
	}
	else if(!chkNull(frmComment.body.value,"留言/评论内容"))
	{
		return false;
	}
	else if(!chkNull(frmComment.imgkey.value,"验证码"))
	{
		return false;
	}
	else if(getRealLength(frmComment.body.value)>1000)
	{
		window.alert("留言/评论内容不能过长,请小于500个汉字");
		return false;
	}
}