var startPos = 0;
var endPos = 0;
var userId = "";
var lForm = "";

function getElementsByTagNames(list,obj) {
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}
		
function resetForm(someForm){
	var someElements = someForm.elements;
	for(i=0; i < someForm.length; i++){
		switch(someElements[i].type){
		case "hidden":
			break;
		case "radio":
			someElements[i].checked = false;
			break;
		case "text":
			someElements[i].value = "";
			break;
		case "select-one":
			someElements[i].selectedIndex = 0;
			break;
		case "textarea":
			someElements[i].value = "";
			break;
		}
	}
}

function validateForm(someForm){
	var someElements = someForm.elements;
	var re_empty = /\w+/;
	var re_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var radioList = new Array();
	for(i=0; i < someElements.length; i++){
		switch(someElements[i].type){
		case "hidden":
			break;
		case "text":
			if(!someElements[i].disabled){
				var alertName = getLabel(someElements[i].id);
				if(alertName.indexOf("*") > -1){
					if(alertName.indexOf("E-mail") > -1){
						if(!re_email.test(someElements[i].value)){
							someElements[i].focus();
							alert("\"" + someElements[i].value + "\" is not a valid email address!");
							return false;
						}
					}else{
						if(!re_empty.test(someElements[i].value)){
							someElements[i].focus();
							alert(alertName.substring(0, alertName.indexOf("<")) + " is a required field!");
							return false;
						}
					}
				}
			}
			break;
		case "select-one":
			var alertName = getLabel(someElements[i].id);
			if(alertName){
				if(alertName.indexOf("*") > -1){
					if(someElements[i].selectedIndex == 0){
						someElements[i].focus();
						alert(alertName.substring(0, alertName.indexOf("<")) + " is a required field!");
						return false;
					}
				}
			}
			break;
		case "radio":
			var stringList = radioList.toString();
			if(stringList.indexOf(someElements[i].name) == -1){
				radioList.push(someElements[i].name);
			}
			break;
		case "textarea":
			var alertName = getLabel(someElements[i].id);
			if(alertName){
				if(alertName.indexOf("*") > -1){
					if(someElements[i].value == ""){
						someElements[i].focus();
						alert(alertName + " is a required field!");
						return false;
					}
				}
			}
			break;
		case "password":
			var alertName = getLabel(someElements[i].id);
			if(alertName.indexOf("*") > -1){
				if(!re_empty.test(someElements[i].value)){
					someElements[i].focus();
					alert(alertName.substring(0, alertName.indexOf("<")) + " is a required field!");
					return false;
				}
			}
			break;
		case "checkbox":
			/*
			if(!someElements[i].checked && someElements[i].name == "termsandconditions"){
				alert("You must accept the Terms and Conditions.");
				return false;
			}
			*/
			break;
		}
	}
	if(radioList.length > 0){
		for(k=0; k < radioList.length; k++){
			var radioCollection = eval("document.forms." + someForm.name + "." + radioList[k]);
			var isChecked = false;
			for(x=0; x < radioCollection.length; x++){
				if(radioCollection[x].checked){
					isChecked = true;
				}else{
					if(radioCollection[x].disabled){
						isChecked = true;
					}
				}
			}
			if(!isChecked){
				switch(radioCollection[0].name){
				case "marketing":
					alert("Please tell us where you heard about McDonald's Passport to Play");
					break;
				case "internet":
					if($('ctl00__mainContent__internetRadioButton').checked){
						alert("Please tell us at which internet site you heard about McDonald's Passport to Play");
						return false;
					}else{
						return true;
					}
					break;
				}
				return false;
			}else{
				if(radioCollection[0].name == "internet"){
					if($('ctl00__mainContent__internetCheckBox_4').checked){
						if(!re_empty.test($F('other_internet'))){
							$('other_internet').focus();
							alert("Please tell us the name of the \"other\" internet site at which you heard about McDonald's Passport to Play.");
							return false;
						}else{
							return true;
						}
					}
				}
			}
		}
	}
	return true;
}

