/**
*	@Name:	logprocessing.ajax.js
*	@Created by: Byron Bricegno
*	@DateCreated:	13/08/2009
*	@LastModification: 13/08/2009
*	@Category: Javascript
*	@Parameters: none
*	@Description: Login - Logout AJAX - JS actions 
*	@Dependencies: Ajaxer.js
*	@Version: 1.0	
*	@Copyright: Fantazzle Fantasy Sports Inc
*/
var objAjax = new Ajaxer(); //Login Ajaxer Module
var loginAjaxPage = new Ajaxer(); //Login Page Ajaxer Module
var objAjax2 = new Ajaxer(); //Logout Ajaxer Module
var RegistrationAjaxObj = new Ajaxer();
var registrationContentAjax = new Ajaxer();
var centerArea= new Ajaxer();

function ProcessLogin()
{
	var url = window.location.host;
	var username=document.getElementById('LoginForm').username.value;
	var password=document.getElementById('LoginForm').password.value;
	var check = "true"; //document.getElementById('LoginForm').logedAutom.checked;
	var foldersOut = fixAjaxURL();
	
	if((password !=null) ||(username!=null))
	{
		setLoadingImage('LoadingImage','Processing data');	
		objAjax.setMethod("POST");		
		objAjax.setURL(foldersOut +'phpajax/login.ajax.php');
		objAjax.setParameter('username',username);
		objAjax.setParameter('password',password);
		objAjax.setParameter('logedAutom',check);
		objAjax.setComplete(
		function()
		{
			var LogResponse = objAjax.getResponseText();
			if((LogResponse==1)||(LogResponse=='1'))
			{
				document.location.reload(true);
			}
			else if((LogResponse==2)||(LogResponse=='2'))
			{//this case is when the users has never deposited so it will bring them to the account
				if(url=="localhost")
					window.location = 'http://'+url+'/newfrontend/account/#CashierContainer';
				else
					window.location = 'http://'+url+'/account/#CashierContainer';			
			}
			else
			{
				var ErrorMessage = document.getElementById('LoadingImage');
				ErrorMessage.innerHTML = LogResponse;
			}
		}
		);
		objAjax.execute();		
	}
} //ProcessLogin

function ProcessPageLogin()
{
	var username=document.getElementById('LoginPageForm').usernamepage.value;
	var password=document.getElementById('LoginPageForm').passwordpage.value;
	var check = "true"; //document.getElementById('LoginForm').logedAutom.checked;
	var foldersOut = fixAjaxURL();
	
	if((password !=null) ||(username!=null))
	{
		
		loginAjaxPage.setMethod("POST");		
		loginAjaxPage.setURL(foldersOut +'phpajax/loginPage.ajax.php');
		loginAjaxPage.setParameter('username',username);
		loginAjaxPage.setParameter('password',password);
		loginAjaxPage.setParameter('logedAutom',check);
		loginAjaxPage.setComplete(setPageTemplateResponse);
		loginAjaxPage.execute();
				
	}
} //ProcessLogin

