/**
 * Fonctions BainUltra
 *
 * @author		Simon
 * @copyright	egzakt.com
 * @since		2007-03-26
 * @package		BUI-104
 *
 * @version		2007-03-26 Simon :	Definition des fonctions
 */

/*
 * Affichage d'un contenu enfant
 * Utilisee dans les sections a 2/3 colonnes, et dans la boutique
 */
function show_contenu(li) {
	li.children("div").toggle();
	li.toggleClass("selected");
	return false;
}

/*
 * Affichage d'un contenu suivant
 * Utilisee dans les sections a 2/3 colonnes
 */
function show_nextcontenu(o) {
	o.next().toggle();
	o.toggleClass("selected");
	o.parent().toggleClass("selected");
	return false;
}

/*
 * Aller a un contenu, a partir d'un objet <select>
 * Utilisee Produits
 */
function jumpto(o) {
	window.location.href = o.val();
	return true;
}

/*
 * Validation formulaire enregistrement de garantie
 */
function validation_enregistrement_garantie() {
	if (!$("#prenom").val()) {
		alert(lang == "fr"?"Entrer votre prénom":"Fill your first name");
		$("#prenom").focus();
		return false;
	}
	if (!$("#nom").val()) {
		alert(lang == "fr"?"Entrer votre nom":"Fill your last name");
		$("#nom").focus();
		return false;
	}
	if (!$("#adresse").val()) {
		alert(lang == "fr"?"Entrer votre adresse":"Fill your address");
		$("#adresse").focus();
		return false;
	}
	if (!$("#ville").val()) {
		alert(lang == "fr"?"Entrer votre ville":"Fill your city");
		$("#ville").focus();
		return false;
	}
	if (!$("#province").val()) {
		alert(lang == "fr"?"Entrer votre province":"Fill your province or state");
		$("#province").focus();
		return false;
	}
	if (!$("#pays").val()) {
		alert(lang == "fr"?"Entrer votre pays":"Fill your country");
		$("#pays").focus();
		return false;
	}
	if (!$("#code_postal").val()) {
		alert(lang == "fr"?"Entrer votre code postal":"Fill your postal code");
		$("#code_postal").focus();
		return false;
	}	
	if (!$("#telephone").val()) {
			alert(lang == "fr"?"Entrer votre numéro de téléphone":"Fill your phone number");
			$("#telephone").focus();
			return false;
	}
	if (!$("#courriel").val()) {
			alert(lang == "fr"?"Entrer votre courriel":"Fill your email");
			$("#courriel").focus();
			return false;
	}else if (!check_email($("#courriel").val())){
		alert(lang == "fr"?"Format de courriel invalide":"Invalid email format");
		$("#courriel").focus();
		return false;
	}	
	if (!$("#numero_serie").val()) {
		alert(lang == "fr"?"Entrer votre numéro de série":"Fill your serial number");
		$("#numero_serie").focus();
		return false;
	}

	if($("#garantie_formulaire")){
		$("#garantie_formulaire").submit();
	}
	
	if($("#Warranty_Form")){
			$("#Warranty_Form").submit();
	}
	
	return false;
}

/*
 * Validation formulaire commande dans la boutique
 */
function validation_soins() {
	if (!$("#nom").val()) {
		alert(lang == "fr"?"Entrer votre nom":"Fill your last name");
		$("#nom").focus();
		return false;
	}
	if (!$("#adresse").val()) {
		alert(lang == "fr"?"Entrer votre adresse":"Fill your address");
		$("#adresse").focus();
		return false;
	}
	if (!$("#ville").val()) {
		alert(lang == "fr"?"Entrer votre ville":"Fill your city");
		$("#ville").focus();
		return false;
	}
	if (!$("#province").val()) {
		alert(lang == "fr"?"Entrer votre province":"Fill your province or state");
		$("#province").focus();
		return false;
	}
	if (!$("#pays").val()) {
		alert(lang == "fr"?"Entrer votre pays":"Fill your country");
		$("#pays").focus();
		return false;
	}
	if (!$("#code_postal").val()) {
		alert(lang == "fr"?"Entrer votre code postal":"Fill your postal code");
		$("#code_postal").focus();
		return false;
	}
	if (!$("#telephone").val()) {
		alert(lang == "fr"?"Entrer votre téléphone":"Fill your phone number");
		$("#telephone").focus();
		return false;
	}
	if (!check_email($("#courriel").val())) {
		alert(lang == "fr"?"Courriel invalide":"Invalid E-mail address");
		$("#courriel").focus();
		return false;
	}

	$("#soins_formulaire").submit();
	return false;
}

