/**************************************************************************
**
**		Copyright © ARONE, 2002-2006
**				All Rights reserved.
**
**
**  FONCTIONS CLIENTES
**************************************************************************/

function FindID(id) {
	return FindIDFrom(id,document)
}

function FindIDParent(id) {
	return FindIDFrom(id,window.opener.document)
}

function FindIDFrom(id,collection) {
	var lo_ctrl;
			
	if(collection.layers) {
		lo_ctrl=collection.layers[id];
	} else if (collection.all && !collection.getElementsByName) {
		lo_ctrl=collection.all[id];
	} else if (collection.all && collection.getElementsByName) {
		lo_ctrl=collection.getElementsByName(id);
	} else if (!collection.all && collection.getElementsByName) {
		lo_ctrl=collection.getElementsByName(id);
	}	
	
	if (lo_ctrl.length==0) {
		try {
			lo_ctrl=undefined;
		} catch(er) { 
			lo_ctrl=null;
		}		
	} else if (lo_ctrl.length==1) {
		lo_ctrl=lo_ctrl[0];
	}		

	if (!lo_ctrl) {
		if(collection.layers) {
			lo_ctrl=collection.layers[id];
		} else if (collection.all && !collection.getElementById) {
			lo_ctrl=collection.all[id];
		} else if (collection.all && collection.getElementById) {
			lo_ctrl=collection.getElementById(id);
		} else if (!collection.all && collection.getElementById) {
			lo_ctrl=collection.getElementById(id);
		}
	}
	
	
	return lo_ctrl

	/*if(document.layers) return document.layers[id];
	if(document.all && !document.getElementById) return document.all[id];
	if(document.all && document.getElementById) return document.getElementById(id);
	if(!document.all && document.getElementById) return document.getElementById(id);*/
}


function getQueryFrom(as_name) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == as_name) {
      return pair[1];
    }
  } 
  //alert('Query Variable ' + as_name + ' not found');
	return "";
}



/**************************************************************
* right: Returns a String containing n characters at the end of 
* the parameter String.
*
* Parameters:
*
* Returns: String
***************************************************************/
function Right(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

/**************************************************************
* left: Returns a String containing n characters at the end of 
* the parameter String.
*
* Parameters:
*
* Returns: String
***************************************************************/
function Left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}


/**************************************************************
* Mid: Returns a String containing n characters at the end of 
* the parameter String.
*
* Parameters:
*
* Returns: String
***************************************************************/
function Mid(str, n, l){
	
	if (n<0)
	{
		n=0;
	}
	
	if (!l)
	{	
		l=String(str).length-n+1;
	}


	return String(str).substring(n-1,n-1+l);
}

function GetUrl()
{
	var ls_url=document.location.href;
	ls_url=Left(ls_url, ls_url.indexOf("?"));
	
	ls_url+="?crit=true"

	if (document.location.href.indexOf("comefrom=labo") > 0)
	{
		ls_url+="&comefrom=labo"
	}
	
	
	if (FindID("CritClass1").value!=-1)
	{
		ls_url += "&id_classe_1=" + FindID("CritClass1").value;	
		
		
		if (FindID("CritClass2").value!=-1)
		{
			ls_url += "&id_classe_2=" + FindID("CritClass2").value;	
			
			if (FindID("CritClass3").value!=-1)
			{
				ls_url += "&id_classe_3=" + FindID("CritClass3").value;					
			}
		}
	}
	
	try
	{
		if (FindID("CritLabo").value!=-1)
		{
			ls_url += "&id_laboratoire=" + escape(FindID("CritLabo").value);			
		}

		if (FindID("CritFourn").value!="")
		{
			ls_url += "&reference_fournisseur=" + escape(FindID("CritFourn").value);			
		}
		
		if (FindID("CritCodeDM").value!="")
		{
			ls_url += "&code_dm=" + escape(FindID("CritCodeDM").value);			
		}
		
		if (FindID("CritProduit").value!="")
		{
			ls_url += "&produit=" + escape(FindID("CritProduit").value);			
		}
		
		if (FindID("CritCodeLPP").value!="")
		{
			ls_url += "&code_lpp=" + escape(FindID("CritCodeLPP").value);			
		}
	}
	catch(err)
	{
	}
	
	return ls_url
}


