/**
 *	模块代码：		jfunction.js
 *	模块名称:		公用处理类
 *	引用程序类:		
 *	功能描述:		
 */


 /**
 *送入一个String，除去左右空格返回字符串。
 */
function javaTrim(string)
{
	var length1, i, j;
	var string1 = "";
	
	length1 = string.length;
	for (i = 0 ; i < length1 ; i++)
	{  //除去左边空格
		if(string.charAt(i) != " ")
		{//除去左边空格后
			for (j = i ; j < length1 ; j++) 
				string1 = string1 + string.charAt(j);
				break;	
		}
	}	
	length1 = string1.length;
	string = string1;
	string1 = "";
	for (i = length1 - 1 ; i >= 0 ; i--) 
	{  //除去右边空格
		if(string.charAt(i) != " ") 
		{//除去右边空格后
			for (j = 0 ; j <= i ; j++) 
				string1 = string1 + string.charAt(j);
				break;	
		}
	}
	string = string1;	
	return(string)	
}

 


/**
 * 检查Email是否合法
 * 返回true为正确
 */
function isEmail(string)

{
	var string1="";
	var len=0;
	
	  
	string1=javaTrim(string.value);
	len=string1.length; 
	if(string1.length!=0)
	{
		if (string1.indexOf("@",1)==-1||string1.indexOf(".",1)==-1||string1.length<7)
		{
			alert("电子邮箱的格式不对，请重新填写！");
			tryToFocus(string);
			return false;
		}

		if (string1.charAt(len-1)=="."||string1.charAt(len-1)=="@")
		{
			alert("电子邮箱的格式不对，请重新填写！");
			tryToFocus(string);
			return false;
		}    
	}
	return true;
}


/**
 *  检查是否闰年
 *  return false 不是闰年，true闰年
 */
function isLeapYear(year)
{
	if(year%4==0 && year%100!=0 || year % 400 == 0) return true;
	return false;
}

/**
 * 是否正确的日期
 * obj : text object of input
 * return true 日期正确，false 日期不正确
 */
function isDate(obj)
{
	if (obj.value=="") return true;

	if (obj.value.length!=8)
	{
		alert ('请输入正确的日期格式!');
		tryToFocus(obj);
		return false;
	}

	if (isValidDate(obj.value.substring(0,4),obj.value.substring(4,6),obj.value.substring(6,8)))
	{
		return true;
	}
	else
	{
	tryToFocus(obj);
	return false;
	}	
}

/**
 * 是否正确的日期
 * text : text object of input
 * return true 日期不正确，false 日期正确
 */
function notValidateDate(text){
  if(isDate(text)) return false;
  return true;
}

function beforeDate(obj,beforeDate)
{
	if (javaTrim(obj.value)=="") return true;
	if (isDate(obj))
	{
		if (obj.value<beforeDate)
		{
			return true;
		}
		else
		{
		alert ("你输入的日期不能大于当前的时间!")
		tryToFocus(obj);
		return false;
		}
		
	}
	
}
/***
*  比较text1,text2的日期:
*     if (text1 > text2) return true and alert;
*  text1, text2 : text object of input
*  textName1, textName2 : the display name of text object
*/
function largerDate(text1, text2, textName1,textName2)
{ 
	i = compareDates(text1.value.substring(0,4),text1.value.substring(4,6),text1.value.substring(6,8),text2.value.substring(0,4),text2.value.substring(4,6),text2.value.substring(6,8));
	if( i > 0){
		alert (textName1 + "的日期不应该大于"  + textName2 + "的日期" ) ;
		text1.select();
		return true;
	}
	return false;
}
/**
 * isValidDate传送过来的year,month,day组合在一起是否正确的日期函数
 * return false 日期不正确，true日期正确
 */
 
 
