/**
 * Some usefull tools
 *
 * @author 	Eric Jeker <eric.jeker@virtua.ch>
 * @version	0.1
 * @licence DWYW : Do Whatever You Want
**/

// Return an element by its id
function getById(id)	{
	if (document.getElementById)	{
		return document.getElementById(id) ;
	} else if (document.all)	{
		return document.all[id] ;
	} else	{
		return null ;
	}
}

function getByTag(tag)	{
	if (document.getElementsByTagName)	{
		return document.getElementsByTagName(tag) ;
	} else	{
		return null ;
	}
}

// Open a popup window
function popup(url, h, w)	{
	window.open(url, 'popup', 'alwaysRaised=yes,dependent=yes,toolbar=no,height='+h+',width='+w+',menubar=no,resizable=yes,scrollbars=yes,status=no');
}

// Return the date in the selected format
function getNow(format)	{
	/**
	 * %y = year  (2006)
	 * %m = month (02)
	 * %d = day   (26)
	 * %h = hours	  (21)
	 * %i = minutes (08)
	 * %s = seconds (12)
	**/ 
	
	if (!format)	{
		format = '%h:%i:%s' ;
	}
	
	var now = new Date();
	var y = now.getFullYear();
	var m = now.getMonth();
	var d = now.getDate();
	var h = now.getHours() ;
	var i = now.getMinutes();
	var s = now.getSeconds();
	
	if (parseInt(m) < 10) {
		m = '0'+m;
	}

	if (parseInt(d) < 10) {
		d = '0'+d;
	}

	if (parseInt(h) < 10) {
		h = '0' + h;
	}

	if (parseInt(i) < 10) {
		i = '0'+i;
	}

	if (parseInt(s) < 10) {
		s = '0'+s;
	}
	
	var result = new String(format) ;
	result = result.replace('%y', y) ;
	result = result.replace('%m', m) ;
	result = result.replace('%d', d) ;
	result = result.replace('%h', h) ;
	result = result.replace('%i', i) ;
	result = result.replace('%s', s) ;
	
	return result.toString() ;
}

function getMouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
   		return evt.clientX + (document.documentElement.scrollLeft ?
   					document.documentElement.scrollLeft :
   					document.body.scrollLeft);
	else return null;
}

function getMouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
   		return evt.clientY + (document.documentElement.scrollTop ?
   					document.documentElement.scrollTop :
   					document.body.scrollTop);
	else return null;
}


function detect_ajax()	{
	var isAjax = true ;
	
	if (window.XMLHttpRequest)	{
		http_request = new XMLHttpRequest();
		
		if (http_request)	{
			isAjax = true ;
		} else	{
			isAjax = false ;
		}
	} else if (window.ActiveXObject)	{
		try	{
			http_request = new ActiveXObject("Msxml2.XMLHTTP") ;
		} catch(e)	{
			try	{
				http_request = new ActiveXObject("Microsoft.XMLHTTP") ;
			} catch (e) {}
		}
		
		if (!http_request) { 
			isAjax = false ; 
		} else 	{
			isAjax = true ;
		}
	} else	{
		var isAjax = false ;
	}
	
	return isAjax ;
}

/**
 * fonction générique pour l'effacement
 * @param	string	URL
 * @param	string	Message
 **/
function del(url,msg)
{
	if(confirm(msg)) {
		location.href = url;
	}
}

/**
 * fonction générique pour la validation
 * @param	string	URL
 * @param	string	Message
 **/
function apply(url,msg)
{
	if(confirm(msg)) {
		location.href = url;
	}
}
