/*
	函数名称：trim
	函数功能: 去除字符串头部和尾部的空格
	传入参数：字符串变量
	传出结果：处理后的子串
*/
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function isChn(d) {
 actlen=d.length;  
 for(i=0;i<d.length;i++)
 if (d.substr(i,1)>"~")
    actlen+=1;
 if( actlen>d.length )
   return true;
 return false;
}

/*  函数功能：判断传入参数是否为yyyy-mm-dd或
			  yyyy/mm/dd格式的正确日期
			  2001/01/2和2001-3-04也是允许的格式
			  如果是，则返回一个对应的日期对象
			  如果否，则返回false
*/
function isDate(strDate){
	var regYear = /\d{4}[-/]/g;	//year pattern
	var regMonth;			
	var regDay =  /\d{1,2}/g;;
	var chrSeperator;
	var arr,str;
	if ((arr = regYear.exec(strDate)) == null) 
		return false;
	var intYearlen = arr.lastIndex - arr.index - 1;
	if (arr.index != 0 || (intYearlen != 4 && intYearlen != 2))
		return false;
	str = arr[0];
	chrSeperator = str.charAt(str.length - 1);  // get the seperator ('-' or '/') 
	intYear = parseInt(str.substr(0, str.length - 1));	// get the year
	if (intYear < 1900 || intYear > 2099)  //Error Year
		return false;

	strDate = strDate.substr(arr.lastIndex);
	if (chrSeperator == "-")
		regMonth = /\d{1,2}[-]/g;
	else
		regMonth = /\d{1,2}[/]/g;
	if ((arr = regMonth.exec(strDate)) == null) 
		return false;
	if (arr.index != 0)
		return false;
	str = arr[0];
	if (str.charAt(0) == '0') {
		intMonth = parseInt(str.substr(1, str.length - 2)); // get the month
	} else {
		intMonth = parseInt(str.substr(0, str.length - 1)); // get the month
	}
	if (intMonth < 1 || intMonth > 12) //Error Month
		return false;

	strDate = strDate.substr(arr.lastIndex);

	if ((arr = regDay.exec(strDate)) == null) 
		return false;
	if (arr.index != 0 || arr.lastIndex != strDate.length)
		return false;
	str = arr[0];
	if (str.charAt(0) == '0') {
		intDay = parseInt(str.substr(1, str.length - 1)); // get the day
	} else {
		intDay = parseInt(str); // get the day
	}	
	if (intDay < 1 || intDay > 31)  //Error Day
		return false;

	datDate = new Date(intYear, intMonth - 1, intDay); //Test the Date
	if (isNaN(datDate))  //Error Date Format
		return false;
	if (datDate.getMonth() != intMonth - 1 || datDate.getDate() != intDay)  //invalid date such as '1999/02/29' and '1999/04/31'
		return false;
	return datDate;  //Return the Date in parsed format
}

function isBirthDate(d) {
	var first,second,yy,mm,dd;
	var today = new Date();
	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<30) 
			yy += 2000;
		else if (yy <100 && yy >= 30)
			yy += 1900;
		if( yy < 1900 || 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;
		}
		if ( yy + 16 > today.getFullYear() ) 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<30) 
			yy += 2000;
		else if (yy <100 && yy >= 30)
			yy += 1900;
		if( yy < 1950 || 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;
		}
		if ( yy + 16 > today.getFullYear() ) return false;
		return d.toLocaleString();  //Return the Date in parsed format
	}
	else
		return false;
}

function isInt(n) {
	var i = parseInt(n);
	if (i == NaN) {
		return false;
	}
	if (i != n * 1){
		return false;
	}
	return true;
}

function isDecimal(str,f,n) {
    var p=str.indexOf(".");
    var int,flt;
    if (p<0) { p=str.length ;}
    int=str.substr(0,p);
    flt=str.substr(p+1);
    if (isInt(int)==false) {
       return false;
    }
    if (flt!='') {
       if (isInt(flt)==false) {
          return false;
       }
    }
    if ((int.length > f-n) || (flt.length > n)) {
       return false;
    }
    return true; 
}
/*  函数功能：检查邮件格式
			  
			
*/

