// JavaScript Document
// 检测浏览器
NS4 = document.layers && true;
IE4 = document.all && parseInt(navigator.appVersion) >= 4;

//函数名：chksafe
//功能介绍：检查是否含有"'",'\\',"/"
//参数说明：要检查的字符串
//返回值：0：是  1：不是
function chksafe(a)
{
	fibdn = new Array ("'",'\\',"/");
	i=fibdn.length;
	j=a.length;
	for (ii=0;ii<i;ii++)
	{
		for (jj=0;jj<j;jj++)
		{	
			temp1=a.charAt(jj);
			temp2=fibdn[ii];
			if (temp1==temp2)
			{
				//alert("You have entered invalid char!");
				return 0;
			}
		}
	}
	return 1;
}

//函数名：chkspc
//功能介绍：检查是否含有空格
//参数说明：要检查的字符串
//返回值：0：是  1：不是
function chkspc(a)
{

	var i=a.length;
	for (j=0;(j<i) && (a.charAt(j)==' ');j++)
	{
	
	}
	if (i==j)
	{
		//alert("You have enter enough space!")
		return 0;
	}
	else
	{
		return 1;
	}
	if (i==0)
	{
		return 0;
	}

}

//验证特殊字符1
function strangecode(code)
{   var strangCode = "~`!@#$%^&*()+=-|\'/　？,， 。.<>";
    var temp;
    for (var i = 0; i<code.length; i++ )
    {
        temp = code.substring(i,i+1);
        if (strangCode.indexOf(temp) !=-1)
        {
            return (false);
        }
    }
}

//函数名：fucPWDchk
//功能介绍：检查是否含有非数字或字母
//参数说明：要检查的字符串
//返回值：0：含有非数字或字母 1：全部为数字或字母
function fucPWDchk(str)
{
  var strSource ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()_-+=|\}{[]:;'?/>.,<";
  var ch;
  var i;
  var temp;
  
  for (i=0;i<=(str.length-1);i++)
  {
  
    ch = str.charAt(i);
    temp = strSource.indexOf(ch);
    if (temp==-1) 
    {
     return 0;
    }
  }
  if (strSource.indexOf(ch)==-1)
  {
    return 0;
  }
  else
  {
    return 1;
  } 
}

//函数名：fucStrchk
//功能介绍：检查是否含有非数字或字母
//参数说明：要检查的字符串
//返回值：0：含有非数字或字母 1：全部为数字或字母
function fucStrchk(str)
{
  var strSource ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var ch;
  var i;
  var temp;
  
  for (i=0;i<=(str.length-1);i++)
  {
  
    ch = str.charAt(i);
    temp = strSource.indexOf(ch);
    if (temp==-1) 
    {
     return 0;
    }
  }
  if (strSource.indexOf(ch)==-1)
  {
    return 0;
  }
  else
  {
    return 1;
  } 
}

function jtrim(str)
{     while ((str.charAt(0)==" ")||(str.charAt(0)=="　"))
          {str=str.substr(1);}      
     while (str.charAt(str.length-1)==" ")
         {str=str.substr(0,str.length-1);}
     return(str);
}


//函数名：fucCheckNUM
//功能介绍：检查是否为数字
//参数说明：要检查的数字
//返回值：1为是数字，0为不是数字
function fucCheckNUM(NUM)
{
	var i,j,strTemp;
	strTemp="0123456789";
	for (i=0;i<NUM.length;i++)
	{
		j=strTemp.indexOf(NUM.charAt(i));	
		if (j==-1)
		{
		//说明有字符不是数字
			return 0;
		}
	}
	//说明是数字
	return 1;
}

//函数名：fucCheckNumeric
//功能介绍：检查是否为数字
//参数说明：要检查的数字
//返回值：1为是数字，0为不是数字
function fucCheckNumeric(NUM)
{
	var i,j,strTemp;
	var PointNum;
	PointNum=0
	strTemp="0123456789.";
	for (i=0;i<NUM.length;i++)
	{
		j=strTemp.indexOf(NUM.charAt(i));	
		if (j==-1)
		{
		//说明有字符不是数字
			return 0;
		}
		if(j==10)
		{
			PointNum=PointNum+1;
		}
		
	}
	if(PointNum>1)
	{
		//含有两个以上小数点
		return 0;
	}
	//说明是数字
	return 1;
}

