﻿function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

var regExpBeginning = /^\s+/;
var regExpEnd       = /\s+$/;
// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
function trimespace(aString) {
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}

function CacheAffiche(div,img){
	if(document.getElementById(div).style.display =='none' || document.getElementById(div).style.display ==''){
		if(div == 'divRegion' || div == 'divDept' || div == 'divVille'){
			document.getElementById('divVille').style.display='block';
			document.getElementById('divRegion').style.display='block';
			document.getElementById('divDept').style.display='block';
		}else{
			document.getElementById(div).style.display='block';
		}
		if(img!='-1'){
			document.getElementById(img).src='../images/picto/picto_moins.gif';
		}
	}else{
		if(div == 'divRegion' || div == 'divDept' || div == 'divVille'){
			document.getElementById('divVille').style.display='none';
			document.getElementById('divRegion').style.display='none';
			document.getElementById('divDept').style.display='none';
		}else{
			document.getElementById(div).style.display='none';
		}
		if(img!='-1'){
			document.getElementById(img).src='../images/picto/picto_plus.gif';
		}
	}
	
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',nameField,args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) {
	   nameField = args[i+1]; 
	  if(nameField!=""){ 
		  nm=nameField;
	  }else{
		  nm=val.name;
	  } 
	  val= trimespace(val.value);
	  if (val!=""){
		  if (test.indexOf('isEmail')!=-1) { 
			p=val.indexOf('@');
			p2=val.lastIndexOf('.');
			if (p<1 || p2 < 1 || p2 <p || p2 == (p+1) || (p2+3) > (val.length) || p==(val.length-1)) errors+='- '+nm+' doit être une adresse mail valide.\n';
		  }else if(test.indexOf('isSelect')!=-1){
			if(val=="-1"){ errors+='- '+nm+' est obligatoire.\n'; }
		  }else if (test.indexOf('isNum')!=-1) { num = parseFloat(val);
			if (isNaN(val)) errors+='- '+nm+' doit être un nombre.\n';
		  }
	  } else if (test.charAt(0) == 'R') errors += '- '+nm+' est obligatoire.\n'; }
  }
  if (errors) alert('Les informations que vous avez saisies sont incorrectes :\n'+errors);
  document.MM_returnValue = (errors == '');
}

var nbCellule = 0 ;

function supCal(trSup){
	aSup = document.getElementById(trSup);
	aSup.parentNode.removeChild(aSup);
	m = document.getElementById('DateSup').value
	document.getElementById('DateSup').value = trSup.substr(5)+"|"+m;
}

function formFusion() {
		nbCellule++ ;
        var args = formFusion.arguments;
      
		var tbody = document.createElement('tbody');
		var newtr = document.createElement('tr');
		newtr.setAttribute('id','trCal'+nbCellule);
		for (i=0;i<args.length;i=i+4) {
            var newinput = null ;	
			var newtd = document.createElement('td');
			newinput = document.createElement('input');
           	newa = document.createElement('a');
			newimg = document.createElement('img');
			var deb_fin= (i==0?"debut_cal":"fin_cal");
			if(args[i+1]=="select"){
				newinput = document.createElement('select');
				newinput.name = (args[i]+nbCellule);
				newopt = document.createElement('option');
				newopt.value = "non";
				newopt.text = "non";
				newinput.appendChild(newopt);
				newopt2 = document.createElement('option');
				newopt2.value = "oui";
				newopt2.text = "oui";
				newinput.appendChild(newopt2);
				
			}else if(args[i+1]=="a") {
					newinput = document.createElement('a');
					newinput.setAttribute('onclick',"if(confirm('Souhaitez vous supprimer la date ?')){ supCal('trCal"+nbCellule+"') }");
					newinput.setAttribute('href',"javascript:;");
					newimg.setAttribute('src','../images/picto/picto_croix.gif');
					newimg.setAttribute('border','0');
					newimg.setAttribute('style','width:16px; height:16px; border:0px;');
					newinput.appendChild(newimg);
			}else{
				
				newinput.setAttribute('type', args[i+1]);
				newinput.setAttribute('id', args[i]+nbCellule);
				newinput.setAttribute('size', args[i+2]);	
				newinput.setAttribute('name', args[i]+nbCellule);
			}
			newtd.appendChild(newinput) ;
			if(args[i+3]=="oui"){
				newtd.appendChild(newa) ;
			}
			newtr.appendChild(newtd) ;
        }     
		tbody.appendChild(newtr) ;
		
		document.getElementById('tabdate').appendChild(tbody) ;	
		document.getElementById('nbAjDate').value=nbCellule ;	
			
}


