/**
 * 功　　能：JavaScript进行提交表单验证。
 * 功能说明：验证表单格式合法性，并且能够除错返回错误消息[未完成]。
 * 试　　用：处于试用阶段，尚未调整、优化。
 *
 * 创建日期：(2004-03-28)
 * @author：牛晓娜
 * 修改日期：(2004-09-13)
 * @author：牛晓娜
 * @copyRight：
 *
 * @para  obj :表单域对象，包括text、textarea、select三种
 * @return false/true
 *
 * 表单格式说明:
 * HTML应用：附加扩展属性用法
 * <input type="text" value="" name="" types="" caption="" minsize="" maxsize="" isnull="" minvalue="" maxvalue="">
 * <input type="password" value="" name="" types="" caption="" minsize="" maxsize="" isnull="" minvalue="" maxvalue="">
 * <select name="" caption="" isnull=""></select>
 * <textarea name="" caption="" minsize="" maxsize="" isnull=""></textarea>
 *
 * Form Objects Types:
 * 0:any
 * 1:number only
 * 11:Integer number only
 * 2:date only
 * 3:Email Style
 * 4:PersonalCode
 * later:not support now
 *
 *
 * Motheds:
 * checkForm(obj);
 * checkTextBox(obj);
 * checkTextArea(obj);
 * checkSelect(obj);
 *
 * checkNull(obj);
 * checkTypes(obj);
 * checkLength(obj);
 * checkValue(obj);
 * checkDateValue(obj);
 * checkEmail(value);
 * checkDate(value);
 *
 * Example:
 *
 * (none)
 *
 */


//搜索表单obj中所有可用的输入，并逐一判断类型，对其中特定类型操作
function checkForm(obj)
{
	elementsCount = obj.elements.length;
	items = obj.elements;
	for (i = 0;i<elementsCount;i++)
		{
			if (items[i].type=="text" || items[i].type=="password")
			{
				if (!checkTextBox(items[i])){	items[i].focus();return false;}
			}
			if (items[i].type=="select-one")
			{
				if (!checkSelect(items[i])){items[i].focus();return false;}
			}
			if (items[i].type=="textarea")
			{
				if (!checkTextArea(items[i]))	{items[i].focus();return false;}
			}
			//Here:more Form Objects types will be check;
		}
	return true;
}


//检验文本框中的的输入是否合法
function checkTextBox(obj)
{
	value = obj.value;
	if (obj.types == null || isNaN(obj.types)) 
	{
	obj.types="0";
		//alert("["+obj.caption+"]的格式设置有误，可能会导致不可预见的问题!");
		//return false;
	}
	if (obj.isnull == null || isNaN(obj.isnull)) 
		obj.isnull="1";//设置是否为空 0:否 1:是
	//if (obj.types == null) 
		//obj.types="0";	//设置输入的类型
	if (obj.minsize == null || isNaN(obj.minsize)) obj.minsize = "0";	//设置输入框的最小长度
	if (obj.maxsize == null || isNaN(obj.maxsize)) obj.maxsize = "999";	//设置输入框的最大长度
	if (obj.minvalue == null || isNaN(obj.minvalue)) obj.minvalue = "-99999999";	//设置输入数字最小值
	if (obj.maxvalue == null || isNaN(obj.maxvalue)) obj.maxvalue = "99999999";//设置输入数字最大值
	if (obj.caption == null) obj.caption = "Noname TextBox";//设置输入的标识名称
	//Check Types
	if (!checkNull(obj)) return false;
	if (obj.isnull =="0" || (obj.isnull=="1" && obj.value.length>0))
	{
	if (!checkTypes(obj))	return false;
	if (!checkLength(obj)) return false;
	}
	return true;
}

//检验选择是否合法
function checkSelect(obj)
{
	if (obj.caption == null) obj.caption = "Noname Select";//设置输入的标识名称
	if (obj.isnull != null && obj.isnull == "0" && obj.value.length <= 0)
	{
		alert("["+obj.caption+"]必须要选择！");
		return false;
	}
	return true;
}


