// JavaScript Document

// fonction ajax de recuperation de contenu pour affichage sans reload

function file_get(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return (false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4)
		return (xhr_object.responseText);
	else
		return (false);
}

function file_post(fichier, data)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return (false);
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState == 4) {
			if (xhr_object.status == 200) {
				document.getElementById("result").innerHTML = xhr_object.responseText;
			}
		}
	}
	xhr_object.open("POST", fichier, true);
	xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr_object.setRequestHeader("Content-length", data.length);
    xhr_object.setRequestHeader("Connection", "close");
	xhr_object.send(data);
}

/**********************************************************
	Formulaire
**********************************************************/
// fonction de parse des formulaires
function	FuncParseForm(form)
{
	// -- Parsing des formulaires --
	var urlParam;
	for(i = 0; i < form.length; i++)
	{
		if (i == 0)
		{
			if (form.elements[i].type == "checkbox" || form.elements[i].type == "radio")
			{
				if (form.elements[i].checked)
					urlParam = form.elements[i].name + "=" + escape(form.elements[i].value);
			}
			else
				urlParam = form.elements[i].name + '=' + escape(form.elements[i].value);
		}
		else
		{
			if (form.elements[i].type == "checkbox" || form.elements[i].type == "radio")
			{
				if (form.elements[i].checked)
					urlParam = urlParam + "&" + form.elements[i].name + "=" + escape(form.elements[i].value);
			}
			else
				urlParam = urlParam + '&' + form.elements[i].name + '=' + escape(form.elements[i].value);
		}
	}
	return (urlParam);
}

/**********************************************************
	FO	-	callToAction
**********************************************************/
function callToAction(){
	var email = document.getElementById('email').value;
	var terms = document.getElementById('terms');
	//var file = document.getElementById('input_text_file').value;
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	var result = "KO";
	
	if(reg.test(email) && terms.checked == true){
		result = "OK";
		////if(file != "")
			////result = "OK";
		////else
			////result = "KKO";
	}
	else
		result = "KO";
	
	if(result == "OK"){
		document.getElementById("email").style.borderColor = "#1d708f";
		document.getElementById("email").style.background = "#1d708f";
		//document.getElementById("input_text_file").style.borderColor = "#1d708f";
		//document.getElementById("input_text_file").style.background = "#1d708f";
		document.getElementById("ok").style.display = "block";
		document.getElementById("ko").style.display = "none";
	}
	else if(result == "KO"){
		document.getElementById("email").style.borderColor = "red";
		document.getElementById("email").style.background = "#f18282";
		document.getElementById("ok").style.display = "none";
		document.getElementById("ko").style.display = "block";
	}
	//else if(result == "KKO"){
		//document.getElementById("input_text_file").style.borderColor = "red";
		//document.getElementById("input_text_file").style.background = "#f18282";
		//document.getElementById("ok").style.display = "none";
		//document.getElementById("ko").style.display = "block";
	//}
	if(result == "OK"){
		document.callToActionForm.submit();
	}
}

/**********************************************************
	BO	-	TRANSLATION DELETION
**********************************************************/
function deleteTranslation(){
	var answer = confirm("Are you sure you want to delete the selected translations ?");
	if(answer){
		var boxes = new Array();
		var checked = "?id=";
		boxes = document.getElementsByName("delBox");
		for(i=0;i<boxes.length;i++){
			if(boxes[i].checked){
				checked += boxes[i].value + '-';
			}
		}
		checked = checked.substring(0, (checked.length - 1));
		//alert(checked);
		
		var result = '';
		if (result = file_get('pages/translation_deletion.php' + checked)){
			//document.getElementById('result')rHTML = result;
			history.go(0);
		}
	} 
}

/**********************************************************
	BO	-	TRANSLATION CREATION
**********************************************************/
function newTranslation(form){
	file_post("pages/translation_creation.php", FuncParseForm(form))
}

/**********************************************************
	BO	-	TRANSLATION EDITION
**********************************************************/
function edititonTranslation(form){
	file_post("pages/translation_edition.php", FuncParseForm(form))
}

/**********************************************************
	BO	-	TRANSLATION SEARCH
**********************************************************/
function searchTranslation(code){
	window.location.href = "?page=translation&sub=list&code=" + code;
}
function campaignSelection(code,campaign){
	window.location.href = "?page=translation&sub=list&code=" + code + "&cp=" + campaign;
}

/**********************************************************
	BO	-	campaign DELETION
**********************************************************/
function deletecampaign(){
	var answer = confirm("Are you sure you want to delete the selected translations ?");
	if(answer){
		var boxes = new Array();
		var checked = "?id=";
		boxes = document.getElementsByName("delBox");
		for(i=0;i<boxes.length;i++){
			if(boxes[i].checked){
				checked += boxes[i].value + '-';
			}
		}
		checked = checked.substring(0, (checked.length - 1));
		//alert(checked);
		
		var result = '';
		if (result = file_get('pages/campaign_deletion.php' + checked)){
			//document.getElementById('result').innerHTML = result;
			history.go(0);
		}
	}
}