function submitForm(someForm){
	if(validBrowser()){
		if(validateForm(someForm)){
			someForm.submit();
			return false;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function getLabel(someValue){
	if(someValue.indexOf("-field") > -1){
		var targetId = someValue.substring(0, someValue.indexOf("-field"));
	}else{
		var targetId = someValue;
	}
	var labelList = getElementsByTagNames('label');
	for(j=0; j < labelList.length; j++){
		try{
			var targetInput = labelList[j].attributes.getNamedItem('for').value;
		}catch(err){
			alert(labelList[j].innerHTML);
			return false;
		}
		if(targetInput == targetId){
			return(String(labelList[j].innerHTML));
		}
	}
	return false;
}

function validBrowser(){
	var versionString = navigator.userAgent;
	var startPos = versionString.indexOf("MSIE");
	if( startPos > 0){
		var intVersion = versionString.substr(startPos + 5, 3);
		if(intVersion < 6.0 ){
			alert("You should really upgrade to Internet Explorer 7\nPlease visit http://update.microsoft.com");
			return false;
		}
	}
	return true;
}

function videoButton(someLink, someState, someButton){
	var someImage = $(someLink).firstChild;
	if(someState == 'over'){
		switch(someButton){
		case "intro":
			someImage.src = "images/intro-over.jpg";
			break;
		case "china":
			someImage.src = "images/china-over.jpg";
			break;
		case "thailand":
			someImage.src = "images/thailand-over.jpg";
			break;
		case "mexico":
			someImage.src = "images/mexico-over.jpg";
			break;
		case "newzealand":
			someImage.src = "images/newzealand-over.jpg";
			break;
		case "australia":
			someImage.src = "images/australia-over.jpg";
			break;
		case "colombia":
			someImage.src = "images/colombia-over.jpg";
			break;
		case "ghana":
			someImage.src = "images/ghana-over.jpg";
			break;
		case "russia":
			someImage.src = "images/russia-over.jpg";
			break;
		}
	}else{
		switch(someButton){
		case "intro":
			someImage.src = "images/intro-video.jpg";
			break;
		case "china":
			someImage.src = "images/china-video.jpg";
			break;
		case "thailand":
			someImage.src = "images/thailand-video.jpg";
			break;
		case "mexico":
			someImage.src = "images/mexico-video.jpg";
			break;
		case "newzealand":
			someImage.src = "images/newzealand-video.jpg";
			break;
		case "australia":
			someImage.src = "images/australia-video.jpg";
			break;
		case "colombia":
			someImage.src = "images/colombia-video.jpg";
			break;
		case "ghana":
			someImage.src = "images/ghana-video.jpg";
			break;
		case "russia":
			someImage.src = "images/russia-video.jpg";
			break;
			
		}
	}
}

function startScroll(someDir){
	var leftPosition = getLeftPosition('scroll-container_');
	if(someDir > 0){
		switch(leftPosition){
		case -720:
			startPos = -720;
			endPos = 0;
			break;
		case -1260:
			startPos = -1260;
			endPos = -720;
			break;
		default:
			return;
		}
	}else{
		switch(leftPosition){
		case 0:
			startPos = 0;
			endPos = -720;
			break;
		case -720:
			startPos = -720;
			endPos = -1260;
			break;
		default:
			return;
		}
	}
	moveScroll();
}

function stopScroll(){
	runningScript.stop();
}

function moveScroll(){
	var currentPos = getLeftPosition('scroll-container_');
	if(startPos > endPos){
		currentPos -= 30;
		if(currentPos < endPos){
			document.getElementById('scroll-container_').style.left = endPos + "px";
			return false;
		}
	}else{
		currentPos += 30;
		if(currentPos > endPos){
			document.getElementById('scroll-container_').style.left = endPos + "px";
			return false;
		}
	}
	document.getElementById('scroll-container_').style.left = currentPos + "px";
	setTimeout(moveScroll, 33);
}

function getLeftPosition(someElement){
	var leftPosition = parseInt(Element.getStyle(someElement, 'left'));
	return leftPosition;
}

function itineraryOutput(someFunction){
	switch(someFunction){
	case 'print':
		document.itineraryForm.action = "print.php";
		document.itineraryForm.setAttribute('target', '_blank');
		document.itineraryForm.task.value = someFunction;
		break;
	case 'send':
		if(String(userId).length != "23"){
			myLightbox.startHtml("subtext.php?task=no-login", 300, 300);
			return false;
		}
		document.itineraryForm.action = "send.php";
		document.itineraryForm.task.value = someFunction;
		break;
	case 'save':
		if(String(userId).length != "23"){
			myLightbox.startHtml("subtext.php?task=no-login", 300, 300);
			return false;
		}
		document.itineraryForm.task.value = someFunction;
		var formData = Form.serialize(document.itineraryForm);
		formData += "&user_cookie=" + userId;
		new Ajax.Request("subtext.php", { method: 'post', postBody: formData });
		return false;
	default:
		return false;
	}
	document.itineraryForm.submit();
	return false;
}

function getGreeting(someType){
	switch(someType){
	case "teacher":
		$('message_').innerHTML = "Our class is exploring the way kids play around the globe.  Check out the cultures we'll be learning about and the games we'll be playing next week.  If you're interested in taking your students on a similar adventure, drop me a line or check out www.passport2play.com to build your own itinerary and get all the material you'll need to complete the journey.";
		break;
	case "student":
		$('message_').innerHTML = "Next week, we're starting a trip around the world of play.  We'll be exploring how kids in other countries learn, play, live and grow.  Better yet, we're actually going to play some of the games they enjoy.";
		break;
	case "parent":
		$('message_').innerHTML = "Next week, your child's class will be starting a global adventure through the world of play. We'll be exploring how kids in other countries learn, play, live and grow. And we're actually going to play some of the games they enjoy. Please help reinforce the lessons at home by asking your child what they learned, how the game was played and if they enjoyed it.  You might even ask them to take you in the backyard and teach you how to play. Nothing else promotes physical activity like your direct involvement.  Won't you join our adventure?";
		break;
	default:
		$('message_').innerHTML = "";
	}
	return false;
}

function sendOutput(someFunction){
	switch(someFunction){
	case 'preview':
		document.sendForm.action = "email.php";
		document.sendForm.task.value = someFunction;
		break;
	case 'edit':
		document.sendForm.action = "send.php";
		document.sendForm.task.value = "send";
		break;
	case 'send':
		document.sendForm.action = "subtext.php";
		document.sendForm.task.value = "email";
		break;
	case 'email':
		document.sendForm.action = "subtext.php";
		document.sendForm.task.value = someFunction;
		break;
	default:
		return false;
	}
	document.sendForm.submit();
	return false;
}

function blogControl(someFunction){
	if(String(userId).length != "23"){
		myLightbox.startHtml("subtext.php?task=no-login", 300, 300);
		return false;
	}
	switch(someFunction){
	case "blog":
		myLightbox.startHtml("subtext.php?task=blog-form", 500, 460);
		return false;
		break;
	case "preview":
		if($F('blog_title') == "" || $F('blog_content') == ""){
			alert("Please fill in the form completely.");
			return false;
		}
		document.blogForm.action = "blog.php";
		document.blogForm.target = "_blank";
		document.blogForm.task.value = "blog-preview";
		document.blogForm.user_cookie.value = userId;
		break;
	case "post":
		if($F('blog_title') == "" || $F('blog_content') == ""){
			alert("Please fill in the form completely.");
			return false;
		}
		document.blogForm.action = "subtext.php";
		document.blogForm.target = "_self";
		document.blogForm.task.value = "blog-post";
		break;
	case "comment":
		if($F('comment_content') == ""){
			alert("Please fill in the form completely.");
			return false;
		}
		var formData = Form.serialize(document.commentForm);
		formData += "&user_cookie=" + userId;
		new Ajax.Updater('body-blog_', 'subtext.php', { method: 'post', postBody: formData, onComplete: function(){ myLightbox.updateImageList();myLightbox.end();} });
		return false;
		break;
	case "email":
		if(validateForm(document.blogForm)){
			document.blogForm.user_cookie.value = userId;
			var formData = Form.serialize(document.blogForm);
			new Ajax.Request('subtext.php', { method: 'post', postBody: formData });
		}
		return false;
	}
	document.blogForm.submit();
	return false;
}

function downloadControl(someFile){
	if(String(userId).length != "23"){
		myLightbox.startHtml("subtext.php?task=no-login", 300, 300);
		return false;
	}else{
		var postData = "task=download&user_cookie=" + userId + "&file_name=" + encodeURIComponent(someFile);
		new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function (someAjax){
				window.location = someFile;
			}
		});
	}
}

/* function downloadControl(someFile){
	if(String(userId).length != "23"){
		myLightbox.startHtml("subtext.php?task=no-login", 300, 300);
		return false;
	}else{
		var postData = "task=download&user_cookie=" + userId + "&file_name=" + encodeURIComponent(someFile);
		new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function (someAjax){
				window.location = someFile;
			}
		});
	}
} */

function logIn(someForm){
	var re_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(!re_email.test(someForm.username.value)){
		someForm.username.focus();
		alert("\"" + someForm.username.value + "\" is not a valid email address!");
		return false;
	}else{
		var postData = Form.serialize(someForm);
		new Ajax.Request("subtext.php", {method: 'post', postBody: postData});
		return false;
	}
}

function logItinerary(someForm){
	var re_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(!re_email.test(someForm.username.value)){
		someForm.username.focus();
		alert("\"" + someForm.username.value + "\" is not a valid email address!");
		return false;
	}else{
		var postData = Form.serialize(someForm);
		new Ajax.Request("subtext.php", { method: 'post', postBody: postData, onComplete: loadItinerary });
		return false;
	}
}

function adminLogIn(someForm){
	if(validateForm(someForm)){
		var postData = Form.serialize(someForm);
		new Ajax.Request("subtext.php", { method: 'post', postBody: postData });
	}
}

function logOut(){
	userId = "";
	var d = new Date();
	document.cookie = "key=;expires=" + d.toGMTString() + ";" + ";";
	userLoginForm();
}

function commentWindow(someId){
	var tinyWin = window.open("subtext.php?task=comment-view&blog_id=" + someId, "commentWindow", "width=390,height=700,location=no,toolbar=no");
}

function userLoginForm(){
	var re = /key=([a-f0-9\.]{23})/;
	if(re.test(document.cookie)){
		userId = RegExp.$1;
		var formData = "task=logout-form&user_cookie=" + userId;
	}else{
		var formData = "task=login-form";
	}
	new Ajax.Updater('login_', 'subtext.php', { method: 'post', postBody: formData });
}

function itineraryLoginForm(){
	var re = /key=([a-f0-9\.]{23})/;
	if(re.test(document.cookie)){
		userId = RegExp.$1;
		var formData = "task=logout-form&user_cookie=" + userId;
	}else{
		var formData = "task=login-itinerary";
	}
	new Ajax.Updater('login_', 'subtext.php', { method: 'post', postBody: formData });
}

function bumperLink(someHref){
	var winHeight = document.documentElement.clientHeight;
	var winWidth = document.documentElement.clientWidth;
	var params = "width=" + winWidth + ",height=" + winHeight + ",location=yes,toolbar=yes";
	alert("Leaving McDonald's Passport2play.com website  the policies and practices on the site you are going to visit may vary from McDonald's passport2play.com.  Be sure to review the policies of every site you visit, including their information collection and use practices (\"Privacy Policies\").    McDonald's is not responsible for information you submit on other companies' web sites or for those companies' policies and practices.");
	var linkWin = window.open(someHref, "linkWindow", params);
}

function writeYear(){
	var d = new Date();
	document.write(d.getFullYear());
}

function otherInput(someInput){
	if(someInput.checked){
		$('other_internet').disabled = false;
	}
}

function internetInput(someInput){
	$('other_internet').value = "";
	$('other_internet').disabled = true;
}

function checkSecurity(){
	re = /_sid=([0-9a-f]{32})/
	if(String(document.cookie).match(re)){
		new Ajax.Request('subtext.php?task=admin-login');
	}
}

function blogPublish(someAnchor, someId){
	someAnchor.onclick = function() { return false; };
	if(String(someAnchor.firstChild.src).match('not')){
		var postData = "task=publish&blog_id=" + someId + "&publish=1";
	}else{
		var postData = "task=publish&blog_id=" + someId + "&publish=0";
	}
	new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function(someAjax){
			someAnchor.firstChild.src = someAjax.responseText;
			someAnchor.onclick = function() { blogPublish(someAnchor, someId); };
		}
	});
	return false;
}

