﻿// JScript 文件
var w3c=(document.getElementById)? true: false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var pubinfo="请选择源码分类@必须选择源码分类@选择正确|||请填写源码名称@源码名称必须填写,且不能包含特殊字符，长度限制在120字符以内@输入正确|||多个关键字请以,分隔@关键字格式不正确@输入正确|||请选择开发工具@必须选择开发工具@选择正确|||请选择程序数据库@必须选择程序数据库@选择正确|||请选择程序开发语言@必须选择程序开发语言@选择正确|||请选择.NET版本@必须选择.NET版本@选择正确|||请选择程序难度@必须选择程序难度@选择正确|||请选择程序开源类型@必须选择程序开源类型@选择正确|||请选择程序授权方式@必须选择程序授权方式@选择正确|||请选择程序界面语言@必须选择程序界面语言@选择正确|||请选择是否含有源码@必须选择是否含有源代码@选择正确|||请输入源码作者@源码作者格式不正确@输入正确|||请输入官方网站@官方网站格式不正确@输入正确|||请输入演示地址@演示地址不正确@输入正确|||请输入下载地址@下载地址格式不正确@输入正确|||请输入源码介绍@源码介绍格式不正确@输入正确|||请输入QQ号(非必填)@QQ格式不正确@输入正确|||请输入MSN(非必填)@MSN格式不正确@输入正确";
var pl=pubinfo.split('|||');
var reginfo="请填写用户帐号@必须填写用户帐号@填写正确|||请输入帐号密码@必须输入帐号密码@填写正确|||请重新输入帐号密码@两次输入密码必须一致@填写正确|||请输入电子邮件地址@电子邮件格式不正确@填写正确|||请选择最适合的描述@请选择最适合的描述@选择正确|||请填写用户昵称@用户昵称格式不正确@填写正确";
var ri=reginfo.split('|||');
for(var i=0;i<ri.length;i++){
    ri[i]=ri[i].split('@');
}
var styleClassName="dokalert@dokerr@dokright";
var scn=styleClassName.split('@');
for(var i=0;i<pl.length;i++){
    pl[i]=pl[i].split('@');
}

function IeTrueBody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function GetScrollTop(){
 return ie ? IeTrueBody().scrollTop : window.pageYOffset;
}