//检验文本域是否合法
function checkTextArea(obj)
{
	if (obj.isnull == null || isNaN(obj.isnull)) obj.isnull="1";
	if (obj.minsize == null || isNaN(obj.minsize)) obj.minsize = "0";	//设置输入框的最小长度
	if (obj.maxsize == null || isNaN(obj.maxsize)) obj.maxsize = "999";	//设置输入框的最大长度
	if (obj.caption == null) obj.caption = "Noname TextArea";//设置输入的标识名称
	if (!checkNull(obj)) return false;
	if (obj.isnull =="0" || (obj.isnull=="1" && obj.value.length>0))
	{
	if (!checkLength(obj)) return false;
	}
	return true;
}


/*
 * 功能：检查对象是否为空
 * @para
 * @return
 */
function checkNull(obj)
{
	if (obj.isnull == "0" && trim(obj.value).length <= 0)
	{
		alert("["+obj.caption+"]不能为空！");
		return false;
	}
	return true;
}


/*
 * 功能：检查对象类型，并对格式合法性判断
 * @para
 * @return
 */
function checkTypes(obj)
{
/* Input Type
 * 0:any
 * 1:number only
 * 11:Integer number only
 * 2:date only
 * 3:Email Style
 * later:not support now
*/
switch(obj.types)
	{
	case "0":
		break;
	case "1":
		if (isNaN(obj.value))
			{
				alert("["+obj.caption+"]输入的数字格式不对！")
				return false;
			}
		break;
	case "11":
		if (!checkInteger(obj.value))
		{
		alert("["+obj.caption+"]输入的整数格式不对！");
		return false;
		}
		break;
	case "2":
		if (!checkDate(obj.value))
		{
		alert("["+obj.caption+"]日期格式不对！");
		return false;			
		}
		break;
	case "3":
		if (!checkEmail(obj.value))
		{
		alert("["+obj.caption+"]Email地址格式不对！");
		return false;			
		}
		break;
	case "4":
		if (!checkPCode(obj.value))
		{
		alert("["+obj.caption+"]身份证编号格式不对！");
		return false;			
		}
		break;
	default:
		break;
	}
	return true;
}


/*
 * 功能：检查对象内容长度是否合法
 * @para
 * @return
 */
function checkLength(obj)
{
	if (trim(obj.value).length<obj.minsize)
		{
		alert("["+obj.caption+"]的长度不能少于"+obj.minsize+"字符！");
		return false;
		}
	if (trim(obj.value).length>obj.maxsize)
	{
		alert("["+obj.caption+"]的长度不能多于"+obj.maxsize+"字符！");
		return false;
	}
	return true;
}


/*
 * 功能：检查对象是否为Email类型
 * @para
 * @return
 */
function checkEmail(n)
{
str = n;
    if (str.length > 0){
    	var a=str.indexOf("@")+1;
    	var p=str.lastIndexOf(".")+1;    
	
    	if ((str.indexOf("'") >= 0)||(str.indexOf('"') >= 0)||(a<2) || (p<1)||(p<a+2)||(str.length==p)){
    		//alert("无效的输入！");
             return false;
    	}    
    	else	
    		return true; 
    }
    return true;
}


/*
 * 功能：检查对象是否为日期类型
 * @para
 * @return
 */
