﻿//自动加载该页图片并调整大小
//<img src="../A_SysAdmin/Inc/图片" size="宽,高"/>
window.onload=function()
{
    for(var n=0;n<document.getElementsByTagName("img").length;n++)
    {
        var aimobj=document.getElementsByTagName("img")[n];
        if(aimobj.size)
        {
            var aimwidth=aimobj.size.split(",")[0];
            var aimheight=aimobj.size.split(",")[1];
            imgsize(aimobj,aimwidth,aimheight);
            aimobj.style.display='';
        }
    }
}

function imgsize(obj,widthvalue,heightvalue)
{
    var w;
    var h;
    w=obj.width;
    h=obj.height;
    if(obj.width>widthvalue)
    {
        do
        w=w-1;
        while (w>widthvalue) ;
        s=w/obj.width;
        obj.width=w;
        obj.height=h*s;
        h=obj.height
    }
    if(obj.height>heightvalue)
    {
        do
        h=h-1;
        while (h>heightvalue) ;
        s=h/obj.height;
        obj.width=w*s;
        obj.height=heightvalue;
    }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function CheckAll(form)  
  {  
  for (var i=0;i<form.id.length;i++)  
    {  
    var e = form.id[i];  
    if (e.name != 'chkall') 
       e.checked = form.chkall.checked;
		form.submitid.disabled = (form.id.value = false);

    }  
  }  


//自动缩小图片
//<img src="../A_SysAdmin/Inc/图片" onload="imgsize(this,200,200)"/>
function imgsize(obj,widthvalue,heightvalue)
{ 
var w; 
var h; 
w=obj.width; 
h=obj.height; 
if(obj.width>widthvalue){ 
do 
w=w-1; 
while (w>widthvalue) ; 
s=w/obj.width; 
obj.width=w; 
obj.height=h*s; 
h=obj.height 
} 
if(obj.height>heightvalue){ 
do 
h=h-1; 
while (h>heightvalue) ; 
s=h/obj.height; 
obj.width=w*s; 
obj.height=heightvalue; 
} 
} 
//鼠标事件移动一个对象
//被移动对象div需要加属性id="div" style="position:absolute;top:0;left:0"
//在触发对象p上onMouseMove="drag(this,div)"
function drag(p,o){   
  p.onmousedown=function(a){   
  var d=document;if(!a)a=window.event;   
  var x=a.layerX?a.layerX:a.offsetX,y=a.layerY?a.layerY:a.offsetY;   
  if(o.setCapture)   
  o.setCapture();   
  else if(window.captureEvents)   
  window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);   
    
  d.onmousemove=function(a){   
  if(!a)a=window.event;   
  if(!a.pageX)a.pageX=a.clientX;   
  if(!a.pageY)a.pageY=a.clientY;   
  var tx=a.pageX-x,ty=a.pageY-y;   
  o.style.left=document.body.scrollLeft+tx;   
  o.style.top=document.body.scrollTop+ty;   
  };   
    
  d.onmouseup=function(){   
  if(o.releaseCapture)   
  o.releaseCapture();   
  else if(window.captureEvents)   
  window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);   
  d.onmousemove=null;   
  d.onmouseup=null;   
  };   
  };   
} 

/*
* 功能:验证所有的表单
* 使用方法:
* <script language="javascript" src="../A_SysAdmin/Inc/inc/script.js"></script>
* <form name="form1">
* <input type="text" name="id" check="参数1,参数2,参数3,参数4">
* <input type="submit" onClick="return checkform(form1)">或者写在form里<form name="form1" onsubmit="return checkform(this)">
* </form>
* 参数使用说明:
* 参数1:如果是必填项该值为"必",如果是可选项该值为"选"
* 参数2:目前有7个值可选
		"空":判断该处不能为空
		"帐":判断该处必须由数字`字母`下划线组成一般用于注册帐号
		"数":判断该处必须为数字
		"邮":判断该处必须为邮件
		"全":判断该处必须为全角字符
		"半":判断该处必须为半角字符
		"英":判断该处必须为字母
		"汉":判断该处必须为汉字
		"网":判断该处必须为网址
		"电":判断该处必须为电话号码
		"日":判断该处必须为日期格式
		"多":判断该处的多项选择 ★例：check="必,多,0,5" 表示为至少选择0个最多选择5个
		"重":当前内容和上边内容相同(密码验证)
* 参数3:限制字符个数的下限
* 参数4:限制字符个数的上限
*例子:check="必,数,0,8" 表示为必选项并且为数字型字符不得超过8个
*例子:check="选,邮,8,8" 表示为可选项并且为邮件型字符数必须为8个
*例子:check="选,英,5,16" 表示为可选项并且为字母型字符数为5~16个之间
*如果表单中含有ewebeditor等在线编辑器请将其调用iframe的名字改为editor即可
*/