function getViewportHeight() {
	      if (window.innerHeight!=window.undefined){ return window.innerHeight;}
	      if (document.compatMode=='CSS1Compat') {return document.documentElement.clientHeight;}
	      if (document.body) {return document.body.clientHeight; }
	      return window.undefined; 
        }
        function getViewportWidth() {
	      if (window.innerWidth!=window.undefined){ return window.innerWidth; }
	      if (document.compatMode=='CSS1Compat'){ return document.documentElement.clientWidth; }
	      if (document.body){ return document.body.clientWidth; }
	      return window.undefined; 
        }
        function getPos(el,sProp) {
	      var iPos = 0;
	      while (el!=null) {
		    iPos+=el["offset" + sProp];
		    el = el.offsetParent;
		  }
	      return iPos;
        }
        function getpopdivpos(popbindobj){
	      var _width = $("popdiv").offsetWidth;
	var _height = $("popdiv").offsetHeight;

	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();

	//var theBody = document.documentElement;
	var theBody = document.getElementsByTagName("BODY")[0];
	//theBody.style.overflow = "hidden";
	var scTop = parseInt(GetScrollTop(),10);
	var scLeft = parseInt(theBody.scrollLeft,10);
	var _top=getPos(popbindobj,"Top")+popbindobj.offsetHeight;

	//alert("scTop="+scTop+"&fullHeight="+fullHeight+"&_top="+_top+"_height="+_height+"&"+(scTop+fullHeight-_top-_height));
	if(scTop+fullHeight-_top-_height<0){
		$("popdiv").style.top = (scTop + (fullHeight - _height)) + "px";
	}else{
		$("popdiv").style.top =_top + "px";;
	}

	var _left=getPos(popbindobj,"Left");
	//alert("scLeft="+scLeft+"&fullWidth="+fullWidth+"&_left="+_left+"_width="+_width+"&"+(fullWidth-_left-_width<0));
	if(fullWidth-_left-_width<0){
		$("popdiv").style.left =  (scLeft + (fullWidth - _width)) + "px";
	}else{
		$("popdiv").style.left = _left + "px";
	}

        }
        function splitclassinc(obj){
          var r;
          r=obj.split("|");
          for(var i=0;i<=r.length-1;i++){
             r[i]=r[i].split(",");
          }
          return r;
        }

        //make data of popbody;
        function mkdata(_width,_cols,classname,obj,objid){
          var datahtml="";
          var classlist=splitclassinc(classname);
          var tdwidth=parseInt(_width/_cols-1);
          datahtml="<table width='99%'><tr>";
          for(var i=1;i<=classlist.length-1;i++){
            if(((i-1)%_cols==0)&&(i>=_cols)){
              datahtml+="</tr><tr>";
            }
            datahtml+='<td width="'+tdwidth+'"><a href="#" onclick="setobjvalue(\''+classlist[i][0]+'\',\''+classlist[i][1]+'\',\''+obj.id+'\',\''+objid+'\');return false;">'+classlist[i][1]+'</a></td>';
          }
          datahtml+="</tr></table>";
          return datahtml;
        }
        function setobjvalue(_value,_text,obj,objid){
          $(obj).value=_text;
          $(objid).value=_value;
          hidepopdiv();
        }
        function poplistdiv(obj,objid,classname,_width,_cols){
          //chgalertarea(numid);
          $("popheadtext").innerHTML=obj.getAttribute("title");
          $("popbody").innerHTML=mkdata(_width,_cols,classname,obj,objid);
          getpopdivpos(obj);
          $("popdiv").style.width=_width+"px";
          $("popdiv").style.display="";
        }
        function hidepopdiv(el){
	      var m=$("popdiv");
	      //alert(GetScrollTop());
	      if (el && m && m.style.display!="none") {
		    if ((document.body.scrollLeft + window.event.x > el.offsetLeft)
		    && (document.body.scrollLeft + window.event.x < el.offsetLeft + el.offsetWidth)
		    && (GetScrollTop() + window.event.y > el.offsetTop)
		    && (GetScrollTop() + window.event.y < el.offsetTop + el.offsetHeight)
		    || (document.body.scrollLeft + window.event.x > m.offsetLeft)
		    && (document.body.scrollLeft + window.event.x < m.offsetLeft + m.offsetWidth)
		    && (GetScrollTop() + window.event.y > m.offsetTop)
		    && (GetScrollTop() + window.event.y < m.offsetTop + m.offsetHeight)) {
		
		    } else {
			  m.style.display = 'none';
		    }
	      }else if(!el){
		    m.style.display = 'none';
	      }
	      
        }
        
        function hidepdiv(el,obj,objtype){
          if(el){hidepopdiv(el);}
          else{hidepopdiv();}
          //checkone(obj,objtype);
        }
         

