function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function getRegions(){

	colexp('locations', 'colapse');

	//oculto el paso 2 - Horarios
	colexp('tit_2', 'colapse');
	colexp('dat_2', 'colapse');
	colexp('calendario', 'colapse');
	document.formulario.date.value = "";
	colexp('schedules', 'colapse');
	//--------------------------------
	colexp('comments', 'colapse');
	//oculto el paso 3 - Servicios
	colexp('tit_3', 'colapse');
	colexp('dat_3', 'colapse');
	radiosEnBlanco();
	//--------------------------------	
	//oculto el paso 4 - Login
	colexp('tit_4', 'colapse');
	colexp('dat_4', 'colapse');
	colexp('bot', 'colapse');
	//--------------------------------	
	
	divResultado = document.getElementById('regions');
	
	id_state = document.formulario.id_state.options[document.formulario.id_state.selectedIndex].value;
	
	ajax=objetoAjax();
	ajax.open("GET", "mobile-regions.php?id_state="+id_state);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)	
	
}

function getLocations(){

	colexp('locations', 'expand');
	
	//oculto el paso 2 - Horarios
	colexp('tit_2', 'colapse');
	colexp('dat_2', 'colapse');
	colexp('calendario', 'colapse');
	document.formulario.date.value = "";
	colexp('schedules', 'colapse');
	//--------------------------------
	colexp('comments', 'colapse');
	//oculto el paso 3 - Servicios
	colexp('tit_3', 'colapse');
	colexp('dat_3', 'colapse');
	radiosEnBlanco();
	//--------------------------------	
	//oculto el paso 4 - Login
	colexp('tit_4', 'colapse');
	colexp('dat_4', 'colapse');
	colexp('bot', 'colapse');
	//--------------------------------	
	
	divResultado = document.getElementById('locations');
	
	//id_state = document.formulario.id_state.options[document.formulario.id_state.selectedIndex].value;
	id_region = document.formulario.id_region.options[document.formulario.id_region.selectedIndex].value;
	
	ajax=objetoAjax();
	//ajax.open("GET", "mobile-locations.php?id_state="+id_state);
	ajax.open("GET", "mobile-locations.php?id_region="+id_region);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)	
	
}

function getDatesAux(){
	id_loc = document.formulario.id_location.options[document.formulario.id_location.selectedIndex].value;
	
	if( id_loc > 0 ){
		getDates(id_loc);
	}else{
		//muestro el paso 2 - Horarios
		colexp('tit_2', 'colapse');
		colexp('dat_2', 'colapse');
		colexp('calendario', 'colapse');
		document.formulario.date.value = "";
		colexp('schedules', 'colapse');
		//colexp('schedules_aux', 'colapse');
		//--------------------------------
		colexp('comments', 'colapse');
		//oculto el paso 3 - Servicios
		colexp('tit_3', 'colapse');
		colexp('dat_3', 'colapse');
		radiosEnBlanco();
		//--------------------------------
		//oculto el paso 4 - Login
		colexp('tit_4', 'colapse');
		colexp('dat_4', 'colapse');
		colexp('bot', 'colapse');
		//--------------------------------	
	}
}

function getDates(id_location){
	
	document.formulario.location.value = id_location;	
		
	//muestro el paso 2 - Horarios
	colexp('tit_2', 'expand');
	colexp('dat_2', 'expand');
	colexp('calendario', 'expand');
	document.formulario.date.value = "";
	colexp('schedules', 'colapse');
	
	//--------------------------------
	colexp('comments', 'colapse');
	//oculto el paso 3 - Servicios
	colexp('tit_3', 'colapse');
	colexp('dat_3', 'colapse');
	radiosEnBlanco();
	//--------------------------------
	//oculto el paso 4 - Login
	colexp('tit_4', 'colapse');
	colexp('dat_4', 'colapse');
	colexp('bot', 'colapse');
	//--------------------------------
	
	//getYears();
	
	divResultado = document.getElementById('calendario');
	
	ajax=objetoAjax();
	ajax.open("GET", "mobile-booking-calendar.php?loc="+id_location);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.send(null);
	
	document.location = 'mobile-booking.php#st2';
}

function calendario(loc, anio, mes){

	divResultado 	= document.getElementById('calendario');	
	sel 			= document.formulario.date.value;
	
	ajax=objetoAjax();
	ajax.open("GET", "mobile-booking-calendar.php?loc="+loc+"&ano="+anio+"&mes="+mes+"&sel="+sel);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)
}

function fijarFecha(fecha){
	//dia seleccionado actual	
	if( document.formulario.date.value != ""){
		fecha_sel = document.formulario.date.value;
		col = document.getElementById('dia_'+fecha_sel);		
		if( col ){			
			col.style.backgroundColor = "#d3e3fd"; 
		}
	}
	
	//nuevo dia seleccionado	
	document.formulario.date.value = fecha;	
	col = document.getElementById('dia_'+fecha);	
	col.style.backgroundColor = "#88b2f5"; // this shows the next line works

	//--------------------------------
	colexp('comments', 'expand');
	//oculto el paso 3 - Servicios
	colexp('tit_3', 'colapse');
	colexp('dat_3', 'colapse');
	radiosEnBlanco();
	//--------------------------------
	//oculto el paso 4 - Login
	colexp('tit_4', 'colapse');
	colexp('dat_4', 'colapse');
	colexp('bot', 'colapse');
	//--------------------------------	
}

