﻿function $() 
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
function equals(p1,p2,info)
{
      var strp1="";
      var strp2="";
      if(p1.value)
      {
         strp1=p1.value;
      }
      else
      {
         strp1=p1.innerText;
	  }
      if(p2.value)
      {
         strp2=p2.value;
      }
      else
      {
		 strp2=p2.innerText;
      }
	  if(strp1.length==0||strp2.length==0)
           return false;
	  if(strp1!=strp2)
      {
          if(info)
          {
               info=$(info.id); 
               info.style.fontSize="9pt";
               p2.value="";
               info.innerHTML="<b style=\"color:red;\"> *</b>两次输入域值不一致!";
          }
          return false;
      }
      return true;
}
function notNull(p,info)
{
	  var strp=p.value;
      if(strp.length==0)
      {
        if(info)
        { 
            info=$(info.id); 
            info.style.fontSize="9pt";
			info.innerHTML="<b style=\"color:red;\"> *</b>此域不能为空!";
            var id=info.id;
			addEvent(p,"focus",function(){ $(id).innerText="";})
        }
        return false;
      }
	  return true;
}
function checkEmail(email,info,type)
{
   var emailStr="";
   if(email.value)
	   emailStr=email.value;
   else
	   emailStr=email;
   if(emailStr.length==0)
   {
        if(info)
        {  
            info=$(info.id); 
            info.style.fontSize="9pt";
			info.innerHTML="<b style=\"color:red;\">*</b>此域不能为空!";
			var id=info.id;
			addEvent(p,"focus",function(){ $(id).innerText="";})
		}
		return false;
   }
   else 
   {
       var regex=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
	   if(!regex.test(emailStr))
       {
           if(info)
           {  
                info=$(info.id); 
                info.style.fontSize="9pt";
                if(info)
                {
					if(type)
                        info.innerHTML="<b style='color:red;'> *</b>输入的MSN格式不合法!";
                    else
				        info.innerHTML="<b style='color:red;'> *</b>输入的Email格式不合法!";
		        }
           }
	       return false;
	   }
   }
   return true;
}
function strNotNull(str,info)
{
    if(str.length==0)
    {  
       info=$(info.id); 
       info.style.fontSize="9pt";
       info.innerHTML="<b style=\"color:red;\"> *</b>此域不能为空!";
       return false;
    }
    return true;
}
function checkLength(field,size,info)
{
    var val=field.value;
    if(val.length>size)
    {  
        info=$(info.id); 
        info.style.fontSize="9pt";
        info.innerHTML="<b style=\"color:red;\"> *</b>此域的值超过了系统允许的长度!";
        return false;
    }
    return true;
}
function isUrl(str_url,inf)
{
    var re=/^http(s){0,1}:\/\/(\w+\.){1,6}([\\/]\w+)*/i;
    if (re.test(str_url))
        return true; 
    else
    {   
        var info=$(inf.id); 
        inf.style.fontSize="9pt";
        inf.innerHTML="<b style=\"color:red;\"> *</b>请输入合法的URL!";
        return false; 
    }
}
function clearInfo(inf)
{
    var info=$(inf.id); 
	inf.innerHTML="";
}
function addEvent(obj, eventName, func)
{
	if(obj.addEventListener)
		return obj.addEventListener(eventName, func, true);
	else if(obj.attachEvent)
	{
		obj.attachEvent("on" + eventName, func);
		return true;
	}
	return false;
}