$.ajaxSetup({
cache: false
});

function lireCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
function setCook2(nom,valeur,jours) {
        var expDate = new Date()
        expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000))
        document.cookie = nom + "=" + escape(valeur)
            + ";expires=" + expDate.toGMTString()
        }
function delCook(nom) { setCook2(nom,"",-1) }	
		
function connexion(type){
$('#loadingspan').html('<img src="img/ajax-loader.gif">');
$.ajax({	
			async: false,
			type: "POST",
			url: "php/login/login.php", 
			data: "action=login&login="+$('#courrielCon').val()+"&pass="+$('#motDePasseCon').val()+"&type="+type,
				success: function(msg){
				//alert(msg);
					if(msg == 0){
					$('#perreur').html('<b>La combinaison usager et mot de passe est invalide</b>');
					$('#loadingspan').html('');
					}else if(msg == 1){
					$('#perreur').html('<b>Veuillez définir le type de connexion :<br> <a href="javascript:connexion(\'usager\');">usager</a> | <a href="javascript:connexion(\'employeur\');">employeur</a></b>');
					$('#loadingspan').html('');
					}else if( msg == 2){
					$('#perreur').html('<b>Votre compte n\'est pas activé</b>');
					$('#loadingspan').html('');
					}else{
						if(window.location.pathname.indexOf('emplois-fiche') != -1){
						location.reload();
						}else{					
						$('#inscription').html(msg);
						$('#loadingspan').html('');	
						fermerfenetreconnexion();
						}
					}
				
				
				}
			});
}
function connexionprobleme(type){
$.ajax({	
			async: false,
			type: "POST",
			url: "php/login/login.php", 
			data: "action=problemeconnexion&login="+$('#courrielprobleme').val()+"&pass=void&capinput="+$('#capinput').val()+"&type="+type,
								success: function(msg){
					if(msg == 0){
					$('#perreurprobleme').html('<b>Votre compte semble inexistant. Si vous possédez un compte, veuillez contacter <a href="mailto:info@placeauxjeunes.qc.ca">Place aux jeunes en région</a>.</b>');
					$('#loadingspanprobleme').html('');
					}else if(msg == 1){
					$('#perreurprobleme').html('<b>Veuillez définir le type de connexion : <a href="javascript:connexionprobleme(\'usager\');">usager</a> | <a href="javascript:connexionprobleme(\'employeur\');">employeur</a></b>');
					$('#loadingspanprobleme').html('');
					}else if( msg == 2){
					$('#perreurprobleme').html('<b>Votre compte n\'est pas activé</b>');
					$('#loadingspanprobleme').html('');
					}else if( msg == 3){
					$('#perreurprobleme').html('<b>Un courriel de réinitialisation vous a été envoyé!</b>');
					$('#loadingspanprobleme').html('');
					}else if( msg == 4){
					$('#perreurprobleme').html('<b>Le code de sécurité est invalide</b>');
					$('#loadingspanprobleme').html('');
					}else{
					alert(msg);
					}
	
				
				}
			});
}
function page(page,region)
{
	$.ajax({	
			async: true,
			type: "POST",
			url: "php/guestbook/list.php", 
			data: "page="+page+"&region="+region,
				success: function(msg){
				$('#zone_commentaire').html(msg);
				}
			});

} 