function blogFlag(someAnchor, someId){
	var someLine = someAnchor.parentNode.parentNode;
	var someChildren = someLine.childNodes;
	for(var i=0; i<someChildren.length; i++){
		if(Element.hasClassName(someChildren[i], 'admin-flag')){
			if(someChildren[i].hasChildNodes()){
				var someFlag = someChildren[i].firstChild;
				if(String(someFlag.firstChild.src).match('flag.png')){
					var postData = "task=flag&blog_id=" + someId;
					new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function(){
							someFlag.firstChild.src = "images/spacer.gif";
						}
					});
				}
			}
		}
	}
}

function flagAgain(someAnchor, someId){
	var someFlag = someAnchor.firstChild;
	if(String(someFlag.src).match('spacer.gif')){
		var postData = "task=flag-again&blog_id=" + someId;
		new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function(){
				someFlag.src = "images/flag.png";
			}
		});
	}
}

function blogDelete(someAnchor, someId){
	var someLine = someAnchor.parentNode.parentNode;
	var userInput = confirm("Are you sure you want to delete this entry? Deleting this entry cannot be undone!");
	if(userInput){
		var formData = "task=blog-delete&blog_id=" + someId;
		new Ajax.Request('subtext.php', { method: 'post', postBody: formData, onComplete: function(){
				Element.remove(someLine);
			}
		});
	}
}

