function popupImage(theImage) {
     //var myWindow=window.open('viewer.php?image=' + theImage,'windowRef','width=500','height=500','resizable=true');
     //if (!myWindow.opener) myWindow.opener = self;
     popUp("viewer.php?image="+theImage);
}

function redirect(direction){
     window.location=direction;
}


function popupOpinion(){
     day = new Date();
     id = day.getTime();
     eval("page"+id+" = window.open('opinion.php' , '"+ id +"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=728,height=190,left=200,top=150');");
}

function popupRefer(){
     day = new Date();
     id = day.getTime();
     eval("page"+id+" = window.open('refer.php' , '"+ id +"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=728,height=190,left=200,top=150');");
}

function popUp(URL) {
	day = new Date();
	id = day.getTime() + "asd";
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=680,height=550');");
}

function popUp(URL, width, height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,left=400,top=50,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');");
}

function popUp2(URL, width, height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,left=300,top=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');");
}

function getElement(id) {
     return document.getElementById ? document.getElementById(id) :
     document.all ? document.all(id) : null;
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}

}

function showdiv(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function addBookmark(title,url) {
     if (window.sidebar) {
          window.sidebar.addPanel(title, url,"");
     } else if( document.all ) {
          window.external.AddFavorite( url, title);
     } else if( window.opera && window.print ) {
          return true;
     }
}

/*
 * Credits to Simon Willison for this function
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function selectField(field_id)
{
	var el = document.getElementById(field_id);
	var current_value = el.value;
	
	if(field_id == "name")
	{
		var default_value = "Votre nom";
	}
	else if(field_id == "mail")
	{
		var default_value = "Votre courriel";
	}
	else
	{
		return false;
	}
	
	if(current_value == default_value)
	{
		el.value = "";
	}
}

function blurField(field_id)
{
	var el = document.getElementById(field_id);
	var current_value = el.value;

	if(field_id == "name")
	{
		var default_value = "Votre nom";
	}
	else if(field_id == "mail")
	{
		var default_value = "Votre courriel";
	}
	else
	{
		return false;
	}
	
	if(current_value == "")
	{
		el.value = default_value;
	}
}

function validateSubmit()
{
	var name_el = document.getElementById("name");
	var mail_el = document.getElementById("mail");
	if (validateName(name_el.value) && validateEmail(mail_el.value))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validateName(str)
{
	if(str != "Votre nom" && str != "")
	{
		return true;
	}
	alert("Entrez votre nom");
	return false;
}

function validateEmail(str)
{
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at) == -1)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.indexOf(at,(lat+1)) != -1)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.indexOf(dot,(lat+2)) == -1)
	{
		alert("Courriel invalide");
		return false;
	}

	if (str.indexOf(" ") != -1)
	{
		alert("Courriel invalide");
		return false;
	}

	return true;
}
