

function setFocus(p_element){
	try{
		p_element.focus();
	}catch (e){}
	document.noSubmit = false;
}

function Trim(str)
{
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

function getValueOfDimension(dimension){
	var result = parseFloat(dimension.substring(0,dimension.indexOf("px")));
	return isNaN(result) ? 0 : result;
}

function ChangeObjectDisplay(id,display){
	document.getElementById(id).style.display = display;
}

function InitFocus(formName){
	var oElement = document.forms[formName].elements;
	for (var i=0; i<oElement.length; i++)
		if (!(oElement[i].tagName=="INPUT" && oElement[i].type=="hidden"))
			if (!oElement[i].disabled){
				setFocus(oElement[i]);
				break;
			}
}

function SelectContent(e) {
	if (!e.target)
		event.srcElement.select();
	else
		e.target.select();
}

function PlaceElement(parent,oElement,left,top){	
	oElement.style.position="absolute";
	oElement.style.left=left-1 + "px";
	oElement.style.top=top-1 + "px";
	document.getElementById(parent).appendChild(oElement);
}

function FindPositionInBody(oElement){
	var position=new Array();
	position[0]=oElement.offsetLeft;
	position[1]=oElement.offsetTop;
	var oParent=oElement.offsetParent;
	while (oParent!=null && oParent.tagName.toUpperCase()!="BODY"){
		position[0]+=oParent.offsetLeft;
		position[1]+=oParent.offsetTop;
		oParent=oParent.offsetParent;
  	}
	return(position); 
}

function ChangeOneElementVisibility(oElement,newVisibility,calqueX,calqueY,calqueWidth,calqueHeight){
	if (!(!oElement||!oElement.offsetParent)){
			oElement.style.visibility = newVisibility;
   	}
}

function ChangeAllElementVisibility(id,newVisibility,calqueX,calqueY,calqueWidth,calqueHeight){
	for (var i = 0; i < document.getElementsByTagName(id).length; i++)
		ChangeOneElementVisibility(document.getElementsByTagName(id)[i],newVisibility,calqueX,calqueY,calqueWidth,calqueHeight);
}

function ChangeFamilyAllElementVisibility(family,newVisibility,calqueX,calqueY,calqueWidth,calqueHeight){
	var oElement=document.body.childNodes;
	for (var i=0; i<oElement.length; i++){
		if (oElement[i].family==family){
			ChangeOneElementVisibility(oElement[i],newVisibility,calqueX,calqueY,calqueWidth,calqueHeight);
		}
	}
}

function ChangeAllCheckboxState(id,state){
	var oObject = document.getElementById(id);
	if (oObject!=null){
		var oCheckbox = oObject.getElementsByTagName("INPUT");
		for(i = 0; i < oCheckbox.length; i++)
			if (oCheckbox[i].type=="checkbox")
				oCheckbox[i].checked = state;
	}
}

function SelectCompagnie(formName,propertyCodeGTA,propertyCompagnieNumero) {
	if((Trim(document.forms[formName].elements(propertyCodeGTA).value)!='')) {
		document.forms[formName].elements(propertyCompagnieNumero).value='TI0000000'+ document.forms[formName].elements(propertyCodeGTA).value;
		if(document.forms[formName].elements(propertyCompagnieNumero).value=='') {
			document.forms[formName].elements(propertyCompagnieNumero).value='' ;
			setFocus(document.forms[formName].elements(propertyCodeGTA));
		}
	}
}

function UpdateCompagnieGTA (formName, propertyCodeGTA, propertyCompagnieNumero) {
	if((Trim(document.forms[formName].elements(propertyCompagnieNumero).value)!='')) {
		var codeCompagnie = document.forms[formName].elements(propertyCompagnieNumero).value;
		document.forms[formName].elements(propertyCodeGTA).value = codeCompagnie.substring(9 , codeCompagnie.length); 
	}
}

function DateIsGreater(date1,date2){
	
	var date1Tab=CutDateWithSeparator(date1);
	var date2Tab=CutDateWithSeparator(date2);
	
	if (date1Tab[2]>date2Tab[2])
		return true;
	if (date1Tab[1]>date2Tab[1])
		return true;
	if (date1Tab[0]>date2Tab[0])
		return true;

	return false;
}

var tempsDernierClick = null; 
var dernierClick = null; 
var tempsEntreDeuxClics = 500; 

function verifDoubleClic() { 
    var tempsClicEnCours = (new Date()).getTime();
    if ( (dernierClick == this) && (tempsClicEnCours < tempsDernierClick + tempsEntreDeuxClics) )
      { 
      dernierClick = null; // on remet ? z?ro
      return true; // c'est bien un double-clic sur le m?me objet
      }
	else 
      { 
      dernierClick = this; 
      // pour ?tre s?r de cliquer sur le m?me objet
      tempsDernierClick = tempsClicEnCours;
      return false; // c'est un simple clic pour le moment)
    }
} 

function includeJS(p_path) {
	var script = window.document.createElement('script');
	script.src = p_path;
	script.type = 'text/javascript';
	script.defer = true;
	window.document.getElementsByTagName('head')[0].appendChild(script);
}

function formatEuro(p_euro) {
	var euro = "" + (Math.round(p_euro*100)/100);
	index = euro.indexOf(".");

	if (index < 0) { 
		euro = euro + ".00";
	} else {
		euro = euro.substring(0, index + 3);
		while (euro.length < (index + 3)) 
			euro += "0";
	}
	return euro;
}

function dispatchZonierData(p_object, p_formName, p_codePostal, p_codeInsee, p_rechercheCommune) {
	concatenation = p_object.value;
	index = getIndexOfPipe(concatenation);
	document.forms[p_formName].elements(p_codePostal).value=concatenation.substr(0,index);
	
	concatenation = getCutConcatenation(concatenation);
	index = getIndexOfPipe(concatenation);
	document.forms[p_formName].elements(p_codeInsee).value=concatenation.substr(0,index);
	
	concatenation = getCutConcatenation(concatenation);
	document.forms[p_formName].elements(p_rechercheCommune).value=concatenation;
}

function getIndexOfPipe(p_concatenation){
	return p_concatenation.indexOf("|");
}

function getCutConcatenation(p_concatenation){
	return p_concatenation.substr(index+1,p_concatenation.length);
}


// Added 20/04/2006, By Huxi LI
//function findFormElementByAttribute(attr, value){
//      var found = new Array();
//	  for (var i=0; i<document.forms.length; i++){
//       var fm = document.forms[i];
//		 for (var j = 0; j<fm.elements.length;j++){
//		     var obj = fm.elements[j];
//			 if (obj.getAttribute && obj.getAttribute(attr) == value){ found.push(obj); }
//         } 
//      }
//      return found;
//}

// Added 20/04/2006, By Huxi LI
function findFormElementByAttribute(formName, attr, value){
      var found = new Array();
      var fm = document.forms[formName];
	  for (var j = 0; j<fm.elements.length;j++){
	       var obj = fm.elements[j];
	  	   if (obj.getAttribute && obj.getAttribute(attr) == value){ found.push(obj); }
         } 
      return found;
}

function showHide(id){
  if(document.getElementById(id).style.visibility=='hidden'){
		document.getElementById(id).style.visibility='visible';
		document.getElementById(id).style.display='block';
		return "show";
   }else{
		document.getElementById(id).style.visibility='hidden';
		document.getElementById(id).style.display='none';
   		return "hide";
   }
}

/*
replace("coucou123mevoila", "123", ",") return "coucou,mevoila"
replace("coucou123mevoila", "123", "") return "coucoumevoila"
*/
function replace(p_chaine, p_oldChaine, p_newChaine) {
	if (!p_chaine || !p_oldChaine)
		return "";
	
	var debutOldChaine = p_chaine.indexOf(p_oldChaine);
	var finOldChaine = debutOldChaine+p_oldChaine.length;
	
	if (debutOldChaine==-1) 
		return "";

	var beforeOldChaine = p_chaine.substring(0, debutOldChaine);
	var afterOldChaine = p_chaine.substring(finOldChaine, p_chaine.length);
	return beforeOldChaine+p_newChaine+afterOldChaine;
}

function replaceAll(p_chaine, p_oldChaine, p_newChaine) {
	var chaine = p_chaine;
	var pos = p_chaine.indexOf(p_oldChaine);
	while(pos!=-1) {
		chaine = replace(chaine, p_oldChaine, p_newChaine);
		pos = chaine.indexOf(p_oldChaine);
	}
	return chaine;
}

// Fermer la windnet courante
function closeCurrentWindnet() {
    var oWindNet = window.parent.frameElement._ownerWindNet();
    if(oWindNet != null){
        oWindNet.execAction(event, 'Close');
    }
}

//InnerText : fonctionne avec IE ET FireFox
function getInnerText(p_text) {
	var compatible_innerText = p_text.innerText;
		if (compatible_innerText == undefined) {
			compatible_innerText = p_text.innerHTML.replace(/<[^>]+>/g,"");
		}
	return compatible_innerText;
}
function displayCssFromWindowParent(){
	// Récupère les CSS du window parent
	oCss = top.window.parent.document.getElementsByTagName("link");
	oCssParent = "";
	for (i = 0 ; i < oCss.length; i++) { 
		if (oCss[i].rel=="stylesheet"){ 
			oCssParent += "<LINK rel=\"stylesheet\" href=\""+ oCss[i].getAttribute('href') +"\" type=\"text/css\">";						
		}
	}
	window.document.write(oCssParent);
}
//Calcule l'arrondi avec precision
function roundWithPrecision(p_number,p_precision){
	p_precision=Math.pow(10,p_precision)
   	return Math.round(p_number*p_precision)/p_precision
}

//Fonction qui fait l'autotabulation aux champs suivants
function autotabulate(p_nomFormulaire,p_id,p_size,p_value){

	 if (p_value.length > p_size-1) {
	 	var oElement = document.getElementById(p_nomFormulaire+":"+p_id);
        setFocus(oElement);
    }
}

//Convert a map to an array
function convertMapToArray(result){
	var myArray=new Array();
	var tab=(result.replace(/[{}]/gi,"")).split(/, /);
	for(var i=0;i<tab.length;i++) {
		var value=tab[i].split(/=/);
		myArray[i] = new Array();
 		myArray[i]["key"] = value[0];
		myArray[i]["value"] = value[1];
	}
	return myArray;
}


//Recherche navigateur
function searchNav(p_redirection){
	var nom_browser = navigator.appName; 
	var version_browser = navigator.appVersion; 

	//Si ie 
	if(version_browser.indexOf('MSIE')!=-1){
		var version=parseFloat(version_browser.split('MSIE')[1]);
		
		//Si version 5
		if(version<6){
			document.location.href=p_redirection;
		}
	}
	
	
}

