var timehasbeenput = false;

function openLocation(location)
  {
	window.open("../popups/location.asp?cvePunto="+location,"","height=410,width=520,left=270,top=90,scrollbars=yes")
  }

//despliega edades de los niños
function showAges(c){
	var box
	for(i=1;i<=c;i++){
		eval("box = document.getElementById('childBox"+i+"')")
		box.style.display='block'
	}
	
	for(;i<=6;i++){
		eval("box = document.getElementById('childBox"+i+"')")
		box.style.display='none'
	}
	
	if(c>=1){
		document.getElementById('childrenBox').className='childrenBoxShw'
		document.getElementById('agesTitle').className='childrenBoxShw'
	}else {
		document.getElementById('childrenBox').className='childrenBoxHid'
		document.getElementById('agesTitle').className='childrenBoxHid'
	}
	
}

//Envia los parametros del dia para recibir el xml
function sendTime(){

	var idTour = document.getElementById('cveTour').value;
	//var dtTime = document.getElementById('cmbTime2').value;
	var sFecha = document.getElementById('Datep').value;
	var url = '/tours/cart/tour_process.asp?';	
	var ins = 'd';
	pars = 'id=' + idTour + "&i=" + ins + "&f=" + sFecha;
	

		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}

		req.onreadystatechange = processRequest2;
		req.open("GET", url+pars, true);
		req.send(null);

}

//Checa que el xml esté bien
function processRequest2() {
		if (req.readyState == 4) {
			if (req.status == 200) {
			  getPoints();
			} else {
			  print (TOU_CART_ERROR_AJAX);
			}
		}
}


//Obtiene los puntos del xml

function getPoints(){
 document.getElementById('hourTable').style.visibility = 'hidden';
  document.getElementById('timeTable').style.visibility = 'hidden';
 document.getElementById('peopleTable').style.visibility = 'hidden';
 document.getElementById('meetTable').style.visibility = 'hidden';
 
 //Prepara los radio buttons
 var divRadios = document.getElementById('radioBtns');
 divRadios.innerHTML = "";

 while(divRadios.hasChildNodes()){
 	divRadios.removeChild(divRadios.firstChild);
 }	
 
  //Muestra los radio buttons
 document.getElementById('meetTable').style.visibility = 'visible';

 response2  = req.responseXML.documentElement;
 putDuration(0); 
	if (response2){

		var radioButtons = document.getElementById('radioBtns');
		var esLaUltima = false;		
		var sDuracion = "";
		var sHoraHasta = "";
		var sName = "";
		var sCve = "";
		var responseLen = response2.childNodes.length;
		var arrayPoints = new Array(responseLen);
		var arrayLen = 0;
		if(responseLen>0){
			for (i = 0; i<responseLen; i++)
			{
				if (i == responseLen && responseLen>1){
					esLaUltima = true;
				}
				//popula variables con los meeting points
				else {
					sCve = response2.getElementsByTagName('cve_pto')[i].firstChild.data;
					sName = response2.getElementsByTagName('punto')[i].firstChild.data;
					sHoraHasta = response2.getElementsByTagName('horahasta')[i].firstChild.data;
}
				
				var repetida = false;
				for(var j=arrayLen-1; j>=0 && !repetida; j--){
					if (sCve == arrayPoints[j])
						repetida = true
				}
				
				if(!repetida){
					arrayPoints[arrayLen]=sCve;
					arrayLen++;
					//Crear radio button
					try{
						//Para IE6
						aE2 = document.createElement('<input type="radio" name="cmbMeet" />');
					}catch(err){
						var aE2 = document.createElement("input");
						aE2.setAttribute("type", "radio");
						aE2.setAttribute("name", "cmbMeet");			
					}
					
					
					aE2.setAttribute("value", sCve);
					aE2.setAttribute("id", "cmbMeet"+i);
					
					
					aE2.setAttribute("onClick", "putDuration(this.id.substring(7,8));  getMeetingTimes(this.value);");			
					
					if (responseLen == 1 || sCve == carrito_Punto) {
						aE2.setAttribute("checked", "checked");
						aE2.defaultChecked  = true;
					}
	
					//Agegar radio button
					radioButtons.appendChild(aE2);	
		
					//Crear span para nombre del meeting place
					var sNameSpan = document.createElement("span");
					sNameSpan.setAttribute("name", "cmbMeet");
					sNameSpan.setAttribute("id", "cmbMeetAt"+i);
					sNameSpan.innerHTML = " "+ sName + " ";
					
					//Agregar Span
					radioButtons.appendChild(sNameSpan);
		
					//Crear link a mapa de meeting place
					var newanchor = document.createElement("a");
					newanchor.setAttribute("name", "radio"+i);
					newanchor.setAttribute("id", "radio"+i);
					newanchor.setAttribute("href", "javascript:openLocation('" + sCve + "')");
					
					linktext = document.createTextNode(TOU_CART_CLICK);
					newanchor.appendChild(linktext);
		
		
					//Agregar link
					radioButtons.appendChild(newanchor);			
		
					//Agregar salto de linea
					if(!esLaUltima) radioButtons.innerHTML += ' <br/>';						
				}
				
				//Dejar la hora de regreso grabada en la forma
				document.getElementById('cmbTimeBack').value = sHoraHasta;
				document.getElementById('cmbTimeDur').value = sDuracion;

				
				
				
			}
				if(editar_de_carrito) {
					getMeetingTimes(carrito_Punto);
				}else if(arrayLen==1) {
					aE2 = document.getElementById('cmbMeet0');
					aE2.setAttribute("checked", "checked");
					aE2.defaultChecked  = true;
					getMeetingTimes(sCve);
				}
		}
		
	}

	if(responseLen==0){
		alert(TOU_CART_ERROR_NO_INFO);
	}

}