/*
 * Validation envoyer a un ami
 */
function validate_send(f) {
	if (!check_email($("#courriel_ami").val())) {
		alert(lang == "fr"?"Courriel invalide":"Invalid E-mail address");
		$("#courriel_ami").focus();
		return false;
	}
	if (!check_email($("#courriel").val())) {
		alert(lang == "fr"?"Courriel invalide":"Invalid E-mail address");
		$("#courriel").focus();
		return false;
	}
	if (!$("#nom").val()) {
		alert(lang == "fr"?"Entrer votre nom":"Fill your name");
		$("#nom").focus();
		return false;
	}
	return true;
}

/*
 * Validation formulaire commande de documentation
 */
function validation_documentation() {
	if (!$("#nom").val()) {
		alert(lang == "fr"?"Entrer votre nom":"Fill your name");
		$("#nom").focus();
		return false;
	}
	if (!$("#adresse").val()) {
		alert(lang == "fr"?"Entrer votre adresse":"Fill your address");
		$("#adresse").focus();
		return false;
	}
	if (!$("#ville").val()) {
		alert(lang == "fr"?"Entrer votre ville":"Fill your city");
		$("#ville").focus();
		return false;
	}
	if (!$("#province").val()) {
		alert(lang == "fr"?"Entrer votre province":"Fill your province or state");
		$("#province").focus();
		return false;
	}
	if (!$("#pays").val()) {
		alert(lang == "fr"?"Entrer votre pays":"Fill your country");
		$("#pays").focus();
		return false;
	}
	if (!$("#code_postal").val()) {
		alert(lang == "fr"?"Entrer votre code postal":"Fill your postal code");
		$("#code_postal").focus();
		return false;
	}
		
	if (!$("#telephone").val() && $("#phoneMandatory").val() == "yes") {
		
		alert(lang == "fr"?"Entrer votre téléphone":"Fill your phone number");
		$("#telephone").focus();
		return false;
	}
	
	$("#documentation_formulaire").submit();
	return false;
}

// Reset formulaires
function formulaire_reset(f) {
	if (lang == "fr") {
		if (confirm("Êtes vous sûr de vouloir vider le formulaire?")) {
			document.getElementById(f).reset();
		}
	} else {
		if (confirm("Are you sure you want to erase the form?")) {
			document.getElementById(f).reset();
		}
	}
	return false;
}

/*
 * apple.com
 */
function check_email(s) {
	var emailFilter=/^.+@.+\..+$/;
	if (!check_illegal(s)) {
		return emailFilter.test(s);
	} else {
		return false;
	}
}
function check_illegal(s) {
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	return s.match(illegalChars);
}


/*
 * Boutique, mise a jour des quantites
 * Appelee au onKeyUp()
 * Simon - ezgzakt.com
 */
function boutique_update(o) {
	var itemtotal = (o.children("input[@type=hidden]").val() * o.children().children("input[@type=text]").val()) * 100;
	var tab_item_nb = new Array();

	// Test de la valeur
	if (parseInt(itemtotal) < 0) {
		return false;
	}

	// Total
	var total = 0;
	var i=0;
	// Pour ne pas "classer" les input, on boucle dans les text et les hidden du conteneur
	$(".soin_quantite input[@type=text]").each(function() {
		tab_item_nb[++i] = this.value;

	});
	var i=0;
	$(".soin_quantite input[@type=hidden]").each(function() {
		total += parseInt(tab_item_nb[++i] * this.value);
	});

	montant = new Array();
	montant = round(itemtotal);
	montanttotal = new Array();
	montanttotal = round(total*100);

	if (lang == "fr") {
		o.children("span").html("= " + montant[0] + ","+ montant[2]+" $");
		$("#total").html(montanttotal[0] + ","+ montanttotal[2]+" $");
	} else {
		o.children("span").html("= $" + montant[0] + "."+ montant[2]);
		$("#total").html("$" + montanttotal[0] + ","+ montanttotal[2]);
	}
	$("#amount_total").val(montanttotal[0] + "."+ montanttotal[2]);
	return true;
}

/*
 * Boutique, valider l'entre utilisateur
 * Appelee au onKeyDown()
 */
function valider_chiffre(e) {
	if (window.event) e = window.event;
	return (((e.keyCode>=48) && (e.keyCode<=57)) || ((e.keyCode>=96) && (e.keyCode<=105)) ||
		(e.keyCode==96) || (e.keyCode<=13));
}