function commentAdmin(someAnchor, someId){
	if (navigator.appName.indexOf("MSIE")!=-1) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}else{
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	myLightbox.startHtml("subtext.php?task=comment-admin&blog_id=" + someId, 880, 500);
}

function commentPublish(someAnchor, someId){
	someAnchor.onclick = function() { return false; };
	if(String(someAnchor.firstChild.src).match('not')){
		var postData = "task=publish&comment_id=" + someId + "&publish=1";
	}else{
		var postData = "task=publish&comment_id=" + someId + "&publish=0";
	}
	new Ajax.Request('subtext.php', { method: 'post', postBody: postData, onComplete: function(someAjax){
			someAnchor.firstChild.src = someAjax.responseText;
			someAnchor.onclick = function() { commentPublish(someAnchor, someId); };
		}
	});
	return false;
}

function commentDelete(someAnchor, someId){
	var someLine = someAnchor.parentNode.parentNode;
	var userInput = confirm("Are you sure you want to delete this entry? Deleting this entry cannot be undone!");
	if(userInput){
		var formData = "task=comment-delete&comment_id=" + someId;
		new Ajax.Request('subtext.php', { method: 'post', postBody: formData, onComplete: function(){
				Element.remove(someLine);
			}
		});
	}
}

var loadItinerary = function(){
	var itinData = "task=itinerary&user_cookie=" + userId;
	new Ajax.Updater('itinerary_', 'subtext.php', { method: 'post', postBody: itinData, onComplete: function(){
		var gameCount = $('itinerary_').childNodes.length
		if(gameCount > 4){
			var newHeight = ((gameCount - 4) * 37) + 185;
			$('step-two_').style.height = newHeight + "px";
		}
		new Effect.Parallel(
			[new Effect.SlideDown( 'step-two_', { duration: resizeDuration, sync: true }),
			new Effect.SlideDown( 'step-three_', { duration: resizeDuration, sync: true })],
			{ queue: 'end'} );
		}
	});
}

