var numKey = 0;
var lastINDEX = 0;
var rExp = new String;
var lastIndex=0;

//win0 = new window();
//win1 = new window();
//win2 = new window();
//win3 = new window();

function openRiv(URL) {
	window.open(URL,'Rivendite','top=300,left=400,location=0,height=600,width=500,scrollbars=1,status=0,resizable=1');
}

function openArt(URL) {
	window.open(URL,'Prezzi','top=300,left=400,location=0,height=400,width=600,scrollbars=1,status=0,resizable=1');
}

function Orari(riga,url,x,y) {
	var mywin;

	mywin = window.open(url,"", "width=500,height=400,directories=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes")
	mywin.moveTo(x,y);
	mywin.focus();
	
/*	var i = riga % 4;			
	
	switch (i) {
		case 0: 
			win0 = mywin;
			break;
		case 1: 
			win1 = mywin; 
			break;
		case 2: 
			win2 = mywin; 
			break;
		case 3: 
			win3 = mywin; 
			break;
	}
	
	win0.focus();
	win1.focus();
	win2.focus(); 
	win3.focus();
*/
	
}

function CalendarioSelezionato(anno,mese,giorno)
{

 var mydate = new Date(anno,mese,giorno); 
 
 Mese=mydate.getMonth();
 Anno=mydate.getFullYear(); 
 
 DataComposta = new String(Mese+'/'+Anno);
 Giorno       = new String(mydate.getDate());
 
 if (DataComposta.length!=7) DataComposta='0'+DataComposta;
 if (Giorno.length!=2) Giorno ='0'+Giorno;

 DataComposta = Giorno+'/'+DataComposta;
 myselect = document.getElementById("dataViaggio");

 for (i=0; i < myselect.options.length; i++){
	if (DataComposta == myselect.options[i].value) 
	{
		myselect.options[i].selected=true;
	}
	else
	{
		myselect.options[i].selected=false;
	}
 }

 /*myselect = document.getElementById("gg");

 for (i=0;i < myselect.options.length;i++){
	
	if (Giorno == myselect.options[i].value) 
	{
		myselect.options[i].selected=true;
	}
	else
	{
		myselect.options[i].selected=false;
	}
 }
 */
 
}

function AVVIO() {
	if(document.EMISSIONE.PARTENZA)
		document.EMISSIONE.PARTENZA.focus(); 
	
	/*if (document.EMISSIONE.LocPart.selectedIndex <=0 ) {
		document.EMISSIONE.LocPart.focus();
		document.EMISSIONE.LocPart.click();
		document.EMISSIONE.LocPart.setActive();	
		document.EMISSIONE.LocDest.focus();
		document.EMISSIONE.LocDest.click();
		document.EMISSIONE.LocDest.setActive();	
	}*/	
	
	
	
}	
	

function setFascia(idx) {
	with (EMISSIONE) {
		OREDalSel.disabled=true;
		OREAlSel.disabled=true;
		switch (idx){
    			case 1 : 
    				OREDalSel.selectedIndex = 0;
    				OREAlSel.selectedIndex = 24;
    				break;
    				
			case 2 : 
				OREDalSel.selectedIndex = 24;
				OREAlSel.selectedIndex = 36;
				break;
			case 3 : 
				OREDalSel.selectedIndex = 36;
				OREAlSel.selectedIndex = 48;
				break;
			case 4 : 
				OREDalSel.selectedIndex =  0;
				OREAlSel.selectedIndex = 48;
				break;
				
			case 5 : 
				OREAlSel.selectedIndex = 48;
				OREDalSel.disabled=false; 
				OREAlSel.disabled=false;
				
				var myTime = new Date();
				var myHour = myTime.getHours();
				
				
				//if (len(myHour)<2) timeI='0'+timeI;
				
				var timeI; timeI  = myHour + ':00';
				
				for(i=0;i<document.EMISSIONE.OREDalSel.options.length;i+=2)
				{
					if (document.EMISSIONE.OREDalSel.options[i].value == timeI ) {
						document.EMISSIONE.OREDalSel.selectedIndex = document.EMISSIONE.OREDalSel.options[i].index;
					}	
				}

				
				break;			
		}
		
	}	
}
	
