/*####################################################################################################################################################
																					检测脚本
																		 2007-11-17日更新 copyright 王可
																	正则表达式检测函数   用户其他检测 1.1更新
																			 解决firefox不兼容的问题
																				
####################################################################################################################################################*/


//取值
function GetValue(objid)
{
	switch ( $(objid).type )
	{
		case "text":
				return $(objid).value;
			break;
		case "textarea":
				return $(objid).value;
		case "password":
				return $(objid).value;
			break;
		case "select-one":
				if ($(objid).options.length < 1)
				{
					return "";
				}
				if( $(objid).options[$(objid).selectedIndex].value == "0" || $(objid).options[$(objid).selectedIndex].value == "" || $(objid).options[$(objid).selectedIndex].value == "请选择" )
				{
					return  "";
				}
				else
				{
					return $(objid).options[$(objid).selectedIndex].value;
				}
			break;
		case "radio":
				for( var i = 0 ; i < $(objid).length ; i ++ )
				{
					if ( $(objid)[i].checked )
					{
						return $(objid)[i].value;
					}
				}
			break;
		case "checkbox":
				if ( $(objid).checked )
				{
					return $(objid).value;
				} 
		default:
				return "";
			break;
	}
}


 function GetObjValue( objid )
	{
		var _objvalue = "" ;
		var _objarr = objid.split(',');
		
		if ( _objarr.length > 1 )
		{
			for ( var i = 0 ; i < _objarr.length ; i++ )
			{
				if ( i > 0 )
					_objvalue += ',';
				
				_objvalue += trim(GetValue(_objarr[i]));
				//alert(GetValue(_objarr[i]));
			}
			
		}
		else
		{
			_objvalue += trim(GetValue(_objarr[0]));
			//alert(GetValue(_objarr[0]));
		}
		//alert( _objvalue );
		return _objvalue;
	}


//赋值
function SetValue(objid , value)
{
	var objtype = $(objid).type;
	
	switch ( objtype )
	{
		case "text":
				$(objid).value = value;
		case "password":
				$(objid).value = value;
			break;
		case "textarea":
				$(objid).value = value;
			break;
		case "select-one":
				if ($(objid).options.length < 1)
				{
					return ;
				}
				else
				{
					for ( var i=0 ; i <  $(objid).options.length ; i ++ )
					{
						if ($(objid).options[i].value == value)
						{
							$(objid).options[i].selected = true;
						}
					}
					
				}
			break;
		case "radio":
				var o = $(objid);
				o.checked = true;
				/*alert( o.count );
				for( var i = 0 ; i < o.length ; i ++ )
				{
					if ( o[i].value == value )
					{
						o[i].checked = true;
					}
				}*/
			break;
		default:
				return "";
			break;
	}
}

//检查是否是数字
function isnumeric(num)
{
	if(num.constructor==Number)
	{
		return true
	}
	else
	{
	   return false
	}
}



//value 对象值 , parnt 正则表达式
function Regex( value , parnt)
{
	///(/S)+[@]{1}(/S)+[.]{1}(/w)+/


	// 检查 E-mail 是否已被注册
		
	//	var o = document.getElementById(objid)type
	//	if ()
        var e = value;
		//alert( parnt.test(e) );
		
        if(e != "") 
		{
            if(!parnt.test(e)) 
            {
                
				return false;
            } 
            else 
            { 
                return true;
            }
        }
		
}

//value 对象值 , parnt 正则表达式
function getRegexlength( value )
{
	///(/S)+[@]{1}(/S)+[.]{1}(/w)+/


	// 检查 E-mail 是否已被注册
		
	//	var o = document.getElementById(objid)type
	//	if ()
        var e = value;
		//alert( parnt.test(e) );
		
        if(e != "") 
		{
		   return e.replace( /[^x00-xff]/ ,  "xx").length;
        }
		else
		{
			return 0;
		}
		
}

//检测是否为空
	function CheckNull( objvalue  )
	{
		var e = objvalue;
		if( e == "" )
		{	
			
			return false;
		}
		else
		{
			return true;
		}
	}
	



	/*
 * 函数说明：去除头尾空格
 * 参数：	字符串
 * 返回值：	无
 * 时间：2006-5-12
 */
function trim(inputString) {
	return inputString.replace(/^ +/,"").replace(/ +$/,"");
}

function replace_comma(inputString ) {
	return inputString.replace(/\,/g,"");
}

	
String.prototype._indexOf = String.prototype.indexOf;
String.prototype.indexOf = function()
{
 if(typeof(arguments[arguments.length - 1]) != 'boolean'){
  return this._indexOf.apply(this.toLowerCase(),arguments);
 }
 else
 {
  var bi = arguments[arguments.length - 1];
  var thisObj = this;
  var idx = 0;
  if(typeof(arguments[arguments.length - 2]) == 'number')
  {
   idx = arguments[arguments.length - 2];
   thisObj = this.substr(idx);
  }
  
  var re = new RegExp(arguments[0],bi?'i':'');
  var r = thisObj.match(re);
  return r==null?-1:r.index + idx;
 }
}


