/*
	purpose : to show/hide details for writer in registration screen
*/
var errorStatus=0;
var loginFldErr = 0;
function hideAlert(msg, divId)
{
	$(divId+'Tr').style.display = 'none';
	$(divId+'Div').innerHTML = '';
	$('w'+divId).style.display = 'none';
	$(divId+'Div').style.display = 'none';
}

function checkAvailUserName(uname){
	loginFldErr = 0;
	var url = 'loginValidation.php';
	var pars = 'userName=' + uname+"&mode=checkUname";
	if(uname == '')
	{
		displayAlert("Please enter your user name.",'userName');
		loginFldErr += 1;
		return false;
	}

	if(alltrim(uname.length) < MAX_USERNAME)
	{
		displayAlert("Username must contain atleast "+MAX_USERNAME+" characters!",'userName');
		loginFldErr += 1;
		return false;
	}
	var myAjax = new Ajax.Request(
	url,
	{
		method: 'get',
		parameters: pars,
		onComplete:showMessage
	});
			hideError1('errMsg');

	if (loginFldErr == 0)
	{
	   hideError('userName');
	   showLoader(0);
	}
}
function checkPassword(pwd){
	loginFldErr = 0;
	hideError1('errMsg');
	if(alltrim(pwd) == '') {
		displayAlert("Please enter your password.",'userPassword');
		loginFldErr += 1;
		showLoader(0);
		return false;
	}
	if(alltrim(pwd) != "" && alltrim(pwd.length) < 6) {
		displayAlert("Password must be at least 6 characters long",'userPassword');
		loginFldErr += 1;
		showLoader(0);
		return false;
	}
	if (loginFldErr == 0)
	{
	   hideError('userPassword');
	   showLoader(0);
	}
}



function showMessage(originalRequest) {
 	var response = alltrim(originalRequest.responseText);
    var res = response.split("|");
	if( res[0] == 'No') {
		displayAlert("Invalid User name",'userName');
		loginFldErr += 1;
		showLoader(0);
		return false;
	}
	else {
	   hideError('userName');
	   showLoader(0);
	}
}
function displayAlert(msg, divId)
{
	$(divId+'Tr').style.display = '';
	if(msg) $(divId+'Div').innerHTML = "*&nbsp;" + msg;
}
function ValidateLoginForm(){
	/*  user name checking start  */
	errorStatus = 0;
	var uname = $('userName').value;
	var url = 'loginValidation.php';
	
	if(alltrim(uname) == '')
	{
		displayAlert("Please enter your user name.",'userName');
		hideAlert('','errMsg');
		errorStatus = 1;
	} else if(alltrim(uname.length) < MAX_USERNAME)
	{
		displayAlert("Username must contain atleast "+MAX_USERNAME+" characters!",'userName');
		hideAlert('','errMsg');
		errorStatus = 1;
	}

	/*  user name checking end  */

	/*  Password checking start  */
	var pwd = $('userPassword').value;
	if(alltrim(pwd) == '') {
		displayAlert("Please enter your password.",'userPassword');
		hideAlert('','errMsg');
		errorStatus = 1;
	}
	if(alltrim(pwd) != "" && alltrim(pwd.length) < 6) {
		displayAlert("Password must be at least 6 characters long",'userPassword');
		hideAlert('','errMsg');
		errorStatus = 1;
	}
	/*  password checking end  */
	if (errorStatus == 0)
	{
		var pars = 'userName=' + uname + "&userPassword=" + pwd + "&mode=checkUnameOnce";
		//alert(pars);
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete:showMessage1
		});
	}

}
function showMessage1(originalRequest) {
 	var response = alltrim(originalRequest.responseText);
	//alert(response);
	var resText = response.split("|");
	if (resText[0] == 'No')
	{
		displayAlert('This Username or Password is not correct','errMsg');
		hideError('userName');
		hideError('userPassword');
		errorStatus = 1;
	} else if(resText[0] == 'Cancelled'){
		displayAlert('Your account is cancelled. Please contact site administrator for further query.','errMsg');
		hideAlert('','userName');
		hideAlert('','userPassword');
		errorStatus = 1;
	} else if(resText[0] == 'Inactive'){
		displayAlert('Your account is not yet activated. Please follow the link in our confirmation email to activate your account.','errMsg');
		hideAlert('','userName');
		hideAlert('','userPassword');
		errorStatus = 1;
	}
	else if(resText[0] == 'unpaid')
	{
		window.location.href='writerPaymentDetails.php?stat=VlE9PQ==';
	}
	else {
		if (resText[1] == 'R'){
			window.location.href='readersHome.php';
		} else if(resText[1] == 'W'){
			window.location.href='author/'+resText[2];
		}
		
	}
}


