/* COOKIES */
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=/');
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

function switchContent(display, hide)
{
	$(display).removeClassName('hidden');
	$(display).addClassName('display');
	
	$(hide).removeClassName('display');
	$(hide).addClassName('hidden');
	
	return false;
}

function lightBoxProduct(id, close_link)
{

	/* icone gif pendant l'attente*/
	/* $('BookingLBContent').innerHTML = $('upload_obj_container').innerHTML;*/
			
	/*new Ajax.Updater(
			'loaded_lightbox_content',
			'ajax_product.php',
			{
				method: 'get',
				parameters: {prodid: id},
				evalScripts : true
			}
		);
	*/
	showLightBox('loaded_lightbox_'+id, 559, close_link);
	
	return false;
}


/* LIGHTBOXES */
function showLightBox(id, width, close_link)
{
	if ( width == undefined ) width = 500;	
	
	hideAllLightBoxes(true);
	
	var lb = $(id);
	
	if ( lb )
	{
		if ( close_link != undefined )
		{
			var cl = Builder.node('A', {className: 'lightbox_close_link'}, close_link);
			Event.observe(cl, 'click', function(e){ hideLightBox(id); Element.remove(Event.element(e)); });	
			Element.insert(lb, cl);		
		}
		$('lightbox_overlay').hide().observe('click', (function() { hideLightBox(id); }));
		lb.hide(); //.observe('click', (function(event) { if (event.element().id == id) hideLightBox(id); }));
		// Visibility: hidden et non pas display none pour l'upload SWFUpload
		$$('select', 'object', 'embed', 'iframe').each(function(node){ if( !Element.descendantOf(node, id) ) node.style.visibility = 'hidden' });
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		$('lightbox_overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		new Effect.Appear('lightbox_overlay', { duration: 0.25, from: 0.0, to: 0.4 });
		
		// calculate top and left offset for the lightbox 
		var arrayPageScroll = document.viewport.getScrollOffsets();
		var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 7);
		var lightboxLeft = arrayPageScroll[0] + ((document.viewport.getWidth()-width)/2);
		lb.setStyle({ width: width + 'px', top: lightboxTop + 'px', left: lightboxLeft + 'px' });
		new Effect.Appear(lb, { duration: 0.25, from: 0.0, to: 1 });
	}
	return false;
}
function hideLightBox(id, ignore_overlay)
{
	$(id).hide();
	if ( ignore_overlay == undefined ) $('lightbox_overlay').hide();
	$$('select', 'object', 'embed', 'iframe').each(function(node){ if( !Element.descendantOf(node, id) ) node.style.visibility = 'visible' });
}
function hideAllLightBoxes(ignore_overlay)
{
	var boxes = document.getElementsByClassName('lighbox');
	for(var i = 0;i<boxes.length;i++) hideLightBox(boxes[i], ignore_overlay);
}
function getPageSize()
{
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function showLoadedLightBox(url, width, close_link, hash)
{
	updateLoadedLightBox(url, hash);
	return showLightBox('loaded_lightbox', width, close_link);
}
function updateLoadedLightBox(url, close_link, hash)
{
	$('loaded_lightbox_content').update(Builder.node('div', {className:'loading_anim', style:'height:28px;'}));
	new Ajax.Updater('loaded_lightbox_content', url, { parameters: hash, evalScripts: true});
}
function showContentLightBox(content, width, close_link, hash)
{
	$('loaded_lightbox_content').update(content);
	return showLightBox('loaded_lightbox', width, close_link);
}
function showPictureLightBox(url, width, close_link)
{
	$('loaded_lightbox_content').update(Builder.node('IMG', {src: url}));
	return showLightBox('loaded_lightbox', width, close_link);
}

/*** OLDIES ***/
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function Increment(obj) {
  if (isNaN(parseInt(obj.value)))
    obj.value = 1;
  else
    obj.value = parseInt(obj.value)+1;
}

function Decrement(obj) {
  if (isNaN(parseInt(obj.value)) || parseInt(obj.value)-1 < 0)
    obj.value = 0;
  else
    obj.value = parseInt(obj.value)-1;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// fonction de validation de formulaire amélioré V5.0
function MM_validateForm()
{
	//v5.0
  var i,p,q,nm,test,num,min,max,errors = '',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3)
 	{
  	test=args[i+2]; val=MM_findObj(args[i]);

    if (val)
   	{
    	affich=args[i+1];
    	nm=val.name;
    	if ((val=val.value)!="")
   		{
      	if (test.indexOf('isEmail')!=-1)
     		{
      		p=val.indexOf('@');
        	if (p<1 || p==(val.length-1)) errors+= '- '+affich+' (doit contenir une adresse e-mail valide).\n';
     		}
      	else if (test!= 'R')
     		{
        	if (isNaN(val)) errors+= '- '+affich+' doit contenir un nombre.\n';
        	if (test.indexOf('inRange') != -1)
       		{
        		p=test.indexOf(':');
          	min=test.substring(8,p); max=test.substring(p+1);
          	if (val<min || max<val) errors+= '- '+affich+' doit contenir un nombre compris entre '+min+' et '+max+'.\n';
					}
				}
			}
    else if (test.charAt(0) == 'R') errors += '- '+affich+'.\n';
    }
  }
    
	  var Compt = 0;
		var Deb = errors.indexOf('\n');
		while (Deb != -1)
		{
		  Compt++;
		  Deb = errors.indexOf('\n',++Deb);
		}
   	if (errors)
   	{
   		if (Compt>1)
   		{
   			alert('Les champs suivants sont obligatoires :\n'+errors);
   		}
   		else
   		{
   		  alert('Le champ suivant est obligatoire :\n'+errors);  		
   		}   	
   	}
  	document.MM_returnValue = (errors == '');
}

// fonction de validation de formulaire amélioré multi-langage V1.0
function MM_validateFormMultiLang()
{
  var i,p,q,nm,test,num,min,max,errors = '',args=MM_validateFormMultiLang.arguments;
    
  for (i=6; i<(args.length-2); i+=3)
 	{
  	test=args[i+2]; val=MM_findObj(args[i]);

    if (val)
   	{
    	affich=args[i+1];
    	nm=val.name;

		//Pour DEBUG
		//errors+='* val='+args[i]+' \n';
		//errors+='* nm='+nm+' \n';
		//errors+='* test='+test+' \n';
		//errors+='* affich='+affich+' \n';	

		if ((val=val.value)!="")
   		{
      	if (test.indexOf('isEmail')!=-1)
     		{
      		p=val.indexOf('@');
        	if (p<1 || p==(val.length-1)) errors+= '- '+affich+' '+args[0]+'.\n';
     		}
      	else if (test!= 'R')
     		{
        	if (isNaN(val)) errors+= '- '+affich+' '+args[1]+'.\n';
        	if (test.indexOf('inRange') != -1)
       		{
        		p=test.indexOf(':');
          	min=test.substring(8,p); max=test.substring(p+1);
          	if (val<min || max<val) errors+= '- '+affich+' '+args[2]+' '+min+' '+args[3]+'et '+max+'.\n';
					}
				}
			}
    else if (test.charAt(0) == 'R') errors += '- '+affich+'.\n';
    }
  }
    
	  var Compt = 0;
		var Deb = errors.indexOf('\n');
		while (Deb != -1)
		{
		  Compt++;
		  Deb = errors.indexOf('\n',++Deb);
		}
   	if (errors)
   	{
   		if (Compt>1)
   		{
   			alert(''+args[4]+' :\n'+errors);
   		}
   		else
   		{
   		  alert(''+args[5]+' :\n'+errors);  		
   		}   	
   	}
  	document.MM_returnValue = (errors == '');
}

function validateDisclaimer(obj)
{
	if ( obj.checked )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function switchDivDisplay(id, value)
{
	if ( div = $(id) )
	{
		if ( value )
		{
			div.style.display = "";
		}
		else
		{
			div.style.display = "none";
		}
	}	
}

function switchMultiDisplay(obj, tag, id, value)
{
	if ( o = MM_findObj(obj) )
	{
		elts = o.getElementsByTagName(tag);

		for(i=0;i<elts.length;i++)
		{
			if ( elts[i].id == id || id == '' )
			{
				if ( value )
				{
					elts[i].style.display = "";
				}
				else
				{
					elts[i].style.display = "none";
				}
			}
		}
	}								 
}

function CheckPassword(error1, error2, text_field,text_field2,length)
{ 
	var mp1 = MM_findObj(text_field).value;
	var mp2 = MM_findObj(text_field2).value;
	
	if( mp1 || mp2 )
	{
		if ( mp1 == mp2 )
		{
			if ( mp1.length < length )
			{
				alert(error1);
				document.MM_returnValue = false;
			}
		}
		else
		{
			alert(error2);
			document.MM_returnValue = false;
		}
	}
}

function CheckLengthField(error1,text_field,length)
{ 
	var field1 = MM_findObj(text_field).value;
	
	if ( field1.length < length )
	{
		alert(error1);
		document.MM_returnValue = false;
	}
}

function CheckCheckbox(error1,checkbox_field)
{
	var field1 = MM_findObj(checkbox_field).checked;
	if (!field1)
	{
		alert(error1);
		document.MM_returnValue = false;
	}
}

function validateLotSpoken(radios)
{
	for( i = 0 ; i < radios.length ; i++ )
	{
		if (radios[i].checked) return true;
	}
	return false;
}

function isSelected(id,keyvalue)
{
	element = $(id);
	if ( element.value == keyvalue )
	{
		return true;
	}
	return false;
	
}

function isChecked(id)
{
	element = $(id);
	if ( element.checked == true )
	{
		return true;
	}
	return false;
	
}

function CheckRoomNumber(FreePlaces,Nb_Adults,Nb_Childs,Nb_Babies,MsgRoom,MsgAdult1,MsgMaxAdult,MsgChilds1,ErrorAdults,ErrorChilds,ErrorBabies,NonDispo)
{ 
	var error= "";
	
	var SelectNbAdults=0;
	var SelectNbChilds=0;
	var SelectNbBabies=0;
	var ChReserved=0;

	var Rooms =  document.getElementsByClassName('Room');

	for( var i = 1 ; i <= Rooms.length ; i++ )	
	{
		var Room = Rooms[i];

		// Récupération des données du formulaire par id
		var Adults = $F('Adults'+i);
		var Childs = $F('Childs'+i);
		var Babies = $F('Babies'+i);
		
		SelectNbAdults+=parseInt(Adults);
		SelectNbChilds+=parseInt(Childs);
		SelectNbBabies+=parseInt(Babies);
		
		if ( parseInt(Adults) < 1 &&  parseInt(Childs)+parseInt(Babies) != 0)
		{
			error+= MsgRoom+i+": " + MsgAdult1 + " \n";
		}
		if (parseInt(Adults) > 2 )
		{
			error+= MsgRoom+i+": " + MsgMaxAdult + " \n";
		}
		if ( parseInt(Babies)+parseInt(Childs) > 1)
		{
			error+= MsgRoom+i+": " + MsgChilds1 + " \n";
		}
		// Chambre occupé s'il y a au moins un adulte
		if ( parseInt(Adults) >= 1 )
		{
			ChReserved++;
		}
		
	}
	

	if (SelectNbAdults != Nb_Adults)
	{
		error+= ErrorAdults + "\n";
	}
	if (SelectNbChilds != Nb_Childs)
	{
		error+= ErrorChilds + "\n";
	}

	if (SelectNbBabies != Nb_Babies)
	{
		error+= ErrorBabies + "\n";
	}
	
	if (error!="")
	{
		alert(error); 
		return false;
	}
	else
	{
		if (FreePlaces < ChReserved)
		{
			alert(NonDispo); 
		}
		return true;
	}
}


function VerifAdultsChilds(MessageError){

	if( parseInt(document.FindForm._bAdults.value) < (parseInt(document.FindForm._bChilds.value)+parseInt(document.FindForm._bBabies.value)))
	{	
		alert(MessageError);
	}
	else
	{
		document.FindForm.submit();
	}

}
	
function VerifAdultsChildsAff(MessageError){

	if( parseInt(document.FindForm._bAdults.value) < (parseInt(document.FindForm._bChilds.value)+parseInt(document.FindForm._bBabies.value)))
	{	
		alert(MessageError);
		return false
	}
	else
	{
		document.FindForm.submit();
	}

}