function setPageTemplateResponse()
{
	var top = document.getElementById('Col1');
	var response = loginAjaxPage.getResponseText();
	var pat = /THERE ARE ERRORS/;
			//DETETCS IN THE RESPONSE IF IT IS AM ERROR OR NO
			if(pat.test(response))
			{
				top.innerHTML = loginAjaxPage.getResponseText();
				//alert(response);				
			}
			else
			{
				var url = window.location;
				/******************Redirects to the homePage***************************/
				if(url=="http://localhost/newfrontend/fantasy_login.php")
				redirectToDestiny('http://localhost/newfrontend/account/');
				
				else if(url=="http://test.fantazzle.com/fantasy_login.php")
				redirectToDestiny('http://test.fantazzle.com/fantasy-games/');

				else if(url=="http://new.fantazzle.com/fantasy_login.php")
				redirectToDestiny('http://new.fantazzle.com/fantasy-games/');				
				
				else if(url=="http://www.fantazzle.com/fantasy_login.php")
				redirectToDestiny('http://www.fantazzle.com/fantasy-games/');	
				
				else if(url=="https://www.fantazzle.com/fantasy_login.php")
				redirectToDestiny('http://www.fantazzle.com/fantasy-games/');		
				
				else
				redirectToDestiny('http://www.fantazzle.com');
				//alert(response);
			}
				
		top=null;
		return true;	
	
}
function redirectToDestiny(dest) 
{
	window.location=dest;
} 
//Register new user. The variable is to set in php the promotion graphic automaticly in the homepage when you login
function ProcessRegistration(nonValidate)
{
	var url = window.location.host;
	if(document.getElementById('RegistrationForm'))
	{
		var username=document.getElementById('RegistrationForm').desired_username.value;
		var password=document.getElementById('RegistrationForm').registerpassword.value;
		//var repass=document.getElementById('RegistrationForm').repassword.value;
		var email=document.getElementById('RegistrationForm').registeremail.value;
		var check = document.getElementById('RegistrationForm').conditions.checked;
		var foldersOut = fixAjaxURL();
		if((password !=null) ||(username!=null))
		{
			setLoadingImage('LoadingImageSignUp','Registering account');				
			RegistrationAjaxObj.setURL(foldersOut +'phpajax/registration.ajax.php');
			RegistrationAjaxObj.setMethod("POST");		
			RegistrationAjaxObj.setParameter('username',username);
			RegistrationAjaxObj.setParameter('password',password);
			//RegistrationAjaxObj.setParameter('repass',repass);
			RegistrationAjaxObj.setParameter('email',email);
			RegistrationAjaxObj.setParameter('nonValidate',nonValidate);
			RegistrationAjaxObj.setParameter('conditions',check);
			RegistrationAjaxObj.setComplete(
			function()
			{
				var RegistrationResponse = RegistrationAjaxObj.getResponseText();
				if((RegistrationResponse==1)||(RegistrationResponse=='1'))
				{
					if(url=="localhost")
						window.location = 'http://'+url+'/newfrontend/account/successfullregister.php';
					else
						window.location = 'http://'+url+'/account/successfullregister.php';
				}
				else
				{
					var ErrorMessage = document.getElementById('LoadingImageSignUp');
					ErrorMessage.innerHTML = RegistrationResponse;
				}
			}			
			);
			RegistrationAjaxObj.execute();		
		}
	}
} //Register User
//Register new user. The variable is to set in php the promotion graphic automaticly in the homepage when you login
function ProcessRegistrationAlternative(nonValidate)
{
	var url = window.location.host;
	if(document.getElementById('RegistrationFormAlternative'))
	{
		var username=document.getElementById('RegistrationFormAlternative').desired_username_alternative.value;
		var password=document.getElementById('RegistrationFormAlternative').registerpassword_alternative.value;
		//var repass=document.getElementById('RegistrationFormAlternative').repassword.value;
		var email=document.getElementById('RegistrationFormAlternative').registeremail_alternative.value;
		var check = document.getElementById('RegistrationFormAlternative').conditions_alternative.checked;
		var foldersOut = fixAjaxURL();
		if((password !=null) ||(username!=null))
		{				
			RegistrationAjaxObj.setURL(foldersOut +'phpajax/registration.ajax.php');
			RegistrationAjaxObj.setMethod("POST");		
			RegistrationAjaxObj.setParameter('username',username);
			RegistrationAjaxObj.setParameter('password',password);
			//RegistrationAjaxObj.setParameter('repass',repass);
			RegistrationAjaxObj.setParameter('email',email);
			RegistrationAjaxObj.setParameter('nonValidate',nonValidate);
			RegistrationAjaxObj.setParameter('conditions',check);
			RegistrationAjaxObj.setComplete(
			function()
			{
				var RegistrationResponse = RegistrationAjaxObj.getResponseText();
				if((RegistrationResponse==1)||(RegistrationResponse=='1'))
				{
					if(url=="localhost")
						window.location = 'http://'+url+'/newfrontend/account/successfullregister.php';
					else
						window.location = 'http://'+url+'/account/successfullregister.php';
				}
				else
				{
					alert(RegistrationResponse);
				}
			}			
			);
			RegistrationAjaxObj.execute();		
		}
	}
} //Register User
function ProcessContentRegistration(isPromotions)
{
	if(document.getElementById('RegistrationForm'))
	{
		var username=document.getElementById('RegistrationForm').desired_username.value;
		var password=document.getElementById('RegistrationForm').registerpassword.value;
		//var repass=document.getElementById('RegistrationForm').repassword.value;
		var email=document.getElementById('RegistrationForm').registeremail.value;
		var check = document.getElementById('RegistrationForm').conditions.checked;
		var content = 'true';
		var foldersOut = fixAjaxURL();
		
		
		if((password !=null) ||(username!=null))
		{
			registrationContentAjax.setMethod("POST");	
			registrationContentAjax.setURL(foldersOut +'phpajax/registration.ajax.php');
			registrationContentAjax.setParameter('username',username);
			registrationContentAjax.setParameter('password',password);
			//RegistrationAjaxObj.setParameter('repass',repass);
			registrationContentAjax.setParameter('email',email);
			registrationContentAjax.setParameter('nonValidate','false');
			registrationContentAjax.setParameter('conditions',check);
			registrationContentAjax.setParameter('content',content);
			registrationContentAjax.setParameter('isPromotions',isPromotions);
			registrationContentAjax.setComplete( ProcessRegistrationContentResponse);
			registrationContentAjax.execute();
		
						
		}
	}
} //Register User