function OPENCALENDAR(){
	
	var url;
	var gg, anno;
	dataOggi = new Date();

	//gg = document.getElementById('gg').value 
	//meseanno = document.getElementById('meseanno').value 
	dataViaggio = document.getElementById('dataViaggio').value 

	myString = new String(dataViaggio);  	 
	
	gg = myString.slice(0,2);
	mese = myString.slice(3,5);
	anno = myString.slice(6,10);
	 
	//c = new String('/')		
	//posDelimiter = myString.indexOf(c);	
	//mese= myString.substr(0,posDelimiter);	
	//anno = myString.substr(posDelimiter+1,myString.length); 
			
	document.EMISSIONE.gg.value=gg;
	document.EMISSIONE.meseanno.value=mese+'/'+anno;
	
	url='../asp/ro_calendar.asp?name_form=vSearch&inout=in&lg=it&mese='+ mese +'&anno='+anno+'&gi='+ gg + '&load=1' + '&dg='+ dataOggi.getDay() + '&dm='+ dataOggi.getMonth() + '&da='+ dataOggi.getFullYear();
	
	window.open(url,'calendario','top=300,left=300,location=0,height=200,width=180,scrollbars=0,status=0,resizable=0');
}		

	
function pulisci(obj)
{	
  	
	rExp = '';
	numKey = 0;

}

function IsCollection(oObject){			
  if (oObject != null)
  {
   if (oObject.length != null) 
     return true 
   else
     return false
  }	
}

function CLICKFERMATA(obj){
	obj.setActive();
	reset();
}		

function ClickFermata(objI){
  rExp='';
  numKey=0;
	if (objI.name=='comunePartINI') {
		document.EMISSIONE.PARTENZA.value = 
			document.EMISSIONE.comunePartINI.options[document.EMISSIONE.comunePartINI.selectedIndex].text;
		document.EMISSIONE.ARRIVO.focus(); // --- CodMod.20090312
	}
		
	if (objI.name=='comuneArrINI') {
		document.EMISSIONE.ARRIVO.value = 
			document.EMISSIONE.comuneArrINI.options[document.EMISSIONE.comuneArrINI.selectedIndex].text;
		document.EMISSIONE.OK_COMUNE.focus(); // --- CodMod.20090312
	}
	if (objI.name=='LocPart') {
  		document.EMISSIONE.PARTENZA.value = 
			document.EMISSIONE.LocPart.options[document.EMISSIONE.LocPart.selectedIndex].text;
		document.EMISSIONE.ARRIVO.focus(); // --- CodMod.20090312
	}
	if (objI.name=='LocDest') {
		document.EMISSIONE.ARRIVO.value = 
			document.EMISSIONE.LocDest.options[document.EMISSIONE.LocDest.selectedIndex].text;
		document.EMISSIONE.CERCA_ANDATA.focus(); // --- CodMod.20090312
	}
  	
}	

