/* Permet d'imprimer le contenu d'un DIV */
function PrintContent(div1,div2,div3)
{
	 var DocumentContainer1 = document.getElementById(div1);
	 var DocumentContainer2 = document.getElementById(div2);
	 var DocumentContainer3 = document.getElementById(div3);
	 
	 var WindowObject = window.open('', "Imprimer","width=730,height=625,top=250,left=345,toolbars=no,scrollbars=no,status=no,resizable=true");
	 WindowObject.document.write('<html>\n');
	 WindowObject.document.write('<head>\n');
	 WindowObject.document.write('<link rel="stylesheet" type="text/css"  href="/epex/front/css/standard.css" />');
	 WindowObject.document.write('<style type="text/css">body {background : #ffffff} input {border : 0}</style>\n');
	 WindowObject.document.write('</head>\n');
	 WindowObject.document.write('<body>\n');
	 
	 if (DocumentContainer1 != null)
	 {
		 WindowObject.document.write(DocumentContainer1.innerHTML);
		 WindowObject.document.write("<br style='clear:both'/>");
	 }
	 
	 if (DocumentContainer2 != null)
	 {
		 WindowObject.document.write(DocumentContainer2.innerHTML);
	 }

	 if (DocumentContainer3 != null)
	 {
		 WindowObject.document.write(DocumentContainer3.innerHTML);
	 }
	 WindowObject.document.write('</body>\n');
	 WindowObject.document.write('</html>\n');	 
	 WindowObject.document.close();
	 WindowObject.focus();
	 WindowObject.print();
	 WindowObject.close();
}

// imprime le resultat d'une action
function printContentProjet(url)
{
	var WindowObject = window.open(url, "Imprimer","width=730,height=625,top=250,left=345,toolbars=no,scrollbars=no,status=no,resizable=true");
	//var WindowObject = window.open(url, "Imprimer");
	//WindowObject.document.addEventListener("load", WindowObject.print(), false);
	//WindowObject.print();
	//WindowObject.close();
}

function openSendMailDialog(idprojet){
	posPopin();
	showDiv();
	$("#form_sendMail_idprojet").val(idprojet);
}

function sendMailDetailProjet(site,idDiv,idProjet, idStand)
{
	if(idProjet==null){
		idProjet = $("#form_sendMail_idprojet").val();
	}
	var fromSession = false;
	if(idStand==""){
		fromSession = true;
	}
	var bValid = true;
	bValid = bValid && isValidEmailAddress("#emailDialog",$(".validateTips"));
	if ($("#emailCopyDialog").val() != "")
			bValid = bValid && isValidEmailAddress("#emailCopyDialog",$(".validateTips"));
	
	//valeur de la checkbox "envoyer une copie", true ou false par cette methode
	var valueCopyMe= $("#emailCopyMeDialog").is(':checked');
	//valeur du message de la textarea
	var message = $("#emailMessageDialog").val();
	
	if (bValid) {
		$.ajax({ url: site,
				 data: "idprojet="+idProjet+"&idstand="+idStand+"&email="+$("#emailDialog").val()+"&sujet="+$("#subjectDialog").val()+"&copy="+$("#emailCopyDialog").val()+"&copyMe="+valueCopyMe+"&message="+message,
		         //passage en post pour garder les retour chariot du message
				 type:"POST",
				 //content type en UTF-8 
				 contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				 success: function(result){
			      if(fromSession){
			    	  $(idDiv).hide();  
			      } else {
			    	  hideDiv();
			      }
				 },
				 error:function (XMLHttpRequest, textStatus, errorThrown){
				   alert("erreur survenu dans l'envoi de l'email.");
				 //  $(idDiv).hide();
				   if(fromSession){
				    	  $(idDiv).hide();  
				      } else {
				    	  hideDiv();
				      }
				 } 

		});						
	}
				
}
//fonction pour cacher une div particulière.
//@param idDiv le nom de la div a cacher(avec le #)
function cacherDiv(idDiv) {
	$(idDiv).remove('#fade');
	$(idDiv).hide();
}

//fonction pour montrer une div particulière.
//@param idDiv le nom de la div a cacher(avec le #)
function montrerDiv(idDiv) {
	$('<div id="fade"></div>').appendTo(idDiv);
	posPopin();
	$(idDiv).show();
}

function sendMailDetailProjetSession(site,idDiv){
	posPopin();
	$(idDiv).show();

}

