// JavaScript Document
// <![CDATA[
var globalCallbacks = {
			onCreate: function(){$("status").style.display = "block";},
			onComplete : function(){
			if (Ajax.activeRequestCount == 0){
			$("status").style.display = "none";
			}
		}
}
/*// despues de esto hacer cualquier peticin con ajax por ejemplo:
function MiFuncionAjax(list_params){
var parametros = "parametros=" + list_params;
new Ajax.Request("script.php", {method: "post", parameters: parametros });
}*/

Ajax.Responders.register(globalCallbacks);

// ************************************************
// START LOADING EVENT HANDLERS AND AJAX REQUESTS.
// ************************************************

/*window.onload = function(){
var searchIt = $("botonbuscar");
//alert(searchIt);
searchIt.onclick = fetchSearchData;
//Effect.BlindUp('siforresults');
}*/

Event.observe(window, 'load', function(){
Event.observe('registrarse', 'click', fetchSearchData);
 //Event.observe('recuperar', 'click', recoverPass);
 });


// function to handle de recovery pass process.
function recoverPass(valuemail){
	//alert($("recovermail").value);
	$("generando").style.display = "block";
	$("generando").innerHTML="generando..."; // clear the content of container.
	var url = 'oop/ajax_control.php?mail='+valuemail;
	var params;
	params="recovery=1&recmail="+$("recovermail").value;

		new Ajax.Updater('generando', url, {
		method: "post",
		parameters:params
		
		})
	
	}


// *******************************
// function to fetch reg form data.
function fetchSearchData(quebusca){
//alert($("passreg").value);
//exit;
$("registerresults").update(); // clear the content of container.
Effect.BlindDown('registerresults');
var parametros;
parametros="nombres="+$("nombres").value+"&apellidos="+$("apellidos").value+"&email="+$("email").value+"&direccion="+$("direccion").value+"&telefono="+$("telefono").value+"&pass="+$("passreg").value+"&pass2="+$("pass2").value+"&code="+$("code").value+"&registrationform="+$("registrationform").value;

//alert(parametros);
//Effect.BlindUp('siforresults');

new Ajax.Updater('registerresults', 'oop/ajax_control.php', {
method: "post",
parameters:parametros
})

/*new Ajax.Request('oop/ajax_control.php', {
method: "post",
parameters: {tematica:$("tematica").value, especie:$("especie").value, departamento:$("deptos").value, titulo:$("titulotesario").value },
onSuccess: displayResults
})*/
}

/*// ******************
// function to select locations of specific origen and destiny countries.
function getDestinos(){
	
	// check that destino y origen are not 0
	if( $('paisorigen').value != '0' && $('paisdestino').value != '0' ){
		
	//$("gencorridas").style.display = "block";
	//$("gencorridas").innerHTML="generando..."; // clear the content of container.
	
	var url = 'oop/ajax_control.php';
	var params;
	params="destinos=1&elorigen="+$("paisorigen").value+"&eldestino="+$('paisdestino').value;

		//alert('hasta entra');
		$("destinoscontainer").style.display = "block";
		new Ajax.Request( url, {
		method: "post",
		parameters:params,	
		onCreate:showWorking('gencorridas'),
		onComplete:insertLocations
		})
	
	}else{ // end if statement to see pais destino y pais origen are not 0
	return false;
	}
	
	}*/
	

