/**
* Ce fichier contient l'ensemble des fonctions et des variables partagées
* dans l'application.
*/

window.ver = navigator.appVersion;
window.app = navigator.appName;
window.isNS = Boolean(navigator.productSub)
//moz_can_do_http = (parseInt(navigator.productSub) >= 20010308)

window.isIE = (ver.indexOf("MSIE 5") != -1 || ver.indexOf("MSIE 6") != -1) ? 1 : 0;
window.isIE55 = (ver.indexOf("MSIE 5.5") != -1) ? 1 : 0;

window.isOTHER = (!isNS && !isIE) ? 1 : 0;

var ACTION_MODIFIER = "modifier" ;
var ACTION_AJOUTER = "ajouter" ;
var ACTION_SUPPRIMER = "supprimer" ; 
var ACTION_LIRE = "lire" ; 
var ACTION_VALIDER = "valider" ; 
var ACTION_LISTER = "lister" ; 
var ACTION_PUBLIER = "publier" ;
var ACTION_RECHERCHER = "rechercher" ; 


String.prototype._replace = String.prototype.replace;

/**
Méthode statique qui détermine si une variable est une chaine
de caractère vide (longueur > 0 ) ou nulle
@param object Variable à évaluer
@return <b>true</b> si la variable est nulle.
@type Boolean 
*/
String.isNull = function (oString) {
	if ( ("" + oString).length == 0 ) {
		return true ;
	}
	return false ; 
}



/**
* Détermine si la chaine de caractère en cours
* est un nombre en l'analysant après avoir supprimé les caractères
* de séparation. (milliers et décimales) déclarés dans la configuration
* @return <b>true</b> si la variable est un nombre ou si la variable est nulle
* @type Boolean 
*/
String.isNumber = function(oString) {
	var sep = " " ;
	var dec = "," ;
	var sValue = oString.toString() ; 
	sValue = sValue.replace(sep , "") ;
	sValue = sValue.replace(dec , ".") ;	
	if (!String.isNull(sValue)) {
		return !isNaN(sValue);
	}
	return true ;
}

String.removeLeadingChar = function (oString , cRemoved) {
	if (oString.length < 1) return oString ;
	var c = oString.charAt(0) ;
	while(c == cRemoved ) {
		oString = oString.substr(1) ;
		c = oString.charAt(0) ;
	}
	return oString ;
}

String.prototype.isNumber = function() {
	return String.isNumber(this) ;
}

String.prototype.removeLeadingChar = function(cRemoved) {
	return String.removeLeadingChar(this, cRemoved) ;
}

/**
* Fonction simplifée de conversion de string au format "dd/mm/yyyy" en date
*/
String.prototype.toDate = function() {
	var aValues = this.split("/");
	if (aValues.length != 3) return null ; 
	var day = parseInt(aValues[0].removeLeadingChar('0')) ;
	var month = parseInt(aValues[1].removeLeadingChar('0'))-1 ;
	var year = parseInt(aValues[2].removeLeadingChar('0')) ;
	return new Date(year,month,day) ;
}


/**
* Détermine si la chaine passée en paramètre est au format d'une adresse
* email
* @return <b>true</b> si la variable est une adresse email valide 
* @type Boolean
*/
String.isEmail = function(oString) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(oString)) ;
}

String.prototype.isEmail = function()  {
	return String.isEmail(this) ;
}

/**
* Remplace une chaine de caractères par une autre dans l'instance en cours
* @param sFind [ {@link String} ] Chaine à remplacer
* @param sReplace [ {@link String} ] Chaine de remplacement
* @return La chaine résultat
* @type String
*/
String.prototype.replace = function(sFind, sReplace) {
 	if (!sFind) {return this;}
 	if (!sReplace) {sReplace = '';}
 	sFind += '';
 	sFind = sFind._replace(/(\^|\$|\[|\]|\(|\)|\||\*|\+|\.|\?|\{|\}|\\)/gi, '\\$1');
	return (this._replace(new RegExp(sFind, 'g'), sReplace));
}

/**
* Supprime les espaces en début et en fin d'instance
* @return La chaine résultat
* @type String
*/
String.prototype.trim = function() {
 return this._replace(/(^\s*)|(\s*$)/g, '');
}