//函数名：fucCheckTel
//功能介绍：检查是否为电话号码
//参数说明：要检查的电话号码
//返回值：1为是电话号码，0为不是电话号码
function fucCheckTel(Tel)
{
	var i,j,strTemp;
	strTemp="0123456789-#() ";
	if (Tel.length < 7 )
	{
	 return 0;
	 }
	for (i=0;i<Tel.length;i++)
	{
		j=strTemp.indexOf(Tel.charAt(i));	
		if (j==-1)
		{
		//说明不是电话号码
			return 0;
		}
	}
	//说明是电话号码
	return 1;
}

//验证电话号码
function phone(phone)
{
    var phoneNum = "1234567890()- #";
    var temp;
    if (phone.length < 7)
    {
         return (false);
    }
    for (var i = 0; i < phone.length; i++)
    {
        temp = phone.substring(i,i+1);
        if (phoneNum.indexOf(temp) == -1)
        {
            return (false);
        }
    }
}

//函数名：chkemail
//功能介绍：检查是否为Email Address
//参数说明：要检查的字符串
//返回值：0：不是  1：是
function chkemail(a)
{

	var i=a.length;

	var temp = a.indexOf('@');
	//var tempd = a.indexOf('.');
	var tempd = a.indexOf('.');

	if (tempd > 0) {
		tempd = (a.substring(temp)).indexOf('.');
		
		if (temp > 1) {
		
			if ((i-temp) > 5){
				if (tempd > 2){
				
					if ((i-temp-tempd)>1){
						return 1;
					}
				}
			}
		}

	}
	
	return 0;
	
}


//opt1 小数     opt2   负数
//当opt2为1时检查num是否是负数
//当opt1为1时检查num是否是小数
//返回1是正确的，0是错误的
function chknbr(num,opt1,opt2)
{
	var i=num.length;
	var status;
//staus用于记录.的个数
	status=0;
	if ((opt2!=1) && (num.charAt(0)=='-'))
	{
		//alert("You have enter a invalid number.");
		return 0;
	
	}
//当最后一位为.时出错
	if (num.charAt(i-1)=='.')
	{
		//alert("You have enter a invalid number.");
		return 0;
	}

	for (j=0;j<i;j++)
	{
		if (num.charAt(j)=='.')
		{
			status++;
		}

		if (status>1) 
		{
		//alert("You have enter a invalid number.");
		return 0;		
		}
		if (num.charAt(j)<'0' || num.charAt(j)>'9' )
		{
			if (((opt1==0) || (num.charAt(j)!='.')) && (j!=0)) 
			{
				//alert("You have enter a invalid number.");
				return 0;
			}
		}
	}
	return 1;
}

//函数名：chkdate
//功能介绍：检查是否为日期
//参数说明：要检查的字符串
//返回值：0：不是日期  1：是日期
function chkdate(datestr)
{
	var lthdatestr=datestr.length;
	var tmpy="";
	var tmpm="";
	var tmpd="";
	var datestr;
	var status;
	status=0;
	for (i=0;i<lthdatestr;i++)
	{	if (datestr.charAt(i)== '-')
		{
			status++;
		}
		if (status>2)
		{
			//alert("Invalid format of date!");
			return 0;
		}
		if ((status==0) && (datestr.charAt(i)!='-'))
		{
			tmpy=tmpy+datestr.charAt(i)
		}
		if ((status==1) && (datestr.charAt(i)!='-'))
		{
			tmpm=tmpm+datestr.charAt(i)
		}
		if ((status==2) && (datestr.charAt(i)!='-'))
		{
			tmpd=tmpd+datestr.charAt(i)
		}

	}
	year=new String (tmpy);
	month=new String (tmpm);
	day=new String (tmpd)
	//tempdate= new String (year+month+day);
	//alert(tempdate);
	if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
	{
		//alert("Invalid format of date!");
		return 0;
	}
	if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
	{
		//alert ("Invalid month or day!");
		return 0;
	}
	if (!((year % 4)==0) && (month==2) && (day==29))
	{
		//alert ("This is not a leap year!");
		return 0;
	}
	if ((month<=7) && ((month % 2)==0) && (day>=31))
	{
		//alert ("This month is a small month!");
		return 0;
	
	}
	if ((month>=8) && ((month % 2)==1) && (day>=31))
	{
		//alert ("This month is a small month!");
		return 0;
	}
	if ((month==2) && (day==30))
	{
		//alert("The Febryary never has this day!");
		return 0;
	}
	return 1;
}



