//合并   js/my/validation.js       js/common/simplifyFunction.js        js/common/cookie.js     js/user/loginLayer.js

//---------------------------- js/my/validation.js --------------------------------
//会员中心注册验证类

Element=function()
{
	this.Element();
}
Element.prototype.Element=function()
{
	this.self=null;
}
Element.prototype.$=function(obj)
{
	this.self=typeof obj=="string"?document.getElementById(obj):obj;
}
Element.prototype.setHTML=function(html)
{
	this.self.innerHTML=html;
}
Element.prototype.getHTML=function()
{
	return this.self.innerHTML;
}
Element.prototype.getParentElement=function()
{
	try{
		return this.self.parentNode;
	}catch(e){
		return null;
	}
	//return this.self.parentNode?this.self.parrentNode:null;
}

Element.prototype.setAttribute=function(attrib,value)
{
	this.self.setAttribute(attrib,value);
}
Element.prototype.create=function(element)
{
	this.self=document.createElement(element);
}
Element.prototype.createByText=function(elementText)
{
	this.self=document.createElement(elementText);
}
Element.prototype.insertBefore=function(element)
{
	return this.getParentElement().insertBefore(element,this.self);
}
Element.prototype.insertAfter=function(element)
{
	var tmp=this.insertBefore(element,this.self);
	var tmp2=this.removeChild();
	this.$(tmp);
	this.insertBefore(tmp2,this.self);
	this.$(tmp2);
}
Element.prototype.removeChild=function()
{
	
	return this.getParentElement().removeChild(this.self);
}

_s=new Element();

Validation=function()
{	
	this.imgSrc;
	this.Validation();
}
Validation.prototype.setImgSrc=function(path)
{
	this.imgSrc=path;
}

Validation.prototype.Validation=function()
{
	this.self=null;
}

//过滤空格
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}

//检测字符（一个中文按两个字符处理）的长度
Validation.prototype.getLength=function(str)
{
	var dou; 
	var asc; 

	try{
		dou = str.match(/[^\x00-\xff]/ig).length*2; 
	}catch(e){
		dou=0;
	}

	try{
		asc = str.match(/[\x00-\xff]/ig).length;
	}catch(e){
		asc = 0;
	}
	return dou+asc;
}


//表单提交时的非空验证
Validation.prototype.isEmpty=function(frm,funcs)
{
	var items = frm.getElementsByTagName("input");
	var flag = true;
	for(var i = 0;i<items.length;i++){		
		var tag = items[i].getAttribute("empMsg");
		
		if(tag)
		{	
			var params = eval(items[i].getAttribute("params"));			
			
			if(params)
			{				
				var temp="";
				for(var k = 0;k<params.length;k++){
					temp += document.getElementById(params[k]).value;					
				}
				
				if(temp.trim()==""){
					flag = false;
					items[i].focus()
					this.showErrInfo(items[i],tag);
					
				}else{
					//this.removeErrInfo(items[i]);
				}
			}else{
				if(items[i].value.trim()=="")
				{
						flag = false;
						items[i].focus();
						this.showErrInfo(items[i],tag);
				}else
				{	
					//this.removeErrInfo(items[i]);
				}
			}
		}		
	}
	
	//验证列表框
	var sItems = frm.getElementsByTagName("select");		
	//定义要将提示显示到哪个控件后面
	var showTar;
	/*
	var	groupShow;
	var groupMsg;
	var group = new Array();
	var count=0;
	*/
	for(var j=0;j<sItems.length;j++)
	{
		//拥有empMsg属性的控件表明需要验证
		var tag = sItems[j].getAttribute("empMsg");
		if(tag)
		{
					
			showTar = sItems[j];
				if(sItems[j].selectedIndex == 0)
				{			
					flag = false;					
					this.showErrInfo(showTar,tag);
				}
			/*
			showTar = document.getElementById(sItems[j].getAttribute("target"));
			//如果该控件没有target属性，说明它单独验证并不属于任何组（仅仅支持一组控件的检测，待扩展）
			if(!showTar)
			{
				showTar = sItems[j];
				if(sItems[j].selectedIndex == 0)
				{			
					flag = false;					
					this.showErrInfo(showTar,tag);
				}else{						
						//this.removeErrInfo(showTar);
				}	
			}else{ 
				//把以组为单位的控件放入一个数组里
				group[count++] = sItems[j];	
				groupShow = showTar;
				groupMsg = tag;			
			}	*/			
		}
	}
	
	/*
	//对组中列表框统一验证
	var mark = group.length;
	for(var i = 0;i<group.length;i++)
	{
		if(group[i].selectedIndex==0)
		{
			mark--;
		}
	}
	//只有当所有验证都通过时方移除提示框
	if(mark==group.length)
	{	
		this.removeErrInfo(groupShow);
	}else{
		flag = false;					
		this.showErrInfo(groupShow,groupMsg);
	}
	 */
	 	
	if(funcs){ 	
	for(var i=0;i<funcs.length;i++)
	{
		if(!funcs[i]())
		{
			flag = false;
		}
	}
	}
	
	//var errInfo = document.getElementsByName("div_err_info");
	var errInfo = document.getElementsByTagName("div");
	
	for(var i=0;i<errInfo.length;i++){
		if(errInfo[i].getAttribute("class")=="err")
		{
			//alert(errInfo[i].getAttribute("class"));
			flag = false;
		}
	}
	return flag;
}

