/****
Fichier de scripts javascript
****/

function setCook(nom,valeur) {
	document.cookie = nom + "=" + escape(valeur);
}

function setCook2(nom,valeur,jours) {
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000));
	document.cookie = nom + "=" + escape(valeur) + ";expires=" + expDate.toGMTString();
}

function delCook(nom) { 
	setCook2(nom,"",-1); 
}

function getCook(nom) {
	var deb,fin;
	deb = document.cookie.indexOf(nom + "=");
	if (deb >= 0) {
	    deb += nom.length + 1;
	    fin = document.cookie.indexOf(";",deb);
	    if (fin < 0) fin = document.cookie.length;
	    return unescape(document.cookie.substring(deb,fin))
	}
	return "";
}

function askCook(nom) {
	 c=prompt("Mettre dans le cookie :","");
	setCook2(nom,c,3);
}

function litCook(nom) {
	c=getCook(nom);
	if(c=="") alert("Le cookie est vide !");
	else alert("Le cookie vaut : "+c);
}


//redirection vers la page de login
function killcookie(){
		delCook("user");
		delCook("user_nom");
		delCook("user_prenom");
		delCook("user_pharmacie");
		window.location="login.php";
	}

function EcrireCookie(nom, valeur)
{
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
	//alert(document.cookie);
}

function showhideSousCat(what)
{
	SousCat = document.getElementById("SousCat"+what);	
	CatIcone = document.getElementById("CatIcone"+what);
	if (SousCat.style.display == 'none')
	{
		EcrireCookie(SousCat.id, 1);
		SousCat.style.display='';
		//CatIcone.src='pics/down.gif'
		CatIcone.src='img/puce2.gif';

	}
	else
	{
		EcrireCookie(SousCat.id, 0);
		SousCat.style.display='none';
		//CatIcone.src='pics/right.gif';
		CatIcone.src='img/puce2.gif'
	}
}

function validation()
{
	var bool=true;
	var msg="";
	var at=document.frmclientedition.txtmail.value.indexOf("@");
	var dot=document.frmclientedition.txtmail.value.indexOf(".");
	var email=document.frmclientedition.txtmail.value;
	var num=parseInt(email.length)-1;

	if(document.getElementById('txtnom').value == "")
	{
		bool = false;
		msg+="nom";
		document.getElementById('txtnom').focus();
	}
	
	else if(document.getElementById('txtprenom').value == "")
	{
		bool = false;
		msg+="prenom";
		document.getElementById('txtprenom').focus();
	}

	else if(document.getElementById('txtmail').value=="")
	{
		bool=false;
		msg+="mail";
		document.getElementById('txtmail').focus();
	}

	else if((at<1)||(at>=email.length))
	{
		bool=false;
		msg+="valid email";
		document.getElementById('txtmail').focus();
	}

	else if(dot<1)
	{
		bool=false;
		msg+="valid email";
		document.getElementById('txtmail').focus();
	}
	
	else if(email.substr(num,email.length)==".")
	{
		bool=false;
		msg+="valid email";
		document.getElementById('txtmail').focus();
	}
		
	else if((at-dot==-1)||(at-dot==1))
	{
		bool=false;
		msg+="valid email";
		document.getElementById('txtmail').focus();
	}

	else if(document.getElementById('txtpswd').value=="")
	{
		bool=false;
		msg+="mot de passe";
		document.getElementById('txtpswd').focus();
	}

	else if(document.getElementById('txtcpswd').value=="")
	{
		bool=false;
		msg+="confirmer mot de passe";
		document.getElementById('txtcpswd').focus();
	}

	else if(document.getElementById('txtpswd').value!=document.getElementById('txtcpswd').value)
	{
		bool=false;
		msg+="meme mot de passe";
		document.getElementById('txtcpswd').focus();
	}

	else if(document.getElementById('txtrsocial').value=="")
	{
		bool=false;
		msg+="raison sociale";
		document.getElementById('txtrsocial').focus();
	}
	else if(document.getElementById('txtrue').value=="")
	{
		bool=false;
		msg+="rue";
		document.getElementById('txtrue').focus();
	}
	else if(document.getElementById('txtruesuite').value=="")
	{
		bool=false;
		msg+="rue suite";
		document.getElementById('txtruesuite').focus();
	}
	else if(document.getElementById('txtcpostal').value=="")
	{
		bool=false;
		msg+="code postal";
		document.getElementById('txtcpostal').focus();
	}
	else if(document.getElementById('txtville').value=="")
	{
		bool=false;
		msg+="ville";
		document.getElementById('txtville').focus();
	}
	
	else if(document.getElementById('txttel').value=="")
	{
		bool=false;
		msg+="telephone domicile";
		document.getElementById('txttel').focus();
	}
	
	if(bool==false)
	{
		alert("Veuillez entrer le\n " + msg + " SVP");
		return false;
	}
	else
		return true;
}

function reloadpage(catid)
{
	var moncookie = getCook(catid);
	
	if(moncookie == "")
	{
		//setCook(moncookie, "show");
		setCook(catid, "show");
		setCook("img"+catid, "down.gif");
		//alert("vide");
	}
	else
	{
		if(moncookie == "show")
		{
			//setCook(moncookie, "hide");
			setCook(catid, "hide");
			setCook("img"+catid, "right.gif");
			//alert(moncookie);
		}
		else
		{
			//setCook(moncookie, "show");
			setCook(catid, "show");
			setCook("img"+catid, "down.gif");
			//alert(moncookie);
		}
	}
	
	window.location = 'index.php?catid='+catid;
}

function derniere_article(subcatid)
{
	setCook("subcatid", subcatid);
}

function validate(detail) 
{  
	var missing = "";
	if (detail.recherche.value == "") missing += "article\n";
	
	if (missing != "") 
	{
	   alert("Veuillez saisir un "
	   + missing
	   + "et re-envoyer");
	  return false;
	}
	else return true;
}

function gotopage()
{
	if(document.myfrm1.histo_cmd.value == "histo")
	{
		window.location.href = "historique_commande.php?choice=histo";
	}
}

var tab_menu=new Array;

tab_menu[tab_menu.length]=8;
tab_menu[tab_menu.length]=10;		
tab_menu[tab_menu.length]=15;

/****
affiche et masques les rubriques d'un menu
****/
		
function show_menu(menu_id) 
{
	alert('ici');
	for(i=0;i<tab_menu.length;i++) 
	{
		if(!document.getElementById('table_menu_'+tab_menu[i])) continue;
		if(menu_id == tab_menu[i])
		{
			EcrireCookie('tab_menu'+tab_menu[i], tab_menu[i]);
			document.getElementById('table_menu_'+tab_menu[i]).style.display='';
		}
		else
		{
			delCook('tab_menu'+tab_menu[i]);
			document.getElementById('table_menu_'+tab_menu[i]).style.display='none';
		}
	}
}