function checkDate(d)
{
//感谢有现成的代码，下面是COPY来的~~@_@
	if (d.length > 0){
	var first,second,yy,mm,dd;
	
	if(d.indexOf("/")!=-1)
	{
		first=d.indexOf("/");
		second=d.lastIndexOf("/");
		if(second==first) return false;
		yy=parseInt(d.substring(0,first));
		if ( d.substr(first + 1, 1) == '0' )
			mm=parseInt(d.substring(first+2,second));
		else
			mm=parseInt(d.substring(first+1,second));
		if ( d.substr(second + 1, 1) == '0' )
			dd=parseInt(d.substring(second+2,d.length));
		else
			dd=parseInt(d.substring(second+1,d.length));
		if (isNaN(yy)) { //Error Year Format
			return false;			
		}
		if (yy < 70) 
			yy += 2000;
		else if (yy < 100 && yy >= 70)
			yy += 1900;
		//if( yy <= 1970 || yy >= 2069) return false;
		if (isNaN(mm) || mm < 1 || mm > 12) { //Error Month Format
			return false;
		}
		if (isNaN(dd) || dd < 1 || dd > 31) { //Error Month Format
			return false;
		}
		d = new Date(yy, mm - 1, dd); //Test the Date
		if (isNaN(d)) { //Error Date Format
			return false;
		}
		if (d.getMonth() != mm - 1 || d.getDate() != dd) { //invalid date such as '1999/02/29' and '1999/04/31'
			return false;
		}
		return d.toLocaleString();  //Return the Date in parsed format
	}
	else if(d.indexOf("-")!=-1)
	{		
		first=d.indexOf("-");
		second=d.lastIndexOf("-");
		if(second==first) return false;
		yy=parseInt(d.substring(0,first));
		if ( d.substr(first + 1, 1) == '0' )
			mm=parseInt(d.substring(first+2,second));
		else
			mm=parseInt(d.substring(first+1,second));
		if ( d.substr(second + 1, 1) == '0' )
			dd=parseInt(d.substring(second+2,d.length));
		else
			dd=parseInt(d.substring(second+1,d.length));
		if (isNaN(yy)) { //Error Year Format
			return false;			
		}
		if (yy < 70) 
			yy += 2000;
		else if (yy < 100 && yy >= 70)
			yy += 1900;
		//if( yy <= 1970 || yy >= 2069) return false;
		if (isNaN(mm) || mm < 1 || mm > 12) { //Error Month Format
			return false;
		}
		if (isNaN(dd) || dd < 1 || dd > 31) { //Error Month Format
			return false;
		}
		d = new Date(yy, mm - 1, dd); //Test the Date
		if (isNaN(d)) { //Error Date Format
			return false;
		}
		if (d.getMonth() != mm - 1 || d.getDate() != dd) { //invalid date such as '1999/02/29' and '1999/04/31'
			return false;
		}
		return d.toLocaleString();  //Return the Date in parsed format
	}
	else
		return false;
    }
    return true;
}


/*
 * 功能：检查对象最大最小值是否合法
 * @para
 * @return
 */
function checkValue(obj)
{
	if (Number(obj.value)<Number(obj.minvalue))
	{
		alert("["+obj.name+"]的值不能小于"+obj.minvalue+"!");
		return false;
	}
	if (Number(obj.value)>Number(obj.maxvalue))
	{
		alert("["+obj.name+"]的值不能大于"+obj.maxvalue+"!");
		return false;
	}			
	return true;
}


/*
 * 功能：检查对象日期最大最小值是否合法
 * @para
 * @return
 */
function checkDateValue(n_obj,v_obj)
{
	if (!checkDate(n_obj.value))
		{
		alert("["+n_obj.caption+"]日期格式不对！");
		return false;			
		}
	if (!checkDate(v_obj.value))
		{
		alert("["+v_obj.caption+"]日期格式不对！");
		return false;			
		}

	date1 = toDate(n_obj.value);
	date2 = toDate(v_obj.value);
	alert(date1);
	alert(date2);
	if (date1 < date2)
	{
		alert("日期["+n_obj.caption+"]不能小于["+v_obj.caption+"]的日期！");
		n_obj.focus();
		return false;
	}
	return true;
}


//test a input value by number styles
function checkNum(n)
{
var rext = /^(\s*[-]?([0123456789]+)|([0123456789]*[.]{1}[0123456789]*)\s*)$/;
if (!rext.test(n)) return false;
return true;
}

/*
 * 功能：检查对象是否整数
 * @para
 * @return
 */
//test a input value by integer number styles
function checkInteger(n){
var rext = /^(\s*[-]?[0123456789]+\s*)$/;
if (!rext.test(n)) return false;
return true;
}


function toDate(s)
{
	if (checkDate(s))
	{
		p = s.indexOf("-");
		year = s.substring(0,p);
		s1 = s.substring(p+1);
		p = s1.indexOf("-");
		month=s1.substring(0,p);
		day = s1.substring(p+1);
		return new Date(year,parseInt(month)-1,day)
	}
	else 
		return new Date();
}

//检查身份证号
function checkPCode(s)
{
    var s=trim(s);
	var rex = /^(\s*[0-9]+\s*)$/;
	var rexs = /^(\s*([0-9]+)|([a-z]+)|([A-Z]+)\s*)$/;
	if (trim(s).length == 15 )	 { if (rex.test(s)) return true;	}
	else if (trim(s).length ==18) { if (rex.test(s.substring(0,17)) && rexs.test(s.substring(17))) return true;	}
	return false;
}


