// Get URL variables
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// Shortcut keys
function gotoURL(event) {
  if (event) {
    if (event.keyCode==27) {document.location.href='http://www.ffme.info';} // Esc
    if (event.keyCode==9) {document.location.href='http://webmail.charlaixscalade.com';} // Tab
    if (event.keyCode==45) {document.location.href='http://www.charlaixscalade.com/docman';} // Ins
    // F1-F12 : 112-123
    if (event.keyCode==120 || event.keyCode==121) {document.location.href='http://www.charlaixscalade.com/index.html?rubrique=admin';} // F9-F10
    if (event.keyCode==122) {document.location.href='https://admin2.infomaniak.com/ftp/';} // F11
    if (event.keyCode==123) {document.location.href='http://admin.charlaixscalade.com';} // F12
  }
}
switch (getUrlVars()["rubrique"]) {
  case "decouverte": break;
  case "photographe": break;
  case "test": break;
  default: document.onkeypress=gotoURL;
}

// If the length of the element's string is 0 then display helper message
function isEmpty(elem,field){
    if(elem.value.length == 0){
	alert("Le champ '"+field+"' est vide !\nMerci de le remplir." );
	elem.focus(); // set the focus to this input
	return false;
    }
    return true;
}
// If the element's string matches the regular expression it is all numbers
function isNumeric(elem,field){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nIl ne doit contenir que des caractères numériques." );
	elem.focus();
	return false;
    }
}
// If the element's string matches the regular expression it is all letters
function isAlphabet(elem,field){
    var alphaExp = /^[a-zA-Z -]+$/;
    if(elem.value.match(alphaExp)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nIl ne doit contenir que des caractères alphabétiques." );
	elem.focus();
	return false;
    }
}
// If the element's string matches the regular expression it is numbers and letters
function isAlphanumeric(elem,field){
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nIl ne doit contenir que des caractères alpha-numériques." );
	elem.focus();
	return false;
    }
}
//
function isUrlValid(elem,field){
    var alphaExp = /^[0-9a-zA-Z _-]+$/;
    if(elem.value.match(alphaExp)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nIl ne doit contenir que des caractères alpha-numériques en plus des espaces, tirets ou soulignés." );
	elem.focus();
	return false;
    }
}
//
function isDate(elem,field){
    var dateformat = /^[0-3][0-9][/][0-1][0-9][/][0-9]+$/;
    if(elem.value.match(dateformat)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nLe format de la date est de la forme JJ/MM/AAAA." );
	elem.focus();
	return false;
    }
}
//
function isEmail(elem,field){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.match(emailExp)){
	return true;
    }else{
	alert("Le champ '"+field+"' n'est pas valide !\nMerci de vérifier votre email." );
	elem.focus();
	return false;
    }
}
//
function lengthRestriction(elem, min, max, helperMsg){
    var uInput = elem.value;
    if(uInput.length >= min && uInput.length <= max){
	return true;
    }else{
	alert(helperMsg);
	elem.focus();
	return false;
    }
}
//
function isJPEG(elem,field){
    if(elem.value.lastIndexOf(".jpg")==-1) {
	alert("La photo '"+field+"' n'est pas reconnu comme étant une photo JPEG !" );
	return true;
    }else{
	return false;
    } 
}

// Pour trombi
function show_img(id,img){document.getElementById(id).src ="./trombi/"+img+".JPG";}
function hide_img(id){document.getElementById(id).src ="";}

// Chargement
function loading(){
  document.write("<img src='/images/loading_aqua.gif'>");
}

