function bonmail(mailteste)
{
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');

	if(reg.test(mailteste))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function centerPopup(width,height) {
  var bodyHeight = 0;
  var bodyWidth  = 0;
  var e = document.getElementById('popup');
  var top = 0;
  var left = 0;
  
  bodyHeight = f_clientHeight();
  bodyWidth = f_clientWidth();

  top  = ((bodyHeight - height) / 2) + f_scrollTop();           
  left = ((bodyWidth - width) / 2) + f_scrollLeft();

  topLoader  = ((bodyHeight - 19) / 2) + f_scrollTop();           
  leftLoader = ((bodyWidth -220) / 2) + f_scrollLeft();
  
  e.style.top    = (top+21)+"px";
  e.style.left   = left+"px";
  
  document.getElementById('popupHeader').style.top    = top+"px";
  document.getElementById('popupHeader').style.left   = left+"px";
  
  document.getElementById('popupLoader').style.top    = topLoader+"px";
  document.getElementById('popupLoader').style.left   = leftLoader+"px";
  
  document.getElementById('grise').style.height=f_clientHeight() + f_scrollTop()+"px";
  document.getElementById('grise').style.width=f_clientWidth() + f_scrollLeft()+"px";
}

function openPopup(url,largeur,hauteur,title) {

  centerPopup(largeur,hauteur);

  $('popup').style.width = (largeur-2)+"px";
  $('popup').style.height = hauteur+"px";
  
  $('popupHeader').style.width = (largeur-2)+"px";
  $('popupHeaderTitle').innerHTML=title;
  
  $('headerProjectDatabaseColumn1').style.display="none"
  $('headerProjectDatabaseColumn2').style.display="none"
  
  new Effect.Appear('grise', {duration: 0.5, from: 0, to: 0.8 });
  new Ajax.Updater('popup', url,
  {
	  onComplete: function(){
		  new Effect.Appear('popup', {duration: 1, from: 0, to: 1 });
		  new Effect.Appear('popupHeader', {duration: 1, from: 0, to: 1 });		  
	  }
  }
  );
  
 refreshCenterPopup = setInterval("centerPopup("+largeur+","+hauteur+")",500);
}

function refreshDatabase(url,page) {
	
/*  var pattern = new RegExp("&","g");
  url = url.replace(pattern,"&");
  prompt("url",url);*/
  dimensions = $('databaseResult').getDimensions();

  topLoader  = ((dimensions['height'] - 19) / 2);           
  leftLoader = ((dimensions['width'] -220) / 2);

  $('databaseLoader').style.top    = topLoader+"px";
  $('databaseLoader').style.left   = leftLoader+"px";

  new Effect.Appear('databaseResultGrise', {duration: 0.5, from: 0, to: 0.8 });
  new Ajax.History.Updater('databaseResult', url,
  {
	  history: {
		  cache : false,
	          onStateChange: function(state) {
			  // change title
			  theTitle = History.getTitle();
			  var pattern = new RegExp("p\.([0-9]+)","g");
			  theTitle = theTitle.replace(pattern,"p."+page);
			  History.setTitle(theTitle);
	          }
	  },
	  
	  onComplete: function(){
		  new Effect.Fade('databaseResultGrise', {duration: 0.5});
		  new Effect.ScrollTo('databaseResult');
	  }
  }
  );
}

function closePopup() {
	document.getElementById('headerProjectDatabaseColumn1').style.display="block";
	document.getElementById('headerProjectDatabaseColumn2').style.display="block";
	new Effect.Fade('grise', {duration: 0.5});
	new Effect.Fade('popup', {duration: 0.5});
	new Effect.Fade('popupHeader', {duration: 0.5});
	clearInterval(refreshCenterPopup);
}

function validateContactForm(){
	names    = $F('names');
	email    = $F('email');
	subject  = $F('subject');
	message  = $F('message');
	
	errorTxt = "";
	error = new Array();
	
	if(names=="Your names"){
		errorTxt += "Please, fill in your names<br />";
		new Effect.Highlight('names',{ startcolor: '#ff9999'});
	}
	if(email=="Your e-mail address" || !bonmail(email)){
		errorTxt += "Please, fill in a correct e-mail address<br />";
		new Effect.Highlight('email',{ startcolor: '#ff9999'});
	}
	if(subject=="Subject of your message"){
		errorTxt += "Please, precise the message's subject<br />";
		new Effect.Highlight('subject',{ startcolor: '#ff9999'});
	}
	if(message=="Message..."){
		errorTxt += "Please, write a message<br />";
		new Effect.Highlight('message',{ startcolor: '#ff9999'});
	}
	
	if(errorTxt!=""){
		$("contactError").update(errorTxt);
		if($('contactError').visible()==false){
			new Effect.Appear('contactError');
		}
		return true;
	} else {
		$("contactError").update("");
		new Effect.Appear('contactSending', {duration: 0.5, from: 0, to: 0.8 });
		
		$('contact').request({
			onFailure: function(){
				$("contactSending").update("<div style='padding:30px;font-weight:bold;text-align:center;'>An error occured while sending, please use the email address on the left with your e-mailer</div>");
				return true;
			},
			onSuccess: function(transport){
				if(transport.responseText.match(/ERREUROUSPAM/)){
					new Effect.Fade('contactSending', {duration: 0.5});
					$("contactError").update("Your message has not been sent<br />Please try again or send us your message by using our direct e-mail address written on the left side<br />");
					if($('contactError').visible()==false){
						new Effect.Appear('contactError');
					}
					return true;
				} else {
					new Effect.Fade('contactSending', {duration: 0.5});
					$("contact").update(transport.responseText);
				}
			}
		})
	}


}

function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;  
        
        // if null, then the id must be the radio group name
        if (element == null) {
                radioGroupName = idOrName;
        } else {
                radioGroupName = element.name;     
        }
        if (radioGroupName == null) {
                return null;
        }
        var radios = document.getElementsByTagName('input');
        for (var i=0; i<radios.length; i++) {
                var input = radios[ i ];    
                if (input.type == 'radio' && input.name == radioGroupName && input.checked) {                          
                        value = input.value;
                        break;
                }
        }
        return value;
}

