/**

VALIDATOR.JS by Steven on 2007-4-2

version:1.0.0

*/
/**
* 为String添加trim(),ltrim(),rtrim()方法
*/

			var response = null 

			

			function prompt2(promptpicture, prompttitle, message, sendto,v) { 

				promptbox = document.createElement('div'); 
				var evt = getEvent();
				promptbox.setAttribute ('id' , 'prompt') 

					document.getElementsByTagName('body')[0].appendChild(promptbox) 

					promptbox = eval("document.getElementById('prompt').style") 

					promptbox.position = 'absolute' 

					promptbox.top = evt.clientY-dojo.byId("prompt").offsetHeight-16;

					promptbox.left = evt.clientX-dojo.byId("prompt").offsetWidth; 

					promptbox.width = 300 

					promptbox.border = 'outset 1 #bbbbbb' 

					document.getElementById('prompt').innerHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%'><tr valign='middle'><td width='22' height='22' style='text-indent:2;' class='titlebar'><img src='" + promptpicture + "' height='18' width='18'></td><td class='titlebar'>" + prompttitle + "</td></tr></table>" 

					document.getElementById('prompt').innerHTML = document.getElementById('prompt').innerHTML + "<table cellspacing='0' cellpadding='0' border='1' width='100%' class='promptbox'><tr><td>" + message + "</td></tr><tr><td><input type='text' id='promptbox' onblur='this.focus()' class='promptbox' style='width:280px;'></td></tr><tr><td align='right'><br><input type='button' class='prompt' value='OK' onMouseOver='this.style.border=\"1 outset #dddddd\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='" + sendto + "(document.getElementById(\"promptbox\").value,\""+v+"\"); document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'> <input type='button' class='prompt' value='Cancel' onMouseOver='this.style.border=\"1 outset transparent\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'></td></tr></table>" 

					document.getElementById("promptbox").focus() 

				} 

	function setCookie(name, value, expires, path, domain)
	{
	    if(!expires) expires = -1;
	    if(!path) path = "/";
	    var d = "" + name + "=" + value;
	
	    var e;
	    if (expires < 0) {
	        e = "";
	    }
	    else if (expires == 0) {
	        var f = new Date(1970, 1, 1);
	        e = ";expires=" + f.toUTCString();
	    }
	    else {
	        var now  = new Date();
	        var f = new Date(now.getTime()  + expires * 1000);
	        e = ";expires=" + f.toUTCString();
	    }
	    var dm;
	    if(!domain){
	        dm = "";
	    }
	    else{
	        dm = ";domain=" + domain;
	    }
	
	    document.cookie = name + "=" + value + ";path=" + path + e + dm;
	};

function getCookie(a)
{
    var b = String(document.cookie);
    var c = b.indexOf(a + "=");

    if (c != -1) {
        var d = b.indexOf(";", c);
        d = d == -1 ? b.length : d;
        c = c + a.length + 1;
        if(b.charAt(c)=='"'&&b.charAt(d-1)=='"'){
            c+=1;d-=1;
        }
        if(c>=d)
            return "";
        return b.substring(c, d);
    }

    return "";
};

	function login(){
		if(validateRequired("loginForm","loginName,loginPassword",'<s:text name="common.all.all.loginname.title"/>,<s:text name="common.all.all.password.title"/>')==false)
			return false;
		dojo.io.bind({
			url:'foreLogin!login.shtml',
			handle:function(type,data,evt){
				if(type=="load"){
					dojo.byId("loginTD").innerHTML=data.trim();
					if(dojo.byId("loginError"))
						alert(dojo.byId("loginError").value);
				}else
					alert(data.message);
			},
			formNode:dojo.byId("loginForm")
		});
	}
	function exitSys(){
		dojo.io.bind({
			url:'foreLogin!exitLogin.shtml',
			handle:function(type,data,evt){
				if(type=="load"){
					dojo.byId("loginTD").innerHTML=data.trim();
				}
			}
		});
	}