function cerca(objI,delimiter,carattere)
{    
  var valReturn;
  var index = -1;
  var slice; 
  var c;
  var i = 0; 
  var cambiocampo = 0;
  
  if (objI.name=='PARTENZA') {  	
  	if (document.EMISSIONE.TIPOVIS.value=='COMUNI')
  		var obj = document.EMISSIONE.comunePartINI; // select dei comuni di partenza
  	else
  		var obj = document.EMISSIONE.LocPart;   	// select delle fermate di partenza	
  }
   
  if (objI.name=='ARRIVO') {  	
	if (document.EMISSIONE.TIPOVIS.value=='COMUNI')
  		var obj = document.EMISSIONE.comuneArrINI; 	// select dei comuni di arrivo
  	else
  		var obj = document.EMISSIONE.LocDest;		// select delle fermate di arrivo
  }  
  
  switch (objI.name)
  {
  	case 'comunePartINI':	
  		//click sulla select dei comuni di partenza
  		var obj = document.EMISSIONE.comunePartINI;  // select dei comuni di partenza  		
  		
  		//document.EMISSIONE.PARTENZA.focus();  		
  		var objFOCUS = document.EMISSIONE.PARTENZA;  	
  		objFOCUS.value='';
  		cambiocampo = 1;
  		break;	
  	case 'comuneArrINI':	
  		//click sulla select dei comuni	di arrivo
  		var obj = document.EMISSIONE.comuneArrINI;  // select dei comuni di arrivo  		
  		
  		//document.EMISSIONE.ARRIVO.focus();
  		var objFOCUS = document.EMISSIONE.ARRIVO; 
  		objFOCUS.value='';
  		cambiocampo = 1;  		 
  		break;
 		
  	case 'LocPart':	
  		//click sulla select dei fermate di partenza  		
  		//var obj = document.EMISSIONE.LocPart;  // select delle fermate di partenza
  		
  		//document.EMISSIONE.PARTENZA.focus();
  		var objFOCUS = document.EMISSIONE.PARTENZA;  	  	  		
  		objFOCUS.value='';
  		cambiocampo = 1;
  		break;	
  			
  	case 'LocDest':	
  		//click sulla select dei fermate di arrivo
  		//var obj = document.EMISSIONE.LocDest;  // select delle fermate di arrivo  	
  			  		
  		//document.EMISSIONE.ARRIVO.focus();
  		var objFOCUS = document.EMISSIONE.ARRIVO;  	  	
  		objFOCUS.value='';
  		cambiocampo = 1;
  		break;	
  		
	default:
		break; 
  }
  
  //obj.setActive();    
  //oOutput.innerText = oOutput.innerText +'\n  rExp: ' + rExp	
  //oOutput.innerText = oOutput.innerText +'\n  tasto: ' + event.keyCode
  //13: ENTER - 9:TAB - 33: pag. su - 34: pag. gił
	switch (carattere)
	{
		case 9:		
		// --- CodMod.20090312
    	//if (lastIndex != 0) obj.selectedIndex = lastIndex;
		//ClickFermata(obj)
		// -------------------
		for (k=1;k<=200;k++){}	
		return true;	
		break;

		case 13:
		//if (lastIndex != 0) obj.size=1 
		//reset();
	
		// --- CodMod.20090312
		ClickFermata(obj)
		// -------------------
		for (k=1;k<=200;k++){}
		return false;
		break;

		case 33 :
		if (obj.selectedIndex != 0) obj.selectedIndex = obj.selectedIndex - 1;
		return false;
		break;

		case 38:
		if (obj.selectedIndex != 0) obj.selectedIndex = obj.selectedIndex - 1;
		return false;
		break;

		case 34 :
		if (obj.selectedIndex = obj.options.length-1) obj.selectedIndex = obj.selectedIndex + 1;
		return false;
		break;

		case 40 :
		if (obj.selectedIndex != obj.options.length-1) obj.selectedIndex = obj.selectedIndex + 1;
		return false;
		break;

		case 37: //freccia SX
		case 39: // freccia DX
		return false;
		break;

		case 46: // tasto CANCEL o DEL
		rExp = '';
		numKey = 0;
		objI.value = '';
		return false;
		break;

		default:
		if (IsCollection(obj))
		{
			// --- CodMod.20090317
			// --- per risolvere il problema del backspace 
			if (carattere == 8 ) {
				if( numKey > 0) {
					numKey = numKey - 1;
					rExp = rExp.substr(0,numKey);
				}
			} 
			else {
				numKey = numKey + 1;
				// --- CodMod.20080205
				// --- per risolvere il problema dell'apostrofo (219) nei nomi di cittą
				if (carattere == 219) 
					rExp = rExp + "'";
				// --- CodMod.20100705
				// --- per risolvere il problema del punto (190)
				else if (carattere == 190) 
					rExp = rExp + ".";
				else
					rExp = rExp + String.fromCharCode(carattere); 
			}
			rExp = rExp.toLowerCase();
			//window.status = numKey + " - " + rExp;
			/* ******************************************* */
  
			while( i <= obj.options.length - 1 && index == -1) { 
				myString = new String(obj.options[i].text);  	  
				c = new String(delimiter)		
				posDelimiter = myString.indexOf[c];
				
				if (posDelimiter>0) numKey = numKey + posDelimiter;
				slice = myString.substr(0,numKey);	
				//alert('slice = ' + i + '>>'  + slice.toLowerCase() + ' da cercare = '+ rExp.toLowerCase());	
				if (slice.toLowerCase() == rExp.toLowerCase()) {
					index = i;
					obj.selectedIndex = index;
					lastIndex = index;						
					if (cambiocampo == 1) objFOCUS.value = rExp.toLowerCase();
					//Output.innerText = oOutput.innerText + '\n ['+rExp +'=' +obj.options(i).text+' ---> OK ' + index + ']';	 
				}	
				i++;
			} // fine While 
		} // fine If IsCollection
		//oOutput.innerText = oOutput.innerText +'\n   FUORI = '+ index;
		//oOutput.innerText = oOutput.innerText +'\n ' + obj.selectedIndex + '= ' + obj.options(obj.selectedIndex).text;
		//return false;
		break;	 
	}  // fine Switch (carattere)  	
}	

