// plastonHome.js


function calendarInit() {
	//offset=-106;
	ini_cal  = new Epoch(null, 'epoch_popup','popup',document.getElementById('ini_date'));
	//ini_cal.calendar.style.top='40px';
}

function iniciaCalVuelta(){
	//offset=-106;
	fin_cal = null;

}

function callCalendar(){
try
{	
	//offset=-106;
	//ini_cal.calendar.style.top='10px';
	if(document.getElementById('ini_date').value!=""){
		document.getElementById('IdOrigen').style.display='none';
		document.getElementById('IdDestino').style.display='none';
		if(ini_cal.selectedDates[0]==null){
			var aux = document.getElementById('ini_date').value;
			var auxD =  parseInt(aux.substring(0,2),10);
			var auxM =  parseInt(aux.substring(3,5),10);
			var auxY =  parseInt(aux.substring(6),10);
			var auxDate = new Date();
			auxDate.setFullYear(auxY, auxM-1, auxD);
			ini_cal.selectedDates[0]=auxDate;
		}
		if(fin_cal==null){
			//if (browser==0)offset=37;
			fin_cal  = new Epoch(ini_cal.selectedDates[0], 'epoch_popup','popup',document.getElementById('fin_date'));
			var sTop = fin_cal.calendar.style.top;
			sTop = sTop.substring (0, sTop.length - 2);
			var sLeft = fin_cal.calendar.style.left;
			sLeft = sLeft.substring (0, sLeft.length - 2);
			//alert (sTop + "," + sLeft);
			fin_cal.calendar.style.top= '' + (parseInt (sTop) - 0) + 'px';
			fin_cal.calendar.style.left= '' + (parseInt (sLeft) - 0) + 'px';
		}
		fin_cal.goToMonth(ini_cal.selectedDates[0].getFullYear(),ini_cal.selectedDates[0].getMonth());	
	
		fin_cal.toggle();

		
	}else alert("Debe seleccionar primero una fecha de salida.")
		
}
catch (e){
  document.getElementById('IdOrigen').style.display='';
  document.getElementById('IdDestino').style.display='';
}
}

function callCalendar2(){ 
	try
	{
			document.getElementById('IdOrigen').style.display='none';
		    document.getElementById('IdDestino').style.display='none';
			//offset=-106;
			if(!ini_cal.visible) {
				calendarInit();	
				ini_cal.toggle();	
				if(fin_cal!=null){
					if(fin_cal.visible)fin_cal.toggle();
					fin_cal = null;
				}
				document.search.FechaVueltaSel.value = "";	
			}else{
				ini_cal.toggle();	
			}
		}
	catch (e){
					document.getElementById('IdOrigen').style.display='';
		    		document.getElementById('IdDestino').style.display='';
			}			
}

function doSubmit(){	

	if (document.search.IdOrigen.value == 0)
	{
		alert("Seleccione una estación de origen");	
		return;
		
	}else if (document.search.IdDestino.value == 0)
	{
		alert("Seleccione una estación de destino");	
		return;
		
	}

	var fechaIni = document.search.FechaIdaSel.value;
	var fechaFin = document.search.FechaVueltaSel.value;
	var fechaEval = new Fecha();
	var flagFechaEval = false;
	flagFechaEval = fechaEval.parseDate(fechaIni ,'dma');	

	if (fechaIni == ""){
		alert("Seleccione una fecha de ida");	
		return;
	}else if(!flagFechaEval){
		alert("Fecha de salida incorrecta");
		return;
	}	
	else if(document.search.adultos.value<=0){
		alert("Seleccione un pasajero");	
		return;
	}else if(parseInt(document.search.adultos.value)>9){
	    alert("Seleccione un número de pasajeros menor, el máximo número de pasajeros permitido es 9");			    				    			  	     
	    return;
    }
	checkRecorrido = obtenerValorRadio(document.search.iv);		
	if(checkRecorrido !=-1 && checkRecorrido=="iv"){
		// Hay vuelta
		if (fechaFin == ""){
			alert("Seleccione una fecha de regreso");			       
			return;
		}else if(!compFecha(fechaIni,fechaFin)){	
		      //parent.isSubmited = false;			      
		      alert("Las fechas son incorrectas");	
		      return;
		}
	}  
document.search.desOrigen.value =document.search.IdOrigen.options[document.search.IdOrigen.selectedIndex].text; 

document.search.desDestino.value =document.search.IdDestino.options[document.search.IdDestino.selectedIndex].text;

	
	document.search.submit();
}

function doDisableIda(){

//document.search.FechaVueltaSel.disabled = false;
document.getElementById("divFechaVuelta").style.display='';
//document.getElementById("divFechaVuelta2").style.display='none';
document.getElementById("auxDIV").style.top='8'
//parent.isSubmited = false;
}

function doDisableVuelta(){

document.getElementById("divFechaVuelta").style.display='none';
//document.getElementById("divFechaVuelta2").style.display='';
//document.search.FechaVueltaSel.disabled = true;
document.search.FechaVueltaSel.value = "";

//if(browser==0)document.getElementById("auxDIV").style.top='8'
//else document.getElementById("auxDIV").style.top='8'



//parent.isSubmited = false;
}

//function callCalendar(){
//if (!document.search.iv[0].checked)
//       fin_cal.toggle();
//}

//function callCalendar2(){    
//	ini_cal.toggle();
//}

function compFecha(fecha1,fecha2){
var ok = true;
Data1_arr = fecha1.split('-');
Data2_arr = fecha2.split('-');

	var fecha1 = new Fecha();
	var fecha2 = new Fecha();
	fecha1.setDate(Data1_arr[0],Data1_arr[1],Data1_arr[2]);
	fecha2.setDate(Data2_arr[0],Data2_arr[1],Data2_arr[2]);
	if(fecha2.compareTo(fecha1)==-1)
		ok = false;
		
return ok;		 		
}

function selDestino(){
	document.search.IdDestino.options.length = 0;
	var sel = document.search.IdOrigen.selectedIndex
	if(sel>0){
		sel=sel-1;
		var estDSel = estD[sel];
		for(i=0,j=estDSel.length;i<j;++i){
			document.search.IdDestino.options[i] = new Option(estDSel[i][1], estDSel[i][0]);
		}
	}else{	
		document.search.IdDestino.options[i] = new Option("&nbsp;Principales Estaciones Destino&nbsp;", "0");
	}
}


function obtenerValorRadio(radio)
{
	if (radio==null) return -1;
	var nTamanioRadio=radio.length;
	//En el caso de que radio.length==null, el objeto tiene sólo un elemento y no se trata como array
	if (radio.length==null)
		if(radio.checked)
			return radio.value;
		else return -1;
	else
	{
			var checq=false;
			var cont=0;
			
			while((cont<radio.length)&&(!checq))
			{
				checq=radio[cont].checked;
				cont++;
				
			}
			if (checq)
				return radio[cont-1].value;
			else return -1;
	}
	
}//obtenerValorRadio(radio)