// form validation function //

function submitForm(form){
	
	var target = document.getElementById('form_regSubmit');
	if(validate(target)){
	sessionId = '';
	
	regErrorText = "<span class='regStatusError'>Registration Error!</span><br /><span>Please click <a href='javascript:void(null)' onClick=\"Effect.BlindDown('form_regSubmit');\">here</a> and attempt your registration again.<br />If you continue to experience difficulty, please contact us at: recruiter@bluesbootcamp.com</span>";
	regConfirmText = "<span class='regStatusConfirmed'>Registration Accepted!</span><br /><span>Click <a href='http://www.danceloot.com/bbcReg/regConfirm.php?ticket="+sessionId+"'>here</a> if you are not automatically redirected.</span>";
	Effect.BlindUp(form);
	document.getElementById('statusBox').style.display = "block";
	document.getElementById('statusBox').innerHTML = "<span style='color:#00CC33;'>processing...</span><span>Please Wait One Moment...</span>";
	status = new Ajax.Request('includes/danceloot_eventReg.php', {
						method:'post',
						parameters: $('form_regSubmit').serialize(true),
						onSuccess: function(obj){
										successRegEx = /\<status>success\<\/status>/;
										if(obj.responseText.match(successRegEx)){
											document.getElementById('statusBox').innerHTML = regConfirmText;
											queryRegEx = /\<regId>(\w+)\<\/regId>/;
											var regId = obj.responseText.match(queryRegEx)[1];
											window.location = "http://www.danceloot.com/bbcReg/regConfirm.php?ticket="+regId;
										}else {
											document.getElementById('statusBox').innerHTML = obj.responseText;
										}
									},
						onFailure: function(obj){
							document.getElementById('statusBox').innerHTML = obj.responseText;
						}
										
									
						});
	}
	
}

function validate(form) {
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var intRegex = /^[0-9]+$/;
	var timeout = 2;	
	
	var firstName = form.firstName.value;
	var lastName= form.lastName.value;
	var phone = form.phone1.value + form.phone2.value + form.phone3.value;
	var email1 = form.emailAddress.value;
    var email2 = form.verifyEmail.value;
	var address1 = form.address1.value;
	var city = form.city.value;
	var state = form.state.value;
	var zip = form.zip.value;
	var yearsExp = form.yearsExp.value;
	var dancePerMonth = form.dancePerMonth.value;
	var gender = false;
	var danceRole = false;
	var agreement = form.readAgreement.checked;
	
	var regType = false;
	var housingOrHosting = "none";

	if(firstName == "") {
		inlineMsg('firstName','You must enter your first name.', timeout);
		return false;
	}
	if(!firstName.match(nameRegex)) {
		inlineMsg('firstName','You have entered an invalid name.', timeout);
		return false;
	}
	if(lastName == "") {
		inlineMsg('lastName','You must enter your last name.', timeout);
		return false;
	}
	if(!lastName.match(nameRegex)) {
		inlineMsg('lastName','You have entered an invalid name.', timeout);
		return false;
	}
	if((!phone.match(intRegex)) || (phone.length != 10)){
		inlineMsg('primaryPhone','You must enter a valid phone number.', timeout);	
		return false;
	}
	if(email1 == "") {
		inlineMsg('emailAddress','<strong>Error</strong><br />You must enter your email.', timeout);
		return false;
	}
	if(!email1.match(emailRegex)) {
		inlineMsg('emailAddress','<strong>Error</strong><br />You have entered an invalid email.', timeout);
		return false;
	}
	if(email2 == "") {
		inlineMsg('verifyEmail','<strong>Error</strong><br />You must enter your email.', timeout);
		return false;
	}
	if(!email2.match(emailRegex)) {
		inlineMsg('verifyEmail','<strong>Error</strong><br />You have entered an invalid email.', timeout);
		return false;
	}
	if(email1.toLowerCase() != email2.toLowerCase()){
		inlineMsg('emailAddress', '<strong>Error</strong><br />You must enter matching email addresses.', timeout);
		return false;
	}
	if(address1 == "") {
		inlineMsg('address1','<strong>Error</strong><br />You must enter your address.', timeout);
		return false;
	}
	if(city == "") {
		inlineMsg('city','<strong>Error</strong><br />You must enter your city.', timeout);
		return false;
	}
	if(state == "") {
		inlineMsg('state','<strong>Error</strong><br />You must select a state.', timeout);
		return false;
	}
	if(zip == "") {
		inlineMsg('zip','<strong>Error</strong><br />You must enter your zip code.', timeout);
		return false;
	}
	if(!zip.match(intRegex)){
		inlineMsg('zip','<strong>Error</strong><br />You must enter a valid zip code.', timeout);
		return false;
	}
	for(i=0; i<form.regType.length; i++){
		if(form.regType[i].checked == true){
			regType = true;		
		}
	}
	if(regType == false){
		inlineMsg('regType', '<strong>Error</strong><br />You must select a registration type!.', timeout);
		return false;
	}

	/*for(i=0; i<form.housingOrHosting.length; i++){
		if(form.housingOrHosting[i].checked == true){
			var housingOrHosting = form.housingOrHosting[i].value;
		}
	}*/
	if(housingOrHosting == false){
		inlineMsg('housingOrHosting', '<strong>Error</strong><br />dohPlease select from one of these options to continue.', timeout);
		return false;
	}
	
	if(housingOrHosting == "host"){
		smokingAllowed = false;
		drinkingAllowed = false;
		maxGuests = form.maxGuests.value;
		
		var showHosts = function(){
			document.getElementById('hostQuestions').style.display="block";
		}
		if(!maxGuests.match(intRegex)){
			showHosts();
			inlineMsg('maxGuests', '<strong>Error:</strong>Not a valid number.', timeout);
			return false;
		}	
		for(i=0; i<form.smokingAllowed.length; i++){
			if(form.smokingAllowed[i].checked == true){
				smokingAllowed = true;		
			}
		}
		if(smokingAllowed == false){
			showHosts();
			inlineMsg('smokingAllowed', '<strong>Error</strong><br />You need to select smoking or non-smoking.', timeout);
			return false;
		}
		for(i=0; i<form.drinkingAllowed.length; i++){
			if(form.drinkingAllowed[i].checked == true){
				drinkingAllowed = true;		
			}
		}
		if(drinkingAllowed == false){
			showHosts();
			inlineMsg('drinkingAllowed', '<strong>Error</strong><br />You need to select drinking or non-drinking.', timeout);
			return false;
		}
	}
	if(housingOrHosting == "guest"){
		smokingGuest = false;
		drinkingGuest = false;
		pickupQuestion = false;
		dropOffQuestion = false;
		
		var showGuests = function(){
			document.getElementById('guestQuestions').style.display = 'block';
		}
		for(i=0; i<form.smokingGuest.length; i++){
			if(form.smokingGuest[i].checked == true){
				smokingGuest = true;		
			}
		}
		if(smokingGuest == false){
			showGuests();
			inlineMsg('smokingGuest', '<strong>Error</strong><br />You need to select smoking or non-smoking.', timeout);
			return false;
		}
		for(i=0; i<form.drinkingGuest.length; i++){
			if(form.drinkingGuest[i].checked == true){
				drinkingGuest = true;		
			}
		}
		if(drinkingGuest == false){
			showGuests();
			inlineMsg('drinkingGuest', '<strong>Error</strong><br />You need to select drinking or non-drinking.', timeout);
			return false;
		}
		for(i=0; i<form.pickupQuestion.length; i++){
			if(form.pickupQuestion[i].checked == true){
				pickupQuestion = true;		
			}
		}
		if(pickupQuestion == false){
			showGuests();
			inlineMsg('pickupQuestion', '<strong>Error</strong><br />You need to select yes or no here.', timeout);
			return false;
		}
		for(i=0; i<form.dropOffQuestion.length; i++){
			if(form.dropOffQuestion[i].checked == true){
				dropOffQuestion = true;		
			}
		}
		if(dropOffQuestion == false){
			showGuests();
			inlineMsg('dropOffQuestion', '<strong>Error</strong><br />You need to select yes or no here.', timeout);
			return false;
		}
	}
	
	if(agreement == "") {
		inlineMsg('readAgreement','<strong>Error</strong><br />You must agree to the terms and conditions.', timeout);
		return false;
	}	
	document.getElementById('errorAlert').style.display = 'none';
	return true;
}
	
	// START OF MESSAGE SCRIPT //
	
