
	
  /* #############################################################################
     #
	 #
	 #   Ajax Code here...
	 #   Global variable expected: remotescript
	 #
	 #
	 #############################################################################
   */



	function indexcode_loadCountryValues (namedropdown, statedropdown ){

		//remote_getDropDownTableRows( $table, $condition, $table_colkey, $table_colname, $table_selected 
		sajax_do_call("remote_getDropDownTableRows", new Array( namedropdown,statedropdown  ,'COUNTRY','','COUNTRY_ID','COUNTRY_NAME',-1,
															  	indexcode_onCountryReturned ), remotescript);
			
	}

	// Mode==1 if it will say 'any state' and 'any country'. Mode==2 if it will say choose country, choose state
	//  default1 is currently selected country
	//  default2 is currently selected state
	//  default3 is currently selected city
	function indexcode_loadCountryValuesDefaults ( namedropdown, default1, statedropdown, default2, citydropdown, default3, mode ){

		//remote_getDropDownTableRows( $table, $condition, $table_colkey, $table_colname, $table_selected 
		sajax_do_call("remote_getDropDownTableRowsDefaults", new Array( mode, namedropdown,default1,statedropdown,default2  ,citydropdown, default3,
																	   		'COUNTRY','','COUNTRY_ID','COUNTRY_NAME',-1,
															  	indexcode_onCountryReturnedDefault ), remotescript);
			
	}


	function indexcode_onCountryReturnedDefault(infoserver){
		
		//alert("onCountryReturned() received this from php " + infoserver );
		var results = infoserver.split("||");

		var mode = results[0];
		var namedropdown = results[1];
		var default1 = results[2];
		var statedropdown = results[3];
		var default2 = results[4];		
		var citydropdown = results[5];
		var default3 = results[6];		


		var dropdownobj = document.getElementById( namedropdown );
		
		if (!dropdownobj){
				//alert("JavaScript browser error: country not found: " + namedropdown);
		}
		
		utils_removeAllChilds( namedropdown);
		
		if ( mode == 1 ) {
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Any Country", -1 );
		}else{
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Choose Country", -1 );
		}
			
		
		//Parse options for 
		for( var m=7; m< results.length; m= m+2){
										
				indexcode_appendChildToDropDown ( dropdownobj , results[m], results[m+1], default1 );
					
		}
		if ( mode == 1 ) {
			indexcode_appendChildToDropDown ( document.getElementById(statedropdown)  , 0, "Any State", -1 );
		}else{
			indexcode_appendChildToDropDown ( document.getElementById(statedropdown)  , 0, "Choose State", -1 );
		}
		

		//initialize search the other dependent select
		indexcode_getStatesFromDefaults ( mode,  namedropdown , statedropdown,  default2, citydropdown, default3  );

		dropdownobj.onchange = function(){
			indexcode_getStatesFromDefaults ( mode, namedropdown , statedropdown,  default2, citydropdown, default3  );
		}
		

		
	}


	function indexcode_onCountryReturned(infoserver){
		
		//alert("onCountryReturned() received this from php " + infoserver );
		var results = infoserver.split("||");

		var namedropdown = results[0];
		var statedropdown = results[1];
		
		var dropdownobj = document.getElementById( namedropdown );
		
		if (!dropdownobj){
				//alert("JavaScript browser error: country not found");
		}
		
		utils_removeAllChilds( namedropdown);
		
		indexcode_appendChildToDropDown ( dropdownobj  , 0, "Choose Country", -1 );
		
		//Parse options for 
		for( var m=2; m< results.length; m= m+2){
										
				indexcode_appendChildToDropDown ( dropdownobj , results[m], results[m+1], -1 );
					
		}
		
		indexcode_appendChildToDropDown ( document.getElementById(statedropdown)  , 0, "Choose State", -1 );

		dropdownobj.onchange = function(){
			indexcode_getStatesFrom ( namedropdown , statedropdown);
		}
		

		
	}
	
	function indexcode_appendChildToDropDown ( dropdownobj, valueelement, nameelement, valueselected ){
		
			if ( dropdownobj ){
				
					var newelement = document.createElement("option" );				
					newelement.setAttribute("value", valueelement );
					newelement.appendChild( document.createTextNode( nameelement ) );
					
					if ( valueelement == valueselected ) {
							newelement.setAttribute("selected", true);
					}
					
					dropdownobj.appendChild( newelement );			
			}
			
	}


	function indexcode_getStatesFrom ( contrydropdownname, statedropdown ){
	
		//alert("will get states from " + contrydropdownname );
		
		var theparentcountry = document.getElementById(  contrydropdownname  );
		
		var thevalue = theparentcountry.value;
		
		sajax_do_call("remote_getDropDownTableRows", new Array( statedropdown, "none", 'STATE'," WHERE STATE_COUNTRY_ID="+thevalue,'STATE_ID','STATE_NAME',-1,
															  	indexcode_onStateReturned ), remotescript);
		
		
	}

	function indexcode_getStatesFromDefaults ( mode, contrydropdownname, statedropdown,  defaultstate, 
													  citydropdown, defaultcity
											  ){
	
		//alert("will get states from " + contrydropdownname );
		
		var theparentcountry = document.getElementById(  contrydropdownname  );
		
		var thevalue = theparentcountry.value;
		
		sajax_do_call("remote_getDropDownTableRowsDefaults", new Array( mode, statedropdown,  defaultstate ,
																	   citydropdown, defaultcity,
																	   "none", "none", 'STATE'," WHERE STATE_COUNTRY_ID="+thevalue,
																   	'STATE_ID','STATE_NAME',-1,
															  	indexcode_onStateReturnedDefaults ), remotescript);
		
		
	}


	function indexcode_onStateReturned (infoserver){
		
		//alert("onStateReturned() received this from php " + infoserver );
		var results = infoserver.split("||");

		var namedropdown = results[0];
		var associateddropdown = results[1];		
		
		var dropdownobj = document.getElementById( namedropdown );
		
		if (!dropdownobj){
				//alert("JavaScript browser error: state not found");
		}

		utils_removeAllChilds( namedropdown);

		indexcode_appendChildToDropDown ( dropdownobj  , 0, "Choose State", -1 );
		
		//Parse options for 
		for( var m=2; m< results.length; m= m+2){
										
				indexcode_appendChildToDropDown ( dropdownobj , results[m], results[m+1], -1 );
					
		}
		

	}

	function indexcode_onStateReturnedDefaults (infoserver){
		
		//alert("onStateReturned() received this from php " + infoserver );
		var results = infoserver.split("||");

		var mode = results[0];
		var namedropdown = results[1];
		var defaultvalue  = results[2];
		var  citydropdown = results[3];		
		var citydefault = results[4];
		var tertiarydropdown = results[5];		
		var defaultvalue3 = results[6];
		
		var dropdownobj = document.getElementById( namedropdown );
		
		if (!dropdownobj){
				//alert("JavaScript browser error: state not found for "  + namedropdown);
		}

		utils_removeAllChilds( namedropdown);
		
		if ( mode == 1) {
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Any State", -1 );
		}else{
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Choose State", -1 );
		}
		
		//Parse options for 
		for( var m=7; m< results.length; m= m+2){
										
				indexcode_appendChildToDropDown ( dropdownobj , results[m], results[m+1], defaultvalue );
					
		}
		

		if ( citydropdown != "" ){
			//initialize search the other dependent select
			indexcode_getCitiesFromDefaults ( mode,  namedropdown ,  citydropdown, citydefault );

			dropdownobj.onchange = function(){
				indexcode_getCitiesFromDefaults ( mode, namedropdown , citydropdown, citydefault  );
			}
			
		}

	}

	function indexcode_getCitiesFromDefaults( mode, statedropdown , citydropdown, citydefault  ){
		//alert("will get states from " + contrydropdownname );
		
		var theparentstate = document.getElementById(  statedropdown  );
		
		var thevalue = theparentstate.value;
		
		sajax_do_call("remote_getDropDownTableRowsDefaults", new Array( mode, citydropdown,  citydefault ,
																	   "none",  "none",
																	   "none", "none", 'CITY'," WHERE CITY_STATE_ID="+thevalue,
																   	'CITY_ID','CITY_NAME',-1,
															  	indexcode_onCityReturnedDefaults ), remotescript);
	}

	function indexcode_onCityReturnedDefaults( infoserver ){
		//alert("onCityReturned() received this from php " + infoserver );
		var results = infoserver.split("||");

		var mode = results[0];
		var namedropdown = results[1];
		var defaultvalue  = results[2];
		var secondarydropdown = results[3];		
		var secondarydefault = results[4];
		var tertiarydropdown = results[5];		
		var defaultvalue3 = results[6];
		
		var dropdownobj = document.getElementById( namedropdown );
		
		if (!dropdownobj){
				//alert("JavaScript browser error: state not found for "  + namedropdown);
		}

		utils_removeAllChilds( namedropdown);
		
		if ( mode == 1) {
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Any City", -1 );
		}else{
			indexcode_appendChildToDropDown ( dropdownobj  , 0, "Choose City", -1 );
		}
		
		//Parse options for 
		for( var m=7; m< results.length; m= m+2){
				
				//indexcode_appendChildToDropDown ( dropdownobj , results[m+1], results[m+1], defaultvalue );										
				//For cities i am using the city name as the value!!
				indexcode_appendChildToDropDown ( dropdownobj , results[m+1], results[m+1], defaultvalue );
					
		}
		


	}

	function validate_getValueTxtField( name ) {
		
			var anobj = document.getElementById (name );
			if ( ! anobj ) {
				alert("error with " +  name	 );				
			}
			return anobj.value;
	}
	
	
	
	
	function validateFormHomePage(){
		
			if ( validate_getValueTxtField("POST_TITLE") == "" ) {
				alert("Please provide the title");
				return false;
			}

			if ( validate_getValueTxtField("POST_FROMCOUNTRY_ID") == 0 || 
			     validate_getValueTxtField("POST_FROMSTATE_ID") == 0 ||
				validate_getValueTxtField("POST_FROMCITY") == "" ){
			
				alert("Please select the country, state and city for the 'leaving from' section");
				return false;
			}

			if ( validate_getValueTxtField("POST_TOCOUNTRY_ID") == 0 || 
			     validate_getValueTxtField("POST_TOSTATE_ID") == 0 ||
				validate_getValueTxtField("POST_TOCITY") == "" ){
			
				alert("Please select the country, state and city for the 'going to' section");
				return false;
			}

			//validate date
			
			var anytime = document.getElementById("POST_ANYTIME");
			
			if ( !anytime.checked ){
				var month =  validate_getValueTxtField('POST_DEPARTUREDATE_MONTH');
				var year =  validate_getValueTxtField('POST_DEPARTUREDATE_YEAR');
				var day =  validate_getValueTxtField('POST_DEPARTUREDATE_DAY');

				if ( utils_validateDate1( year, month, day, " for departure date" ) == false ){
						return false;
				}
		
			}
		
		
			if ( utils_validateRadioSelected( "POST_DRIVERMODE", "Please choose if you are a rider or driver") == false ){
					return false;
			}
		
			return true;
	}
	
	
	function submit_advancedform(){
		
		
		document.FORMSEARCH.action = "./search/advanced-search.php";
		//alert( "now the action is " + document.FORMSEARCH.action );
		
		document.FORMSEARCH.submit();
		
	}
