/************************* Show Next Step  ***********************************/
//Shows the next section of the trip leg.
function showNextStep(legNum, sessionID)
{

	var container = $('nextStep'+legNum);
	var content = 'reserve_step2.php';
	
	//Populate the POST variables..
	var start = $('calcStartId'+legNum).getValue();
	var destination = $('calcEndId'+legNum).getValue();
	var vehicle = $('vehicle'+legNum).getValue();
	//window.alert(vehicle);
	var transferType = $('transferType'+legNum).getValue();
	var passengers = '';
	var tripType = 'OneWay';
	try{
	var passengers = $('passengers'+legNum).getValue();
	//var tripType = $('tripType'+legNum).getValue();
	}catch(err){
	
	}
	
	var dataString = 'start='+ start + '&destination=' + destination + '&vehicle=' + vehicle + '&transferType=' + transferType + '&leg=' + legNum + '&passengers=' + passengers+ '&tripType=' + tripType+'&PHPSESSID=' + sessionID;
	new Ajax.Updater(container, content, {  evalScripts: true, method: 'post', postBody: dataString, onComplete:function(){
		container.appear({ duration: 0.5, delay: 0.1 });
		}
	});
	
	
	//$('quote-reserve'+legNum).hide();
	$('legPrice'+legNum).show();
  
}



/************************* Add Trip Leg  ***********************************/
//Adds a trip leg via AJAX.
function addLeg(legNum)
{
	$('loader').show();
	var container = $('additionalLegs');
	var content = 'tripLeg.php';
	var dataString = 'count='+legNum;
	new Ajax.Updater(container, content, { method: 'post', postBody: dataString, insertion: 'bottom', evalScripts: 'true', onComplete:function(){
		$('tripLeg'+legNum).hide(); $('tripLeg'+legNum).appear({ duration: 0.8, delay: 0.1});  
		$('loader').hide();
		} 
	});

  
}

/************************* Update Grand Total  ***********************************/
//Updates the total price of all the trip legs.
function updateGrandTotal()
{
	var grandTotal = 0;
	$$('.legAmount').each(function(item){
		if(item.value != 0){
			grandTotal += parseInt(item.value);
		};
	});
	if (grandTotal == 0){
		$('tripTotal').innerHTML = '$0.00';
	}else{
		$('tripTotal').innerHTML = money(grandTotal);
	}
	
  
}

/************************* Gather Leg Posts  ***********************************/
//Goes through each trip leg and gets the POST variables for each form.
function gatherLegPosts()
{
	//Check to see if every trip leg has a total.
	var valid = true;
	$$('.legAmount').each(function(legTotal){
		if (legTotal.value == 0 || legTotal.value == ""){
			valid = false;	
		}
	});
	
	
	//If we have a valid trip, get the form values.
	if (valid){
		//window.alert('Gathering Results');
		var formResults = new Array();
		var dataString = '';
		
		var formHash = new Hash();
		var count = 1;
		$$('.step1Form').each(function(firstForm){
			formHash = formHash.merge(firstForm.serialize(true));
			$$('.step2Form').each(function(secondForm){
				formHash = formHash.merge(secondForm.serialize(true));
			});
			count++;
		});
		

		new Ajax.Request("processForm.php",
		{
			method: "post",
			parameters: formHash,
			onSuccess: function(transport) {
				//window.alert('Result: ' + transport.responseText);
				if (transport.responseText == 'failed' || transport.responseText == 'date'){
					if (transport.responseText == 'date'){
						window.alert('Please make sure your departure date is after your arrival date.');
					}else{
						window.alert('Error submitting data');
					}
				}else{
					$('goNextStep').submit();
				}
			},
			onFailure: function(transport) {
				window.alert('Failed: ' + transport.responseText);
			}
		});
		
		//window.alert('finished');
		
		
	}else{
		customAlert('Please make sure your trip is valid and none of the sections are empty.')
		
	}
	
}




/************************* Gather Leg Posts  ***********************************/
//Goes through each trip leg and gets the POST variables for each form.
function gatherLegPosts2()
{
	//Check to see if every trip leg has a total.
	var valid = true;
	$$('.legAmount').each(function(legTotal){
		if (legTotal.value == 0 || legTotal.value == ""){
			valid = false;	
		}
	});
	
	//If we have a valid trip, get the form values.
	if (valid){
		//window.alert('Gathering Results');
		var formResults = new Array();
		var dataString = '';
		
		var formHash = new Hash();
		var count = 1;
		$$('.step1Form').each(function(firstForm){
			formHash = formHash.merge(firstForm.serialize(true));
			$$('.step2Form').each(function(secondForm){
				formHash = formHash.merge(secondForm.serialize(true));
			});
			count++;
		});
		
		
		//var sessID = $F('PHPSESSID');

		new Ajax.Request("processForm.php",
		{
			method: "post",
			parameters: formHash,
			onSuccess: function(transport) {
				//window.alert('Result: ' + transport.responseText);
				if (transport.responseText == 'failed'){
					window.alert('Error submitting data');
				}else{
					window.alert('Result: ' + transport.responseText);
					$('goNextStep').submit();
				}
			},
			onFailure: function(transport) {
				//window.alert('Failed: ' + transport.responseText);
			}
		});
		
		//window.alert('finished');
		
		
	}else{
		customAlert('Please make sure your trip is valid and none of the sections are empty.')
		
	}
	
}