String.prototype.trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g,'');
}

function isValidEmailAddress(emailAddress) {
  // If a simple regular expression doesn't work
  if(!new RegExp(/^test$/).test("test")) { return true; }

  // If the email address is empty
  if(emailAddress.trim() == '') { return false; }

  return new RegExp(/^([a-z]([_\.0-9a-z-]+)?@)([a-z]([_\.0-9a-z-]+)?\.)([a-z]{2,4})$/i).test(emailAddress.trim());
}

var obj_border = new Array();
function valid_input(o, tab) {
  var buffer = '';
  var obj = '';
  var texte = '';
  var retour_tmp = '';
  for (var i = 0; i < tab.length; i++) {
	obj = o.elements[tab[i][0]];
	texte = tab[i][1];
	if(!tab[i][2]) { tab[i][2] = ''; }

	if(obj) {	//Vrifie que l'object existe bien
	  retour_tmp = true;
	  switch (obj.type) {
		case 'text' :
		case 'password' :
		case 'textarea' :
		  if ((obj.value == '') || (obj.value == tab[i][2])) { retour_tmp = false; }
		  break;
		case 'select-one' :
		  if (obj.selectedIndex <= 0) { retour_tmp = false; }
		  break;
		case 'checkbox' :
		  if (obj.checked == false) { retour_tmp = false; }
		  break;
		default :
		  switch (obj[0].type) {
			case 'radio' :
			  var checked = false;
			  for (var j = 0; j < obj.length; j++) {
			  	checked = obj[j].checked?true:checked;
			  }
			  if (checked == false) { retour_tmp = false; }
			  break;
	  }}
	  obj.className = (retour_tmp == false)?'invalidInput':'validInput';

	  if (retour_tmp == false) {

		if((buffer == '') && (obj.name != undefined)) {
		  obj.focus();
		}

		buffer += "- "+texte+"\n";
  }}}

  return buffer;
}


//Combo multiple sans Ctrl
//<select name="exemple" multiple="multiple" onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);">
var arrOldValues;

function SelectAllList(CONTROL) {
  for(var i = 0;i < CONTROL.length;i++) {
	CONTROL.options[i].selected = true;
}}

function DeselectAllList(CONTROL){
  for(var i = 0;i < CONTROL.length;i++){
	CONTROL.options[i].selected = false;
}}

function FillListValues(CONTROL) {
  var arrNewValues;
  var intNewPos = -1;
  var strTemp = GetSelectValues(CONTROL);
  arrNewValues = strTemp.split(",");
  for(var i=0;i<arrNewValues.length-1;i++) {
	if(arrNewValues[i]==1) {
	  intNewPos = i;
  }}
  if(intNewPos == -1) { return; }

  for(var i=0;i<arrOldValues.length-1;i++) {
	if(arrOldValues[i]==1 && i != intNewPos){
	  CONTROL.options[i].selected= true;
	} else if(arrOldValues[i]==0 && i != intNewPos) {
	  CONTROL.options[i].selected= false;
	}

	if(arrOldValues[intNewPos]== 1) {
	  CONTROL.options[intNewPos].selected = false;
	} else {
	  CONTROL.options[intNewPos].selected = true;
}}}

function GetSelectValues(CONTROL) {
  var strTemp = "";
  for(var i = 0;i < CONTROL.length;i++) {
	if(CONTROL.options[i].selected == true) {
	  strTemp += "1,";
	} else {
	  strTemp += "0,";
  }}
  return strTemp;
}

function GetCurrentListValues(CONTROL) {
  var strValues = "";
  strValues = GetSelectValues(CONTROL);
  arrOldValues = strValues.split(",")
}
//Fin Combo multiple sans Ctrl