function isValidDate( year, month, day )
{
	if(isNaN(year) || isNaN(month) || isNaN(day))
	{
				alert("日期只能输入数字 ！")
				return false
	}
	if((year.length!=4) || (year.substring(0,1)<1) || year.indexOf(".") != -1)
	{
				alert("年份应为4位整数,并第一位大于1 ！")
				return false
	}
  
   
   if(month.indexOf(".") != -1)
   {
		   	alert("月份不应该包括小数点 ！")
			return false
	}
	
	if(day.indexOf(".") != -1)
	{
		   	alert("日期不应该包括小数点 ！")
			return false
	}
   
    year  = Math.floor(year,10);
   month = Math.floor(month,10);
   day   = Math.floor(day,10);
   
	if((month<1) || (month>12)){
		   	alert("月份应该在1-12之间 ！")
			return false
	}
   if (( month==4) || (month==6) || (month==9) || (month==11) )
   { if (( day < 1) || (day > 30) )
     { alert( "日期在1 - 30之间 ！" );
       return false;
     }
   }
   else
   { if ( month!=2 )
     { if ( (day < 1) || (day > 31 ))
       {  alert( "日期在1 - 31之间 ！" );
          return false;
       }
     }
     else
     { // month == 2
       if ( ( year % 100 ) != 0 && (year % 4 == 0) || ( year % 100 ) == 0 && ( year % 400) == 0 )
       { if ( day > 29 )
         {  alert( "日期在1 - 29之间 ！" );
            return false;
         }
       }
       else
       { if ( day > 28 )
         { alert( "日期在1 - 28之间 ！" );
           return false;
         }
       }
     }
   }
 return (true);
}

/**
* 判断数值,是否为浮点数（金额）
* return false错误，true正确
*/
function isMoney(string) 
{
	var length1 , i , j, k, flag = 0;
	var string1="";
   
	string1 = javaTrim(string);
	k = length1 = string1.length;
	
	if ((string1 == "0.00") || (string1 == "0.0") || (string1 == "0.") || (string1 == "0")) 
	{
    alert("错误！金额不能为0，请重新填写。");    
		return (false);
  }
	if (length1 == 0) 
	{
		alert( "不能输入空字符");
		return(false); 
	}
	if (string1.charAt(0)=="0" )
	{
		if (length1 == 1)
		{
	        	alert("金额不能为0，请重新填写！");
	        	return(false);
	  }
	  else
	  {
	  	if (!(string1.charAt(1)=="."))
	  	{
				alert("金额首位不能为0，请重新填写！");
				return(false);
			}				
		}
	}
	j=0;
	for (i = 0 ; i < length1 ; i++) {  //判断每位数字
		if(isNaN(Math.floor(string1.charAt(i),10)))  
		{
			if(string1.charAt(i) != ".")  
			{
				alert( "请输入数值型数据！");					
				return(false); 
			} 
			else  {
				j++;
				if(length1 - i > 3 )
				{
					alert("小数点后只能有两位！");
					return(false);
				}
			}
			if (flag == 0) { 
				k = i;	flag = 1;
			}
			
		}	
	}	

	if (k > 12) {
		alert("金额长度超过限额");
		return false;
	} 

	if(j > 1) {
		alert( "小数点只能有一个!");			
		return false;
	}

	return true;
}

/**
 * checkNo用于判断传送过来的obj是否正确的整数,并小于maxValue,
 * 注意:noObj格式应该是form1.filedname,调用方法if (isInt(form1.fieldname,100))返回真则为数字
 */
function isInt(noObj,maxValue)
{
	if (noObj.value=="")
	{
		return true;
	}

	if (isNaN(noObj.value) || (noObj.value.indexOf("-")>-1))
	{
		alert ("请输入大于0并且小于" + maxValue + "的整数 ！");
		tryToFocus(noObj);
		return false;
	}
	if (isNaN(noObj.value) || (noObj.value.indexOf(".")>-1))
	{
		alert ("请输入大于0并且小于" + maxValue + "的整数 ！");
		tryToFocus(noObj);
		return false;
	}
	strValue=noObj.value;
	for (i=0;i<strValue.length;i++)
	{
		if (strValue.charCodeAt(i,1)!=46 && (strValue.charCodeAt(i,1)<48 || strValue.charCodeAt(i,1)>57))
		{
			alert ("请输入大于0并且小于" + maxValue + "的整数 ！");
			tryToFocus(noObj);
			return false;
		}
	}

		if (Math.floor(noObj.value)<Math.floor(maxValue))
		{
			return true;
		}
		else
		{
		alert ("您输入的数值应该小于 " + maxValue + "！");
		tryToFocus(noObj);
		return false;
		}

}