// ********************************************
// function to show all the results thru webservice
function mostrarcorridas(){
		
	// $("gencorridas").style.display = "block";
	// $("gencorridas").innerHTML="generando..."; // clear the content of container.
	
	var url = 'oop/ajax_control.php';
	var params;
	
	
	// check form field values
	if( $('ido').value == '' ){
			alert('Pais origen es requerido!');
			exit;
		}else if( $('idd').value == '' ){
			alert('Pais destino es requerido!');
			exit;
			}else if( $('fechasalida').value == '' ){
			alert('Fecha de salida es requerido!');
			exit;
			}
	
	// check the trip type
	if( $('tipoboleto0').checked == true ){
		//alert('chequeado 0');
		var tipoboleto = $('tipoboleto0').value;
		}else if( $('tipoboleto1').checked == true ){
			//alert('chequeado 1');
			var tipoboleto = $('tipoboleto1').value;
			}else{
				var tipoboleto = 0;
				alert('debe seleccionar el tipo de boleto');
				exit;
				}
	
	params="mostrarlas=1&localorigen="+$("ido").value+"&localdestino="+$('idd').value+"&fechasalida="+$('fechasalida').value+"&tipoboleto="+tipoboleto;

		//alert('hasta entra');
		$("thefinalresults").style.display = "block";
		
		
		new Ajax.Request( url, {
		method: "post",
		parameters:params,	
		onCreate:showWorking('gencorridas'),
		onComplete:insertLast
		})
				
/*		new Ajax.Updater('thefinalresults', url, {
		method: "post",
		onCreate:showWorking('gencorridas'),
		parameters:params
})*/
		
}
	
	
	// *****************
	// funciton to check that seats are set
	function checktheseats(theform){
		
		with(theform){
			
		if( asientos.value == '' ){
			alert('Debe especificar cuantos asientos!!');
			return false;
			exit;
			}else{
				return true;
				}
		}// end with
		}
		
		
		
		// ******************
		// strip white spaces
		function stripwhite(cadena){
			return cadena.replace(/(^\s*)|(\s*$)/g,""); 
			}
	
			
			
			
			// ***********************
			// check detail values
			function checkdetailvalues(){
			var cantidadasientos = $('asientoscliente').value;
			var i;
			
			
			// - check the buyer data -
			if( stripwhite( $('quiencompra').value ) == '' ){
					alert('Nombre Quien compra es requerido!');
					return false;
				}else if( stripwhite( $('quiencompraapellidos').value ) == '' ){
					alert('Apellidos Quien compra es requerido!');
					return false;
				}else if( stripwhite( $('emailquiencompra').value ) == '' || !echeck($('emailquiencompra').value) ){
					alert('e-mail Quien compra es requerido y debe ser valido!');
					return false;
				}else if( stripwhite( $('paisquiencompra').value ) == '' ){
					alert('País Quien compra es requerido!');
					return false;
				}else if( stripwhite( $('telquiencompra').value ) == '' ){
					alert('Teléfono Quien compra es requerido!');
					return false;
					
			}
			
			
			
			// check in this for that all passanger data is set
			for( i=0;i<cantidadasientos;i++ ){
				//alert(i);
				if( stripwhite($('no_asiento['+i+']').value) == '' || stripwhite($('pa_nombre['+i+']').value) == '' || stripwhite($('pa_apellido['+i+']').value) == '' || stripwhite($('pa_nacionalidad['+i+']').value) == '' || stripwhite($('pa_pasaporte['+i+']').value) == '' ){
					alert('Datos de los pasajeros son obligatorios');
					return false;
					
				}
				
			}
				return true;
			// alert(cantidadasientos);
			// return false;
			
			}
	
	
		
// *************
// function to insert the locations to container
function insertLast(transport){
		hideWorking('gencorridas');
		//alert(transport.responseText);
		$('thefinalresults').innerHTML = transport.responseText;
		}
		
		
		
	
	// *************
	// function to insert the locations to container
	function insertLocations(transport){
		hideWorking('gencorridas');
		//alert(transport.responseText);
		$('destinoscontainer').innerHTML = transport.responseText;
		}
	

	// *************
	// function to handle the show and hide of the working......
	function showWorking(theelement){
		$(theelement).style.display = "block"
		}
		
	
	// *************
	// this function hides the container of an specific message of an ajax action.
	function hideWorking(theelement){
		$(theelement).style.display = "none"
		}
		