function gbook(page,region)
{
		var hash = "!page>"+page+"-"+region;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
}
$("a.remote").click(function(){
									var hash = $(this).attr('href');
									hash = hash.replace(/^.*#/, '');
									hash = hash.split(':')
									// moves to a new page. 
									// pageload is called at once. 
									// hash don't contain "#", "?"
									$.historyLoad(hash[1]);
									return false;
									});

//$('#_codeMrc_Emplois').change(actualites());
$(document).ready( function () {

$('#stateSelect').change(function(){
if($('#stateSelect :selected').val() == "QC"){
$(".spanMrcOrigine").removeClass('hidden');
}else{ 
$(".spanMrcOrigine").addClass('hidden');
}
});

$('.selectMrcRegion').change(function() {
$.ajax({
		   type: "POST",
		   url:  "/php/fct/ajaxFCT.php",
		   async: false,
		   data: "action=selectMrcRegion&region="+$('.selectMrcRegion').val(),
		   success: function(msg){
				$('#contenantMRC').html(msg);
		   }
	});
});

	
				$('#actualitesans').change(function() {
				 filtre();
				});
				$('#_codeMrc_Emplois_filtre').change(function() {
				 filtre();
				});
				
				//On vérifie si la connexion est fait pour faire apparaitre le menu de connexion ou pas
$.ajax({	
			async: false,
			type: "POST",
			url: "php/login/login.php", 
			data: "action=ifconnect",
				success: function(msg){
				//alert(msg);
				if(msg != '0')
					{
					$('#inscription', window.parent.document).html(msg);
						Shadowbox.init({skipSetup: true});
					$('#inscription', window.parent.document).slideDown('slow');
						
					}
				}
			});
if(lireCookie('gestion') == '1'){
$.ajax({	
			async: false,
			type: "POST",
			url: "php/login/gestion.php", 
			data: "action=ifconnect",
				success: function(msg){
				//alert(msg);
					$("div#container").html(msg);
				}
			});
}

				
				
});




$(window).load(function(){
    var origContent = "";
			$.historyInit(pageload);	
			$("a.remote").click(function(){
									var hash = $(this).attr('href');
									hash = hash.replace(/^.*#/, '');
									hash = hash.split(':')
									// moves to a new page. 
									// pageload is called at once. 
									// hash don't contain "#", "?"
									if(!hash[1]){
									//alert('page d');
									//$.historyLoad('emplois');
									}else{
									$.historyLoad(hash[1]);
									}
									return false;
									});
							var hash = window.location.href;
							hash = hash.split('#')					
return false;
});


function pageload(hash) {
				
				//	alert(hash+'1');
hash = decodeURI(hash);

								//le nom de la page qui load le script
								var nom = window.location.pathname;
								nom = nom.split("/");
								nom = nom[nom.length - 1];
								//nom = nom.substr(0, nom.lastIndexOf("."));
								nom = nom.replace(new RegExp("(%20)", "g"), "");
								if(nom.indexOf('#')!=-1){var sentinelle = 1}
								nom = nom.replace("#", "");
								//On initialise l'historique du menu 
								//alert(hash);

		//alert(hash);
		if(hash && hash.indexOf("!") != -1) {
		var param = hash.split('>');
		var longparam = param[1].split('-');
		var string = '(';
		for(i=0;i<longparam.length;i++){
		string += '"'+longparam[i]+'",'
		}
		string+= '"");';
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() ne prend pas les caractères trop spéciaux ex: ë
				hash = encodeURIComponent(hash);
			}
			param[0] = param[0].replace('!','');
			//alert(param[0]+string)
		
			if(param[0] == 'recherche'){
			hash = "<script type='text\/javascript'>"+param[0]+string+"<\/script>";
			hash = unescape(hash);
			$("div#emploisBox").html(hash);
			$("#_codeMrc_Emplois").val(longparam[0]);
			$("#_idSecteurActivites_Emplois").val(longparam[1]);
			$("#typePoste_Emplois").val(longparam[2]);
			$("#temps_Emplois").val(longparam[3]);
			$("#keywords_Emplois").val(longparam[4]);
			$("#page").val(longparam[5]);
			}else if(param[0] == 'stages'){
			hash = "<script type='text\/javascript'>"+param[0]+string+"<\/script>";
			hash = unescape(hash);
			$("div#stagesFull").html(hash);
			$("#_codeMrc_Emplois").val(longparam[0]);
			$("#_idSecteurActivites_Emplois").val(longparam[1]);
			$("#typePoste_Emplois").val(longparam[2]);
			$("#temps_Emplois").val(longparam[3]);
			$("#keywords_Emplois").val(longparam[4]);
			$("#page").val(longparam[5]);
			}else if(param[0] == 'postes'){
			hash = "<script type='text\/javascript'>"+param[0]+string+"<\/script>";
			hash = unescape(hash);
			$("div#emploisFull").html(hash);
			$("#_codeMrc_Emplois").val(longparam[0]);
			$("#_idSecteurActivites_Emplois").val(longparam[1]);
			$("#typePoste_Emplois").val(longparam[2]);
			$("#temps_Emplois").val(longparam[3]);
			$("#keywords_Emplois").val(longparam[4]);
			$("#page").val(longparam[5]);
			}else if(param[0] == 'postes'){
			hash = "<script type='text\/javascript'>"+param[0]+string+"<\/script>";
			hash = unescape(hash);
			$("div#emploisFull").html(hash);
			$("#_codeMrc_Emplois").val(longparam[0]);
			$("#_idSecteurActivites_Emplois").val(longparam[1]);
			$("#typePoste_Emplois").val(longparam[2]);
			$("#temps_Emplois").val(longparam[3]);
			$("#keywords_Emplois").val(longparam[4]);
			$("#page").val(longparam[5]);
			}else{
			//alert(param[0]+string);
			hash = "<script type='text\/javascript'>"+param[0]+string+"<\/script>";
			$("#zone_commentaire").html(hash);
		


			}

		}
		else{

var origContent = $('#contenu').html();

			// start page
			nom = nom+'#'+hash;
			window.location.replace(nom);
		}
	} 
	
function logout(){
$.ajax({	
			async: true,
			type: "POST",
			url: "php/login/login.php", 
			data: "action=logout",
				success: function(msg){
				if(window.location.href.indexOf('compte') == -1){
				window.location.reload(true);
				}else{
				window.location.href = 'index';
				}
				}
			});
}
/*
///////////////////////
Recherche d'emplois
///////////////////////
*/
	function postesinit()
{		
$("#keywords_Emplois").val(no_accent($("#keywords_Emplois").val()));

		var hash = "!postes>"+$("#_codeMrc_Emplois option:selected").val()+"-"+$("#_idSecteurActivites_Emplois option:selected").val()+"-"+$("#typePoste_Emplois option:selected").val()+"-"+$("#temps_Emplois option:selected").val()+"-"+encodeURIComponent($("#keywords_Emplois").val())+'-'+$("#page").val();
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }	
	function stagesinit()
{
$("#keywords_Emplois").val(no_accent($("#keywords_Emplois").val()));

		var hash = "!stages>"+$("#_codeMrc_Emplois option:selected").val()+"-"+$("#_idSecteurActivites_Emplois option:selected").val()+"-"+$("#typePoste_Emplois option:selected").val()+"-"+$("#temps_Emplois option:selected").val()+"-"+encodeURIComponent($("#keywords_Emplois").val())+'-'+$("#page").val();
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
}	


	function rechercheinit()
{
$("#keywords_Emplois").val(no_accent($("#keywords_Emplois").val()));
		var hash = "!recherche>"+$("#_codeMrc_Emplois option:selected").val()+"-"+$("#_idSecteurActivites_Emplois option:selected").val()+"-"+$("#typePoste_Emplois option:selected").val()+"-"+$("#temps_Emplois option:selected").val()+"-"+encodeURIComponent($("#keywords_Emplois").val())+'-1';//+$("#page").val();
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
}
 
 	function postespage(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page)
{		

		var hash = "!postes>"+_codeMrc_Emplois+"-"+_idSecteurActivites_Emplois+"-"+typePoste_Emplois+"-"+temps_Emplois+"-"+encodeURIComponent(keywords_Emplois)+'-'+page;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
}	
	function stagespage(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page)
{
		var hash = "!stages>"+_codeMrc_Emplois+"-"+_idSecteurActivites_Emplois+"-"+typePoste_Emplois+"-"+temps_Emplois+"-"+encodeURIComponent(keywords_Emplois)+'-'+page;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
}	
	
	function recherchepage(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page)
{
		var hash = "!recherche>"+_codeMrc_Emplois+"-"+_idSecteurActivites_Emplois+"-"+typePoste_Emplois+"-"+temps_Emplois+"-"+encodeURIComponent(keywords_Emplois)+'-'+page;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }
 
	function recherche(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page,nothing)
{

		$.ajax({	
			async: true,
			type: "POST",
			url: "php/emplois/emplois.php?a=recherche",
			data: "_codeMrc_Emplois="+_codeMrc_Emplois+"&_idSecteurActivites_Emplois="+_idSecteurActivites_Emplois+"&typePoste_Emplois="+typePoste_Emplois+"&temps_Emplois="+temps_Emplois+"&keywords_Emplois="+encodeURIComponent(keywords_Emplois)+"&page="+page,
				success: function(msg){
					//alert(msg);
						 $('#emploisBox').slideUp('fast', function() {
						 $('#emploisBox').html(msg);
						 $("#emploisBox").slideDown("fast");
						 });

				}
			});
}

function postes(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page,nothing){
	$.ajax({	
			async: true,
			type: "POST",
			url: "php/emplois/emplois.php?a=postes",
			data: "_codeMrc_Emplois="+_codeMrc_Emplois+"&_idSecteurActivites_Emplois="+_idSecteurActivites_Emplois+"&typePoste_Emplois="+typePoste_Emplois+"&temps_Emplois="+temps_Emplois+"&keywords_Emplois="+keywords_Emplois+"&page="+page,
  		    success: function(msg){
			//alert(msg);
			$('#emploisFull').slideUp('fast', function() {
			$('#emploisFull').html(msg);
			});
			$("#emploisFull").slideDown("fast");
			}
			});
}
function stages(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page,nothing){
		$.ajax({	
			async: true,
			type: "POST",
			url: "php/emplois/emplois.php?a=stages",
			data: "_codeMrc_Emplois="+_codeMrc_Emplois+"&_idSecteurActivites_Emplois="+_idSecteurActivites_Emplois+"&typePoste_Emplois="+typePoste_Emplois+"&temps_Emplois="+temps_Emplois+"&keywords_Emplois="+keywords_Emplois+"&page="+page,
  		    success: function(msg){
			//alert(msg);
			$('#stagesFull').slideUp('fast', function() {
			$('#stagesFull').html(msg);
			$("#stagesFull").slideDown("fast");
			});

			}
			});
}

/*
///////////////////////
Actualités
///////////////////////
*/
	function postesinit()
{		

		var hash = "!actualites>"+$("#_codeMrc_Emplois option:selected").val()+"-"+$("#_idSecteurActivites_Emplois option:selected").val()+"-"+$("#typePoste_Emplois option:selected").val()+"-"+$("#temps_Emplois option:selected").val()+"-"+encodeURIComponent($("#keywords_Emplois").val())+'-'+$("#page").val();
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }	
 	function postespage(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page)
{		

		var hash = "!actualites>"+_codeMrc_Emplois+"-"+_idSecteurActivites_Emplois+"-"+typePoste_Emplois+"-"+temps_Emplois+"-"+encodeURIComponent(keywords_Emplois)+'-'+page;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }	
 /*
function actualites(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page,nothing){
	$.ajax({	
			async: true,
			type: "POST",
			url: "php/actualites/actualites.php?a=recherche",
			data: "IdMrc="+_codeMrc_Emplois+"&mdate="+_idSecteurActivites_Emplois+"&adate="+typePoste_Emplois+"&page="+page,
  		    success: function(msg){
			//alert(msg);
			$('#emploisFull').slideUp('fast', function() {
			$('#emploisFull').html(msg);
			$("#emploisFull").slideDown("fast");
			});

			}
			});
}
*/

function filtre(){
if(!isNaN($("#_codeMrc_Emplois_filtre :selected").val())){
	
	
	
	var url = window.location.href;
	var formFiltre = '';
	if(url.indexOf('actualites') != -1)formFiltre = 'actualites';
	if(url.indexOf('evenements') != -1)formFiltre = 'evenements';
	
	var newhash='';
		newhash = url.split('#');
		newhash = newhash[0].split('/');
		newhash = newhash[newhash.length-1];
		newhash = newhash.split('-');

		if(newhash[0].indexOf('actualites') != -1 ){
			newhash[2] = 'actualites';
		}
		if(newhash[0].indexOf('evenements') != -1 ){
			newhash[2] = 'evenements';
		}
		
			if($("#_codeMrc_Emplois_filtre :selected").val().length == '3'){
				url = 'mrc-'+$("#_codeMrc_Emplois_filtre :selected").val()+'-'+newhash[2]+'-1-'+$('#actualitesans :selected').val()
			}else if($("#_codeMrc_Emplois_filtre :selected").val().length == '2'){
				url = 'region-'+$("#_codeMrc_Emplois_filtre :selected").val()+'-'+newhash[2]+'-1-'+$('#actualitesans :selected').val()
			}else{
				url = formFiltre+'-1-'+$('#actualitesans :selected').val()
			}
				window.location=url;
}

} 

function restorecap(){
var mycapbox = document.getElementById('capchatimg');
var mycapimg = document.getElementById('capchatimg-i');
if(mycapimg != null){
var myimg = mycapimg.src;

var newcap = document.createElement('img');
if(myimg.indexOf('-a') != -1){
newcap.setAttribute('src','php/guestbook/captcha-b.php?a='+Math.random());
}else{
newcap.setAttribute('src','php/guestbook/captcha-a.php?b='+Math.random());
}
newcap.setAttribute('id','capchatimg-i');
newcap.setAttribute('width','98');
newcap.setAttribute('height','33');
newcap.setAttribute('style','vertical-align:middle;');
newcap.setAttribute('id','capchatimg-i');

mycapbox.removeChild(mycapimg);
mycapbox.appendChild(newcap);
}
}
function restorecapLog(){
var mycapbox = document.getElementById('capchatimgLog');
var mycapimg = document.getElementById('capchatimgLog-i');
var myimg = mycapimg.src;

var newcap = document.createElement('img');
if(myimg.indexOf('-a') != -1){
newcap.setAttribute('src','php/guestbook/captcha-b.php?a='+Math.random());
}else{
newcap.setAttribute('src','php/guestbook/captcha-a.php?b='+Math.random());
}
newcap.setAttribute('id','capchatimgLog-i');
newcap.setAttribute('width','98');
newcap.setAttribute('height','33');
newcap.setAttribute('style','vertical-align:middle;');
newcap.setAttribute('id','capchatimgLog-i');

mycapbox.removeChild(mycapimg);
mycapbox.appendChild(newcap);
}
/*
///////////////////////
Évènements
///////////////////////
*/ 
function postesinit()
{		

		var hash = "!postes>"+$("#_codeMrc_Emplois option:selected").val()+"-"+$("#_idSecteurActivites_Emplois option:selected").val()+"-"+$("#typePoste_Emplois option:selected").val()+"-"+$("#temps_Emplois option:selected").val()+"-"+encodeURIComponent($("#keywords_Emplois").val())+'-'+$("#page").val();
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }	
 	function postespage(_codeMrc_Emplois,_idSecteurActivites_Emplois,typePoste_Emplois,temps_Emplois,keywords_Emplois,page)
{		

		var hash = "!postes>"+_codeMrc_Emplois+"-"+_idSecteurActivites_Emplois+"-"+typePoste_Emplois+"-"+temps_Emplois+"-"+encodeURIComponent(keywords_Emplois)+'-'+page;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);	
 }	

 
 
 
 
//Le enter sur le champs keywords
function onEnter(event){
if(event.keyCode==13){
rechercheinit();
return false;
}


}


   function no_accent(chaine) {
      temp = chaine.replace(/[àâä]/gi,"a")
      temp = temp.replace(/[éèêë]/gi,"e")
      temp = temp.replace(/[îï]/gi,"i")
      temp = temp.replace(/[ôö]/gi,"o")
      temp = temp.replace(/[ùûü]/gi,"u")
      temp = temp.replace(/"/gi,"'")
      return temp
   }


//Share - facebook
function fbs_click() {
u=location.href;
t=$("#titre_FB").val();
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
return false;}
