//ON LOAD !

jQuery(document).ready(function(){
   /*
    $('#topMenu ul li a').mouseover( function() {
      $(this).parent().parent().parent().removeFE(false);
      $(this).FontEffect({shadow:true, shadowOffsetTop: 0, shadowOffsetLeft: 0, shadowOpacity: 1.0, shadowColor: "#2295ff", shadowBlur:1}); 
    })
    
    $('#topMenu ul').mouseout( function() {
      $(this).children('li a').removeFE(false); 
    })
    */

    $('#cat_all').hide();
    
    
	  $('#categorie1, #categorie2, #categorie3, #categorie4, #categorie5, #categorie6, #categorie7, #categorie8, #categorie9').click(function() {
      
      // Former le nom du div qui nous intéresse grâce au numéro de catégorie (enlever les 9 premieres lettres du id)
      var num_cate = ($(this).attr('id')).substring(9);
      var nom_div = "#cat" + (num_cate);
      
      // Changer le contenu du conteneur selon ce qui a été cliqué, sauf si 1 ou 10 (Toutes Catégories ou ++)
      
      $('#cat_all').hide();
      $('#categories_content').html( $(nom_div).html() );

	  })
	  
    $('.categories_hidden_divs').hide();
    $('#categories_content').html( $('#cat1').html() );
	  
	  $('#BanniereHeader').innerfade({ speed: 'slow', timeout: 12000, type: 'random_start', containerheight: '88px' });
	  $('#BannierePubAccueil').innerfade({ speed: 'slow', timeout: 7000, type: 'random_start', containerheight: '106px' });
    
    

    $('#searchFormQuoi').click(function() {
      if ($(this).attr('value') == "Quoi? (Restaurant)") {
        $(this).attr('value', "");
      }
    })
    
    $('#searchFormOu').click(function() {
      if ($(this).attr('value') == "Où? (Montréal)") {
        $(this).attr('value', "");
      }
    })
    
    $('#searchFormOu').blur( function() {
      if ($(this).attr('value') == "") {
        $(this).attr('value', "Où? (Montréal)");
      }
    })
    
    $('#searchFormQuoi').blur( function() {
      if ($(this).attr('value') == "") {
        $(this).attr('value', "Quoi? (Restaurant)");
      }
    })
    
});

function change_catego_content($id_catego) {
  var my_div = "#cat"+$id_catego;
  $('#categories_content').html( $(my_div).html() );
}



/* FUNCTION replace attribute "rel='external'"  BY "target='_blank'" */
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}
window.onload = externalLinks;

function f_open_window( aURL, aWinName ){
	 var wOpen;
	 var sOptions;
	
	 sOptions = 'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no';
	 sOptions = sOptions + ',width=' + (screen.width/1.5);
	 sOptions = sOptions + ',height=' + (screen.height/2);
	 sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';
	
	 wOpen = window.open( '', aWinName, sOptions );
	 wOpen.location = aURL;
	 wOpen.focus();
} 

/************************Validation de formulaire**************************/

/*****************Function validation formulaire*********************/

$('#NousjoindresSaveForm input').live('blur', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});

$('#NousjoindresSaveForm select').live('change', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});

$('#NousjoindresSaveForm #ContactByTelephone, #NousjoindresSaveForm #ContactByCourriel').live('change', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});

$('#NousjoindresSaveForm textarea').live('change', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});

function validAllJQueryFormat(ids){
		/*Case pour savoir de quel type de champ il sagit*/
		switch (ids) {
 			case 'courriel':
				validCourriel(ids);
 				break;
			case 'conf_courriel' :
				/*Permet d'extraire le id du champ courriel*/
				email = ids.slice(ids.indexOf("_")+1);
				validconfirmCourriel(ids, email);
 				break;
			case 'tel' :
				validNumber(ids, 'ContactByTelephone');
				break;
			case 'subject' :
				validNotRequiredField(ids);
				break;
			case 'heard_by':
				/*Validation du select*/
				validSelect(ids);
				otherbox('Other', ids);
				break;
			case 'ContactByTelephone' :
				phonebox();
				break;
			case 'ContactByCourriel' :
				phonebox();
				break;
			case 'comment' :
				validTextArea(ids);
				break;
			case 'recaptcha_response_field' :
				validateSecurity();
				break;
			case 'Other' :
				validother(ids, 'heard_by');
				break;
			case 'NousjoindresBulletinNewsYannick' :
				/*Fait Rien*/
				break;
			case 'NousjoindresBulletinClicYannick' :
				/*Fait Rien*/
				break;
			case 'NousjoindresBulletinNewsYannickTpr' :
				/*Fait Rien*/
				break;
			default:
				validvide(ids)
				break;
 
		}
}