function ProcessRegistrationContentResponse()
{
	setTemplateRegistrationContentResponse();	
} // 

//Redirects to the account or the registration page (if there are errors) from the right menu registration form
function setTemplateRegistrationContentResponse()
{
	var url = window.location.host;
	var response = registrationContentAjax.getResponseText();
	var pat = /There are errors/;
		//DETETCS IN THE RESPONSE IF IT IS LOGINERROR.TMPL.HTML OR THE LOGINBOX.TMPL.HTML
	if(pat.test(response))
	{
		if(url=="localhost")
			redirectToDestiny('http://localhost/newfrontend/register.php');
		else if(url=="test.fantazzle.com")
			redirectToDestiny('http://test.fantazzle.com/register.php');
		else if(url=="new.fantazzle.com")
			redirectToDestiny('http://new.fantazzle.com/register.php');			
		else
			redirectToDestiny('http://www.fantazzle.com/register.php');
	}
	else
	{
		if(url=="localhost")
			redirectToDestiny('http://localhost/newsite/account/successfullregister.php');
		else if(url=="test.fantazzle.com")
			redirectToDestiny('http://test.fantazzle.com/account/successfullregister.php');
		else if(url=="new.fantazzle.com")
			redirectToDestiny('http://new.fantazzle.com/account/successfullregister.php');			
		else
			redirectToDestiny('http://www.fantazzle.com/account/successfullregister.php');
	}
	col2=null;
	return true;
}
function ProcessLoginResponse()
{
	setTemplateResponse('login');
} 
// ProcessLoginResponse
function ProcessLogOut()
{
	var foldersOut = fixAjaxURL();
	objAjax2.setURL(foldersOut +'phpajax/logout.ajax.php');
	objAjax2.setComplete(
		function()
		{
			document.location.reload(true);			
		}
	);
	objAjax2.execute();
}//ProcessLogin

//Redirects from the homepage to the account if the registration is success or to the registration page if there is an error
//This method also executes when a newuser(no cookies) login... so to change the topmain content of the homepage from the registration form to the promotio graphic
function setTemplateRegistrationResponse()
{
	var url = window.location.host;
	var response = RegistrationAjaxObj.getResponseText();
	var pat = /There are errors/;
	//DETETCS IN THE RESPONSE IF IT IS LOGINERROR.TMPL.HTML OR THE LOGINBOX.TMPL.HTML
	if(pat.test(response))
	{
		if(url=="localhost")
			redirectToDestiny('http://localhost/newfrontend/register.php');
		else if(url=="test.fantazzle.com")
			redirectToDestiny('http://test.fantazzle.com/register.php');
		else if(url=="new.fantazzle.com")
			redirectToDestiny('http://new.fantazzle.com/register.php');			
		else
			redirectToDestiny('http://www.fantazzle.com/register.php');
	}
	else
	{
		if(url=="localhost")
			redirectToDestiny('http://localhost/newfrontend/account/successfullregister.php');
		else if(url=="test.fantazzle.com")
			redirectToDestiny('http://test.fantazzle.com/account/successfullregister.php');
		else if(url=="new.fantazzle.com")
			redirectToDestiny('http://new.fantazzle.com/account/successfullregister.php');			
		else
			redirectToDestiny('http://www.fantazzle.com/account/successfullregister.php');
	}
}
function setTemplateResponse(action)
{
	var top = document.getElementById('Top');
	var LoadingImageDIV = document.getElementById('LoadingImage');
	var LoginForm = document.getElementById('LoginForm');
	
	var top=document.getElementById('Top');	
	if(action=='login') //login
	{
		if(top!=null)
		{
		
			//alert(objAjax.getResponseText());	
			
			top.innerHTML = objAjax.getResponseText();
			//eval(document.getElementById('sharethisScript').innerHTML);
			var response = objAjax.getResponseText();
			var pat = /THERE ARE ERRORS/;
			//DETETCS IN THE RESPONSE IF IT IS LOGINERROR.TMPL.HTML OR THE LOGINBOX.TMPL.HTML
			if(pat.test(response))
			{
				top.style.height = "152px";
				//alert(objAjax.getResponseText());
			}
			else
			{
				top.style.height = "130px";
				//ProcessRegistration("true");
			}
			
			
			LoadingImageDIV.style.display = "none";
			LoginForm.style.display = "none";
		}
		top=null;
		return true;	

	}
	else //logout
	{
		if(top!=null)
		{	
			//top.innerHTML = objAjax2.getResponseText();	
			window.location.reload();
			//eval(document.getElementById('sharethisScript').innerHTML);			
		}
		top=null;
		return true;	
	}
}

