// JavaScript Document

function openWindow(path){
	var theHeight = 410;
	var theWidth = 520;
	var theTop = (screen.height/2)-(theHeight/2);
	var theLeft = (screen.width/2)-(theWidth/2);
	newwindow=window.open(path, '','scrollbars=1,toolbar=0,status=0,resizable=1,height='+theHeight+',width='+theWidth+',top='+theTop+',left='+theLeft);
	if (window.focus) {
		newwindow.focus();
	}	
	return false;			
}

function makeUppercase(document) {
	var f = document.forms[0]
	for(i = 0; i< f.elements.length; i++){
		f.elements[i].value = f.elements[i].value.toUpperCase();
	}
}

function createXMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("Ajax non supportato, si consiglia di aggiornare il browser e/o di abilitare i controlli ActiveX");
	return null;
}

function deleteOptionList() {
	var num_option=document.getElementById('meseanno').options.length;	
	for(a = num_option; a >= 0; a--) {
		document.getElementById('meseanno').options[a] = null;	
	}
}

function createOptionList(xml, selectedMonth) {
	var selectTag = document.getElementById('meseanno');
	var optionList = xml.getElementsByTagName("option");
	selectTag.options[0] = new Option(" ", "");
	for (i = 0; i <= optionList.length; i++) {
		selectTag.options[i+1] = new Option(optionList[i].getAttributeNode("description").value, optionList[i].getAttributeNode("value").value);
		if (selectedMonth == optionList[i].getAttributeNode("value").value) {
			selectTag.options[i+1].selected = true;
		}
	}
}

function VisualizzaLastMinute() {   
    var req = createXMLHttpRequest();
    var url='lastMinute?width='+screen.width;
    req.open("GET", url, true);
    req.onreadystatechange = function() {
        if (req.readyState == 4){
            if ((req.status == 200)) {
            	document.getElementById('alert').innerHTML = req.responseText;
            	document.getElementById('alert').style.visibility = 'visible';
            } 
        }
    }
    req.send(null);
}

function ChiudiVisualizzazione() {
	document.getElementById('alert').style.visibility = 'hidden'
}

/* box ricerca */

function submitSearchForm(baseURL){
	var inputParams='';
	for (var i=0;i< document.searchForm.elements.length;i++){
		if (document.searchForm.elements[i].type=='checkbox' && document.searchForm.elements[i].checked==true){
			inputParams+='1';
		}
	}
	if (document.searchForm.regione.selectedIndex==0){
	  document.searchForm.regione.selectedIndex=-1;
	}
	if (document.searchForm.siglaProv.selectedIndex==0){
	  document.searchForm.siglaProv.selectedIndex=-1;
	}
	if (document.searchForm.comune.selectedIndex==0){
	  document.searchForm.comune.selectedIndex=-1;
	}
	document.searchForm.action=baseURL+'/searchResSimple';
    if (document.getElementById('adv') != null) {
    	document.searchForm.action=baseURL+'/search';
	}
    document.searchForm.submit();
}

function AJAX_getProvince(baseURL, selected_value) {
	$.getJSON(baseURL+"/AJAX_getProvince",{regione: $("#regione").val()}, function(json) {
		fillOptions("siglaProv", json, selected_value);
		//$('#comune').html('<option value=""></option>');
    });
}
	        	

function AJAX_getComuni(baseURL, siglaProv, selected_value) {
	if (siglaProv == '') {
		siglaProv = $("#siglaProv").val();
	}
	$.getJSON(baseURL+"/AJAX_getComuni",{siglaProv: siglaProv}, function(json) {
		fillOptions("comune", json, selected_value) 
    });
}

function fillOptions(element_id, json, selected_value) {
	var options = '';
	for (var i = 0; i < json.length; i++) {
		options += '<option value="' + json[i].optionValue + '"';
		if (selected_value == json[i].optionValue) { 
			options += ' selected="selected"';
		}
		options += '>' + json[i].optionDisplay + '</option>';
	}
  	$('#'+element_id).html(options);
  	$('#'+element_id).removeAttr('disabled');
}