String.toPhoneNumber = function(sValue) {
	if (String.isNull(sValue)) return "" ; 
	
	sValue = sValue.replace("." , "") ;
	sValue = sValue.replace(" " , "") ;
	
	if (sValue.length != 10 ) return "" ; 
	
	if (String.isNumber(sValue)) {
		return sValue ; 
	}
	return "" ; 
}

/**
* Ajoute autant de caractères que necessaire <b>à gauche</b> de l'instance
* en cours pour atteindre la longeur spécifiée. Par défaut le caractère
* ajouté est un espace mais il peut être spécifié dans le deuxième paramètre.
* @param nLength [ {@link Number} ] <b>optional</b> Taille de la chaine à construire
* @param sValue [ {@link String} ] <b>optional</b> Chaine de caractère(s) à utilser pour la complétion
* @return La chaine résultat
* @type String
*/
String.prototype.padLeft = function() {
	if (arguments.length == 0) {return this;}
	if (arguments.length >= 1 && isNaN(arguments[0])) {return this;}
	if (arguments[0] < 0) {return this;}
 	
 	var strThis = this, intLength = parseInt(arguments[0], 10), strChar = String.fromCharCode(32);
 	if (arguments.length == 2) {strChar = '' + arguments[1];}
 	while (strThis.length < intLength) {strThis = strChar + strThis;}
 	return strThis;
}

/**
* Ajoute autant de caractères que necessaire <b>à droite</b> de l'instance
* en cours pour atteindre la longeur spécifiée. Par défaut le caractère
* ajouté est un espace mais il peut être spécifié dans le deuxième paramètre.
* @param nLength [ {@link Number} ] <b>optional</b> Taille de la chaine à construire
* @param sValue [ {@link String} ] <b>optional</b> Chaine de caractère(s) à utilser pour la complétion
* @return La chaine résultat
* @type String
*/
String.prototype.padRight = function() {
	if (arguments.length == 0) {return this;}
	if (arguments.length >= 1 && isNaN(arguments[0])) {return this;}
	if (arguments[0] < 0) {return this;}
 	
 	var strThis = this, intLength = parseInt(arguments[0], 10), strChar = String.fromCharCode(32);
 	if (arguments.length == 2) {strChar = '' + arguments[1];}
 	while (strThis.length < intLength) {strThis = strThis + strChar;}
 	return strThis;
}


/**
* Supprime les espaces de la chaine de caractère
* et complète sa longueur avec pour avoir 10 chiffres
*/
String.prototype.toPhoneNumber = function() {
	return String.toPhoneNumber(this.toString()) ;
}

/**
* Extension de l'objet Array. La méthode 
* retourne TRUE si la valeur passée en paramètre
* est trouvée dans le tableau.
*/
Array.prototype.contains = function(vValue) {
	for(var i=0 ; i<this.length;i++) {
		if (this[i]==vValue) return true ; 
	}
	return false ;
}


/**
* Extension de l'objet Array. Empile la valeur
*/
Array.prototype.push = function() {
	for (var i = 0; i < arguments.length; i++) {
		this[this.length] = arguments[i]
	}
	return this.length
}



function Html() {} ;

/**
* Retourne l'élément parent
* @param e Element HTML
* @return Element parent de celui passé en paramètre
* @type HtmlElement
*/
Html.getParent = function (e) {
		if (isIE) return e.parentElement ; 
		if (isNS) return e.parentNode ;
		return null ;
}

/**
* Récupère l'élément parent portant le nom de tag fourni
* Parcourt les éléments parent et si aucun élément ne correspond 
* s'arrète sur l'élément "BODY"
* @param e Element HTML à la racine de la recherche
* @param sTagName {@link String} nom du tag
* @return Element HTML dont le nom de tag correspond à celui fourni
* @type HtmlElement
*/
Html.getParentByTag = function(e , sTagName) {
	while( (e.tagName.toUpperCase() != sTagName.toUpperCase()) || (e.tagName.toUpperCase() == "BODY")  ) {
		e = Html.getParent(e);
	}
	return e ;
} 


/**
* cette methode sert à neutraliser le comportement d'un a Href
*/
function cancelHref() {return false ;} 
