//
// Carnet de Route  -(c)opyright Alliance Réseaux - 2008
//					- LP

function EstMailValide(adresse)
{
	return new RegExp("^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\.([a-zA-Z]{2,5})$","g").test(adresse);
}

function EstGuidValide(guid)
{
	return new RegExp("^.{8}-.{4}-.{4}-.{4}-.{12}$","gi").test(guid);
}

function DonneIdCarnetCk()
{
	var regguid = new RegExp("idCarnetRoute=([^;]*)","gi").exec(document.cookie);	
	if(regguid != null && regguid.length >1) return regguid[1];
	return "";
}

function DonneIdCarnetReq()
{
	var regguid = new RegExp("idCarnetRoute=([^&]*)","gi").exec(window.location.search);	
	if(regguid != null && regguid.length >1) return regguid[1];
	return "";
}

function DonneNbCarnetCk()
{
	var regguid = new RegExp("nbCREntree=([^;]*)","gi").exec(document.cookie);	
	if(regguid != null && regguid.length >1) return regguid[1];
	return "";
}

function ObjCarnet()
{
	this.TagNbEntree = new Array();
	this.JoueMsg = true;
	this.JoueAlerteSuppression = false;
	this.JoueMsgEnvoi = true;
	this.UrlRESTCarnet = "/ILCarnetRoute/carnetrest.aspx";
	this.MsgAjoutOk = _MsgAjoutOk;
	this.MsgAjoutErr = _MsgAjoutErr;
	this.MsgProvenanceInvalide = _MsgProvenanceInvalide;
	this.MsgDestinataireInvalide = _MsgDestinataireInvalide;
	this.MsgMailEnvoye = _MsgMailEnvoye;
	this.MsgMailErreur = _MsgMailErreur;
	this.MsgSupprOk = _MsgSupprOk;
	this.MsgCarnetVide = _MsgCarnetVide;
	this.MsgCarnet1 = _MsgCarnet1;
	this.MsgCarnetN = _MsgCarnetN;
	this.AutoReloadSuppression = true;
	this.AjouteEntree = mtdAjouteCarnetEntree;
	this.AjoutePage = mtdAjoutePage;
	this.AjouteDepuisInput = mtdAjouteDepuisInput;
	this.AffecteCarnet = mtdAffecteCarnet;
	this.EnvoieCarnet = mtdEnvoieCarnet;
	this.SupprimeEntree = mtdSupprimeEntree;
	this.ApresAjout = null;
	this.ApresEnvoi = null;
	this.ApresSupprime = null;
	this.NbEntree = null;
	this.CheckNbEntree = mtdCheckNbEntree;
	this.PushTagNb = mtdPushTagNb;	
	this.Init = mtdInit;
}

function InitialiseNbEntreeCarnet()
{
	if(Carnet.NbEntree==null) PlaceCarnetNbEntree();
	if(Carnet.NbEntree==null) Carnet.CheckNbEntree();

}

function mtdInit()
{
	var newguid = DonneIdCarnetReq();
	if(newguid!="" && EstGuidValide(newguid) && newguid!=DonneIdCarnetCk()) 
	{
		this.AffecteCarnet(newguid);
	}
	else 
	{
		if(DonneIdCarnetCk()!="") InitialiseNbEntreeCarnet();
		else this.NbEntree = 0;
	}
	
}

var Carnet = new ObjCarnet();
Carnet.Init();


function DonneContenu(node) 
{	
	if(node==null) return "";
	if (typeof(node.textContent) != "undefined")
	{
		return node.textContent;
	}
	else if (typeof(node.innerText) != "undefined") 
	{
		return node.innerText;
	}
	else if (typeof(node.text) != "undefined")
	{
		return node.text;
	}
	else if (typeof(node.value) != "undefined")
	{
		return node.value;
	}
	else return "";

}

function DonneResultatAjax(obj)
{
   var Result;	 
	
	try
	{
		obj.responseXML.loadXML(obj.responseText);		
		Result = obj.responseXML.getElementsByTagName("Sortie");
	}
	catch(err2)
	{
		var xmlDocument = (new DOMParser()).parseFromString(obj.responseText, "text/xml");
		Result = xmlDocument.getElementsByTagName("Sortie");		
	}
	return Result;
}

