//字符替换
String.prototype.replaceAll = strReplace;
function strReplace(findText, replaceText) {
  var str = new String(this);
  while (str.indexOf(findText)!=-1) {
    str = str.replace(findText, replaceText);
  }
  return str;
}

//刷新验证码
function getVerifyCode(){
 var imgurl="/inc/VerifyCode.asp?"+Math.random();
 $("verifycodeimg").src=imgurl;
}

String.prototype.trim = function()
{
	// 用正则表达式将前后空格
	// 用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
/**
 * 统计字符串字节数
 *
 * return	integer
 */
String.prototype.ByteCount = function()
{
	txt = this.replace(/(<.*?>)/ig,'');
	txt = txt.replace(/([\u0391-\uFFE5])/ig, '11');
	var count = txt.length;
	return count;
}

//非法字符
function isValidChar(str)
{
var invalidchar= ';|<>`&!*(~^)-#? :"/$=\\'+"'"
for(i=0;i<str.length;i++)
 {char=str.charAt(i);
  if(char==" "){return char;}
  if(invalidchar.indexOf(char)>=0){return char;} 
 }
return "";
}

function $(id) {
	return document.getElementById(id);
}


//显示输入提示信息
function showMsgTips(objName,tipsInfo){
	$(objName).className="msgTips";
	$(objName).innerHTML=tipsInfo;	
	}
function showErrMsgTips(objName,tipsInfo){
	$(objName).className="msgErrorTips";
	$(objName).innerHTML=tipsInfo;		
	}
function showRightMsgTips(objName,tipsInfo){
	$(objName).className="msgRightTips";
	$(objName).innerHTML=tipsInfo;		
	}
	
	
	
//获得密码焦点
function click_password(thisinput)
{	
    showMsgTips("passwordMsg","请输入密码");
}
//失去密码焦点
function check_password(thisinput)
{
	var count = thisinput.value.trim().ByteCount();
	var verify=0;
	var space=" ";
	if ( count == 0 ) {
		showErrMsgTips("passwordMsg","请输入密码");
	}
	else
	{
		showRightMsgTips("passwordMsg","");
		verify=1;		
	}	
	return verify;
}




//获得验证码焦点
function click_verifycode(thisinput)
{
	showMsgTips("verifycodeMsg","请输入验证码");
}
//失去验证码焦点
function check_verifycode(thisinput)
{
	var verify=0;
	var re = /^([0-9]){4}$/;

    if (!thisinput.value.match(re))
	{
		showErrMsgTips("verifycodeMsg","验证码输入不正确");
	}
	else
	{
		showRightMsgTips("verifycodeMsg","");
		verify=1;
	}	
	return verify;
}



function  chkLogin(objForm)
  {   

  if(objForm.password)
	{
		if(!check_password(objForm.password))
		{
			objForm.password.focus();
			return false;
		}
	}

	if(objForm.verifycode) //验证码
	{
		if(!check_verifycode(objForm.verifycode))
		{
			objForm.verifycode.focus();
			return false;
		}
	}

	
	$("btnLogin").disabled="true";
	var ajax=new AJAXRequest_post();
	
    ajax.url="/ajaxfile/checkLogin.asp?"+Math.random();
	ajax.setcharset("GB2312");
	ajax.onexception=function(e) {
		alert("登录请求失败，请重试");
		$("btnLogin").disabled="";
	}	
	
	ajax.postf(
		"loginform",
		function oncomplete(obj){returnAjaxLoginResult(obj.responseText)}
	);
	return false;
}	



function returnAjaxLoginResult(AjaxResult){

	
	
	resultStr=AjaxResult.split("$$$");
	switch(resultStr[0]) 
	{
		case "verifycodenull":
		    alert("验证码错误，请重新输入");
			$("verifycode").focus();
			$("btnLogin").disabled="";
			getVerifyCode();
			break;	
        case "postok":
		    alert("登陆成功");
			window.location.reload();
			break;
        case "postfalse":
		    alert("密码不正确，登录失败！");
			$("btnLogin").disabled="";
			break;				
		default:
			alert("登录请求失败，请重试");
			$("btnLogin").disabled="";
			break;
	}
}