var previousTab = "";

function getForms(type)
{
	/*
	document.getElementById(type).className += " selected";
	if(previousTab != "")
	{ document.getElementById(previousTab).className = document.getElementById(previousTab).className.replace(new RegExp(" selected\\b"), ""); }
	previousTab = type;
	*/
	
	new Ajax.Request('scripts/getForms.php', {
		method:'get',
			parameters:"type=" + type,
			onSuccess: function(transport){
				document.getElementById("form").style.backgroundImage = "url(/images/form.jpg)";
				document.getElementById("forms").innerHTML = transport.responseText;
			},
			onFailure:function(){ alert('Something went wrong...') }
		});
}

function facebook_onload(already_logged_into_facebook)
{
	// user state is either: has a session, or does not.
	// if the state has changed, detect that and reload.
	FB.ensureInit(function()
	{
		FB.Facebook.get_sessionState().waitUntilReady(function(session)
		{
			var is_now_logged_into_facebook = session ? true : false;

			// if the new state is the same as the old (i.e., nothing changed)
			// then do nothing
			if (is_now_logged_into_facebook == already_logged_into_facebook)
			{ return; }

			// otherwise, refresh to pick up the state change
			//window.location = "http://www.vistaprintgiveaway.com/";
			window.location = "http://vistaprint-contest.com/";
		});
	});
}

function facebook()
{
	window.open("http://www.facebook.com/share.php?u=http://vistaprintgiveaway.com/",'sharer','toolbar=0,status=0,width=626,height=436');
	new Ajax.Request('scripts/facebook.php', {
	method:'get',
		parameters:"",
		onSuccess: function(transport){
			document.getElementById("dynamic-content").innerHTML = transport.responseText;
		},
		onFailure:function(){ alert('Something went wrong...') }
	});

}

function twitter(formInfo)
{
	var username = formInfo.username.value;
	var password = formInfo.password.value;
	var tweet = formInfo.tweet;
	var tweetCheck = false;
	
	for (var i=0; i < tweet.length; i++) {
	 if (tweet[i].checked) {
		tweetCheck = true;
		break;
	 }
	}
	
	var outTxt = "";
	if (username == "")
	{ outTxt += "Please fill in the Username field.\n"; }
	if (password == "")
	{ outTxt += "Please fill in the Password field.\n"; }
	if (!tweetCheck)
	{ outTxt += "Please select a Tweet.\n"; }
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}

	new Ajax.Request('scripts/twitter.php', {
		method:'get',
			parameters:"username=" + username + "&password=" + password + "&tweet=" + tweet[i].value,
			onSuccess: function(transport){
				document.getElementById("dynamic-content").innerHTML = transport.responseText;
			},
			onFailure:function(){ alert('Something went wrong...') }
		});
		
	return true;
}

function validateEmail(formInfo)
{
	var outTxt = "";
	if (formInfo.fname.value == "")
	{ outTxt += "Please fill in the First Name field.\n"; }
	if (formInfo.lname.value == "")
	{ outTxt += "Please fill in the Last Name field.\n"; }
	if (!formInfo.email.value.match(/.+@.+\..+/))
	{ outTxt += "Your email is invalid.\n"; }
	if (formInfo.rules.checked == false)
	{ outTxt += "You must agree to the Privacy Policy & Rules.\n"; }
	
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}
	return true;
}

function reset()
{
	new Ajax.Request('scripts/reset.php', {
	method:'get',
		parameters:"",
		onSuccess: function(transport){
			document.getElementById("dynamic-content").innerHTML = transport.responseText;
		},
		onFailure:function(){ alert('Something went wrong...') }
	});
}

function tweets()
{
	new Ajax.Request('scripts/tweets.php', {
		method:'get',
			parameters:"",
			onSuccess: function(transport){
				document.getElementById("tweets").innerHTML = transport.responseText;
			},
			onFailure:function(){ alert('Something went wrong...') }
		});
	return true;
}
