// ouvre un fenetre center
function open_win( html , width, height ){

  var client_w = screen.width;
  var client_h = screen.height;
  
  // trouve le centre de l'ecran
  var win_left = (client_w - width) / 2;
  var win_top = (client_h - height) / 2;

  window.open(html,'','titlebar=no,resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=' + width + ',height=' + height + ',left=' + win_left + ',top=' + win_top + '');
}

// function pour afficher les swf avec ie et firefox
function displayFlash(swf, largeur , hauteur, alternatif)
{
  document.write('<object border="0" type="application/x-shockwave-flash" data="'+swf+'" width="'+largeur+'" height="'+hauteur+'">');
  document.write('<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" />');
  document.write('<param name="allowScriptAccess" value="sameDomain" />');
  document.write('<param name="movie" value="'+swf+'" />');
  document.write('<param name="quality" value="high" />');
  document.write(alternatif);
  document.write('</object>');
}

// pour savoir si ses
function is_ie(){
  var tmp_nav = navigator.userAgent.toLowerCase();
  var resultat = tmp_nav.search('msie');


  if (resultat != -1){
    return true;
  }else{
    return false;
  }
}
//-----------------------------
// pour avoir les name avec IE A MARDE
function getElementsByName_iefix(tag, name) {

     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute('name',2);
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}
//-----------------------------
// function pour changer les prix dynamiquement pourle module produit
function changeText(id, id_produit , id_input , default_prix , id_total){

  item_num = window.document.getElementById(id_input).value;
  //alert( item_num );
  default_prix = default_prix.replace(',','.');

  new_prix = item_num * parseFloat(default_prix) ;
  new_prix2 = fdp(new_prix+'',2) ;

  // pour les objet link
  var str_link = 'link_add_' + id;


   // check si total existe
  find_link = true;
  try{
    prix_total = window.document.getElementById(str_link).innerHTML;
  } catch(e){
    find_link = false;
  }

  if (item_num == '') {
    item_num = 0;
    default_prix = '0,00 $';
    window.document.getElementById(id_input).value = 0;
  }

  //pour changer le prix
  if ( (item_num != 'NaN') && (item_num != '') )   {
    new_prix2 = new_prix2.replace('.',',');
    window.document.getElementById(id_produit).innerHTML = new_prix2 + ' $';

    //si les link pour ajouter existe change la qty du lien
    if (find_link == true){


      var link_value = window.document.getElementById(str_link).href;
      var resultat = link_value.search('&qty=');

      if(resultat != -1){
        link_value = link_value.slice(0,resultat);
      }

      link_value = link_value + '&qty=' + item_num ;
      window.document.getElementById(str_link).href = link_value;
    }

  }else{
    default_prix = default_prix.replace('.',',');
    window.document.getElementById(id_produit).innerHTML = default_prix;
  }


  // --------- si les lien existe pas -------------
  if (find_link == false){
    back_prix_total = window.document.getElementById(id_total).innerHTML;

    // hack pour IE
    if (is_ie() == false) {
      //alert('pas ie');
      prix_total = window.document.getElementsByName('liste_produits_prix');
    }else{
      //alert('ses ie');
      prix_total = getElementsByName_iefix('div','liste_produits_prix');
    }


    //alert('nombre de produit: ' + prix_total.length);
    // get tout les prix pour changer les total
    var new_total = 0;
    for(i=0; i< prix_total.length; i++){
      tmp_prix = prix_total[i].innerHTML;
      tmp_prix = tmp_prix.replace(',','.');
      new_total = parseFloat(new_total) + parseFloat(tmp_prix);
    }



    var final_total = fdp(new_total + '',2);
    final_total = final_total.replace('.',',');
    //alert(final_total);

    var resultat = back_prix_total.search(':');

    if(resultat != -1){
      tmp_value = back_prix_total.slice(0,resultat);
      window.document.getElementById(id_total).innerHTML = tmp_value + ': ' + final_total + ' $';
    }
  }

}
// pour effacer les cookis par timer

function timer_cookie_del (){
 // setTimeout("alert('cool')",1000);
}

function onEnter(e){
var key = (window.event) ? event.keyCode : e.which;
  if (window.event){
    key = event.keyCode
  }else{
    key = e.which
  }

  if(key==13){
    document.form_login.submit();
  }
}

function fdp(n,d){
	var xx = n.indexOf('.')
	var l = n.length
	var zstr = '0000000000000000000000'
	var theInt = ''
	var theFrac = ''
	var theNo = ''
	rfac = ''
	rfacx = 0
	nx = 0
	var xt = parseInt(d) + 1
	var rstr = '' + zstr.substring(1,xt)
	var rfac = '.' + rstr + '5'
	var rfacx = parseFloat(rfac)
	if (xx == -1 ) 	{    // No fraction
		theFrac = zstr
		theInt = "" + n
	}
	else if (xx == 0) {
		theInt = '0'
		nx = 0 + parseFloat(n) + parseFloat(rfacx)
		n = nx + zstr
		theFrac = '' + n.substring(1, n.length)
	}
	else {
		theInt = n.substring(0,xx)
		nx = parseFloat(n) + rfacx
		n = '' + nx + zstr
		theFrac = '' + n.substring(xx+1,xx + 1 + parseInt(d))
		var astr = 'd = ' + d
	}
	theFrac = theFrac.substring(0,parseInt(d))
	var ii = 0
	theNo = theInt + '.' + theFrac
	return theNo
}

function del_cookies(cookieName){

  var doc_cookies = document.cookie;
  var url = window.document.URL;
  //alert(window.document.title);
 // alert('Les cookies ' + doc_cookies + ' ' + url);

  if( window.screenLeft < 10004){
    //refresh
  }else{
    cookieExpiration = new Date(1900,0,1)
    window.document.cookie = cookieName + "=;path=/;expires=" + cookieExpiration.toGMTString();
  //  alert('Les cookies ' + cookieName +  ' sont effacer');
  }
}