//函数名：fucCheckLength
//功能介绍：检查字符串的长度
//参数说明：要检查的字符串
//返回值：长度值
function fucCheckLength(strTemp)
{
	var i,sum;
	sum=0;
	for(i=0;i<strTemp.length;i++)
	{
		if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
			sum=sum+1;
		else
			sum=sum+2;
	}
	return sum;
}

//函数名：jreplace
//功能介绍：替换字符串中的'
//参数说明：要检查的字符串
//返回值：已经替换好的字符串
function jreplace(str)
{
	var strTemp=str;
	var i=0;
	while (i!=-1)
	{
		i=strTemp.indexOf("'");
		if (i!=-1)
		{
			strTemp=strTemp.substring(0,i)+"’"+strTemp.substring(i+1,strTemp.length);
		}
	}
	return strTemp;
}

//函数名：fucChkFileName
//功能介绍：检查是否含有 "gif","jpg","zip","htm","html"
//参数说明：filename: 要检查的字符串
//	filenarr(): 被包含的字符集合
//返回值：0：没有  1：有
function fucChkFileName(filename, filenarr)
{
	var intfarrlen = filenarr.length;
	var intflen = filename.length;
	var strfilen = filename.toLowerCase();
	for (i=0;i<intflen;i++)
	{
			temp=filenarr[i];
			if ( temp==strfilen )
			{
				//You upload a image
				return 1;
			}

	}
	return 0;
}

//函数名
//功能介绍：将月、日变为两位
//参数说明：strDate为 要检查的字符串
//返回值：变换完毕的日期型字符串（yyyy-mm-dd）
//请先用chkdate检查日期的合法性
function datechange(strDate)
{
	var tempy="";
	var tempm="";
	var tempd="";
	var tempDate;
	var status;
	status=0;
	for (i=0;i<strDate.length;i++)
	{	if (strDate.charAt(i)== '-')
		{
			status++;
		}
		if ((status==0) && (strDate.charAt(i)!='-'))
		{
			tempy=tempy+strDate.charAt(i);
		}
		if ((status==1) && (strDate.charAt(i)!='-'))
		{
			tempm=tempm+strDate.charAt(i);
		}
		if ((status==2) && (strDate.charAt(i)!='-'))
		{
			tempd=tempd+strDate.charAt(i);
		}

	}
	tempy="0000"+tempy;tempy=tempy.substring(tempy.length-4,tempy.length);
	tempm="00"+tempm;tempm=tempm.substring(tempm.length-2,tempm.length);
	tempd="00"+tempd;tempd=tempd.substring(tempd.length-2,tempd.length);
	tempDate=tempy+"-"+tempm+"-"+tempd;
	return tempDate;

}

//函数名：fucDateLimit
//功能介绍：处理客户端的时间有效性检查
//参数说明：要检查的时间串
//返回TRUE时说明时间没有越界，返回FALSE则相反
//注意必须保证前四位为年份
function fucDateLimit(strDate)
{
	var strTempYear;
	strTempYear=strDate.substring(0,4);
	if ((strTempYear<"1900") ||(strTempYear>"2999"))
	{
		return false;
	}
	return true;
}


//函数名：DrawImage 
//功能介绍：成比例缩小图片显示尺寸
//参数说明：ImgD图片对象,x图片宽度，y图片高度
var flag=false;
function DrawImage(ImgD,x,y){
   var image=new Image();
   image.src=ImgD.src;
   if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= x/y){
     if(image.width>x){  
     ImgD.width=x;
     ImgD.height=(image.height*x)/image.width;
     }else{
     ImgD.width=image.width;  
     ImgD.height=image.height;
     }
     //ImgD.alt=image.width+"×"+image.height;
     }
    else{
     if(image.height>y){  
     ImgD.height=y;
     ImgD.width=(image.width*y)/image.height;     
     }else{
     ImgD.width=image.width;  
     ImgD.height=image.height;
     }
     //ImgD.alt=image.width+"×"+image.height;
     }
    }
} 