function isMail(str) {
    var a=str.indexOf("@")+1;
    var p=str.indexOf(".")+1;
    if(str.indexOf("'") > 0)
		return false;
	if(str.indexOf('"') > 0)
		return false;
    if (a<2)
       return false;    
    if (p<1)
       return false;    
    if (p<a+2)
       return false;    
    if (str.length==p)
       return false;		
    return true; 
}


			  
			


/*  函数功能：检查小数位数
*/	
  
 /* 
  函数功能：检查是否为数值

		  
			
*/
function isNumber(str) {
    for (var i=0; i < str.length; i++)
	{	var ch=str.charAt(i);
		if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))	
			return false;
	}
    return true; 
}

function CheckUserInput(vstrInput) {
	var intIndex;
	var intCharCount;
	for(intIndex = 0; intIndex < vstrInput.length; intIndex++){
		if (vstrInput.charCodeAt(intIndex) < 48) 
			return false;
		if ((vstrInput.charCodeAt(intIndex) > 57) && (vstrInput.charCodeAt(intIndex) < 64)) 
			return false;
		if ((vstrInput.charCodeAt(intIndex) > 90) && (vstrInput.charCodeAt(intIndex) < 97)) 
			return false;
		if (vstrInput.charCodeAt(intIndex) > 122) 
			return false;
	}
	return true;
}
function isPhone(str){
	var intIndex;
	var intCharCount;
	for(intIndex = 0; intIndex < str.length; intIndex++){
		if(str.charCodeAt(intIndex) < 32)
			return false;
		if(str.charCodeAt(intIndex) == 34)
			return false;
		if(str.charCodeAt(intIndex) == 39)
			return false;
		if(str.charCodeAt(intIndex) > 126)
			return false;
	}
	return true;
}

/*
	函数名称：checkString()
	函数功能: 不能包含&、’、”、<、>、:、;等特殊字符;
		合法字符：32（空格）、48~57（数字）、65~90（大写字符）、95（下划线）、97~122（小写字符）、>127（汉字）。
	作		者：蒲林
	创建时间：2001/06/14
	传入参数：字符串变量
	传出结果：处理后的子串
*/
function checkString(str){
	var strChar = str;
	var isValid = true;
	for (var i = 0; i < str.length; i++){
		if ( (str.charCodeAt(i) == 32) || ((str.charCodeAt(i) >= 48) && (str.charCodeAt(i) <= 57)) || ((str.charCodeAt(i) >= 65) && (str.charCodeAt(i) <= 90)) || (str.charCodeAt(i) == 95) || ((str.charCodeAt(i) >= 97) && (str.charCodeAt(i) <= 122)) || (str.charCodeAt(i) > 127) ) {
			// do nothing				
		} else {
			isValid = false;
			break;
		}
	}
	return isValid;
}

/*
	函数名称：selectToString(selectObject)
	函数功能: 将select中的项组成字符串，以逗号分隔
	传入参数：select对象
	传出结果：字符串
	作	  者：吕锴
	创建时间：2001/08/30
*/
function selectToString(selectObject){
	var str = "";
	for(var i = 0; i < selectObject.length; i++){
		str = str + selectObject(i).value + ",";
	}
	if(str.substr(str.length - 1, 1) == ",")
		str = str.substr(0, str.length - 1);
	return str;
}
/*
	函数名称：CheckPostCode(str)
	函数功能: 检查邮编的合法性
	传入参数：str——输入字符
	传出结果：true or false
	作	  者：蒲林
	创建时间：2001/10/10
*/
function CheckPostCode(str){
	if (trim(str) == ""){
		return true;
	}
	for (var i=0; i < str.length; i++){	
		var ch=str.charAt(i);
		if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))	
			return false;
		else
			return true;
	}
}

