var httpObj = false;
try {
	  httpObj = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
		httpObj = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
	try {
	  httpObj = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (failed) {
	  httpObj = false;
	}
  }
}

function DisplayError(Message,DivName){
	document.getElementById(DivName).style.display = "block";
	document.getElementById(DivName).innerHTML = '<p style="margin:0 auto; color:red;">' + Message + '</p>';
}

function HideError(DivName){
	document.getElementById(DivName).innerHTML = '';
	document.getElementById(DivName).style.display = "none";
}

function ShowCross(name){
	document[name].src = "/images/delete.png";
	document.getElementById(name).style.border = "1px solid #d12f19";
	document.getElementById(name).style.background = "#f7cbc2";
}

function RemoveCross(name){
	document[name].src = "/images/accept.png";
	document.getElementById(name).style.border = "1px solid #a1a1a1";
	document.getElementById(name).style.background = "#fff";
}

function getInputLogin(name){
	if(trim(document.getElementById(name).value) == ''){
		document.getElementById(name).style.border = "1px solid #d12f19";
		document.getElementById(name).style.background = "#f7cbc2";
		ShowCross(name);
		if(name=='user_name2')
			DisplayError('Username / Mobile is empty.','LoginError');
		if(name=='user_pass2')
			DisplayError('Password is empty.','LoginError');
		return false;
	}
	if(document.getElementById(name).value != ''){
		document.getElementById(name).style.border = "1px solid #a1a1a1";
		document.getElementById(name).style.background = "#fff";
		RemoveCross(name);
		HideError('LoginError');
		return true;
	}
}

function Login(){
	
	if(!getInputLogin('user_name2')) return false;
	else if(!getInputLogin('user_pass2')) return false;
	var name = document.getElementById("user_name2").value;
	var pass = document.getElementById("user_pass2").value;
	var agreement = document.getElementById("agreement2").checked;
	
	var url =  "/ajax/login-verification.asp?name="+name+"&pass="+pass+"&agreement="+agreement;
	
	document.getElementById('loginbutton').value='Signing In...';
	document.getElementById('loginbutton').disabled = 'true';

	httpObj.open("GET",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById('loginbutton').value = 'Signing In...';
			document.getElementById('loginbutton').disabled = 'true';
		}
		if(httpObj.readyState==4){
			var strContent = httpObj.responseText;
			if(strContent==1){
				document.getElementById('loginbutton').value='Login';
				document.getElementById('loginbutton').disabled = '';
				DisplayError('Username / Mobile or Password is not correct!','LoginError');
			}
			else if(strContent == 2)
			{
				document.getElementById('loginbutton').value='Login';
				document.getElementById('loginbutton').disabled = '';
				location.href="/code-check.asp";
			}
			else
			{
				location.href="/profile.asp";
			}
		}
	}
	httpObj.send(null);
	return false;
}
function captureHitEnter(e, func_with_param) {
	var characterCode;

	if(e && e.which){
		e = e
		characterCode = e.which
	}
	else{
		e = event
		characterCode = e.keyCode
	}
	if(characterCode == 13){
		eval(func_with_param);
		return false;
	}
	else{
			return true;
	}
}

function OpenForgotPassword(){
	document.getElementById('ForgitPasswordDiv').style.display = 'block';
	document.getElementById('mask_div').style.visibility = 'visible';
	document.getElementById('user_name1').focus();
}

function CloseForgotPassword(){
	document.getElementById('ForgitPasswordDiv').style.display = 'none';
	document.getElementById('mask_div').style.visibility = 'hidden';
}

function CheckForgotPasswordUserName(){
	if(!getInputLogin('user_name1')) return false;
	else return true;
}

function CheckForgotPasswordMobile(){
	var MobileValue = trim(document.getElementById('mobile1').value);
	var value = MobileValidity(MobileValue);
	
	if(MobileValue == ''){
		ShowCross('mobile1');
		return false;
	}else if(value){
		ShowCross('mobile1');
		DisplayError(value,'forgotpassworderror');
		return false;
	}else{
		RemoveCross('mobile1');
		HideError('forgotpassworderror');
		return true;
	}
}

function ForgotPasswordRegistration(){
	if(!CheckForgotPasswordUserName()) return false;
	else if(!CheckForgotPasswordMobile()) return false;
	
	var username = document.getElementById("user_name1").value;
	var mobile = document.getElementById("mobile1").value;
	var url = urlpath+"ForgotPassword.php?username="+username+"&mobile="+mobile;
	httpObj.open("POST",url,true);
	
	document.getElementById("ForgotPaswordFormDiv").style.display = "none";

	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById("ForgotPaswordLoadingDiv").style.display = "block";
		}
		if(httpObj.readyState==4){
			document.getElementById("ForgotPaswordLoadingDiv").style.display = "none";
			if(httpObj.responseText=='--No--'){
				document.getElementById("ForgotPaswordFormDiv").style.display = "block";
				document.getElementById("user_name1").value = username;
				document.getElementById("mobile1").value = mobile;
				DisplayError('Information provided is not correct!','forgotpassworderror');
				return false;
			}else{
				document.getElementById("ForgotPaswordFormDiv").style.display = "block";
				document.getElementById("ForgotPaswordFormDiv").innerHTML = '<p style=" align:center;margin-top:20px;margin-bottom:20px;  font-size:11px;">Your password has been sent to your email account.</p>';
			}
		}
	}
	httpObj.send(null);
}	
