/**
 * 公共JS函数: pesun.js
 * 这些函数是由公司人员写的。
 * 建议下个版本使用别人的 富客户端AJAX 框架，如 EXT 、 DOJO 。
 * JQUERY 的功能不够多,像表单验证这种常见的操作必须手写JS很麻烦
 * */
function doc_pesun()
{
	//只是为了配合 doxygen 软件生成帮助文档而专门写的
	//没有任何用处
}

/**********************************************************
 * JS TRIM函数，搜集来自网上
 * */
String.prototype.Trim = function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, "");
} 

/**
 * 检查输入框的数据长度是否符合要求
 * @param string id
 * @param int Min
 * @param int Max
 * @param string msg
 * @return bool
 * */
function checkLength(id,Min,Max) {
	var obj = document.getElementById(id);
	var value = obj.value.Trim();
	var len=value.length;
	if(len<Min || len>Max) {
		obj.focus();
		return false;
	}
	return true;
}

/**
 * 检查EMAIL格式是否正确
 * @param string id
 * @param string msg
 * @return bool
 * */
function checkEmail(id){
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  var obj = document.getElementById(id);
  var email=obj.value.Trim();

  if (!(!reg1.test(email) && reg2.test(email))) { // if syntax is valid
		 // this is also optional
		obj.focus();
		return false;
  }
  return true;
}






/**
 * 初始化XMLHTTP。
 * 由于 魏兴峰 还不会 JQUERY ，他写的 AJAX 异步通信只能靠最原始的 JS 
 * */