var loadAdmin = function(){
	new Effect.Parallel(
	[new Effect.Fade('admin-login_', { duration: resizeDuration, sync: true, afterFinish: function(){
			lForm = $('admin-login_').innerHTML;
			$('admin-login_').innerHTML = "";
			var objLogout = document.createElement("a");
			objLogout.setAttribute('href', '#');
			objLogout.innerHTML = "Click here to securely logout";
			objLogout.style.color = "#333";
			objLogout.onclick = closeAdmin;
			$('admin-login_').appendChild(objLogout);
			new Effect.Appear('admin-login_', { duration: resizeDuration });
		}
	}),
	new Effect.SlideDown( 'body_', { duration: resizeDuration, sync: true })]);
}

var closeAdmin = function(){
	new Effect.Parallel(
	[new Effect.SlideUp('body_', { duration: resizeDuration, sync: true }),
	new Effect.Fade('admin-login_', { duration: resizeDuration, sync: true, afterFinish: function(){
			var re = /_sid=([0-9a-f]{32})/;
			var formData = "task=admin-logout";
			new Ajax.Request('subtext.php', { method: 'post', postBody: formData });			
			$('admin-login_').innerHTML = lForm;
			new Effect.Appear('admin-login_', { duration: resizeDuration });
		}
	})],
	{ queue: 'end' } );
}