var ajaxRequest;

/////////////////////// get AJAX XmlHttpObject  //////////////////////////
function getXmlHttpObject(){
	var httpObject = null;
	try{
  		// Firefox, Opera 8.0+, Safari
  		httpObject = new XMLHttpRequest();
  	}
	catch (e){
  		// Internet Explorer
  		try{
    		httpObject = new ActiveXObject("Msxml2.XMLHTTP");
   	 	}
 	 	catch (e){
   	 		httpObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return httpObject;
}


function show(e){
	if (document.getElementById(e)) {
		document.getElementById(e).style.display = "block";
	}
}

function hide(e){
	if (document.getElementById(e)) {
		document.getElementById(e).style.display = "none";
	}
}

var tcTimer;
function tcHide(){
	if(tcTimer) {
		window.clearTimeout(tcTimer);
	}
	tcTimer = window.setTimeout(hideTcPane, 500);
}

function hideTcPane(){
	tcTimer = null;
	document.getElementById('catFamFastPage').style.display = "none";
}
function showTcPane(){
	window.clearTimeout(tcTimer);
	document.getElementById('catFamFastPage').style.display = "block";
}

function showRefList(id){
	document.getElementById('plusieursRefMain'+ id).style.display = "block";
}

function hideRefList(id){
	document.getElementById('plusieursRefMain'+ id).style.display = "none";
}


//////////////// javascript trim //////////////////////////
//
function trim(str){
	// making sure str is a string
	str = str + ""; 
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

/////////////////////////////////////////
//
function showColorisNom(nom){
	document.getElementById('colorisHeadNom').innerHTML = "&nbsp;&nbsp;" + nom;
}

/////////////////////////////////////////
//
function clearColorisNom(){
	document.getElementById('colorisHeadNom').innerHTML = "";
}

//////////////////////////////////////////////
function hideParentMouseoutCheck(parentE, childE){
	var ce = document.getElementById(childE);
	if('mouseout'.relatedTarget == ce){
		return;
	}
	else{
		hide(parentE);
	}
	
}

/////////////////////////////////////////////////////////
function freeSearchSuggest(){
	ajaxRequest = getXmlHttpObject();
	var kStr = document.getElementById('freeSearchKeys').value;
	if (kStr.length > 2) {
		var keyStr = "?keys=" + kStr;
		ajaxRequest.onreadystatechange = ajaxFreeSearchSuggest;
		ajaxRequest.open("POST", "./ajax/ajaxFreeSearch.php" + keyStr, true);
		ajaxRequest.send(null);
	}
	else{
		document.getElementById('freeSearchResults').innerHTML = "";
		document.getElementById('freeSearchResults').style.display = "none";	
	}
}

function ajaxFreeSearchSuggest(){
	if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
		if (ajaxRequest.responseText != "") {
			document.getElementById('freeSearchResults').style.display = "block";
			document.getElementById('freeSearchResults').innerHTML = ajaxRequest.responseText;
		}
		else{
			document.getElementById('freeSearchResults').innerHTML = "";
			document.getElementById('freeSearchResults').style.display = "none";
		}
	}
}

////////////////////////////////////////////////////////////
function freeSearchLink(satId){
	var kStr = document.getElementById('freeSearchKeys').value;
	if (kStr.length > 2) {
		kStr = kStr.replace(/ /g, "-");
		kStr = kStr.replace(/é/g, "e");
		kStr = kStr.replace(/è/g, "e");
		kStr = kStr.replace(/â/g, "e");
		kStr = kStr.replace(/\./g, "-");
		var urlStr = "http://catalogue.lagardere-france.com/chercher_" + kStr + "_" + satId + ".0.html";
		window.location = urlStr;
	}
	else{
		//alert("your keyword is too short for a search");
	}
}

function IsNumeric(str){
   var ValidChars = "0123456789.,+- ";
   var IsNumber=true;
   var oneChar;
   for (i = 0; i < str.length; i++) 
      { 
      oneChar = str.charAt(i); 
      if (ValidChars.indexOf(oneChar) == -1) 
         {
         IsNumber = false;
		 return IsNumber;
         }
      }
   return IsNumber;
}

function setAffinPrixLink(urlP, urlS){
	var pfCom = document.prixForm.prixCom.value;
	var pfFin = document.prixForm.prixFin.value;
	if(pfCom != "" ||  pfFin != ""){
		pfCom = (pfCom != "" && IsNumeric(pfCom)) ? pfCom : "0";
		pfFin = (pfCom != "" && IsNumeric(pfFin)) ? pfFin : "";	
		if(pfFin != "" && pfCom > pfFin){
			pfCom = "0";
		}
		location.href= urlP + "?pr=" + pfCom + "_" + pfFin + urlS;	
	}
	else{
		return false;
	}
}

///////////////////////// email form ///////////////////////////////////////
function sendFromEmail(){
	document.getElementById('submit').style.background = "#dddddd";
	document.getElementById('reset').style.background = "#dddddd";

	document.getElementById('emailProgressIcon').style.display = "block";
	ajaxRequest = getXmlHttpObject();

	var fNom = document.getElementById('nom').value;
	var fPrenom = document.getElementById('prenom').value;
	var fSociete = document.getElementById('societe').value;
	var fTel = document.getElementById('tel').value;
	var fEmail = document.getElementById('email').value;
	var fDate_livraison = document.getElementById('date_livraison').value;
	var fMessage = document.getElementById('message').value;
	var fCodePostal = document.getElementById('code_postal').value;
	var fNombreCoulMarq = document.getElementById('nombre_couleurs_marq').value;
	if(document.getElementById('emailCameFromRef')){
		var fCameFromRef = document.getElementById('emailCameFromRef').value;
	}
	else{
		var fCameFromRef = "";
	}

	var produitStr =  document.getElementById('emailProduitNom').value + "<br /><strong>Réf: </strong>" + document.getElementById('emailProduitRef').value +  "<br /><strong>Quantité : </strong>" + document.getElementById('quantiteValue').value;

	var urlStr = "?nom=" + fNom + "&prenom=" + fPrenom + "&societe=" + fSociete + "&tel=" + fTel + "&codePostal="+ fCodePostal +"&email=" + fEmail + "&produit=" + produitStr + "&date_livraison=" + fDate_livraison + "&numbreCoulMarq=" + fNombreCoulMarq + "&message=" + fMessage + "&cameFromRef=" + fCameFromRef;
	ajaxRequest.onreadystatechange = ajaxFormEmail;
	ajaxRequest.open("POST", "./ajax/ajaxSendFormEmail.php" + urlStr, true);
	ajaxRequest.send(null);
}

function ajaxFormEmail(){
	if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
		document.getElementById('emailProgressIcon').style.display = "none";
		var response = ajaxRequest.responseText;
		if(response){
			document.getElementById('formulaire').innerHTML = "<span id='emailSentMessage'>Merci de votre demande.</span>";
		}
		else{
			document.getElementById('emailFormErrorMsg').innerHTML = "Le email n'a pas été pas envoyé!";
			document.getElementById('emailFormError').style.display = "block";
		}
	}
}

function checkEmail(theEmail){
	var email = theEmail.value;

	if (email != "") {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(email)) {
			document.getElementById("emailFormError").style.display = "none";
			return true;
		}
		else{
			document.getElementById("emailFormError").style.display = "block";
			document.getElementById("emailFormErrorMsg").innerHTML = "La syntaxe de votre email est incorrect !";
			theEmail.style.backgroundColor = "#FEFCC4";
			return false;
		}
	}
        return false;
}

