// JavaScript Document

function click_link(dom_elt){
	var href = $(dom_elt).find('a:first').attr('href');
	if($(dom_elt).find('a:first').attr('target') == '_blank'){
		window.open (href);
	}else{
		document.location.replace(href);
	}
}

function getLink(element){		
	var elmList = element.getElementsByTagName("a");
	
	for (var i = 0; i < elmList.length; i++) { 
		var url = elmList[i].href;
		document.location.replace(url);
	}
}

function getURL_base(url){

	var S_url = url;

	stopIndex=S_url.indexOf("?");
	url_base=S_url.substr(0,stopIndex);
	
	return url_base;
}


function getURL_param(url){
	//renvoie les param sans le ?
	var S_url = url;
	startIndex=S_url.indexOf("?");
	param=S_url.substr(startIndex+1);
	
	return param;
} 

function checkbox_control(cell_id){
	if(document.getElementById(cell_id).checked){
		document.getElementById(cell_id).checked = false;
	}else{
		document.getElementById(cell_id).checked = true;
	}
} 

function getCheckboxList(fieldname){
	arrayCheckbox = document.getElementsByName(fieldname);
	var checkboxList = '';
	for(i=0; i< arrayCheckbox.length; i++){
		if(arrayCheckbox[i].checked == true){
			if(checkboxList == ''){
				checkboxList = arrayCheckbox[i].value;
			}else{
				checkboxList = checkboxList + ',' + arrayCheckbox[i].value;
			}
		}
	}
	return checkboxList;
}


function setFieldValue(field_id, field_value){
	var input = document.getElementById(field_id);
	input.value = field_value;
	return input.value;
}

function submit_form(formName){
	document.forms[formName].submit();
}

function empty_form(fieldList) {
	for (i=0; i<fieldList.length; i++){
		
		var o_field = document.getElementById(fieldList[i]);
		switch (o_field.type){ 
			case 'text' : o_field.value = '';
				break
			case 'select-one' : o_field.value = 0;
				break
			case 'radio' : 
				var o_field = document.getElementsByName(fieldList[i]);
				for (j=0; j<o_field.length; j++){
					o_field[j].checked = false;
				}
				break
			default	     :
		}	//end switch
	}	//end for
}

// fonction de récupération extension fichier
function file_extension(file) {
	if (file != ""){
		var length = file.length;
		var limit = file.lastIndexOf('.');
		
		extension = file.substring(limit,length);
		extension=extension.toLowerCase();
		return extension;
	}
}

function submit_form(form_id){
	document.getElementById(form_id).submit();
}

function form_to_param(contener_id){
	var param = '';
	$inputlist = $('#'+contener_id).find(":input");
	
	$inputlist.each(
		function(i){
			switch(this.type){
				case 'text':
				case 'textarea':
				case 'hidden':
				case 'file':
				case 'submit':
				case 'button':
				case 'select-one':
				case 'select-multiple':
					param += '&'+this.name+'='+$(this).val();
					break;
				case 'checkbox':
					if(this.checked){param += '&'+this.name+'='+$(this).val();}
					break;
				case 'radio':
					var radio_name = $(this).attr("name");
					var radio_group = document.getElementsByName(radio_name);

					is_empty = true;
					for (j=0; j < radio_group.length; j++) {
						if(radio_group[j].checked){
							param += '&'+this.name+'='+$(this).val();
						}
					}
					break;
				default:
			}//end switch
		}
	);
	return param;
}
