//filtre les espaces dans une chaine de caractères
function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g,"");
}

function suppr_hotel(id_hotel, section){
	if(confirm("Voulez-vous supprimer cet hôtel ?")){
		location.href = "suppr_hotel.php?id_hotel=" + id_hotel + "&section=" + section;
	}
}

function changeCouleur(ligne, ma_couleur){
	ligne.bgColor = ma_couleur;
}

function remetCouleur(ligne, ma_couleur){
	ligne.bgColor = ma_couleur;
}

//ouvre une popup
function MM_openBrWindow(theURL, winName, features)
{
	window.open(theURL, winName, features);
}

//ouvre une popup centrée
function MM_openBrWindow2(theURL,winName, largeur, hauteur, features)
{
	var largeurEcran = (screen.width - largeur) / 2;
	var hauteurEcran = (screen.height - hauteur) / 2;
	window.open(theURL,winName,'top=' + hauteurEcran + ',left=' + largeurEcran + ',width=' + largeur + ',height=' + hauteur + ',' + features);
}

//envoi la date sélectionnée depuis le calendrier
function SetDate(jour, mois_annee, source)
{
	if(source == "cal"){
		self.opener.document.forms["recherche_dispos"].jour_arr.value = jour;
		self.opener.document.forms["recherche_dispos"].annee_mois_arr.value = mois_annee;
		window.close();
	}else{
		document.forms["recherche_dispos"].jour_arr.value = jour;
		document.forms["recherche_dispos"].annee_mois_arr.value = mois_annee;
	}
}

function SetDate2(jour, mois, annee)
{
	top.document.forms["send_mail"].arrivee.value = jour + '/' + mois + '/' + annee;
	parent.window.hs.close();
}

function SetDate3(jour, mois, annee)
{
	top.document.forms["recherche_dispos"].jour_arr.value = jour;
	top.document.forms["recherche_dispos"].annee_mois_arr.value = annee + '-' + mois;
	parent.window.hs.close();
}

//vérifie le formulaire d'envoi de mail
function VerifSendMail() 
{
	if((document.forms["send_mail"].nom_message.value == "") || (document.forms["send_mail"].email_message.value == "") || (document.forms["send_mail"].message.value == ""))
	{
		alert( "Please fill in all required fields");
	}else{
		if((document.forms["send_mail"].email_message.value.indexOf("@") == -1) || (document.forms["send_mail"].email_message.value.indexOf("@") == 0) || (document.forms["send_mail"].email_message.value.indexOf("@") != document.forms["send_mail"].email_message.value.lastIndexOf("@")) || (document.forms["send_mail"].email_message.value.indexOf(".") == document.forms["send_mail"].email_message.value.indexOf("@")-1) || (document.forms["send_mail"].email_message.value.indexOf(".") == document.forms["send_mail"].email_message.value.indexOf("@") +1) || (document.forms["send_mail"].email_message.value.indexOf("@") == document.forms["send_mail"].email_message.value.length -1) || (document.forms["send_mail"].email_message.value.indexOf (".") == -1) || (document.forms["send_mail"].email_message.value.lastIndexOf (".") == document.forms["send_mail"].email_message.value.length -1))
		{
			alert( "Your email should be in aaa@bbb.ccc format" ); 
		}else{
			document.forms["send_mail"].submit();
		}
	}
}

//ajouter la page aux favoris
function addFav(){
	if (document.all){
		window.external.AddFavorite(location.href, document.title);
	}else{
		alert('Your can press CTRL + D to add this page to your bookmarks.')
	}
}

//redimensionnement automatique
function getRefToDivMod( divID, oDoc ) {
	if( !oDoc ){
		oDoc = document;
	}
	if( document.layers ){
		if( oDoc.layers[divID] ){
			return oDoc.layers[divID];
		}else{
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ){
				y = getRefToDivNest(divID,oDoc.layers[x].document);
			}
			return y;
		}
	}
	if( document.getElementById ){
		return oDoc.getElementById(divID);
	}
	if( document.all ){
		return oDoc.all[divID];
	}
	return document[divID];
}

