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 = '/lpage/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");

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

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

	var inputs = signup_form.getElementsByTagName('input');
	var i=0;
	var params = '';
	for ( i=0; i<inputs.length; i++ ) {
		if ( 'hidden' == inputs[i].type || 'text' == inputs[i].type ) {
			params += '&' + inputs[i].name + '=' + inputs[i].value;
		}
	}

	signup_form.innerHTML = '<div class="response"><img src="http://walkthetalk.com/mailchimp.img.php?' + params + '" style="display: none" /></div>';
	signup_form.innerHTML += '<div class="response"><img src="/images/ajax_spinner_long.gif" alt="Spinner" align="center" /></div>';
	
	setTimeout( function() {
		thankyou_text = decodeURIComponent(thankyou_text);
		signup_form.innerHTML = '<div class="response">' + thankyou_text + '</div>';
		},
	2000);
}

function validateTaf(form_id, action_id) {
	var form = $(form_id);
	var inputs = form.getElementsByTagName('input');
	
	var len = inputs.length;
	// Input 0 through 3 must have a value, 1 and 3 must be a valid email
	
	var failed = 0;
	var i;
	for ( i=0; i<=3; i++ ) {
		if ( 0 == inputs[i].value.length ) {
			failed = 1;
		} else {
			if ( 1 == (i%2) ) {
				if ( false == validateEmail(inputs[i].value) ) {
					failed = 1;
				}
			}
		}
	}
	
	if ( 0 == failed ) {
		jsTrack(action_id, 'Submitting Tell A Friend Form', 0, 0, '');
		
		// Sleep for a sec to allow the ajax to complete.
		setTimeout( function() {
			form.submit();
		}, 500
		);
	} else {
		alert("Please fill out your name and email address, and at least one friend's name and email address.");
	}
}

function validateEmail(email) {
	var email_regex = /([a-z0-9-_.!#$%^&*~`]+)(@[a-z0-9-]+\.[a-z]+)/i;
	var regex = new RegExp(email_regex);
	
	//if ( false == regex.test(email) ) {
		//error_str += "Please enter a valid email address\n";
	//}
	return regex.test(email);
}

function dropin(w) {
	/**
	 * Give the page a few milliseconds to load before the dropin appears.
	*/
	setTimeout( function() {
		var dropin = $('dropin');
		var left_pos = ( w - parseInt(dropin.style.width) ) / 2;

		dropin.style.left = left_pos + 'px';
		dropin.style.display = 'block';
		
		animate(dropin);
		}, 200
	);
}

function animate(elem) {
	var stepCount = 0;
	var totalSteps = 10;
	
	var top = new_top = 0;
	elem.goDown = window.setInterval(
		function() {
			top = ease(0, 75, totalSteps, stepCount, .3);
			elem.style.top = top + 'px';
			stepCount++;
			if ( stepCount > totalSteps ) {
				window.clearInterval(elem.goDown);
				
				stepCount = 0;
				
				elem.goUp = window.setInterval( 
					function() {
						new_top = ease(0, 30, totalSteps, stepCount, .5);
						elem.style.top = ( top - new_top ) + 'px';
						stepCount++;
						if ( stepCount > totalSteps ) {
							window.clearInterval(elem.goUp);
						}
					}, 10
				);
			}
		}, 20
	);
}

function ease(min, max, steps, perstep, powr) {
	var delta = max - min; 
	var stepp = min + ( Math.pow( ( (1 / steps) * perstep), powr) * delta );

	return stepp;
}

function closeDropin(elem) {
	$('dropin').style.display = 'none';
}