function isInteger(noObj)
{
	if (noObj.value=="")
	{
		return true;
	}

	if (isNaN(noObj.value) || (noObj.value.indexOf("-")>-1))
	{
		alert ("请输入大于0的整数 ！");
		tryToFocus(noObj);
		return false;
	}
	if (isNaN(noObj.value) || (noObj.value.indexOf(".")>-1))
	{
		alert ("请输入大于0的整数 ！");
		tryToFocus(noObj);
		return false; 
	}
	strValue=noObj.value;
	for (i=0;i<strValue.length;i++)
	{
		if (strValue.charCodeAt(i,1)!=46 && (strValue.charCodeAt(i,1)<48 || strValue.charCodeAt(i,1)>57))
		{
			alert ("请输入大于0整数 ！");
			tryToFocus(noObj);
			return false;
		}
	}


	return true;
}



/*
 * isFloat用于判断传送过来的obj是否正确的实数,并小于maxValue,小数长度不超过decimalNo
 * 注意:noObj格式应该是form1.filedname,调用方法if (isFloat(form1.fieldname,100,1))返回真则为数字
 */
function isFloat(noObj,maxValue,decimalNo)
{
	return isFloatVl(noObj,maxValue,decimalNo,0);
} 

function isFloatEq(noObj,maxValue,decimalNo)
{
	return isFloatVl(noObj,maxValue,decimalNo,1);
} 

 
function isFloatVl(noObj,maxValue,decimalNo,isEq)
{
	if (noObj.value=="")
	{
		return true;
	}

	if (isNaN(noObj.value) || (noObj.value.indexOf("-")>-1))
	{
		alert ("请输入数字 ！");
		tryToFocus(noObj);
		return false;
	}
	
	strValue=noObj.value;
	for (i=0;i<strValue.length-1;i++)
	{
		if (strValue.charCodeAt(i,1)!=46 && (strValue.charCodeAt(i,1)<48 || strValue.charCodeAt(i,1)>57))
		{
			alert ("请输入数字！");
			tryToFocus(noObj);
			return false;
		}
	}
	if(isEq==0)
	{

		if (Math.floor(noObj.value)>=maxValue)
		{
			alert ("您输入的数值应当小于"+ maxValue + " ！");
			tryToFocus(noObj);
			return false;
		}
	}else{
		if ((Math.ceil(noObj.value)>maxValue))
		{
			alert ("您输入的数值应当小于"+ maxValue + " ！");
			tryToFocus(noObj);
			return false;
		}	
	}

	if(noObj.value.indexOf(".")!=-1){
		if((noObj.value.length-noObj.value.indexOf(".")-1)>decimalNo)
		{		
			alert("本栏只能输入"+decimalNo+"位小数 ！");
			tryToFocus(noObj);
			return false;
		}
	}

		return true;
}



/**
 * clearSpaceKey在onkeyPress中调用,叛断当前按下的键是否含有空格,
 * 调用方法onKeyPress="clearSpaceKey()"
 */
function clearSpaceKey(){
	if((event.keyCode == "32")){
		event.returnValue=false
		alert("不能输入空格 ！")
	}
}

/**
 * onlyNumPoint在onkeyPress中调用,叛断当前按下的键是否整数,调用方法onKeyPress="onlyNumPoint()"
 */
function onlyNumPoint(){
	if(!((event.keyCode >"47" )&& (event.keyCode < "58") || (event.keyCode == "46")) && event.keyCode!=13){
		event.returnValue=false
		alert("只能输入数字与小数点 ！")
	}
}