//validateAccount
function validateAccount()
{

	var username=document.getElementById('RegistrationForm').desired_username.value;
	var password=document.getElementById('RegistrationForm').registerpassword.value;
	var email=document.getElementById('RegistrationForm').registeremail.value;	
	var terms=document.getElementById('RegistrationForm').conditions.checked;	
	var blankspaces = "";
	var validate = true;

	if (username=="Desired username")
	{
	blankspaces += "-Username\n";
	validate = false;
	}
	if (password=="Passwor")
	{
	validate = false;
	blankspaces += "-Password \n";
	}
	if(email=="Valid email address")
	{
	validate = false;
	blankspaces += "-Email \n";
	}
	if(terms == 0)
	{
	validate = false;
	blankspaces += "-Agree to the terms and conditions \n";
	}
	
	if(validate == true)
	{
		if(validateEmail(email))
		{
				if((username.length <= 12) && (username.length >= 5))
				return true;
				else
				{
				alert("Your username must be 5 to 12 characters");
				return false;
				}
		}
		else
		return false;
	}
	else
	{
	alert("Please complete the following fields:\n" + blankspaces);
	return false;
	}

}
//validateAccount
function validateAccountAlternative()
{

	var username=document.getElementById('RegistrationFormAlternative').desired_username_alternative.value;
	var password=document.getElementById('RegistrationFormAlternative').registerpassword_alternative.value;
	var email=document.getElementById('RegistrationFormAlternative').registeremail_alternative.value;	
	var terms=document.getElementById('RegistrationFormAlternative').conditions_alternative.checked;	
	var blankspaces = "";
	var validate = true;

	if (username=="Desired username")
	{
	blankspaces += "-Username\n";
	validate = false;
	}
	if (password=="Passwor")
	{
	validate = false;
	blankspaces += "-Password \n";
	}
	if(email=="Valid email address")
	{
	validate = false;
	blankspaces += "-Email \n";
	}
	if(terms == 0)
	{
	validate = false;
	blankspaces += "-Agree to the terms and conditions \n";
	}
	
	if(validate == true)
	{
		if(validateEmail(email))
		{
				if((username.length <= 12) && (username.length >= 5))
				return true;
				else
				{
				alert("Your username must be 5 to 12 characters");
				return false;
				}
		}
		else
		return false;
	}
	else
	{
	alert("Please complete the following fields:\n" + blankspaces);
	return false;
	}

}
function trim12 (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
function validateEmail(email)
{
	var Emailtocheck = trim12(email);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Emailtocheck))
		return true;
	else
	{
		alert("Please enter a valid email address.")
		return false;
	}
}
function setLoadingImage(finaldiv,message)
{
	var foldersOut = fixAjaxURL();
	var obj = document.getElementById(finaldiv);
	if ( obj!=null )
	{
		obj.innerHTML = ""+message+"...<img src=\""+foldersOut+"images/signbox/loading_image.gif\" alt=\"Log in...\"/>";
	}
} // setLoadingImage

