// Denis.js
// ---------

function in_array(s,a){
	for(var k in a)
   	if(a[k]==s)
      	return true;
	return false;
}


function PanierAdd(get_url, item, colour, qty)
{
   document.panier_add_form.action      = get_url;
   document.panier_add_form.CMD.value   = "PANIER_ADD";
   document.panier_add_form.ITEM.value  = item;
   document.panier_add_form.COLOUR.value= colour;

   if(isNaN(qty) || (qty.length == 0))
      qty = "1";

   document.panier_add_form.QTY.value   = parseInt(qty);

   document.panier_add_form.submit();
}

function PanierRem(get_url, panier_id, lang)
{
   var   conf = false;
   
   if(lang == 'FR')
      conf = confirm("Effacer du panier ?");
   else
      conf = confirm("Remove from cart?");
   
   if(conf)
   {
      document.panier_rem_form.action           = get_url;
      document.panier_rem_form.CMD.value        = "PANIER_REM";
      document.panier_rem_form.PANIER_ID.value  = panier_id;
      document.panier_rem_form.submit();
   }
}

function PanierProduitCmt(get_url, panier_id, lang, cmt)
{
   
   if(lang == 'FR')
      cmt = prompt("Votre commentaire (50 caractères max.):", cmt);
   else
      cmt = prompt("Your comment (max. 50 characters):", cmt);
   
   if((cmt != null) && (cmt.length))
   {
      document.action_form.action            = get_url;
      document.action_form.CMD.value         = "PANIER_CMT";
      document.action_form.PANIER_ID.value   = panier_id;
      document.action_form.COMMENTAIRE.value = cmt;
      document.action_form.submit();
   }
   
}

function PanierProduitCmtRem(get_url, panier_id, lang)
{
   var   conf = false;
   
   if(lang == 'FR')
      conf = confirm("Effacer ce commentaire ?");
   else
      conf = confirm("Remove this comment?");
   
   if(conf)
   {
      document.action_form.action            = get_url;
      document.action_form.CMD.value         = "PANIER_CMT";
      document.action_form.PANIER_ID.value   = panier_id;
      document.action_form.COMMENTAIRE.value = '';
      document.action_form.submit();
   }
   
}

function PanierEmpty(get_url, lang)
{
   var   conf = false;

   if(lang == 'FR')
      conf = confirm("Vider votre panier?");
   else
      conf = confirm("Empty your cart?");

   if(conf)
   {
      document.panier_rem_form.action           = get_url;
      document.panier_rem_form.CMD.value        = "PANIER_EMPTY";
      document.panier_rem_form.submit();
   }
}

function PanierSauvegarder(get_url, lang)
{
   var   conf = false;

   if(lang == 'FR')
      conf = confirm("Transférer panier aux commandes retenues ?");
   else
      conf = confirm("Transfer basket to orders on hold?");

   if(conf)
   {
      document.panier_rem_form.action           = get_url;
      document.panier_rem_form.CMD.value        = "PANIER_SAUVEGARDER";
      document.panier_rem_form.submit();
   }
}

//function PanierSauvegarderNotify(get_url, lang)
//{
//   var   conf = false;
//
//   if(lang == 'FR')
//      conf = confirm("Transférer panier aux commandes commandes en attente et notifier votre/vos patrons(s) ?");
//   else
//      conf = confirm("Transfer basket to pending orders and notify your supervisors(s)?");
//
//   if(conf)
//   {
//      document.panier_rem_form.action           = get_url;
//      document.panier_rem_form.CMD.value        = "PANIER_SAUVEGARDER_NOTIFY";
//      document.panier_rem_form.submit();
//   }
//}

function PanierSubmitNotify(get_url, lang)
{
   var   conf = false;

   if(lang == 'FR')
      conf = confirm("Soumettre cette commande pour approbation ?");
   else
      conf = confirm("Submit this order for approval?");

   if(conf)
   {
      document.panier_rem_form.action           = get_url;
      document.panier_rem_form.CMD.value        = "PANIER_SOUMETTRE_APPRO";
      document.panier_rem_form.submit();
   }
}

