//Javascript document

function switchDisplay(id){
	$("#"+id).slideToggle("slow");
}

function autocompleteField(base_path, controller_path, input_field){
	 $(document).ready(function(){
		$("#"+input_field).autocomplete(base_path+controller_path+'/ajax_tag_list', {
			multiple: false,
			autoFill: true
		});
	  });
	}

function viewImage(id, viewW, viewH) {
	//Get the screen height and width
	var maskWidth = $(window).width();
	var maskHeight = $(document).height();
	
	//Set height and width to mask to fill up the whole screen
	$('#mask_'+id).css({'top':0, 'left':0, 'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$('#mask_'+id).fadeIn(1000);	
	$('#mask_'+id).fadeTo("slow",0.8);	

	var winW = $(window).width();
	var winH = $(window).height();
	if (viewW != 0) {
		$('#dialog_'+id).css('width', viewW);
	}
	if (viewH != 0) {
		$('#dialog_'+id).css('height', viewH + 22);
	}
	
	$('#dialog_'+id).css('left', winW/2-$('#dialog_'+id).width()/2);
	$('#dialog_'+id).css('top',  winH/2-$('#dialog_'+id).height()/2);

	//$('#file_title').html("&nbsp;<b>"+file_name+"</b>");
	//$('#file_viewer').html(spinner);

	//transition effect
	$('#dialog_'+id).fadeIn(2000); 
	
	$('#close_button_'+id).click(function (e) { e.preventDefault(); $('#mask_'+id+', .window').hide(); });		
	$('#mask_'+id).click(function () { $('#mask_'+id+', .window').hide(); });		 
}

function cleanUsername(str) {
	str = str.replace(/à/gi, 'a');
	str = str.replace(/á/gi, 'a');
	str = str.replace(/ä/gi, 'a');
	str = str.replace(/è/gi, 'e');
	str = str.replace(/é/gi, 'e');
	str = str.replace(/ë/gi, 'e');
	str = str.replace(/ì/gi, 'i');
	str = str.replace(/í/gi, 'i');
	str = str.replace(/ï/gi, 'i');
	str = str.replace(/ò/gi, 'o');
	str = str.replace(/ó/gi, 'o');
	str = str.replace(/ö/gi, 'o');
	str = str.replace(/ù/gi, 'u');
	str = str.replace(/ú/gi, 'u');
	str = str.replace(/ü/gi, 'u');
	str = str.replace(/\'/gi, '');
	str = str.replace(/ /gi, '');
	return str;
}

function fillUsername(base_url) {
	var nome = document.getElementById('UserNome').value;
	var cognome = document.getElementById('UserCognome').value;
	var n = cleanUsername(nome.toLowerCase());
	var c = cleanUsername(cognome.toLowerCase());
	var username = document.getElementById('UserUsername');	
	if ((n != '') && (c != '')){
		$.get(base_url+'users/ajax_generate_username', 
     		{name: n, surname: c},
     		function(result) {
     			username.value = result;
     		}
     	 );
	}
	return;
}

function generatePassword() {
	document.getElementById('UserPassword').value = document.getElementById('UserNascitaDataYear').value 
												  + document.getElementById('UserNascitaDataMonth').value
												  + document.getElementById('UserNascitaDataDay').value;
}

function validateContentForm() {
	return true;
	var v1 = document.getElementById('ContentMenuParentId').value;
	var v2 = document.getElementById('ContentNomeMenu').value;
	if ((v1 == '') && (v2 == '')) {
		return true;
	}
	if ((v1 != '') && (v2 != '')) {
		return true;
	}
	alert('I campi Menu Padre e Nome Menu devono essere entrambe riempiti oppure vuoti!')
	return false;
}

function showButtonContent(buttonNumber){
    for (var i=1; i<=3; i++) {
        var tab=document.getElementById('tab'+i);        
        var box=document.getElementById('body'+i);
        if (i != buttonNumber) {
            tab.style.backgroundColor="#E0A7A7";
            //box.style.visibility='hidden';
            $('#body'+i).fadeOut('fast');
        } else {
            tab.style.backgroundColor="#AB0000";
            box.style.visibility='visible';
            box.style.clear='both';
            $('#body'+i).fadeIn('slow');
        }
    }
}

function loadSelectElement(element_from, url, element_to, selected_value) {
	if($('#'+element_from).val().length != 0) {
     	$.getJSON(url, 
     		{id: $('#'+element_from).val()},
     		function(options) {
				if(options !== null) {
					fillOptions(element_to, options, selected_value);
            	}
    		}
     	);
	}
}

function changeSelectElement(element_from, url, element_to) {
	$('#'+element_from).change(function() {
		loadSelectElement(element_from, url, element_to, null);
	});
}

function prepareHtmlCommonElement(base_url, province_val, common_val) {
	if($('#SearchRegionId').val().length != 0) {
     	$.getJSON(base_url+'regions/ajax_get_province', 
     		{id: $('#SearchRegionId').val()},
     		function(options) {
				if(options !== null) {
        			fillOptions('SearchProvinceId', options, province_val);
            	}
    		});
	}
	if(province_val.length != 0) {
     	$.getJSON(base_url+'provinces/ajax_get_commons', 
     		{id: province_val},
     		function(options) {
				if(options !== null) {
        			fillOptions('SearchCommonId', options, common_val);
            	}
    		});
	}
	return;
}

// funzione da usare nei CRUD per inizializzare regioni, province e comuni in add e edit
function prepareSelectElement(base_url, f1, f2, f3, f3_val) {
	$(document).ready(function() {
		if ((f3_val != null) && (f3_val.length != 0)) {
			$.get(base_url+'commons/ajax_get_parent_ids', 
	     		{id: f3_val},
	     		function(indexes) {
	     			var index_array = indexes.split(":");
					if ((index_array[0] !== null) && (index_array[1] !== null)) {
						$('#'+f1).val(index_array[0]);
						$.getJSON(base_url+'regions/ajax_get_province', 
			         		{id: index_array[0]},
			         		function(options) {
								if(options !== null) {
			            			fillOptions(f2, options, index_array[1]);
			                	}
			        		});
						$('#'+f2).val(index_array[1]);
						$.getJSON(base_url+'provinces/ajax_get_commons', 
			         		{id: index_array[1]},
			         		function(options) {
								if(options !== null) {
			            			fillOptions(f3, options, f3_val);
			                	}
			        		});
	            	}
	    		}
	     	);
		}
	});
}

function fillSelectElement(element_from, url, element_to) {
	$(document).ready(function() {
	    $('#'+element_from).change( function () {
	    	if($(this).val().length != 0) {
	         	$.getJSON(url, 
	         		{id: $(this).val()},
	         		function(options) {
						if(options !== null) {
	            			fillOptions(element_to, options, null);
	                	}
	        		});
	    	}
	    });
	});
}

function fillOptions(element_id, options_list, selected_value) {
  	var options = '';
  	options += '<option value="">---</option>';
  	$.each(options_list, function(index, option) {
  		if (selected_value == index) {
  			options += '<option value="' + index + '" selected="selected">' + option + '</option>';
  		}
  		else {
  			options += '<option value="' + index + '">' + option + '</option>';
  		}
  	});
  	$('#'+element_id).html(options);
}

function fillMultipleOptions(element_id, options_list, selected_values) {
  	var options = '';
  	$.each(options_list, function(index, option) {
  		var written = false;
  		if (selected_values == 'all') {
  			options += '<option value="' + index + '" selected="selected">' + option + '</option>';
  			written = true;	
  		}
  		for (var i in selected_values) {
	  		if (selected_values[i] == index) {
	  			options += '<option value="' + index + '" selected="selected">' + option + '</option>';
	  			written = true;
	  		}
  		}
  		if (!written) {
  			options += '<option value="' + index + '">' + option + '</option>';
  		}
  	});
  	$('#'+element_id).html(options);
}