//******主函数*****
function checkform(Formname)
{
var obj = Formname.elements;
var BoxValue=0;//多选的初始值
var limit1=0//多选的初始下限
var limit2=0//多选的初始上限
//遍历所有表元素
for(var i=0;i<obj.length;i++)
  {
  //是验证检测类型
  if(obj[i].check)
    {
	  if(obj[i].check.split(",")[0]=="选"&&obj[i].value||obj[i].check.split(",")[0]=="必")
	  {
		//替换表单中的非法字符开始
		nostr="',`,delete,clean,insert,add,update,modify,iframe,script"
		objvaluestr=obj[i].value
		//循环替换
		nosplit=nostr.split(",");
		nostrnum=nosplit.length;
		for(var m=0;m<nostrnum;m=m+2)
		{
   	    	objvaluenum=objvaluestr.split(nosplit[m]).length
   	    	for(var k=1;k<objvaluenum;k++)
   	    	{
   	    	objvaluestr=objvaluestr.replace(nosplit[m],nosplit[m+1]);
   	    	}
		}
		//字符还原
		obj[i].value=objvaluestr
	    //替换表单中的非法字符结束
		if(!obj[i].value){loseback(obj[i],"<--提示-->\n\n变色处为必须填写项!")}
		else
		 {
		  var switchstr=obj[i].check.split(",")[1]
		  var text=obj[i].check.split(",")[5]
		  switch(switchstr)
            {
             case "空" : CheckExp(obj[i],/[^\f\n\r\t\v]/i,"<--提示-->\n\n让你随意输都可以出错看样我们的程序有问题谢谢您的反馈!");
			 break;
			 case "帐" : CheckExp(obj[i],/^[A-Za-z0-9._@]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^A-Za-z0-9._@]/i))+"\n\n帐号必须由数字`字母`下划线组成!");
			 break;
             case "数" : CheckExp(obj[i],/^[0-9.]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^0-9.]/i))+"\n\n变色处必须为数字!");
			 break;
			 case "电" : CheckExp(obj[i],/^[0-9-]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^0-9-]/i))+"\n\n变色处必须为电话号码!");
			 break;
			 case "日" : CheckExp(obj[i],/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/i,"<--提示-->\n\n变色处必须为日期格式!");
			 break;
			 case "邮" : CheckExp(obj[i],/(.+)\@+([\,\w\.-]+)\.+([\,\w\.-]+)/i,"<--提示-->\n\n变色处必须为Email格式!");
			 break;
			 case "全" : CheckExp(obj[i],/^[\W\n\r]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^\W\n\r]/i))+"\n\n变色处必须为全角字符!");
			 break;
			 case "半" : CheckExp(obj[i],/^[\x00-\x88]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^\x00-\x88]/i))+"\n\n变色处必须为半角字符!");
			 break;
			 case "英" : CheckExp(obj[i],/^[a-zA-Z]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[^a-zA-Z]/i))+"\n\n变色处必须为字母格式!");
			 break;
			 case "汉" : CheckExp(obj[i],/^[^\x00-\xff]+$/i,"<--提示-->非法字符: "+obj[i].value.charAt(obj[i].value.search(/[\x00-\xff]/i))+"\n\n变色处必须为汉字!");
			 break;
			 case "网" : CheckExp(obj[i],/^http:[/][/]+([\,\w\.-]+)\.+([\,\w\.-]+)/i,"<--提示-->\n\n变色处必须为网址的标准格式!\n如：http://www.xxx.com");
			 break;
			 case "多" : if(obj[i].checked){BoxValue++};limit1=obj[i].check.split(",")[2];limit2=obj[i].check.split(",")[3];
			 break;
			 case "重" : if(obj[i].value!=obj[i-1].value){loseback(obj[i],"<--提示-->\n\n两次输入的密码不相同！")}else{sucback(obj[i])};
			 break;
			 default  : loseback(obj[i],"变色处的校验代码 ‘"+switchstr+"’ 不存在,请程序员核实!");
            }
		  }
		}else{result=1}
     }else{result=1}
  if(result==0){return false}
  }
  //如果存在多选判断开始
  if(parseInt(BoxValue)<parseInt(limit1)){alert("<--提示-->\n\n多选处请至少选择"+limit1+"个!");return false;}
  if(parseInt(BoxValue)>parseInt(limit2)){alert("<--提示-->\n\n多选处的选择结果不得超过"+limit2+"个!");return false;}
  //如果存在ewebsoft编辑器那么判断其不能为空（iframe的名字改为editor即可）
  var iseditor=document.getElementById("editor");
  if(iseditor){if(editor.getHTML()==''){alert('<--提示-->\n\n内容不能为空！');return false}}
}


//******功能模块函数*****
//验证失败返回结果
function loseback(eimobj,notice)
{
  eimobj.style.background='#F2DDDA';//失败后的颜色
  alert(notice);
  eimobj.focus();
  result=0;
}
//验证成功并检测字符个数后返回结果
function sucback(eimobj)
{
  var strnum1=parseInt(eimobj.check.split(",")[2]);
  var strnum2=parseInt(eimobj.check.split(",")[3]);
      strlen=eimobj.value.length
  var arr=eimobj.value.match(/[^\x00-\x80]/ig)
      if(arr!=null){strlen+=arr.length}
  if (parseInt(strlen)<parseInt(strnum1)||parseInt(strlen)>parseInt(strnum2))
    {
	  if(parseInt(strlen)<parseInt(strnum1)){notice="<--提示-->\n\n变色处的字符数至少为"+strnum1+"个!"}
	  if(parseInt(strlen)>parseInt(strnum2)){notice="<--提示-->\n\n变色处的字符数不得超过"+strnum2+"个!"}
	  if(parseInt(strnum1)==parseInt(strnum2)){notice="<--提示-->\n\n变色处的字符数必须为"+strnum2+"个!"}
      eimobj.style.background='#F2DDDA';//失败后的颜色
      alert(notice);
      eimobj.focus();
      result=0;
    }
	else
    {
      eimobj.style.background='#FFFFFF';//正确后的颜色
      result=1;
	}
}
//判断当前元素正则表达式
function CheckExp(eimobj,str,notice)
{
 if(!eimobj.value.match(str)){loseback(eimobj,notice);}
 else{sucback(eimobj)}
}



