// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
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;
	} 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;
	}

	return [pageWidth,pageHeight];
}

function test_email() {
	
	var regExp =  new RegExp(/^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/);
	var error_sub = 0;
	
	if ($F('_email') == "") {
		error_sub++;
		$('email_incorrect').addClassName('error');
		$('email_incorrect').update('Veuillez saisir un email.<br />');
		$('email_incorrect').show();
	} else {
		if (!$F('_email').match(regExp)) {
			error_sub++;
			$('email_incorrect').addClassName('error');
			$('email_incorrect').update('Veuillez saisir un email valide.<br />');
			$('email_incorrect').show();
		} else {
			$('email_incorrect').hide();
		}
	}
	
	var error = 0;
	if ($('question_1') && $F('question_1') == "") {
		error_sub++;
		error++;
	}
	if ($('question_2') && $F('question_2') == "") {
		error_sub++;
		error++;
	}
	if ($('question_3') && $F('question_3') == "") {
		error_sub++;
		error++;
	}
	if (error != 0) {
		$('question_vide').addClassName('error');
		$('question_vide').update('Veuillez remplir toutes vos questions.<br /><br />');
		$('question_vide').show();
	} else {
		$('question_vide').hide();
	}
	if (error_sub == 0) {
		$('my_form').submit();
	}
}

function show_card(id, i) {
	if ($('card_number_'+i).value < 5) {
		Effect.toggle('back_'+id+'_'+i, 'appear');
		Effect.toggle('cache_'+id+'_'+i, 'appear');
		$('card_number_'+i).value = parseInt($('card_number_'+i).value) + 1;
		num_card = $F('card_number_'+i);
		$('card_'+num_card+'_'+i).update('<img alt="10" src="/images/../images/'+id+'.gif?1263219507">');
		new Ajax.Request("/pages/save_session/"+id+"/"+i, {
		  method:'get'
		});
		
	} else {
		$('error_1').show();
		$('error_2').hide();
		show_pop_up();
	}
}

function show_pop_up() {
	sizes = getPageSize();
	$('bg').setStyle({ width: sizes[0] + 'px', height: sizes[1] + 'px', top: 0, left: 0 });
	$('bg').setOpacity(0.7);
	$('bg').show();
	
	screen_height = screen.height;
	if (screen_height < 810) {
		$('pop_up').setStyle( { 'top' : '10px' });
	}
	
	pos_x = String((sizes[0] / 2) - 150)+'px';
	$('pop_up').setStyle( { 'left' : pos_x });
	$('pop_up').show();
}

function hide_pop_up() {
	$('bg').hide();
	$('pop_up').hide();
	 return false;
}

function answer_questions(nb_question) {
	for (var i = 1; i <= nb_question; i++) {
		new Insertion.Bottom($("questions"), '<strong>Thème</strong> :<br /><select id="theme_'+i+'" name="themes[]"><option value="amour">amour</option><option value="travail">travail</option><option value="argent">argent</option><option value="santé">santé</option><option value="autre">autre</option></select><br /><br />');
		new Insertion.Bottom($("questions"), '<strong>Question</strong> '+i+' :<br /><textarea name="questions[]" id="question_'+i+'" cols="26" rows="2"></textarea><br /><br />');
	}
	switch (nb_question) {
	 case 1:
	 var height = '420px';
	 break;
	 case 2:
	 var height = '480px';
	 break;
	 case 3:
	 var height = '610px';
	 break;
	 
	}
	$('pop_up').setStyle( { 'height' : height});
}

function test_cards() {
	error = 0;
	$$('.hidden_num').each(function(h) {
		if (h.value < 5) {
			error++;
		}
	})
	
	if (error != 0) {
		$('error_1').hide();
		$('error_2').show();
		show_pop_up();
	} else {
		$('my_form').submit();
	}
}