var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
	var msg;
	var msgcontent;
	if(!document.getElementById('msg')) {
		msg = document.createElement('div');
		msg.id = 'msg';
		msgcontent = document.createElement('div');
		msgcontent.id = 'msgcontent';
		document.body.appendChild(msg);
		msg.appendChild(msgcontent);
		msg.style.filter = 'alpha(opacity=0)';
		msg.style.opacity = 0;
		msg.alpha = 0;
	} else {
		msg = document.getElementById('msg');
		msgcontent = document.getElementById('msgcontent');
	}
	msgcontent.innerHTML = string;
	msg.style.display = 'block';
	var msgheight = msg.offsetHeight;
	var targetdiv = document.getElementById(target);
	targetdiv.focus();
	var targetheight = targetdiv.offsetHeight;
	var targetwidth = targetdiv.offsetWidth;
	var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
	var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
	msg.style.top = topposition + 'px';
	msg.style.left = leftposition + 'px';
	clearInterval(msg.timer);
	msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
	if(!autohide) {
		autohide = MSGHIDE;  
	}
	window.setTimeout("hideMsg()", (autohide * 1000));
	document.getElementById('errorAlert').style.display = 'block';
}
	
// hide the form alert //
function hideMsg(msg) {
	var msg = document.getElementById('msg');
	if(!msg.timer) {
		msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
	}
}
	
// fade the message box //
function fadeMsg(flag) {
	if(flag == null) {
		flag = 1;
	}
	var msg = document.getElementById('msg');
	var value;
	if(flag == 1) {
		value = msg.alpha + MSGSPEED;
	} else {
		value = msg.alpha - MSGSPEED;
	}
	msg.alpha = value;
	msg.style.opacity = (value / 100);
	msg.style.filter = 'alpha(opacity=' + value + ')';
	if(value >= 99) {
		clearInterval(msg.timer);
		msg.timer = null;
	} else if(value <= 1) {
		msg.style.display = "none";
		clearInterval(msg.timer);
	}
}
	
// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
	var left = 0;
	if(target.offsetParent) {
		while(1) {
			left += target.offsetLeft;
			if(!target.offsetParent) {
				break;
			}
			target = target.offsetParent;
		}
	} else if(target.x) {
		left += target.x;
	}
	return left;
}
	
// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
	var top = 0;
	if(target.offsetParent) {
		while(1) {
		  top += target.offsetTop;
		  if(!target.offsetParent) {
			break;
		  }
		  target = target.offsetParent;
		}
	} else if(target.y) {
		top += target.y;
	}
	return top;
}
	
// preload the arrow //
if(document.images) {
	arrow = new Image(7,80); 
	arrow.src = "images/msg_arrow.gif"; 
}