String.prototype.isEmail = function(){
	var tmpStr = this;
	var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return email.test(tmpStr)
}
//├http地址验证
String.prototype.isUrl = function(){
	var url = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	var tmpStr = this;
	return url.test(tmpStr);
}
//├日期验证（第一部分）
String.prototype.isDateTime = function(){
	if(Date.parse(this)||Date.parseDate(this))
	{
		return true;
	}
	else
	{
		return false;
	}
}
String.prototype.isInteger = function()
{
	var _i = /^[-\+]?\d+$/;
	var _s = this; 
	return _i.test(_s);
}
Date.prototype.toIsoDate = function()
{
	var _d = this;
	var _s;
	_Y =_d.getFullYear();
	_M = _d.getMonth() + 1;
	_D = _d.getDate();
	_H = _d.getHours();
	_I = _d.getMinutes();
	_S = _d.getSeconds();
	with(_d)
	{
		_s = [getMonth() + 1,getDate(),getHours(),getMinutes(),getSeconds()];
	}
	for(var i = 0; i < _s.length; i++)
	{
		if (_s[i].toString().length == 1)_s[i]= '0'+_s[i];
	}
		return (_Y + '-'+_s[0]+'-'+_s[1]+' '+_s[2]+':'+_s[3]+':'+_s[4])
}
//├日期验证（第二部分）
Date.parseDate = function(str, fmt) {
	fmt = fmt||"%Y-%m-%d %H:%M";
	var today = new Date();
	var y = 0;
	var m = -1;
	var d = 0;
	var a = str.split(/\W+/);
	var b = fmt.match(/%./g);
	var i = 0, j = 0;
	var hr = 0;
	var min = 0;
	for (i = 0; i < a.length; ++i) {
		if (!a[i])
			continue;
		switch (b[i]) {
		    case "%d":
		    case "%e":
			d = parseInt(a[i], 10);
			break;
		    case "%m":
			m = parseInt(a[i], 10) - 1;
			break;
		    case "%Y":
		    case "%y":
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
			break;
		    case "%b":
		    case "%B":
			for (j = 0; j < 12; ++j) {
				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
			}
			break;
		    case "%H":
		    case "%I":
		    case "%k":
		    case "%l":
			hr = parseInt(a[i], 10);
			break;
		    case "%P":
		    case "%p":
			if (/pm/i.test(a[i]) && hr < 12)
				hr += 12;
			else if (/am/i.test(a[i]) && hr >= 12)
				hr -= 12;
			break;
		    case "%M":
			min = parseInt(a[i], 10);
			break;
		}
	}
	if (isNaN(y)) y = today.getFullYear();
	if (isNaN(m)) m = today.getMonth();
	if (isNaN(d)) d = today.getDate();
	if (isNaN(hr)) hr = today.getHours();
	if (isNaN(min)) min = today.getMinutes();
	if (y != 0 && m != -1 && d != 0)
		return new Date(y, m, d, hr, min, 0);
	y = 0; m = -1; d = 0;
	for (i = 0; i < a.length; ++i) {
		if (a[i].search(/[a-zA-Z]+/) != -1) {
			var t = -1;
			for (j = 0; j < 12; ++j) {
				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
			}
			if (t != -1) {
				if (m != -1) {
					d = m+1;
				}
				m = t;
			}
		} else if (parseInt(a[i], 10) <= 12 && m == -1) {
			m = a[i]-1;
		} else if (parseInt(a[i], 10) > 31 && y == 0) {
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
		} else if (d == 0) {
			d = a[i];
		}
	}
	if (y == 0)
		y = today.getFullYear();
	if (m != -1 && d != 0)
		return new Date(y, m, d, hr, min, 0);
	return today;
};
//扩展完成
//对象定义
var vform = new Object;
//获取弹出提示的显示位置
vform.getAbsolutePos = function(el) {
	var _p = { x: 0, y: 0 };
	 do{
				_p.x += (el.offsetLeft - el.scrollLeft);
				_p.y += (el.offsetTop - el.scrollTop); 
		}
		 while(el=el.offsetParent)
     return _p;
      };