function resizeWinTo() {
	if( !document.images.length ){
		document.images[0] = document.layers[0].images[0];
	}
	if( !document.images[0].height || window.doneAlready ){
		return; //in case images are disabled
	}
	var oH = getRefToDivMod( 'myID' );
	if( !oH ) {
		return false;
	}
	var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
	var oH = oH.clip ? oH.clip.height : oH.offsetHeight;
	if( !oH ){
		return false;
	}
	if( !oH || window.doneAlready ){
		return; //in case images are disabled
	}
	window.doneAlready = true; //for Safari and Opera
	if(document.getElementsByTagName) {
		for( var l = document.getElementsByTagName('a'), x = 0; l[x]; x++ ) {
			if(l[x].className=='makeright'&&!l[x].style.position){
				l[x].style.position='relative';
				l[x].style.left=(document.images[0].width-(l[x].offsetWidth+l[x].offsetLeft))+'px';
			}
		}
	}
	var x = window; x.resizeTo( oW + 200, oH + 200 );
	var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
	if( x.innerWidth ){
		myW = x.innerWidth; myH = x.innerHeight;
	}else if( d && d.clientWidth ){
		myW = d.clientWidth; myH = d.clientHeight;
	}else if( b && b.clientWidth ){
		myW = b.clientWidth; myH = b.clientHeight;
	}
	if( window.opera && !document.childNodes ){
		myW += 16;
	}
	x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );
	var scW = screen.availWidth ? screen.availWidth : screen.width;
	var scH = screen.availHeight ? screen.availHeight : screen.height;
	if( !window.opera ){
		x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2));
	}
}

//vérifie et bloque la longueur d'un texte
function CheckLen(Target, maxLength){
	if (Target.value.length > maxLength) {
		Target.value = Target.value.substring(0,maxLength);
		CharsLeft = 0;
	}
	else {
		CharsLeft = maxLength - Target.value.length;
	}
}

//vérifie que le numero de cb fait au moins 14 caractères et la présence du cryptogramme
function verif_lg_nb_carte() 
{
	if ((document.forms["reservation"].numero_carte.value.length >= 14)){
		if (document.forms["reservation"].cryptogramme.value == ""){
			alert( "Please indicate the cryptogram") ;
		}else{
			document.forms["reservation"].submit();
		}
	}else{
		alert( "Your credit card number is incomplete" ) ;
	}
}


//vérifie la validité d'un email
function verifMail ( email, mode ) 
{ 
	if ( ( email.indexOf("@") == -1 ) || ( email.indexOf("@") == 0 ) || ( email.indexOf("@") != email.lastIndexOf("@") ) || ( email.indexOf(".") == email.indexOf("@")-1 ) || ( email.indexOf(".") == email.indexOf("@") +1 ) || ( email.indexOf("@") == email.length -1 ) || ( email.indexOf (".") == -1 ) || ( email.lastIndexOf (".") == email.length -1 ) ) {
		alert( "Error !\nYour email shoud be in xxx@xxx.xxx format" ) ; 
	}else{
		if((document.forms["reservation"].email.value) != (document.forms["reservation"].email_conf.value)){
			alert( "Error !\nThe email confirmation doesn't match the provided email" ); 
		}else{
			if(mode == "tarifs"){
				document.forms["reservation"].submit();
			}else{
				verif_lg_nb_carte();
			}
		}
	}
}


//vérifie la date d'expiration
function verifDateExpire(mois_courant, annee_courant) 
{
	if ((document.forms["reservation"].annee_valid.value > annee_courant) || ((document.forms["reservation"].mois_valid.value >= mois_courant) && (document.forms["reservation"].annee_valid.value = annee_courant)) || (document.forms["reservation"].numero_carte.value == ""))
		verifMail(document.forms["reservation"].email.value, document.forms["reservation"].mode.value);
	else
		alert( "Please enter a valid expiration date for your credit card" ) ; 
}

function ValidationFormModif()
{
	if ((document.forms["modification"].num_resa.value == "") || (document.forms["modification"].email_client.value == "") || (document.forms["modification"].nom_hotel.value == "") || (document.forms["modification"].nom_client.value == "")){
		alert("Please fill in all the fields :\n - Booking number\n - Hotel name\n - Name & First Name\n - E-mail");
	}else{
		var email = document.forms["modification"].email_client.value;
		if(document.forms["modification"].demande[0].checked || document.forms["modification"].demande[1].checked){
			if ( ( email.indexOf("@") == -1 ) || ( email.indexOf("@") == 0 ) || ( email.indexOf("@") != email.lastIndexOf("@") ) || ( email.indexOf(".") == email.indexOf("@")-1 ) || ( email.indexOf(".") == email.indexOf("@") +1 ) || ( email.indexOf("@") == email.length -1 ) || ( email.indexOf (".") == -1 ) || ( email.lastIndexOf (".") == email.length -1 ) ){
				alert( "Error !\nYour email should be in xxx@xxx.xxx format" );
			}else{ 
				document.forms["modification"].submit();
			}
		}else{
			alert("Please mention your request (cancellation or modification)");
		}
	}
}

function display_boite(boite){
	var browser = navigator.appName;

	if(browser == "Netscape"){ 
		displayString = 'table-row'; 
	}else{ 
		displayString = 'inline'; 
	}
	if(document.getElementById(boite).style.display == 'none'){
		document.getElementById(boite).style.display = displayString;
	}else{
		document.getElementById(boite).style.display = 'none';
	}
}

