var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  	try {
    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  	} catch (e2) {
    		xmlHttp = false;
  	}
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
  xmlHttp = new XMLHttpRequest();
}

function showreg(accounttype) {
window.scroll(0,0);
maskresize();
_("mask").style.display='';
_("regbox").style.display='';
_("regbox").innerHTML = "<div class='Processing_div'><br><br><br><br><br><br><br><div class='explain_list'><p class='Processing'>Loading...</p></div></div>";
//_("toxls").innerHTML = '';
//regbox2.style.display='';
//_("stage1").style.display='';
//_("save").style.display='';
//_("cancel").value='Cancel';
//document.body.style.filter="Mask(Color='#A8B3B8')";
//document.body.style.filter="alpha(Opacity=70)";
//document.body.style.background="#000000";
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	try{
		var timestamps = new Date().getTime()+Math.random();
 		xmlHttp.Open("GET", "user/regboxdiv"+accounttype+".asp?timestamps="+timestamps, true);
 		xmlHttp.onreadystatechange = function showpwOk(){
 				if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					try{
					_("regbox").innerHTML="";
 					_("regbox").innerHTML=xmlHttp.responseText;
 					}catch(exception){}
 				}
 				}
 		};
		xmlHttp.Send(null);
	}catch(exception){}
}
}

function closereg(){
if (_("userid")!=null){_("userid").value='';}
if (_("password")!=null){_("password").value='';}
if (_("password_cf")!=null){_("password_cf").value='';}
if (_("chkPwd")!=null){_( "chkPwd" ).style.display = 'none';}
if (_("pwdConfirm")!=null){_( "pwdConfirm" ).style.display = 'none';}
if (_("errMsg")!=null){_("errMsg").style.display = 'none';}
if (_("chkpswdcnt")!=null){_("chkpswdcnt").innerHTML = "";}
if (_("chkpswd")!=null){_( "chkpswd" ).className = "PSW_P_0";}
mask.style.display = 'none';
regbox.style.display='none';
//document.body.style.background="#FFF";
//document.body.style.filter='';
_("regbox").innerHTML='';
window.scroll(0,0);
window.location.href("profile.asp");
}

function closereg2(){
if (_("userid")!=null){_("userid").value='';}
if (_("password")!=null){_("password").value='';}
if (_("password_cf")!=null){_("password_cf").value='';}
if (_("chkPwd")!=null){_( "chkPwd" ).style.display = 'none';}
if (_("pwdConfirm")!=null){_( "pwdConfirm" ).style.display = 'none';}
if (_("errMsg")!=null){_("errMsg").style.display = 'none';}
if (_("chkpswdcnt")!=null){_("chkpswdcnt").innerHTML = "";}
if (_("chkpswd")!=null){_( "chkpswd" ).className = "PSW_P_0";}
mask.style.display = 'none';
regbox.style.display='none';
//document.body.style.background="#FFF";
//document.body.style.filter='';
_("regbox").innerHTML='';
window.scroll(0,0);
}