vform.toString = function()
{
	return("WAEMZ.FormValidProgram");
}
vform.rules = new Array;
vform.rules.add = function(obj,minLength,dataType,errmsg,maxLength,rule,patams,isChoose,hiddenId,classList,widthPx,colsCount)
{
    var curlen = this.length;
        this[curlen] = [obj,minLength,dataType,errmsg,maxLength,rule,patams,isChoose,hiddenId,classList,widthPx,colsCount];
        //this[curlen] = [ 0 ,    1    ,    2   ,   3  ,   4  ,  5 ,   6  ,    7  ,    8  ,    9    ,    10  ,    11 ];
    return this.length;
}
vform.init= function()
{
	if(document.getElementById(this.form_id))
	{
		//获取表单
		var o = document.getElementById(this.form_id);
		//遍历规则
		for(var i = 0 ;i< this.rules.length;i++)
		{
			_r = this.rules[i]
			//如果存在元素，则添加验证程序
			if(_o = o.elements[_r[0]])
			{
			   
				//判断是是否必填,是否有最小长度
				if(_r[1] > 0 )
				{
					_o.required = true;//必填的含义和最小长度为1是一样的
					_o.minLength = parseInt(_r[1]);
				}
				else
				{
					_o.required = false;
					_o.minLength = 0;
				}
				//判断是否有最大长度;
				if(_r[4])
				{
					_o.maxLength = parseInt(_r[4]);
				}
				//判断是否为选择
				if(_r[7])
		        {
		            _o.isChoose=true;
				    _o.inputId=_r[0];
				    _o.hiddenId=_r[8];
				    _o.classList=_r[9];
				    _o.widthPx=_r[10];
				    _o.colsCount=_r[11];
				    _o.mkdata=function()
				    {
                        $("popbody").innerHTML="";
                        var classlist=splitclassinc(this.classList);
                        var tdwidth=parseInt(this.widthPx/this.colsCount-1);
                        var oDiv=document.createElement('div');
                        with(oDiv)
                        {
                            //style.width="99%";
                        }
                        for(var i=1;i<=classlist.length-1;i++)
                        {
                            if(((i-1)%this.colsCount==0)&&(i>=this.colsCount))
                            {
                                var spDiv=document.createElement('div');
                                with(spDiv)
                                {
                                    style.clear="both";
                                }
                                oDiv.appendChild(spDiv);
                            }
                            var tDiv=document.createElement('div');
                            with(tDiv)
                            {
                                style.styleFloat="left";
                                style.width=tdwidth+"px";
                            }
                            var oA=document.createElement('a');
                            with(oA){
                                style.lineHeight="20px";
                            }
                            oA.href="#";
                            oA.innerHTML=classlist[i][1];
                            oA.inputId=this.inputId;
                            oA.hiddenId=this.hiddenId;
                            oA.listValue=classlist[i][0];
                            oA.onclick=function()
                            {
                                $(this.inputId).value=this.innerHTML;
                                $(this.hiddenId).value=this.listValue; 
                                hidepopdiv();
                                $(this.inputId).validInput();
                                return false;
                            }
                            tDiv.appendChild(oA);
                            $("popbody").appendChild(tDiv);
                            
                         }
				    }
				    _o.poplistdiv=function()
				    {
				        $("popheadtext").innerHTML=$(this.inputId).getAttribute("title");
                        this.mkdata();
                        getpopdivpos($(this.inputId));
                        $("popdiv").style.width=this.widthPx+"px";
                        $("popdiv").style.display="";
				    }
				}
				else{
				    _o.isChoose=false;
				}
				
				//添加长度验证函数
				_o.validLength = function ()
				{
					var b =true;
					if(this.minLength)
					{
						b = (this.minLength <= this.value.length);
					}
					if(this.type == 'textarea' && this.maxLength )
					{
						b = b && (this.maxLength >= this.value.length );
					}
					return (b);
				}
				//添加验证，进行格式验证
				switch(_r[2])
				{
					case 'e-mail':
						_o.validate = function()
						{
							this.isvalid = this.validLength() && this.value.isEmail();
							return (this.isvalid);
						};
						break;
					case 'url':
						_o.validate = function()
						{
							if (this.value.substring(0,7) != 'http://')this.value = 'http://' +this.value;
							this.isvalid = this.validLength() && this.value.isUrl();
							return (this.isvalid);
						}
						break;
					case 'date':
						_o.validate = function()
						{
							var _d = Date.parse(this.value)||Date.parseDate(this.value);
							this.value =  _d.toIsoDate();
							
							this.isvalid = this.validLength() && this.value.isDateTime();
							return (this.isvalid);
							a=a>b?1:1;
						}
						break;
					case 'number':
						_o.validate = function()
						{
							this.isvalid = this.validLength() && this.value.isInteger();
							return (this.isvalid);
						}
						break;
					case 'any':
						_o.validate = function()
						{
							this.isvalid = this.validLength();
							return  this.isvalid
						}
						break;
					default :
						var regexp = /^\\\w+$/;
						if ( regexp.test(_r[2]))//表示必须和同表单下的某个字段的值一样。用于重复输入的验证
						{
							_el = _r[2].substring(1);
							if (o.elements[_el]){
								_o.equal = _el;
								_o.validate = function()
								{
									if(_o = this.form.elements[this.equal])
									{
										if ( (_o.value == this.value) && this.validLength())
										{
											return true;
										}else {
										return false;
										}
									}else{
										alert('setup error');
									}
								
								}
							}else
							{
								alert(_el + 'is not a valid form element');
								_o.validate = function(){return true;}
							}
						}
						var regexp1 = /^\\(==|!=|>=|<=|>|<)/;
						if ( regexp1.test(_r[2]) )
						{
							_s0 = _r[2];
							_s1 = RegExp.$1;
							_s2 = _s0.replace(regexp1,'');
							_operator = _s1.substring(0);//比较操作符
							var regexp2 = /^\w+$/;
							if (regexp2.test(_s2))//是一个标志符，整数 或者变量
							{
								_o.operation = _operator+_s2;
								_o.validate = function()
								{
									_b = true;
									if (this.value.length !=0)
									{
										_b = eval(this.value+this.operation+';');
									}									
									_b = _b && this.validLength();
									return _b;
								}
							}
						};
						break;
						
				}
				//添加验证提示(div标签)并初始化
				var _p = vform.getAbsolutePos(_o);
				_o.tip = new tip(_r[3],vform.err_class,_p.x+_o.offsetWidth+10,_p.y,_r[6]);
				_o.tip.init();
                
                //获得焦点时，开始处理。
                _o.onfocus =function(e){
                    this.tip.showalert();
                }
                //如果是弹出选择框，单击时开始处理。
                if(_o.isChoose){
                    _o.onclick=function(e){
                        //alert($(this.inputId))
                        this.poplistdiv();
                    }
                }
                
                 //添加焦点失去和选择后的验证
				 _o.validInput=function(){
                    if(this.minLength || this.value.length >0) 
					{ 
						if( this.validate() )
						{
							this.tip.showok();
						}else
						{
							this.tip.show();//显示错误信息
							//this.focus(); 添加这句在ie里会导致死循环 :(
							return false;
						}
					}
                }
				//失去焦点时，开始验证
				_o.onblur =function(e)
				{
					return this.validInput();
				}
			}
		}
	//焦点验证可能会失败,所以最后需要表单提交前的验证作为最后的补充。
		document.getElementById(this.form_id).onsubmit = function()
		{
			var valid = true;
			for(i=0;i<this.elements.length;i++)
			{
				_o = this.elements[i];
				if(_o.minLength && !_o.validate())
				{
					_o.tip.show();
					valid = false;
				}
			}
			return valid;
		}
	}
}
//弹出提示定义
function tip(text,className,x,y,_parentNode)
{
	var o = document.createElement("div");
	o.style.display = "none";
	//o.innerHTML = text[1];
	//var t = document.createTextNode(text);
	$(_parentNode).appendChild(o);
	//o.appendChild(t);
	
	this.init = function(dis)
	{
		o.style.left = x+"px";
		o.style.top = y+"px";
		o.style.zindex = 100;
		if(dis)
		{
			o.style.display = "";
		}
		else
		{
			o.style.display = "none";
		}
	}
	this.show = function()
	{
	    o.className = className[1];
	    o.innerHTML = text[1];
		o.style.display = "";
	}
	this.showok = function()
	{
	    o.className = className[2];
	    o.innerHTML = text[2];
        o.style.display = "";
	}
	this.showalert = function()
	{
	    o.className = className[0];
	    o.innerHTML = text[0];
        o.style.display= "";
	}
	this.hide = function()
	{
		o.style.display = "none";
	}
}

