// JavaScript Document
function rollOvers(){
	if (!document.getElementById) return
	
	var imgOriginSrc;
	var MouseOverHappened;
	var oldtop;
	var imgTemp = new Array();
	var imgarr = document.getElementsByTagName('img');
	
	for (var i = 0; i < imgarr.length; i++) {
		//Attribut hsrc pour Rollovers
		if (imgarr[i].getAttribute('hsrc')) {
			imgTemp[i] = new Image();
			imgTemp[i].src = imgarr[i].getAttribute('hsrc');
			imgarr[i].onmouseover = function() {
				imgOriginSrc = this.getAttribute('src');			
				this.setAttribute('src',this.getAttribute('hsrc'));
				MouseOverHappened=true;
			}
			imgarr[i].onmouseout = function() {
				if(MouseOverHappened)
					this.setAttribute('src',imgOriginSrc);
			}
		}
	
		//Attribut csrc pour action de click comme boutons
		if (imgarr[i].getAttribute('csrc')) {
			imgTemp[i] = new Image();
			imgTemp[i].src = imgarr[i].getAttribute('csrc');
			imgarr[i].onmousedown = function() {
				imgOriginSrc = this.getAttribute('src');			
				this.setAttribute('src',this.getAttribute('csrc'));
				MouseOverHappened=true;
			}
			imgarr[i].onmouseup = function() {
				if(MouseOverHappened)
					this.setAttribute('src',imgOriginSrc);
			}
		}
	}
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}


function goBack() {
history.go(-1);
} 

function envoiDemande(hndl){
	if(validForm(hndl)){
		callAjax('/script/concoursadd.php',null,$('resultat'),hndl);
	}
}

function demandePreinsc(hndl){
	if(validForm(hndl)){
	nbr5x10 = $('quantite510').value ;
	nbr10x10 = $('quantite1010').value ;
	
	total5x10 = nbr5x10 * 350 ;
	total5x10 = (total5x10 * 5/100) + total5x10;
	total5x10 = (total5x10 * 7.5/100) + total5x10;

	total10x10 = nbr10x10 * 450 ;
	total10x10 = (total10x10 * 5/100) + total10x10;
	total10x10 = (total10x10 * 7.5/100) + total10x10;

	totkiosque = total5x10 + total10x10 ;
	totkiosque = formatAsMoney(totkiosque);
	
	total5x10 = formatAsMoney(total5x10);
	total10x10 = formatAsMoney(total10x10);
	
	if($('quantite510').value == 0 & $('quantite1010').value == 0){
	alert('Vous devez commander au moins 1 kiosque.');
	}else{
		
		if(nbr5x10 != "" & nbr10x10 != ""){
			
			if(confirm('Vous avez réservé ' + nbr5x10 + " Kiosques 5' x 10' pour un coût de " + total5x10 + " $ \n" +
					   'Vous avez réservé ' + nbr10x10 + " Kiosques 10' x 10' pour un coût de " + total10x10 + " $ \n" +
					   'Grand total : ' + totkiosque + ' $ \n' +
					   'Voulez vous continuer ?'
					   )){
			if(validForm(hndl)){
				callAjax('/script/pre_insc_add.php',null,$('resultat'),hndl);
			}
			}
		
		}else if(nbr5x10 != "" & nbr10x10 == ""){
			if(confirm('Vous avez réservé ' + nbr5x10 + " Kiosques 5' x 10' pour un coût de " + total5x10 + " $ \n" +
					   'Grand total : ' + totkiosque + ' $ \n' +
					   'Voulez vous continuer ?'
					   )){
			if(validForm(hndl)){
				callAjax('/script/pre_insc_add.php',null,$('resultat'),hndl);
			}
			}
		
		}else{
			
			if(confirm('Vous avez réservé ' + nbr10x10 + " Kiosques 10' x 10' pour un coût de " + total10x10 + " $ \n" +
					   'Grand total : ' + totkiosque + ' $ \n' +
					   'Voulez vous continuer ?'
					   )){
			if(validForm(hndl)){
				callAjax('/script/pre_insc_add.php',null,$('resultat'),hndl);
			}
			}
			
		}
	}
	}
}


