function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function CheckDateRange2(from, to) {
  var from_str = from.value.split('/');
  var to_str = to.value.split('/');
  from_data = new Date(from_str[2],from_str[1],from_str[0]);
  to_data = new Date(to_str[2],to_str[1],to_str[0]);
  //if (Date.parse(from.value) < Date.parse(to.value)) {
  //alert(from_data+" - "+to_data+" | ");
  if (from_data < to_data) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

function noFest(date){
  var daysett = date.getDay();
  var day = date.getDate();
  var month = date.getMonth()+1;
  var year = date.getFullYear();
    
	//domenica
	if (daysett == 0) return false;
	if (daysett == 6) return false;

	//1° gennaio - Capodanno  
	if (day == 1 && month ==1) return false;
	
	//6 gennaio - Epifania di Nostro Signore  
	if (day == 6 && month ==1) return false;
	
	//17 marzo - Festa 150 anni unità d'italia  
	if (day == 17 && month ==3) return false;
	//18 marzo - chiusura aziendale  
	if (day == 18 && month ==3) return false;
	
	//25 aprile - Anniversario della Liberazione  
	if (day == 25 && month ==4) return false;

	//1° maggio - Festa del Lavoro  
	if (day == 1 && month ==5) return false;

	//2 giugno - Festa della Repubblica  
	if (day == 2 && month ==6) return false;

	//15 agosto - Assunzione di Maria Vergine   
	if (day == 15 && month ==8) return false;

	//1° novembre - Tutti i Santi  
	if (day == 1 && month ==11) return false;

	//8 dicembre - Immacolata Concezione  
	if (day == 8 && month ==12) return false;
	
	//24 dicembre - Il corriere non effettua consegne
	if (day == 24 && month ==12) return false;
	
	//25 dicembre - Natale di Nostro Signore   
	if (day == 25 && month ==12) return false;

	//26 dicembre - Santo Stefano  
	if (day == 26 && month ==12) return false;

	//31 dicembre - Il corriere non effettua consegne
	if (day == 31 && month ==12) return false;
	
	//PASQUA
	if (month == 3 || month == 4){
		pasqua = calcolaPasqua(year);

		if (day == (pasqua['giorno'])+1 && month == pasqua['mese']) return false;
	}

    return [1];
}; 
  
function calcolaPasqua(year) {
	year = parseInt(year, 10);
	if (isNaN(year)) year = 0;

	var jDay;
	jDay = parseInt(jDay, 10);
	if (isNaN(jDay)) jDay = 0;
		
	var g;
	var i;
	var j;
	var p;
	
	g = year % 19;
	i = (19 * g + 15) % 30;
	j = (year + Math.floor(year / 4) + i) % 7;
	p = i - j + 28;

	if (p <= 31)
	{
		jDay = p;
		 FPasqua = {'giorno':jDay,'mese':3};
	}
	if (p > 31)
	{
		jDay = p - 31;
		FPasqua = {'giorno':jDay,'mese':4};

	}

	
	var wDay;
	var oDay;


	wDay = parseInt(wDay, 10);
	if (isNaN(wDay))wDay = 0;

	oDay = parseInt(oDay, 10);
	if (isNaN(oDay))oDay = 0;


	var g;
	var i;
	var j;
	var p;

   g = year % 19;
   i = (19 * g + 15) % 30;
   j = (year + Math.floor(year / 4) + i) % 7;
   p = i - j + 28;


	var a;
	var c;
	var h;
	var b;
	var d;
	var q;


   a = year % 19;
   c = Math.floor(year / 100);
   h = (c - Math.floor(c / 4) - Math.floor((8 * c + 13) / 25) + 19 * a + 15) % 30;
   b = h - Math.floor(h / 28) * (1 - Math.floor(29 / (h + 1)) * Math.floor((21 - a) / 11));
   d = (year + Math.floor(year / 4) + b + 2 - c + Math.floor(c / 4)) % 7;
   q = b - d + 28;

	var extra = 10;
	var tmp;

	tmp = Math.floor(year / 100) - 16;
	extra = 10 + tmp - Math.floor(tmp / 4);
	oDay = p + extra;

	if (q <= 31)
	{
		wDay = q;
		//FPasqua = wDay + " " + "marzo";
		FPasqua = {'giorno':wDay,'mese':3};		
	}
	if (q > 31)
	{
		wDay = q - 31;
		//FPasqua = wDay + " " + "aprile";ù
		FPasqua = {'giorno':wDay,'mese':4};		

	}

	return FPasqua
}

function chkCity() {
    //alert(this.id);
    if (this.id == 'mitt_city') {
        chkCityStart();
        //if(!chkCityStart()) return false;
    } else {
        chkCityDest();
        //if(!chkCityDest()) return false;
    }
    return true
}

function chkCityStart() {
    if($.trim($("#mitt_city").val()) == '') {
        $("#mitt_postcode").val("");
        $("#mitt_state").val("");
        $("#mitt_regione").val("");
        $("#div_mitt_regione").html("");
        $("#mitt_city").val("");
        return false;        
    }
    var esito = true;
	$.ajax({
			type: "POST",
			url: 'autocomplete_prov.php',
			data: {
                 term: $("#mitt_city").val(), 
                 country_id: document.getElementById('mitt_country').value,
                 tipo: 'str'
            },
            success: function( data ) {
                var dati = data.split("|");
                
                $("#mitt_city").val(dati[1]);
                $("#mitt_regione").val(dati[7]);
                $("#mitt_state").val(dati[3]);
                if (dati[7] != undefined) {
                    $("#div_mitt_regione").html("<b>"+dati[7]+"</b>");
                }
                $("#mitt_postcode").val(dati[2]);
                $("#mitt_country").val(dati[4]);
                if ($("#mitt_country_name").val() == '') {
                    $("#mitt_country").val(dati[4]);
                    $("#mitt_country_name").val(dati[5]);
                    $("#div_mitt_country").html("<img src='images/flags/"+dati[6]+".gif' border='0'>");
                }
            } 
	});
	return esito;    
}

function chkCityDest() {
    if($.trim($("#dest_city").val()) == '') return false;
    var esito = true;
	$.ajax({
			type: "POST",
			url: 'autocomplete_prov.php',
			data: {
                 term: $("#dest_city").val(), 
                 country_id: document.getElementById('dest_country').value,
                 tipo: 'str'
            },
			async: false,				
            success: function( data ) {
                var dati = data.split("|");
                
                $("#dest_city").val(dati[1]);
                $("#dest_regione").val(dati[7]);
                $("#dest_state").val(dati[3]);
                $("#div_dest_regione").html("<b>"+dati[7]+"</b>");
                $("#dest_postcode").val(dati[2]);
                $("#dest_country").val(dati[4]);
                if ($("#dest_country_name").val() == '') {
                    $("#dest_country").val(dati[4]);
                    $("#dest_country_name").val(dati[5]);
                    $("#div_dest_country").html("<img src='images/flags/"+dati[6]+".gif' border='0'>");
                }
            } 
	});
	return esito;    
}

function launchJavascript(responseText) {
  // RegExp from prototype.sonio.net
/*
  var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
		   
  var match_exp    = new RegExp(ScriptFragment, 'img');
  var scripts  = responseText.match(match_exp);

    if(scripts) {
		var js = '';
		for(var s = 0; s < scripts.length; s++) {
			var match = new RegExp(ScriptFragment, 'im');
			js += scripts[s].match(match_exp)[1];
		}
        eval(js);
	}
*/
	//var source = _source;
	var source = responseText;
	var scripts = new Array();

	// Strip out tags
    while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);

		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
            eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}

	// Return the cleaned source
	return source;
}
