/* ====================================== EVENEMENTS DIVERS ====================================================*/

//Evenement par défaut sur les champs de saisie pour mettre le fond en bleu lorsqu'il a le focus
function InputFocusEvt(inputElt) {
  inputElt.className = "inputBgColor";
}

//Evenement par défaut sur les champs de saisie pour remettre le fond comme il était avant lorsqu'il perd le focus
function InputBlurEvt(inputElt) {
  inputElt.className = "";
}

//Evenement sur un lien du menu pour l'apparence du bouton en mode "allumé"
function highlight(link)
{
  var imgId = 'btn'+link.id.substr(3,link.id.length);
  var img   = document.getElementById(imgId);
  var lg    = img.src.length;
  var ext   = img.src.substr(lg-3,lg);


  if (img.nodeName.toUpperCase() =="IMG") {
    img.src   = '../images/'+ imgId +'_on.'+ext;
  }
  else {
    var filter = img.style.getAttribute('filter',false);
    var start  = filter.indexOf(imgId+'.'+ext);

    if (start > 0) {
      filter = filter.substr(0,start)+ imgId + '_on.'+ ext + filter.substr(start+imgId.length+4,filter.length);
      img.style.setAttribute('filter',filter);
    }
  }
}

//Evenement sur les les liens du menu pour l'apparence du bouton en mode "normal"
function normalLook(link) {
  var imgId = 'btn'+link.id.substr(3,link.id.length);
  var img   = document.getElementById(imgId);
  var lg    = img.src.length;
  var ext   = img.src.substr(lg-3,lg);
  var str   = "";

  if (img.nodeName.toUpperCase() == "IMG")
    str = img.src;
  else {
    if (img.nodeName.toUpperCase() == "SPAN")
      str = img.style.getAttribute('filter',false);
  }

 var pos = str.indexOf("_on");
 if (pos > 0) str = str.substring(0,pos)+'.'+ext;

  if (img.nodeName.toUpperCase() == "IMG")
    img.src = str
  else {
    if (img.nodeName.toUpperCase() == "SPAN")
      img.style.setAttribute('filter',str);
  }
}

function affectFocusEvents(input) {

	if (input==null) return false;

  input.onfocus = new Function("onFocusSearch(this)");
  input.onblur  = new Function("onBlurSearch(this)");
}

function onBlurLogin(input,prefix,adheNum) {


  input.value = completeId(input.value,prefix,adheNum);


  if (IEBrowser && IEVersion < 7) InputBlurEvt(input);
}

function onFocusSearch(srchBox) {
  if ( (srchBox.value == 'Titre, auteur ou code') || (srchBox.value == 'Devis/commande')) {
    srchBox.value = '';
  }

  srchBox.style.color     = "black";
  srchBox.style.fontStyle = "normal";
  if (IEBrowser && IEVersion < 7) InputFocusEvt(srchBox);
}

function onBlurSearch(srchBox)
{
  srchBox.value = trim(srchBox.value);

  if (srchBox.value  == '') {
     srchBox.value           = srchBox.id == "searchtxt"  ? 'Titre, auteur ou code' : 'Devis/commande';
     srchBox.removeAttribute("style");
  }
  if (IEBrowser && IEVersion < 7) InputBlurEvt(srchBox);
}

function stretchRayon(rayoContainer)
{

  if (rayoContainer == null) exit;
  rayoContainer.style.width  = '300px';


  var rayonBox = document.getElementById('rayon_box');

  if (rayonBox!=null) {
    rayonBox.style.width    = '290px';
    rayonBox.style.overflow = 'auto';
  }

}

function normalRayon(rayoContainer)
{
  rayoContainer.style.width = '160px';


  var rayonBox = document.getElementById('rayon_box');
  if (rayonBox!=null) {
     rayonBox.style.width    = '150px';
     rayonBox.style.overflow = 'hidden';
  }
}

function initCalendar(input,offsetWidth) {
	if (!input || input.nodeName.toUpperCase()!="INPUT" || input.type!="text") return;

	var x = input.offsetTop + input.offsetHeight;
  var y = input.offsetLeft;

  if (IEBrowser) {
  	x+=140;
  }


	switch(input.id) {
		case "fVal" :

      if (IEBrowser) y+=60; else y-=100;
      input.onfocus = function() {showCalendar(calDate,"Date de d&eacute;but",input,x,y);}



		  break;

		case "fVal2" :
      if (IEBrowser) y+=140; else y-=offsetWidth;
      input.onfocus =  function() {showCalendar(calDate,"Date de fin",input,x,y);}

		  break;

	}
}

function showCalendar(calendar,title,input,x,y) {

	if (!input || !calendar || input.nodeName.toUpperCase()!="INPUT" || input.type!="text") return;

	if (!x) {
		x = input.offsetTop + input.offsetHeight;
		if (IEBrowser) {
			x+= input.id=="cmde_dated" || input.id=="cmde_datef" ? 439 : 142;
		}
	}
	if (!y) {
		 y = input.offsetLeft-30;
		 if (IEBrowser) y+=380;
	}

  calendar.init("calid",input);
  calendar.isDragable(true);
  calendar.setTitle(title && title !="" ? title : "");
  calendar.move(x,y);
  calendar.show();
  if (IEBrowser && IEVersion < 7) InputFocusEvt(input);
}


function hideCalendar(calendar,input) {
	if (!calendar) return;


	if (calDate && calDate.vis) {
	  calendar.hide();
  }

}

function checkFilter(select) {

	var selectedOption = select.options[select.options.selectedIndex];

	var labels = document.getElementsByTagName("LABEL");

	if (selectedOption.value.indexOf("date") >= 0 || selectedOption.value == "oeuv_dtparu") {

		for(var i =0;i < labels.length; i++) {


			if (labels[i].className =="hidden") {
				labels[i].className = "";
				var inputId = labels[i].htmlFor;
				if (inputId!="") {
				  var input = document.getElementById(inputId);

				  if (input) {

					  if (input.className == "hidden") input.className = "";

					  if (input.value != "") input.value = "";
					  initCalendar(input,labels[i].offsetWidth);
				  }
				}
			}
		}

	}
	else {
		calDate.hide();

		for(var i =0;i < labels.length; i++) {
			if (labels[i].htmlFor.indexOf("fVal") >= 0) {

				var input = document.getElementById(labels[i].htmlFor);

				if (input) {
					input.onfocus = (IEBrowser && IEVersion < 7) ? new Function("InputFocusEvt(this)") : null;

					if (labels[i].className != "hidden") {

						labels[i].className = "hidden";
						if (input.id == "fVal2" && input.className != "hidden") {
							input.className = "hidden";
						}
			  	}
			  }
			}
		}
	}




	input = document.getElementById("fVal");
	if (input.value != "") input.value = "";
	input.focus();
}

/**
 *
 * @access public
 * @return void
 **/
function addBtn(){
  if (validTabForm(document.tabForm)) {
    document.tabForm.submit();
  }
}