function searchMenu(url,currentDropdown,duration,work,region,country){
	for (i=3;i<arguments.length;i++){
/*		alert(
			"("+currentDropdown+" != "+arguments[i]+") && "+
			"("+currentDropdown+" != "+country+" || "+
			region+" != "+arguments[i]+")\n"
		);*/
		if((currentDropdown != arguments[i]) || currentDropdown == region){
			new Ajax.Updater(arguments[i], url, {
				parameters: {
					dropdown:	arguments[i],
					duration:	getRadioValue(duration),
					work:		$F(work),
					region:		$F(region),
					country:	$F(country)
				},
				method: 'post',
				onCreate: function()
				{
					(currentDropdown != work)?$(work).disable():"";
					$(region).disable();
					(currentDropdown != country)?$(country).disable():"";
					$("headerProjectDatabaseSubmit").disable();
				
					(currentDropdown != work)?$(work).setOpacity(0.5):"";
					$(region).setOpacity(0.5);
					(currentDropdown != country)?$(country).setOpacity(0.5):"";
					$("headerProjectDatabaseSubmit").setOpacity(0.5);
					
					$('headerProjectDatabaseSubmit').className = 'cursorDefault submitButton';
				},
				onComplete: function()
				{
					(currentDropdown != work)?$(work).enable():"";
					$(region).enable();
					(currentDropdown != country)?$(country).enable():"";
					$("headerProjectDatabaseSubmit").enable();
						
					(currentDropdown != work)?$(work).setOpacity(1):"";
					$(region).setOpacity(1);
					(currentDropdown != country)?$(country).setOpacity(1):"";
					$("headerProjectDatabaseSubmit").setOpacity(1);
					
					$('headerProjectDatabaseSubmit').className = 'cursorHand validation submitButton';
				}
			});
		}
	}
}

function moreDetails(idDetails,idButton){
	Effect.toggle(idDetails, 'blind', { duration: 2.0 });

	if($F(idButton) == "↓ more details ↓"){
		$(idButton).value = "↑ hide details ↑";
	} else {
		$(idButton).value = "↓ more details ↓";
	}
}