function getMeetingTimes(sCve_Pto){	

		var timeDropDown = document.getElementById('cmbTime');
		if (timeDropDown){
			emptyCmb(timeDropDown);
		}else {
			var objDiv = document.getElementById('divTime')
			objDiv.innerHTML = "";
			objDiv.style.display = 'none';
		}		
		//document.getElementById('radioBtns').innerHTML = "";
		document.getElementById('hourTable').style.visibility = 'hidden';
		document.getElementById('timeTable').style.visibility = 'hidden';
		document.getElementById('peopleTable').style.visibility = 'hidden';
		document.getElementById('sbmt').style.visibility = 'hidden';
		
		var idTour = document.getElementById('cveTour').value;
		var sFecha = document.getElementById('Datep').value;
		var dtTime = document.getElementById('cmbTime2').value;
		var ins = 'c';
		var pto = sCve_Pto;
	
		var url = '/tours/cart/tour_process.asp?';
		var pars = 'id=' + idTour + "&i=" + ins + "&f=" + sFecha + "&p="+pto+"&t=" + dtTime ;

//alert(url)
//alert(pars)
		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.onreadystatechange = processRequest;
		req.open("GET", url+pars, true);
		req.send(null);
		
	}
	
function processRequest() {
		if (req.readyState == 4) {
			if (req.status == 200) {
			  getTimes();
			} else {
	  alert (TOU_CART_ERROR_AJAX);
			}
		}
	}
	

function getTimes() {
		response  = req.responseXML.documentElement;
		if (response.getElementsByTagName('tour')){
			oStartTime = response.getElementsByTagName('horade');//[1].firstChild.data;
			oEndTime = response.getElementsByTagName('horahasta');//[1].firstChild.data;
			oDuration = response2.getElementsByTagName('horadiff');
			
			var i
			var timeDropdown = document.getElementById('cmbTime');
//alert(timeDropdown)					
			if(!timeDropdown){
//alert("inside")					
				var d = document.getElementById('divMTime');
			    var timeDropdown = document.createElement("SELECT");	
				timeDropdown.setAttribute('id', 'cmbTime');
				timeDropdown.setAttribute('name', 'cmbTime');
				timeDropdown.setAttribute('class', 'cmbTime');
				timeDropdown.setAttribute('onchange', 'javascript:putDuration(cmb);');
				d.insertBefore(timeDropdown,d.firstChild);	
//				d.appendChild(oSelect);
//alert("new select!")				
			}

//			addItemCmb(timeDropdown,'','-- Select a time --';

			var oOption = document.createElement("OPTION");
          oOption.text=TOU_CART_SELECT_TIME;
          oOption.value='';
          timeDropdown.options.add(oOption); 

check = 1;


			if(oStartTime.length<1){
				alert(TOU_CART_ERROR_NO_PRICES);
			}else {
			for (i=0;i<oStartTime.length;i++)
			{
				if (oStartTime[i].firstChild) {
				   bVal=isCmbItem(timeDropdown, oStartTime[i].firstChild.data)
				   if (bVal==false) addItemCmb(timeDropdown,oStartTime[i].firstChild.data,oStartTime[i].firstChild.data, i);
				}
				
			}			
			
				chkShowObj(timeDropdown,document.getElementById('divTime'));
				if(editar_de_carrito) showPeople();
			}
			


			
		} else {
			alert(TOU_CART_ERROR_NO_INFO);
		}
	}


