/* ----------------------- */
/* COMMUN */

function file_quick (fichier) {
	if (window.XMLHttpRequest) { // FIREFOX
		xhr_object = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return(false);		
	}
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) {
		return(xhr_object.responseText);
	} else { 
		return(false);
	}
}


function checkEmail(email) {
	if (email.search(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*[^._-]@[a-z0-9-]+(\.[a-z0-9]{2,4})$/i) == -1) { return false; }
	else { return true; }
}


function verifFormAndSaveEmail (params) {
	var error = 0;
	var myForm = document.forms["email_form"];
	var email_input = myForm.elements["email"].value;
	if ((typeof(wnc_lang) == "undefined") || (wnc_lang == "")) { wnc_lang = "fr"; }
	
	if (!checkEmail(email_input)) {
		error = 1;
		msg = "Champ EMAIL non valide !\n";
		if (wnc_lang == "fr") { msg = "Veuillez entrer un EMAIL valide (ex : nom@fournisseur.com) !\n"; }
		else if (wnc_lang == "en") { msg = "Please enter a valid EMAIL (ex : name@company.com) !\n"; }
		else if (wnc_lang == "es") { msg = "Por favor entren un EMAIL valido (por ejemplo: apellido@proveedor.com) !\n"; }		
	}
	
	if (error == 0) {
		 error_in = file_quick("http://media.acces-charme.com/email_catcher/click.php?email="+escape(email_input)+"&"+params);
		return true;
	} else {
		alert(msg);
		return false;
	}
}

function wnc_addEvent(obj, evType, fn) {  
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent) { 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

/* ----------------------- */
/* EMAIL */

function wnc_start_blink() {
	wnc_email = document.getElementById("email");
	if ((wnc_email.value == wnc_txt) || (wnc_email.value == '')) {
		wnc_timer = window.setInterval("wnc_blinking()",500);
	}
}

function wnc_blinking() {
	wnc_email.value =  wnc_email.value != wnc_txt ? wnc_txt : "";
}

function wnc_stop_blink() {
	window.clearInterval(wnc_timer);
	if ((wnc_email.value == wnc_txt) || (wnc_email.value == '')) {
		wnc_email.value = '';
	}
}

function wnc_warn(name,nb) {
	if (wnc_active_shake) { wnc_shaker.start(); }
	var n = document.getElementById(name);
    var b = 0;
    var c = [ "red", "white" ];
	var i = window.setInterval(function() {
		n.style.backgroundColor =  n.style.backgroundColor != wnc_couleur_warn ? wnc_couleur_warn : "white";
		b++;
		if (b == nb) { window.clearInterval(i); }
	}, 100);
}

function wnc_shake() { wnc_shaker = new wnc_shaker_class("email",20,70,10); }
function wnc_shaker_class (name,rate,speed, nb_times) {
	obj = document.getElementById(name);
	var oL = obj.offsetLeft;
	var oT = obj.offsetTop;
	this.stop = null;
	this.oTime = null;
	this.oStop = null;
	var om = this;
	this.start = function() {
		if (parseInt(obj.style.left) == oL-2) {
			obj.style.top = oT+2+"px";
			setTimeout(function(){obj.style.left=oL+2+"px"},rate)
		} else {
			obj.style.top = oT-2+"px";
			setTimeout(function(){obj.style.left=oL-2+"px"},rate)
		}
		this.oTime = setTimeout(function(){om.start()},speed);
		this.oStop = setTimeout(function(){om.stop()},1000);
	}
	this.stop = function() {
		clearTimeout(this.oTime);
		clearTimeout(this.oStop);
	}
}

/* ----------------------- */
/* CLIGNOTEMENT */

function wnc_lancer_clignotement() {
	wnc_effet_clignotement(0, 0, wnc_clignotement_max);
	setTimeout("wnc_lancer_clignotement()", 4000);
}

function wnc_effet_clignotement(bIsRouge, clignIndice, clignMaxFct) {
	if (clignIndice < clignMaxFct) {
		if (bIsRouge == 0) { document.getElementById("email").style.backgroundColor = wnc_couleur_warn; bIsRouge = 1; } 
		else { document.getElementById("email").style.backgroundColor = '#FFFFFF'; bIsRouge = 0; }
		clignIndice++;
		setTimeout("wnc_effet_clignotement("+bIsRouge+","+clignIndice+","+clignMaxFct+")", 100);
	} else { document.getElementById("email").style.backgroundColor = '#FFFFFF'; }
}

/* ----------------------- */
/* DIAPORAMA */
function wnc_preload_diapo() {
	wnc_diapo_j = 0;
   if (typeof(wnc_diapo_Pic) == "undefined") { wnc_diapo_Pic = new Array(); }
	wnc_diapo_p = wnc_diapo_Pic.length;
	for (var i=0 ; i<wnc_diapo_p ; i++) {
	   wnc_diapo_preLoad[i] = new Image();
	   wnc_diapo_preLoad[i].src = wnc_diapo_Pic[i];
	}
	wnc_diapo();
}

function wnc_diapo() {
   if (typeof(wnc_diapo_nb) == "undefined") { wnc_diapo_nb = 1; }
   if (wnc_diapo_nb == 1) {
       document.images.diaporama.src = wnc_diapo_preLoad[wnc_diapo_j].src;
   } else {
       for (var i=0 ; i<wnc_diapo_nb ; i++) {
           document.images["diaporama"+(i+1)].src = wnc_diapo_preLoad[wnc_diapo_j+i].src;
       }
   }
   wnc_diapo_j++;
   if (wnc_diapo_j+(wnc_diapo_nb-1) > (wnc_diapo_p-1)) { wnc_diapo_j = 0; }
   wnc_diapo_t = setTimeout('wnc_diapo()', wnc_diapo_speed);
}

/* ----------------------- */
if (typeof(wnc_active_blink) == "undefined") { wnc_active_blink = false; }
if (typeof(wnc_active_shake) == "undefined") { wnc_active_shake = false; }
if (typeof(wnc_active_diapo) == "undefined") { wnc_active_diapo = false; }
if (typeof(wnc_active_cligno) == "undefined") { wnc_active_cligno = false; }
if (wnc_active_blink) { wnc_addEvent(window, "load", wnc_start_blink); }
if (wnc_active_shake) { wnc_addEvent(window, "load", wnc_shake); }
if (wnc_active_diapo) { wnc_addEvent(window, "load", wnc_preload_diapo); }
if (wnc_active_cligno) { wnc_addEvent(window, "load", wnc_lancer_clignotement); }