var userchk=false;
function checkuser(user){
//	alertObj.innerHTML="processing...";
//_("chk_old_result").style.display = '';
	try{
//		var pwObj = document.getElementById(pwold);
		var userObj = _( "userid" );
		var chkurl = "user/checkuser.asp?id=" +escape(userObj.value);
		xmlHttp.open("GET", chkurl, true);
		xmlHttp.onreadystatechange = function checkuserOk(){
			if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				try{
					var response = xmlHttp.responseText;
					var alertObj = _("userid_result");
					_( 'userid_result' ).style.display = '';
					if(response == "Err_102"){
						alertObj.innerHTML="<span class='correct'>"+userObj.value+" is available.</span>";
						userchk=true;
					}else if(response == "Err_101"){
						alertObj.innerHTML="Please type your User ID.";
						userchk=false;
					}else if(response == "Err_100"){
						alertObj.innerHTML=userObj.value+" is unavailable. Please try another one.";
						userchk=false;
					}else if(response == "timeout"){
						alertObj.innerHTML="Timeout, please login again.";
						userchk=false;
					}else if(response == "Err_103"){
						alertObj.innerHTML="Initial character should be alpha.";
						userchk=false;
					}else if(response == "Err_104"){
						alertObj.innerHTML="Your User ID can contain only letters, numbers, hyphens (-), and underscores (_). Special characters or accented letters are not allowed. Please type a different User ID.";
						userchk=false;
					}
				}catch(exception){}
			}	
			}
		};
		xmlHttp.send(null);	
	}catch(exception){}	
}
function checkuserOk1(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var response = xmlHttp.responseText;
			var alertObj = _("userid_result");
			_( 'userid_result' ).style.display = '';
			if(response == "Err_102"){
				alertObj.innerHTML="<span class='correct'>User ID is available.</span>";
				userchk=true;
			}else if(response == "Err_101"){
				alertObj.innerHTML="Please type User ID.";
				userchk=false;
			}else if(response == "Err_100"){
				alertObj.innerHTML="User ID is unavailable. Please try another one.";
				userchk=false;
			}else if(response == "timeout"){
				alertObj.innerHTML="Timeout, please login again.";
				userchk=false;
			}else if(response == "Err_103"){
				alertObj.innerHTML="Initial character should be alpha.";
				userchk=false;
			}else if(response == "Err_104"){
				alertObj.innerHTML="Invalid symbol(s).";
				userchk=false;
			}
		}catch(exception){}
	}	
	}
}