// function applyOpenFile(){
//// Open a modal window with an iframe page inside, and assign the result to a global variable called "emailwindow"
//divwin=dhtmlmodal.open('basicdata', 'iframe', 'comentario.php?d=<?php echo($searchingAssoc['docid']); ?>', 'SIFOR-NOROC archivos', 'width=450px,height=370px,left=230px,top=80px,resize=1,scrolling=1');
//
//emailwindow.onclose=function(){
//var theform=this.contentDoc.forms[0] //Access first form inside iframe for demo purposes
//var theemail=this.contentDoc.getElementById("correo") //Access form field with id="emailfield"
//if (theemail.value.indexOf("@")==-1){ //crude check for invalid email
//alert("Favor introduzca un correo valido")
//return false //cancel closing of modal window
//}
///*else{ //else if this is a valid email
//document.getElementById("youremail").innerHTML=theemail.value //Assign the email to a span on the page
//return true //allow closing of window
//}*/
//}
//}

//function displayResults(respuesta){
//	//alert(respuesta.responseXML)
//			var name="";
//			var descrip="";
//			var xmlDoc=respuesta.responseXML;
//			//alert(xmlDoc);
//			var elDocRoot=xmlDoc.getElementsByTagName("tesarios")[0];
//			
//			//alert(elDocRoot.childNodes.length);
//			
//			/*if (elDocRoot){
//			attrs=elDocRoot.attributes;
//			docid=attrs.getNamedItem("id").value;
//			var autor=attrs.getNamedItem("autor").value;
//			}*/
//			
//			for(var i=0;i<elDocRoot.childNodes.length;i++){
//			elChild=elDocRoot.childNodes[i];
//			
//			if (elChild.nodeName=="titulo"){
//			descrip+="<li>"+elChild.firstChild.data+"</li>\n";
//			}
//			} // end of for
//			
//			alert(descrip);
//			
//			if (elChild.nodeName=="info"){
//			descrip+="<li>"+elChild.firstChild.data+"</li>\n";
//			}
//			
//			
//			
//		
//			
//	
//	}

/*function formatFetchedData(){

}*/


// *******************************************************************
// ALL THIS BELOW IS RELATED TO BOLETOSDEBUS.COM SHOPBOLETOS ORDER PROCESSING.
// *******************************************************************


// function with the seats
// ************
// function to add the input from FORM
function seleccionar(val){
//alert(val);

	for( i=0;i<$('asientoscliente').value;i++ ){

		if( $('no_asiento['+i+']').present() == false  ){
		//alert('si hay vacios');
		$('no_asiento['+i+']').value = val;
		$('ca').insert('<input type="hidden" name="losasientos[]" id="losasientos'+val+'" value="'+val+'" />');
		return;
		}
	}
}


// ************
// function to delete the input from FORM
function limpiar(val){
//alert(val);

	for( i=0;i<$('asientoscliente').value;i++ ){
	
		if( $('no_asiento['+i+']').value == val ){
			$('no_asiento['+i+']').value = '';
			$('losasientos'+val).remove();
			return;
		}
	}
}



function setFocus(ctl,sel){
	var obj = document.getElementById(ctl);
	obj.focus();
	if(Boolean(sel))
		obj.select();
}


// ************************
// validate email process
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("E-mail no es valido")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("E-mail no es valido")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("E-mail no es valido")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("E-mail no es valido")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("E-mail no es valido")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("E-mail no es valido")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("E-mail no es valido")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Profavor ingrese su E-mail")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 

//// ***************************
//// this is for the autosuggest
//var options1 = {
//	script: "oop/ajax_control_suggest.php?",
//	varname: "input1",
//	json: false,
//	maxresults: 20,
//	callback: function (obj) { document.getElementById('ido').value = obj.id_pais; }
//};
//
//var as1 = new bsn.AutoSuggest('paisorigen', options1);
//
//var options2 = {
//	script: "oop/ajax_control_suggest.php?",
//	varname: "input2",
//	json: false,
//	maxresults: 35,
//	callback: function (obj) { document.getElementById('idd').value = obj.id_pais; }
//};
//
//
//var as2 = new bsn.AutoSuggest('paisdestino', options2);




//]]>