// ---------------------------------------------------------------------------------------------

function disp_alert(msg) {
	alert(msg);
}

function disp_window(adresse) {
	$.get(adresse, {test:1}, function(data){popup_win(data);});
}
function popup_win(content) {
	arrPageSizes = ___getPageSize();
	var hei = arrPageSizes[1] - 90;
	var wid = Math.min(800, arrPageSizes[0] - 60);
	$('body').append('<div id="jquery-overlay" ></div><div id="jquery-lightbox"><div style="width:'+(wid+30)+'px;background-color:white;position:relative;padding-top:20px;margin:auto;" ><div id="lightbox-container-image-box" style="width:'+(wid)+'px;padding:15px;height:1px;text-align:left;overflow:hidden;">'+content+'</div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="/images/croix_noir.gif" border=0 ></a></div></div></div>');
	
	$('#jquery-overlay').css({
				backgroundColor:	"#000",
				opacity:			0.8,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
	_resize_container_image_box(wid, hei);
	
	$('#jquery-overlay,#jquery-lightbox').bind('click',function() {
		_finish();									
	});
	$('#lightbox-secNav-btnClose').click(function() {
		_finish();
		return false;
	});
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight + 17;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight ;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function _finish() {
	$('#jquery-lightbox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	$('embed, object, select').css({ 'visibility' : 'visible' });
}

function _resize_container_image_box(intImageWidth,intImageHeight) {
	var intCurrentWidth = $('#lightbox-container-image-box').width();
	var intCurrentHeight = $('#lightbox-container-image-box').height();
	var intWidth = intImageWidth; // Plus the images width and the left and right padding value
	var intHeight = intImageHeight; // Plus the images height and the left and right padding value
	var intDiffW = intCurrentWidth - intWidth;
	var intDiffH = intCurrentHeight - intHeight;
	$('#lightbox-container-image-box').animate(
					{ width: intWidth, height: intHeight },
					2000,
					function() {
						document.getElementById('lightbox-container-image-box').style.overflowY='auto'; 
						}
				);
}

function goto_previous() {
	var form = document.frm_jeu;
	form.elements["do"].value = "previous";
	form.submit();
}


// ----------------------------------------------------------------------------------------------------------------------


function has_checked_one_radio(liste) {
	for(var i=0; i < liste.length; i++) {
		if(liste[i].checked)
			return true;
	}
	return false;
}

function valid_form_sign() {
	var form = document.frm_jeu;
	
	if(form.nom.value == "" || form.prenom.value == "" || form.pseudo.value == "" || form.email.value == "" || form.check_email.value == "" || form.pass.value == "") {
		disp_alert("Merci de remplir tous les champs");
		return;
	}
	
	if(form.email.value != form.check_email.value) {
		disp_alert("L'email et l'email de confirmation que vous avez saisi sont diff\xE9rents");
		return;
	}
	if(!has_checked_one_radio(form.genre)) {
		disp_alert("Vous n'avez pas chosi votre genre (Mme/Mlle/M.)");
		return;
	}
	if(!has_checked_one_radio(form.pub_partenaire)) {
		disp_alert("Merci de sp\xE9cifier si vous souhaitez recevoir des publicit\xE9s de nos partenaires");
		return;
	}
	var j = parseInt(form.j_nais.value, 10);
	var m = parseInt(form.m_nais.value, 10);
	var a = parseInt(form.a_nais.value, 10);
	
	var d = new Date();
	var curr_year = d.getFullYear();
	
	if(j<=0 || j > 31 || m <= 0 || m > 12 || a <= 1900 || a > curr_year) {
		disp_alert("Vous devez saisir une date valide");
		return;
	}
	
	form.elements["do"].value = "submit";
	form.submit();
}

// ---------------------------------------------------------------------------------------------
function get_array_checked() {
	var ls = new Array();	
	$(".sel_chk").each(function(i) {
		if(this.checked)
			ls.push(this.id.substring(2));
	});
	return ls;
}
function count_opt_checked() {
	var tmp =  get_array_checked();
	return tmp.length;
}

function select_opt(id) {
	var inp = document.getElementById("r_"+id);
	var nb_checked = count_opt_checked();
	if(inp.checked) {
		if(nb_max_sel < nb_checked) {
			disp_alert("Vous avez d\xE9j\xE0 s\xE9lectionn\xE9 "+nb_max_sel+" r\xE9ponses");
			inp.checked = false;
			return;
		}
	}
	document.getElementById("nb_value_left").innerHTML = (nb_max_sel - nb_checked);
}

function valid_form_select() {
	var ls_checked = get_array_checked();
	if(ls_checked.length < nb_max_sel) {
		alert("Vous devez encore s\xE9lectionner "+(nb_max_sel - ls_checked.length)+" r\xE9ponses");
		return false;
	}
	var form = document.frm_jeu;
	document.frm_jeu.selection.value = ls_checked.join(";");
	form.elements["do"].value = "submit";
	form.submit();
}

// ------------------------------------------------------------------------
function update_order() {
	var ls = new Array();
	$(".sort_rep").each(function(i) {
		ls.push(this.id);
		document.getElementById("p_"+this.id).innerHTML = ls.length;
	});
	var form = document.frm_jeu;
	document.frm_jeu.selection.value = ls.join(";");
}

function valid_form_sort() {
	var form = document.frm_jeu;
	form.elements["do"].value = "submit";
	form.submit();
}

// -----------------------------------------------------------------------
function valid_form_qcm() {
	var form = document.frm_jeu;
	form.elements["do"].value = "submit";
	form.submit();
}

// -------------------------------------------------------------------------
function modif_form_validate() {
	var form = document.frm_jeu;
	form.elements["do"].value = "goto_step_name:select";
	form.submit();
}

function valid_form_validate() {
	var form = document.frm_jeu;
	
	
	form.elements["do"].value = "submit";
	form.submit();
}

// -------------------------------------------------------------------------

function valid_friend_form() {
	var form = document.frm_jeu;
	form.elements["do"].value = "submit";
	form.submit();
}

function valid_no_friend_form() {
	var form = document.frm_jeu;
	form.elements["do"].value = "goto_step_name:submit";
	form.submit();
}

// -----------------------------------------------------------------------

function valid_submit_form() {
	var form = document.frm_jeu;
	
	if(form.adresse.value == "" || form.CP.value == "" || form.ville.value == "" || form.telephone.value == "") {
		disp_alert("Merci de remplir tous les champs");
		return;
	}
	form.elements["do"].value = "submit";
	form.submit();
}

// -----------------------------------------------------------------------

function valid_invit_form() {
	var form = document.frm_jeu;
	
	if(!form.accept.checked) {
		disp_alert("Vous devez accepter les conditions pour pouvoir jouer");
		return;
	}
	form.elements["do"].value = "submit";
	form.submit();
}

// -----------------------------------------------------------------------
function disp_window_inc(url, titre) {
	$.get(url, {}, function(data){
		init_form();
		form_1er_plan_ref.dialog("option", "width", 780);
		form_1er_plan_ref.dialog("option", "height", 600);
		form_1er_plan_ref.html(data).dialog("option", "title", "<img src='/images/logo_header_popup_jquery.jpg' alt='' style='position:absolute; top:0px; left:0px;' /> <div class='lo_jq' style='padding:5px 35px;height:20px;width:710px;text-align:center;' >"+titre+"</div>").dialog('open');
		Cufon.set('fontFamily', 'HelveticaNeue').replace('.lo_jq', {hover:true});
	});
}

function show_reglement_inc() {
	disp_window_inc("/jeu/reglement.php?id_jeu="+id_jeu, "R&egrave;glement");
}
function show_partenaires_inc() {
	disp_window_inc("/jeu/partenaires.php?id_jeu="+id_jeu, "Partenaires");
}
function show_lots_inc() {
	disp_window_inc("/jeu/lots.php?id_jeu="+id_jeu, "Lots");
}
function show_article_inc(id) {
	disp_window_inc("/jeu/article.php?id="+id, "");
}


function show_reglement() {
	disp_window("/jeu/reglement.php?id_jeu="+id_jeu);
}
function show_partenaires() {
	disp_window("/jeu/partenaires.php?id_jeu="+id_jeu);
}
function show_lots() {
	disp_window("/jeu/lots.php?id_jeu="+id_jeu);
}
function show_article(id) {
	disp_window("/jeu/article.php?id="+id);
}