/** 
 *	Ajax Requests
 *
 *	@author Andrea Gatti
 *	@version 0.6
 *
 *	Requirements:
 *		AjaxLib.js
 */

//------------------------------------------------------------------------------
// Combo operation
//------------------------------------------------------------------------------

	// Set Combo line -> "Tutte"
	function funResetComboLine(comboLine){
		var updateLine = false;
		var nCombo = funGetElementById(comboLine);
		for (i=0; i<(nCombo.length); i++){
			if (i == 0) { 
				if (nCombo.options[i].selected == false) {
					nCombo.options[i].selected = true;
					updateLine = true;
				} else {
					updateLine = false;
				}
			} else { nCombo.options[i].selected = false; }
		}
		// Load "tutte"
	}
	
	//--------------------------------------------------------------------------
	
	// Load options of the node's combo
	function funSetComboNode(comboLine, comboOutput, comboOutputCopy) {
	//<!--
		lineCombo = funGetElementById(comboLine);
		for (i=0; i<(lineCombo.length); i++){
			if(lineCombo.options[i].selected){
				statusOk = ajax_post("NodesSelector","line="+lineCombo[i].value+"&comboBox="+comboOutput , comboOutput, comboOutputCopy); // Update via direct modification
				statusOk = ajax_postExec("NodesSelector","line="+lineCombo[i].value+"&comboBox="+comboOutput+"&comboBoxCopy="+comboOutputCopy+"&blnExec=true"); // Update via DOM
				//if (comboOutputCopy!=null){statusOk = ajax_postExec("NodesSelectorDOM","line="+lineCombo[i].value+"&comboBox="+comboOutputCopy);} // Update via DOM
				i=lineCombo.length; // force exit
			}
		}
	//-->
	}
	

//------------------------------------------------------------------------------
// Capture operations
//------------------------------------------------------------------------------
	
	/* Get from google maps a node, and update combo box */
	function askNode(xCoord, yCoord, comboLine, comboNode){
		var parameters = "xCoord=" + xCoord + "&yCoord=" + yCoord;
			
		// Set Combo line -> "Tutte"
		//funResetComboLine(comboLine);
		//funSetComboNode(comboLine, comboNode);
		
		funAjaxGoogleGetNode("GetNode",parameters, comboNode);
		return;
	}

	//---------------------------------------------------------------------------
	
	/* Read the id of the nearest node (parameters: coord x,y) */
	function funAjaxGoogleGetNode(processor, parameters, comboNode) {
	<!--
		var ajax = funGetXMLHttpRequest();
		// check if I can use Ajax
		if(ajax) {
			// I can use Ajax ;-)
			ajax.open("post", processor, true); // Open a post request
			ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("Content-length", parameters.length);
			ajax.setRequestHeader("Connection", "close");// Send Request
			// Send Request
			ajax.send(parameters); // Set parameters to send
			
			funAppear('processing',0.0);
			ajax.onreadystatechange = function() {
				// Check status
				if(ajax.readyState === readyState.LOADED) {
					new Effect.Fade('processing',{ delay:0.0, duration:0.2 })
					if(statusText[ajax.status] === "OK") {
						// Operation OK :-)
						xml = ajax.responseXML;
						
						// Read parameters from  xml
						v = xml.getElementsByTagName("id").item(0).firstChild.nodeValue;

						// set combo box to the right node
						var nCombo = funGetElementById(comboNode);
						for (i=0; i<(nCombo.length); i++){
							if(nCombo.options[i].value === v){
								nCombo.options[i].selected = true;
							}else { nCombo.options[i].selected = false; }
						}
						funOnNodeSelected(); // EventManager.js
					} else {
						// error
						funError(ajax.status + ": " + statusText[ajax.status] + " - " + parameters + " - " + processor);
					}
				}
			}
		}
		return ajax;
	//-->
	}
 
//------------------------------------------------------------------------------
// Start Search operation
//------------------------------------------------------------------------------

	/** 
	 * Submit Search
	 * Parameters:
     *    - sNode = id of the start node's combobox 
     *    - eNode = id of the arrive node's combobox
     *    - time = id of the time's inputbox
     *    - blnStartTime = tipe of search: 
	 *								- true  = from start
	 *								- false = from arrive
     * Returns:	
	 */
	function funSubmitSearch(sNode, eNode, time, blnStartTime, intAlgorithm){
		var parameters = "";
		var timeBox;
		
		// Read start and end node
		var nCombo = funGetElementById(sNode);
		for (i=0; i<(nCombo.length); i++){
			if(nCombo.options[i].selected){
				parameters = 'partenza='+nCombo.options[i].value;
			}
		}
		nCombo = funGetElementById(eNode);
		for (i=0; i<(nCombo.length); i++){
			if(nCombo.options[i].selected){
				parameters += '&arrivo='+nCombo.options[i].value;
			}
		}
		
		// Read time
		timeBox = funGetElementById(time);
		t = timeBox.value.replace(/:/g,"%3A");
		parameters += '&orario=' + t;
		parameters += '&blnStart=' + blnStartTime;
		parameters += '&intAlg=' + intAlgorithm;
		// Call ajax request
		// first parameter: servlet processor 
		ajax_postExec("CallAlgorithms",parameters, 'result');
		return;
	}