function PanierSubmitNotifyAdmin(get_url, lang)
{
   var   conf = false;

   if(lang == 'FR')
      conf = confirm("Soumettre cette commande pour approbation ?");
   else
      conf = confirm("Submit this order for approval?");

   if(conf)
   {
      document.panier_rem_form.action           = get_url;
      document.panier_rem_form.CMD.value        = "PANIER_SOUMETTRE_APPRO_ADMIN";
      document.panier_rem_form.submit();
   }
}

function ValidateQuantity(el, lang)
{
   if(isNaN(el.value) && (el.value.length > 0))
   {
      if(lang == 'FR')
         alert('Cette valeur doit être un chiffre plus grand que 0 ou laissez ce champ vide.');
      else
         alert('This value must be a number greater than 0 or leave this field empty.');
               
      el.select();
      el.focus();
   }
   else
   {
      if(el.value == "0" || el.value == "")
      {
         el.value = "";
      }
      else
      {
         el.value = parseInt(el.value);
      }
      
   }
}

function ValidateItem(el, required_len, lang)
{
   if(isNaN(el.value) || ((el.value.length > 0) && (el.value.length != required_len)))
   {
      if(lang == 'FR')
         alert('Cette valeur doit être un chiffre de ' + required_len + ' caractères ou laissez ce champ vide.');
      else
         alert('This value must be a  ' + required_len + ' digit number or leave this field empty.');
               
      el.select();
      el.focus();
   }
}

function autofocus(field, next, evt)
{
    evt = (evt) ? evt : event;
    
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));


    if (charCode > 31 && field.value.length == field.maxLength)
    {
         
        field.form.elements[next].focus( );
    }
}

function ProdSearchBuildCategories(n1_id)
{
   // N1
   document.product_search.R_N1.options.length = 0;

   var i;
   for(i=0; i<niv1.length; i++)
   {
      //alert(niv1[i]);
   
      document.product_search.R_N1.options[i] = new Option(niv1[i], i);

      //alert("i=" + i + " niv1=" + niv1[i] + " sel=" + sel);
   }

   document.product_search.R_N1.options[n1_id].selected = true;
}

// Niveau 1
// --------
function ProdSearchN1Change(n2_id)
{
   document.product_search.R_N2.options.length = 0;

   var n1 = document.product_search.R_N1.value;

   //alert(n1);


   var i;
   var sel = null;
   for(i=0; i<niv2[n1].length; i++)
   {
      //alert(niv2[n1][i]);

      if(niv2[n1][i][0] == n2_id)
         sel = i;
      
      document.product_search.R_N2.options[i] = new Option(niv2[n1][i][1], niv2[n1][i][0]);
   }

   if(sel != null)
      document.product_search.R_N2.options[sel].selected = true;
}

function ProdSearchN2Change(n3_id)
{
   document.product_search.R_N3.options.length = 0;

   var n2 = document.product_search.R_N2.value;

   //alert(n2);

   var i;
   var sel = null;
   for(i=0; i<niv3[n2].length; i++)
   {
      //alert(niv3[n2][i]);

      if(niv3[n2][i][0] == n3_id)
         sel = i;
      
      document.product_search.R_N3.options[i] = new Option(niv3[n2][i][1], niv3[n2][i][0]);
   }

   if(sel != null)
      document.product_search.R_N3.options[sel].selected = true;

}


