/*
 * contact.js
 * js de la section contact, comprenant la google map
 * simon - egzakt.com
 * 2009-03-04
 */

// Config
var vide = "";


function account_view(btn) {
	$(btn).val(txt_un_instant);
	$(btn).removeClass('editselected');
	
	$("label").not('.lab_radio').each(function(i){
	
		var input_id = "#"+$(this).attr('id').replace(/lab_/,'');
		var input_value = "";


		// Valeurs texte
		if ($(input_id)[0]) {

//console.log($(this).attr('id'));

	// Exceptions
		// Champs de numero de tel
			if (input_id.match(/(phone|fax|cell)_1/)) {
				var exp = 'phone';
				if (input_id.match(/fax/)) {
					exp = 'fax';
				} else if (input_id.match(/cell/)) {
					exp = 'cell';
				}
				
				var re = new RegExp(exp+'_1', 'gi');
				var part_1 = input_id;
				var part_2 = input_id.replace(re,exp+'_2');
				var part_3 = input_id.replace(re,exp+'_3');
// console.log(part_1);
// console.log(part_2);
// console.log(part_3);
				input_value = $(part_1).val()+ ' '+$(part_2).val()+'-'+$(part_3).val();
				
				if (trim(input_value) == "-") {
					input_value = vide;
					$(this).next('span.value').css('width','200px');
				}
				
				$(part_1).hide();
				$(part_2).hide();
				$(part_3).hide();

		// Champ texte normal
			} else {
				input_value = ($(input_id).val() != "") ? $(input_id).val() : vide;
				$(input_id).hide();
			}
			if (input_id.match(/(phone|fax|cell)_1/)) {
				$(this).next('span.value').html(input_value).show().css('width', '100px');
			} else {
				$(this).next('span.value').html(input_value).show().css('width', $(input_id).css('width'));
			}
		// Choix champs radio
		// <label><span><div.choices>
		} else if ($(this).next().next().is('.choices')) {

//console.log($(this).next().is('.choices'));

			$(this).next().next().children("input[@type='radio']").each(function() {
				if ($(this).attr('checked')) {
					input_value = $(this).val();
				}
			});

			if (input_value == "")
				input_value = vide;

//console.log($(this).next('span.value').html());

			$(this).next('span.value').html(input_value).show();

			$(this).next().next().hide();
		}

	});
	
	if ($(btn).hasClass('btn_print')) {
		$(btn).val(txt_imprimer);
	} else {
		$(btn).val(txt_modifier);
	}
	
	return false;
}

function account_edit(btn) {

	$(btn).val(txt_un_instant);
	$(btn).addClass('editselected');

	$('.wp8 #fcompte span.value').hide();
	$('.wp8 #fcompte label').show();
	$('.wp8 #fcompte input').show();
	$('.wp8 #fcompte .choices').show();
	
	
	$(btn).val(txt_visualiser);
	
	return false;
}

// Fonction pour autofiller la form
function form_autofill() {
	$("input").each(function()
	{
		//Si c'est un numéro de téléphone
		if (this.id.match(/(phone|fax|cell)_1/) != null || this.id.match(/(phone|fax|cell)_2/) != null) {
			this.value = '123';
		} else if (this.id.match(/(phone|fax|cell)_3/)) {
			this.value = '1234';
			
		// Si c'est un radio
		} else if (this.type == 'radio'){
			$(this).attr('checked', 'checked');
			
		//Si c'est TOUT sauf les boutons du bas.
		} else if (this.name != 'btn_view') {
			this.value = 'Valeurs de test';
		} 
	});
}

// Ajout au document_ready (voir la documentready.js)
function document_ready_compte() {

	$("#fcompte").submit(function() {
		return false;
	});
	
	$(".btn_view").click(function() {
		
		if ($(this).hasClass('editselected')) {
			$('.btn_print').show();
			return account_view(this);
		} else {
			account_edit(this);
			$('.btn_print').hide();
			return false;
		}
	});
	
	
	$('.btn_print').hide().click(function() {
		if ($('.btn_view').hasClass('editselected')) {
			account_view(this);
		}
		window.print();
	});
}

