//	*********************************************************************************************		Author
//	*********************************************************************************************		

/*

Title:		ApInit - version NA

Author:		James Hall - www.astonprograms.co.uk

Uses:		ApCore		- version 3.0.1
Uses:		ApAsync		- version 1.0

*/

//	*********************************************************************************************		Variables
//	*********************************************************************************************		

var ajaxLoginDetails = null;

var ajaxLoginDiv;
var ajaxEmailInput;

//	*********************************************************************************************		
//	*********************************************************************************************		SetAsyncLogin

function SetAsyncLogin()
{	
	var text = document.createElement("p");
	text.innerHTML = "S'identifier";
	
	ajaxEmailInput = document.createElement("input");
	ajaxEmailInput.type = "text";
	ajaxEmailInput.onkeypress = function(event){if(event && event.keyCode == 13)AsyncLogin(ajaxEmailInput.value, true);};
	
	var button = document.createElement("input");
	button.type = "button";
	button.className = "submit";
	button.value = "Login";	
	button.onclick = function(){AsyncLogin(ajaxEmailInput.value, true);};

	clearElement(ajaxLoginDiv);	
	ajaxLoginDiv.appendChild(button);
	ajaxLoginDiv.appendChild(ajaxEmailInput);
	ajaxLoginDiv.appendChild(text);
}

//	*********************************************************************************************		AsyncLogin

function AsyncLogin(emailAddress, showLoading)
{
	if(emailAddress != "")
	{
		if(showLoading)
		{
			var text = document.createElement("p");
			text.innerHTML = "S'identifier. Veuillez patienter...";	

			clearElement(ajaxLoginDiv);
			ajaxLoginDiv.appendChild(text);
		}

		new ApAsyncPostback("/AJAXLogin.aspx?email=" + emailAddress, null, AsyncLogin_Result);
	}
	else
	{
		alert("Veuillez saisir une adresse email pour vous connecter");
		ajaxEmailInput.focus();
	}
}

//	*********************************************************************************************		AsyncLogin_Result

function AsyncLogin_Result(ajaxObj, status)
{
	clearElement(ajaxLoginDiv);

	if(status == 200)
	{
		if(ajaxObj.responseText == "")
		{
			var text = document.createElement("p");
			text.innerHTML = "Désolé, adresse email invalide. <a href='javascript:;' onclick='SetAsyncLogin();'>Veuillez essayer à nouveau</a>";

			ajaxLoginDiv.appendChild(text);
		}
		else
		{
			SetLoggedIn(ajaxObj.responseText);
		}
	}
	else
	{
		var text = document.createElement("p");
		text.innerHTML = "Erreur d'identification. <a href='javascript:;' onclick='SetAsyncLogin();'>Veuillez essayer à nouveau</a>";

		ajaxLoginDiv.appendChild(text);
	}
}

//	*********************************************************************************************		SetLoggedIn

function SetLoggedIn(userDetails)
{
	clearElement(ajaxLoginDiv);
	
	var username = userDetails.split(",")[0]; // 0 - username, 1 - email

	var text = document.createElement("p");
	text.innerHTML = "Bienvenue à nouveau <span>" + username + "</span>";

	ap.addStylesheet('/styles/loggedIn.css');

	ajaxLoginDiv.appendChild(text);	
	
	ap.setCookie("Eureka_User", userDetails, 31);
}

//	*********************************************************************************************		clearElement

function clearElement(element)
{
	while(element.hasChildNodes())
		element.removeChild(element.childNodes[0]);
}


//	*********************************************************************************************		popupPDFs

function popupPDFs()
{
	var anchors = document.getElementsByTagName("a");
	for(var i = 0; i < anchors.length; i++)
	{
		if(anchors[i].href.substr(anchors[i].href.length - 4).toLowerCase() == ".pdf")
		{
			anchors[i].onclick = function(){window.open(this.href);return false;};
		}
	}
}


//	*********************************************************************************************		
//	*********************************************************************************************

//	***************************************************************************		ImportScript

function ApImportScript(url)
{
	scriptObj = document.createElement("script");
	scriptObj.src = url;
	scriptObj.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(scriptObj);
}

//	*********************************************************************************************		ApInit

function Ap_Init(onLoad)
{	
	popupPDFs();

	var mainDiv = document.getElementById("main");
	mainDiv.style.paddingTop = "0px";
	
	ajaxLoginDiv = document.createElement("div");
	ajaxLoginDiv.id="AJAXLogin";	
	
	mainDiv.insertBefore(ajaxLoginDiv, mainDiv.childNodes[0])
	
	var loginCookie = ap.getCookie("Eureka_User");
	if(loginCookie == null)
	{
		SetAsyncLogin();
	}
	else
	{
		SetLoggedIn(loginCookie);
		AsyncLogin(loginCookie.split(",")[1], false); // login with email
	}
	
	if(ajaxLoginDetails != null)
	{
		SetLoggedIn(ajaxLoginDetails);
	}
	
	if(onLoad)
		onLoad();
}

//	*********************************************************************************************		Execute

ApImportScript("/scripts/ApCore.js");
ApImportScript("/scripts/ApAsync.js");

ApImportScript("/scripts/cssHax.js");
ApImportScript("/scripts/submit.js");

var apLoading = window.onload;
window.onload = function(){Ap_Init(apLoading)};