function mtdAffecteCarnet(idcarnet)
{
	var XHR = new XHRConnection();
	XHR.appendData("Action", "AffecteCarnet");
	XHR.appendData("CarnetUrl", window.location.href);
	XHR.appendData("idCarnetRoute", idcarnet);
	XHR.sendAndLoad(this.UrlRESTCarnet, "POST",mtdRetourAffecteCarnet);
}

function mtdRetourAffecteCarnet(obj)
{
	InitialiseNbEntreeCarnet();
}	

function mtdAjouteCarnetEntree(titre,description,famille,urlphoto,lien,lienxml)
{
	var XHR = new XHRConnection();
	XHR.appendData("Action", "AjouteEntree");
	XHR.appendData("CarnetUrl", window.location.href);
	XHR.appendData("Titre", titre);
	XHR.appendData("Description", description);
	XHR.appendData("Famille", famille);
	XHR.appendData("UrlPhoto", urlphoto);
	XHR.appendData("Lien", lien);
	XHR.appendData("LienXml", lienxml);
	XHR.sendAndLoad(this.UrlRESTCarnet, "POST", mtdRetourAjouteCarnetEntree);
}

function mtdRetourAjouteCarnetEntree(obj)
{
	var Result = DonneResultatAjax(obj);	 
	var ret = DonneContenu(Result.item(0).getElementsByTagName("Retour").item(0));
	PlaceCarnetNbEntree();
	RafraichisTagNb();
	if(Carnet.JoueMsg)
	{
		if(ret=="OK") alert(Carnet.MsgAjoutOk);
		else alert(Carnet.MsgAjoutErr);
	}
	if(Carnet.ApresAjout != null) Carnet.ApresAjout();
}
/*
function mtdAjoutePage()
{
	var titre = "";
	var description = "";
	var obj = document.getElementsByTagName("meta"); 
	for(var i=0;i<obj.length;i++) 
	{
		var el = obj[i];
		if(DonneContenu(el.attributes["name"]).toLowerCase() == "description") description =DonneContenu(el.attributes["content"]);
	}
	obj = document.getElementsByTagName("title"); 
	if(obj[0] != null) titre = DonneContenu(obj[0]);
	if(titre != "") this.AjouteEntree(titre,description,"","",window.location.href,"");
}*/

function mtdAjoutePage()
{
	var titre = "";
	var description = "";
	var photo="";
	var famille="";
	var obj;
	
	if (document.getElementById("Carnet_Titre")){
		titre=document.getElementById("Carnet_Titre").value;
	} else {
		obj = document.getElementsByTagName("title"); 
		if(obj[0] != null) titre = DonneContenu(obj[0]);
	}

	if (document.getElementById("Carnet_Resume")){
		description=document.getElementById("Carnet_Resume").value;
	} else {
		obj = document.getElementsByTagName("meta"); 
		for(var i=0;i<obj.length;i++) 
		{
			var el = obj[i];
			if(DonneContenu(el.attributes["name"]).toLowerCase() == "description") description =DonneContenu(el.attributes["content"]);
		}
	}
	
	if (document.getElementById("Carnet_Photo")){
		photo=document.getElementById("Carnet_Photo").value;
	} 
	if (document.getElementById("Carnet_Famille")){
		famille=document.getElementById("Carnet_Famille").value;
	} 
	if(titre != "") this.AjouteEntree(titre,description,famille,photo,window.location.href,"");
}


// contenu input
function ContInp(inputid)
{
	if(inputid==null || inputid=="") return "";
	var obj = document.getElementById(inputid);
	return (obj==null) ? "":obj.value;
}

function mtdAjouteDepuisInput(ititre,idescription,ifamille,iurlphoto,ilien,ibaselien,ilienxml)
{
	var titre = ContInp(ititre);
	var lien = ContInp(ibaselien)+ContInp(ilien);
	if(titre!="" && lien!="") this.AjouteEntree(titre,ContInp(idescription),ContInp(ifamille),ContInp(iurlphoto),lien,ContInp(ilienxml))
}

