function openError() {

	// show the form
	var tellAFriendBackground = document.getElementById('tell-a-friend-background');
	tellAFriendBackground.style.display = "block";

	var errorBox = document.getElementById('error-layer');
	errorBox.style.display = "block";
	
	var content = document.getElementById('content');
	var footer = document.getElementById('footer');
	
	tellAFriendBackground.style.top = content.offsetTop;
	errorBox.style.top = (content.offsetTop + 20) + "px";
	
	tellAFriendBackground.style.left = getX(content);
	errorBox.style.left = (getX(content) + 95) + "px";
	document.getElementById('content').style.height = "650px";
	
	tellAFriendBackground.style.height = ((footer.offsetTop - content.offsetTop) + (footer.offsetHeight - 16)) + "px"; 
	
	/* 16 is the distance between the keyline and the bottom of the page */
	
	tellAFriendBackground.style.width = (content.offsetWidth + 2) + "px";
	tellAFriendBackground.style.width = "772px";
	
	// set focus
	window.location = '#';
}

function openMailingList() {

 // show the form
 var tellAFriendBackground = document.getElementById('tell-a-friend-background');
 tellAFriendBackground.style.display = "block";
/*
 document.getElementById('tell-a-friend-form').style.display = 'block';

 document.getElementById('tell-a-friend-message').innerHTML = "";
 document.getElementById('tell-a-friend-error').innerHTML = "";
*/
 var mailingList = document.getElementById('mailing-list-layer');
 mailingList.style.display = "block";

 var content = document.getElementById('content');
 var footer = document.getElementById('footer');

 tellAFriendBackground.style.top = (content.offsetTop) + "px";
 mailingList.style.top = (content.offsetTop + 20) + "px";

 tellAFriendBackground.style.left = (getX(content)) + "px";
 mailingList.style.left = (getX(content) + 95) + "px";
 document.getElementById('content').style.height = "650px";

 tellAFriendBackground.style.height = ((footer.offsetTop - content.offsetTop) + (footer.offsetHeight - 16)) + "px";

 /* 16 is the distance between the keyline and the bottom of the page */

 tellAFriendBackground.style.width = (content.offsetWidth + 2) + "px";
 tellAFriendBackground.style.width = "772px";

 // set focus
 window.location = '#';
}


