/************************************************
* MaToolBox v0.3 — Nicolas Feschotte 10/11/2010 *
* need JQUERY 1.4.3                             *
************************************************/

/* redim une window */
function resize(percentage_largeur,percentage_hauteur) {
	largeur_ecran = screen.width;
	hauteur_ecran = screen.height;
	new_largeur = (percentage_largeur * largeur_ecran) / 100;
	new_hauteur = (percentage_hauteur * hauteur_ecran) / 100;
	window.resizeTo(new_largeur, new_hauteur);
}

/* déplace une window */
function move(topx,topy) {
	window.moveTo(topx,topy);
}

/* coche ou décoche toutes les checkbox du formulaire "form" */
function selectAllChk(form){
	var frm = document.forms[form];
	var chk = eval(form+'.checked');
	var cpt = eval(form+'.length');
	if(cpt) {
		for(i=0;i<cpt;i+=1){
			eval(form+'[i].checked = chk');
		}
	} else {
		eval(form+'.checked = chk');
	}
}

/* on click tu coche / décoche une box */
function checkmabox(mabox) {
	labox = getID(mabox);
	if (labox.checked == 1) labox.checked = 0;
	else labox.checked = 1;
}

/* verifie la liste des comboxs pour savoir si elles sont déchecked… si c'est le cas on check lastbox */
function verif_box(lastbox, comboxs) {
	var anychecked = false;
	var allbox = comboxs.split(',');
	for (var i=0; i<allbox.length; i++) {
		var abox = getID(allbox[i]);
		if (abox.checked == 1) { anychecked = true; }
	}
	if(anychecked == false) {
		checkmabox(lastbox, 'check');
	}
}

/* recharger ou aller à une page */
function gotoURL(dest) {
	if (dest != null) {
	window.location.href = dest; } else { window.location.href= window.location.href; }
}
/* recharger dans une frame */
function gotoURLinFrame(dest, maframe) {
	parent.frames[maframe].window.location = dest; 
}

/* equivalent php de is_set */
function is_set(variablename){
	if(variablename.value == "") {
		return false;
	};
	return true;
}

/* prendre le focus */
function focusON(form,champ) {
	document.forms[form].elements[champ].focus(); 
}


/* blocage de la touche enter/return */
/* uncomment pour activer le blocage */
//if (document.layers) document.captureEvents(Event.KEYPRESS)

function process_keypress(e) {
	if(window.event){
		if (window.event.type == "keypress" & window.event.keyCode == 13) return !(window.event.type == "keypress" & window.event.keyCode == 13);
	}
	if(e) {
		if (e.type == "keypress" & e.keyCode == 13)	return !e;
	}
}
/*
document.onkeypress = process_keypress;
*/

/* fonction d'ajout de favoris ie/firefox */
function aj_favoris(nom,adresse) {
	// exemple
	// javascript:void(aj_favoris('Home Staging Europe','http://www.home-staging-europe.fr/'));
	
	if ( navigator.appName != 'Microsoft Internet Explorer' ) { window.sidebar.addPanel(nom,adresse,""); } else { window.external.AddFavorite(adresse,nom); }
}

// on source rollOver maBox:obj switch display & follow mouse 
function flyOver(source, maBox, mX, mY) {
	$(maBox).hide();
	$(source).mousemove(function(e) {
		modifX = modif_flyOver(e.pageX, mX, $(window).width(), $(maBox).width());
		modifY = modif_flyOver(e.pageY, mY, $(window).height(), $(maBox).height());
		$(maBox).show();
		$(maBox).css({
			left: (modifX) + "px",
			top: (modifY) + "px"
		});
	});
	
	$(source).mouseout(function(e){
		$(maBox).hide();
	});	
	
}
// move the box to coord:mouse + mod:size
// if the mouse closer to browser border, move the box to coord:mouse - box:size - mod:size
function modif_flyOver(coord, mod, wT, bT) {
	newMod = coord + mod;

	if(coord > wT-bT) newMod = coord-bT-mod;
	
	return newMod;
}

function EcrireCookie(nom, valeur) {
		var argv=EcrireCookie.arguments;
		var argc=EcrireCookie.arguments.length;
		var expires=(argc > 2) ? argv[2] : null;
		var path=(argc > 3) ? argv[3] : null;
		var domain=(argc > 4) ? argv[4] : null;
		var secure=(argc > 5) ? argv[5] : false;
		document.cookie=nom+"="+escape(valeur)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
	};
	
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
};

function LireCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	};
	return null;
};

function active_cookie(mode) {
	switch (mode) {
		default:
		case "start":
			EcrireCookie ("acanthes", "ok");	
			break;
		case "stop":
			EcrireCookie ("acanthes", "pasok");	
			break;	
	};
};
