function chkBEMail(vEmail){
	var indexofat=vEmail.indexOf("@");
	if(indexofat == -1) return false;
	var domain=vEmail.substr(indexofat,vEmail.length);
	if(domain.indexOf(".") == -1) return false;
	if(vEmail.length < 6)
		return false;
	return true;
}

// 字符校验
function chkInput(obj,Value,Length){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(obj.value.length < Length){
		alert("“"+Value+"” 字段不能短于 "+ Length+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	return true;
}

function chkInput(obj,Value,Length,MaxLength){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(obj.value.length < Length){
		alert("“"+Value+"” 字段不能短于 "+ Length+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	if(obj.value.length > MaxLength){
		alert("“"+Value+"” 字段不能长于 "+ MaxLength+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	return true;
}


// Select 列表校验
function chkSelect(obj,Value){
	//if(obj != "[object]"){return false;}
	if(obj.value == "0" || obj.value == ""){
		alert("您没有选中 “"+Value+"” 内容。\n\n请您选中后再提交。");
		obj.focus();return false;
	}
	return true;
}

// EMail 校验
function chkEMail(obj,Value,Length){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(!chkBEMail(obj.value)){
		alert("电子邮件的格式不对。\n\n请重新输入！");
		obj.focus();obj.select();return false;
  	}
	if(obj.value < Length){
		alert("“"+Value+"” 字段不能短于 "+Length+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	return true;
}

function chkEMail(obj,Value,Length,MaxLength){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(!chkBEMail(obj.value)){
		alert("电子邮件的格式不对。\n\n请重新输入！");
		obj.focus();obj.select();return false;
  	}
	if(obj.value < Length){
		alert("“"+Value+"” 字段不能短于 "+Length+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	if(obj.value > MaxLength){
		alert("“"+Value+"” 字段不能长于 "+MaxLength+" 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	return true;
}

// 数字校验
function chkNumber(obj,Value,MaxVal,MinVal){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(isNaN(obj.value)){
		alert("“"+Value+"” 必须为数字。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	var sCode = obj.value;
	while(sCode.indexOf("0") == 0){
		sCode = sCode.substring(1,sCode.length)
	}
	var iVal = parseInt(sCode);
	if(iVal < parseInt(MinVal) || iVal > parseInt(MaxVal)){
		alert("“"+Value+"” 的范围必须在 "+MinVal+"~"+MaxVal+" 之间。\n\n请您确定后再提交。");
		obj.focus();obj.select();return false;
	}
	return true;
}



function IsNumber(obj,Value){
	//if(obj != "[object]"){return false;}
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” ,内容必须为数字。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(isNaN(obj.value)){
		alert("“"+Value+"” 必须为数字。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	return true;
}

/**整数校验**/
function IsInteger(obj,Value)
{
	if(obj.value == ""){
		alert("您没有填写 “"+Value+"” ,内容必须为整数。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(obj.value.indexOf(".") != -1 || isNaN(obj.value) ){
		alert("“"+Value+"” 必须为整数。\n\n请您修改后再提交。");
		obj.focus();obj.select();return false;
	}
	return true;
}


// 时间校验
function chkDate(oYear,oMonth,oDay){
	if(!chkNumber(oYear,"年份",2000,1900))
		return false;
	if(!chkNumber(oMonth,"月份",12,1))
		return false;
	if(!chkNumber(oDay,"日期",31,1))
		return false;
	return true;
}

// 取得 Radio 的选定值
function GetRadioValue(oMain,RadioName){
	var sValue = "";
	//oRadios = oMain.all(RadioName);
	oRadios = oMain.elements[RadioName];
	if(oRadios.length)
	{
		oRadios = oMain.elements[RadioName];
		for(var i=0;i<oRadios.length;i++){
			if(oRadios[i].checked){
				sValue = oRadios[i].value;
				break;
			}
		}
	}else
	{
		if(oRadios.checked)
		{
			sValue = oRadios.value;
		}
	}
	return sValue;
}

// 检测 Radio 是否选定
function chkRadio(form,radioName)
{
	var oRadios = form.elements[radioName];
	if(oRadios.length==0)
	{
		return oRadios.checked;
	}
	for(var i=0;i<form.elements[radioName].length;i++)
	{
		if(form.elements[radioName][i].checked)
		{
			return true;
		}
	}
	return false;
}

// 检测 CheckBox 是否选定
function chkCheckBox(form,checkBoxName)
{
	var checkBoxes = form.elements[checkBoxName];
	if(checkBoxes.length==0)
	{
		return checkBoxes.checked;
	}
	for(var i=0;i<checkBoxes.length;i++)
	{
		if(checkBoxes[i].checked)
		{
			return true;
		}
	}
	return false;
}

// 移除 checkBox的checked
function removeChecked(form,checkBoxName)
{
	var checkBoxes = form.elements[checkBoxName];
	if(checkBoxes.length==0 && checkBoxes.checked)
	{
		checkBoxes.checked = false;
	}
	for(var i=0;i<checkBoxes.length;i++)
	{
		if(checkBoxes[i].checked)
		{
			checkBoxes[i].checked = false;
		}
	}
}


// 电话号码校验
function chkCodeNum(obj,Value){
	//if(obj != "[object]"){return false;}
	var sCode = obj.value;
	if(sCode == ""){
		alert("您没有填写 “"+Value+"” 内容。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	if(sCode.length < 6){
		alert("“"+Value+"” 的内容必须长于 6 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	if(sCode.length > 24){
		alert("“"+Value+"” 的内容必须短于于 24 个字符。\n\n请您重新填写。");
		obj.focus();obj.select();return false;
	}
	while(sCode.indexOf("-") != -1){
		sCode = sCode.replace("-","");
	}
	while(sCode.indexOf(" ") != -1){
		sCode = sCode.replace(" ","");
	}
	while(sCode.indexOf("ext") != -1){
		sCode = sCode.replace("ext.","");
		sCode = sCode.replace("ext","");
	}
	if(isNaN(sCode)){
		alert("“"+Value+"” 必须为数字。\n\n请您填写后再提交。");
		obj.focus();obj.select();return false;
	}
	return true;
}
//去除form表单中的文本输入值的空值
function trim_para(form)
{
	for (var j = 0; j < form.elements.length; j++)
	{
		if (form.elements[j].type == 'text') 
		{
			form.elements[j].value = trim(form.elements[j].value);
		}
	}
}

// 去空格
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   /*
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   */
   return retValue; // Return the trimmed string back to the user
}

// 去所有空格
function trimAll(inputString) 
{
	while(inputString.indexOf(" ")!=-1)
	{
		inputString = inputString.replace(" ","");
	}
	return inputString;
}

//检查日期
function checkdate(objName)
{
        var datefield = objName;
        if (chkdate(objName) == false)
        {
                datefield.select();
                alert("That date is invalid.  Please try again.");
                datefield.focus();
                return false;
        }
        else
        {
                return true;
           }
}

function chkdate(objName) {

var strDate = objName.value;
if (strDate.length < 1)
{
        return true;
}
if (strDate.length > 9)
{
        var strMonth = strDate.substr(5, 2);
        var strDay = strDate.substr(8, 2);
        var strYear = strDate.substr(0,4);
}
else
{
        return false;
}
var intday = parseInt(strDay,10);
if (isNaN(intday))
{
        return false;
}
intMonth = parseInt(strMonth,10);
if (isNaN(intMonth))
{
        return false;
}
intYear = parseInt(strYear,10);
if (isNaN(intYear))
{
        return false;
}
if (intMonth>12 || intMonth<1)
{
        return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
{
        return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
{
        return false;
}
if (intMonth == 2)
{
        if (intday < 1)
        {
                return false;
        }
        if (LeapYear(intYear) == true)
        {
                if (intday > 29)
                {
                        return false;
                }
        }
        else
        {
                if (intday > 28)
                {
                        return false;
                }
        }
}
return true;
}

function LeapYear(intYear)
{
        if (intYear % 100 == 0)
        {
                if (intYear % 400 == 0)
                {
                        return true;
                }
        }
        else
        {
                if ((intYear % 4) == 0)
                {
                        return true;
                }
        }
        return false;
}

function doDateCheck(from, to)
{
        if (Date.parse(from.value) <= Date.parse(to.value))
        {
                alert("The dates are valid.");
        }
        else
        {
                if (from.value == "" || to.value == "")
                        alert("Both dates must be entered.");
                else
                        alert("To date must occur after the from date.");
           }
}
