Ajax.Responders.register({
    onCreate: function(){ Element.show('ajaxLoader')},
    onComplete: function(){Element.hide('ajaxLoader')}
});


function validateFriends(formInfo)
{
	var outTxt = "";

	if (formInfo.name.value == "")
	{ outTxt += "Please fill in the Name field.\n"; }
	if (!formInfo.email.value.match(/.+@.+\..+/))
	{ outTxt += "Your email is invalid.\n"; }
	if (formInfo.friends.value == "")
	{ outTxt += "Please fill in the Friend's email address field.\n"; }
 	
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}
	return true;
}

function limitText(textarea)
{
	if (textarea.value.length > 1000)
	{
		textarea.value = textarea.value.substring(0, 1000);
	}
}

function videoStep1(formInfo)
{
	var outTxt = "";
	var firstName = formInfo.firstName.value;
	var lastName = formInfo.lastName.value;
	var address = formInfo.address.value;
	var city = formInfo.city.value;
	var state = formInfo.state.value;
	var zip = formInfo.zip.value;
	var country = formInfo.country.value;
	var email = formInfo.email.value;
	var sendInfo = formInfo.sendInfo.checked;

	if (firstName == "")
	{ outTxt += "Please fill in the First Name field.\n"; }
	if (lastName == "")
	{ outTxt += "Please fill in the Last Name field.\n"; }
	if (address == "")
	{ outTxt += "Please fill in the Address field.\n"; }
	if (city == "")
	{ outTxt += "Please fill in the City field.\n"; }
	if (state == "")
	{ outTxt += "Please select your State.\n"; }
	if (zip == "")
	{ outTxt += "Please fill in the Zip field.\n"; }
	if (country == "")
	{ outTxt += "Please select your Country.\n"; }
	if (!email.match(/.+@.+\..+/))
	{ outTxt += "Your email is invalid.\n"; }
	if (formInfo.Rules.checked == false)
	{ outTxt += "You must agree to the Privacy Policy, Rules and Terms of Service.\n"; }
 	
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}
	
	new Ajax.Request('scripts/videoStep1.php', {
		method:'get',
			parameters:"firstName=" + firstName + "&lastName=" + lastName + "&address=" + escape(address) + "&city=" + city + "&state=" + state + "&zip=" + zip + "&country=" + country + "&email=" + email + "&sendInfo=" + sendInfo,
			onSuccess: function(transport){
				document.getElementById("formDiv").innerHTML = transport.responseText;
			},
			onFailure:function(){ alert('Something went wrong...') }
		});
	return true;
}

function videoStep2(formInfo)
{
	var outTxt = "";
	var id = formInfo.id.value;
	var videoTitle = formInfo.videoTitle.value;
	var videoDescription = formInfo.videoDescription.value;

	if (videoTitle == "")
	{ outTxt += "Please fill in the Video Title field.\n"; }
	if (videoDescription == "")
	{ outTxt += "Please fill in the Video Description field.\n"; }
	if (videoTitle.match(/<|>/) || videoDescription.match(/<|>/))
	{ outTxt += "The less than and greater than symbols (< and >) are not allowed."; }
 	
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}
	
	new Ajax.Request('scripts/videoStep2.php', {
		method:'post',
			parameters:"id=" + id + "&videoTitle=" + videoTitle + "&videoDescription=" + videoDescription,
			onSuccess: function(transport){
				document.getElementById("formDiv").innerHTML = transport.responseText;
			},
			onFailure:function(){ alert('Something went wrong...') }
		});
	return true;
}

function videoStep3(formInfo)
{
	var outTxt = "";

	if (formInfo.file.value == "")
	{ outTxt += "Please select a file to upload.\n"; }
 	
	if (outTxt != "")
	{
		alert("The following information is incorrect:\n" + outTxt);
		return false;
	}
	
	document.getElementById("ajaxLoader").style.display = "block";
	return true;
}

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 = "submit-your-video.php";
		});
	});
}

function postToWall()
{
	FB.ensureInit(function()
	{
		FB.Connect.showFeedDialog("128800300406", {"images":[{"src":"http://www.freemeupgiveaway.com/video/images/free-me-up-video-logo.jpg", "href":"http://www.freemeupgiveaway.com/video/"}], "title":"entered the FreeMeUp Giveaway Video Contest", "content":" just submitted a video for a chance to win a new iPhone 3GS with a year of free service.  Plus, the first 1,000 entrants get a LogMeIn T-shirt.  View and vote for this video today."}, null, "", null, FB.RequireConnect.doNotRequire, callback);
	});
}

function callback()
{ return; }
