/**
 * fonctions courants
 */

/*
 * Selects an option by value
 *
 * @name     selectOptions
 * @author   Mathias Bank (http://www.mathias-bank.de)
 * @param    value specifies, which options should be selected
 * @example  jQuery("#myselect").selectOptions("val1");
 *
 */
jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}



function getDate(value) {
	fields = value.split('/');
	return (fields.length < 3 ? null :
		new Date(parseInt(fields[2], 10), parseInt(fields[1], 10) - 1, parseInt(fields[0], 10)));
}



function formatDate(date) {
	var day = date.getDate();
	var month = date.getMonth() + 1;
	return (day < 10 ? '0' : '') + day + '/' +
		(month < 10 ? '0' : '') + month + '/' + date.getFullYear();
}

function isset(varname)
{
	if (typeof(varname) != 'undefined')
		return true;
		
	return false;
}

function dateFromString(datestring)
{
	var parts = datestring.split('-');
	var iday = parseInt(parts[2]);
	var imonth = parseInt(parts[1]) - 1;
	var iyear = parseInt(parts[0]);
	
	var theDate = new Date(iyear, imonth, iday);
	return theDate;
}

/**
  * formatte une chaine numerique flottante (xxx,xx)
  *
  * @param float money
  * @return string formatted
  */
function formatAsMoney(mnt) {

    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    var res =  (mnt == Math.floor(mnt)) ? mnt + '.00'
              : ( (mnt*10 == Math.floor(mnt*10)) ?
                       mnt + '0' : mnt);

    var sres = String(res);
    var retString = sres;

    if (sres.indexOf(".") != -1)
        retString = sres.replace(".", ",");

    return retString;
}

function getBrowser() {
	var ua = navigator.userAgent.toLowerCase();
	  if (ua.indexOf('opera')!=-1) { // Opera (check first in case of spoof)
		 return 'opera';
	  } else if (ua.indexOf('msie 7')!=-1) { // IE7
		 return 'ie7';
	  } else if (ua.indexOf('msie') !=-1) { // IE
		 return 'ie';
	  } else if (ua.indexOf('safari')!=-1) { // Safari (check before Gecko because it includes "like Gecko")
		 return 'safari';
	  } else if (ua.indexOf('gecko') != -1) { // Gecko
		 return 'gecko';
	  } else {
		 return false;
	  }
}



function doPreloadImages(loadImages)
{
	if(document.images){
		var imArray = new Array();
		var im;
		for(var i = 0; i < loadImages.length; i++){
			im = new Image(640,427);
			im.src = "http://" + location.hostname + loadImages[i];
			imArray[i] = im;
		}
	}
}