function demandeKiosque(hndl, val){
	if(validForm(hndl)){
	repas1 = $('repasdin3').value ;
	repas2 = $('repassoup3').value ;
	repas3 = $('repasdin4').value ;
	

	totrepas = parseInt(repas1) + parseInt(repas2) + parseInt(repas3) ;
	totrepas = totrepas * 15 ; 
	totrepas = (totrepas * 5/100) + totrepas;
	totrepas = (totrepas * 7.5/100) + totrepas;

	grandtot = totrepas ;
	
	grandtot = formatAsMoney(grandtot);
	totrepas = formatAsMoney(totrepas);
	
	
	
	if(document.getElementById("nbPoste").value <= 0){
		alert('Vous devez entrer au moins un poste');
	}
	else if(document.getElementById('nbPoste').value < 1){
		alert("Vous devez entrer au moins un poste");
	}
	else{
		if(confirm('Coût total pour les repas : ' + totrepas + ' $ \n' +
				   'Grand total : ' + grandtot + ' $ \n' +
				   'Voulez vous continuer ?'
				   )){
		if(validForm(hndl)){
			killAjax();
			callAjax('/script/kiosque_add.php?nb='+val,null,$('resultat'),hndl);
		}
		}
	}
	}
}


function demandeTri(hndl){
	if(validForm(hndl)){
	nbrPoste = $('posteadditionnel').value ; 
	totalPoste = nbrPoste * 20 ; 
	total = 150 + totalPoste ; 
	
	if(confirm('Vous avez choisi ' + nbrPoste + ' postes additionels pour un total de ' + total + ' $ \n' +
			   'Voulez vous continuer ?'
			   )){
	if(validForm(hndl)){
		callAjax('/script/tri_add.php',null,$('resultat'),hndl);
	}
	}
	}
}

function demandePoste(hndl){
	if(validForm(hndl)){
		callAjax('/script/poste_add.php',null,$('resultat'),hndl);
	}
}

function demandeMailing(hndl){
	if(validForm(hndl)){
		callAjax('/script/mailing_add.php',null,$('resultat'),hndl);
	}
}



function quantite510a(hndl){
	if(validForm(hndl)){
		callAjax('/script/quantite510.php',null,$('quantite510'),hndl);
	}
}

function quantite1010a(hndl){
	if(validForm(hndl)){
		callAjax('/script/quantite1010.php',null,$('quantite1010'),hndl);
	}
}


sfHover = function() {
	var sfEls = document.getElementById("sidebar").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function afficheform(form1){
	if($(form1).style.display == "none"){
	$(form1).style.display = "block";
	}else{
	$(form1).style.display = "none";
	}
}

function addPoste(val){
	killAjax();
	val++;
	document.getElementById('nbPoste').value = val;
	ret = function(){
		if(ajaxOk()){
			document.getElementById('posteContainer' + (val - 1)).innerHTML += objAjax.responseText;
		}
	}
	callAjax('/script/addPoste.php?nb='+val,ret,null,null);
}


function demandeMarathon(hndl, val){
		if(document.getElementById("nbPoste").value <= 0){
			alert('Vous devez entrer au moins un poste');
		}else{
			if(validForm(hndl)){
				killAjax();
				callAjax('/script/marathon_add.php?nb='+val,null,$('resultat'),hndl);
			}
		}
}

function demandeEmploiQc(hndl, val){
		if(document.getElementById("nbPoste").value <= 0){
			alert('Vous devez entrer au moins un poste');
		}else{
			if(validForm(hndl)){
				killAjax();
				callAjax('/script/emploiQc_add.php?nb='+val,null,$('resultat'),hndl);
			}
		}
}

function searchEmploi(hndl){
	killAjax();
	callAjax('/script/searchEmploi.php',null,$('sresult'),hndl);
}

function getHash() {
	var hash = window.location.hash;
	return hash.substring(0);
}

var myhash='';
function handleHistory(){
	if(getHash()==''){
		window.location.hash='#start';
		myhash='#start';
	}
	if(getHash()!==myhash){
		killAjax();
		myhash=getHash();
		callAjax('/script/searchEmploi.php?hash='+myhash.substring(1),null,$('sresult'),$('formulaire'));
	}
}