/**********************************************************
	BO	-	campaign EDITION
**********************************************************/
/*function edititoncampaign(form){
	file_post("pages/campaign_edition.php", FuncParseForm(form))
}*/

/**********************************************************
	BO	-	CALLTOACTION EDITION
**********************************************************/
function editionCallToAction(form){
	file_post("pages/calltoaction_edition.php", FuncParseForm(form))
}

/**********************************************************
	BO	-	CALLTOACTION DELETION
**********************************************************/
function deleteParticipation(){
	var answer = confirm("Are you sure you want to delete the selected participations ?");
	if(answer){
		var boxes = new Array();
		var checked = "?id=";
		boxes = document.getElementsByName("delBox");
		for(i=0;i<boxes.length;i++){
			if(boxes[i].checked){
				checked += boxes[i].value + '-';
			}
		}
		checked = checked.substring(0, (checked.length - 1));
		//alert(checked);
		
		var result = '';
		if (result = file_get('pages/calltoaction_deletion.php' + checked)){
			//document.getElementById('result').innerHTML = result;
			history.go(0);
		}
	}
}

/**********************************************************
	BO	-	LANGUAGE CREATION
**********************************************************/
function newLanguage(form){
	file_post("pages/language_creation.php", FuncParseForm(form))
}

/**********************************************************
	BO	-	LANGUAGE EDITION
**********************************************************/
function editLanguage(form){
	file_post("pages/language_edition.php", FuncParseForm(form))
}

/**********************************************************
	BO	-	LANGUAGE DELETION
**********************************************************/
function deleteLanguage(){
	var answer = confirm("Are you sure you want to delete the selected languages ?");
	if(answer){
		var boxes = new Array();
		var checked = "?id=";
		boxes = document.getElementsByName("delBox");
		for(i=0;i<boxes.length;i++){
			if(boxes[i].checked){
				checked += boxes[i].value + '-';
			}
		}
		checked = checked.substring(0, (checked.length - 1));
		//alert(checked);
		
		var result = '';
		if (result = file_get('pages/language_deletion.php' + checked)){
			if (result == "ok")
				history.go(0);
			else{
				document.getElementById('result').innerHTML = result;
				setTimeout("history.go(0)", 10000);
			}
		}
	}
}

// /**********************************************************
	// BO	-	COUNTRY CREATION
// **********************************************************/
// function newCountry(form){
	// if (result = file("pages/country_creation.php?" + FuncParseForm(form)))
	// {
		// document.getElementById("result").innerHTML = result;
	// }
// }

// /**********************************************************
	// BO	-	COUNTRY EDITION
// **********************************************************/
// function editCountry(form){
	// if (result = file("pages/country_edition.php?" + FuncParseForm(form)))
	// {
		// document.getElementById("result").innerHTML = result;
	// }
// }

/**********************************************************
	BO	-	COUNTRY DELETION
**********************************************************/
function deleteCountry(){
	var answer = confirm("Are you sure you want to delete the selected countries ?");
	if(answer){
		var boxes = new Array();
		var checked = "?id=";
		boxes = document.getElementsByName("delBox");
		for(i=0;i<boxes.length;i++){
			if(boxes[i].checked){
				checked += boxes[i].value + '-';
			}
		}
		checked = checked.substring(0, (checked.length - 1));
		//alert(checked);
		
		var result = '';
		if (result = file_get('pages/country_deletion.php' + checked)){
			//document.getElementById('result').innerHTML = result;
			history.go(0);
		}
	}
}

/**********************************************************
	BO	-	LOGIN
**********************************************************/
function logUser(form){
	var result = '';
	if (result = file_get("include/login.php?" + FuncParseForm(form)))
	{
		if(result == "OK")
			window.location.href = "?onglet=0&page=home";
		else
			document.getElementById("result").innerHTML = result;
	}
}
/**********************************************************
	BO	-	LOGOUT
**********************************************************/
function userLogout(form){
	var result = '';
	if (result = file_get("include/logout.php"))
	{
		if(result == "ok")
			window.location.href = "?page=login";
	}
}

/**********************************************************
	FO	-	INDEX
**********************************************************/
function displayCountries(campaignId){
	if(campaignId != "-"){
		var result = '';
		if (result = file_get("include/getCountries.php?campaignId=" + campaignId))
		{
			document.getElementById('countriesSelect').innerHTML = result;
			document.getElementById('countriesList').style.display = "block";
		}
	}
	else {
			document.getElementById('countriesList').style.display = "none";
	}
}
function redirect(languageId,campaignId) {
	var result = '';
	if (result = file_get("include/homeRedirect.php?campaignId=" + campaignId + "&languageId=" + languageId)){
		window.location = result;
	}
}
