/**
*	@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/?show=cashier';
				else
					window.location = 'http://'+url+'/account/?show=cashier';			
			}
			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 promocode ="";
		if(document.getElementById('RegistrationForm').promocode)
		{
			promocode=document.getElementById('RegistrationForm').promocode.value;
		}
		var check = "true";
		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('promocode',promocode);
			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 = '/newfrontend/account/successfullregister.php';
					else
						window.location = '/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 promocode = "";
		if(document.getElementById('RegistrationFormAlternative').promocode_alternative)
		{
			promocode=document.getElementById('RegistrationFormAlternative').promocode_alternative.value;
		}	
		var check = document.getElementById('RegistrationFormAlternative').conditions_alternative.checked;
		var check = "true";
		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('promocode',promocode);
			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 = '/newfrontend/account/successfullregister.php';
					else
						window.location = '/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 promocode ="";
		if(document.getElementById('RegistrationForm').registerpromocode)
		{
			promocode=document.getElementById('RegistrationForm').registerpromocode.value;
		}
		var check = "true";
		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('promocode',promocode);
			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")
			window.location = '/newfrontend/register.php';
		else
			window.location = '/account/register.php';
	}
	else
	{
		if(url=="localhost")
			window.location = '/newfrontend/account/successfullregister.php';
		else
			window.location = '/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")
			window.location = '/newfrontend/register.php';
		else
			window.location = '/register.php';	
	}
	else
	{
		if(url=="localhost")
			window.location = '/newfrontend/account/successfullregister.php';
		else
			window.location = '/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;	
	}
}
function StrTrimmer(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 EmailChecker(email)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(StrTrimmer(email)))
		return true;
	else
		return false;
}
//validateAccount
function validateAccount()
{
	var ErrorStr = '';
	var ValidUsername = false;
	var ValidEmail = false;	
	var ValidPassword = false;
	var FieldName ='';
	var DesiredPassword = '';
	var DesiredEmail = '';	
	var DesiredUsername = '';		
	//validating all the input type=text
	DesiredEmail = $('#RegistrationForm input:text[name=registeremail]').val();
	DesiredUsername = $('#RegistrationForm input:text[name=desired_username]').val();	
	DesiredPassword = $('#RegistrationForm input:password[name=registerpassword]').val();
	if((DesiredPassword != null)&&(DesiredPassword != undefined)&&(DesiredPassword != ''))
	{
		ValidPassword = true;
	}	
	if((DesiredUsername != null)&&(DesiredUsername != undefined)&&(DesiredUsername != '')&&(DesiredUsername !='Desired username'))
	{
		if(DesiredUsername.length >=4 && DesiredUsername.length <= 12)
			ValidUsername = true;
	}
	if((DesiredEmail != null)&&(DesiredEmail != undefined)&&(DesiredEmail != '')&&(DesiredEmail !='Valid email address'))
	{
		if(EmailChecker(DesiredEmail))
			ValidEmail = true;
	}	
	//verifying final results	
	if(ValidUsername && ValidPassword && ValidEmail)
	{
		return true;
	}
	else
	{
		if(!ValidUsername)
			ErrorStr = ErrorStr+' - Username: 4 to 12 characters\n';	
		if(!ValidPassword)
			ErrorStr = ErrorStr+' - Type a valid Password\n';	
		if(!ValidEmail)
			ErrorStr = ErrorStr+' - Type a valid Email\n';			
		alert('Please fill out correctly the following fields:\n'+ErrorStr);
		return false;
	}
}
//validateAccount
function validateAccountAlternative()
{
	var ErrorStr = '';
	var ValidUsername = false;
	var ValidEmail = false;	
	var ValidPassword = false;
	var FieldName ='';
	var DesiredPassword = '';
	var DesiredEmail = '';	
	var DesiredUsername = '';
	
	//validating all the input type=text
	DesiredEmail = $('#RegistrationFormAlternative input:text[name=registeremail_alternative]').val();
	DesiredUsername = $('#RegistrationFormAlternative input:text[name=desired_username_alternative]').val();	
	DesiredPassword = $('#RegistrationFormAlternative input:password[name=registerpassword_alternative]').val();
	if((DesiredPassword != null)&&(DesiredPassword != undefined)&&(DesiredPassword != ''))
	{
		ValidPassword = true;
	}	
	if((DesiredUsername != null)&&(DesiredUsername != undefined)&&(DesiredUsername != '')&&(DesiredUsername !='Desired username'))
	{
		if(DesiredUsername.length >=4 && DesiredUsername.length <= 12)
			ValidUsername = true;
	}
	if((DesiredEmail != null)&&(DesiredEmail != undefined)&&(DesiredEmail != '')&&(DesiredEmail !='Valid email address'))
	{
		if(EmailChecker(DesiredEmail))
			ValidEmail = true;
	}	
	//verifying final results	
	if(ValidUsername && ValidPassword && ValidEmail)
	{
		return true;
	}
	else
	{
		if(!ValidUsername)
			ErrorStr = ErrorStr+' - Username: 4 to 12 characters\n';	
		if(!ValidPassword)
			ErrorStr = ErrorStr+' - Type a valid Password\n';	
		if(!ValidEmail)
			ErrorStr = ErrorStr+' - Type a valid Email\n';			
		alert('Please fill out correctly the following fields:\n'+ErrorStr);
		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
function ShowPromocode()
{
	var Area = document.getElementById("promocode");
	if(Area!=null)
	{
		if(Area.className=="HiddenContent")
		{
			Area.className = "VisibleContent";
		}
		else
			Area.className = "HiddenContent";
	}
	else
	{
		alert('Javascript:Error :: Cannot locate the container ID');
	}
}