/*
	函数名称：MakeString(intMonth)
	函数功能: 将数值型月份转换为英文月份
	作		者：
	创建时间：
	传入参数：
	传出结果：
*/
function MakeString(intMonth) {
	var Month
	switch (intMonth) {
		case 0 :
			Month = "JANUARY";
			break;
		case 1 :
			Month = "FEBUARY";
			break;
		case 2 :
			Month = "MARCH";
			break;
		case 3 :
			Month = "APRIL";
			break;
		case 4 :
			Month = "MAY";
			break;
		case 5 :
			Month = "JUNE";
			break;
		case 6 :
			Month = "JULY";
			break;
		case 7 :
			Month = "AUGUST";
			break;
		case 8  :
			Month = "SEPTEMBER";
			break;
		case 9 :
			Month = "OCTOBER";
			break;
		case 10 :
			Month = "NOVEMBER";
			break;
		case 11 :
			Month = "DECEMBER";
	}
	return (Month);
}
/*
	函数名称：MakeString(intMonth)
	函数功能: 将数值型月份转换为大写月份
	作		者：
	创建时间：
	传入参数：
	传出结果：
*/
function MakeStringT(intMonth) {
	var Month
	switch (intMonth) {
		case 0 :
			Month = "一月";
			break;
		case 1 :
			Month = "二月";
			break;
		case 2 :
			Month = "三月";
			break;
		case 3 :
			Month = "四月";
			break;
		case 4 :
			Month = "五月";
			break;
		case 5 :
			Month = "六月";
			break;
		case 6 :
			Month = "七月";
			break;
		case 7 :
			Month = "八月";
			break;
		case 8  :
			Month = "九月";
			break;
		case 9 :
			Month = "十月";
			break;
		case 10 :
			Month = "十一月";
			break;
		case 11 :
			Month = "十二月";
	}
	return (Month);
}
/*
	函数名称：
	函数功能: 
	作	 者：
	创建时间：
	传入参数：
	传出结果：
*/
function AddOptionRemote(val, text, ListName)
{
	if (val == "") return;
	var bFound=false;
	var List = document.all(ListName);
	var ListLen = List.options.length;
	if( ListLen > 0)
	{
		for(i = 0; i < ListLen; i++)
		{
			if( List.options(i).value == val )
			{
				bFound=true;
				break;
			}
		}
	}
	if(!bFound)
	{
		var oOption=document.createElement("OPTION");
		oOption.text=text;
		oOption.value=val;
		List.add(oOption);
	}
}

function AddOption(InputName, ListName)
{
	var val = document.all(InputName).value;
	if (val == "") return;
	var bFound=false;
	var List = document.all(ListName);
	var ListLen = List.options.length;
	if( ListLen > 0)
	{
		for(i = 0; i < ListLen; i++)
		{
			if( List.options(i).value == val )
			{
				bFound=true;
				break;
			}
		}
	}
	if(!bFound)
	{
		var oOption=document.createElement("OPTION");
		oOption.text=val;
		oOption.value=val;
		List.add(oOption);
	}
}

function AddOptionItemTest(SourceListName, DestListName)
{
	var SourceList = document.all(SourceListName);
	var bFound;
	var List = document.all(DestListName);
	var ListLen;
	var SourceListLen = SourceList.options.length;
	var DestListLen=List.options.length;
	var OptionArrayText =new Array();
	var OptionArrayValue=new Array();
	
	for(j=0;j<DestListLen;j++)	{
		OptionArrayText(j)=List.options(j).text;
		OptionArrayValue(j)=List.options(j).value;
	}
	for (j=SourceListLen-1; j>=0; j--)
	  if (SourceList.options(j).selected) {
		val = SourceList.options(j).value;
		text = SourceList.options(j).text;
		ListLen = List.options.length;
		bFound = false;
		if( ListLen > 0)
		{
			for(i = 0; i < ListLen; i++)
			{
				if( OptionArrayValue(i) == val )
				{
					bFound=true;
					break;
				}
			}
		}
		if(!bFound)
		{
			var oOption=document.createElement("OPTION");
			oOption.text=text;
			oOption.value=val;
			List.add(oOption);
			SourceList.remove(j);
		}
	}
}

