var B_ajaxSubmit = true;

function ajaxFunction(theForm, theData) {
	if (typeof(B_debug) != "undefined" && B_debug) alert('ajaxFunction');

	var xmlHttp;
	var theUrl = theForm.action;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		// In order to avoid a syntax error in Firefox, 
		// if the returned content is NOT valid XML, use the 
		// overrideMimeType method to set the content-type.
		xmlHttp.overrideMimeType('text/html');
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e) {
				// Browser doesn't support AJAX
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			// Get the data from the server's response
			if (xmlHttp.responseText.match("success") != null) {
				ajaxDoSuccess(theForm);
			}
			else {
				ajaxDoError(theForm, xmlHttp.responseText);
			}
		}
	}

	if (typeof(B_debug) != "undefined" && B_debug) alert(theUrl);
	if (typeof(B_debug) != "undefined" && B_debug) alert(theData);

	theData +='&_useAjax=1';

	xmlHttp.open('POST', theUrl, true);
	if (typeof(B_debug) != "undefined" && B_debug) alert('xmlHttp.open');
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", theData.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(theData);
	return true;
}

function ajaxDoSuccess(theForm) {
	if (typeof(B_debug) != "undefined" && B_debug) alert('ajaxDoSuccess');

	var defSuccessMsg = 'Thank you!'

    if (typeof(B_ajaxSuccessAlert) == "undefined" || B_ajaxSuccessAlert) {
		// We want to alert() the message
		if (document.getElementById('formSuccess') != null && document.getElementById('formSuccess').innerHTML)
			alert(document.getElementById('formSuccess').innerHTML);
		else
			alert(defSuccessMsg);
	}
    else if (document.getElementById('formSuccess') != null && document.getElementById('formSuccess').innerHTML && document.getElementById('formContents') != null) {
		// Else, if the div is there, put it inline
		document.getElementById('formContents').innerHTML = document.getElementById('formSuccess').innerHTML;
	}
	else if (typeof(theForm.success_url) != "undefined" && theForm.success_url.value) {
		// Else, if the form param is there, redirect
		location.href = theForm.success_url.value;
	}
	else {
		// Otherwise, alert a default msg
		alert(defSuccessMsg);
	}
}

function ajaxDoError(theForm, theError) {
	if (typeof(B_debug) != "undefined" && B_debug) alert('ajaxDoError');

        if (typeof(B_ajaxErrorAlert) == "undefined" || B_ajaxErrorAlert) {
		// If we want to alert() the message
		alert(theError);
	}
        else if (document.getElementById('formError') != null) {
		// Else, if the div is there, put it inline
		document.getElementById('formError').innerHTML = theError;
	}
	else if (typeof(theForm.failure_url) != "undefined" && theForm.failure_url.value) {
		// Else, if the form param is there, redirect
		location.href = theForm.failure_url.value+"?error_msg="+encodeURI(theError);
	}
	else {
		// Otherwise, alert the message
		alert(theError);
	}
}

function createFilteredBuffer(theForm) {
	var t_str = '';
	var t_name = '';
	var result = '';

	for (var a = 0; a < theForm.elements.length; a++) {
		theInput = theForm.elements[a];
		t_str = '';

		if (theInput.name == 'success_url' || theInput.name == 'failure_url')
			continue;

		// If there are any file upload fields, we can't submit
		// using ajax because javascript cannot access local files
		if (theInput.type == 'file')
			B_ajaxSubmit = false;

		if (theInput.type == 'select-one' || theInput.type == 'select-multiple') {
			t_name = theInput.name;
			for (var b = 0; b < theInput.options.length; b++) {
				if (theInput.options[b].selected)
					t_str = t_str + theInput.options[b].value;
			}
		}
		else if (theInput.length > 1) {
			t_name = theInput[0].name;
			for (var b = 0; b < theInput.length; b++) {
				if (theInput[b].checked)
					t_str = t_str + theInput[b].value;
			}

		}
		else if (theInput.type == 'checkbox' || theInput.type == 'radio') {
			t_name = theInput.name;
			if (theInput.checked)
				t_str = t_str + theInput.value;
		}
		else {
			t_name = theInput.name;
			t_str = theInput.value;
		}

		if (t_str) {
			result += (result.length ? '&' : '');
			result += escape(t_name) + '=' + escape(t_str);
		}
	}

	return result;
}