function alertPlan(idDiv,idForm)
{
		$(idDiv).dialog({
			autoOpen: true,
			height: 400,
			width: 350,
			position: 'top',
			modal: true,
			buttons: {
				'Ok': function() {
						$(this).dialog('close');
						document.getElementById(idForm).submit();
						}
						
				,
				'Annuler': function() {
					$(this).dialog('close');
					return false;
				}
			}
		});
}
function updateTips(tipsLocal, t) {
	tipsLocal
		.text(t)
		.addClass('ui-state-highlight');
	setTimeout(function() {
		tipsLocal.removeClass('ui-state-highlight', 1500);
	}, 500);
}

function checkLength(o,n,min,max,tipsLocal) {

	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		updateTips(tipsLocal, "Taille du champ " + n + " doit être compris entre "+min+" et "+max+".");
		return false;
	} else {
		return true;
	}
}

function checkRegexp(o,regexp,n,tipsLocal) {

	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		updateTips(tipsLocal, n);
		return false;
	} else {
		return true;
	}

}
function isValidEmailAddress(email,classErreur) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

	return checkRegexp($(email),pattern ,"Mauvais email",classErreur);
	//return pattern.test(emailAddress);
}

/**
 * Check de l'input entrée de la quantité. 
 * Doit etre un entier; sinon on remet l'ancienne valeur.
 * @param id, l'id de la prestation a checker
 * @param value la valeur entrée dans l'input.
 */
function checkQuantite(id, value) {
	var reg = /^[0-9]+$/;
	var re = new RegExp(reg);
	/**champ quantite*/
	var qte="#input_quantite_"+id;
	/**champ caché contenant la valeur precedente*/
	var oldqte="#oldqte"+id;

 	 if(re.test(value)==false) {
 	/**en cas d'echec, on remet l'ancienne valeur*/	 
      oldQuantite = $(oldqte).val();
	  $(qte).val(oldQuantite);
	
	  }
 	 else {
 	 /**sinon on update le champ cache*/
	  //quantite = $(qte).val();
	  $(oldqte).val(value);
	  }	    
}

function ajouteAuPanier(idprestation){
	var quantite;
	quantite = $("#input_quantite_"+idprestation).val();
	if (quantite == null || quantite == undefined || quantite == "" )
		quantite = 1;
	
	$.ajax({
		url : '/epex/do/module/panierProjet?reqCode=ajouteAuPanier',
		data : "idprestation="+idprestation+"&quantite="+quantite,
		cache: false,
		success : function(retour){
			$("#div_recapcommande").html(retour);
		}
	});
}

function enleveDuPanier(idprestation){
	$.ajax({
		url : '/epex/do/module/panierProjet?reqCode=enleveDuPanier',
		data : "idprestation="+idprestation,
		cache: false,
		success : function(retour){
			$("#div_recapcommande").html(retour);
		}
	});
}

function enregistreProjetExistant(){
	$.ajax({
		url : '/epex/do/module/panierProjet?reqCode=enregistreProjetExistant',
		success : function(retour){
			$("#div_recapcommande").html(retour);
			//affichage de la popin d'alerte 
			   showDiv();
		}
	});
}

function videProjet(disclaimer){
	if(confirm(disclaimer)){
		$.ajax({
			url : '/epex/do/module/panierProjet?reqCode=vide',
			success : function(result){
				$("#div_recapcommande").html(result);
			} 
		});
	}
}

function ajaxChangeLangue(langue){
	$.ajax({
		url : '/epex/do/ChangeLangue',
		data: 'langue='+langue,
		cache: false,
		success : function(retour){
			parent.location.reload();
		},
	
	    error:function (XMLHttpRequest, textStatus, errorThrown){
				   alert("erreur survenu dans le chagement de langue.");
				   alert(errorThrown);
				 
				 } 

		});									   

}

function submitForm(idForm, actionUrl,valReqCode){
	var vForm = document.getElementById(idForm);
	vForm.action = actionUrl;
	vForm.reqCode.value=valReqCode;
	vForm.submit();
}

/* Julien CA */
function posPopin(){
	var popinHeightDivided = $(".popin_block").height()/2;
	var scroll = $(window).scrollTop();
	var wHeight = $(window).height();
	var pos = scroll+(wHeight/2)-popinHeightDivided+'px';
	$(".popin_block").css('top',pos);
}

function logout(){
	//$.cookie("login", "");
	//$.cookie("password", "");
	document.cookie = 'login=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
	document.cookie = 'password=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
	document.location.href='/epex/do/loginSecurise?reqCode=logout';
}
