/*COLOR FADE*/
var isFading = false;

function makearray(n){
  this.length = n;
  for(var i = 1; i <= n; i++)
  this[i] = 0;
  return this;
}

hexa = new makearray(16);
for(var i = 0; i < 10; i++) hexa[i] = i;
hexa[10]="a"; hexa[11]="b"; hexa[12]="c";
hexa[13]="d"; hexa[14]="e"; hexa[15]="f";

function hex(i){
  if (i < 0) return "00";
  else if (i >255) return "ff";
  else return "" + hexa[Math.floor(i/16)] + hexa[i%16];
}

function animateFade(r, g, b, er, eg, eb, timer, step, elementID){
	var hr = hex(r); var hg = hex(g); var hb = hex(b);
	document.getElementById(elementID).style.backgroundColor="#"+hr+hg+hb;
	if (r < er) r+=step;
	if (g < eg) g+=step;
	if (b < eb) b+=step;
	if (r != er || g != eg || b != eb) {
		setTimeout("animateFade(" + r + "," + g + "," + b + "," + er + "," + eg + "," + eb + "," + timer + "," + step + ",'" + elementID + "')", timer);
	} else {
		isFading = false;
	}
}

function fade(r, g, b, er, eg, eb, timer, step, elementID){
	if (!isFading) {
		isFading = true;
		setTimeout("animateFade(" + r + "," + g + "," + b + "," + er + "," + eg + "," + eb + "," + timer + "," + step + ",'" + elementID + "')", timer);
	}
}
/*END COLOR FADE*/

/*COOKIES*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
/*END COOKIES*/

function htmlentities( s ){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
 
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}

function show(id) {
	var d = document.getElementById(id);
	for (var i = 0; i<=200; i++) {
		if (document.getElementById('smenu'+i)) {
			document.getElementById('smenu'+i).style.display='none';
		}
	}
	if (d) {d.style.display='block';}
}

function show_college(id) {
	var d = document.getElementById('smenu'+id);
	var dt = document.getElementById('smenudt'+id);
	
	for (var i = 0; i<=200; i++) {
		if (document.getElementById('smenu'+i)) {
			document.getElementById('smenu'+i).style.display='none';
		}
		if (document.getElementById('smenudt'+i)) {
			document.getElementById('smenudt'+i).setAttribute('class', 'non-active-menu-bg');
			document.getElementById('smenudt'+i).setAttribute('className', 'non-active-menu-bg');
		}
	}
	if (d) {
		d.style.display='block';
		dt.setAttribute('class', 'active-menu-bg');
		dt.setAttribute('className', 'active-menu-bg');
	}
}

function validatePlaces(nbPlaces) {
	if (document.getElementById("participants").value > nbPlaces || !(document.getElementById("participants").value > 0)) {
		alert('Il ne reste que ' + nbPlaces + ' places disponibles pour cette formation.');
		return false;
	}
	return true
}