/******************************************************
*日期从y,m,d三个select中选出来，并输入到
*out里面(text,hidden)
*先面三个为显示年月日列表
*******************************************************/
function showDate(y,m,d,out)
{
id=y.options[y.selectedIndex].value;
id2=m.options[m.selectedIndex].value;
if(id2==2){
if (id%4 != 0)
//非润年，2月28日
{d.options.length = 29;}
else 
{d.options.length = 30;
d.options[29].value = "29";
d.options[29].text = "29";}
}
if(id2==1 || id2==3 || id2==5 || id2==7 || id2==8 || id2==10 || id2==12)
{d.options.length = 32;
d.options[29].value = "29";
d.options[29].text = "29";
d.options[30].value = "30";
d.options[30].text = "30";
d.options[31].value = "31";
d.options[31].text = "31";}
if(id2==4 || id2==6 || id2==9 || id2==11)
{d.options.length = 31;
d.options[29].value = "29";
d.options[29].text = "29";
d.options[30].value = "30";
d.options[30].text = "30";}

id3=d.options[d.selectedIndex].value;
out.value=id+"-"+id2+"-"+id3;
}


//*******显示年列表1900-今年+10***//
function showYear(year)
{
//if (year == null || year=="") year = new Date().getYear();
if (year == null || year=="") year = "";
//for (i = 1900;i<=new Date().getYear();i++)
document.write("<option value=''>年</option>");
for (i = 1900;i<2050;i++)
{se = "";
if (i == year) se = " selected";
document.write("<option value='"+i+"'"+se+">"+i+"</option>");}
}

//*******显示年列表1980-今年+10*** 牛晓娜改 2003-10-09//
function showYear1(year)
{
//if (year == null || year=="") year = new Date().getYear();
if (year == null || year=="") year = "";
//for (i = 1900;i<=new Date().getYear();i++)
document.write("<option value=''>年</option>");
for (i = 1980;i<2050;i++)
{se = "";
if (i == year) se = " selected";
document.write("<option value='"+i+"'"+se+">"+i+"</option>");}
}

//*******显示月份列表***//
function showMonth(month)
{
//if (month == null ||  month=="") month = new Date().getMonth()+1;
if (month == null ||  month=="") month = "";
document.write("<option value=''>月</option>");
for (i = 1;i<=12;i++)
{se = "";ln = "";
if (i == month) se = " selected";
if (i<10) ln="0"; else ln = "";
document.write("<option value='"+ln+i+"'"+se+">"+ln+i+"</option>");}
}


//*******显示日期列表//
function showDay(day)
{
//if (day == null || day == "") day = new Date().getDate();
if (day == null || day == "") day = "";
document.write("<option value=''>日</option>");
for (i = 1;i<=31;i++)
{se = "";ln="";
if (i == day) se = " selected";
if (i<10) ln="0";
else ln = "";
document.write("<option value='"+ln+i+"'"+se+">"+ln+i+"</option>");}
}


function trim(sString)
{
var strTmp ;

strTmp = JHshRTrim(JHshLTrim(sString)) ;

return strTmp ;
} 

function JHshRTrim(sString)
{ 
var sStr,i,sResult = "",sTemp = "" ;

// if (sString.length == 0) { return "" ;} // 参数sString是空串

sStr = sString.split("");
for (i = sStr.length - 1 ; i >= 0 ; i --)  // 将字符串进行倒序
{ 
sResult = sResult + sStr[i]; 
}
sTemp = JHshLTrim(sResult) ; // 进行字符串前空格截除

if (sTemp == "") { return "" ; }

sStr = sTemp.split("");
sResult = "" ;
for (i = sStr.length - 1 ; i >= 0 ; i--) // 将经处理后的字符串再进行倒序
{
sResult = sResult + sStr[i];
}
return sResult ;
} 

function JHshLTrim(sString)
{ 
var sStr,i,iStart,sResult = "";

sStr = sString.split("");
iStart = -1 ;
for (i = 0 ; i < sStr.length ; i++)
{
if (sStr[i] != " ") 
{
iStart = i;
break;
}
}
if (iStart == -1) { return "" ;}    //表示sString中的所有字符均是空格,则返回空串
else { return sString.substring(iStart) ;}
}
 