function openWin(url,width,height){
	window.open(url,'','height='+height+',width='+width+',toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
}
function selectSingleMulti(o,id,type){
	var chS ;
	if(type=="1"){
		chS = document.getElementsByName(id);
	}else
		chS = document.getElementsByName(o.name);
	
	if(type=="1"){
		if(o.checked){
			if(chS!=null && chS.length>0){
				for(i=0;i<chS.length;i++){
					chS[i].checked=true;
				}
			}
		}else{
			if(chS!=null && chS.length>0){
				for(i=0;i<chS.length;i++){
					chS[i].checked=false;
				}
			}
		}
	}else{
		if(o.checked){
			var b =true;
			if(chS!=null && chS.length>0){
				for(m=0;m<chS.length;m++){
					if(!chS[m].checked){
						b = false;
						break;
					}
				}
			}
			if(b)
				document.getElementById(id).checked=true;
		}else{
			document.getElementById(id).checked=false;
		}
	}
	
}

function openWin(url,width,height){
	var left = parseInt((screen.availWidth/2) - (width/2));//屏幕居中
    var top = parseInt((screen.availHeight/2) - (height/2));
	
	window.open (url, '', 'height='+height+', width='+width+', top='+top+', left='+left+', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')  
}

var errorMsg=""; // PUBLIC error message String
var focusElement=null; // PUBLIC focus Element
var checkVal=null; // PUBLIC check Value

	function nextPage(){
		var pc = dojo.byId('pageList.currentPage').value;
		dojo.byId('pageList.currentPage').value=parseInt(pc)+1;
		dojo.io.bind(
			{
				url:"pickerEntity!nextPage.shtml",
				mimetype:'text/plain',
				handler:function(type,data,evt){
					dojo.byId('pickerDetailListInfo').innerHTML=data;
				},
				formNode:document.forms[0]
			}
		);
	}
	
	function prevPage(){
		var pc = dojo.byId('pageList.currentPage').value;
		dojo.byId('pageList.currentPage').value=parseInt(pc)-1;
		dojo.io.bind(
			{
				url:"pickerEntity!nextPage.shtml",
				mimetype:'text/plain',
				handler:function(type,data,evt){
					dojo.byId('pickerDetailListInfo').innerHTML=data;
				},
				formNode:document.forms[0]
			}
		);
	}
	function firstPage(){
		dojo.byId('pageList.currentPage').value=0;
		dojo.io.bind(
			{
				url:"pickerEntity!nextPage.shtml",
				mimetype:'text/plain',
				handler:function(type,data,evt){
					dojo.byId('pickerDetailListInfo').innerHTML=data;
				},
				formNode:document.forms[0]
			}
		);
	}
	
	function lastPage(){
		dojo.io.bind(
			{
				url:"pickerEntity!lastPage.shtml",
				mimetype:'text/plain',
				handler:function(type,data,evt){
					dojo.byId('pickerDetailListInfo').innerHTML=data;
				},
				formNode:document.forms[0]
			}
		);
	}

	function hideDiv(){
		dojo.byId('pickerDetailListInfo').style.display='none';
		dojo.byId('pickerDetailListInfo').innerHTML="";
		dojo.byId("bg").style.visibility="hidden";
	}
	
	function showDiv(){
		dojo.byId("bg").style.visibility="visible";
		var divId = dojo.byId("pickerDetailListInfo");
		divId.innerHTML="";
		var v_left =(document.body.clientWidth-divId.clientWidth)/2;
		var v_top = (document.body.clientHeight-divId.clientHeight)/2;
		divId.style.display="";
		
		divId.style.left = v_left;
		divId.style.top = v_top;
	}
	
function changePage(){
	var pcount = dojo.byId("pageList.currentPage").value;
	if(parseInt(pcount)>0){
		dojo.byId("pageList.currentPage").value = parseInt(pcount)-1;		
		nextPage();				
	}else
		firstPage();
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

function trim(val) {
	val=ltrim(val);
	val=rtrim(val);
	return val;
}

function ltrim(val) {
	for(var l=0;l<val.length;) {
		if(val.charAt(l)==' ')
			val=val.substring(1);
		else
			break;
	}
	return val;
}

function rtrim(val) {
	for(var r=val.length-1;r>0;r--) {
		if(val.charAt(r)==' ')
			val=val.substring(0,val.length-1);
		else
			break;
	}
	return val;
}

function len(str) {
	var len=str.length;
	var arr=str.match(/[^\x00-\x80]/ig);
	if(arr!=null) len += arr.length;
	return len;
};

/**
 *	得到字符串的长度(增强了对汉字的识别,一个汉字的长度为2)
 *	Steve Add at 2007-04-02
 */
function getStringLength(val) {
	num=val.length
	var arr=val.match(/[^\x00-\x80]/ig)
	if(arr!=null) num += arr.length
	return num;
};

/**
PUBLIC check and get a FORM object
2007-04-02 by Steven
*/
function checkFormArg(form) {
	var obj;
	if(typeof(form)=="object")
		obj=form;
	else if(isNaN(form))
		obj=document.forms[form];
	else
		obj=document.forms[form];

	if(typeof(obj)!="object")
		alert("error form!");
	else
		return obj
};
/** END */
/**
PUBLIC check and get a FORM object
2007-04-02 by Steven
*/

function checkReturn() {
	if(errorMsg!="") {
		//alert(errorMsg);
		MessageBox(errorMsg,"Info",MB_ICON,MB_OK);
		errorMsg="";
		return false;
	}
};

/** END */
/**
PUBLIC set focus To Element
2007-04-02 by Steven
*/
function focusToElement(obj) {
	if(errorMsg!="") {
		obj.select();
		obj.focus();
	}
	focusElement==null;
};
/** END */

/**
PUBLIC return Function is checkVal is Empty
2007-04-02 by Steven
*/
function checkEmpty(objVal) {
	checkVal=trim(objVal);
	if(checkVal=="")
		return true;
};
/** END */
function validateCharacter(form,element,argName) {
	with(checkFormArg(form)) {
		if(checkCharacter(document.getElementById(element).value))
			return;
		if(parseInt(checkVal)!=checkVal) {
			errorMsg+=argName+" 只能输入英文字母或数字.";
			focusToElement(document.getElementById(element));
		}
	}
	return checkReturn();
};

function getEvent()
{
	if(document.all)
	{
		return window.event;//如果是ie
	}
	func=getEvent.caller;
	while(func!=null)
	{
		var arg0=func.arguments[0];
		if(arg0)
		{
			if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
				||(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
			{
				return arg0;
			}
		}
		func=func.caller;
	}
	return null;
}


function validateRequired_Two(form,element,argName) {
	var b = false;
	with(checkFormArg(form)) {
		element = element.split(",");
		argName = argName.split(",");
		if(element.length!=argName.length) {
			alert("error arg!");
			return;
		}
		for(var i=0;i<element.length;i++) {
			var v = element[i];
			if(trim(document.getElementById(v).value)=="" || document.getElementById(v).value.indexOf("Please input")>=0) {
				document.getElementById(v).value="Please input " + argName[i];
				document.getElementById(v).style.fontStyle="italic";
				document.getElementById(v).style.color="#CCCCCC";
				if(window.addEventListener){
					document.getElementById(v).addEventListener("click",function(){
						var event = getEvent();
						var obj = document.getElementById(event.target.id);
						obj.value="";
						obj.style.fontStyle="";
						obj.style.color="black";},false);
				}else{
					document.getElementById(v).attachEvent("onclick",function(){
						var obj = document.getElementById(event.srcElement.id);
						obj.value="";
						obj.style.fontStyle="";
						obj.style.color="black";});
				}
				b = true;
			}
		}
	}
	if(b)
		return false;
	else
		return true;
};

function validateRequired(form,element,argName) {
	with(checkFormArg(form)) {
		element = element.split(",");
		argName = argName.split(",");
		if(element.length!=argName.length) {
			alert("error arg!");
			return;
		}
		for(var i=0;i<element.length;i++) {
			var v = element[i];
			if(trim(document.getElementById(v).value)=="") {
				errorMsg+=argName[i]+" is required.\n";
				if(focusElement==null)
					focusElement=document.getElementById(v);
			}
		}
		focusToElement(focusElement);
	}
	return checkReturn();
};

function validateNumber(form,element,argName) {
	with(checkFormArg(form)) {
		element = element.split(",");
		argName = argName.split(",");
		if(element.length!=argName.length) {
			alert("error arg!");
			return;
		}
		for(var i=0;i<element.length;i++) {
			var v = element[i];
			if(checkEmpty(document.getElementById(v).value))
				continue;
			if(isNaN(checkVal)) {
				errorMsg+=argName[i]+" 不是一个有效的数字.\n";
				if(focusElement==null)
					focusElement=document.getElementById(v);
			}
		}
		focusToElement(focusElement);
	}
	return checkReturn();
};

function validateInt(form,element,argName) {
	with(checkFormArg(form)) {
		if(checkEmpty(document.getElementById(element).value))
			return;
		if(parseInt(checkVal)!=checkVal) {
			errorMsg+=argName+" 不是一个有效的整数.";
			focusToElement(document.getElementById(element));
		}
	}
	return checkReturn();
};

function validateLengthRange(form,element,argName,minLength,maxLength) {
	with(checkFormArg(form)) {
		if(minLength>maxLength) {
			alert("error arg!");
			return;
		}
		if(checkEmpty(document.getElementById(element).value))
			return;
		if(minLength!=-1) {
			if(getStringLength(checkVal)<minLength)
				errorMsg+=argName+" 的长度不能小于 "+minLength+".\n";
		}
		if(maxLength!=-1) {
			if(getStringLength(checkVal)>maxLength)
				errorMsg+=argName+" 的长度不能大于 "+maxLength+".\n";
		}
		if(minLength!=-1 && maxLength!=-1) {
			if(errorMsg!="")
				errorMsg=argName+" 的长度必须在 "+minLength+" 到 "+maxLength+" 之间.\n";
		}
		focusToElement(document.getElementById(element));
	}
	return checkReturn();
};

function validateValRange(form,element,argName,minValue,maxValue) {
	with(checkFormArg(form)) {
		if(checkEmpty(document.getElementById(element).value))
			return;
		if(isNaN(checkVal))
			errorMsg+=argName+" 不是一个有效数字.\n";
		else {
			if(minValue!=-1) {
				if(parseFloat(checkVal)<parseFloat(minValue))
					errorMsg+=argName+" 不能小于 "+minValue+".\n";
			}
			if(maxValue!=-1) {
				if(parseFloat(checkVal)>parseFloat(maxValue))
					errorMsg+=argName+" 不能大于 "+maxValue+".\n";
			}
			if(minValue!=-1 && maxValue!=-1) {
				if(errorMsg!="")
					errorMsg=argName+" 必须在 "+minValue+" 到 "+maxValue+" 之间.\n";
			}
		}
		focusToElement(document.getElementById(element));
	}
	return checkReturn();
};

function validateEmail(form,element,argName) {
	with(checkFormArg(form)) {
		if(checkEmpty(document.getElementById(element).value))
			return;
		var emailPat=/([\-\_\w]+@[\-\_\w]+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
		if(checkVal.match(emailPat)==null) {
			errorMsg+="The email address is invalid.";
			focusToElement(document.getElementById(element));
		}
	}
	return checkReturn();
};

function validateDate(form,element,argName) {
	with(checkFormArg(form)) {
		var isDate=true;
		if(checkEmpty(document.getElementById(element).value))
			return;
		checkVal=checkVal.replace('/','');
		checkVal=checkVal.replace('-','/');
		checkVal=checkVal.replace('-','/');
		if(new Date(checkVal)=="NaN")
			isDate=false;
		else {
			var newDate=new Date(checkVal);
			var y=newDate.getFullYear();
			var m=newDate.getMonth()+1;
			var d=newDate.getDate();
			var dateMatch = eval(element+".value");
			dateMatch = dateMatch.split(/\D/);
			if(parseInt(y)!=dateMatch[0] || parseInt(m)!=dateMatch[1] || parseInt(d)!=dateMatch[2])
				isDate=false;
		}
		if(isDate==false) {
			errorMsg+=argName+" 不是一个有效的日期,\n日期格式应为 yyyy-mm-dd .";
			focusToElement(document.getElementById(element));
		}
	}
	return checkReturn();
};


function validateCheckBox(form,element,argName,minSele,maxSele) {
	with(checkFormArg(form)) {
		var seleCount=0;
		element=document.getElementsByName(element);
		if(typeof(element.length)=="undefined") {
			if(element.checked==true)
				seleCount++;
		}
		else {
			for(var i=0;i<element.length;i++) {
				if(element[i].checked==true)
					seleCount++;
			}
		}
		if(minSele!=-1) {
			if(seleCount<minSele)
				errorMsg=argName+" 不能选择少于 "+minSele+" 项.\n";
		}
		if(maxSele!=-1) {
			if(seleCount>maxSele)
				errorMsg+=argName+" 不能选择多于 "+maxSele+" 项.\n";
		}
		if(minSele!=-1 && maxSele!=-1) {
			if(errorMsg!="")
				errorMsg=argName+" 必须选择 "+minSele+" 到 "+maxSele+" 之间."
		}
	}
	return checkReturn();
};

function validateRadio(form,element,argName) {
	with(checkFormArg(form)) {
		var selected=false;
		element=document.getElementsByName(element);
		if(typeof(element.length)=="undefined") {
			if(element.checked==true) {
				selected=true;
				return selected;
			}
		}
		else {
			for(var i=0;i<element.length;i++) {
				if(element[i].checked==true) {
					selected=true;
					return selected;
				}
			}
		}
		if(!selected)
			errorMsg=argName+" 必须选择一项.";
	}
	return checkReturn();
};

function validateEquals(form,element1,element2,argName1,argName2) {
	with(checkFormArg(form)) {
		if(trim(document.getElementById(element1).value!=trim(document.getElementById(element2).value))) {
			errorMsg=argName2+" must be equal to "+argName1+".";
			focusToElement(document.getElementById(element2));
		}
	}
	return checkReturn();
};

function validateLink(form,element,argName) {
	with(checkFormArg(form)) {
		if(checkEmpty(document.getElementById(element).value))
			return;
		var linkPat=/(http:\/\/\w+.\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
		if(checkVal.match(linkPat)==null) {
			errorMsg+=argName+" 不是一个有效的 连接 格式.";
			focusToElement(document.getElementById(element));
		}
	}
	return checkReturn();
};

function validateSelect(form,element,argName) {
	with(checkFormArg(form)) {
		if(document.getElementById(element).selectedIndex==0)
			errorMsg="Please select "+argName+" .";
	}
	return checkReturn();
};

function validateSelectExt(form,element,argName) {
	with(checkFormArg(form)) {
		if(document.getElementById(element).selectedIndex==-1)
			errorMsg="请选择 "+argName+" .";
	}
	return checkReturn();
};

function validateCompareDate(form,element1,element2,argName1,argName2) {
	if(validateDate(form,element1,argName1)==false)
		return false;
	if(validateDate(form,element2,argName2)==false)
		return false;
	with(checkFormArg(form)) {
		date1=eval(element1+".value");
		date2=eval(element2+".value");
		date1=date1.replace('/','');
		date1=date1.replace('-','/');
		date1=date1.replace('-','/');
		date2=date2.replace('/','');
		date2=date2.replace('-','/');
		date2=date2.replace('-','/');
		if(new Date(date1)<new Date(date2)) {
			errorMsg=argName1+" 必须大于 "+argName2+" .";
			focusToElement(document.getElementById(element1));
		}
	}
	return checkReturn();
};

function cancelRightClick(){
	if(event.button!=1){
		alert("请按Ctrl+A选择,Ctrl+C复制!");
	}
}