function RegFinalCheck(){
	checkuser(_( "userid" ).value);
	if (userchk==false){
		//_( "userid" ).focus;
		showfieldset(1);
		window.scroll(0,80);
		return false;
	}
	if( ! isPassword( _("password").value ) || !/^[^%&]*$/.test( _("password").value )) {
		//_("password").focus();
		showfieldset(1);
		window.scroll(0,80);
		_( "chkPwd" ).style.display = "";
		return false;
	}
	if( _("password_cf").value != _("password").value ) {
		//_( "password_cf" ).focus();
		showfieldset(1);
		window.scroll(0,80);
		_( "pwdConfirm" ).innerHTML = "The password and the confirmation password don't match. Please type the same password in both boxes.";
		_( "pwdConfirm" ).style.display = "";
		return false;
	}
	if( _("password_cf").value == "" ) {
		//_( "password_cf" ).focus();
		showfieldset(1);
		window.scroll(0,80);
		_( "pwdConfirm" ).innerHTML = "Your password must be at least 6 characters long. Please type a different password.";
		_( "pwdConfirm" ).style.display = "";
		return false;
	}
	if( _( "organization" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		showfieldset(2);
		_( "organization_result" ).innerHTML = "This information is required.";	
		_( "organization_result" ).style.display = "";
		window.scroll(0,260);
		return false;
	}
	if( _( "address1" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		showfieldset(2);
		_( "address1_result" ).innerHTML = "This information is required.";	
		_( "address1_result" ).style.display = "";
		window.scroll(0,260);
		return false;
	}
	if( _( "fname" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		showfieldset(3);
		_( "fname_result" ).innerHTML = "This information is required.";	
		_( "fname_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if( _( "lname" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		showfieldset(3);
		_( "lname_result" ).innerHTML = "This information is required.";	
		_( "lname_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if( _( "department" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		showfieldset(3);
		_( "department_result" ).innerHTML = "This information is required.";	
		_( "department_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if(!isAlt_email(_("email").value) || _("email").value.replace(/(^\s*)|(\s*$)/g, "") == "" ) {
		showfieldset(3);
		window.scroll(0,405);
		_( "email_result" ).style.display = "";
		return false;
		//_( "alt_email" ).focus();
	}
	if( _("showaddcontactaddyes").checked ){		
		if( _("address2").value.replace(/(^\s*)|(\s*$)/g, "") == "" ) {
			showfieldset(3);
			_( "address2_result" ).innerHTML = "This information is required.";
			_( "address2_result" ).style.display = "";
			//window.scroll(0,405);
			return false;
			//_( "alt_email" ).focus();
		}
	} else {
		_("icountry2").value = "";
	}
	_("errMsg").innerHTML = "<p class='Processing fright'>Processing...&nbsp;&nbsp;</p>";
	_("errMsg").style.display = "";
	window.scroll(0,0);
	createuser();
	return true;
}
function EditFinalCheck(){	
	if( _( "organization" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		//showfieldset(2);
		_( "organization_result" ).innerHTML = "This information is required.";	
		_( "organization_result" ).style.display = "";
		window.scroll(0,260);
		return false;
	}
	if( _( "address1" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		//showfieldset(2);
		_( "address1_result" ).innerHTML = "This information is required.";	
		_( "address1_result" ).style.display = "";
		window.scroll(0,260);
		return false;
	}
	if( _( "fname" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		//showfieldset(3);
		_( "fname_result" ).innerHTML = "This information is required.";	
		_( "fname_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if( _( "lname" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		//showfieldset(3);
		_( "lname_result" ).innerHTML = "This information is required.";	
		_( "lname_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if( _( "department" ).value.replace(/(^\s*)|(\s*$)/g, "") == "" ){
		//showfieldset(3);
		_( "department_result" ).innerHTML = "This information is required.";	
		_( "department_result" ).style.display = "";
		window.scroll(0,405);
		return false;
	}
	if(!isAlt_email(_("email").value) || _("email").value.replace(/(^\s*)|(\s*$)/g, "") == "" ) {
		//showfieldset(3);
		window.scroll(0,405);
		_( "email_result" ).style.display = "";
		return false;
		//_( "alt_email" ).focus();
	}
	if( _("showaddcontactaddyes").checked ){		
		if( _("address2").value.replace(/(^\s*)|(\s*$)/g, "") == "" ) {
			//showfieldset(3);
			_( "address2_result" ).innerHTML = "This information is required.";
			_( "address2_result" ).style.display = "";
			//window.scroll(0,405);
			return false;
			//_( "alt_email" ).focus();
		}
	} else {
		_("icountry2").value = "";
	}
	_("errMsg").innerHTML = "<p class='Processing fright'>Processing...&nbsp;&nbsp;</p>";
	_("errMsg").style.display = "";
	window.scroll(0,0);
	edituser();
	return true;
}
function createuser() {
	try{
		var id = estrim('userid');
		var pwd = estrim('password_cf');
		var organization = estrim('organization');
		var icountry1 = estrim('icountry1');
		var istate1 = estrim('istate1');
		var izip1 = estrim('izip1');
		var icity1 = estrim('icity1');
		var address1 = estrim('address1');
		var title = estrim('title');
		var fname = estrim('fname');
		var lname = estrim('lname');
		var position = estrim('position');
		var department = estrim('department');
		var email = estrim('email');
		var tel = estrim('tel');
		var mobile = estrim('mobile');
		var fax = estrim('fax');
		var icountry2 = estrim('icountry2');
		var istate2 = estrim('istate2');
		var izip2 = estrim('izip2');
		var icity2 = estrim('icity2');
		var address2 = estrim('address2');
		var utype = estrim('usertype');
		var urlcreateuser = "user/createuser.asp?id="+id+"&pwd="+pwd+"&organization="+organization+"&icountry1="+icountry1+"&istate1="+istate1+"&izip1="+izip1+"&icity1="+icity1+"&address1="+address1+"&title="+title+"&fname="+fname+"&lname="+lname+"&position="+position+"&department="+department+"&email="+email+"&tel="+tel+"&mobile="+mobile+"&fax="+fax+"&icountry2="+icountry2+"&istate2="+istate2+"&izip2="+izip2+"&icity2="+icity2+"&address2="+address2+"&utype="+utype;
		xmlHttp.open("POST", urlcreateuser, true);
		xmlHttp.onreadystatechange = createOk;
		xmlHttp.send(null);
	}catch(exception){}	
}

function createOk(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var response = xmlHttp.responseText;
			if(response == "success"){
				stage1.style.display='none';
				_("description").style.display='none';
				_("cancel").value='Close';
				if (_("usertype").value == "1"){
					_("errMsg").innerHTML = "<br><br><span class='subscribedone'>Thank you. Your free trial account has been created successfully.</span>";
				}else if(_("usertype").value =="2"){
					_("errMsg").innerHTML = "<br><br><span class='subscribedone'>Thank you. Your subscription has been submitted successfully.<br><br>Annual subscription fee for the forecasting system is US$2,400. The fee may be paid by cheque/bank draft (in US$). Please make the payment to <i>The Hong Kong Polytechnic University</i> and indicate your <i>User ID</i> on the back of the cheque/bank draft. For payment in HK$, please use the exchange rate of US$1 = HK$7.8. Once your payment has been confirmed, your account will be upgraded from a trial user to a subscribing user.</span>";
				}
				_("save").style.display='none';
				window.scroll(0,0);
			}else if(response == "error"){
				_("errMsg").innerHTML = "<br>System error. Please try later.";
				window.scroll(0,0);
			}
		}catch(exception){}
	}
	}
}

function edituser() {
	try{
		var id = estrim('userid');
		var organization = estrim('organization');
		var icountry1 = estrim('icountry1');
		var istate1 = estrim('istate1');
		var izip1 = estrim('izip1');
		var icity1 = estrim('icity1');
		var address1 = estrim('address1');
		var title = estrim('title');
		var fname = estrim('fname');
		var lname = estrim('lname');
		var position = estrim('position');
		var department = estrim('department');
		var email = estrim('email');
		var tel = estrim('tel');
		var mobile = estrim('mobile');
		var fax = estrim('fax');
		var icountry2 = estrim('icountry2');
		var istate2 = estrim('istate2');
		var izip2 = estrim('izip2');
		var icity2 = estrim('icity2');
		var address2 = estrim('address2');
		var urlcreateuser = "user/edituser.asp?id="+id+"&organization="+organization+"&icountry1="+icountry1+"&istate1="+istate1+"&izip1="+izip1+"&icity1="+icity1+"&address1="+address1+"&title="+title+"&fname="+fname+"&lname="+lname+"&position="+position+"&department="+department+"&email="+email+"&tel="+tel+"&mobile="+mobile+"&fax="+fax+"&icountry2="+icountry2+"&istate2="+istate2+"&izip2="+izip2+"&icity2="+icity2+"&address2="+address2;
		xmlHttp.open("POST", urlcreateuser, true);
		xmlHttp.onreadystatechange = editOk;
		xmlHttp.send(null);
	}catch(exception){}	
}

function editOk(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			var response = xmlHttp.responseText;
			if(response == "success"){
				stage1.style.display='none';
				_("description").style.display='none';
				_("cancel").value='Close';
				_("errMsg").innerHTML = "<br><br><br><br><br><br><div class='f33'>Your information has been saved.</div><br><br><br><br><br>";
				_("save").style.display='none';
				window.scroll(0,0);
			}else if(response == "error"){
				_("errMsg").innerHTML = "<br>System error. Please try later.";
				window.scroll(0,0);
			}
		}catch(exception){}
	}
	}
}

function ChangeValue(objTD)
{
 var objName;
 objName = objTD.id;
 objTD.innerHTML = "<input type=text id=txtBox name=textfield value="+objTD.value+" onBlur='WriteNewValue(this.value,"+objTD+");'>";
// objTD.innerHTML = "<input type=text id='txtBox' name='textfield' value="+objTD.value+" onBlur='WriteNewValue(this.value,"+objTD+")'>";

	 objTD.style.background='#FFF';
	_("txtBox").select();
}
function WriteNewValue(strTxt,objTD)
{
	objTD.innerText = strTxt;
 	alert(objTD.innerHTML);
//_("txtBox").value=strTxt;
//	 objTD.style.background='#efefef';
}

function showstate(id,country){
	var c=country.value;
	var st="";
	var zt="";
	var d="";
	if(c=="HK"||c=="MO"||c=="TW"){
		st="";
		zt="";
		d="none";
	}else if(c=="CN"||c=="TH"){
		st=' Province: ';
		zt=' Postal Code: ';
		d='';
	}else if(c=="US"){
		st=' State: ';
		zt=' Zip: ';
		d='';
	}else if(c=="UK"){
		st=' Constituent Country: ';
		zt=' Postal Code: ';
		d='';
	}else{
		st=' State / Province: ';
		zt=' Zip / Postal Code: ';
		d='';
	}
	_('istate'+id).value="";
	_('izip'+id).value="";
	_('icity'+id).value="";
	_('statetitle'+id).innerText=st;
	_('ziptitle'+id).innerText=zt;
	_('state'+id).style.display=d;
	_('zip'+id).style.display=d;
	_('city'+id).style.display=d;
}

function showcontactadd(yn){
	if(yn=="yes"){
		_('contactadd').style.display='';
		_('address2').value="";
		_('icity2').value="";
		_('istate2').value="";
		_('izip2').value="";
		_('icountry2').selectedIndex = 0;
		_('state2').style.display='none';
		_('zip2').style.display='none';
		_('city2').style.display='none';
	}else if(yn=="no"){
		_('contactadd').style.display='none';
		_('address2').value="";
		_('icity2').value="";
		_('istate2').value="";
		_('izip2').value="";
		_('icountry2').selectedIndex = -1;		
	}
}

function showfieldset(id){
	//_('block'+id).style.border='1px solid #993333';
	//_('block'+id+'_legend').style.color='#993333';
	_('block'+id).style.border='1px solid #D3D3D3';
	//_('block'+id).style='';
	//_('block'+id).style.background='#F3F3F3';
	_('block'+id+'_legend').style.color='#444444';
	if(_('block'+id+'_explain')!=null){
		_('block'+id+'_explain').className='explain_list fleft';
	}
	for(i = 1; i <= 4; i++){
		if(i!=id && _('block'+i)!=null){
			_('block'+i).style.borderColor='#D3D3D3 #FFF #FFF #FFF';
			_('block'+i).style.background='#FFF';
			//_('block'+i).style.border='1px solid #A3A3A3';
			_('block'+i+'_legend').style.color='#D3D3D3';
			if(_('block'+i+'_explain')!=null){
				_('block'+i+'_explain').className='explain_list fleft hide';
			}
		}
	}
}

function numonly(me){
	var k = event.keyCode;
	if(!event.shiftKey){
		if(((k>=48)&(k<=57))|((k>=96)&(k<=105))|(k==8)|(k==9)|(k==46)|(k==109)|(k==189)|(k==107)|((k>16)&(k<=18))|(k==20)|(k==27)|(k==32)|(k==91)|(k==144)|((k>=33)&(k<=40))|((k>=112)&(k<=123))){
		}else{event.returnValue=false;}
	}else{
		if((k==187)|(k==48)|(k==57)){}else{event.returnValue=false;}
	}
}

function estrim(obj){
	_(obj).value = _(obj).value.replace(/(^\s*)|(\s*$)/g, "");
	//var estrimvalue = URLEncode(_(obj).value.replace(/[\'*]/g, "''"));
	if(_(obj).value!=""){
		var estrimvalue = URLEncode(_(obj).value).replace(/%D%A*/g,"%0D%0A");
	}else{
		var estrimvalue = "";
	}
	return estrimvalue;
}


function showdel() {
pwdbox.style.display='none';
regbox.style.display='none';
//mask.style.display='';
delbox.style.left=178;
delbox.style.display='';
	try{
xmlHttp.Open("GET", "user/delboxdiv.asp", true);
//xmlHttp.Send(null);
		xmlHttp.onreadystatechange = refreshdel;
		xmlHttp.send(null);	
	}catch(exception){}	
		

}

function refreshdel(){
	if (xmlHttp.readyState == 4) {
	if (xmlHttp.status == 200) {
		try{
			delbox.innerHTML=xmlHttp.responseText;
		}catch(exception){}
	}	
	}
}

function closedel(){
//mask.style.display = 'none';
delbox.style.display='none';
delbox.innerHTML='';
}