function initxmlhttp()
{
	var xmlhttp
	try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try{
			xmlhttp = new XMLHttpRequest();
		}catch(e){
			xmlhttp = false;
		}
	}
	if(!xmlhttp && window.createRequest){
		try{
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}


/**********************************************************
 * 多选列表函数，搜集来自网上
 * */
 
/*移除左边选中的列表项到右边*/  
function moveMultiSelect(select1,select2)   
{      
    if(!(select1&&select2))   
    {   
        return;   
    }   
    if(!hasOptions(select1))   
    {   
        return;   
    }   
    if(select1.selectedIndex==-1)   
    {   
        select1.selectedIndex=0;   
    }   
    for(var i=0;i<select1.options.length;i++)   
    {   
        if(select1.options[i].selected)   
        {       
            var oOption = document.createElement("OPTION");   
            oOption.setAttribute("text",select1.options[i].text);   
            oOption.setAttribute("value",select1.options[i].value);   
            select2.add(oOption);   
        }   
    }   
    clearSelectedOptions(select1);   
}   
  
/*移除左边的所有列表项到右边*/  
function moveAllMultiSelect(select1,select2)   
{   
    if(!(select1&&select2))   
    {   
        return;   
    }   
    if(!hasOptions(select1))   
    {   
        return;   
    }   
    for(var i=0;i<select1.options.length;i++)   
    {   
        var oOption = document.createElement("OPTION");   
        oOption.setAttribute("text",select1.options[i].text);   
        oOption.setAttribute("value",select1.options[i].value);   
        select2.add(oOption);   
    }   
    clearAllOptions(select1);   
}   
  
/*清空select所有options*/  
function clearAllOptions(oSelect)   
{   
    if(oSelect)   
    {   
        var ops=oSelect.options;   
        while(ops.length>0)   
        {   
            oSelect.remove(ops.length-1);   
        }   
    }       
}   
  
/*清空select所有选中的options*/  
function clearSelectedOptions(oSelect)   
{   
    if(oSelect)   
    {   
        for(var i=0;i<oSelect.options.length;i++)   
        {   
            if(oSelect.options[i].selected)   
            {   
                oSelect.remove(i--);   
            }   
        }   
    }       
}   
  
/*判断select是否有options*/  
function hasOptions(oSelect)   
{   
    if(oSelect)   
    {   
        return oSelect.options.length>0;   
    }   
    return false;   
}  



/**********************************************************
 * JS 验证,这些验证都是针对表单元素的
 * */
 
/**
 * 是否为 YY-MM-DD 的日期格式
 * */
function isDate(id){
	var obj = document.getElementById(id);
	var reg = /^((((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-))$/;
	if (reg.test(obj.value.Trim())==false) { 
		obj.focus();
		return false;
	}else{
		return true;
	}
}

/**
 * 是否为 HH:MM:SS 的时间格式
 * */
function isTime(id){//TODO
	var obj = document.getElementById(id);
	return true;
}

/**
 * 是否为整数
 * */
function isInit(id){
	var obj = document.getElementById(id);
	var reg = /(\d){1,}/;
	if (reg.test(obj.value.Trim())==false) { 
		obj.focus();
		return false;
	}else{
		return true;
	}
}
 
/**
 * 是否为 字母，数字，下划线 组合
 * */
function isAlphaNumX(id)
{
	var obj = document.getElementById(id);
	var reg = /[\w]/;
 	if (reg.test(obj.value.Trim())==false) { 
		obj.focus();
		return false;
	}else{
		return true;
	}
}

/**
 * 是否为身份证
 * */
function isIDCard(id)
{
	var obj = document.getElementById(id);
	var reg1 =/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; 
	var reg2 =/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/; 
	var reg  =/^(\d{18,18}|\d{15,15}|\d{17,17}(x|X))$/; 
 	//if (reg1.test(obj.value.Trim())==true||reg2.test(obj.value.Trim())==true) { 
	if (reg.test(obj.value.Trim())==true) {
		return true;
	}else{
		obj.focus();
		return false;
	}
}

/**
 * 是否为手机号
 * */
function isCellPhone(id)
{
	var obj = document.getElementById(id);
	var reg1 =/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/;
 	if (reg1.test(obj.value.Trim())==true) { 
		return true;
	}else{
		obj.focus();
		return false;
	}
}


/**
 * 是否为空
 * */
function isEmpty(id)
{
	var obj = document.getElementById(id);
	if(obj.value.Trim() == '') {	
		obj.focus();
		return false;
	}
	return true;
}

/**
 * 检查EMAIL格式是否正确
 * @param string id
 * @param string msg
 * @return bool
 * */
function isEmail(id)
{
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  var obj = document.getElementById(id);
  var email=obj.value.Trim();

  if (!(!reg1.test(email) && reg2.test(email))) { // if syntax is valid
		 // this is also optional
		obj.focus();
		return false;
  }
  return true;
}

/**
 * 检验两个值是否相等
 * @param string id1
 * @param string id2
 * @param string msg
 * */
function isEqual(id1,id2) {
	var obj1 = document.getElementById(id1);
	var obj2 = document.getElementById(id2);
	if(obj1.value.Trim() != obj2.value.Trim()) {		
		obj2.focus();
		return false;
	}
	else {
		return true;
	}
}

/**
 * 检查是否选中了下拉框
 * @param string id
 * @return bool
 * */
function isSelectSelected(id){
	var obj1 = document.getElementById(id);
	//alert(obj1.value);
	if(obj1.value.Trim()=="0"){	
	obj1.focus();
	return false;
	}
	else{return true;}
}

/**
 * 检查是否选中了单选按钮组
 * */
function isRadioSelected(id)
{ 
	var obj = document.getElementById(id);
	var radionum=0; 
	for (var i=0; i<obj.length; i++)
	{ 
		if ((obj[i].type == "radio")&&(obj[i].checked))radionum++; 		
	} 
	if (radionum==0) 
	{ 
		return false; 
	} 
	return true; 
} 	



/**
 * 功能：限制输入。只能输入 英文字母、数字、下划线，无法输入其他的。
 * 这些内容原本可以写在 html 内部，但是会给美工人员带来困难
 * @param string 在前台页面的元素ID
 * @author 魏兴峰
 * */
function restrictInput(id)
{
	var obj = document.getElementById(id);
	//obj.setAttribute('onkeyup',null);
	obj.onkeyup = function()
	{
		obj.value.Trim() = obj.value.Trim().replace(/[\W]/g,'');
	};
	//obj.setAttribute('onbeforepaste',null);
	obj.onbeforepaste = function()
	{
		clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''));
	};	
}

/**
 * 功能：id必须全为数字
 * @param  id
 * @author 邵世炳
 * */
function isNumber(id)
{
        var obj = document.getElementById(id);
		for(var i=0;i<obj.length;i++)
		{
			var ch=obj.charCodeAt(i);
			if(ch<48 || ch>57)
			{
				return false;
			}
		}
		return true;
}


//检查邮政编码是否合法
	function CheckZipCode(id)
	{
	 var obj = document.getElementById(id);
	 if(obj.value.length != 6)
		{
			return false;
		}
		else
		{
		   return true;
		}
	}	

