
function open_url(param) {
	var sep_pos = param.indexOf(',', 0);
	var url = param.substring(0, sep_pos);
	var win = param.substr(sep_pos+1, param.length);
	if(win=='self') {
		document.location.href = url;
	} else {
		var c_date = new Date();
		window.open(url, "popup"+c_date.getTime(), "directories=no, location=no, menubar=no, resizable= yes, scrollbars=yes, status=yes, toolbar=no, width=1024, height=700");
	}
}

function callback_reload_page(z) {
	document.location.reload();
}

function callback_redirect(url_dest) {
	document.location.href = url_dest;
}

//COOKIE FUNCTION

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function show_cover() {
	//set the position according to scroll
	var pageHeight = (document.body.clientHeight);
	var Yscroll = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
	var newmargin = ((pageHeight/4)+Yscroll)-100;
	if(newmargin<50)
		newmargin = 50;

	document.getElementById('pageCover').style.height= '100%';
	document.getElementById('pageCover').style.display = 'block';
	document.getElementById('gamePopupDiv').style.marginTop = newmargin+'px';
	
}

function hide_cover() {
	document.getElementById('pageCover').style.display = 'none';
}

function displayPopup(popup_content) {
	show_cover();
	document.getElementById('gamePopupDiv').innerHTML = popup_content;
}

function addtolist(game_id) {
	x_sajax_addtolist(game_id, cb_addtolist);
}

function addtocookie(game_id) {
	createCookie('last_selected_game_id', game_id, 30);	
	var list_selected_game_id = readCookie('list_selected_game_id');
	if(list_selected_game_id==null || list_selected_game_id==0) {
		list_selected_game_id = game_id;
	} else {
		list_selected_game_id = list_selected_game_id+'-'+game_id;
	}
	createCookie('list_selected_game_id', list_selected_game_id, 30);
	document.location.href = _url_site+'index.php?todo=public_subscribe&subtodo=step1';
	//document.location.href = _url_site+'index.php?todo=game_selection';
	//document.location.href = _url_site+'index.php?todo=public_faq';
}

function cb_addtolist(game_id_popup_content) {
	var sep_pos = game_id_popup_content.indexOf('|', 0);
	var game_id = parseInt(game_id_popup_content.substring(0, sep_pos));
	var popup_content = game_id_popup_content.substr(sep_pos+1, game_id_popup_content.length);	
	if(!isNaN(game_id)) {
		if(game_id>0) {
			if(document.getElementById('btgameadd'+game_id)) {
				document.getElementById('btgameadd'+game_id).src = _url_site+'img/bt_game_in_list_min.png';
				document.getElementById('btgameadd'+game_id).onclick = function() {document.location.href = _url_site+'index.php?todo=user_account&subtodo=list';};				
			}
			
			if(document.getElementById('divbtgameadd'+game_id)) {
				document.getElementById('divbtgameadd'+game_id).innerHTML = 'Dans votre liste';
				document.getElementById('divbtgameadd'+game_id).onclick = function() {document.location.href = _url_site+'index.php?todo=user_account&subtodo=list';};				
			}

		}
	}
	displayPopup(popup_content);
}

function show_membre(user_id) {
	x_sajax_show_membre(user_id, cb_show_membre);	
}

function cb_show_membre(popup_content) {
	displayPopup(popup_content);
}

function forgot_password(close_window) {
	if(close_window==undefined)
		close_window = 0;
	document.getElementById('logininfos').style.display='none';
	x_sajax_forgot_password(close_window, cb_forgot_password);	
}

function cb_forgot_password(popup_content) {
	displayPopup(popup_content);
	document.getElementById('email_forgot').value = document.loginFrm.username.value;
}

function recup_forgot_password() {	
	// var a_fact_forced_field = new Array('email_forgot', 'security_code');
	// //check necessary informations
	// for(var i=0; i<a_fact_forced_field.length; i++) {
	// 	if( document.getElementById(a_fact_forced_field[i]).value == "") {
	// 		alert('{$a_fields_label['account']['register_page_email_js_fill_all']}');
	// 		return false;
	// 	}
	// }
	// document.getElementById('security_code').value,
	x_sajax_reset_and_send(document.getElementById('email_forgot').value, cb_reset_and_send);
}

function cb_reset_and_send(result) {
	if(result == 0) {
		document.getElementById('email_forgotRes').innerHTML = 'Code de sécurité incorrect';
		// refresh_code();
		// //empty code
		// document.getElementById('security_code').value = '';
		// document.getElementById('security_code').focus();
	} else {
		if(result == -1) {
			document.getElementById('email_forgotRes').innerHTML = 'Votre adresse email n\'est pas enregistrée en tant que membre.';		
		} else {
			if(result == 1)
				document.getElementById('email_forgotRes').innerHTML = 'Votre mot de passe a été réinitialisé et vous a été envoyé par email. N\'oubliez pas de consulter votre boite Spam. En cas de problème, contactez notre <a href="'+_url_site+'index.php?todo=help">service client</a>.';
			else
				document.getElementById('email_forgotRes').innerHTML = 'Une erreur est survenue de la réinitialisation de votre mot de passe. Merci de contactez notre <a href="'+_url_site+'index.php?todo=help">service client</a>.';
		}
	}
}

function game_popup(game_id) {
	x_sajax_game_popup(game_id, cb_game_popup);
}

function cb_game_popup(popup_content) {
	displayPopup(popup_content);
}

function reminder_popup() {
	popup_content = '<div class="reminder_popup"><div style="float:right;margin:20px;"><img src="'+_url_site+'img/bt_close.png" style="vertical-align:middle;cursor:pointer;" onclick="hide_cover();" alt="Fermer" /></div><div style="padding: 340px 0 0 385px;"><a href="'+_url_site+'Comment_ca_marche.html"><img src="'+_url_site+'img/bt_know_more.png" border="0" alt="En savoir plus" title="En savoir plus" /></a></div></div>';
	displayPopup(popup_content);
}


/* cross browser method to set opacity */
function setOpacityElement(level, imgId) {
  var element = document.getElementById(imgId);
  element.style.opacity = level;
  element.style.MozOpacity = level;
  element.style.KhtmlOpacity = level;
  element.style.filter = "alpha(opacity=" + (level * 100) + ");";
}

/* rotate and create timers to fade the testimonial in */
function fadeInGameImage(imgId){
  for (i = 0; i <= 1; i = i + 0.05) {
    setTimeout("setOpacityElement(" + i + ", '"+imgId+"')", i * 500);
  }
}

/* create timers to fade the testimonial away */
function fadeOutGameImage(imgId) {
  for (i = 0; i <= 1; i = i + 0.05) {
    setTimeout("setOpacityElement(" + (1 - i) + ", '"+imgId+"')", i * 500);
  }
}


function show_offers() {
	url = _url_site+'index.php?todo=public_faq&subtodo=recap';
	window.open(url, "popupAbonnement", "directories=no, location=no, menubar=no, resizable= yes, scrollbars=yes, status=yes, toolbar=no, width=980, height=700");
}