function closeMailingList() {
	var mailingList = document.getElementById('mailing-list-layer');
	mailingList.style.display = "none";
	
	var tellAFriendBackground = document.getElementById('tell-a-friend-background');
	tellAFriendBackground.style.display = "none";
	
	document.getElementById('content').style.height = "auto";
}
function openTellAFriend() {

 // show the form
 var tellAFriendBackground = document.getElementById('tell-a-friend-background');
 tellAFriendBackground.style.display = "block";

 document.getElementById('tell-a-friend-form').style.display = 'block';

 document.getElementById('tell-a-friend-message').innerHTML = "";
 document.getElementById('tell-a-friend-error').innerHTML = "";

 document.getElementById('content').style.height = "650px";

 var tellAFriend = document.getElementById('tell-a-friend');
 tellAFriend.style.display = "block";

 var content = document.getElementById('content');
 var footer = document.getElementById('footer');

 tellAFriendBackground.style.top = (content.offsetTop) + "px";
 tellAFriend.style.top = (content.offsetTop + 20) + "px";

 tellAFriendBackground.style.left = getX(content) + "px";
 tellAFriend.style.left = (getX(content) + 95) + "px";


 tellAFriendBackground.style.height = ((footer.offsetTop - content.offsetTop) + (footer.offsetHeight - 16)) + "px";

 /* 16 is the distance between the keyline and the bottom of the page */

 tellAFriendBackground.style.width = (content.offsetWidth + 2) + "px";
 tellAFriendBackground.style.width = "772px";

 // set focus
 window.location = '#';
}
function closeTellAFriend() {
	var tellAFriend = document.getElementById('tell-a-friend');
	tellAFriend.style.display = "none";
	
	var tellAFriendBackground = document.getElementById('tell-a-friend-background');
	tellAFriendBackground.style.display = "none";
	
	document.getElementById('content').style.height = "auto";
}
function mailingListSubmit(formType) {
	
	if(
		document.getElementById('ml-first-name-field').value.length < 1
		|| document.getElementById('ml-last-name-field').value.length < 1
		|| !validateEmail(document.getElementById('ml-email-field').value) 
	)
	{
		if(formType == 'wide') {
			document.getElementById('mailing-list-intro').style.display = 'none';
		}
		document.getElementById('mailing-list-error').style.display = 'block';
		document.getElementById('mailing-list-error').innerHTML = "<div class=\"error\">Please complete all fields.</div>";
	}
	else
	{
			var ajaxRequest;  // The variable that makes Ajax possible!
			var ajaxStatus; // 1:success 0:fail
	
			try{
				// Opera 8.0+, Firefox, Safari
				ajaxRequest = new XMLHttpRequest();
			} catch (e){
				// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					//alert("Your browser broke!");
					return false;
				}
			}
		}
	
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				//alert(ajaxRequest.responseText); //debug
				
				var str=ajaxRequest.responseText;	
				if (str.replace(/ /, "") == "1")
				{
					// Update display
					document.getElementById('mailing-list-form').style.display = 'none';
					document.getElementById('mailing-list-error').style.display = 'none';
					document.getElementById('mailing-list-message').style.display = 'block';
					
					if(formType != 'wide') {
						document.getElementById('mailing-list-message').innerHTML = "<h3>YOU'VE JOINED OUR</h3><h2>MAILING LIST</h2><p><strong>Thanks for signing-up!</strong> Look for our next monthly newsletter in your inbox soon.</p><p><strong>Please tell your friends about us.</strong> Help spread the word by clicking below:</p><p class=\"button-taf\"><a href=\"javascript:closeMailingList();openTellAFriend()\"><img src=\"images/button-taf.gif\" alt=\"Tell a Friend\" /></a></p>";
					}
				}
				else
				{
					document.getElementById('mailing-list-error').style.display = 'block';
					document.getElementById('mailing-list-error').innerHTML = "<p><div class=\"error\">There was a problem accepting your request.</div></p>";
				}
			}
		}
		
		var url = "/process-mailing-list.php?"+ returnFormAsQueryString('mailing-list-form');
		
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null); 

	}
}
function returnFormAsQueryString(formID)
{
	// inputs
	var queryString = "";
	var textFields = document.getElementById(formID).getElementsByTagName('input');
	for (i=0; i<textFields.length; i++) {
		queryString += textFields[i].name + "=" + urlencode(textFields[i].value) + "&";
	}
	
	// textareas
	var textareas = document.getElementById(formID).getElementsByTagName('textarea');
	for (i=0; i<textareas.length; i++) {
		queryString += textareas[i].name + "=" + urlencode(textareas[i].value) + "&";
	}
	
	return(queryString);
}