function getSchedules(){
	
	divResultado = document.getElementById('schedules');

	date = document.formulario.date.value;
	
	id_location = document.formulario.id_location.options[document.formulario.id_location.selectedIndex].value; //document.formulario.location.value;

	if( date!="" && date!=null ){	
		ajax=objetoAjax();
		ajax.open("GET", "mobile-schedules.php?id_location="+id_location+"&date="+date);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				colexp('schedules', 'expand');
				divResultado.innerHTML = ajax.responseText
			}
		}
		ajax.send(null);
	}else{
		alert("Please, select the date");	
	}	
	
	//muestro la caja de comentarios
	//colexp('comments', 'expand');
	
	document.location = 'mobile-booking.php#st22';
}

function getServices(){
	/*
	sel = document.formulario.date.value;
	
	if( sel != "" ){
		//muestro el paso 3 - Servicios
		colexp('tit_3', 'expand');
		colexp('dat_3', 'expand');
		//--------------------------------
	}else{
		//oculto el paso 3 - Servicios
		colexp('tit_3', 'colapse');
		colexp('dat_3', 'colapse');
		radiosEnBlanco();
		//--------------------------------		
		//oculto el paso 4 - Login
		colexp('tit_4', 'colapse');
		colexp('dat_4', 'colapse');
		colexp('bot', 'colapse');
		//--------------------------------		
	}
	
	document.location = 'mobile-booking.php#st3';
	*/
	
	divResultado 	= document.getElementById('services');
	id_location 	= document.formulario.id_location.options[document.formulario.id_location.selectedIndex].value; //document.formulario.location.value;
	sel 			= document.formulario.date.value;
	
	if( sel != "" ){
		//muestro el paso 3 - Servicios
		colexp('tit_3', 'expand');
		colexp('dat_3', 'expand');
		//--------------------------------
		//oculto el paso 4 - Login
		colexp('tit_4', 'expand');
		colexp('dat_4', 'expand');
		colexp('bot', 'expand');
		//--------------------------------	
		
		ajax=objetoAjax();
		ajax.open("GET", "mobile-services.php?id_location="+id_location);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				colexp('schedules', 'expand');
				divResultado.innerHTML = ajax.responseText
			}
		}
		ajax.send(null);
		
	}else{
		//oculto el paso 3 - Servicios
		colexp('tit_3', 'colapse');
		colexp('dat_3', 'colapse');
		radiosEnBlanco();
		//--------------------------------		
		//oculto el paso 4 - Login
		colexp('tit_4', 'colapse');
		colexp('dat_4', 'colapse');
		colexp('bot', 'colapse');
		//--------------------------------		
	}	
	document.location = 'mobile-booking.php#st3';	
}

function getLogin(){	
	/*
	//muestro el paso 3 - Servicios
	colexp('tit_4', 'expand');
	colexp('dat_4', 'expand');
	colexp('bot', 'expand');
	//--------------------------------
	
	document.location = 'mobile-booking.php#st4';
	*/
}


//des-selecciono todos los radiobutton de servicios
function radiosEnBlanco(){
	/*
	for (i=0;i<document.formulario.id_service.length;i++){
       	document.formulario.id_service[i].checked = false;
    } 
	*/
}
function borrarOpciones(obj){
	for (var i in obj.options){
		obj.remove(obj.options[i])
	}
}

function getNewuser(){
	colexp('formNewuser', 'expand');
	colexp('formLoguser', 'colapse');	
	document.formulario.formUser.value = 2;	
}
function getLoguser(){
	colexp('formNewuser', 'colapse');
	colexp('formLoguser', 'expand');	
	document.formulario.formUser.value = 1;	
}

function validar(f){
	
	var e = '';

	if( document.formulario.formUser.value == 2 ){
		e = chkNotNull(e, 'May not be empty', f.user_lastname, 'Lastname');
		e = chkNotNull(e, 'May not be empty', f.user_name, 'Name');		
		e = chkNotNull(e, 'May not be empty', f.user_email, 'E-Mail');
		e = chkEmail(e, 'Enter a valid email address', f.user_email, 'E-Mail');		
		e = chkNotNull(e, 'May not be empty', f.user_password, 'Password');		
		e = chkNotNull(e, 'May not be empty', f.user_phone, 'Phone');
		
		//if( document.formulario.dobYear.value=="0000" || document.formulario.dobMonth.value=="00" || document.formulario.dobDay.value=="00" ){
		if( document.formulario.dobMonth.value=="00" || document.formulario.dobDay.value=="00" ){
		 	e += '- Select the DOB.\n';
		}
		
	}else{
		e = chkNotNull(e, 'May not be empty', f.login_mail, 'E-Mail');
		e = chkEmail(e, 'Enter a valid email address', f.login_mail, 'E-Mail');	
		e = chkNotNull(e, 'May not be empty', f.login_pass, 'Password');			
	}
	
	if (e) {
		alert('There are errors in your form data:\n\n' + e);
		return false;
	}
	else{
		if( document.formulario.formUser.value == 2 ){
			validateNewuser(f);
			return false;
		}else{
			validateLogin(f);
			return false;
		}
	}
}

