﻿function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		//var sel2 = document.getElementById(id2);
		//var next = document.getElementById("next");
		
		// Clone the dynamic select box
		var clone = sel1.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		//refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		
		sel1.onchange = function()
		{
			//alert('asd');
			var params = 'generarParades.php?param='+sel1.options[sel1.selectedIndex].value;
			new ajax(params, {update: $(id2)});
		}
		
	}
}
/* buida sel2 i l'omple amb els elements de clonedOptions que tinguin la class "select" o la class igual al valor seleccionat de sel1.
* 
* 
*/

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
		//sel3.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		//	sel3.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}

function check(origen, desti)
{	
	if (origen.options[origen.selectedIndex].text == desti.options[desti.selectedIndex].text) return false;
	if (origen.options[origen.selectedIndex].value == "select" || desti.options[desti.selectedIndex].value == "select") return false; 
	return true;
}

var pop = null;
window.onload = function()
{
	dynamicSelect("sorigen", "paradesDesti");
	var fmain = document.getElementById("fhoraris");
	var next = document.getElementById("next");
	
	fmain.onsubmit = function()
			 {
				 var origen = document.getElementById("sorigen");
				 var desti = document.getElementById("sdesti");
				// var shores = document.getElementById("shores");
				 //var ahores = document.getElementById("ahores");
				 var horari = "select";
				 var horari2 = "select";
				 var pHores = "";
				amagarEsteve();
				
				/*if (shores.checked)
				{
					pHores = "sortida";
				}
				else if (ahores.checked)
				{
					pHores = "arribada";
				}*/
				//modificacio per tal de que no busqui si no es fiquen les hores quan es marca hores sortida ho ariibada
			/*	if ((shores.checked||ahores.checked)){
					if((horari.options[horari.selectedIndex].value == "select")||(horari2.options[horari2.selectedIndex].value == "select")){
						alert("Has de seleccionar les hores de arribada i de sortida");
						return false;	
					}
					
				 }
				 else{
					 if((horari.options[horari.selectedIndex].value != "select")||(horari2.options[horari2.selectedIndex].value != "select")){alert("Has de seleccionar si son hores de arribada o de sortida");
					 return false;
					 }
					 }*/
				

				 //----------------------------------------------------------------

				 if (check(origen, desti))
				 {
					var params = 'horaris.php?origen='+origen.options[origen.selectedIndex].value+'&desti='+desti.options[desti.selectedIndex].value+'&hores='+pHores+'&horari='+horari+'&horari2='+horari2;   //+'&horari='+horari.options[horari.selectedIndex].value+'&horari2='+horari2.options[horari2.selectedIndex].value
					//alert (params);
					//new ajax(params, {update: $('ResultatsDinamics')});
					jQuery('#ResultatsDinamics').load(params);
					salto();
					
					
					return true;
				 }
				 alert("Sel·lecció invàlida.");
				 return false;
			 }
	
	document.getElementById("consultar").onclick = fmain.onsubmit;
	
	// ==================== El mateix pero amb el cercador de Sant Esteve: ==================== //
	
	dynamicSelect("sorigenEsteve", "paradesDestiEsteve");
	var fmain = document.getElementById("fhorarisEsteve");
	var next = document.getElementById("next");
	
	fmain.onsubmit = function()
			 {
				 var origen = document.getElementById("sorigenEsteve");
				 var desti = document.getElementById("sdestiEsteve");
				 //var shores = document.getElementById("shoresEsteve");
				 //var ahores = document.getElementById("ahoresEsteve");
				 var horari = "select";
				 var horari2 = "select";
				 var pHores = "";
				amagarLinies();
				
				/*if (shores.checked)
				{
					pHores = "sortida";
				}
				else if (ahores.checked)
				{
					pHores = "arribada";
				}*/
				//modificacio per tal de que no busqui si no es fiquen les hores quan es marca hores sortida ho ariibada
				/*if ((shores.checked||ahores.checked)){
					if((horari.options[horari.selectedIndex].value == "select")||(horari2.options[horari2.selectedIndex].value == "select")){
						alert("Has de seleccionar les hores de arribada i de sortida");
						return false;	
					}
					
				 }
				 else{
					 if((horari.options[horari.selectedIndex].value != "select")||(horari2.options[horari2.selectedIndex].value != "select")){alert("Has de seleccionar si son hores de arribada o de sortida");
					 return false;
					 }
					 }*/
				

				 //----------------------------------------------------------------

				 if (check(origen, desti))
				 {
					var params = 'horaris.php?origen='+origen.options[origen.selectedIndex].value+'&desti='+desti.options[desti.selectedIndex].value+'&hores='+pHores+'&horari='+horari+'&horari2='+horari2;
					//new ajax(params, {update: $('ResultatsDinamics')});
					jQuery('#ResultatsDinamics').load(params);
					salto();
					
					
					return true;
				 }
				 alert("Sel·lecció invàlida.");
				 return false;
			 }
	
	document.getElementById("consultarEsteve").onclick = fmain.onsubmit;
	
}

