// ***********************************************************
//	programme boutik.jss
//
//	diverses fonctions javascript
//	
//	estNum(id, message, oblig) 
//		- controle le contenu d'un champ input_text et vérifie qu'il est numérique
//		- renvoi libelle si champ vide on non numerique: avec message "xxx obligatoire" ou "xxx incorrect"

// renvoi libelle si champ vide on non numerique
function estNum(id, message, oblig) {
	// si controle existe
  if (id) {
		// si vide ou =0 ET obligatoire
	  if (oblig && (id.value=="" || id.value=="0")) {
			return message+" obligatoire";
	  }
	  if (isNaN(id.value)) {
	  	return message+" incorrect";
	  }
	}
 	return "";
}

// change l'état de la boite à cocher passée en paramètre
function setCheckboxColumn(boiteacocher){
  // si la boite a cocher existe
  if (document.getElementById(boiteacocher)) {
    document.getElementById(boiteacocher).checked = (document.getElementById(boiteacocher).checked ? false : true);
  }
}
/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 * @param   string   name of checkbox object
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check, champcheckbox) {
    var elts      = document.forms[the_form].elements[champcheckbox];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function

/**
 * getElement
 */
function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e];
    }
    return document.getElementById(e);
}
/**
 * ouvreimage: affiche le picto et si click: ouvre la photo 
 * 	- par défaut la nouvelle fenetre fait 600 X 600
 * 	- possible de définir la largeur et la hauteur
 */
function ouvreimage(source) {
	ouvreimage(source,600,600);
}
function ouvreimage(source,xwidth,xheight) {
//	alert("w:"+xwidth + "/h:"+xheight);
	if(xwidth == undefined) xwidth=600;
	if(xheight == undefined) xheight=600;
	img = new Image();
	img.src = source;
//  params = 'width='+eval(img.width+40)+', height='+eval(img.height+30)+', screenX=0, screenY=0, top=200, left=200, resizable';
	params = 'width='+xwidth+', height='+xheight+', screenX=0, screenY=0, top=100, left=100, resizable';
	newwindow = window.open(source, 'Photo', params);
	newwindow.focus();
}

/*
* Fonction renvoyant la propriété 'Value' de l'item courant de l'objet combo passé en paramètre
*/
function cboValeurs(objet,sep) {
  ret="";
  objetCombo = document.getElementById(objet);
  for (i=0; i<objetCombo.length; i++) {
    if (objetCombo.options[i].selected) { ret += sep + objetCombo.options[i].value;}
  }
  if(ret!="") ret=ret.substr(sep.length);
  return ( ret );
}

/* 
* Fonction de formatage de date 
*/
function formatDate(valeur) {
	var JJ;var MM;var AAAA;
	isMatch = false;
	var tabDate
	// L'ordre de lecture des expressions est important
	var expReg1 = /(\d{2})(\d{2})(\d{4}|\d{2})/;
	var expReg2 = /(\d{2}|\d)\/(\d{2}|\d)\/(\d{4}|\d{2})/;
	// tabDate={JJMMAAAA ou JJMMAA}
	tabDate = valeur.match(expReg1);
	if ((tabDate != null) && (tabDate[0] == valeur)) {
		isMatch = true;
		JJ = tabDate[1];
		MM = tabDate[2];
		if (tabDate[3].length == 2 ) AAAA = '20' + tabDate[3] ;
		else AAAA = tabDate[3];
	}
	// tabDate={JJ/MM/AAAA ou JJ/MM/AA}
	tabDate = valeur.match(expReg2);
	if ((tabDate != null) && (tabDate[0] == valeur)) {
		isMatch = true;
		if (tabDate[1].length == 1 ) JJ = '0' + tabDate[1];
		else JJ = tabDate[1];
		if (tabDate[2].length == 1 ) MM = '0' + tabDate[2];
		else MM = tabDate[2];
		if (tabDate[3].length == 2 ) AAAA = '20' + tabDate[3] ;
		else AAAA = tabDate[3];
	}
	
	var objDate=new Date(AAAA, (MM - 1) ,JJ);
	
	if ((JJ=='32') && (MM=='13') && isMatch) {
		//return JJ + '/' + MM + '/' + AAAA;
		return objDate;
	}
	
	if ( (!isMatch) || (objDate.getDate() != JJ) || ((objDate.getMonth()+1) != MM )) {
		return null;
	}
	//return JJ + '/' + MM + '/' + AAAA;
	return objDate;
} 