function validateLogin(f){	
	mail = f.login_mail.value;
	pass = f.login_pass.value;
	
	ajax=objetoAjax();
	ajax.open("GET", "mobile-login.php?mail="+mail+"&pass="+pass);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if( ajax.responseText == 0){
				alert("Sorry, your login was incorrect.");
				return false;
			}else{
				getConfirmation();
			}
		}
	}
	ajax.send(null);
}
function validateNewuser(f){	
	mail = f.user_email.value;
	pass = f.user_password.value;
	
	ajax=objetoAjax();
	ajax.open("GET", "mobile-newuser.php?mail="+mail+"&pass="+pass);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if( ajax.responseText == 0){
				alert("The  Mail ID you have choosen has already been taken. \nPlease try a different Mail.");
				return false;
			}else{
				//f.submit();
				getConfirmation();
			}
		}
	}
	ajax.send(null);
}

function getConfirmation(){
	
	selecService = false;
	//valido que haya seleccionado al menos un servicio
	for (i=0;i<document.formulario.id_service.length;i++){
       	if( document.formulario.id_service[i].checked ){
			selecService = true;
		}
	}
	
	if( selecService == true ){
		
		id_service = "";
	
		colexp('divForm', 'colapse');
		colexp('divConf', 'expand');	
		
		//-- RECUPERO LOS DATOS ------------------------------------------------------------------------------------------------------
		id_state 	= document.formulario.id_state.options[document.formulario.id_state.selectedIndex].value;
		id_region 	= document.formulario.id_region.options[document.formulario.id_region.selectedIndex].value;
		/*
		for (i=0;i<document.formulario.id_location.length;i++){
			if( document.formulario.id_location[i].checked ){
				id_location = document.formulario.id_location[i].value;
			}
		} 
		*/
		id_location = document.formulario.id_location.options[document.formulario.id_location.selectedIndex].value;
	
		date 		= document.formulario.date.value;
		id_schedule = document.formulario.id_schedule.options[document.formulario.id_schedule.selectedIndex].value;
		comentarios	= document.formulario.comments.value;	
		
		/*
		for (i=0;i<document.formulario.id_service.length;i++){
			if( document.formulario.id_service[i].checked ){
				id_service = document.formulario.id_service[i].value;
			}
		} 
		*/
		for (i=0;i<document.formulario.id_service.length;i++){
			if( document.formulario.id_service[i].checked ){
				if( id_service == ""){
					id_service = document.formulario.id_service[i].value;
				}else{
					id_service += "|"+document.formulario.id_service[i].value;
				}
			}
		} 
		document.formulario.arr_services.value = id_service;
		
		
		param = "&form_user="+document.formulario.formUser.value;
		if( document.formulario.formUser.value == 2 ){
				param += "&user_lastname="+document.formulario.user_lastname.value;
				param += "&user_name="+document.formulario.user_name.value;
				param += "&user_email="+document.formulario.user_email.value;
				param += "&user_password="+document.formulario.user_password.value;
				param += "&user_phone="+document.formulario.user_phone.value;				
				//param += "&dob="+document.formulario.dobYear.value+"-"+document.formulario.dobMonth.value+"-"+document.formulario.dobDay.value;			
				param += "&dob="+document.formulario.dobMonth.value+"-"+document.formulario.dobDay.value;			
				param += "&car_make="+document.formulario.car_make.value;	
				param += "&car_model="+document.formulario.car_model.value;	
				param += "&plate_number="+document.formulario.plate_number.value;	
		}else{
			param += "&login_mail="+document.formulario.login_mail.value;
			param += "&login_pass="+document.formulario.login_pass.value;		
		}
		
		//----------------------------------------------------------------------------------------------------------------------------
		
		//alert("id_state="+id_state+"&id_location="+id_location+"&date="+date+"&id_schedule="+id_schedule+"&id_service="+id_service+param);			
			
		divResultado = document.getElementById('divConf');
		ajax=objetoAjax();
		ajax.open("GET", "mobile-confirmation.php?id_state="+id_state+"&id_region="+id_region+"&id_location="+id_location+"&date="+date+"&id_schedule="+id_schedule+"&id_service="+id_service+"&comments="+comentarios+param);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				divResultado.innerHTML = ajax.responseText
			}
		}
		ajax.send(null);
		
		document.location = 'mobile-booking.php#st1';
	}else{
		alert("You have to select at least a service.");
		return false;
	}
}

function getForm(){
	colexp('divForm', 'expand');
	colexp('divConf', 'colapse');
	
	document.location = 'mobile-booking.php#st1';
}

function confirmForm(){
	document.formulario.submit();	
}