var xmlHttp;
var SERVER_ERROR = 'Server error occured, please contact admin.';

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)objXMLHttp=new XMLHttpRequest()
	else if (window.ActiveXObject)	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	return objXMLHttp
}

function logIn()
{
	var sEmail = Trim(document.getElementById('sEmail').value);
	var sPassword = Trim(document.getElementById('sPassword').value);
	
	var valid =true;
	if(CheckFormValue("sEmail","spansEmail")) 
	{
		document.getElementById('spansEmail').style.display="inline";
		if(InvalidEmail(sEmail))
		{
			document.getElementById('spansEmail').innerHTML="<strong><font color='#ff0000'>Invalid Email.</font></strong>";
			valid = false;
		}
	}
	else
	{
		document.getElementById('spansEmail').innerHTML="<strong><font color='#ff0000'>required</font></strong>";
		valid=false;
	}
	
	if(!CheckFormValue("sPassword","spansPassword")) 
	{
		document.getElementById('spansPassword').innerHTML="<strong><font color='#ff0000'>required</font></strong>";
		valid=false;
	}
	
	if(valid)
	{
		Signin(sEmail,sPassword);
	}
}

function checkFpEmail() // Belongs to Forget Password process.
{
	var sEmail = Trim(document.getElementById('sForgotEmail').value);
	
	var valid =true;
	if(CheckFormValue("sForgotEmail","spansForgotEmail")) 
	{
		document.getElementById('spansForgotEmail').style.display="inline";
		if(InvalidEmail(sEmail))
		{
			document.getElementById('spansForgotEmail').innerHTML="<strong><font color='#ff0000'>Invalid Email.</font></strong>";
			valid = false;
		}
	}
	else
	{
		document.getElementById('spansForgotEmail').innerHTML="<strong><font color='#ff0000'>required</font></strong>";
		valid=false;
	}
	
	if(valid)
	{
		fcnQuestionToForgettingUser(sEmail);
	}
}

function checkFpAnswer() // Belongs to Forget Password process.
{
	var sAnswer = Trim(document.getElementById('sForgotAnswer').value);
	
	var valid =false;
	if (CheckFormValue("sForgotAnswer","spansForgotAnswer")) {
		valid =true;
	} else {
		document.getElementById('spansForgotAnswer').innerHTML="<strong><font color='#ff0000'>required</font></strong>";
	}
	
	if (valid)
	{
		fcnCheckAnswer(document.getElementById('sForgotEmail').value, sAnswer);
	}
}


function CheckEmail(sEmail)
{
	var valid =true;
	if(CheckFormValue("sEmail","spansEmail"))
	{
		document.getElementById('spansEmail').style.display="inline";
		if(InvalidEmail(sEmail))
		{
			document.getElementById('spansEmail').innerHTML="<strong><font color='#ff0000'>Invalid Email.</font></strong>";
			valid = false;
		}
		else
		{
			if(document.getElementById("spansEmail").innerHTML.indexOf("already registered!") != -1)
			{
				document.getElementById('spansEmail').innerHTML="<strong><font color='#ff0000'>already registered!</font></strong>";
				valid = false;
			}
		}
	
	}
	else
	{
		document.getElementById('spansEmail').innerHTML="<strong><font color='#ff0000'>required</font></strong>";
		valid=false;
	}
	return valid;
}

/**
 * Ajax calling Javascript Functions
 */