function AddOptionItem(SourceListName, DestListName)
{
	var SourceList = document.all(SourceListName);
	var bFound;
	var List = document.all(DestListName);
	var ListLen;
	var SourceListLen = SourceList.options.length;
	
	
	for (j=SourceListLen-1; j>=0; j--)
	  if (SourceList.options(j).selected) {
		val = SourceList.options(j).value;
		text = SourceList.options(j).text;
		ListLen = List.options.length;
		bFound = false;
		if( ListLen > 0)
		{
			for(i = 0; i < ListLen; i++)
			{
				if( List.options(i).value == val )
				{
					bFound=true;
					break;
				}
			}
		}
		if(!bFound)
		{
			var oOption=document.createElement("OPTION");
			oOption.text=text;
			oOption.value=val;
			List.add(oOption);
			SourceList.remove(j);
		}
	}
}
function DelOption(ListName)
{
	var List = document.all(ListName);
	var ListLen = List.options.length;
	for(i = ListLen - 1; i >= 0; i--)
	{
		if( List.options(i).selected )
		{
			List.remove(i);
		}
	}
}

function AddGroupItem(SourceListName, DestListName)
{
	var SourceList = document.all(SourceListName);
	var bFound;
	var List = document.all(DestListName);
	var ListLen;
	var SourceListLen = SourceList.options.length;
	var ItemArray, Item, ItemKeyArray, ItemKey, ItemValue;
	for (j=0; j< SourceListLen; j++)
		if (SourceList.options(j).selected) {
			val = SourceList.options(j).value;
			text = SourceList.options(j).text;
			if (text.substr(0, 3) == '(G)')
			{
				ItemArray = val.split(",");
				for (Item in ItemArray)
				{
					ItemKeyArray = ItemArray[Item].split("=");
					ItemKey = ItemKeyArray[0];
					ItemValue = ItemKeyArray[1];
					AddOptionRemote(ItemKey, ItemValue, DestListName);
				}
			}
			else
				AddOptionRemote(val, text, DestListName);
		}
}
function isEmpty(inputStr)
{
	if(inputStr==null || inputStr==""){
		return true;
	}
	return false;
}

//*******************删除。修改。查看按钮的js函数
function dellink(linkpage){

 if (typeof(document.buttonform)=="undefined") // 无button 显示
 {
	 	yesno=window.confirm("Delete the record? ");
	if (yesno==1)
		{window.location.href=linkpage}
	else
		{return false;}
 }

 else
	 
 {//有button 显示

	 if (document.buttonform.inbutton.value!="")
     {
	
     linkpage=linkpage+document.buttonform.inbutton.value
     

	yesno=window.confirm("您确信要删除该记录吗？/Delete the record? ");
	if (yesno==1)
		{window.location.href=linkpage}
	else
		{return false;}
    }
  }
}


function updatelink(linkpage){
 if (typeof(document.buttonform)=="undefined") // 无button 显示
 {
	yesno=window.confirm("您确信要修改该记录吗？/Update the record? ");
	if (yesno==1)
		{window.location.href=linkpage}
	else
		{return false;}

 }
 else
	 
 {//有button 显示
	 if (document.buttonform.inbutton.value!="")
     {
	
     linkpage=linkpage+document.buttonform.inbutton.value
    


	yesno=window.confirm("您确信要修改该记录吗？/Update the record? ");
	if (yesno==1)
		{window.location.href=linkpage}
	else
		{return false;}
     }

 }
}


function displaylink(linkpage)
{
if (typeof(document.buttonform)=="undefined") // 无button 显示
 {
		window.location.href=linkpage

 }
 else
	 
 {//有button 显示
 	 if (document.buttonform.inbutton.value!="")
     {
	
     linkpage=linkpage+document.buttonform.inbutton.value
    
		{window.location.href=linkpage}
	 }
 }
}

function open_newpage(url,nWidth,nHeight)
{
	window.open(url,"","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,left=80,top=50,width="+nWidth+",height="+nHeight)
}