/*
 * Boutique, arrondir le montant
 * Arrondir centieme (cents)
 */
function round(n) {
	n = Math.round(n) / 100;
	var montant = new String(n);
	montant = montant.split(".");
	if (montant[1]) {
		montant[2] = montant[1];
	} else {
		montant[1] = montant[2] = "";
	}

	// Pad 0
	for (i=montant[1].length;i<2;i++) {
		montant[2] = montant[2] + "0";
	}
	return montant;
}
/*
 * Fonction d'ouverture de fenetre popup centree
 * Simon - egzakt.com
 * 2004-10-28
 */
function egz_openwindow_param(lien,cible,w,h,param) {
	var _win;
	_win = window.open(lien,cible,param);
	_win.moveTo((screen.width - w)/2,(screen.height - h)/2);
	_win.focus();

	return _win;
}

/*
 * Envoyer a un ami
 */
function envoyer_ami(a) {
	egz_openwindow_param(a.href + "?url="+document.location.href,this.target,550,500,'width=650,height=520,scrollbars=yes');
	return false;
}


/*
 * Sélectionne / Déselectionne des checkboxes
 * Utilisee dans le comparateur
 */
function toggle_checkboxes(bool) {
	$("input[@type='checkbox']").each(function() {
		this.checked = bool;
	});

	return false;
}


// PHP trim() equivalent
// Used in Product Features
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re,"$1");
}
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re,"$1");
}

/* Generer un nombre aleatoire entre 2 valeurs
 * Exception si le nombre aleatoire a deja etet trouve auparavant
 *
 */
function aleatoire(min,max,no) {
	var rd = (Math.round(Math.random()*(max-min)))+min;
	if (rd == no) {
		rd = ((rd+1) > max) ? min:rd+1;
	}
	return rd;
}

/*
 * IE6 Click to activate
 * Ecriture des obj flash
 */
function write_flash(file,obj_id,bgcolor,w,h) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+w+'" height="'+h+'" id="'+obj_id+'" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+file+'" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="'+bgcolor+'" />');
	document.write('<embed src="'+file+'" quality="high" bgcolor="'+bgcolor+'" width="'+w+'" height="'+h+'" name="'+obj_id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}


/* PRODUCTS
 *
 * Utilisee par la navigation des gammes de produits
 */
var navGamme = new Object;
navGamme.navWidth = 480;
navGamme.visibleGammes = 5;
navGamme.movingLeft = false;
navGamme.movingRight = false;
navGamme.myCtr = 0;
navGamme.flecheGaucheActif = true;
navGamme.flecheDroiteActif = true;

function setFleches(){
	if(parseInt($("#listeGammesContainer div").css("margin-left")) > - navGamme.visibleGammes){
		$("#listeGammesContainer div").css("margin-left",0)
	}
	myPos = $("#listeGammesContainer div").css("margin-left");
	if((parseInt(myPos) < navGamme.visibleGammes) && (parseInt(myPos) >= 0)){
		$("#flecheGauche").css("opacity",".2");
		navGamme.flecheGaucheActif = false;
		$("#flecheDroite").css("opacity",1);
		navGamme.flecheDroiteActif = true;
	} else {
		$("#flecheGauche").css("opacity",1);
		navGamme.flecheGaucheActif = true;
		newPos = parseInt(myPos) - navGamme.navWidth;
		maxVisibleProd = Math.round(Math.abs(newPos/navGamme.navWidth) * navGamme.visibleGammes);
		if(maxVisibleProd >= $("#listeGammesContainer div").children().length){
			$("#flecheDroite").css("opacity",".2");
			navGamme.flecheDroiteActif = false;
		} else {
			$("#flecheDroite").css("opacity",1);
			navGamme.flecheDroiteActif = true;
		}
	}
}

function flecheGaucheClick(){
	if(!navGamme.movingLeft){
		navGamme.movingLeft = true;
		myPos = $("#listeGammesContainer div").css("margin-left");
		if (window.myPos) {
			myPos = myPos.replace(/px/gi,"")
			if (myPos < -navGamme.visibleGammes){
				newPos = parseInt(myPos) + navGamme.navWidth;
				$("#listeGammesContainer div").animate({marginLeft:newPos}, "slow", "backout",function(){navGamme.movingLeft = false;setFleches();});
			} else {
				navGamme.movingLeft = false;
			}
		}
	}

	return false;
}


