                                            

    $(document).ready(function(){
	
	$(function() {
	    $("#phoneDateDateTextField").datepicker({minDate: +1, maxDate: '+1M'});
	});
	
	$("#replyToMessageButton").click(function()
	{
	    $(this).siblings("#deleteMessageButton").attr("disabled", "disabled");
	    $(this).attr("disabled", "disabled");
	    //alert("caught");
	    $("#invisibleMessageSendController").attr("id", "visibleControl");
	});
	
	$("#sendMessageDateReviewButton").click(function()
	{
	   $(this).siblings("#deleteDateReviewButton").attr("disabled", "disabled");
	   $(this).attr("disabled", "disabled");
	   $("#invisibleMessageSendController").attr("id", "visibleControl");
	});
	
	$("#deleteMessageButton").click(function()
	{
	    $(this).siblings("#replyToMessageButton").attr("disabled", "disabled");
	    $(this).attr("disabled", "disabled");
	    var messageId = $("input#messageId").val();
	    $.post("http://www.speedphonedating.com/application/updateMessageStatus.php",
		   {'message_id' : messageId, 'status_id' : '2' },
		   function() {
			window.location.reload(true);
		   });  
	});
	
	$("a#gotoNewMessagesLink").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '17' },
		   function (){
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });
	});
	
	$("#personalsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '12' },
		   function (){
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });
	});
	
	$(".faqPersonalsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '12' },
		   function ()
		   {
			window.location.reload(true);
		   });
	});
	
	$(".faqEventsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '2' },
		   function ()
		   {
			window.location.reload(true);
		   });
	});
	
	$("#mainMenuEventsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '2' },
		   function (){
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });
	});

	$("input#requestPhoneDateCheckBox").click(function()
	{
	    var boxChecked = $(this).attr("checked");
	    if (boxChecked)
	    {
		$("input#phoneDateDateTextField").attr("disabled", "");
		$("select#phoneDateHourSelector").attr("disabled", "");
	    }
	    else
	    {
		$("input#phoneDateDateTextField").attr("disabled", "disabled");
		$("select#phoneDateHourSelector").attr("disabled", "disabled");
	    }
	});
	
	$("input.dateTimeRadio").click(function()
	{
	    var selected = $("input#reviseDateTimeRadio").attr("checked");
	    if (selected)
	    {
		$("input#phoneDateDateTextField").attr("disabled", "");
		$("select#phoneDateHourSelector").attr("disabled", "");
	    }
	    else
	    {
		$("input#phoneDateDateTextField").attr("disabled", "disabled");
		$("select#phoneDateHourSelector").attr("disabled", "disabled");
	    }
	})
	
	$("#m4wLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoDisplayPersonals.php",
		   {'fb_user_id' : fbUserId, 'category' : '1'},
		   function (){
			window.location.reload(true);
		   });
	});
	
	$("#w4mLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoDisplayPersonals.php",
		   {'fb_user_id' : fbUserId, 'category' : '2'},
		   function (){
			window.location.reload(true);
		   });   
	});
	
	$("#m4mLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoDisplayPersonals.php",
		   {'fb_user_id' : fbUserId, 'category' : '3'},
		   function (){
			window.location.reload(true);
		   });
	});
	
	$("#viewMorePersonalsLink").click(function()
	{
	    window.location.reload(true); 
	});
	
	$("#w4wLink").click(function()
	{
	    //alert("w4w");
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoDisplayPersonals.php",
		   {'fb_user_id' : fbUserId, 'category' : '4'},
		   function (){
			window.location.reload(true);
		   });
	});
	
	$("#submitPersonalsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoSubmitPersonal.php",
		   { 'fb_user_id' : fbUserId},
		   function () {
			window.location.reload(true);
		   });
	});
	
	$("#documentRefreshLink").click(function()
	{
	    //alert("caught");
	    window.location.reload(true);
	});
	
	$("#inviteFriendsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoInviteFriends.php",
		   { 'fb_user_id' : fbUserId },
		   function() {
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });    
	});
	
	$("#faqLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoFaq.php",
		   { 'fb_user_id' : fbUserId},
		   function() {
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });   
	});
	
	$(".earnCreditsLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var regFormCompleted = $(this).siblings(".reg_form_completed").val();
	    var gotoConnect = $(this).siblings(".gotoConnect").val();
	    $.post("http://www.speedphonedating.com/application/gotoEarnCredits.php",
		   { 'fb_user_id' : fbUserId, 'reg_form_completed' : regFormCompleted },
		   function() {
			if (gotoConnect == "true")
			    window.location = "http://www.speedphonedating.com/application/loadFbConnect.php";
			else
			    window.location.reload(true);
		   });  
	});
	
	$(".gotoSeminarsPageLink").click(function()
	{
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/gotoMySeminars.php",
		   { 'fb_user_id' : fbUserId },
		   function() {
			window.location.reload(true);
		   });  
	});
	
	$("#contactUsSubmit").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var subject = $("#subject").val();
	    var body = $("#body").val();
	    var emailAddy = $("#emailAddy").val();
	    $("#contactUsMessageBox").empty();
	    var noBlankValues = true;
	    if (subject == "")
	    {
		$("#contactUsMessageBox").append('<div class="error">You must include a subject for your message.</div>');
		$(this).attr("disabled", "");
		noBlankValues = false;
	    }
	    if (body == "")
	    {
		$("#contactUsMessageBox").append('<div class="error">You must include a body for your message.</div>');
		$(this).attr("disabled", "");
		noBlankValues = false;
	    }
	    if (emailAddy == "")
	    {
		$("#contactUsMessageBox").append('<div class="error">You must include your email address with your message.</div>');
		$(this).attr("disabled", "");
		noBlankValues = false;
	    }
	    if (noBlankValues)
	    {
		//validate email address
		var emailAddyValid = true;
		
		if (emailAddy.indexOf ('@', 0) == -1)  //check for @ character
		    { emailAddyValid = false; }
		else if (emailAddy.indexOf ( '@', 0 ) < 1)  //check for at least one character before @
		    { emailAddyValid = false; }
		else if (emailAddy.indexOf ( '.', 0 ) == -1)  //check for at least one period period
		    { emailAddyValid = false; }
		//check for proper suffix (of 2 or 3 characters)
		var len = emailAddy.length;
		var pos = emailAddy.lastIndexOf ( '.', len - 1 ) + 1;
		if ( ( len - pos ) < 2 || ( len - pos ) > 4 )
		    { emailAddyValid = false; }
		    
		if (!emailAddyValid)
		{
		    $("#contactUsMessageBox").append('<div class="error">The email address submitted does not appear to be valid.</div>');
		    $(this).attr("disabled", "");
		}
		else  //all fields filled in with something and valid email address
		{
		    $.ajax({
			url : "http://www.speedphonedating.com/application/contact-form-emailer/submitMessage.php",
			data : ({'emailAddress' : emailAddy, 'subject' : subject, 'body' : body}),
			type: "POST",
			success: function(msg)
			{
			    //window.location = "http://www.speedphonedating.com/contact-us/submissionResult.php";
			    //alert("message = " + msg);
			    if (msg == "true")
				{ $("#contactUsMessageBox").append('<div class="success">Your message was successfully delivered.</div>'); }
			    else //unkown status message
				{ $("#contactUsMessageBox").append('<div class="error">There was an error delivering your message.  Please contact us through our Facebook application profile.</div>'); }
			}
		    });
		}
	    }
	});
	
	$("#deleteDateReviewButton").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    $(this).siblings("#sendMessageDateReviewButton").attr("disabled", "disabled");
	    var fbUserId = $(this).siblings("#fbUserId").val();
	    var seminarId = $(this).siblings("#seminarId").val();
	    var anonSessionId = $(this).siblings("#anonSessionId").val();
	    $.post("http://www.speedphonedating.com/application/deleteDateReview.php",
		   {'fb_user_id' : fbUserId, 'seminar_id' : seminarId, 'anon_session_id' : anonSessionId},
		   function()
		   {
			window.location.reload(true);
		   });
	});
	
	$("#refundCreditLink").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var fbUserId = $(this).siblings("#fbUserId").val();
	    var seminarId = $(this).siblings("#seminarId").val();
	    $.post("http://www.speedphonedating.com/application/deleteDateReview.php",
		{'fb_user_id' : fbUserId, 'seminar_id' : seminarId, 'refund' : 1},
		function()
		{
		     window.location.reload(true);
		});  
	});
	
	$("#seminarAttendanceConfirmationButton").click(function()
	{
	   //alert("detected.");
	   $(this).attr("disabled", "disabled");
	   var fbUserId = $(this).siblings(".fbUserId").val();
	   var seminarId = $(this).siblings(".seminarId").val();
	   $.post("http://www.speedphonedating.com/application/confirmSeminarAttendance.php",
		{'fb_user_id' : fbUserId, 'seminar_id' : seminarId},
		function ()
		{
		    window.location.reload(true);
		});
	});
	
	$("#noPublishToFeedButton").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var creditsEmpty = $(this).siblings(".seminar_credits_empty").val();
	    $.post("http://www.speedphonedating.com/application/finish_seminar_registration.php",
	    {'fb_user_id' : fbUserId, 'seminar_credits_empty' : creditsEmpty },
	    function ()
	    {
		window.location.reload(true);
	    });  
	});
	
	$("#publishToFeedButton").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var fbEventId = $(this).siblings(".fb_event_id").val();
	    var creditsEmpty = $(this).siblings(".seminar_credits_empty").val();
	    var fbEventLink = "<a href='http://www.facebook.com/event.php?eid=" + fbEventId + "'>Speed Phone Dating Event</a>";
	    var websiteLink = "<a href='http://apps.facebook.com/speedphonedating'>Speed Phone Dating</a>";
	    var imgRef = "http://apps.facebook.com/speedphonedating";
	    var imgSrc = "http://www.speedphonedating.com/template/feedImg.gif";
	    $.post("http://www.speedphonedating.com/application/finish_seminar_registration.php",
	    {'fb_user_id' : fbUserId, 'seminar_credits_empty' : creditsEmpty },
	    function ()
	    {
		var templateData = {"event" : fbEventLink, "app_link" : websiteLink, "images" : [{ "src" : imgSrc, "href" : imgRef}]};
		var user_message_prompt = "Add personal comment?";
		var user_message = {value: "Write your comment here."};
		FB.Connect.showFeedDialog(173935174975, templateData, null, null, null, FB.RequireConnect.require,
		    function() { window.location.reload(true); } , user_message_prompt, user_message);
		
	    });
	});
	
	$("#publishPersonalToFeedButton").click(function()
	{
	    //alert("caught");
	    $(this).attr("disabled", "disabled");
	    var websiteLink = "<a href='http://apps.facebook.com/speedphonedating'>Speed Phone Dating</a>";
	    var imgRef = "http://apps.facebook.com/speedphonedating";
	    var imgSrc = "http://www.speedphonedating.com/template/feedImg.gif";
	    var templateData = {"app-link" : websiteLink, "images" : [{ "src" : imgSrc, "href" : imgRef}]};
	    var user_message_prompt = "Add personal comment?";
	    var user_message = {value: "Write your comment here."};
	    FB.Connect.showFeedDialog(173944914975, templateData, null, null, null, FB.RequireConnect.require,
		function() { window.location.reload(true); } , user_message_prompt, user_message);		
	});
	
	//seminar registration click
	$(".inputsubmit_seminar_reg").click(function ()
	{
	   $(".inputsubmit_seminar_reg").attr("disabled", "disabled"); //disable all seminar registration buttons
	   seminarId = $(this).siblings(".seminar_id").val();
	   fbUserId = $(this).siblings(".fb_user_id").val();
	    $.post("http://www.speedphonedating.com/application/register_seminar.php",
		   { 'seminar_id' : seminarId, 'fb_user_id' : fbUserId },
		   function() {
			window.location.reload(true);
		   });
	});
	
	//phone verification click
	$("#phoneVerificationButton").click(function(){
		    $(this).attr("disabled", "disabled");
		    $("#editAccountSettingsButton").attr("disabled", "disabled");
		    //jQuery(this).siblings("input").remove();  
		    var messageBlock = $("#phoneVerificationMessageBox");
		    var fbUserId = $("input#fbUserId").val();
		    var userTc = $("input#userTc").val();
		    messageBlock.append("<div class='success'>Working...Please Wait.  If this message persists longer then two minutes then please refresh this page, wait until it loads completely, and try again.</div>");
		    $(this).remove();
		    $("#editAccountSettingsButton").remove();
		    messageBlock.load("twilio/phone_verification/verifyPhoneNumber.php", { 'fb_user_id' : fbUserId, 'user_tc' : userTc } );
		    //messageBlock.append(" id returned: " + fbUserId);
		    });
	
	$("#verifyFinishedLink").live("click", function(){
	    window.location.reload(true);
	});

	
	$("#editAccountSettingsButton").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    $("#phoneVerificationButton").attr("disabled", "disabled");
	    var fbUserId = $("input#fbUserId").val();
	    //alert(fbUserId);
	    $.post("http://www.speedphonedating.com/application/gotoAccountSettings.php",
		 { 'fb_user_id' : fbUserId },
		   function() {
			window.location.reload(true);
		   });  
	});
	
	$("#openAccountButton").click(function()
	{
	    $(this).attr("disabled", "disabled");
	    var fbUserId = $("input#fbUserId").val();
	    $.post("http://www.speedphonedating.com/application/gotoAccountSettings.php",
		 { 'fb_user_id' : fbUserId },
		   function() {
			window.location.reload(true);
		   });  
	    
	});
	
	$("#personalResponseForm").submit(function()
	{
	   $("#personalResponseSubmit").attr("disabled", "disabled");
	   var errorMessages = [];
	   var errorCount = 0;
	   
	   //make sure sender is not recipient
	   var recipientId = $("#recipientFbId").val();
	   var senderId = $("#senderFbId").val();
	   if (recipientId == senderId)
	   {
		errorMessages[errorCount] = "You can not send a message to yourself.";
		errorCount++;
	   }
	   
	   //check message body
	    var disallowedCharactersRegex = /[<>]/;
	    var onlyWhiteSpaceRegex = /^\s+$/;
	    var body = $("#personalResponseTextArea").val();
	    if (body == '')
	    {
		errorMessages[errorCount] = "You must enter a message to send.";
		errorCount++;
	    }
	    var onlyWhiteSpaceMatch = body.match(onlyWhiteSpaceRegex);
	    if (onlyWhiteSpaceMatch)
	    {
		errorMessages[errorCount] = "You must enter a message to send.";
		errorCount++;
	    }
	    //check for scripts and html, < or >
	    var disallowedCharactersMatch = body.match(disallowedCharactersRegex);
	    if (disallowedCharactersMatch)
	    {
		errorMessages[errorCount] = "The message can not contain the < or > characters.";
		errorCount++;
	    }
	    //check maximum length for a description
	    var maxBodyLength = 5000;
	    if (body.length > maxBodyLength)
	    {
		errorMessages[errorCount] = "Your message has " + body.length + " characters, the maximum allowed is " + maxBodyLength + " characters.";
		errorCount++;
	    }
	    
	    //check message subject
	    var messageSubject = $("input#messageSubject").val();
	    if (messageSubject == '')
	    {
		errorMessages[errorCount] = "Please enter a subject.";
		errorCount++;
	    }
	    var onlyWhiteSpaceMatch = messageSubject.match(onlyWhiteSpaceRegex);
	    if (onlyWhiteSpaceMatch)
	    {
		errorMessages[errorCount] = "Please enter a subject.";
		errorCount++;
	    }
	    //check for scripts and html, < or >
	    var disallowedCharactersMatch = messageSubject.match(disallowedCharactersRegex);
	    if (disallowedCharactersMatch)
	    {
		errorMessages[errorCount] = "The subject can not contain the < or > characters.";
		errorCount++;
	    }
	    //check maximum length for a description
	    var maxSubjectLength = 100;
	    if (messageSubject.length > maxSubjectLength)
	    {
		errorMessages[errorCount] = "Your subject has " + messageSubject.length + " characters, the maximum allowed is " + maxSubjectLength + " characters.";
		errorCount++;
	    }
	    
	    //check if phone date requested, if so: validate date and time, check credits available
	    var boxChecked = $("input#requestPhoneDateCheckBox").attr("checked");
	    if (boxChecked)
	    {
		//check available credits
		var availableCredits = $("#availableCredits").val();
		if (availableCredits < 1)
		{
		    errorMessages[errorCount] = "You must have at least 1 phone date credit to request a phone date.";
		    errorCount++;
		}
		
		//check date of phone date
		var phoneDateDate = $("#phoneDateDateTextField").val();
		var phoneDateHour = $("#phoneDateHourSelector").val();
		var errorMessagesArray = verifyPhoneDateDateTime(phoneDateDate, phoneDateHour);
		for (var dateError in errorMessagesArray)
		{
		    errorMessages[errorCount] = errorMessagesArray[dateError];
		    errorCount++;
		}
	    }
	    
	    //check to see if a phone date was accepted
	    var acceptPhoneDateSelected = $("input#acceptDateTimeRadio").attr("checked");
	    var revisePhoneDateSelected = $("input#reviseDateTimeRadio").attr("checked");
	    if (acceptPhoneDateSelected || revisePhoneDateSelected)
	    {
		if (acceptPhoneDateSelected && revisePhoneDateSelected)
		{
		    errorMessages[errorCount] = "You can not acceptance a phone date and submit a revision of a phone date.";
		    errorCount++;
		}
		if (revisePhoneDateSelected)
		{
		    var phoneDateDate = $("#phoneDateDateTextField").val();
		    var phoneDateHour = $("#phoneDateHourSelector").val();
		    var errorMessagesArray = verifyPhoneDateDateTime(phoneDateDate, phoneDateHour);
		    for (var dateError in errorMessagesArray)
		    {
			errorMessages[errorCount] = errorMessagesArray[dateError];
			errorCount++;
		    }
		}
	    }
	    $("#personalResponseMessageBox").empty();
	    if (errorCount > 0)
	    {
		for (var i in errorMessages)
		{
		    $("#personalResponseMessageBox").append('<div class="error">' + errorMessages[i] + '</div>');
		}
		$("#personalResponseSubmit").attr("disabled", "");
		return false;  //do not submit form
	    }
	    else
	    {
		$("#personalResponseMessageBox").append('<div class="success">Submission successful.  Please wait while we process your message.</div>');
		return true;
	    }
	    return false;  
	});
	
	function verifyPhoneDateDateTime(phoneDateDate, phoneDateHour)
	{
	    var errorMessages = [];
	    var errorCount = 0;
	    var phoneDateDateRegex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})/;
	    var phoneDateDateMatch = phoneDateDate.match(phoneDateDateRegex);
	    var month;
	    var day;
	    var year;
	    if (phoneDateDateMatch)
	    {
		var month = phoneDateDateMatch[1];
		var day = phoneDateDateMatch[2];
		var year = phoneDateDateMatch[3];
		
		//check for valid dates and that the date is set to occur in the future
		if (month > 0 && month < 13)
		{
		    if (day > 0)
		    {
			if (month == 4 || month == 6 || month == 9 || month == 11)  //months with 30 days
			{
			    if (day > 30)
			    {
				errorMessages[errorCount] = "April, June, September, and November only have 30 days.";
				errorCount++;
			    }
			}
			else if (month == 2)
			{
			    if (day > 29)
			    {
				errorMessages[errorCount] = "February only has a maximum of 29 days.";
				errorCount++;
			    }
			}
			else
			{
			    if (day > 31)
			    {
				errorMessages[errorCount] = "No month has greater then 31 days.";
				errorCount++;
			    }
			}
		    }
		    else
		    {
			errorMessages[errorCount] = "The date for the phone date request must have a day of the month greater then 0.";
			errorCount++;
		    }
		}
		else
		{
		    errorMessages[errorCount] = "The date for the phone date request must have a month between the values of 1 and 12.";
		    errorCount++;
		}
	    }
	    else  //error
	    {
		var currentTime = new Date();
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		errorMessages[errorCount] = "The date for the phone date must be in the format of mm/dd/yyyy, for example today is " + month + "/" + day + "/" + year + ".";
		errorCount++;
	    }
	    
	    //check hour of phone date
	    var phoneDateHourRegex = /^\d{1,2}$/;
	    var phoneDateHourMatch = phoneDateHour.match(phoneDateHourRegex);
	    if (phoneDateHourMatch)
	    {
		if (phoneDateHour < 0 || phoneDateHour > 23)
		{
		    errorMessages[errorCount] = "Please select a valid hour for the phone date.";
		    errorCount++;
		}
	    }
	    else
	    {
		errorMessages[errorCount] = "Please select a valid hour for the phone date.";
		errorCount++;
	    }
	    
	    var dateChosen = new Date(year, month - 1, day,0,0,0);
	    //check to make sure date chosen occurs tomorrow or later
	    var tomorrowsDate = new Date();
	    tomorrowsDate.setDate(tomorrowsDate.getDate() + 1);
	    tomorrowsDate.setHours(0);
	    tomorrowsDate.setMinutes(0);
	    tomorrowsDate.setSeconds(0);
	    tomorrowsDate.setMilliseconds(0);
	    if (dateChosen.getTime() < tomorrowsDate.getTime())
	    {
		errorMessages[errorCount] = "Please select a date that occurs tomorrow or later for the phone date request.";
		errorCount++;
	    }
	    
	    //check to make sure date chosen occurs less then one month from now
	    var oneMonthFromNowDate = new Date();
	    oneMonthFromNowDate.setMonth(oneMonthFromNowDate.getMonth() + 1);
	    oneMonthFromNowDate.setHours(0);
	    oneMonthFromNowDate.setMinutes(0);
	    oneMonthFromNowDate.setSeconds(0);
	    oneMonthFromNowDate.setMilliseconds(0);
	    if (dateChosen.getTime() > oneMonthFromNowDate.getTime())
	    {
		errorMessages[errorCount] = "Please select a date that is less then one month from now for the phone date request.";
		errorCount++;
	    }
	    
	    return errorMessages;
	}
	
	$(".personalDetailsLink").click(function(){
	    var fbUserId = $(this).siblings(".fb_user_id").val();
	    var personalId = $(this).siblings(".personal_id").val();
	    //alert("fb user id= " + fbUserId + ",personal id = " + personalId);
	    $.post("http://www.speedphonedating.com/application/gotoRegistrationStage.php",
		   { 'fb_user_id' : fbUserId, 'reg_stage' : '15', 'reg_stage_data' : "personalIdForDetails=" + personalId },
		   function (){
			window.location.reload(true);
		   });
	});
	
	$("#submitPersonalForm").submit(function ()
	{
	    //alert("caught");
	    $("#personalSubmit").attr("disabled", "disabled");
	    var errorMessages = [];
	    var errorCount = 0;
	    
	    //check gender
	    var gender = $("select#userGender").val();
	    if (gender != "Man")
	    {
		if (gender != "Woman")
		{
		    errorMessages[errorCount] = "Please enter a valid gender.";
		    errorCount++;
		}
	    }
	    
	    //check seeking gender
	    var seekingGender = $("select#interestedInGender").val();
	    if (seekingGender != "Men")
	    {
		if (seekingGender != "Women")
		{
		    if (seekingGender != "Men and Women")
		    {
			errorMessages[errorCount] = "Please specify a valid seeking gender.";
			errorCount++;
		    }
		}
	    }
	    
	    //check age
	    var age= $("#age_control").val();
	    var ageRegex = /^\d{2,3}$/;
	    var ageMatch = age.match(ageRegex);
	    if (!ageMatch)
	    {
		errorMessages[errorCount] = 'Your age must be a number.';
		errorCount++;
	    }
	    if (age < 18)
	    {
		errorMessages[errorCount] = "You must be at least 18 years of age to submit a personal.";
		errorCount++;
	    }
	    else if (age > 150)
	    {
		errorMessages[errorCount] = "Please enter your real age.";
		errorCount++;
	    }
	    
	    //check title
	    var title = $("#personalTitle").val();
	    if (title == '')
	    {
		errorMessages[errorCount] = "You must enter a title for your personal.";
		errorCount++;
	    }
	    var onlyWhiteSpaceRegex = /^\s+$/;
	    var onlyWhiteSpaceMatch = title.match(onlyWhiteSpaceRegex);
	    if (onlyWhiteSpaceMatch)
	    {
		errorMessages[errorCount] = "You must enter a title for your personal.";
		errorCount++;
	    }
	    //check for scripts and html, < or >
	    var disallowedCharactersRegex = /[<>]/;
	    var disallowedCharactersMatch = title.match(disallowedCharactersRegex);
	    if (disallowedCharactersMatch)
	    {
		errorMessages[errorCount] = "The title can not contain the < or > characters.";
		errorCount++;
	    }
	    //check maximum length for a title
	    var maxTitleLength = 50;
	    if (title.length > maxTitleLength)
	    {
		errorMessages[errorCount] = "Your personal's title has " + title.length + " characters, the maximum allowed is " + maxTitleLength + " characters.";
		errorCount++;
	    }
	    
	    //check description
	    var description = $("#personalDescriptionTextBox").val();
	    if (description == '')
	    {
		errorMessages[errorCount] = "You must enter a description for your personal.";
		errorCount++;
	    }
	    var onlyWhiteSpaceMatch = description.match(onlyWhiteSpaceRegex);
	    if (onlyWhiteSpaceMatch)
	    {
		errorMessages[errorCount] = "You must enter a description for your personal.";
		errorCount++;
	    }
	    //check for scripts and html, < or >
	    var disallowedCharactersMatch = description.match(disallowedCharactersRegex);
	    if (disallowedCharactersMatch)
	    {
		errorMessages[errorCount] = "The description can not contain the < or > characters.";
		errorCount++;
	    }
	    //check maximum length for a description
	    var maxDescriptionLength = 5000;
	    if (description.length > maxDescriptionLength)
	    {
		errorMessages[errorCount] = "Your personal's description has " + description.length + " characters, the maximum allowed is " + maxDescriptionLength + " characters.";
		errorCount++;
	    }
	    
	    $("#personalsMessageBox").empty();
	    if (errorCount > 0)
	    {
		for (var i in errorMessages)
		{
		    $("#personalsMessageBox").append('<div class="error">' + errorMessages[i] + '</div>');
		}
		$("#personalSubmit").attr("disabled", "");
		return false;  //do not submit form
	    }
	    else
	    {
		$("#personalsMessageBox").append('<div class="success">Submission successful.  Please waite while we process your personal\'s information.</div>');
		return true;
	    }
	});
	
	//registration submission form click
	$("#regForm").submit(function(){
	    $("#regSubmit").attr("disabled", "disabled");
	    //validate registration form
	    var errorMessages = [];
	    var errorCount = 0;
	    
	    var fbUserId = $("input#fbUserId").val();
	    
	    //check gender
	    var gender = $("select#userGender").val();
	    if (gender != "Man")
	    {
		if (gender != "Woman")
		{
		    errorMessages[errorCount] = "Please enter a valid gender.";
		    errorCount++;
		}
	    }
	    
	    //check gender interested in
	    var interestedInGender = $("select#interestedInGender").val();
	    if (interestedInGender != "Men")
	    {
		if (interestedInGender != "Women")
		{
		    if (interestedInGender != "Men and Women")
		    {
			errorMessages[errorCount] = "Please enter a valid gender for phone date partners.";
			errorCount++;
		    }
		}
	    }
	    
	    //check time zone
	    var timeZone = $("select#timeZoneSelect").val();
	    var validTimeZoneEntered = false;
	    if (timeZone == "Pacific Standard Time (UTC - 8)")
		{ validTimeZoneEntered = true; }
	    else if (timeZone == "Mountain Standard Time (UTC - 7)")
		{ validTimeZoneEntered = true; }
	    else if (timeZone == "Central Standard Time (UTC - 6)")
		{ validTimeZoneEntered = true; }
	    else if (timeZone == "Eastern Standard Time (UTC - 5)")
		{ validTimeZoneEntered = true; }
	    if (!validTimeZoneEntered)
	    {
		errorMessages[errorCount] = "Please enter a valid time zone.";
		errorCount++;
	    }
	    
	    //check residence
	    var residence = $("select#countrySelect").val();
	    if (residence == "Somewhere Else")
	    {
		errorMessages[errorCount] = "Unfortunately, SpeedPhoneDating.com is only able to offer our services to people living in the United States and Canada. <a href='http://www.facebook.com'>Click here to go to Facebook.</a>";
		errorCount++;
	    }
	    
	    //check phone number
	    var phoneNumber = $("input#phoneNumberTextInput").val();
	    phoneNumberValid = false;
	    var digits10Regex = /^\d{10}$/;
	    var matchMade = phoneNumber.match(digits10Regex);
	    if (matchMade)
	    {
		phoneNumberValid = true;
	    }
	    if (!phoneNumberValid)
	    {
		errorMessages[errorCount] = "Please enter extactly 10 digits for your phone number.";
		errorCount++;
	    }
	    
	    //check interested-in ages
	    var minJoinAge = 18;
	    var interestedInStartAge = $("input#interestedInStartAgeInputBox").val();
	    var interestedInEndAge = $("input#interestedInEndAgeInputBox").val();
	    var ageRegex = /^\d{2,3}$/;
	    var startMatch = interestedInStartAge.match(ageRegex);
	    var endMatch = interestedInEndAge.match(ageRegex);
	    if (!startMatch)
	    {
		errorMessages[errorCount] = 'The minimum age for speed phone date partners must be a number.';
		errorCount++;
	    }
	    if (!endMatch)
	    {
		errorMessages[errorCount] = 'The maximum age for speed phone date partners must be a number.';
		errorCount++;
	    }
	    if (startMatch > endMatch)
	    {
		errorMessages[errorCount] = 'The minimum age for speed phone date partners must be less then or equal to the maximum age for speed phone date partners.';
		errorCount++;
	    }
	    
	    //check birthdate
	    var birthMonth = $("input#birthMonthInputBox").val();
	    var birthDateDay = $("input#birthDateDayInputBox").val();
	    var birthYear = $("input#birthYearInputBox").val();
	    var monthDayRegex = /^\d{1,2}$/;
	    var yearRegex = /^\d{4}$/;
	    var monthMatch = birthMonth.match(monthDayRegex);
	    var dayMatch = birthDateDay.match(monthDayRegex);
	    var yearMatch = birthYear.match(yearRegex);
	    if (monthMatch)  //check valid range
	    {
		if (birthMonth < 1 || birthMonth > 12)
		{
		    errorMessages[errorCount] = 'Please enter a number between 1 and 12 for your birth month.';
		    errorCount++;
		}
	    }
	    else  //did not enter a number
	    {
		errorMessages[errorCount] = 'Please enter a number between 1 and 12 for your birth month.';
		errorCount++;
	    }
	    if (dayMatch)  //check valid range
	    {
		if (birthDateDay > 31 || birthDateDay <= 0)
		{
		    errorMessages[errorCount] = 'Please enter a valid birth date.  No month has more then 31 days or less then 1 day.';
		    errorCount++;
		}
		else if (birthMonth == 4 || birthMonth == 6 || birthMonth == 9 || birthMonth == 11)
		{
		    //if month is april, june, september, or nov then only 30 days in the month
		    if (birthDateDay > 30)
		    {
			errorMessages[errorCount] = 'Please enter a valid birth date.  April, June, September, and November have 30 days in a month.';
			errorCount++;
		    }
		}
		else if (birthMonth == 2)  //Feb, max days is 29
		{
		    if (birthDateDay > 29)
		    {
			errorMessages[errorCount] = 'Please enter a valid birth date.  February can have a maximum of 29 days.';
			errorCount++;
		    }
		}
	    }
	    else  //did not enter a number
	    {
		errorMessages[errorCount] = 'Please enter a number between 1 and 31 for your birth date day.';
		errorCount++;
	    }
	    if (yearMatch) //check valid range
	    {
		if (birthYear < 1900 || birthYear > new Date().getFullYear())
		{
		    errorMessages[errorCount] = 'Please enter a valid birth year, 1900 or greater.';
		    errorCount++;
		}
	    }
	    else //did not enter valid year
	    {
		errorMessages[errorCount] = 'Please enter a valid 4 digit year for your birth year.';
		errorCount++;
	    }
	    
	    //check over 18
	    var currentDateTime = new Date();
	    var birthDate = new Date(birthYear, birthMonth - 1, birthDateDay);  //-1 on month, js uses 0-11
	    var secondsAlive = (currentDateTime.getTime() - birthDate.getTime()) / 1000;
	    var yearsAlive = secondsAlive / 31557826;  //seconds in a year 31556926 + 900 seconds, 15 minutes
	    yearsAlive = Math.floor(yearsAlive);
	    if (yearsAlive < minJoinAge)
	    {
		errorMessages[errorCount] = 'I\'m sorry.  We can not complete your account registration.  All users must be 18 years of age or older. <a href=\'http://www.facebook.com\'>Click here to go to Facebook.</a>';
		errorCount++;
	    }
	    
	    $("#registrationMessageBox").empty();
	    if (errorCount > 0)
	    {
		for (var i in errorMessages)
		{
		    $("#registrationMessageBox").append('<div class="error">' + errorMessages[i] + '</div>');
		}
		$("#regSubmit").attr("disabled", "");
		return false;  //do not submit form
	    }
	    else  //no errors, submit form
	    {
		$("#registrationMessageBox").append('<div class="success">Submission successful.  Please waite while we process your account information.</div>');
		return true;
	    }
	    });
	
	});