//script pour afficher une grande image au survol de la souris
var w=1
var h=1

if (document.getElementById || document.all)
document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;"><img id="ttimg" src="http://www.cybevasion.fr/lib2/trans.gif" class="bordure5" /></div>')

function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	document.onmousemove=""
	gettrailobj().visibility="hidden"
	gettrailobj().left=-1000
	gettrailobj().top=0
}


function showtrail(file, height, width)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{
		w=width
		h=height
		
		// followmouse()
	
		gettrailobj().visibility="visible"
		gettrailobj().zIndex = 5;
		document.getElementById('ttimg').src=file
		document.onmousemove=followmouse
	}
}


function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{

		var xcoord=20
		var ycoord=20

		if (typeof e != "undefined")
		{
			xcoord+=e.pageX
			ycoord+=e.pageY
		}
		else if (typeof window.event !="undefined")
		{
			xcoord+=truebody().scrollLeft+event.clientX
			ycoord+=truebody().scrollTop+event.clientY
		}

		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

		if (xcoord+w+10>docwidth)
		xcoord=xcoord-w-40

		if (ycoord-truebody().scrollTop+h>truebody().clientHeight)
		ycoord=ycoord-h-20;

		gettrailobj().left=xcoord+"px"
		gettrailobj().top=ycoord+"px"

	}

}

function Affiche_OBJ(){
	var Arg0 = document.getElementById('reg_sel').options[document.getElementById('reg_sel').selectedIndex].value;
	var Arg=Arg0.split(',');

	if(Arg0=='-1'){
		for(b=1; b<=105; b++){
			document.getElementById('deptbox' + b).checked = true;
		}
		hide_nocheck(-1);
	}else{
		var b=1;
		for(b=1; b<=105; b++){
			document.getElementById('deptbox' + b).checked = false;
		}
	
		var Obj;
		for(var i=0; i<Arg.length; i++){
			Obj = document.getElementById(Arg[i]);
			Obj2 = document.getElementById(Arg[i].replace(/dept/,"deptbox"));
			if(Obj){
				if(Obj2.checked == false){
					Obj2.checked = true;
				}else{
					Obj2.checked = false;
				}
			}
		}
		hide_nocheck(0);
	}
}

function hide_nocheck(mon_arg){
	dml=document.forms["search_ann"];
	len = dml.elements.length;
	var i=0;
	var z=0;
	for(i=0; i<len; i++) {
		var my_new_box = document.getElementById(dml.elements[i].id.replace(/deptbox/,"dept"));
		if (dml.elements[i].id.substring(0,7) == 'deptbox'){
			if (dml.elements[i].checked==false){
				if(mon_arg==0){
					my_new_box.style.display='none';
				}
				z++;
			}else{
				my_new_box.style.display='inline';
				if(mon_arg==0){
					document.getElementById(dml.elements[i].id).checked = false;
				}
			}
		}
	}
	
	if(z==105){
		var i=0;
		for(i=0; i<len; i++) {
			var my_new_box = document.getElementById(dml.elements[i].id.replace(/deptbox/,"dept"));
			if(my_new_box!=null && my_new_box.id!='chdh_select' && my_new_box.id!='gites_select'){
				my_new_box.style.display='inline';
			}
		}
	}
}

function verif_form_adv(){
	dml=document.forms["search_ann"];
	len = dml.elements.length;
	
	var z=0;
	var i=0;
	for(i=0; i<len; i++) {
		if (dml.elements[i].id.substring(0,7) == 'deptbox'){
			if(dml.elements[i].checked==true){
				z++;
			}
		}
	}

	if(document.getElementById('mots_cles_ann').value!=""){
		z++;
	}
	
	if(z==0){
		alert('Please type a destination or select at least one department');
	}else{
		document.forms["search_ann"].submit();
	}
}

function valid_form_dispos(){
	if(document.forms["recherche_dispos"].jour_arr.value!="" && document.forms["recherche_dispos"].annee_mois_arr.value!=""){
		if(document.forms["recherche_dispos"].nb_nuits.value=="" || document.forms["recherche_dispos"].nb_chambres.value=="" || document.forms["recherche_dispos"].nb_personnes.value==""){
			alert( "Please fill all fields to know the availabilities !");
		}else{
			document.forms["recherche_dispos"].submit();
		}
	}else{
		document.forms["recherche_dispos"].submit();
	}
}

function change_pic(source, titre, taille, dest){
	if(!dest){
		dest = '';
	}
	document.getElementById('photo_big' + dest).src = source;
	if(taille!=0){
		document.getElementById('photo_big' + dest).width = taille;
	}
	document.getElementById('photo_big' + dest).title = titre;

	if(titre!=''){
		document.getElementById('photo_lgd' + dest).innerHTML = titre;
	}
	document.getElementById('link_photo' + dest).href = source;
}