function showPeople(){

var timeDropdown = document.getElementById('cmbTime');	
if(timeDropdown){
	document.getElementById('cmbTime2').value = timeDropdown.options[timeDropdown.selectedIndex].value;
	cmb = timeDropdown.options[timeDropdown.selectedIndex].id;

}else {
	var divTime = document.getElementById('divTime');
	document.getElementById('cmbTime2').value = divTime.innerHTML;
}

if (!timehasbeenput) {
		putDuration(1);
		}
//document.getElementById('meetTable').style.visibility = 'hidden';	
			 document.getElementById('timeTable').style.visibility = 'visible';
document.getElementById('peopleTable').style.visibility = 'visible';
document.getElementById('sbmt').style.visibility = 'visible';

}
//*********** Funciones Auxiliares

	function addItemCmb(cmb, value, text, id)
	  { 
		if (cmb.name=="cmbTime" || cmb.name=="cmbTimeBack")
	           {
			    text=new Date(text);
				mins=text.getMinutes();
				if (mins=='0') mins='00';
                hours=text.getHours()
				if (hours>12) {
				   hours=(hours - 12) 
				   mins=mins + " pm"}
				else
					if (hours==12) {
					mins=mins + " pm"
					}
					else {
				    mins=mins+" am"
				    }
				   
				text=hours + ':' + mins
				}

	      var oOption = document.createElement("OPTION");
          oOption.text=text;
          oOption.value=value;
					oOption.id = id;

		  if(value == carrito_Time) {
			  oOption.defaultSelected = true;
			  oOption.selected = true; //IE6
		  }
          cmb.options.add(oOption); 
	  }  

    function  isCmbItem(cmb, value)
	  {
	     for (i=0;i<=cmb.options.length-1;i++)
		      if (cmb.options[i].value==value)
			     return true;
		   
		  return false;
	  }
     
	 function getCmbValue(cmb)
	  {
	    return cmb.value;
	  }

     function getCmbText(cmb)
	   {
	     return cmb.options[cmb.selectedIndex].text
	   }

	 function emptyCmb(cmb)
	  {
		cmb.options.length=0	  
/*	    while (cmb.options.length!=0) 
        cmb.options.remove(0);   
*/		
	  }

		function chkShowObj(objCmb,objDiv)
		  {
//alert(objCmb.length);		

			 if (objCmb.length==2)
	 	      {
				objCmb.style.display = 'none';
				objDiv.style.display = 'block'
			    val = objCmb.options[1].text;
				emptyCmb(objCmb);			   
			    objDiv.innerHTML=val;
				var d = document.getElementById('divMTime');
				d.removeChild(objCmb);
				document.getElementById('cmbTime2').value = val;
				showPeople();
			   }
			 else
			  {
				objCmb.style.display = 'block';
			   	objDiv.style.display = 'none';
			   } 
			 document.getElementById('hourTable').style.visibility = 'visible';

		  }
			
			function putDuration(i) {
				try{
				sDuracion = response2.getElementsByTagName('horadiff')[i].firstChild.data;
					document.getElementById('totalTime').innerHTML = sDuracion;

				} catch(err) {
				sDuracion = 'NA';
				}
				
       timehasbeenput = true;
			}




	
