var theRequest;
var animateRight;
var currentStep;

function sendRequest(step, back) {
    theRequest = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    
    if(theRequest) {
		// setup the post data we'll need for the Ajax request				
		var formElements = document.sellForm.elements;
		var data = '';
		var extra = '';
		animateRight = false;
		currentStep = step - 1;
		if(back) {
			animateRight = true;
			extra = '&skipSave=yes';
			currentStep = step + 1;
		}
		
		for(i = 0; i < formElements.length; i++) {
			if(formElements[i].disabled)
				continue;
		
			if(i)
				data += "&";
			
			if(formElements[i].options) {
				var valueAppend = "";
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + "=" + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + "=" + escape(formElements[i].value);
			}
		}

		theRequest.onreadystatechange = processRequest;
		theRequest.open("POST", 'lib/ajax_sell_your_aircraft_processor.php?step='+step+extra, true);
		theRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		theRequest.setRequestHeader("Content-length", data.length);
		theRequest.setRequestHeader("Connection", "close");
		theRequest.send(data);
    } else {
		alert("Error - form cannot be submitted.");
	}
}

function processRequest() {
    if(theRequest.readyState == 4) {
        if(theRequest.status == 200) {
        	if(theRequest.responseText.length > 2 && theRequest.responseText.substr(0,2) == 'FL') {
        		document.getElementById('sellContent'+currentStep).innerHTML = theRequest.responseText.substr(2);
        	} else {
				animate(theRequest.responseText);
			}
        } else {
            // oh noes, http error
			var errorMessage = document.getElementById('errorMessage');
			if (errorMessage) {
				errorMessage.innerHTML = "HTTP Error!";
			}
        }
    }
}

function animate(data) {
	var nextStep = animateRight ? currentStep-1 : currentStep+1;
	var scroller = document.getElementById('sellSlider');
	var content1 = document.getElementById('sellContent'+currentStep);
	var content2 = document.getElementById('sellContent'+nextStep);

	content2.innerHTML = data;
	scroller.hChange = new Fx.Styles(scroller.id, {duration: 600, transition: Fx.Transitions.quadInOut, onComplete: function (){
		content1.innerHTML = '';
		scroller.hChange = null;
	} });
	scroller.hChange.start({'left': [-(currentStep-1)*660, -(nextStep-1)*660]});
}