// --------- Fonctions JavaScript
function openMenu(menu) {
	//document.getElementById("U10").style.display = "none";
	//document.getElementById("U20").style.display = "none";
	//document.getElementById("U30").style.display = "none";
	//if(menu == "Q10") document.getElementById("U10").style.display = "block";
	//else if(menu == "Q20") document.getElementById("U20").style.display = "block";
	//else if(menu == "Q30") document.getElementById("U30").style.display = "block";
	document.getElementById(menu).style.backgroundPosition = "bottom";
	menu = "U" + menu.substring(1);
	document.getElementById(menu).style.display = "block";
}
function openCategory(category) {
	document.getElementById(category).style.display = "block";
}
// Vérifier :
// IE = conflit Name vs. Id ?...


function validateLogin() {
    return checkForm(document.Login, new Array ('EMail','Password'));
}




// Onglets Index
var ongletIndex;
var imgIndex;
	
function ongletsIndex(i) {
	imgIndex = document.getElementById("img_index");
	// Décalages des onglets
	document.getElementById("onglet_index0").style.marginLeft = "0px";
	document.getElementById("onglet_index1").style.marginLeft = "0px";
	document.getElementById("onglet_index2").style.marginLeft = "0px";
	if(i == 0) document.getElementById("onglet_index0").style.marginLeft = "9px";
	else if(i == 1) document.getElementById("onglet_index1").style.marginLeft = "9px";
	else if(i == 2) document.getElementById("onglet_index2").style.marginLeft = "9px";
	// Changement grande image
	i = (i+1)*10;
	document.getElementById("img_index_src").src = "img/index_img"+i+".jpg";
}
	
function ongletsIndexOut(i) {
	document.getElementById("onglet_index0").style.marginLeft = "0px";
	document.getElementById("onglet_index1").style.marginLeft = "0px";
	document.getElementById("onglet_index2").style.marginLeft = "0px";
	document.getElementById("img_index_src").src = "img/index_imgfocus.jpg";
}
// Onglets Index



// Onglets Products
	var current = "";

	function initOnglet(start, i) {
		current = start;
			document.getElementById(start+"_name").style.visibility = "hidden";					
			document.getElementById(start+"_name_selected").style.visibility = "visible";
		document.getElementById("onglet_product_line").style.display = "none";
		document.getElementById("onglet_product_specs").style.display = "none";
		document.getElementById("onglet_product_spare").style.display = "none";
		document.getElementById("onglet_product_advice").style.display = "none";
		document.getElementById("onglet_product_video").style.display = "none";
		document.getElementById(start).style.display = "block";
		ongletUpdate(start, i);
	}

	function ongletUpdate(nouvo, i) {
		// Visibilité du contenu		
		document.getElementById(current).style.display = "none";
		document.getElementById(nouvo).style.display = "block";
			//spares img //
			document.getElementById("img_spare").style.visibility = "hidden";
			document.getElementById("img_spare_shadow").style.visibility = "hidden";
		// Visibilité des onglets
		document.getElementById(current+"_img").style.zIndex = (i+4) - (i*2);
		document.getElementById(nouvo+"_img").style.zIndex = "10";
		// Visibilité des textes onglets
		document.getElementById(current+"_name").style.visibility = "visible";
		document.getElementById(current+"_name_selected").style.visibility = "hidden";
		document.getElementById(nouvo+"_name").style.visibility = "hidden";					
		document.getElementById(nouvo+"_name_selected").style.visibility = "visible";

		// Nouvelle valeur de current
		current = nouvo;
	}
// Onglets Products



// Efface ou remplit champs saisie
function eraseField(champ) {
	if(champ.name == "EMail" && champ.value == "adresse e-mail") document.getElementById("EMail").value = "";
	else if(champ.name == "Password" && champ.value == "mot de passe") document.getElementById("Password").value = "";
	// Empeche disparition du formulaire on rollOut
	nom = navigator.appName; 
	if (nom == 'Microsoft Internet Explorer') {
		document.getElementById("login_div").style.margin = "-161px -212px 0 0";
		document.getElementById("login_div").style.height = "102px";
	}
	else {
		document.getElementById("login_div").style.margin = "-161px -6px 0 0";
		document.getElementById("login_div").style.height = "102px";
	}
}

function fillField(champ) {
	if(champ.name == "EMail" && champ.value == "") document.getElementById("EMail").value = "adresse e-mail";
	else if(champ.name == "Password" && champ.value == "") document.getElementById("Password").value = "mot de passe";
}
// Efface ou remplit champs saisie