/*
 * onlyNum在onkeyPress中调用,叛断当前按下的键是否实数,调用方法onKeyPress="onlyNum()"
 */
function onlyNum(){
	if(!((event.keyCode >"47" )&& (event.keyCode < "58")) && event.keyCode!=13){
		event.returnValue = false
		alert("只能输入数字！")
	}
}

/**
 * filterDoubleMarksSpace在onkeyPress中调用,叛断当前按下的键是否含有双引号,
 * 调用方法onKeyPress="filterDoubleMarksSpace()"
 */
function filterDoubleMarksSpace(){
	if(event.keyCode == "34"){
		event.returnValue = false
		alert("不能输入双引号！")
	}
}


function isCheckedSelectNo(objCheckbox)
	{
		SelectNo="";
		if (! objCheckbox)
		{
			alert ("现在没有任何记录可供下一步操作!")
			return ""
		}
		if (objCheckbox.length>1)
		{
			
			for (i=0;i<objCheckbox.length;i++)
			{
				if (objCheckbox[i].checked == true)
				{
					SelectNo=objCheckbox[i].value;
				}
			}
		}
		else
		{
			if (objCheckbox.checked == true)
			{
				SelectNo=objCheckbox.value;			
			}
		}
		if (SelectNo=="")
		{
			alert ("请选择一个记录,再进行下一步的操作!");
			return "";
		}	
		else
		{
			return SelectNo;
		}
}	




/**
 * isChecked叛断传送过来的单选框或复选框是否有被选中,isChecked(form1.filedname)
 * 调用方法if (isChecked(form1.fieldname))返回真则用户有选中一个单选框
 */
function isChecked(objCheckbox)
	{
		isCheck=false;
		if (! objCheckbox)
		{
			alert ("现在没有任何记录可供下一步操作!")
			return false
		}
		if (objCheckbox.length>1)
		{
			
			for (i=0;i<objCheckbox.length;i++)
			{
				if (objCheckbox[i].checked == true)
				{
					isCheck=true;
				}
			}
		}
		else
		{
			if (objCheckbox.checked == true)
			{
				isCheck=true;			
			}
		}
		if (isCheck==false)
		{
			alert ("请选择一个记录,再进行下一步的操作!");
			return false;
		}	
		else
		{
			return true;
		}
}	

/**
* 检查密码验证是否正确
*/	
function isValidatePasswd(passwd,passwd2)
{
	if (passwd.value.length < 4) {
		alert("密码长度至少为四位");
		tryToFocus(passwd);
		return true;
	}

	if (passwd.value.length > 10) {
		alert("密码长度不能大于十位");
		tryToFocus(passwd);
		return true;
	}
	
	if (passwd.value != passwd2.value){
	    alert("两次输入的密码不同，请重新输入密码");
		passwd.value="";
		passwd.value="";
		tryToFocus(passwd);
		return true;
	}
	

		
	
	return false;
}

/**
* 检查密码输入是否正确
*/	
function isPassword(passwd)
{
	if (passwd.value.length < 4) {
		alert("密码长度至少为四位");
		tryToFocus(passwd);
		return true;
	}

	if (passwd.value.length > 10) {
		alert("密码长度不能大于十位");
		tryToFocus(passwd);
		return true;
	}
		
	return true;
}

/***
*  比较日期:
*     return 1 : if date1 is later than date2
*     return 0 : if date1 is equal to date2
*     return -1: if date1 is earlier than date2
*/
function compareDates(yy1, mm1, dd1, yy2, mm2, dd2)
{ 
	if(yy1 > yy2) {	return 1 }
	else if(yy1 < yy2) { return -1 }
	else if(yy1 == yy2){
	    if(mm1 > mm2) { return 1 }
	    else if(mm1 < mm2) { return -1}
	    else if(mm1 == mm2) {
	    	if(dd1 > dd2) {return 1}
	    	else if(dd1 < dd2) {return -1}
	    	else if(dd1 == dd2) { return 0 }
	    }
	}
}