function mtdEnvoieCarnet()
{
	var from = ContInp("CarnetFrom");
	var to = ContInp("CarnetTo");
	if(!EstMailValide(from))
	{
		alert(this.MsgProvenanceInvalide);return;
	}
	if(!EstMailValide(to)) 
	{
		alert(this.MsgDestinataireInvalide);return;
	}
	var XHR = new XHRConnection();
	XHR.appendData("Action", "EnvoieCarnet");
	XHR.appendData("Provenance", from);
	XHR.appendData("Destinataire", to);
	XHR.appendData("Copie", ContInp("CarnetToi"));
	XHR.appendData("Maquette", ContInp("CarnetMaquette"));
	XHR.appendData("UrlVisu", ContInp("CarnetUrl"));
	XHR.appendData("ObjEnvoi", ContInp("CarnetObjet"));
	XHR.sendAndLoad(this.UrlRESTCarnet, "POST",mtdRetourEnvoieCarnet);
}

function mtdRetourEnvoieCarnet(obj)
{
	var Result = DonneResultatAjax(obj);	 
	var ret = DonneContenu(Result.item(0).getElementsByTagName("Retour").item(0));
	if(Carnet.JoueMsgEnvoi) 
	{ 
		if(ret=="OK") alert(Carnet.MsgMailEnvoye);
		else alert(Carnet.MsgMailErreur);
	}
	if(Carnet.ApresEnvoi != null) Carnet.ApresEnvoi(ret);
}

function mtdSupprimeEntree(entree)
{
	if(!EstGuidValide(entree)) return;
	var XHR = new XHRConnection();
	XHR.appendData("Action", "SupprimeEntree");
	XHR.appendData("idEntree", entree);
	XHR.sendAndLoad(this.UrlRESTCarnet, "POST",mtdRetourSupprimeEntree);
}

function mtdRetourSupprimeEntree(obj)
{
	var Result = DonneResultatAjax(obj);	 
	var ret = DonneContenu(Result.item(0).getElementsByTagName("Retour").item(0));
	PlaceCarnetNbEntree();
	RafraichisTagNb();
	if(ret=="OK" && Carnet.JoueAlerteSuppression) alert(Carnet.MsgSupprOk);
	if(Carnet.ApresSupprime != null) Carnet.ApresSupprime();
	if(Carnet.AutoReloadSuppression) window.location.reload();
}

function mtdCheckNbEntree(obj)
{
	var XHR = new XHRConnection();
	XHR.appendData("Action", "DonneNombreEntree");
	XHR.sendAndLoad(this.UrlRESTCarnet, "POST",mtdRetourCheckNbEntree);
}

function PlaceCarnetNbEntree()
{
	var nbch = DonneNbCarnetCk();
	if(nbch!="") 
	{
		Carnet.NbEntree = parseInt(nbch);	
	}
	
}

function mtdRetourCheckNbEntree(obj)
{
		PlaceCarnetNbEntree();
}

function mtdPushTagNb(tag)
{
	this.TagNbEntree[this.TagNbEntree.length] = tag;
	AfficheTagNb(tag,0);
}

function AfficheTagNb(tag,cpt)
{
	if(cpt>20) return;
	var obj = document.getElementById(tag);
	if(obj==null) setTimeout("AfficheTagNb('"+tag+"',"+(cpt+1)+")",300);
	else if(Carnet.NbEntree != null)
	{
		if(Carnet.NbEntree<1) obj.innerHTML = Carnet.MsgCarnetVide;
		else if(Carnet.NbEntree == 1) obj.innerHTML = Carnet.MsgCarnet1;
		else obj.innerHTML = Carnet.MsgCarnetN.replace(new RegExp("\\{0\\}","g"),""+Carnet.NbEntree);
	}
}

function RafraichisTagNb()
{
	for(var i=0;i<Carnet.TagNbEntree.length;i++) 
	{
		AfficheTagNb(Carnet.TagNbEntree[i],0);
	}
}

//function asynch	