// Img spare parts
function showImg(id, elTop, isSpare) {
	img = new Image();

	if (isSpare == "0")  {
		div_id = "img_product";
		document.getElementById("img_spare").style.visibility = "hidden";
		document.getElementById("img_spare_shadow").style.visibility = "hidden";
		document.getElementById("img_product_shadow").style.visibility = "hidden";
		img.src = "image?id="+id;
	}
	else {
		div_id = "img_spare";		
		img.src = "http://www.bluewaterparts.com/shelf/image?id="+id;
		document.getElementById("img_spare_shadow").style.visibility = "hidden";
		if (isSpare == "1") {
			document.getElementById("img_product").style.visibility = "hidden";
			document.getElementById("img_product_shadow").style.visibility = "hidden";
			//Ajustement vertical pour spares
			elTop += 60 + document.getElementById("onglet_line").offsetTop + document.getElementById("bigImageBom").height;
		}
		else if (isSpare == "2") {
			elTop += document.getElementById("bigImageBom").offsetTop + document.getElementById("bigImageBom").height;
		}
		else if (isSpare == "4" && navigator.appName == "Microsoft Internet Explorer") {
			elTop += 170;
		}
	}

	document.getElementById(div_id).innerHTML = "";
	document.getElementById(div_id).style.backgroundImage = "url(img/ajaxloader2.gif)";
	document.getElementById(div_id).style.top = elTop + "px";	
	document.getElementById(div_id).style.height = "32px";
	document.getElementById(div_id).style.width = "32px";		
	document.getElementById(div_id).style.visibility = "visible";
	
	//Hack IE7 pour les images déjà chargées
	if(img.height != 0) {
		document.getElementById(div_id).style.backgroundImage = "url("+img.src+")";
		document.getElementById(div_id).style.height = img.height + "px";
		document.getElementById(div_id).style.width = img.width + "px";
		document.getElementById(div_id).innerHTML = "<a onclick='hideImg();'><span>Cliquez pour fermer l'image</span><div></div></a>";
		document.getElementById(div_id).innerHTML += "<img src='img/z.png' style='margin: -30px 0 0 -3px; cursor: pointer;' width='"+(img.width+6)+"' height='"+(img.height+6)+"' border='0' />";
		showImgShadow();
	}

	img.onload = function() {
		//document.getElementById("img_spare"+id).style.backgroundImage = "url("+img.src+")";
		document.getElementById(div_id).style.backgroundImage = "url("+img.src+")";
		document.getElementById(div_id).style.height = img.height + "px";
		document.getElementById(div_id).style.width = img.width + "px";
		document.getElementById(div_id).innerHTML = "<a onclick='hideImg();'><span>Cliquez pour fermer l'image</span><div></div></a>";
		document.getElementById(div_id).innerHTML += "<img src='img/z.png' style='margin: -30px 0 0 -3px; cursor: pointer;' width='"+(img.width+6)+"' height='"+(img.height+6)+"' border='0' />";
		showImgShadow();
	}
}

function hideImg() {
	document.getElementById(div_id).style.visibility = "hidden";
	document.getElementById(div_id+"_shadow").style.visibility = "hidden";
}

function showImgShadow() {
	document.getElementById(div_id+"_shadow").style.top = document.getElementById(div_id).offsetTop - 8 + "px";
	document.getElementById(div_id+"_shadow").style.left = document.getElementById(div_id).offsetLeft - 7 + "px";
	document.getElementById(div_id+"_shadow").style.height = document.getElementById(div_id).offsetHeight + 17 + "px";
	document.getElementById(div_id+"_shadow").style.width = document.getElementById(div_id).offsetWidth + 17 + "px";
		document.getElementById(div_id+"_s2").style.width = document.getElementById(div_id).offsetWidth - 23 + "px";
		document.getElementById(div_id+"_s4").style.width = document.getElementById(div_id).offsetWidth - 3 + "px";
		document.getElementById(div_id+"_s4").style.height = document.getElementById(div_id).offsetHeight - 23 + "px";
		document.getElementById(div_id+"_s5").style.height = document.getElementById(div_id).offsetHeight - 23 + "px";
		document.getElementById(div_id+"_s7").style.width = document.getElementById(div_id).offsetWidth - 23 + "px";
	if(document.getElementById(div_id).style.visibility == "visible") document.getElementById(div_id+"_shadow").style.visibility = "visible";
}
// Img spare parts



// Tests validité formulaires
function AJAXInteraction(url, callback) {

   var req = init();
   req.onreadystatechange = processRequest;
          function init() {
     if (window.XMLHttpRequest) {
       return new XMLHttpRequest();
     } else if (window.ActiveXObject) {
       return new ActiveXObject("Microsoft.XMLHTTP");
     }
   }
      function processRequest () {
     // readyState of 4 signifies request is complete
     if (req.readyState == 4) {
       // status of 200 signifies sucessful HTTP call
       if (req.status == 200) {
         if (callback) callback(req.responseXML);
       }
     }
   }

   this.doGet = function() {
     req.open("GET", url, true);
     req.send(null);
   }
}

var validateEmail;
var proceed_bt;

function validateUserEmail(email) {
	validateEmail = email;
	proceed_bt = email;
   var target = document.getElementById(email);
   var url = "validate?email=" + encodeURIComponent(target.value);
   var target = document.getElementById(email);
   var ajax = new AJAXInteraction(url, validateCallback);
   ajax.doGet();
}

function validatePostal(email) {
  validateEmail = email;
  var target = document.getElementById(email);
  var countryId = document.getElementById('ID_C_Country_ID').options[document.getElementById('ID_C_Country_ID').selectedIndex].value;
  var url = "validate?postal=" + encodeURIComponent(target.value)+"&countryId="+encodeURIComponent(countryId);
  var target = document.getElementById(email);
  var ajax = new AJAXInteraction(url, validateCallback);
  ajax.doGet();
} 

function changeCountry(countrySelectId) {
  var txt = document.getElementById('ID_C_Country_ID').options[document.getElementById('ID_C_Country_ID').selectedIndex].text;
  document.getElementById('selectcountry_txt').innerHTML = txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
  validatePostal("Postal");
} 

function validateCallback(responseXML) {
  var msg = responseXML.getElementsByTagName("valid")[0].firstChild.nodeValue;
  if (msg == "false"){
      var mdiv = document.getElementById(validateEmail);
      // set the style on the div to invalid
      mdiv.className = "bp_invalid";
	  mdiv.style.backgroundImage = "url('img/x.png')";
	  mdiv.style.backgroundRepeat = "no-repeat";
	  mdiv.style.backgroundPosition = "98%";
   }
   else {
      var mdiv = document.getElementById(validateEmail);
      // set the style on the div to valid
      mdiv.className = "bp_valid";
	  mdiv.style.backgroundImage = "url('img/v.png')";
	  mdiv.style.backgroundRepeat = "no-repeat";
	  mdiv.style.backgroundPosition = "98%";
   }
}
// Tests validité formulaires
