var EMAIL_FIELD = 'email';

function $(e) { return document.getElementById(e); }

function initializeAjax() {
	var xmlHttp;
	try {
			// Firefox, Opera 8.0+, Safari, and other good browsers
			xmlHttp = new XMLHttpRequest();
	} catch (e) {
			// Internet Explorer
			try {
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {
							xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
							return false;
					}
			}
	}

	return xmlHttp;
}

function jsTrack(action_id, action_value, product_id, amount, url) {
	var xmlHttp = initializeAjax();

	var http_url = 'lp.http.php';
	var params = 'action_id=' + parseInt(action_id);
	params += '&action_value=' + encodeURIComponent(action_value);
	params += '&product_id=' + parseInt(product_id);
	params += '&amount=' + amount;

	if ( xmlHttp ) {
		xmlHttp.open("POST", http_url, true);

		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		xmlHttp.send(params);
		if ( '' != url ) {
			xmlHttp.onreadystatechange = function() {
				if ( ( xmlHttp.readyState == 4 ) && ( xmlHttp.status == 200 ) ) {
					window.location = url;
				}
			}
		}
	}
}


function inlineSubmit(form_name, thankyou_text) {
	var signup_form = $(form_name);

	var inputs = signup_form.getElementsByTagName('input');
	var i=0;
	var params = '';
	var errors = false;
	var total = 0;
	var checkbox_error = 0;
	for ( i=0; i<inputs.length; i++ ) {
		if ( 'checkbox' == inputs[i].type ) {
			total++;
			
			if ( false == inputs[i].checked ) {
				checkbox_error++;
			}
		}
	}
	
	if ( total == checkbox_error ) {
		alert('Please select at least one newsletter.');
		errors = true;
	} else {
		for ( i=0; i<inputs.length; i++ ) {
			if ( 'hidden' == inputs[i].type || 'text' == inputs[i].type || 'checkbox' == inputs[i].type ) {
				if ( -1 != inputs[i].name.indexOf(EMAIL_FIELD) ) {
					if ( false == validateEmail(inputs[i].value) ) {
						alert('Please enter a valid email address.');
						errors = true;
					}
				}
				
				if ( inputs[i].value.length > 0 ) {
					if ( ('checkbox' == inputs[i].type && true == inputs[i].checked) || ( 'checkbox' != inputs[i].type ) ) {
						params += '&' + inputs[i].name + '=' + inputs[i].value;
					}
				}
			}
		}
	}

	if ( false == errors ) {
	
		var xmlHttp = initializeAjax();

		var http_url = 'net_atlantic.http.php';
		
		if ( xmlHttp ) {
			xmlHttp.open("POST", http_url, true);

			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			xmlHttp.send(params);
			xmlHttp.onreadystatechange = function() {
				if ( ( xmlHttp.readyState == 4 ) && ( xmlHttp.status == 200 ) ) {
					signup_form.innerHTML = '<div class="response">' + thankyou_text + '</div>';
				}
			}
		}
		
		//signup_form.innerHTML = '<div class="response"><img src="http://mk1.netatlantic.com/subscribe/subscribe.tml?' + params + '" style="display: none" /></div>';
		//signup_form.innerHTML += '<div class="response"><img src="images/ajax_spinner_long.gif" alt="Spinner" align="center" /></div>';

		return true;
	}

	return false;
}
