﻿/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;

if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
  xmlHttp = new XMLHttpRequest();
}

function maskresize(){
	if (document.body.offsetWidth > "1003"){
		_("mask").style.width=document.body.offsetWidth;
	}else{
		_("mask").style.width="1003px";
	}
}

function maximize(){
	_( 'maindiv' ).style.width=document.body.offsetWidth-8;
	_( 'maindiv' ).style.height=document.body.offsetHeight;
	_( 'regbox' ).style.left=0;
}	

function showpwd(){
window.scroll(0,0);
maskresize();
//_("toxls").innerHTML = '';
_("mask").style.display='';
_("pwdbox").style.display='';
_("pwdbox").innerHTML = "<div class='Processing_div'><br><br><br><br><br><br><br><div class='explain_list'><p class='Processing'>Loading...</p></div></div>";
//_("chk_old_result").innerHTML='';
//_("stage1").style.display='';
//_("save").style.display='';
//_("cancel").value='Cancel';

//document.body.style.filter="alpha(Opacity=40)";
//document.body.style.background="#000";
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	try{
		var timestamps = new Date().getTime()+Math.random();
 		xmlHttp.Open("GET", "user/pwdboxdiv.asp?timestamps="+timestamps, true);
 		xmlHttp.onreadystatechange = function showpwOk(){
 				if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					try{
 						_("pwdbox").innerHTML="";
 						_("pwdbox").innerHTML=xmlHttp.responseText;
 					}catch(exception){}
 				}
 				}
 		};
		xmlHttp.Send(null);
	}catch(exception){}
}
}

function closepwd(){
_( "password_old" ).value='';
_("password").value='';
_("password_cf").value='';
_( "chkPwd" ).style.display = 'none';
_( "pwdConfirm" ).style.display = 'none';
_("errMsg").style.display = 'none';
_("chkpswdcnt").innerHTML = "";
_( "chkpswd" ).className = "PSW_P_0";
pwdbox.innerHTML='';
mask.style.display = 'none';
pwdbox.style.display = 'none';
regbox.style.display = 'none';

}


function checkoldpw(pwold){
	var alertObj = document.getElementById("chk_old_result");
	alertObj.innerHTML="<span class='explain'>Verifying...</span>";
//_("chk_old_result").style.display = '';
	try{
//		var pwObj = document.getElementById(pwold);
		var pwObj = _( "password_old" );
		var chkurl = "user/checkpw.asp?pwold=" +escape(pwObj.value);
		xmlHttp.open("GET", chkurl, true);
		xmlHttp.onreadystatechange = checkoldpwOk;
		xmlHttp.send(null);
	}catch(exception){}
}
var oldpwchk=false;
function checkoldpwOk(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var alertObj = document.getElementById("chk_old_result");
			var response = xmlHttp.responseText;
			if(response == "Err_100"){
				alertObj.innerHTML="<span class='correct'>Password is correct.</span>";
				oldpwchk=true;
			}else if(response == "Err_101"){
				alertObj.innerHTML="Please type your old password to verify your identity.";
				oldpwchk=false;
			}else if(response == "Err_102"){
				alertObj.innerHTML="The password is incorrect. Please try again.";
				oldpwchk=false;
				//_( "password_old" ).select();
			}else if(response == "timeout"){
				alertObj.innerHTML="Timeout, please login again.";
				oldpwchk=false;
			}else if(response == "Err_103"){
				alertObj.innerHTML="对不起，<br/>用户名不能以数字开头！";
			}else if(response == "Err_104"){
				alertObj.innerHTML="对不起，<br/>用户名不合法，含有非法字符！";
			}
		}catch(exception){}
	}	
	}
}

function checkPWD( me ){
	_( 'chkPwd' ).style.display = 'none';
	if (me == "") {
		_( "chkpswd" ).className = "PSW_P_0";
		_("chkpswdcnt").innerHTML = "";
		_( 'chkPwd' ).style.display = '';
	} else if (me.length < 6) {
		_( "chkpswd" ).className = "PSW_P_1";
		_("chkpswdcnt").innerHTML = "Too short";
	} else if( ! isPassword( me ) || !/^[^%&]*$/.test( me )) {
		_( "chkpswd" ).className = "PSW_P_0";
		_("chkpswdcnt").innerHTML = "";
		_( 'chkPwd' ).style.display = '';
	}
	else {
		var csint = checkStrong(me);
		switch (csint) {
		case 1:
			_("chkpswdcnt").innerHTML = "Weak";
			_( "chkpswd" ).className = "PSW_P_"+(csint + 1);
			break;
		case 2:
			_("chkpswdcnt").innerHTML = "Medium";
			_( "chkpswd" ).className = "PSW_P_"+(csint + 1);
			break;
		case 3:		
			_("chkpswdcnt").innerHTML = "Strong";
			_( "chkpswd" ).className = "PSW_P_"+(csint + 1);
			break;
		}
	}
}
function isPassword( str ){
	if (str.length < 6 || str.length > 16) return false;
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) return false;
	}
	return true;
}