function flecheDroiteClick(){
	if(!navGamme.movingRight){
		navGamme.movingRight = true;
		myPos = $("#listeGammesContainer div").css("margin-left");
		newPos = parseInt(myPos) - navGamme.navWidth;
		maxVisibleProd = Math.round(Math.abs(newPos/navGamme.navWidth) * navGamme.visibleGammes);
		if(maxVisibleProd <= $("#listeGammesContainer div").children().length){
			$("#listeGammesContainer div").animate({marginLeft:newPos}, "slow", "backout",function(){navGamme.movingRight = false;setFleches();});
		} else {
			navGamme.movingRight = false;
		}
	}
	return false;
}

function listeGammesContainerChildrenEach(obj){
	navGamme.myCtr += 1;
	if(obj.attr("class").indexOf("selected") != -1){
		//navGamme.movingRight = true;
		myPos = $("#listeGammesContainer div").css("margin-left");
		myPos = myPos.replace(/px/gi,"");
		newPos = myPos - ((Math.ceil(navGamme.myCtr/navGamme.visibleGammes)-1) * navGamme.navWidth);
		//$("#listeGammesContainer div").animate({marginLeft:newPos}, "slow", "backout",function(){navGamme.movingRight = false;setFleches();});
		$("#listeGammesContainer div").css("margin-left",newPos);
		setFleches();
	}
}


// name - name of the desired cookie
// return string containing value of specified cookie or
//  null if cookie does not exist
// http://www.webreference.com/js/column8/functions.html
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


/* Copyright (c) 2006 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Thanks to Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 */
jQuery.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* @example value = $.getURLParam("paramName");
*/
 getURLParam: function(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  var bFound=false;

	  var cmpstring = strParamName + "=";
	  var cmplen = cmpstring.length;

	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = aParam[1];
	        bFound=true;
	        break;
	      }

	    }
	  }
	  if (bFound==false) return null;
	  return strReturn;
	}
});


/* 2007-04-23 Simon
 * Retourne le seul parametre de l'URL
 */
jQuery.extend({
	getOneParam: function(){
		if (window.location.search) {
			return window.location.search.substring(1);
		} else {
			return null;
		}
	}
});

/*
* Change l'url du francais vers l'anglais ou vice-verca
* Auteur: Rémy Dufour <rdufour@propage.com>
* Date: 2007-04-25
*
* Propage
*/
function _cl( langue )
{
    var loc = document.location.pathname;

    if (langue == 'fr') {
        document.location = loc.replace('/bainultra/en', '/bainultra/fr');
    } else {
        document.location = loc.replace('/bainultra/fr', '/bainultra/en');
    } // if

} // function

function _clVedana( langue )
{
    var loc = document.location.pathname;

    if (langue == 'fr') {
        document.location = loc.replace('/vedana/en', '/vedana/fr');
    } else {
        document.location = loc.replace('/vedana/fr', '/vedana/en');
    } // if

} // function


function orderHideAndCheck()
{
	// For order documentation section, hide acconrding to query string a certain portion of DOM
	// check element selected
	
	var toDisplay = $.getURLParam("section");
	var toCheck = $.getURLParam("produit");
	
	if(toDisplay == "documentation"){		
		document.getElementById("choix2").style.display = "none";		
	}else{
		document.getElementById("choix1").style.display = "none";
		document.getElementById("phoneMandatory").value = "yes";
	}
	
	
	// Check radio OR checkbox
	if(toCheck == "dvd"){
		document.getElementById("CHOIXFormat").checked = true;
		
	}else if(toCheck == "Wellness"){
		document.getElementById("QTEGuide").checked = true;
		
	}else if(toCheck == "BrochureProduit"){
		document.getElementById("QTECatalogue").checked = true;
		
	}else{
		document.getElementById(toCheck).checked = true;
		
	}	
}

function showAllDocumentation()
{
    var loc = document.location.pathname;
    loc = loc + "#Documentation_Center";
    document.location = loc.replace('products', 'ressources');
}


// global variable for document opened pdf and others.. Doc center
var theDocumentWindow;

function showDocument(theLang,theWinTitle,theURL)
{
	var popUpUrl = "/web/wcm/connect/bainultra/" + theLang + "/download";

	theDocumentWindow = window.open (popUpUrl,theWinTitle,"location=0,menubar=0,resizable=1,toolbar=0,status=1,scrollbars=1,width=670,height=760");

	setTimeout("loadURLtoDoc('" + theURL + "');",1000);
	
}

function loadURLtoDoc (theUrl)
{
	theDocumentWindow.document.location.href = theUrl;
}