/*######################################################################################################################################################
																checkDate
使用之前需要定义错误数组
错误样式LightStyle,
和状态跟踪变量Status

参数:
		1.验证对象obj form中的对象ID or ID[]  Name 根据不同的类型取得值
		2.长度验证 lg 为空不验证
		3.正则表达式  regstr 正则表达式 null不验证
		4.是否允许为空  AllowNull 是否允许为空
		5.是否比较相等 comparevalue null为不验证
		6.ajax验证 路径
		7.ajax验证 参数
		8.错误信息数组 Msg
		9.MsgPrint 显示错误信息的对象ID
												
######################################################################################################################################################*/

function CheckDate( objid , Lg ,  regstr , AllowNull , comparevalue  , Ajaxurl , Ajaxparam , Msg , MsgPrint)
{
	
	this.Status = false;

	this.ObjectInfo = function()
	{
		$(MsgPrint).innerHTML = Lpic + Msg[0];
		$(MsgPrint).className = "ShowStyle";
	}
	

	this.ObjectOnfocus = function ()
	{
		
		//Light
		var objvalue = GetObjValue( objid );
		
		if ( ! CheckNull( objvalue  ) )
		{
			$(MsgPrint).innerHTML = Lpic + Msg[0];
			$(MsgPrint).className = "LightStyle";
		}
	}

	this.ObjectOnBlur = function()
	{
			var AjaxBool;
			var objvalue = GetObjValue( objid );
			//alert(replace_comma(objvalue));
			if (  AllowNull  )
			{
				///检测信息
				var Av = objvalue.split(',');
				
				for ( var i=0 ; i < Av.length ; i ++)
				{
					if ( trim ( Av[i] ) == "" )
					{
						$(MsgPrint).innerHTML = Epic + Msg[1];
						$(MsgPrint).className = "ErrStyle";
						Status = Status && false;
						return;
					}	
				}		
			}
			if ( Lg != null && getRegexlength ( objvalue ) >= Lg)
			{//长度验证
				$(MsgPrint).innerHTML = Epic + Msg[2];
				$(MsgPrint).className = "ErrStyle";
				Status = Status && false;
				return;
			}
			if( comparevalue != null )
			{//对比验证
				
				var cv;
				if (  $(comparevalue) != null  )
				{
					cv =  GetValue(comparevalue);
				}
				else if ( getCookie(comparevalue) != null && getCookie(comparevalue) != ""  )
				{
					cv = getCookie(comparevalue);
				}
				else
				{
					cv = comparevalue;
				}
				
				if (  objvalue.toUpperCase() != cv.toUpperCase() )
				{
					$(MsgPrint).innerHTML = Epic + Msg[3];
					$(MsgPrint).className = "ErrStyle";
					Status = Status && false;
					return;
				}	
			}
			if ( objvalue != "" && regstr != null && ! Regex ( objvalue , regstr ) )
			{//正则表达式验证
				$(MsgPrint).innerHTML = Epic + Msg[4];
				$(MsgPrint).className = "ErrStyle";
				Status = Status && false;
				return;
			}
			
			if( Ajaxurl !=null &&  Ajaxparam != null )
			{//ajax验证
				
				var url = Ajaxurl;
				var pars = Ajaxparam + objvalue;
				//location.href=url + "?" + pars;
 
				var myAjax = new Ajax.Request(
												url,
												{method: 'get', parameters: pars , asynchronous:false
													,onUninitialized : function ()
													{
														
														$(MsgPrint).innerHTML = "检测中，请稍候...";
													}
													, onComplete: function( originalRequest )
													{//Ajax
														
														var Message = trim ( originalRequest.responseText );
														//alert(Message);
														switch( Message )
														{
															case "1":
																	AjaxBool = true;
																break;
															case "0":
																	$(MsgPrint).innerHTML = Epic + Msg[5];
																	$(MsgPrint).className = "ErrStyle";
																	Status = Status && false;
																	AjaxBool = false;
																break;
															case "-1":
																	$(MsgPrint).innerHTML = "Ajax Error";
																	$(MsgPrint).className = "ErrStyle";
																	Status = Status && false;
																	AjaxBool = false;
																break;
															default:
																	alert(Message);
																break;
														}
													}
													,onFailure: function()
													{
														 alert('error');
														 location.href=url + "?" + pars;
													}
										
												}
											);
			
				
					if ( ! AjaxBool )
					{
						return ;
					}
			}
			if ( Msg[6] != null && Msg[6] != "")
			{
				$(MsgPrint).innerHTML = Rpic + Msg[6];
				$(MsgPrint).className = "RightStyle";
			}
			else
			{
				$(MsgPrint).innerHTML = "";
				$(MsgPrint).style.display = "none";
			}
			Status = Status && true;
	}
}