/**
 * 根据年/月取得对应日
 */
function fGetDaysInMonth(iMonth, iYear) {
  if(iMonth==1|iMonth==3|iMonth==5|iMonth==7|iMonth==8|iMonth==10|iMonth==12)
     {   return 31;}  
  if(iMonth==4|iMonth==6|iMonth==9|iMonth==11)
     {  return 30; } 
	 var frun,frun1,frun2;
	frun=Math.floor(iYear/4) * 4;
	frun1=Math.floor(iYear/100)*100
	frun2=Math.floor(iYear/400)*400
    if(frun1==iYear)
	{
	   if(frun2==iYear)     return 29;
       else     return 28;
        }
	else
	{
       if(frun==iYear)   {return 29;}
       else  {return 28;}
	 }	
}

/**
 * 返回str的长度,中文的长度为2
 */

function strLength(str)
{
	var l=str.length;
	var n=l
	for (var i=0;i<l;i++)
	{
		if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++;
	}
	return n;
}

/**
 * 判断str的长度是否大于size,中文的长度为2
*/

function checkLen(textObj,size)
{
	if (strLength(textObj.value)>size)
	{
		alert ("输入的字符过多.您最多只可以输入"+size+"个字符.")
		tryToFocus(textObj)
		return true;
	}
	return false;
}


/**
 * 判断textObj是否有填写数据
*/
	
function isNull(textObj)
{
	
	if (javaTrim(textObj.value)=="")
	{
		tryToFocus(textObj);
		alert ("该栏输入不能为空!")
		return true;
	}
	return false;
}	
	
/**
 * 判断textObj是否有填写数据,用于list
*/
function isNullList(textObj)
{
	if (javaTrim(textObj.value)=="")
	{		
		tryToFocus(textObj);
		alert (textObj.name + "不能为空!")
		return true;
	}
	return false;
}	

/**
 * 用于确认是否添加记录
 */

function confirmAdd()
{
	if (confirm("您确认需要添加当前记录吗?"))
	{
		return true;
	}
}


function confirmPrint()
{
	if (confirm("您确认需要打印吗?"))
	{
		return true;
	}
}

function confirmSub()
{
	if (confirm("您确认需要提交吗?"))
	{
		return true;
	}
}


/**
 * 用于确认是否删除记录
 */


function confirmDel()
{
	if (confirm("您确认需要删除当前记录吗?"))
	{
		return true;
	}
}

/**
 * 用于确认是否更新记录
 */

function confirmUpdate()
{
	if (confirm("您确认需要修改当前记录吗?"))
	{
		return true;
	}
}

function confirmCopy()
{
	if (confirm("确认开始复制产品信息？"))
	{
		return true;
	}
}


function confirmNext()
{
	return true;
}

function confirmPrev()
{
	return true;
}
function confirmNextYear()
{
	if (confirm("确认开始进入下一年？"))
	{
		return true;
	}
}

function confirmPrevYear()
{
	if (confirm("确认返回到上一年？"))
	{
		return true;
	}
}





/**
 * 用于双主键checkbox,redio的传值
 */


function isCheckedTwoKey(objCheckbox,objHid1,objHid2,objField1,ObjField2)
	{
		isCheck=false;
		if (! objCheckbox)
		{
			alert ("现在没有任何记录可供下一步操作!")
			return false
		}
		if (objCheckbox.length>1)
		{
			
			for (i=0;i<objCheckbox.length;i++)
			{
				if (objCheckbox[i].checked == true)
				{
					objField1.value=objHid1[i].value
					ObjField2.value=objHid2[i].value
					isCheck=true;
				}
			}
		}
		else
		{
			if (objCheckbox.checked == true)
			{
				objField1.value=objHid1.value
				ObjField2.value=objHid2.value
				isCheck=true;			
			}
		}
		if (isCheck==false)
		{
			alert ("请选择一个记录,再进行下一步的操作!");
			return false;
		}	
			return true;
}