//显示错误信息提示
Validation.prototype.showErrInfo=function(item,msg)
{		
		this.removeErrInfo(item);
		_s.create("div");
		_s.setHTML(this.imgSrc+msg);
		_s.setAttribute("className","err");
		_s.setAttribute("class","err");				
		_s.setAttribute("name","div_err_info");
		_e=new Element();
		_e.$(item);
		_e.insertAfter(_s.self);
}

//去掉所有错误提示信息
Validation.prototype.removeErrInfo=function(item){
		var s2 = item.parentNode;	
		var div1 = s2.getElementsByTagName("div");			
		for(var i = 0;i<div1.length;i++){
			s2.removeChild(div1[i]);
		}
}

//即时检测两控件值是否相等
Validation.prototype.checkEqual=function(item,s,msg)
{
	var iValue = item.value;
	var tValue = document.getElementById(s).value;

	if(iValue!=tValue)
	{
		this.showErrInfo(item,msg);
	}else
	{
		this.removeErrInfo(item);
	}
}


//即时检测文本框(支持正则表达式，支持利用属性控制的长度验证！)
Validation.prototype.checkText=function(item,regExps,msgs,pos){
	
	//获取文本框值
	var vValue = item.value.trim().toLowerCase();
	var flag = true;
	if(!regExps)
		regExps = new RegExp();
	//只有值不为空时方执行验证
	if(vValue.trim()!=""){
		//首先执行value的基于正则表达式的验证
		var length = regExps.length;
		
		if(!pos)
		{
			pos=[];
		}
			for(var i = 0;i<length;i++)
    		{    		
    			if(pos[i]==1)
    			{
    				if(regExps[i].test(vValue)){
	    			this.showErrInfo(item,msgs[i]);
					flag = false;					
					break;
	    			}
    			}else{
	    			if(!regExps[i].test(vValue)){
		    			this.showErrInfo(item,msgs[i]);
						flag = false;					
						break;
		    		}
    			}    		
    		}		
    	
		//如果该控件限制了输入字符个数限制再执行长度验证
		var min = item.getAttribute("minLength");
		var max = item.getAttribute("maxLen");		
		if(min)
		{
			var len = this.getLength(vValue);
			
			if(len<min || len>max)
			{				
				this.showErrInfo(item,item.getAttribute("lengthMsg"));
				flag=false;				
			}
		}
		
		if(flag)
		{
			this.removeErrInfo(item);
		}
	}
	
	return flag;
}


//--------------js/common/simplifyFunction.js-------------------------------------
/**
*得到表单控件的value
*@param: 表单控件本身或者其id
*@return: 表单控件的value
*/
 function $V(param)
 {
  if(typeof(param)=='object')
  {
    return param.value;
  }
  if(typeof(param)=='string')
  {
  return $(param).value;
  }
 } 
 
 
 
/**
 * document.getElementById()的简化写法.
 */
function $(elementId)
{
   return document.getElementById(elementId);
}