function clearFieldColor(f){
	f.style.backgroundColor = "#ffffff";
	clearErrorMessage();

}

function clearErrorMessage(){
	if(document.getElementById("nom").style.backgroundColor == "#FFFFFF"
		&& document.getElementById("societe").style.backgroundColor == "#FFFFFF"
		&& document.getElementById("email").style.backgroundColor == "#FFFFFF"){

		document.getElementById("emailFormError").style.display = "none";
	}
}

function checkEmptyFields(){
	var emptyFields = 0;
	if(document.getElementById("nom").value == ""){
		document.getElementById("nom").style.backgroundColor = "#FDFA6F";
		emptyFields++;
	}
	if(document.getElementById("societe").value == ""){
		document.getElementById("societe").style.backgroundColor = "#FDFA6F";
		emptyFields++;
	}
	if(document.getElementById("tel").value == ""){
		document.getElementById("tel").style.backgroundColor = "#FDFA6F";
		emptyFields++;
	}
	var fEmail = document.getElementById("email");
	if(fEmail.value == ""){
		document.getElementById("email").style.backgroundColor = "#FDFA6F";
		emptyFields++;
	}
	if(emptyFields == 0 && !checkEmail(fEmail)){
		return false;
	}


	if(emptyFields > 0){
		document.getElementById("emailFormError").style.display = "block";
		if(emptyFields == 1){
			document.getElementById("emailFormErrorMsg").innerHTML = "Le champ obligatoire en jaune est vide !";
		}
		else{
			document.getElementById("emailFormErrorMsg").innerHTML = "Les champs obligatoires en jaune sont vides !";
		}
		return false;
	}
	else{
		document.getElementById("emailFormError").style.display = "none";
		sendFromEmail();
		return true;
	}
}