function validvide(objet){
	
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(document.getElementById(objet).value == ""){
			document.getElementById(a).innerHTML = "requis";
			document.getElementById(objet).style.borderColor = "#ff0000";
			ERR = 1;
	}else{
		if(!noSpace(objet) || !specialcaracter(objet)){
			document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
			document.getElementById(objet).style.borderColor = "#ff0000";
			ERR = 1;
		}else{
			document.getElementById(a).innerHTML = "";
			document.getElementById(objet).style.borderColor = "";
			ERR = 0;
		}
	}
	return ERR;
}

function validCourriel(original){
		a = document.getElementById(original).id;
		a = a+"_err";
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.getElementById(original).value)){
			document.getElementById(a).innerHTML = "";
			document.getElementById(original).style.borderColor = "";
			ERR = 0;
			
		}else{
		document.getElementById(a).innerHTML = "Courriel invalide";
		document.getElementById(original).style.borderColor = "#ff0000";
		ERR = 1;
			

		}
		if(document.getElementById(original).value.length == 0){
			document.getElementById(a).innerHTML = "requis";
			document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1;
		}
	return ERR;
}

function validconfirmCourriel(original, first){
		a = document.getElementById(original).id;
		a = a+"_err";
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.getElementById(original).value)){
			if(document.getElementById(original).value != document.getElementById(first).value && document.getElementById(first).value != ""){
				document.getElementById(a).innerHTML = "Courriel non-identique";
				document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1
			}else{
				document.getElementById(a).innerHTML = "";
				document.getElementById(original).style.borderColor = "";
				ERR = 0
			}
			
		}else{
		if(document.getElementById(original).value != ""){
			if(document.getElementById(original).value != document.getElementById(first).value){
				document.getElementById(a).innerHTML = "Courriel non-identique";

			}else{
				document.getElementById(a).innerHTML = "Courriel invalide";

			}
		}
		
			document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1

		}
		if(document.getElementById(original).value.length == 0){
			document.getElementById(a).innerHTML ="requis";
			document.getElementById(original).style.borderColor = "#ff0000";
	
			ERR = 1
		}
	return ERR;	
}

function validNumber(original, conditions){
	a = document.getElementById(original).id;
	a = a+"_err";
	if(document.getElementById(conditions).checked == true){
		if(/^[0-9\s-]*$/.test(document.getElementById(original).value)){
			if(document.getElementById(original).value.length < 12 && document.getElementById(original).value != ""){
				document.getElementById(a).innerHTML = "<br/>num&eacute;ro incomplet";
				document.getElementById(original).style.borderColor = "#ff0000";
		
				ERR = 1;
			}else{
				if(document.getElementById(original).id == "tel" && document.getElementById(original).value == ""){
					document.getElementById(a).innerHTML = "Requis";
					document.getElementById(original).style.borderColor = "#ff0000";
					ERR = 1;
				}else{
					document.getElementById(a).innerHTML = "";
					document.getElementById(original).style.borderColor = "";
					ERR = 0;
				}
			}
			
		}else{
			document.getElementById(a).innerHTML = "<br/>num&eacute;ro incorrecte";
			document.getElementById(original).style.borderColor = "#ff0000";
			
			ERR = 1;
			
		}
	}else{
		ERR = 0;
	}
	return ERR;
}

function validNotRequiredField(objet){
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(!specialcaracter3(objet)){
		document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
		document.getElementById(objet).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(objet).style.borderColor = "";
		ERR = 0;
	}
	return ERR;
}

function validTextArea(objet){
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(!specialcaracter4(objet)){
		document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
		document.getElementById(objet).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(objet).style.borderColor = "";
		ERR = 0;
	}
	return ERR;	
}



function noSpace(original){
		if(/^\s*$/.test(document.getElementById(original).value)){
			return false;
		}else{
			return true;
		}
	}
function specialcaracter(original){
	if(/^[a-zA-Z\-]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		return false;
	}
}

function specialcaracter2(original){
	if(/^[a-zA-Z\s-.]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		return false;
	}
}

function specialcaracter3(original){
	//Cette fonction valide si le champ contient seulement des accents, des symboles (!"#?$%&*) et des espaces
	//dans les champs non obligatoire
	if(/^[\w\x80-\xFF\s-!?.,]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		
		//alert(valeura)
		return false;
	}
	
}
function specialcaracter4(original){
	//Cette fonction valide si le champ contient seulement des accents, des symboles (!"#?$%&*) et des espaces
	//dans le textarea
	if(/^[\w\x80-\xFF\s-!?.,]*$/.test($('#'+original).val())){
		return true;
	}else{
		return false;
	}
}