/**
 * document.getElementsByName()的简化写法
 */
function $_(elementName)
{
   return document.getElementsByName(elementName);
}

/**
 * document.body.getElementsByTagName()的简化写法
 */
function _$_(tagName)
{
   return document.body.getElementsByTagName(tagName);
}

//----------------- js/common/cookie.js ---------------------------------------

function Cookie()
{
this.getClassName=function(){return "Cookie";}
}

Cookie.prototype.defaultInvalidHours=48;

/**
*删除指定名称cookie
*@param: parameter1  指定删除的name
*@param: parameter1  指定删除的path
*@param: parameter1  指定删除的domain
*/
Cookie.prototype.delCookie=function()
{
   var args=this.delCookie.arguments;
   var path=null;
   var domain=null;
   if(args.length>0)
   {
   var date = new Date();
   date.setTime(date.getTime() - 10000);
   var name=args[0];
   if(args.length>1)
   var path=args[1];
   if(args.length>2)
   var domain=args[2];
   var delCkStr=name + "=a;"+(path==null?"":("path="+path+";"))+(domain==null?"":("domain="+domain+";"))+"expires=" + date.toGMTString();
   document.cookie = delCkStr;
   }
}


/**
*添加cookie 
*@param: parameter1 name (必填)
*@param: parameter2 value (必填)
*@param: parameter3 过期时间.单位:小时,如无此参数,则使用默认值，暂定48小时 (可选)
*@param: parameter4 path (可选)
*@param: parameter5 domain (可选)
*@param: parameter6 secure (可选)boolean
*/
Cookie.prototype.addCookie=function()
{
var args=this.addCookie.arguments;
if(args.length>1)
{
   var objName=args[0];
   var objValue=args[1];
   var expires=null;
   var objHours=(args.length>2 && args[2]!=null)? args[2]:this.defaultInvalidHours;   
   //没有给出cookie过期时间,则为默认值,暂定48小时
   	var path = (args.length > 3) ? args[3] : null;
	var domain = (args.length> 4) ? args[4] : null;
	var secure = (args.length > 5) ? args[5] : false;
   if(objHours > 0){
    var date = new Date();
    var ms = objHours*3600*1000;
    date.setTime(date.getTime() + ms);
    expires=date.toGMTString();
   }
   	document.cookie = objName + "=" + escape (objValue) +((expires == null) ? "" : ("; expires="+ expires))
		+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
		+((secure == true) ? "; secure" : "");
}
}


/**
*根据cookieName返回value,如果不输入参数，则返回所有的cookie信息。不存在的话，返回null
*@param: parameter1 name
*/
Cookie.prototype.getCookie=function()
{
var allcookies = document.cookie;
var args=this.getCookie.arguments;
if(args.length==0)
{
return allcookies;
}
else{
var cookie_name=args[0];
var cookie_pos = allcookies.indexOf(cookie_name);
var value=null;
if (cookie_pos != -1)
{
cookie_pos += cookie_name.length + 1;
var cookie_end = allcookies.indexOf(";", cookie_pos);

if (cookie_end == -1)
{
cookie_end = allcookies.length;
}

 value = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return value;
}
}

Cookie.prototype.isEnabled=function()
{
return (navigator.cookieEnabled);
}

Cookie.prototype.toString=function()
{
 return "[all cookies:"+this.getCookie()+"]";
}


//----------------js/user/loginLayer.js----------------------------------------

function login()
{
  if(saveAccount()){
  
  var url = location.href;

  var pos = url.indexOf("#");
  if( pos != -1) {
     url = url.substring(0,pos);
  }
  var posT=url.indexOf("type=");
  if(posT!=-1)
 {
	   if(url.indexOf("&type")!=-1)
	   {
	     posT=url.indexOf("&type");
	   }
	   else
	   {
	    posT=posT-1;
	   }
       url=url.substring(0,posT) 
 }

 $("returnUrl").value=url;
 $("failedUrl").value=url;//window.location.href;
 $('frmLogin').submit();

  }
}

function hiddenLoginLayer()
{
$('massage_box').style.visibility='hidden'; 
$('mask').style.visibility='hidden'
$('logErr').style.display="none";
}