function GetUrlLexique()
{
	var ls_url=document.location.href;
	ls_url=Left(ls_url, ls_url.indexOf("?"));
	
	ls_url+="?crit=true"

	if (FindID("CritClass1").value!=-1)
	{
		ls_url += "&id_classe_1=" + FindID("CritClass1").value;					
	}
	
	if (FindID("CritClass2").value!=-1)
	{
		ls_url += "&id_classe_2=" + FindID("CritClass2").value;					
	}

	if (FindID("CritClass3").value!=-1)
	{
		ls_url += "&id_classe_3=" + FindID("CritClass3").value;					
	}

	return ls_url
}


function onChangeCriteria()
{		
	document.location.href = GetUrl() + "&search=" + getQueryFrom("search")
}

function onChangeCriteriaLexique()
{		
	document.location.href = GetUrlLexique() + "&search=" + getQueryFrom("search")
}

function executeSearch()
{
	document.location.href = GetUrl() + "&search=true"
}

function executeRAZ()
{
	FindID("CritClass1").value = "-1";
	FindID("CritClass2").value = "-1";
	FindID("CritClass3").value = "-1";
	FindID("CritLabo").value = "-1";
	FindID("CritProduit").value = "";
	FindID("CritCodeDM").value = "";
	FindID("CritFourn").value = "";
	FindID("CritCodeLPP").value = "";
}

function addToCaddie(ai_id_pdt, as_produit)
{		
	if (document.location.href.indexOf("search=true") > 0)
	{
		document.location.href = GetUrl() + "&search=true" + "&add_id_pdt=" + ai_id_pdt + "&add_pdt_label=" + as_produit
	}
	else
	{
		var ls_urn=document.location.href;
		if (ls_urn.indexOf("&cut=true") < 0) 
		{
		document.location.href = ls_urn + "&cut=true" + "&add_id_pdt=" + ai_id_pdt + "&add_pdt_label=" + as_produit
		}
		else
		{
		ls_urn=Left(ls_urn, ls_urn.indexOf("&cut=true"));
		document.location.href = ls_urn + "&cut=true" + "&add_id_pdt=" + ai_id_pdt + "&add_pdt_label=" + as_produit
		}
		
	}
	
}

function delFromCaddie(ao_liste)
{
	
	if (ao_liste)
	{
		if (ao_liste.options.length>0)
		{
			if (ao_liste.value)
			{
				var li_id_pdt=ao_liste.value;
				var ls_produit=ao_liste.options[ao_liste.selectedIndex].text;				
			
				if (confirm("Etes-vous sur de vouloir supprimer ce produit?"))
				{
					if (document.location.href.indexOf("search=true") > 0)
					{
						document.location.href = GetUrl() + "&search=true" + "&del_id_pdt=" + li_id_pdt +  "&del_pdt_label=" + ls_produit
					}
					else
					{
						var ls_urn=document.location.href;
						if (ls_urn.indexOf("&cut=true") < 0) 
						{
						document.location.href = ls_urn + "&cut=true" + "&del_id_pdt=" + li_id_pdt +  "&del_pdt_label=" + ls_produit
						}
						else
						{
						ls_urn=Left(ls_urn, ls_urn.indexOf("&cut=true"));
						document.location.href = ls_urn + "&cut=true" + "&del_id_pdt=" + li_id_pdt +  "&del_pdt_label=" + ls_produit
						}
						
					}
										
				}
			}
			else
			{
				alert('Vous devez sélectionner un produit!')	
			}
		}
		else
		{
			alert('Le devis est vide!')	
		}
	}
	else
	{
		alert('Error in List!')	
	}
}

function OpenWindow(chemin,nom,fenetre){
	window.open(chemin,nom,fenetre);
}

function ShowButton() 
{
	if (FindID('form_cont').checked=='')

	{
		FindID('btn_suite').disabled = true;
		
	}
	else
	{
		FindID('btn_suite').disabled = false;
	}
}

function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