function fcnCheckAnswer(ema, ans)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	document.getElementById('ajaxCheckAnswer').innerHTML = 'Checking your answer...';
	
	var url="https://lendinghub.com.au/base/phpAjax/ajCheckAnswerOfForgettingUser.php?ema="+ema+"&ans="+ans+"&rand="+Math.random();
	xmlHttp.onreadystatechange=checkAnswerDone;
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function ResendConfirmationEmail()
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	document.getElementById('ajaxResendingEmail').innerHTML = 'Re-sending email...';
	
	var url="https://lendinghub.com.au/base/phpAjax/ajResendConfirmationEmail.php?rand="+Math.random();
	xmlHttp.onreadystatechange=confirmationEmailSent;
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function CheckUserNameEmail(str,typ)
{ 
	str = Trim(str);
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	if (str.length>0)
	{
		var url="https://lendinghub.com.au/base/phpAjax/ajUserNameEmailCheck.php?typ="+typ+"&qv="+str+"&sid="+Math.random();
		if (typ=="email") 
		{
			xmlHttp.onreadystatechange=emailChanged;		
		}
		else if (typ=="un") 
		{
			xmlHttp.onreadystatechange=usernameChanged;
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function Signin(ema,pas)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	document.getElementById('spansLogin').innerHTML		= '';
	document.getElementById('ajaxLoggingIn').innerHTML	= '<span style="font-weight:bold" class="green_1">Processing, please wait...</span>';
	
	var url="https://lendinghub.com.au/base/phpAjax/ajLogin.php?ema="+ema+"&pas="+pas+"&sid="+Math.random();
	xmlHttp.onreadystatechange=logInCheck;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function fcnQuestionToForgettingUser(ema)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="https://lendinghub.com.au/base/phpAjax/ajGetQuestionToForgettingUser.php?ema="+ema+"&sid="+Math.random();
	xmlHttp.onreadystatechange=etbQuestionToForgettingUser;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function etbQuestionToForgettingUser() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var output = xmlHttp.responseText;
		if (output == "0") {
			document.getElementById('spansForgotLogin').innerHTML="Sorry, that email is not known to this site.";
		} else {
			// protect it here as we go to further step of the login recovery process.
			document.getElementById('sForgotEmail').readOnly = true;
			
			document.getElementById('favoriteQuestion').innerHTML=output;
			document.getElementById('divFavoriteQuestion').style.display='inline'; // show it, previously none (hidden)
			
			// Switch buttons to show...
			document.getElementById('divButtonCheckEmail').style.display='none';
			document.getElementById('divButtonCheckAnswer').style.display='inline';
			
			// Set focus to Answer field.
			document.getElementById('sForgotAnswer').focus();
		}
	}
} 

function logInCheck() 
{ 
/*	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var output = xmlHttp.responseText;
		
		if (output == 1) {
			// Sign-up
			location.href="https://lendinghub.com.au/Secure/SelectBorrowerOrLender.php";
			
		} else if (output == 2) {
			// Borrower
			location.href="https://lendinghub.com.au/Borrowing/Apply/index.php";
			
		} else if (output == 3) {
			// Lender
			location.href="https://lendinghub.com.au/Lending/Apply/index.php";
			
		} else if (output == 4) {
			// Admin
			location.href="https://lendinghub.com.au/CPanel/index.php";
			
		} else if (output == 5) {
			// Group Leader
			location.href="https://lendinghub.com.au/index.php";
			
		} else {
			// Login failed.  Clear processing login message.
			document.getElementById('ajaxLoggingIn').innerHTML	= '';
			
			document.getElementById('spansLogin').style.display	= "inline";
			document.getElementById('spansLogin').innerHTML		= xmlHttp.responseText;
		}
	}*/
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var output = xmlHttp.responseText;
	
		if (output == -1) {
			// Login failed.  Clear processing login message.
			document.getElementById('ajaxLoggingIn').innerHTML	= '';
			
			document.getElementById('spansLogin').style.display	= "inline";
			document.getElementById('spansLogin').innerHTML		= "<font color='#ff0000'>Invalid Email and Password.</font>";
			
		//~ if (output == 1) {
			//~ // Sign-up
			//~ location.href="https://lendinghub.com.au/Secure/SelectBorrowerOrLender.php";
			
		//~ } else if (output == 2) {
			//~ // Borrower
			//~ location.href="https://lendinghub.com.au/Borrowing/Apply/index.php";
			
		//~ } else if (output == 3) {
			//~ // Lender
			//~ location.href="https://lendinghub.com.au/Lending/Apply/index.php";
			
		//~ } else if (output == 4) {
			//~ // Admin
			//~ location.href="https://lendinghub.com.au/CPanel/index.php";
			
		//~ } else if (output == 5) {
			//~ // Group Leader
			//~ location.href="https://lendinghub.com.au/index.php";
			
		} 
		else if (output == -2) {
			// Login failed.  Clear processing login message.
			document.getElementById('ajaxLoggingIn').innerHTML	= '';
			
			document.getElementById('spansLogin').style.display	= "inline";
			document.getElementById('spansLogin').innerHTML		= "<font color='#ff0000'>Sorry you have attempted to login with 3 incorrect tries, access will be disabled for 10 minutes.</font>";
			
		}
		else {
			// Login failed.  Clear processing login message.
			//~ document.getElementById('ajaxLoggingIn').innerHTML	= '';
			
			//~ document.getElementById('spansLogin').style.display	= "inline";
			//~ document.getElementById('spansLogin').innerHTML		= xmlHttp.responseText;
			
			location.href	= output;
		}
	}
} 

function checkAnswerDone()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var output = xmlHttp.responseText;
		if (output.length > 2)
		{
			document.getElementById('ajaxCheckAnswer').style.display="inline";
			document.getElementById('ajaxCheckAnswer').innerHTML = output;
		}
		else
		{
			//~ location.href="/Borrowing/Apply/index.php";
			
			ClearSpanErrorMessages();

			/**
			 * Display the login screen here, we have already sent the new password to the forgetting user.
			 */
			document.getElementById('canvas').style.display = 'inline';
			document.getElementById('forgotPassword').style.display = 'none';
			
			// Hide Join Now message area.
			document.getElementById('divMessageArea_joinNow').style.display = 'none';
			
			/**
			 * Display message to inform user that email is already sent to him on Sign-in form message area.
			 */
			//~ document.getElementById('canvas2').style.width = '360px';
			//~ document.getElementById('signin_message_area').innerHTML = '<h3>Password sent successfully to your email address.</h3>';
			//~ document.getElementById('signin_message_area').innerHTML = '<h3>Your new password has been sent to your email address.</h3>';	
			document.getElementById('ajaxLoggingIn').style.display = 'inline';
			document.getElementById('ajaxLoggingIn').innerHTML = '<span style="font-size:1.1em; font-weight:bold;" class="green_1">Your new password has been sent to your email address.</span>';
			
			/**
			 * Hide the Forgot my password link.
			 */
			document.getElementById('divLinkIForgotMyPassword').style.display = 'none';
			
		}
	}
}

function usernameChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		document.getElementById('spansUserName').style.display="inline";
		document.getElementById('spansUserName').innerHTML=xmlHttp.responseText
	}
} 

function emailChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		document.getElementById('spansEmail').style.display="inline";
		document.getElementById('spansEmail').innerHTML=xmlHttp.responseText;
	}
} 

function confirmationEmailSent()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		//~ document.getElementById('spansEmail').style.display="inline";
		document.getElementById('ajaxResendingEmail').innerHTML=xmlHttp.responseText;
	}
}

function ShowForgetMyPasswordForm()
{
	ClearSpanErrorMessages();

	// Empty the password input field...
	document.getElementById('sPassword').value			= '';
	
	document.getElementById('canvas').style.display			= 'none';
	document.getElementById('forgotPassword').style.display		= 'block';
	
	// Set focus to email field.
	document.getElementById('sForgotEmail').focus();
	
	return false;
}

function ClearSpanErrorMessages()
{
	// Empty span elements that hold error may messages...
	document.getElementById('spansEmail').innerHTML			= '';
	document.getElementById('spansPassword').innerHTML		= '';
	document.getElementById('spansForgotEmail').innerHTML		= '';
	document.getElementById('spansForgotAnswer').innerHTML		= '';
	document.getElementById('spansLogin').innerHTML			= '';
	document.getElementById('ajaxLoggingIn').innerHTML		= '';
}

/**
 * Following function called from SelectBorrowerOrLender.php
 */
function fcnSetPersonType(personId, personTypeId)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="https://lendinghub.com.au/base/phpAjax/ajSetPersonType.php?personId="+personId+"&personTypeId="+personTypeId+"&sid="+Math.random();
	xmlHttp.onreadystatechange=etbSetPersonType;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function etbSetPersonType() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var output = xmlHttp.responseText;
		
		if (output == "0") {
			document.getElementById('spansAjax').innerHTML="<span class='red_1'>"+SERVER_ERROR+"</span>";
			
		} else if (output == "2") {
			location.href	= "https://lendinghub.com.au/Borrowing/Apply/index.php";
			
		} else if (output == "3") {
			location.href	= "https://lendinghub.com.au/Lending/Apply/index.php";
		}
	}
}



function checkBid(id , max)
{
	var amount = Trim(document.getElementById('amount').value);
	var rate = Trim(document.getElementById('rate').value);
	
	var valid =true;
        document.getElementById('amountDiv').innerHTML="";
        document.getElementById('rateDiv').innerHTML="";
        document.getElementById('ajaxLoggingIn').innerHTML="";

	if(CheckFormValue("amount","amountDiv"))
	{
           
           if( !isNumeric(amount) ){

          		document.getElementById('amountDiv').innerHTML="<strong><font color='#ff0000' size='-2'>Amount should be numeric </font></strong>";
          		valid=false;

           }
           else{
               check = amount % 50
			   document.getElementById('amountDiv').style.display="inline";
		if( check != 0  ){
                   document.getElementById('amountDiv').innerHTML="<strong><font color='#ff0000' size='-2'>The Amount You lend should be in increments of 50$</font></strong>";
		   valid=false;
                }
                else if(amount < 100 ){
                   document.getElementById('amountDiv').innerHTML="<strong><font color='#ff0000' size='-2'>Amount cannot be smaller than 100$</font></strong>";
		   valid=false;
                }
                else if(amount > max) {
                	document.getElementById('amountDiv').innerHTML="<strong><font color='#ff0000' size='-2'>You cannot bid more than total amount . </font></strong>";
		   			valid=false;
                }
           }
        }
	else
	{
		document.getElementById('amountDiv').innerHTML="<strong><font color='#ff0000' size='-2'>required</font></strong>";
		valid=false;
	}
	
	if(CheckFormValue("rate","rateDiv"))
	{
                document.getElementById('rateDiv').style.display="inline";

                if( !isNumeric(rate) ){

          		document.getElementById('rateDiv').innerHTML="<strong><font color='#ff0000' size='-2'>Rate should be numeric </font></strong>";
          		valid=false;

                }
                else if (rate > 60) {
                	document.getElementById('rateDiv').innerHTML="<strong><font color='#ff0000' size='-2'>Rate cannot be greater than %60 </font></strong>";
          			valid=false;
                }
                else if (rate <= 0){
                	document.getElementById('rateDiv').innerHTML="<strong><font color='#ff0000' size='-2'>RAte should be greater than %0 </font></strong>";
          			valid=false;
                }

	}
	else
	{
		document.getElementById('rateDiv').innerHTML="<strong><font color='#ff0000' size='-2'>required</font></strong>";
		valid=false;
	}
	
	if(valid)
	{
			var url="ConfirmYourBid.php?amount=" + amount + "&rate=" + rate + "&confirm=" + id ;
			window.location.href = url ;
	}
}
