
var detect = navigator.userAgent.toLowerCase();

var OS, browser, version, total, thestring;

function openRecherche(urlRechOffres, ultraseekUrl) {
	mode = document.getElementById('recherchemode').value;
	queryTerm = document.getElementById('keyword').value;
	if ((OS == 'Windows') || (OS == 'Mac' && browser != 'msie' && browser != 'safari' && browser != 'opera')) {
		queryTerm = encodeURI(queryTerm);
	}
	if (mode == '1') {
		if (queryTerm.length > 0){
			queryTerm = '&qt=+' + queryTerm;
		}
		window.open(ultraseekUrl + queryTerm, '_recherche', 'width=700,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
	} else if (mode == '2') {
		// Hotjob
		if (queryTerm.length > 0) {
			if (urlRechOffres.indexOf("?") > -1) {
				queryTerm = '&exParfullText=' + queryTerm;
			} else {
				queryTerm = '?exParfullText=' + queryTerm;
			}
		}
		newUrl = urlRechOffres + queryTerm;
		window.open(newUrl, '_leforem');
	}
}

function openRechercheCoc(ultraseekUrl) {
	dt = 'an';
    dtRadios = document.getElementsByName('dt');
    if (dtRadios != null){
	    for (i = 0; i < dtRadios.length; i++) {
	        if (dtRadios[i].checked) {
	        	dt = dtRadios[i].value;
	        }
	    }
    }
	queryTerm = document.getElementById('qt').value;
	cocId = document.getElementById('qp').value;
	minYear = document.getElementById('ayr').value;
	maxYear = document.getElementById('byr').value;
	
	if ((OS == 'Windows') || (OS == 'Mac' && browser != 'msie' && browser != 'safari' && browser != 'opera')) {
		queryTerm = encodeURI(queryTerm);
	}
	query = '&qt=+';
	if (queryTerm.length > 0) {
		query = query + queryTerm;
	}
	query = query + '&qp=' + cocId;
	query = query +  '&dt=' + dt + '&ady=1&amo=1&ayr='+ minYear +'&bdy=31&bmo=12&byr=' + maxYear;
	
	window.open(ultraseekUrl + query, '_recherche', 'width=700,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

if (!OS) {
	if (checkIt('mac')) {
		OS = "Mac";
	}
	if (checkIt('win')) {
		OS = "Windows";
	}
}

if (!browser) {
	if (checkIt('msie')) {
		browser = "msie";
	}
	if (checkIt('firefox')) {
		browser = "firefox";
	}
	if (checkIt('safari')) {
		browser = "safari";
	}
	if (checkIt('netscape')) {
		browser = "netscape";
	}
	if (checkIt('opera')) {
		browser = "opera";
	}
}

//
// Fonctions pour ouvrir des rubriques
//
function openRubriqueDefault(newUrl) {
	// Check if we are running in frames
	if (top.frames.length > 0) {
		top.location.href = newUrl;
	} else {
		// window.location.href = newUrl;
		window.open(newUrl, '_leforem');
	}
}

function openCarnet(newUrl) {
	window.open(newUrl, '_carnet', 'width=700,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

function openEspaceJeunes(newUrl) {
	window.open(newUrl, '_espjeu', 'width=850,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

function openContacts(newUrl) {
	window.open(newUrl, '_contacts', 'width=700,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

function openFaq(newUrl) {
	window.open(newUrl, '_faq', 'width=700,height=450,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');

}

function openPopup(newUrl) {
	window.open(newUrl, '_faq', 'width=600,height=500,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

function openPopupSE(newUrl) {
	window.open(newUrl, '_SE', 'width=750,height=700,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

function openEnvoyerAmi(newUrl) {
	window.open(newUrl, '', 'width=600,height=500,resizable=yes,menubar=no,toolbar=no,scrollbars=yes,dependent=no');
}

//
// These functions are not related to a Rubrique
//

function openVideo(newUrl, hauteur, largeur) {
	window.open(newUrl, "_video", "width=" + (largeur + 80) + ",height=" + (hauteur + 130) + ",top=140,left=520,resizable=no,scrollbars=yes,menubar=no");
}

function accesDirect() {
	if (document.getElementById) {
		URL = document.getElementById('accdirliste').value;
		window.location.href = URL;
	}
}

function printpage() {
	window.print();
}

function setBlocConnection() {
	var detectstringpers = 'AuthFedHandler=Pers-';
	var detectstringentr = 'AuthFedHandler=Entr-';
	var nom = '';
	var type = -1;
	var connected = false;
	var UrlModification = '#';
	var indexpers = document.cookie.indexOf(detectstringpers);
	var indexentr = document.cookie.indexOf(detectstringentr);
	if (indexpers != -1) {
		var lastindex = document.cookie.indexOf(';', indexpers);
		if (lastindex == -1) {
			lastindex = document.cookie.length;
		}
		nom = document.cookie.substring(document.cookie.indexOf('-', indexpers) + 1, lastindex);
		type = 0;
		connected = true;
		UrlModification = '/particuliers/modifier-particulier.html';
	} else if (indexentr != -1) {
		type = 1;
		connected = true;
		UrlModification = '/entreprises/modifier-coordonnees.html';
	}
	var erreurmessage = (window.location.href.indexOf("errorloginsdc=") == -1) ? "" : "Veuillez corriger votre identifiant et mot de passe.";
	var sformdeb = "<form id=\"form1\" name=\"connect\" method=\"post\" action=\"/Enr/servlet/connection.Connect\"><input name=\"action\" value=\"connect\" type=\"hidden\"/>";
	var shead = (type == 0) ? "<h3>Bonjour " + nom + "</h3>" : "<h3>Mon espace personnel</h3>";
	var content = '';
	if (connected) {
		content = "<input type=\"hidden\" value=\"true\" name=\"connected\"/>" +
				"<br/>" + "<input name=\"type\" type=\"hidden\" value=\""
				+ type + "\"/>" + "<ul><li>" + "<a href='#' onClick=\"document.connect.action.value='redirect';document.connect.submit();return false;\">Mon espace personnel</a>"
				+ "</li>" +
				// "<a href='"+UrlModification+"' >Modifier mes
				// coordonn&eacute;es</a>"+
				"<li>" + "<a href='#' onClick=\"document.connect.action.value='disconnect';document.connect.submit();return false;\">Se d&eacute;connecter</a>"
				+ "</li></ul>";
	} else {
		if (window.location.href.indexOf("errorloginsdc=") != -1) {
			alert(erreurmessage);
		}
		content = "<input type=\"hidden\" value=\"false\" name=\"connected\"/>" + "<input name=\"type\" type=\"radio\" value=\"0\" class=\"radio\" id=\"particulier\"/>"
				+ "<label class=\"radioLabel\" for=\"particulier\">Particulier</label>"
				+ "<input name=\"type\" type=\"radio\" class=\"radio\" value=\"1\" id=\"entreprise\"/>"
				+ "<label class=\"radioLabel\" for=\"entreprise\">Entreprise</label>"
				+ "<div class=\"block\"/>" +
				// erreurmessage +
				"<div class=\"block\"/>" + "<label class=\"champLabel\" for=\"identifiant\">Identifiant : </label>"
				+ "<input name=\"login\" type=\"text\" class=\"champ\" id=\"identifiant\"/>"
				+ "<div class=\"block\"/>"
				+ "<label class=\"champLabel\" for=\"motDePasse\">Mot de passe : </label>"
				+ "<input name=\"password_\" type=\"password\" class=\"champ\" id=\"motDePasse\"/>"
				+ "<div class=\"block\"/>" +
				// "<br/>"+
				"<div class=\"alignementGauche\">"
				+ "<ul><li>"
				+ "<a href='#' onClick=\"var foundtype=false;for (var i=0; i < document.connect.type.length; i++){if (document.connect.type[i].checked){foundtype=true;}}if (foundtype == false){alert('Veuillez choisir entre particulier et entreprise.');return false;} else {document.connect.action.value='creation';document.connect.submit();return false;}\">Cr&eacute;er un acc&egrave;s</a>"
				+ "</li><li>"
				+
				// "<a href='#' onClick=\"alert('Si vous avez oubli&eacute;
				// votre identifiant, contactez notre Back Office par email
				// (backoffice@forem.be ) ou par t&eacute;l&eacute;phone au
				// 071/20 65 13,du lundi au jeudi, de 8h30 &agrave; 12h30 et de
				// 13h30 &agrave; 17h, le vendredi de 8h30 &agrave; 12h30 et de
				// 13h30 &agrave; 16h.');return false;\">Identifiant
				// oubli&eacute;</a>"+
				// "<a title=\"Contactez notre Back Office\" href='#'
				// onClick=\"document.location.href='/contacts/formulaire-demande_motif=1.html';return
				// false;\">Identifiant oubli&eacute;</a>"+
				"<a title=\"Contactez notre Back Office\" href='#' onClick=\"if(document.connect.type[1].checked){document.location.href='/contacts/formulaire-demande_motif=1.html';return false;} else if (document.connect.type[0].checked){document.location.href='/particuliers/identifiant-oublie.html';return false;}else { alert('Veuillez choisir entre particulier et entreprise.'); }return false;\">Identifiant oubli&eacute;</a>"
				+ "</li><li>" +
				/*
				 * "<a href='#' onClick=\"var ttt='';var foundtype=false;for
				 * (var i=0; i < document.connect.type.length; i++){if
				 * (document.connect.type[i].checked){if(document.connect.type[i].id=='entreprise'){alert('Pour
				 * modifier votre mot de passe veuillez contacter notre Back
				 * Office par email (backoffice@forem.be ) ou par
				 * t&eacute;l&eacute;phone au 071/20 65 13,du lundi au jeudi, de
				 * 8h30 &agrave; 12h30 et de 13h30 &agrave; 17h, le vendredi de
				 * 8h30 &agrave; 12h30 et de 13h30 &agrave; 16h.');return
				 * false;};foundtype=true;}}if (document.connect.login.value == '' ||
				 * foundtype==false){alert('Veuillez entrer votre identifiant et
				 * choisir entre particulier et entreprise.');return false;}
				 * else
				 * {document.connect.action.value='password';document.connect.submit();return
				 * false;}\">Mot de passe oubli&eacute;</a>"+
				 */
				// "<a title=\"Contactez notre Back Office\" href='#'
				// onClick=\"var ttt='';var foundtype=false;for (var i=0; i <
				// document.connect.type.length; i++){if
				// (document.connect.type[i].checked){if(document.connect.type[i].id=='entreprise'){document.location.href='/contacts/formulaire-demande_motif=11.html';return
				// false;};foundtype=true;}}if (document.connect.login.value ==
				// '' || foundtype==false){alert('Veuillez entrer votre
				// identifiant et choisir entre particulier et
				// entreprise.');return false;} else
				// {document.connect.action.value='password';document.connect.submit();return
				// false;}\">Mot de passe oubli&eacute;</a>"+
				"<a title=\"Contactez notre Back Office\" href='/contacts/formulaire-demande.html' onClick=\"var ttt='';var foundtype=false;for (var i=0; i < document.connect.type.length; i++){if (document.connect.type[i].checked){if(document.connect.type[i].id=='entreprise'){setMotif(this.href,11);return false;};foundtype=true;}}if (document.connect.login.value == '' || foundtype==false){alert('Veuillez entrer votre identifiant et choisir entre particulier et entreprise.');return false;} else {document.connect.action.value='password';document.connect.submit();return false;}\">Mot de passe oubli&eacute;</a>"
				+ "</li></ul>" + "</div>" + "<input name=\"nextUrl\" value=\"\" type=\"hidden\"/>"
				+ "<input name=\"connect\" type=\"submit\" class=\"boutonForm alignementDroite\" id=\"boutonLogin\" value=\"Entrer\" onclick=\"document.connect.nextUrl.value=window.location.href;var foundtype=false;for (var i=0; i < document.connect.type.length; i++){if (document.connect.type[i].checked){foundtype=true;}}if (document.connect.login.value == '' || document.connect.password_.value == '' || foundtype == false){alert('Veuillez entrer votre identifiant, votre mot de passe et choisir entre particulier et entreprise.');return false;}return true;\"/>"
				+ "<div class=\"block\"/>";
	}
	var sformend = "</form>";

	changeBlocConnectionContent(sformdeb + shead + content + sformend);

}

function setMotif(lien, motif) {
	var Link = '';
	Link = lien + '?motif=' + motif;
	document.location.href = Link;
}

function changeBlocConnectionContent(s) {
	var elem = document.getElementById('espacePerso');
	elem.innerHTML = s;
}

function setWindowName() {
	window.name = "_leforem";
	setBlocConnection();
	getMotif();
}

function getMotif() {
	var nom = new Array();
	var valeur = new Array();
	param = window.location.search.slice(1, window.location.search.length);
	first = param.split("&");
	for (i = 0; i < first.length; i++) {
		second = first[i].split("=");
		nom[i] = second[0];
		valeur[i] = second[1];
	}
	if (nom[0] == "motif") {
		var select = document.getElementById("Motif");
		var options = select.options;
		for ( var j = 0; j < options.length; ++j) {
			var option = options[j];
			if (option.value == valeur[0]) {
				option.selected = true;
				break;
			}
		}
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function URLEncode(plaintext) {
	var SAFECHARS = "0123456789" + // Numeric
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
			"abcdefghijklmnopqrstuvwxyz" +
			"-_.!~*'()"; // RFC2396 Mark characters

	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for ( var i = 0; i < plaintext.length; i++) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert("Unicode Character '"
				+ ch
				+ "' cannot be encoded using standard URL encoding.\n" +
				"(URL encoding only supports 8-bit characters.)\n" +
				"A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;

}

function URLDecode(encoded) {
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef";
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length - 2)
			&& HEXCHARS.indexOf(encoded.charAt(i + 1)) != -1
			&& HEXCHARS.indexOf(encoded.charAt(i + 2)) != -1) {
				plaintext += unescape(encoded.substr(i, 3));
				i += 3;
			} else {
				alert('Bad escape combination near ...' + encoded.substr(i));
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	} // while
	return plaintext;
}