function validateInscription() {
	var valid = true;
	if (document.frmInsc.company.value.length < 1) {
		alert('Vous devez inscrire le nom de votre entreprise avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.ressource.value.length < 1) {
		alert('Vous devez inscrire le nom de votre personne ressource avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.address.value.length < 1) {
		alert('Vous devez inscrire votre adresse avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.city.value.length < 1) {
		alert('Vous devez inscrire votre ville avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.postalcode.value.length < 1) {
		alert('Vous devez inscrire votre code postal avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.telephone1.value.length < 1) {
		alert('Vous devez inscrire votre num\351ro de t\351l\351phone avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.telephone2.value.length < 1) {
		alert('Vous devez inscrire votre num\351ro de t\351l\351phone avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.email.value.length < 1) {
		alert('Vous devez inscrire votre adresse courriel avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.email.value != document.frmInsc.emailVerif.value) {
		alert(htmlentities('Les informations saisies dans les champs "Courriel" ne concordent pas.'));
		valid = false;
	}
	if (document.frmInsc.signature.value.length < 1 ) {
		alert('Vous devez inscrire votre nom au complet en guise de signature.');
		valid = false;
	}
	if (document.frmInsc.accepte.checked == false) {
		alert('Vous devez accepter les conditions avant de poursuivre.');
		valid = false;
	}
	if (document.frmInsc.modePaiement != undefined) {
		var modePaiement = false;
		for (i=0;i<document.frmInsc.modePaiement.length;i++) {
			if (document.frmInsc.modePaiement[i].checked) {
				modePaiement = true;
			}
		}
		if (!modePaiement) {
			alert('Vous devez choisir un mode de paiement.');
			valid = false;
		}
	}
	return valid;
}

function validateSousTraitanceSubscription() {
	if (document.formSubscribe.nom.value.length < 1 || document.formSubscribe.nom.value == document.formSubscribe.nom.defaultValue) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Vous devez inscrire le nom de votre entreprise avant de poursuivre.</div>';
		return false;
	}
	if (document.formSubscribe.courriel.value.length < 1 || document.formSubscribe.courriel.value == document.formSubscribe.courriel.defaultValue) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Vous devez inscrire une adresse courriel avant de poursuivre.</div>';
		return false;
	}
	if (document.formSubscribe.utilisateur.value.length < 1 || document.formSubscribe.utilisateur.value == document.formSubscribe.utilisateur.defaultValue) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Vous devez inscrire un nom d\'utilisateur avant de poursuivre.</div>';
		return false;
	}
	if (document.formSubscribe.motpasse.length < 1 || document.formSubscribe.motpasse.value == document.formSubscribe.motpasse.defaultValue) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Vous devez inscrire un mot de passe avant de poursuivre.</div>';
		return false;
	}
	
	if (document.formSubscribe.motpasseconfirme.length < 1 || document.formSubscribe.motpasseconfirme.value == document.formSubscribe.motpasseconfirme.defaultValue) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Vous devez confirmer votre mot de passe avant de poursuivre.</div>';
		return false;
	}
	if (document.formSubscribe.motpasse.value != document.formSubscribe.motpasseconfirme.value) {
		document.getElementById('messageContainer').innerHTML = '<div id="nosuccess">Les deux mots de passe ne concordent pas.</div>';
		return false;
	}
	return true;
}

function sendAlert(title, alertMessage){
  html = "<html><head><title>" + title +"</title></head>" + "<body bgcolor=thistle>" + alertMessage + "</body></html>";
  args = "width=300,height=150";
  nw = window.open('',"myAlert",args);
  nw.document.write(html);
}

function switchCom(id) {
	for (var i=0; i < communique.length; i++) {
		if (communique[i][0] == id) {
			var idService = communique[i][1];
			var owner = communique[i][2];
			var author = communique[i][3];
			var date = communique[i][4];
			var place = communique[i][5];
			var title = communique[i][6];
			var teaser = communique[i][7];
			var body = communique[i][8];
			break;
		}
	}
	//document.getElementById("id").value = id;
	//document.getElementById("idService").value = idService;
	//document.getElementById("owner").value = owner;
	document.getElementById("author").value = author;
	document.getElementById("date").value = date;
	//document.getElementById("place").value = place;
	document.getElementById("title").value = title;
	document.getElementById("teaser").value = teaser;
	document.getElementById("body").value = body;
}

function validateImage() {
	var ext = document.profilEdit.logo.value;
	ext = ext.substring(ext.length-4,ext.length);
	ext = ext.toLowerCase();
	if ( (ext == ".jpg") || (ext == ".png") || (ext == ".gif") || (ext == "jpeg") || (ext == "") ) {
		return true;
	}
	alert('Le format de l\'image est incorrect.');
	return false;
}

function callService(file, functionName, callback, params) {
	// instanciate an XMLHttpRequest object
	var xmlhttp = false;
	try {
		// non IE browser
		xmlhttp = new XMLHttpRequest(); 
	} catch(e) { 
		try {
			//If the Javascript version is greater than 5.
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}  catch (e2) { 
			try {
				//If not, then use the older active x object.
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (functionName != "") {
		params = "functionName=" + functionName + "&" + params;	
	}
	
	if (params.length > 0) {
		xmlhttp.open("POST", file, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
	} else {
		xmlhttp.open("GET", file, true);
	}
	
	xmlhttp.onreadystatechange = function() { 
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				if (callback) {
					callback(xmlhttp.responseText);
				}
			} 
		}
	};
	
	if (params) {
		xmlhttp.send(params);
	} else {
		xmlhttp.send("");
	}
	
}

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	type = type.toLowerCase();
	if (type == "fullscreen"){
		strWidth = screen.availWidth;
		strHeight = screen.availHeight;
	}
	var tools="";
	if (type == "standard") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console" || type == "fullscreen") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

function doPopUp(e) {
	//set defaults - if nothing in rel attrib, these will be used
	var t = "standard";
	var w = "780";
	var h = "580";
	//look for parameters
	attribs = this.rel.split(" ");
	if (attribs[1]!=null) {t = attribs[1];}
	if (attribs[2]!=null) {w = attribs[2];}
	if (attribs[3]!=null) {h = attribs[3];}
	//call the popup script
	popUpWin(this.href,t,w,h);
	//cancel the default link action if pop-up activated
	if (window.event) {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	} else if (e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function findPopUps() {
	var popups = document.getElementsByTagName("a");
	for (i=0;i<popups.length;i++) {
		if (popups[i].rel.indexOf("popup")!=-1){
			// attach popup behaviour
			popups[i].onclick = doPopUp;
		}
	}
}

function getTaxes(subtotal, tpsRate, tvqRate) {
	var tps = Math.round(subtotal*tpsRate*100)/100;
	var tvq = Math.round(((subtotal+tps)*tvqRate)*100)/100;
	var total = subtotal + tps + tvq;
	
	return new Array(tps, tvq, total)
}