function tellAFriendSubmit() {

	if(
		document.getElementById('taf-first-name-field').value.length < 1
		|| !validateEmail(document.getElementById('taf-email-field').value) 
		|| document.getElementById('taf-friend-first-name-field').value.length < 1
		|| !validateEmail(document.getElementById('taf-friend-email-field').value) 
	)
	{
		document.getElementById('tell-a-friend-error').style.display = 'block';
		document.getElementById('tell-a-friend-error').innerHTML = "<p><div class=\"error\">Please complete all required fields to continue.</div></p>";
	}
	else
	{
			var ajaxRequest;  // The variable that makes Ajax possible!
			var ajaxStatus; // 1:success 0:fail
	
			try{
				// Opera 8.0+, Firefox, Safari
				ajaxRequest = new XMLHttpRequest();
			} catch (e){
				// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					//alert("Your browser broke!");
					return false;
				}
			}
		}
	
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				//alert(ajaxRequest.responseText); //debug
				
				var str=ajaxRequest.responseText;	
				if (str.replace(/ /, "") == "1")
				{
						
					// Update display
					document.getElementById('tell-a-friend-form').reset();
					document.getElementById('tell-a-friend-form').style.display = 'none';
					document.getElementById('tell-a-friend-error').style.display = 'none';
					document.getElementById('tell-a-friend-message').style.display = 'block';
		 			document.getElementById('tell-a-friend-message').innerHTML = "<h3>&nbsp;</h3><h2>THANKS FOR SHARING!</h2><p>We'll send your friend(s) an email telling them about us.</p><p>Bon App&eacute;tit!</p><p class=\"taf-ctas\"><a href=\"javascript:closeTellAFriend()\" class=\"first\"><img src=\"images/button-close.gif\" alt=\"Close\" /></a><a href=\"javascript:openTellAFriend()\"><img src=\"images/button-tell-more-friends.gif\" alt=\"Tell More Friends\" /></a></p>";
				}
				else
				{
					document.getElementById('tell-a-friend-error').style.display = 'block';
					document.getElementById('tell-a-friend-error').innerHTML = "<p><div class=\"error\">There was a problem accepting your request.</div></p>";
				}
			}
		}
		
		var url = "process-tell-a-friend.php?"+ returnFormAsQueryString('tell-a-friend-form');
		
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null); 

	}
	window.location = '#';

}

/* this function helps get around IE bugs with offsetLeft */
function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
return iReturnValue;
}

function showClassDetail(sku, classData)
{
	document.getElementById("class-detail-key").setAttribute('value', sku);
	document.getElementById("class-detail-date").innerHTML = classData[sku]['date'];
	document.getElementById("class-detail-title").innerHTML = classData[sku]['title'];
	document.getElementById("class-detail-type").innerHTML = classData[sku]['type'];
	document.getElementById("class-detail-time").innerHTML = classData[sku]['time'];
	document.getElementById("class-detail-price").innerHTML = classData[sku]['price'];
	document.getElementById("class-detail-description").innerHTML = classData[sku]['description'];
	document.getElementById("class-detail-seats").innerHTML = classData[sku]['seats'];
	
	if(classData[sku]['type'] == " Demo" || classData[sku]['type'] == "Demo") {
		document.getElementById("class-detail-photo").src = "images/class-detail-photo-demo.jpg";
	}
	else
	{
		document.getElementById("class-detail-photo").src = "images/class-detail-photo.jpg";
	}
	document.getElementById("class-detail-added").style.display = 'none';
	if(classData[sku]['seats'] == '0' || classData[sku]['seats'] == undefined) {
		document.getElementById("class-detail-unavailable").style.display = 'block';
		document.getElementById("class-detail-form").style.display = 'none';
	} else {
		document.getElementById("class-detail-form").style.display = 'block';
		document.getElementById("class-detail-unavailable").style.display = 'none';
		/*
		document.getElementById("class-detail-seat-select").innerHTML = "";
		var i = 1;
		while(i <= classData[sku]['seats'])
		{
			//document.getElementById("class-detail-seat-select").innerHTML += '<option value="'+i+'">'+i+'</option>';
			i++;
		}
		*/
	}
	document.getElementById('class-detail').style.display = 'block';
}
function hideClassDetail()
{
	document.getElementById('class-detail').style.display = 'none';
}
function submitClassDetailForm() {
	document.getElementById('class-detail-form').style.display = 'none';
	document.getElementById('class-detail-added').style.display = 'block';
}


/* validate email */
function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


function showShipping() {
	if(document.getElementById('opposite-shiptobilladdr').checked == true) {
		document.getElementById('checkout-shipping').style.display = 'block';
		document.getElementById('shiptobilladdr').value = "";
		document.getElementById('shiptobilladdr').name = "offshiptobilladdr";
	}
	else
	{
		document.getElementById('checkout-shipping').style.display = 'none';
		document.getElementById('shiptobilladdr').value = "yes";
		document.getElementById('shiptobilladdr').name = "shiptobilladdr";
	}
}
function updateCheckOutFields() {
	document.getElementById('contactname').value = document.getElementById('billfirstname').value + document.getElementById('billlastname').value;
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}	
