/*
 *  UseFul JavaScripts :)
 *  Copyright (c) 2009 Informatique de France
*/


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// Paragraphes
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
function fermer(lid){
	var ie = (document.all) ? true : false;
	var elements = (ie) ? document.all : document.getElementsByTagName('*');
	for (i=0; i<elements.length; i++){
		if (elements[i].className=='innerbox002' && elements[i].id!=lid ){
			document.getElementById('txt'+elements[i].id).style.display="none";
			document.getElementById(elements[i].id).className="innerbox005";
			document.getElementById('img'+elements[i].id).src="images/arrow.gif";
		}
	}
	return true;
}

function toggleMe(a,b,c){
	var e=document.getElementById(a);
	var f=document.getElementById(b);
	var g=document.getElementById(c);
	if(!e)return true;
	fermer(b);
	if(e.style.display=="none"){
		e.style.display="block";
		f.className="innerbox002";
		g.src="images/arrowbottom.gif";
	}else{
		e.style.display="none";
		f.className="innerbox005";
		g.src="images/arrow.gif";
	}
	return true;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// Affichage
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
function afficher(id){
	document.getElementById(id).style.display="block"; 
	return true;
}

function cacher(id){
	document.getElementById(id).style.display="none";
	return true;
}

function changer_etat(id1, id2, img_ferme, img_ouvert){
	var element_id1 = document.getElementById(id1);
	var element_id2 = document.getElementById(id2);
	if (element_id1.style.display == "block"){
		cacher(id1);
		element_id2.style.backgroundImage = 'url('+img_ferme+')';
		element_id2.style.fontWeight = 'normal';
	}
	else {
		afficher(id1);
		element_id2.style.backgroundImage = 'url('+img_ouvert+')';
		element_id2.style.fontWeight = 'bold';
	}
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// Formulaires
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

// Désactive le champs "id"
function disable (id){
	document.getElementById(id).disabled = true;
	return true;
}

// Active le champs "id"
function enable (id){
	document.getElementById(id).disabled = false;
	return true;
}

// Change l'état actif / inactif sur le champs "id" 
function changer_disponibilite (id){
	var etat = document.getElementById(id);
	if (etat.disabled == true) document.getElementById(id).disabled = false;
	else document.getElementById(id).disabled = true;
	return true;
}

// Initialise sur la valeur "valeur" le champs texte "id"
function initialiser (id, valeur){
	document.getElementById(id).value = valeur;
	return true;
}

// Efface toutes les options du select "id"
function effacer_toutes_options(id){
	var nselect=0;
	for(n=0;n<document.getElementById(id).length;n++){
		if (document.getElementById(id).options[n].selected == true) {
		nselect++;
		}
		if (nselect>0) {
			for (n=0;n<document.getElementById(id).length;n++) {
				if (document.getElementById(id).options[n].selected == true) {
				document.getElementById(id).options[n] = null;
				}
			}
		effacer_toutes_options(id);
		}
	}
}

// Ajoute l'option "label" de valeur "valeur" au select "id"
function ajouter_option(id, label, valeur){
	var nouvelleOption = document.createElement('option');
	nouvelleOption.text = label;
	nouvelleOption.value = valeur;
	var select = document.getElementById(id);
	try {
		select.add(nouvelleOption, null); // Firefox
	}
	catch(ex) {
		select.add(nouvelleOption); // IE
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// Impressions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

// Imprime le contenu d'une iframe
function impression() {
	window.frames[0].print();
	return true;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// PoPuP
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

function popUpBD(url) {
	window.open (url,"_blank","directories=no, toolbar=0, addressbar=no, copyhistory=0, menubar=no, status=no, location=no, scrollbars=no, resizable=no, width=1002, height=695");
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// FONCTIONS GENERIQUES
// VERIFICATION DE FORMULAIRES
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

// INPUTS
function check_input(field_name, field_size) {
  classError = 'error';
  classOK = 'input_text';
  
  var label_id = 'label_'+field_name;
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;
    if (field_value == '' || field_value.length < field_size) {
		form.elements[field_name].className = classError;
		document.getElementById(label_id).style.color = "#CC0000";
		error = true;
    }
	else {
		document.getElementById(label_id).style.color = "#000000";
		form.elements[field_name].className = classOK;
	}
  }
}

// RADIOS
function check_radio(field_name) {
	var isChecked = false;
	var label_id = 'label_'+field_name;
	if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
		var radio = form.elements[field_name];
		for (var i=0; i<radio.length; i++) {
			if (radio[i].checked == true) {
				isChecked = true;
				break;
			}
		}
		if (isChecked == false) {
			document.getElementById(label_id).style.color = "#CC0000";
			error = true;
		}
		else {
			document.getElementById(label_id).style.color = "#000000";
		}
	}
}

// SELECT
/*
function check_select(field_name, field_default, message) {
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;

    if (field_value == field_default) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}
*/

// MOTS DE PASSE
function check_password(field_name_1, field_name_2, field_size) {
	classError = 'error';
	classOK = 'input_text';
	
	if (form.elements[field_name_1] && (form.elements[field_name_1].type != "hidden")) {  
		var password_current = form.elements[field_name_1].value;
		var id_label_password_current = 'label_'+field_name_1;
		var password_confirmation = form.elements[field_name_2].value;
		var id_label_password_confirmation = 'label_'+field_name_2;	
		if (password_current != password_confirmation) {
			form.elements[field_name_1].className = classError;
			form.elements[field_name_2].className = classError;
			document.getElementById(id_label_password_current).style.color = "#CC0000";
			document.getElementById(id_label_password_confirmation).style.color = "#CC0000";
			error = true;
		}
		else {
			document.getElementById(id_label_password_current).style.color = "#000000";
			document.getElementById(id_label_password_confirmation).style.color = "#000000";
			form.elements[field_name_1].className = classOK;
			form.elements[field_name_2].className = classOK;
		}
	}
}

// VERIFICATION EMAIL
function check_mail (field_name) {
	classError = 'error';
	classOK = 'input_text';
	var label_id = 'label_'+field_name;
	a = document.getElementById(field_name).value;
	valide1 = false;
	
	for (var j=1;j<(a.length);j++) {
		if( a.charAt(j)=='@') {
			if (j<(a.length-4)) {
				for ( var k=j; k<(a.length-2); k++){
					if ( a.charAt(k)=='.' ) valide1 = true;
				}
			}
		}
	}
	if( valide1==false ){
		form.elements[field_name].className = classError;
		document.getElementById(label_id).style.color = "#CC0000";
		error = true;
    }
	else {
		document.getElementById(label_id).style.color = "#000000";
		form.elements[field_name].className = classOK;
	}
}

// GESTION DE L'ESPACE DE COMMUNICATION
function update_status(text, id){
	document.getElementById(id).innerHTML = text;
}

// VERIFICATION ASYNCHRONE DU LOGIN
function handle_login(url_verif_login){
	var loginRequest = null;
	
	// Définition du connecteur en fonction du navigateur :
	if (window.XMLHttpRequest){
		loginRequest = new XMLHttpRequest();
		if (loginRequest.overrideMimeType) {
				loginRequest.overrideMimeType('text/xml');
			}
	} 
	else if (window.ActiveXObject) {
		try {
			loginRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try {
					loginRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
					alert("Impossible de communiquer avec le serveur");
			}
		}
    }
	
	// Réception et traitement des résultats en background :
	loginRequest.onreadystatechange = function() {
		if ( loginRequest.readyState == 4 ) {
			if ( loginRequest.status == 200 ) {
				if ( loginRequest.responseText == "0" ){
					document.getElementById('label_identifiant').style.color = "";
					document.getElementById('identifiant').className = "input_text";
					document.getElementById('warning').style.display = "none";
					update_status("1", "identifiant_verif");
				} else {
					document.getElementById('label_identifiant').style.color = "#CC0000";
					document.getElementById('identifiant').className = "error";
					update_status("Cet identifiant est d&eacute;j&agrave; pris.", "warning");
					document.getElementById('warning').style.display = "block";
					update_status("0", "identifiant_verif");
				}
			}
			else {
				alert( "Une erreur est survenue : " + loginRequest.status + " " + loginRequest.statusText );
			}	
		} 
	};
	
	// Envoi de la requête en background :
	var username = document.getElementById('identifiant').value;
	loginRequest.open("GET", url_verif_login + "?username=" + username, true); 
	loginRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	loginRequest.send(null);

	if (error == true){
		document.getElementById("warning").style.display="block";
		return false;
	} else {
		return true;
	}
	
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// REGLES DES FORMULAIRES
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

function check_form_inscription(){
	error = false;
	form = document.form_inscription;
	
	check_input("nom", 2);
	check_input("prenom", 2);
	check_mail ("email");
	check_input("telephone", 10);
	check_input("nom_commercial", 2);
	check_input("siret", 9);
	check_input("tva", 2);
	check_input("adresse", 2);
	check_input("code_postal", 5);
	check_input("ville", 2);
	check_input("pays", 2);
	check_input("url", 2);
	// check_radio("technologie");
	// check_input("token", 2);
	check_input("identifiant", 2);
	check_input("pass", 2);

	if (document.getElementById('identifiant_verif').innerHTML == "0"){
		alert("ERREUR LOGIN");
		document.getElementById('identifiant').className = "error";
		document.getElementById('label_identifiant').style.color = "#CC0000";
		error = true;
	}
	else {
		document.getElementById('identifiant').className = "input_text";
		document.getElementById('label_identifiant').style.color = "#000000";
	}
	
	if (error == true) {
		update_status("Veillez &agrave; remplir correctement les champs indiqu&eacute;s.", "warning");
		document.getElementById('warning').style.display="block";
		return false;
	} else {
		return true;
	}
}

function check_form_modification(){
	error = false;
	form = document.form_modification;
	
	check_input("nom", 2);
	check_input("prenom", 2);
	check_mail ("email");
	check_input("telephone", 10);
	check_input("nom_commercial", 2);
	check_input("siret", 9);
	check_input("tva", 2);
	check_input("adresse", 2);
	check_input("code_postal", 5);
	check_input("ville", 2);
	check_input("pays", 2);
	check_input("url", 2);
	// check_radio("technologie");
	// check_input("token", 2);
	check_password("new_pass", "old_pass", 2);

	if (error == true) {
		update_status("Veillez &agrave; remplir correctement les champs indiqu&eacute;s.", "warning");
		document.getElementById('warning').style.display="block";
		return false;
	} else {
		return true;
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// REGLES DES "FORMULES" - PAIEMENT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
function selection_formule(formule){
	// Formules initiales
	if (formule == 'formule_1'){
		document.getElementById("amount").value = "20";
		document.getElementById("item_name").value = "1000 points sellXtender";
	}
	if (formule == 'formule_2'){
		document.getElementById("amount").value = "50";
		document.getElementById("item_name").value = "2500 points sellXtender";
	}
	if (formule == 'formule_3'){
		document.getElementById("amount").value = "100";
		document.getElementById("item_name").value = "5000 points sellXtender";
	}
	
	var tax = Math.round(document.getElementById("amount").value * 19.6) / 100;
	document.getElementById("tax").value = tax;
}

function selection_paragraphe(paragraphe){
	if (paragraphe == 'informations') {
		document.getElementById('informations').style.display="block";
		document.getElementById('formule').style.display="none";
		document.getElementById('historique').style.display="none";
		
	}
	if (paragraphe == 'formule') {
		document.getElementById('informations').style.display="none";
		document.getElementById('formule').style.display="block";
		document.getElementById('historique').style.display="none";
	}
	if (paragraphe == 'historique') {
		document.getElementById('informations').style.display="none";
		document.getElementById('formule').style.display="none";
		document.getElementById('historique').style.display="block";
	}
}