function n2Build(form_name, n2_field, n1_id, n2_id, only)
{
   document.forms[form_name].elements[n2_field].options.length = 0;

   var i;
   var sel = null;
	var t = !(only);
   for(i=0; i<niv2[n1_id].length && (t || in_array(i,only)); i++)
   {
      if(niv2[n1_id][i][0] == n2_id)
         sel = i;
      
      document.forms[form_name].elements[n2_field].options[i] = new Option(niv2[n1_id][i][1], niv2[n1_id][i][0]);
   }

   if(sel != null)
      document.forms[form_name].elements[n2_field].options[sel].selected = true;

}

function n2Change(form_name, n2_field, n3_field, n3_id, lang)
{
   document.forms[form_name].elements[n3_field].options.length = 0;

   var n2 = document.forms[form_name].elements[n2_field].value;

   var offset = 0;
   if(n3_id == -1)
   {
      offset = 1;

      var def;
      if(lang == 0)
         def = '-- Choisissez une sous-catégorie --';
      else
         def = '-- Choose a sub-category --';

      document.forms[form_name].elements[n3_field].options[0] = new Option(def, '');   
      document.forms[form_name].elements[n3_field].options[0].selected = true;
   }

   //alert(n2);

   var i;
   var sel = null;
   for(i=0; i<niv3[n2].length; i++)
   {
      //alert(niv3[n2][i]);
      //alert("n2=" + n2 + " i=" + i);

      if(niv3[n2][i][0] == n3_id)
         sel = i;

      document.forms[form_name].elements[n3_field].options[i+offset] = new Option(niv3[n2][i][1], niv3[n2][i][0]);
   }

   if(sel != null)
      document.forms[form_name].elements[n3_field].options[sel].selected = true;
}



function CheckForSubmit(formname)
{
	var code = 0;

   NS4 = (document.layers) ? true : false;
	if (NS4)
	{
   	//alert("ns4");
		code = event.which;
	}
	else
	{
   	//alert("not ns4");
		code = event.keyCode;
	}

	if (code==13)
	{
		formname.submit();
	}

   return(true);
}


function PopWindow(url, width, height)
{
   win = window.open(url,
                     "pop_window",
                     "toolbar=no,location=no,status=yes,resizable=yes,scrollbars=yes,width=" + String(width) + ",height=" + String(height) + ",top=50,left=50"
               );

   
   win.focus();
   
   
}

function openDhtmlConfirm(message, lang)
{
	var btn_cancel = "Cancel";
	if(lang=='FR')
		btn_cancel = "Annuler";
	
	if(document.getElementById('dhtml_confirm_window'))
		closeDhtmlConfirm();
	
	var div_window = document.createElement('DIV');
	div_window.id = 'dhtml_confirm_window';
	div_window.style.cssText = 'width:400px;background-color:#fafafa;font-weight:bold;border:2px solid #aaaaaa;position:absolute;top:'+(posY-70)+'px;left:30%;';
	var content = "<div style='text-align:right;background-color:#f0f0f0;padding:2px 3px;'><a href='javascript:void(0)' onclick='closeDhtmlConfirm()'>X</a></div>";
	content += "<div style='border-top:1px solid #aaa;border-bottom:1px solid #aaa;padding:5px 3px;'>"+message+"</div>";
	content += "<div style='text-align:right;padding:2px 3px;'><button id='dhtml_confirm_ok'>Ok</button><button onclick='closeDhtmlConfirm()'>"+btn_cancel+"</button></div>";
	
	var x = document.getElementsByTagName('BODY');
	var body = x[0];
	body.insertBefore(div_window,body.firstChild);
	document.getElementById('dhtml_confirm_window').innerHTML = content;
	
	document.getElementById("dhtml_confirm_ok").focus();
	return false;
}

function closeDhtmlConfirm()
{
	var popup = document.getElementById('dhtml_confirm_window');
	//alert(popup)
	var x = document.getElementsByTagName('BODY');
	var body = x[0];
	body.removeChild(popup);
}

var posY;
document.documentElement.onmouseover = getPosY;
function getPosY(e)
{
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posY = e.pageY;
	}
	else if (e.clientY) 	{
		posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
}