function displayForbiddenCaracter(objet){
		valeura = "";
		for (i = 0; i < document.getElementById(objet).value.length; ++i) {
			if (valeura.length >= 1){
				valeura += " "+document.getElementById(objet).value.charAt(i).replace(/^[\w\x80-\xFF\s-!?.,]*$/, "");
			}else{
				valeura += document.getElementById(objet).value.charAt(i).replace(/^[\w\x80-\xFF\s-!?.,]*$/, "");
			}
		}
		return valeura;
}


function validSelect(original){
		a = document.getElementById(original).id;
		a = a+"_err";
	if(document.getElementById(original).value == "Choisissez"){
		document.getElementById(a).innerHTML = "Requis";
		document.getElementById(original).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(original).style.borderColor = "";
		ERR = 0;
	}
	return ERR;
}

function validother(objet, autre){

 a = document.getElementById(autre).id;
 a = a+"_err";

 if(document.getElementById(autre).value == "autre"){
 	if(document.getElementById(objet).value == ""){
 		document.getElementById(a).innerHTML = "requis";
 		document.getElementById(objet).style.borderColor = "#ff0000";
 		ERR = 1;
 	}else{
 		if(!noSpace(objet) || !specialcaracter2(objet) ){
 		document.getElementById(a).innerHTML = "Caractères non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
 		document.getElementById(objet).style.borderColor = "#ff0000";
 		ERR = 1;
 		}else{
 			document.getElementById(a).innerHTML = "";
 			document.getElementById(objet).style.borderColor = "";
 			ERR = 0;
 		}
 	}
 }else{
 	ERR = 0;
 }
 	return ERR;
} 


function validateSecurity(){
 Invalide = true;
 strSecurity = document.getElementById("recaptcha_response_field")
 if(strSecurity.value == ''){
 	strSecurity.style.border = '1px solid #dd7627';
 	//document.getElementById('strSecurityError').style.display = 'inline';
 	document.getElementById('strSecurityError').innerHTML = '(requis)';
 	Invalide = false;
 }
 if(Invalide != false){
 	ERR = 0;
 }else{
	 ERR = 1;
 }
 	return ERR;
} 




function validall(a, b, c, d, e, h, i, j, k, l){
	/*
		a = prenom
		b = Nom
		c = email
		d = confirmationemail
		e = Phone
		h = heard_by
		i = comment
		j = subject
		k = Other
		l = ContactBy
		
	*/ 
	if(validvide(a) == 1 || validvide(b) == 1 || validCourriel(c) == 1 || validconfirmCourriel(d, c) == 1 || validNumber(e, l) == 1 ||  validSelect(h) == 1 || validTextArea(i) == 1 || validNotRequiredField(j) == 1 || validateSecurity() == 1 || validother(k, h) == 1){
		
		if(validTextArea(i) == 1){
			validTextArea(i);
			document.getElementById(i).focus();
		}
		if(validother(k, h) == 1){
 			validother(k, h);
 			document.getElementById(k).focus();
 		} 
		if(validSelect(h) == 1){
			validSelect(h);
			document.getElementById(h).focus();
		}
		if(validNumber(e, l) == 1){
			validNumber(e, l);
			document.getElementById(e).focus();
		}
		if(validconfirmCourriel(d, c) == 1){
			validconfirmCourriel(d, c);
			document.getElementById(d).focus();
		}
		if(validCourriel(c) == 1){
			validCourriel(c);
			document.getElementById(c).focus();
		}
		if(validvide(b) == 1){
			validvide(b);
			document.getElementById(b).focus();
		}
		if(validvide(a) == 1){
			validvide(a);
			document.getElementById(a).focus();
		}
		if(validNotRequiredField(j) == 1){
			validNotRequiredField(j)
			document.getElementById(j).focus();
		}
		if(validateSecurity() == 1){
			validateSecurity()
		}
		
		return false;
		
	}else{
		return true;
		
	}
}

function otherbox(original, objet){
	if(document.getElementById(objet).value == "autre"){
		document.getElementById(original).style.visibility ="visible";
		document.getElementById('Other2').style.visibility ="visible";
	}else{
		document.getElementById(original).style.visibility ="hidden";
		document.getElementById('Other2').style.visibility ="hidden";
	}
}

function phonebox(){
	if(document.getElementById('ContactByTelephone').checked == true){
		document.getElementById('tel').style.visibility ="visible";
		document.getElementById('tel2').style.visibility ="visible";
	}else{
		document.getElementById('tel').style.visibility ="hidden";
		document.getElementById('tel').value ="";
		document.getElementById('tel2').style.visibility ="hidden";
		document.getElementById('tel_err').innerHTML = "";
	}
}

/********************************************************************/

