var baseURL="";

var formSubmitted = false;

function onSubmitForm(formName) {

	if (formSubmitted) {
		alert('Ce formulaire a déjà été envoyé et ne peut être envoyé plusieurs fois. Si besoin, rechargez la page puis réessayez. ');
		return false;
	}
	else {
		formSubmitted = true;		
		return true;
	}
}

function checkAll(checkbox) {
	for (i = 0; i < checkbox.length; i++) {
		checkbox[i].checked = true;
	}
	return "Select all";
}

function unCheckAll(checkbox) {
	for (i = 0; i < checkbox.length; i++) {
		checkbox[i].checked = false;
	}
	return "Select none";
}

function isCheckAll(checkbox) {

	for (i = 0; i < checkbox.length; i++) {
		if(! checkbox[i].checked) {
			return false;
		}
	}
	return true;
}

function isCheckedOne(checkbox) {

	for (i = 0; i < checkbox.length; i++) {
		if(checkbox[i].checked) {
			return true;
		}
	}
	return false;
}


function checkUncheckAll(cBoxAll, cb) {
		
	if(cBoxAll.checked) {
		checkAll(cb);
	}
	else {
		unCheckAll(cb);
	}
}

function oneCheckUncheckAll(cBoxAll, cb) {
	
	if(isCheckAll(cb)) {
		cBoxAll.checked = true;
	}
	else {
		cBoxAll.checked = false;
	}
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
 }

function ExtensionsOkay() {
	
	var extension = new Array();
	
	// Step 1 of 2:
	// Replace MyForm with the name of your form and 
	//    replace FieldName with the upload field name.
	
	var fieldvalue = document.templateForm.fileUpload.value;
	
	if(fieldvalue == '') {
		alert('Veuillez attacher le fichier de template')
		return false;
	}
	// Step 2 of 2:
	// Add the file name extensions that are okay (with 
	//    the period), for the variables with their numbers 
	//    in sequential order, as many or as few as needed, 
	//    starting with 0. (These are case sensitive.)
	
	extension[0] = ".tpl";

	// No other customization needed.
	var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
	for(var i = 0; i < extension.length; i++) {
		if(thisext == extension[i]) { return true; }
		}
	alert("Seul extension '.tpl' de template est autorisé");
	return false;
}


function deleteAlertMessage() {
   		
   		var message = "Vous êtes sur de vouloir le supprimer?"
   		answer = confirm(message)
   		if(answer) {
    		return confirm("Vous êtes vraiment sûr de supprimer ?");
    		//alert("L'opération est en cours de contruction");
   		}
		return false;
}

function get(ID) {
	return document.getElementById(ID);
}

function adaptTimeField(adapterId, adapteeId) {
	adapter = get(adapterId);
	adaptee = get(adapteeId);
	
	if (adapter.value == '-1') {
		adaptee.value = '-1';
	}
	else {
		if (adaptee.value == '-1') {
			adaptee.value = '00';
		}
	}
}


function toggle(obj) {
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
}


function get(ID) {
	return document.getElementById(ID);
}

function showSubFilters(parentFilterID) {
	for (var i = 0; i < listParentFilterID.length; i++) {
		if (listParentFilterID[i] != parentFilterID || (listParentFilterID[i] == parentFilterID && get('filter' + listParentFilterID[i] + 'SubFilters').style.display == '')) {
			get('filter' + listParentFilterID[i] + 'Img').src = baseURL+'/images/arrow_down_short.gif';
			new Effect.BlindUp(get('filter' + listParentFilterID[i] + 'SubFilters'), {duration : 0.5});
		}
		else {
			get('filter' + listParentFilterID[i] + 'Img').src = baseURL+'/images/arrow_up_short.gif';
					
			new Effect.BlindDown(get('filter' + listParentFilterID[i] + 'SubFilters'), {duration : 0.5});
		}
	}
}


function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		
		return false;
	}
	
	month = matchArray[3]; // p@rse date into variables
	day = matchArray[1];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		
		return false;
	}
	
	if (day < 1 || day > 31) {
		
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		
			return false;
		}
	}
	return true; // date is valid
}



function getStyle(elementID) {
	return get(elementID).style.display;
}	

function setStyle(elementID, value) {
	get(elementID).style.display = value;
}



