
var win_assist;
function AssistCitta(c)
{
	if(win_assist && win_assist.open && !win_assist.closed)
	{
		win_assist.focus();
	}
	else
	{
		win_assist = window.open('', 'win_assist','width=400,height=600,resizable=1,scrollbars=0,left=50,top=50');
	}   

	document.form.assist.value = c;
	document.form.action = 'ncitta.pl'	
	document.form.target = 'win_assist';
	document.form.submit();
	document.form.target = '';
}

var win_giorno;
function Giorno(giorno, ncalendario)
{
	if(win_giorno && win_giorno.open && !win_giorno.closed)
	{
		win_giorno.focus();
	}
	else
	{
		win_giorno = window.open('', 'win_giorno','width=600,height=600,resizable=1,scrollbars=1,left=0,top=0');
	}   

	if(giorno)
		document.form.giorno.value = giorno;
	if(ncalendario)
		document.form.ncalendario.value = ncalendario;
	document.form.action = 'giorno.pl'	
	document.form.target = 'win_giorno';
	document.form.submit();
	document.form.target = '';
}

function AbilitaBottoni(name)
{
	if(!name) return;

	var array = new Array();
	array = document.getElementsByName(name);

	for(i=0; i< array.length; i++)
	{
		var bt = array[i];
		bt.className = 'bottone';
		bt.disabled = 0;
	}

	return;
}

function DisabilitaBottoni(name)
{
	if(!name);
	var array = new Array();
	array = document.getElementsByName(name);

	for(i=0; i< array.length; i++)
	{
		var bt = array[i];
		bt.className = 'inattivo';
		bt.disabled = 1;
	}

	return;
}

function Numero(v)
{
	var rExp = /\./g;
	v = v.replace(rExp, '');

	var rExp = /,/;
	v = v.replace(rExp, '.');

	return v;
}

// Controllo Validazione data
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function Chk_Date(obj , label)
{
	var msg = '\n\t' + label + ': ' + obj.value + '\n';
	if(isDate(obj.value, msg, obj)==false) {
		obj.value = '';
		return 1
	}
}

function isDate(dtStr,field,e){
	if (!dtStr)
		return
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Il formato della data in "+field+" (gg/mm/aaaa) non  corretto")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Il mese specificato in "+field+" non  valido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Il giorno specificato in "+field+" non  valido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Inserire in "+field+" un anno valido di 4 cifre tra "+minYear+" e "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("La data specificata in "+field+" non  valida")
		return false
	}
	if (e) {
		if (strDay.length==1) strDay='0'+strDay;
		if (strMonth.length==1) strMonth='0'+strMonth;
		e.value = strDay+'/'+strMonth+'/'+strYear;
	}
return true
}
// Fine Validazione Data

function Chk_Time(t)
{
  var test = /^([01][0-9]|[2][0-3])(:[0-5][0-9])$/.test(t) 
  if(! test)
  {
  	alert('Ora non valida');
  }
}

function Calcola(g)
{
	var s = Numero(g.value);
	var cgp = document.form.costo_gp.value;

	var i = s * cgp;
	document.form.importo.value = i;
}

function Chk_Integer(obj, label)
{
	var msg = '\n\t' + label + ': ' + obj.value + '\n';
	if(isInteger(obj.value, msg, obj)==false) {
		alert('Il campo IVA: ' + obj.value + ' non e valido');
		obj.value = '';
		return 1
	}
}

function go(path) {
	document.location.href = path;
}

