function addEvent(elm, evType, fn, useCapture)
{
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko by Scott Andrew
	if(elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else if(elm.attachEvent)
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} 
	else 
	{
		elm['on' + evType] = fn;
	}
}

function thursday()
{
	var frm = document.getElementById('sign-me-up');
	var error = false;
	var f = null;
	function focusCheck(focusTo){
		if(f==null) {
			f=focusTo;
		}
	}
	function displayError(inputName,errorField)
	{
		document.getElementById(errorField).style.display = "block";
		document.getElementById(inputName).style.background = "#ffc";
		document.getElementById(inputName).style.border = "1px red solid";
		error = true;
		focusCheck(inputName);		
	}	
	var field = new Array("validate_top","validate_firstname","validate_lastname","validate_address","validate_city","validate_province","validate_country","validate_postal_code","validate_email","validate_home_phone_a","validate_home_phone_b","validate_home_phone_c","validate_month","validate_day","validate_year");
		for(i=0;i<field.length;i++) {
			document.getElementById(field[i]).style.display = "none";
		}	
	var inputs = new Array("first_name","last_name","address1","city","state","country","zip","email","night_phone_a","night_phone_b","night_phone_c","month","day","year");	
		for(j=0;j<inputs.length;j++) {
			document.getElementById(inputs[j]).style.background = "#fff";
			document.getElementById(inputs[j]).style.border = "1px #3c3 solid";
		}		
		if(!validField(frm.first_name.value)) {
			displayError("first_name","validate_firstname");
			}
		if(!validField(frm.last_name.value)) {
			displayError("last_name","validate_lastname");	
			}					
		if(!validField(frm.address1.value)) {
			displayError("address1","validate_address");
			}	
		if(!validField(frm.city.value)) {
			displayError("city","validate_city");
			}				
		if(!validSelect(frm.state.value)) {
			displayError("state","validate_province");
			}	
		if(!validSelect(frm.country.value)) {
			displayError("country","validate_country");
			}	
		if(!validField(frm.zip.value)) {
			displayError("zip","validate_postal_code");
			}				
		if(!validEmail(frm.email.value)) {
			displayError("email","validate_email");
			}			
		if(!validNumber(frm.night_phone_a.value) || !validField(frm.night_phone_a.value)) {
			displayError("night_phone_a","validate_home_phone_a");
			}
		if(!validNumber(frm.night_phone_b.value) || !validField(frm.night_phone_b.value)) {
			displayError("night_phone_b","validate_home_phone_b");
			}
		if(!validNumber(frm.night_phone_c.value) || !validField(frm.night_phone_c.value)) {
			displayError("night_phone_c","validate_home_phone_c");
			}
		if(!validSelect(frm.month.value)) {
			displayError("month","validate_month");
			}	
		if(!validSelect(frm.day.value)) {
			displayError("day","validate_day");
			}
		if(!validSelect(frm.year.value)) {
			displayError("year","validate_year");
			}			
	if(error){
		document.getElementById("validate_top").style.display = "block";
		document.getElementById(f).focus();		
		return false;
	} else {
		$.post("add-user.php", { first_name: $("#first_name").val(), last_name: $("#last_name").val(), address1: $("#address1").val(), city: $("#city").val(), state: $("#state").val(), country: $("#country").val(), zip: $("#zip").val(), night_phone_a: $("#night_phone_a").val(), night_phone_b: $("#night_phone_b").val(), night_phone_c: $("#night_phone_c").val(), email: $("#email").val(), gender: $("#gender").val(), month: $("#month").val(), day: $("#day").val(), year: $("#year").val() });
		 changeTabs("payment");
		 changePlan();		
	}
}

function changeTabs(val)
{
	if(val=="payment")
	{
		$("#personal-information-content").slideUp("slow");
		$("#payment-options").slideDown("slow");
		$("#personal-information-change").show();
	}
	else if(val=="personal")
	{
		$("#payment-options").slideUp("slow");
		$("#personal-information-content").slideDown("slow");	
		$("#personal-information-change").hide();
	}
}

function changePlan()
{
	var promo_code = document.getElementById("promo-code").value;
	$.post('submit-information.php', {first_name: $("#first_name").val(), last_name: $("#last_name").val(), address1: $("#address1").val(), city: $("#city").val(), state: $("#state").val(), country: $("#country").val(), zip: $("#zip").val(), night_phone_a: $("#night_phone_a").val(), night_phone_b: $("#night_phone_b").val(), night_phone_c: $("#night_phone_c").val(), email: $("#email").val(), promo_code: promo_code}, function(data) { 
		if(data.length > 0)
		{
			$("#buttons").html(data);
		}
	});	
}

function addListeners()
{
	//document.getElementById("promo-code").value = "";	
	// add a listener to the change event of the promo code box
	var promoBtn = document.getElementById('btn-enter-code');
	addEvent(promoBtn,'click',changePlan,false);	
}	

window.addEvent(window,'load',addListeners,false);

// run this function on load of page to refresh values in case of Back button from PayPal to this form to rechoose a plan payment type
window.onload = changePlan;