function CONTROLLO() {
	CONTROLLASUBMIT();
}

function CONTROLLORICERCA() {
	if (RICERCAAVANZATA2.style.display == 'none') 
		RICERCAAVANZATA2.style.display = 'inline';
	else 
		RICERCAAVANZATA2.style.display = 'none';
}

// --- CodMod.20100719
function CONTROLLACOMUNI(){
	var idxPart = document.getElementById('comunePartINI').selectedIndex;
	var idxDest = document.getElementById('comuneArrINI').selectedIndex;
	var DesComPart =  document.getElementById('comunePartINI').options[idxPart].innerHTML;
	var DesComDest = document.getElementById('comuneArrINI').options[idxDest].innerHTML;
	
	if(document.getElementById('controlloEsisteFermata').value == 1) {
		if(DesComPart.toLowerCase().lastIndexOf(document.getElementsByName('PARTENZA')[0].value.toLowerCase()) < 0 ) {
			if (document.getElementsByName('PARTENZA')[0].value != "") {
				alert("PARTENZA:\nhai cercato un comune che non esiste.");
				return false;
			}
		}
		// ---
		if(DesComDest.toLowerCase().lastIndexOf(document.getElementsByName('ARRIVO')[0].value.toLowerCase()) < 0 ) {
			if (document.getElementsByName('ARRIVO')[0].value != "") {
				alert("ARRIVO:\nhai cercato un comune che non esiste.");
				return false;
			}
		}
	}
	return true;
}
// ----------------------------

function CONTROLLASUBMIT(){
	
	idxPart = document.getElementById('LocPart').selectedIndex;
	idxDest = document.getElementById('LocDest').selectedIndex;
	
	document.getElementById('DesLocPart').value =  document.getElementById('LocPart').options[idxPart].innerHTML;
	document.getElementById('DesLocDest').value = document.getElementById('LocDest').options[idxDest].innerHTML;
	
	// --- CodMod.20071114
	// --- controllo che il testo scritto nel campo di ricerca della fermata o della 
	// --- localitą sia contenuto nelle voci della lista delle fermate o localitą
	if(document.getElementById('controlloEsisteFermata').value == 1)
	{
		if(document.getElementById('DesLocPart').value.toLowerCase().lastIndexOf(
			document.getElementsByName('PARTENZA')[0].value.toLowerCase()) < 0 )
		{
			if (document.getElementsByName('PARTENZA')[0].value != "")
			{
				alert("PARTENZA:\nhai cercato una localitą o una fermata che non esiste.");
				return false;
			}
		}
		// ---
		if(document.getElementById('DesLocDest').value.toLowerCase().lastIndexOf(
			document.getElementsByName('ARRIVO')[0].value.toLowerCase()) < 0 )
		{
			if (document.getElementsByName('ARRIVO')[0].value != "")
			{
				alert("ARRIVO:\nhai cercato una localitą o una fermata che non esiste.");
				return false;
			}
		}
	}
	// --- FINE CodMod.20071114 ------------------------------------------------------

	document.EMISSIONE.MinOraPart.value = document.EMISSIONE.OREDalSel.value;
	document.EMISSIONE.MaxOraPart.value = document.EMISSIONE.OREAlSel.value;
	MinAttesa = new String(document.EMISSIONE.MinimoV.value);	
	if (MinAttesa == '')  MinAttesa = "0";
	if (MinAttesa > 59) MinAttesa = "60";
	// --- CodMod.20080916 ---
//	switch (MinAttesa) {	
//		case '0'	: MinimoViaggio='00:00';
//		case '60' 	: MinimoViaggio='01:00';
//		default 	: MinimoViaggio = '00:' + MinAttesa;
//	}
	MinimoViaggio = MinAttesa;
	// --- FINE CodMod.20080916 ---
	
	document.EMISSIONE.MinimoV.value = MinimoViaggio;
	if (document.getElementById('DesLocPart').value  == document.getElementById('DesLocDest').value) 
	{
		alert('Le fermate di PARTENZA e DESTINAZIONE non devono essere la stessa!');
		return false;
	}	
	else{	
		//document.EMISSIONE.submit();
		return true;
	}
}

function CaricaComuniDefault(){
	//document.getElementById('comunePartINI').value=''
	//document.getElementById('comuneArrINI').value=''
	document.getElementById('CAMBIOCOMUNE').value='1'
	document.EMISSIONE.submit();
}	