function checkRequired( me ){
	me.value = me.value.replace(/(^\s*)|(\s*$)/g, "");
	//var trimvalue = me.value.replace(/(^\s*)|(\s*$)/g, "");
	if( me.value =="" ){
		_( me.id + "_result" ).innerHTML = "This information is required.";	
		_( me.id + "_result" ).style.display = "";
		return false;
	}
}
function checkNumber( num ,max){
    if( /^[0-9]+$/.test( num ) && num <=max && num > 0)
		return true;
	else return false;
}
function birthPress(){
	var k = window.event.keyCode;
	if (k < 48 || k > 57)
		window.event.keyCode = 0 ;
}
function confirmPwd( me ){
	var oPassword = _( "password" );
	if (oPassword.value == "") {
		return;
	}
	if( ! ( me.value == oPassword.value ) ){
		_( "pwdConfirm" ).style.display = "";
		_( "pwdConfirm" ).innerHTML = "The new password and the confirmation password don't match. Please type the same password in both boxes.";
	} else if ( ! isPassword( me.value ) ) {
		_( "pwdConfirm" ).style.display = "";
		_( "pwdConfirm" ).innerHTML = "Your password must be at least 6 characters long. Please type a different password."
	}
}
function checkVerifyCode( me ){
	if( me.value == "") _( "verifyConfirm" ).style.display = "";
}

function isMobile( mobile ){
    return /^13\d{9}$/.test( mobile ) | /^15\d{9}$/.test( mobile );
}
function isAlt_email( alt_email ){
    return ( /^[^@]+@[^@]+\.[^@]+$/.test( alt_email ) && alt_email.length<128 );
}
function checkAltEmail( me ){
	me.value = me.value.replace(/(^\s*)|(\s*$)/g, "");
	if( ! isAlt_email( me.value ) ) _( "email_result" ).style.display = "";
}
function checkMobile( me ){
	if( ! isMobile( me.value ) ) _( "mobileConfirm" ).style.display = "";
}

function _(id) {
	return document.getElementById(id);
}
function fCheck(){
	if( ! isPassword( _("password").value ) || !/^[^%&]*$/.test( _("password").value )) {
		window.scroll(0,0);
		//_("password").focus();
		_( "chkPwd" ).style.display = "";
		return false;
	}
	if( _("password_cf").value =="" ) {
		window.scroll(0,0);
		//_( "password_cf" ).focus();
		_( "pwdConfirm" ).style.display = "";
		return false;
	}
	if( _("password_cf").value != _("password").value ) {
		window.scroll(0,0);
		//_( "password_cf" ).focus();
		_( "pwdConfirm" ).style.display = "";
		return false;
	}
	checkoldpw(_( "password_old" ).value);
	if (oldpwchk==false) return false;
	_("errMsg").innerHTML = "<br>Processing...";
	//_("errMsgTitle").style.display = "none";
	_("errMsg").style.display = "";
	updatepw();
	return true;
}

function CharMode(iN){ 
	if (iN>=48 && iN <=57)
	return 1; 
	if (iN>=65 && iN <=90)
	return 2; 
	if (iN>=97 && iN <=122)
	return 4; 
	else 
	return 8;
} 

function bitTotal(num){ 
	modes=0; 
	for (i=0;i<4;i++){ 
		if (num & 1) modes++; 
		num>>>=1; 
	} 
	return modes; 
} 

function checkStrong(sPW){ 
	Modes=0; 
	for (i=0;i<sPW.length;i++){ 
		Modes|=CharMode(sPW.charCodeAt(i)); 
	} 
	return bitTotal(Modes);
}

function updatepw(){
	try{
		var pwd = _( 'password_cf' );
		var upurl = "user/updatepw.asp?pwd=" +escape(pwd.value);
		xmlHttp.open("GET", upurl, true);
		xmlHttp.onreadystatechange = updateOk;
		xmlHttp.send(null);
	}catch(exception){}	
}

function updateOk(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var response = xmlHttp.responseText;
			if(response == "success"){
				stage1.style.display='none';
				_("cancel").value='Close';
				_("errMsg").innerHTML = "<br><br><br><br><br><br>Your password has been changed successfully.<br><br><br><br><br><br>";
				_("save").style.display='none';
			}else if(response == "error"){
				_("errMsg").innerHTML = "<br>System error";
			}
		}catch(exception){}
	}
	}
}


//Export data to excel file

function gettab() {
	try{
		var timestamps = new Date().getTime() + Math.random();
		var taburl = "user/Data/AspExcel/tablename.asp?timestamps="+timestamps;
		xmlHttp.open("GET", taburl, true);
		xmlHttp.onreadystatechange = gettabOk;
		xmlHttp.send(null);	
	}catch(exception){}
}

function gettabOk(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var response = xmlHttp.responseText;
			if (response == "timeout") {
				str="Timeout, please login again."
				_("dbmgt").innerHTML=str;
			} else {				
				str="<select id='tablename' onChange='choosetable(this.options[this.selectedIndex].value);' onblur='notab();' />";
				str+="<option value='javascript:;' selected>Select a table to download</option>";
				str+=response;
				str+="</select>";
				_("toxls").innerHTML=str;
			}
		}catch(exception){}
	} else { 
    window.alert(xmlHttp.status);
  }
	}
}		

function choosetable(table) {
	xls="user/Data/AspExcel/ASPExcel.asp?tab="+table;
  window.location.href(xls);
}

function notab() {
	_("toxls").innerHTML="";
}

function usermanage() {
	if (_("usrmgt").style.display=='none') {
		_("usrmgt").style.display='';
	}	else {
		_("usrmgt").style.display='none';
	}
}