/******************************************************
*	判断start date String是否比 end date String小
*	（ 两个字段的值可以为空 ）	
*******************************************************/
function isStartEndDate(ObjStartD,ObjEndD)
	{	
		if(ObjStartD.value==""||ObjEndD.value=="") return true;
		if(ObjStartD.value<=ObjEndD.value) return true;
		else {
			alert("结束日期要大于开始日期!");
			return false;
		}
	}
	
	
/******************************************************
*用于得到指定年月的太阳日	
*******************************************************/

function julianDay(intYear,intMonth,intDay,intYearSize)
{
	intDay=intDay;
	for (i=1;i<intMonth;i++)
	{
		intDay=intDay+fGetDaysInMonth(i,intYear);
	}
	if (intYearSize==0) return intDay;
	strDay=(intYear+"").substring(4-intYearSize,4)
	if ((intDay+"").length==2) return strDay+"0"+intDay;
	if ((intDay+"").length==1) return strDay+"00"+intDay;
	return strDay+intDay;
}

/******************************************************
*   to focus on 	form elements (liujing add)
*******************************************************/
	function tryToFocus(obj)
	{ 	
		if(obj.type!="hidden")
		{
			obj.focus();
		}
		else
		{
			var tmpFouceName=obj.name+"1";
			tmpFouceName="(form1."+tmpFouceName+")";
			if (eval(tmpFouceName))
			{
				tmpFouceName="form1."+obj.name+"1"+".focus()";
				eval(tmpFouceName);
			}
			
		}
		if(obj.type=="textarea" || obj.type=="text") obj.select();
		
	//	if(obj.type!="hidden") obj.className='notNull';
	}
	
//--------------------------------------
//	 确定是否年满18岁   (liujing add) 
//	date string formate is "yyyymmdd"
//--------------------------------------
	function isOldThan18(birthday,today)
	{
		var dob =parseInt(birthday);
		var tod= parseInt(today);
		if((dob+180000)<tod) return true;
		return false;
	}
	
/**
* 判断电话号码
* return false错误，true正确
*/
function isPhone(String,intLength) 
{
	var length1 , i , j, k, flag = 0;
	var string1="";   
	
	string1 = javaTrim(String);
	k = length1 = string1.length;
	j=0;
	for (i = 0 ; i < length1 ; i++) {  //判断每位数字
		if(isNaN(Math.floor(string1.charAt(i),10)))  
		{
			if(string1.charAt(i) != "-")  
			{
				alert( "请输入正确的电话号码！");					
				return(false); 
			} 
			else  {
				j++;				
			}
			if (flag == 0) { 
				k = i;	flag = 1;
			}
			
		}	
	}	

	if (k > intLength) {
		alert("电话长度超过限额");
		return false;
	} 

	if(j > 1) {
		alert( " -  只能有一个!");			
		return false;
	}

	return true;
}

/**
 * 是否正确的日期
 * obj : text object of input
 * return true 日期正确，false 日期不正确
 */

function undoDate(strDate)
   {
   var var_dateFormat = "yyyyMMdd";
   	var strDateFormat=var_dateFormat
   	var strTmp="";
	var strYear="";
	var strMonth="";
	var strDay="";

	while (""!=strDate)
	{
	   	strTmp=strDateFormat.substring(0,1);
   		if (strTmp=="y")
   		{
   			strYear=strDate.substring(0,4);
   			strDate=strDate.substring(4);
   			strDateFormat=strDateFormat.substring(4);
   		}
   		if (strTmp=="M")
   		{
   			strMonth=strDate.substring(0,2);
   			strDate=strDate.substring(2);
   			strDateFormat=strDateFormat.substring(2);
   		}
   		if (strTmp=="d")
   		{
   			strDay=strDate.substring(0,2);
   			strDate=strDate.substring(2);
   			strDateFormat=strDateFormat.substring(2);
   		}
   	}

   	return strYear+strMonth+strDay;
   }

