//function is attached to shaking event from an iPhone or iPad
function onShake(){

}

//function to place the contact email address in an element.
//the purpose is to hide the email address from bots by using js to place the text
function showEmailAddress(){
	var em = 'info';
	var em2 = '@';
	var em3 = 'alpineachievement';
	var em4 = '.com';
	$(".email-address").html('<a href="mailto:'+em+em2+em3+em4+'">'+em+em2+em3+em4+'</a>');
}

//function to remove the spam-bot only elements. Normal users with js will not see these elements
function hideSpamCatcher(){
	$(".verify").remove();
}

//function to place the logo at the top of the z-index
function placeLogo(){
	$("#header").html('');
}

//function to clear login forms
function clearFormLabels(){
	if($("#login_password").val() != ''){
		$("#login_password").siblings('span').text('');
	}
}

//function to validate contact form
function validateContactForm(){
	//gather the field values
	var subject = $("#contact_subject").val();
	var email = $("#contact_email").val();
	var name = $("#contact_name").val();
	var message = $("#contact_message").val();
	var timestamp = $("#timestamp").val();
	
	//init an error message and count
	var error = '<strong>Please check the following errors:</strong>';
	var err_count = 0;
	
	if(isEmpty(subject)){
		err_count++;
		error += '<br /><strong>Subject</strong> - Choose a subject that most closely matches the reason for your email.';
		$("#contact_subject").removeClass('pass');
		$("#contact_subject").addClass('fail');
	}else{
		$("#contact_subject").removeClass('fail');
		$("#contact_subject").addClass('pass');
	}
	
	if(isEmpty(email) || !isEmail(email)){
		err_count++;
		error += '<br /><strong>Email Address</strong> - You must give us your email address so we can get back to you.';
		$("#contact_email").removeClass('pass');
		$("#contact_email").addClass('fail');
	}else{
		$("#contact_email").removeClass('fail');
		$("#contact_email").addClass('pass');
	}
	
	if(isEmpty(name) || !isName(name)){
		err_count++;
		error += '<br /><strong>Full Name</strong> - Please include your full name.';
		$("#contact_name").removeClass('pass');
		$("#contact_name").addClass('fail');
	}else{
		$("#contact_name").removeClass('fail');
		$("#contact_name").addClass('pass');
	}
	
	if(isEmpty(message)){
		err_count++;
		error += '<br /><strong>Message</strong> - Please include a message.';
		$("#contact_message").removeClass('pass');
		$("#contact_message").addClass('fail');
	}else{
		$("#contact_message").removeClass('fail');
		$("#contact_message").addClass('pass');
	}
	
	if(err_count > 0){
		$("#error_box").html(error);
		$("#error_box").fadeIn('slow');
		//scroll the window to the top of the error box
		var offset = $("#error_box").offset()
		window.scrollTo(0,offset.top);
	}else{
		$("#error_box").hide();
		
		//send out the data via ajax
		var pars = 'ajax=true&timestamp='+timestamp+'&contact_subject='+subject+'&contact_email='+email+'&contact_name='+name+'&contact_message='+message;
		
		$.ajax({
				type: "POST",
				url:	"form.contact.php",
				data:	pars,
				success:	function(msg){
					if(msg == 0){
						$("#error_box").html(error);
						$("#error_box").fadeIn('slow');
						//scroll the window to the top of the error box
						var offset = $("#error_box").offset()
						window.scrollTo(0,offset.top);	
					}else{
						$("#success_box").fadeIn('slow');
						document.contact_form.reset();
						//scroll the window to the top of the success box
						var offset = $("#success_box").offset()
						window.scrollTo(0,offset.top);
					}
				}
		});
		return false;
	
	}
	
	
	return false;
}

//function to validate demo request form
function validateDemoForm(){
	//gather the field values
	var fullname = $("#demo_name").val();
	var title = $("#demo_title").val();
	var email = $("#demo_email").val();
	var phone_number = $("#demo_phone_number").val();
	var contact_time_day = $("#demo_contact_time_day").val();
	var contact_time_time = $("#demo_contact_time_time").val();
	var district = $("#demo_district").val();
	var state = $("#demo_state").val();
	var school_number = $("#demo_school_number").val();
	var student_number = $("#demo_student_number").val();
	var quote_option = $("#demo_quote_option").val();
	var needs = $("#demo_needs").val();
	var questions = $("#demo_questions").val();
	var referral = $("#demo_referral").val();
	var timestamp = $("#timestamp").val();
	
	//init an error message and count
	var error = '<strong>Please check the following errors:</strong>';
	var err_count = 0;
	
	if(isEmpty(fullname) || !isName(fullname)){
		err_count++;
		error += '<br /><strong>Full Name</strong> - Please include your full name.';
		$("#demo_name").removeClass('pass');
		$("#demo_name").addClass('fail');
	}else{
		$("#demo_name").removeClass('fail');
		$("#demo_name").addClass('pass');
	}
	
	if(title.length > 50){
		err_count++;
		error += '<br /><strong>Title</strong> - Please just include a short title.';
		$("#demo_title").removeClass('pass');
		$("#demo_title").addClass('fail');
	}else{
		$("#demo_title").removeClass('fail');
		$("#demo_title").addClass('pass');
	}
	
	if(!isEmail(email)){
		err_count++;
		error += '<br /><strong>Email Address</strong> - You must give us your email address so we can get back to you.';
		$("#demo_email").removeClass('pass');
		$("#demo_email").addClass('fail');
	}else{
		$("#demo_email").removeClass('fail');
		$("#demo_email").addClass('pass');
	}
	
	if(!isEmpty(phone_number) && !isPhoneNumber(phone_number)){
		err_count++;
		error += '<br /><strong>Daytime Phone Number</strong> - The phone number you entered is not valid. Please make sure to include your area code.';
		$("#demo_phone_number").removeClass('pass');
		$("#demo_phone_number").addClass('fail');
	}else{
		$("#demo_phone_number").removeClass('fail');
		$("#demo_phone_number").addClass('pass');
	}
	
	if(isEmpty(contact_time_day)){
		err_count++;
		error += '<br /><strong>Best Time to Contact</strong> - Please indicate the best day to contact you.';
		$("#demo_contact_time_day").removeClass('pass');
		$("#demo_contact_time_day").addClass('fail');
	}else{
		$("#demo_contact_time_day").removeClass('fail');
		$("#demo_contact_time_day").addClass('pass');
	}
	
	if(isEmpty(contact_time_time)){
		err_count++;
		error += '<br /><strong>Best Time to Contact</strong> - Please indicate the best time of day to contact you.';
		$("#demo_contact_time_time").removeClass('pass');
		$("#demo_contact_time_time").addClass('fail');
	}else{
		$("#demo_contact_time_time").removeClass('fail');
		$("#demo_contact_time_time").addClass('pass');
	}
	
	if(district.length > 100){
		err_count++;
		error += '<br /><strong>School District/Organization Name</strong> - Please only include the name of your district or organization.';
		$("#demo_district").removeClass('pass');
		$("#demo_district").addClass('fail');
	}else{
		$("#demo_district").removeClass('fail');
		$("#demo_district").addClass('pass');
	}
	
	if(isEmpty(state)){
		err_count++;
		error += '<br /><strong>State</strong> - Please indicate your state.';
		$("#demo_state").removeClass('pass');
		$("#demo_state").addClass('fail');
	}else{
		$("#demo_state").removeClass('fail');
		$("#demo_state").addClass('pass');
	}
	
	if(school_number > 1000){
		err_count++;
		error += '<br /><strong>Number of Schools</strong> - Please only include the approximate number of schools in your district or group.';
		$("#demo_school_number").removeClass('pass');
		$("#demo_school_number").addClass('fail');
	}else{
		$("#demo_school_number").removeClass('fail');
		$("#demo_school_number").addClass('pass');
	}
	
	if(student_number > 1000000){
		err_count++;
		error += '<br /><strong>Approximate Number of Students</strong> - Please only include the approximate number of students.';
		$("#demo_student_number").removeClass('pass');
		$("#demo_student_number").addClass('fail');
	}else{
		$("#demo_student_number").removeClass('fail');
		$("#demo_student_number").addClass('pass');
	}
	
	if(isEmpty(quote_option)){
		err_count++;
		error += '<br /><strong>Main Interest</strong> - Please indicate your main interest.';
		$("#demo_quote_option").removeClass('pass');
		$("#demo_quote_option").addClass('fail');
	}else{
		$("#demo_quote_option").removeClass('fail');
		$("#demo_quote_option").addClass('pass');
	}
	
	if(needs.length > 2500){
		err_count++;
		error += '<br /><strong>Key Needs</strong> - Please limit your key needs to 2500 characters or less.';
		$("#demo_needs").removeClass('pass');
		$("#demo_needs").addClass('fail');
	}else{
		$("#demo_needs").removeClass('fail');
		$("#demo_needs").addClass('pass');
	}
	
	if(questions.length > 2500){
		err_count++;
		error += '<br /><strong>Like Us to Know</strong> - Please limit what you would like us to know to 2500 characters or less.';
		$("#demo_questions").removeClass('pass');
		$("#demo_questions").addClass('fail');
	}else{
		$("#demo_questions").removeClass('fail');
		$("#demo_questions").addClass('pass');
	}
	
	if(referral.length > 2500){
		err_count++;
		error += '<br /><strong>How you heard about Alpine</strong> - Please limit how you heard of Alpine to 2500 characters or less.';
		$("#demo_referral").removeClass('pass');
		$("#demo_referral").addClass('fail');
	}else{
		$("#demo_referral").removeClass('fail');
		$("#demo_referral").addClass('pass');
	}

	if(err_count > 0){
		$("#error_box").html(error);
		$("#error_box").fadeIn('slow');
		
		//scroll the window to the top of the error box
		var offset = $("#error_box").offset()
		window.scrollTo(0,offset.top);		
	}else{
		$("#error_box").hide();
		
		//send out the data via ajax
		var pars = 'ajax=true&timestamp='+timestamp+'&demo_name='+fullname+'&demo_title='+title+'&demo_email='+email+'&demo_phone_number='+phone_number+'&demo_contact_time_day='+contact_time_day+'&demo_contact_time_time='+contact_time_time+'&demo_district='+district+'&demo_state='+state+'&demo_school_number='+school_number+'&demo_student_number='+student_number+'&demo_quote_option='+quote_option+'&demo_needs='+needs+'&demo_questions='+questions+'&demo_referral='+referral;
		
		$.ajax({
				type: "POST",
				url:	"form.demo.php",
				data:	pars,
				success:	function(msg){
					if(msg == 0){
						$("#error_box").html(error);
						$("#error_box").fadeIn('slow');
						//scroll the window to the top of the error box
						var offset = $("#error_box").offset()
						window.scrollTo(0,offset.top);
					}else{
						$("#success_box").fadeIn('slow');
						document.demo_form.reset();
						//scroll the window to the top of the success box
						var offset = $("#success_box").offset()
						window.scrollTo(0,offset.top);
					}
				}
		});
		return false;
	
	}
	
	
	return false;
	
}

function validateLoginForm(){

	//gather the field values
	var username = $("#login_username").val();
	var password = $("#login_password").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(username) || username == 'username'){
		err_count++;
		$("#login_username").removeClass('pass');
		$("#login_username").addClass('fail');
	}else{
		$("#login_username").removeClass('fail');
		$("#login_username").addClass('pass');
	}
	
	if(isEmpty(password)){
		err_count++;
		$("#login_password").removeClass('pass');
		$("#login_password").addClass('fail');
	}else{
		$("#login_password").removeClass('fail');
		$("#login_password").addClass('pass');
	}
	
	if(!err_count){
		document.login_form.submit();
	}
	
	return false;
}

function validatePasswordReset(){
	
	//gather the field values
	var confirm = $("#reset_password_confirm").val();
	var password = $("#reset_password").val();
	var strength = $("#strong_password_required").val();
	var user_id = $("#reset_user_id").val();
	
	//init an error message and count
	var err_count = 0;

	if(isStrongPassword(password,strength) && isStrongPassword(confirm,strength) && password == confirm){
		$("#reset_password").removeClass('fail');
		$("#reset_password").addClass('pass');
		$("#reset_password_confirm").removeClass('fail');
		$("#reset_password_confirm").addClass('pass');
	}else{
		err_count++;
		$("#reset_password").removeClass('pass');
		$("#reset_password").addClass('fail');
		$("#reset_password_confirm").removeClass('pass');
		$("#reset_password_confirm").addClass('fail');
	}

	if(err_count == 0){
		
		var pars = 'reset_user_id='+user_id+'&strong_password_required='+strength+'&reset_password='+password+'&reset_password_confirm='+confirm;
		$.ajax({
				type: "POST",
				url:	"form.password-reset.php",
				data:	pars,
				success:	function(msg){
					
					if(msg == 'match'){
						$("#error_box").text('Passwords must be between 6 and 30 characters long, not include common words or the username, and include at least a lowercase letter, a number, and an uppercase letter or non-alphanumeric character.\nYour passwords did not match.');
						$("#error_box").fadeIn(200);
					}else if(msg == 'strength'){
						$("#error_box").text('Passwords must be between 6 and 30 characters long, not include common words or the username, and include at least a lowercase letter, a number, and an uppercase letter or non-alphanumeric character.');
						$("#error_box").fadeIn(200);
					}else{
						$("#error_box").fadeOut();
						$("#success_box").fadeIn(200);
						window.location = "thanks/reset";
					}
				}
		});
		
		return false;
		
	}else{
		//$("#error_box").text('Please make sure your new password is 6-30 characters long and contains at least 1 of each: Uppercase letter, lowercase letter, and number.');
		$("#error_box").fadeIn(200);
	}

	return false;
}


//function will get the password criteria
function getPasswordSecurityMessage(pwd,strength,username){
	//send out the data via ajax
		var pars = 'password='+pwd+'&strength='+strength+'&username='+username;
		
		$.ajax({
				type: "POST",
				url:	"ajax.test-password-strength.php",
				data:	pars,
				success:	function(msg){
					alert(msg);
				}
		});
		
}


function showMore(elem){	
	//grab the link element that triggered this event
	var link = elem.toString()+'_link';
	
	//hide all the other "show more" links (accordian effect)
	$(".see_more").each( function(){
		var this_id = $(this).attr('id');
		if(this_id != elem){ showLess(this_id); }
	} );
	
	//now, show the rest of the details and get the link ready to start hiding
	$("#"+elem).slideDown();
	$("#"+link+" a").removeClass('more');
	$("#"+link+" a").addClass('less');
	$("#"+link+" a").text('show less');
	$("#"+link+" a").attr('href',"javascript:showLess('"+elem+"');");
}

function showLess(elem){
	//grab the link that belongs to this block
	var link = elem.toString()+'_link';
	
	//hide the content and get the link ready to start showing
	$("#"+elem).slideUp();
	$("#"+link+" a").removeClass('less');
	$("#"+link+" a").addClass('more');
	$("#"+link+" a").text('show more');
	$("#"+link+" a").attr('href',"javascript:showMore('"+elem+"');");
}

/* show/hide FAQ answers */
function showAnswer(elem_id){
	var answer_id = elem_id.toString().replace('faq','faq_answer_');
	
	//hide all the other answers (accordian effect)
	$(".faq_answer").each( function(){
		var this_id = $(this).attr('id');
		if(this_id != answer_id){ $("#"+this_id).hide(); }
	} );
	
	$("#"+answer_id).fadeIn(100);
}

function faqFilter(){
	
	//reset the "show" flag
	$(".faq_question").each( function(){
		$(this).removeClass('nohide');
	} );
	
	var str = $("#filter_string").val();
	var pars = 'term='+str;
	
	$.ajax({
				type: "POST",
				url:	"ajax.faq-filter.php",
				data:	pars,
				success:	function(msg){
					
					var ids = msg.split(',');
										
					if(ids.length > 0 && ids != ''){
					
						//hide the error message
						$("#faq_error").hide();
						
						//add a special flag to the answers we want to show
						for(var i=0;i<ids.length;i++){
							var this_id = "faq_question_"+ids[i];
							$("#"+this_id).addClass('nohide');
						}
					
						//then remove all the answers
						$(".faq_category").fadeOut(200);
						$(".faq_question").each( function(){
							$(this).fadeOut(200);
						} );
						
						//and show the matching questions/answers
						$(".nohide").each( function(){
							var par_id = $(this).attr('id');
							$(this).fadeIn(500, showFilterAnswer(par_id));
						} );
						
						//and show the search results header
						$(".serp").delay(200).fadeIn(500);
						
					}else{
						//remove all the answers
						$(".faq_category").fadeOut(200);
						$(".faq_question").each( function(){
							$(this).fadeOut(200);
						} );
						
						$(".serp").fadeOut(200);
						
						//show the error message
						$("#faq_error").show();
					}
					
				}
		});
		
	return false;
}

function filterByTag(tag){
	$("#filter_string").val(tag);
	faqFilter();
}

function showFilterAnswer(par_id){
	$("#"+par_id+" > .faq_answer").delay(200).fadeIn(500);
}

//function to reset the filter - showing all Q/A again
function faqFilterReset(){
	$("#filter_string").val($("#filter_string").attr('title'));
	
	$(".faq_question").each( function(){
		$(this).removeClass('nohide');
		$(this).hide();
	} );
	
	$(".faq_answer").hide();
	$(".faq_category").delay(250).fadeIn(500);
	$(".faq_question").delay(250).each(function(){ $(this).fadeIn(500); });
	
	//hide the error message
	$("#faq_error").hide();
	
	$(".serp").fadeOut(200);
}

/* showing/hiding modal boxes */
function showOverviewVideo(){
	showBox();
	$("#modal").html(makeVideo());
	var offset = $("#modal").offset().top;
	var winheight = $(window).height();
	var csstop = parseInt(winheight/2) + parseInt(offset);
	
	var elemheight = $("#modal").height();
	var elemwidth = $("#modal").width();

	//$("#modal").css('width',elemwidth+'px');
	$("#modal").css('margin-left',(elemwidth/2)*-1+'px');
		
	var toppos = csstop - elemheight/2;
	$("#modal").css('top',toppos+'px');
	$("#modal").show();
}

function makeVideo(){
	var movurl = '<div class="video_container">';
			movurl +='<video id="html_video" class="video-player" width="720" height="540" controls="controls" preload="auto" autoplay="autoplay" poster="im/poster720x540.gif">';
			movurl +='<source src="downloads/Overview-Video.m4v" type="video/mp4" />';
			movurl +='<source src="downloads/Overview-Video.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />';
			movurl +='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="801" height="632" id="flash_video" class="video-player" title="Overview Video">';
			movurl +='<param name="movie" value="Overview-Video.swf" /><param name="quality" value="high" />';
			movurl +='<param name="wmode" value="opaque" />';
			movurl +='<embed src="downloads/Overview-Video.swf" width="801" height="632" />';
			movurl +='</object>';
			movurl +='</video>';
			movurl +='</div><a href="javascript:hideVideo()">';
			movurl += '<img src="im/btn_close.png" alt="Close Window" class="hideVideo" /></a>';
	return movurl;	
}

function hideVideo(){
	$("#modal").hide();
	hideBox();
	$("#modal").html('');
	clearHashTag();
}

function showTermsOfService(terms,user_id,subdomain){
	showBox();
	$("#modal").html('<div id="tos"><h3 class="copse">Accept Terms of Use</h3><div class="scroller">'+terms+'</div><div class="hdiv"></div><form action="accept.php" name="tos_form" method="post"><input type="hidden" name="user_id" value="'+user_id+'" /><a href="javascript:document.tos_form.submit()" class="btn33"><img src="im/btn_accept_terms.gif" alt="Accept Terms" /></a> <a href="https://'+subdomain+'.myalpine.com/logout.php" class="cancel_form">cancel</a></form></div>');
	var offset = $("#modal").offset().top;
	var winheight = $(window).height();
	var csstop = parseInt(winheight/2) + parseInt(offset);
	
	var elemheight = $("#modal").height();
	var elemwidth = $("#modal").width();

	$("#modal").css('width',elemwidth+'px');
	$("#modal").css('margin-left',(elemwidth/2)*-1+'px');
	  	
	var toppos = csstop - elemheight/2;
	$("#modal").css('top',toppos+'px');
	$("#modal").show();
}

//function to hide the login error and show form again
function hideLoginError(){
	$('#login_error').slideUp();
}

//function to show the login help container
function showLoginHelp(){

	showBox();
	$("#modal").html(makeLoginHelp('overall'));
	$(this).formevents();
	
	var offset = $("#modal").offset().top;
	var winheight = $(window).height();
	var csstop = parseInt(winheight/2) + parseInt(offset);

	var elemheight = $("#modal").height();
	var elemwidth = $("#modal").width();

	//$("#modal").css('width',elemwidth+'px');
	$("#modal").css('margin-left',(elemwidth/2)*-1+'px');
		
	var toppos = csstop - elemheight/2;
	$("#modal").css('top',toppos+'px');
	$("#modal").show();
	
}

function makeLoginHelp(view){
	switch (view){
		case 'overall':
			var retstr = '<div id="login_help">';
					retstr+= '<a href="javascript:hideModal()"><img src="im/btn_close.png" alt="Close Window" class="hideModal" /></a>';
					retstr+= '<h3 class="copse" style="margin-bottom:0px;">Password Help</h3>';
					retstr+= '<form action="ajax.advanced-password-recovery.php" onsubmit="return advancedPasswordRecovery()" method="post" id="password_hint_form">';
					retstr+= '<p>Please submit your username and we will help you retrieve your password.</p>';
					retstr+= '<label class="input-item"><input type="text" class="txt" name="hint_username" id="hint_username" title="username" />';
					retstr+= '<a href="#" onclick="return advancedPasswordRecovery()" class="btn28"><img src="im/btn_go_right.png" alt="GO" /></a></label>';
					retstr+= '<div class="hdiv_text"><h2 class="copse">or</h2></div>';
					retstr+= '<p>If you do not know your username, please call your main Alpine contact for assistance.</p>';
					retstr+= '</form>';
		break;
	}
	
	return retstr;
}

function makeFullLoginHelp(view){
	switch (view){
		case 'overall':
			var retstr = '<div id="login_help">';
					retstr+= '<a href="javascript:hideModal()"><img src="im/btn_close.png" alt="Close Window" class="hideModal" /></a>';
					retstr+= '<h3 class="copse">Password Help</h3>';
					retstr+= '<form action="ajax.get-password-hint.php" onsubmit="return getPasswordHint()" method="post" id="password_hint_form">';
					retstr+= '<h4 class="copse">Get my password hint</h4>';
					retstr+= '<p>Please submit your username and we will retrieve your password hint to help you out.</p>';
					retstr+= '<label class="input-item"><input type="text" class="txt" name="hint_username" id="hint_username" title="username" />';
					retstr+= '<a href="#" onclick="return getPasswordHint()" class="btn28"><img src="im/btn_go_right.png" alt="GO" /></a></label>';
					retstr+= '</form>';
					retstr+= '<div class="hdiv_text"><h2 class="copse">or</h2></div>';
					retstr+= '<form action="ajax.reset-password.php" onsubmit="return resetMyPassword()" method="post" id="password_reset_form">';
					retstr+= '<h4 class="copse">Reset my password</h4>';
					retstr+= '<p>Please submit your password retrieval email address to get started.</p>';
					retstr+= '<label class="input-item"><input type="text" class="txt" name="reset_email" id="reset_email" title="email address" />';
					retstr+= '<a href="#" onclick="return resetMyPassword()" class="btn28"><img src="im/btn_go_right.png" alt="GO" /></a></label>';
					retstr+= '</form>';				
					retstr+= '</div>';
		break;
	}
	
	return retstr;
}

//function to hide a modal window
function hideModal(){
	$("#modal").hide();
	hideBox();
	$("#modal").html('');
	clearHashTag();
}

//function used to recover the password hint/reset password
function advancedPasswordRecovery(){
	
	//first, validate that the user has entered something in the field
	var hint_username = $("#hint_username").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(hint_username) || hint_username == 'username'){
		err_count++;
		$("#hint_username").removeClass('pass');
		$("#hint_username").addClass('fail');
	}else{
		$("#hint_username").removeClass('fail');
		$("#hint_username").addClass('pass');
	}
	
	if(err_count == 0){
		//all is well, lets do it!
		$.ajax({
				type: "POST",
				url:	"ajax.advanced-password-recovery.php",
				data:	"hint_username="+hint_username,
				success:	function(msg){
					
					if(msg == 'no-user'){
						
						$("#hint_username").removeClass('pass');
						$("#hint_username").addClass('fail');
						$("#password_hint_form #success_box").remove();
						$("#password_hint_form #error_box").remove();
						
						$("#password_hint_form p:first").after('<p id="error_box">Sorry, the username you entered was not recognized. Please check it and try again.<br />If you continue to have problems, please contact your district/organization administrator for assistance.</p>');
						$("#password_hint_form #error_box").fadeIn('slow');
					
					}else{
						
						var userinfo = eval('('+msg+')');
						
						var this_email = userinfo.user_email ? userinfo.user_email : false;
						var this_reminder_email = userinfo.reminder_email ? userinfo.reminder_email : false;
						
						//if the user has no emails on file, we cannot do anything for him
						if(!this_email && !this_reminder_email){
							$("#hint_username").removeClass('pass');
							$("#hint_username").addClass('fail');
							$("#password_hint_form #error_box").remove();
							$("#password_hint_form #success_box").remove();
							$("#password_hint_form p:first").after('<p id="error_box">Sorry, you do not have an email address on file.</p>');
							var password_contact = getHintPasswordContact(hint_username,"#password_hint_form #error_box");
							$("#password_hint_form #error_box").fadeIn('slow');
						}else{
							//the user has at least one email
							var email_used = this_reminder_email;

														
							$("#hint_username").addClass('pass');
							$("#hint_username").removeClass('fail');
							$("#password_hint_form #error_box").remove();
							$("#password_hint_form #success_box").remove();
							$("#password_hint_form p:first").after('<p id="success_box">An email was sent to: '+email_used+' with instructions on recovering your password.</p>');
							$("#password_hint_form #success_box").fadeIn('slow');
						}
						
					}
											
				}
		});
	}
	
	return false;
	
}

//function used to submit password hint form
function getPasswordHint(){
	
	//first, validate that the user has entered something in the field
	var hint_username = $("#hint_username").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(hint_username) || hint_username == 'username'){
		err_count++;
		$("#hint_username").removeClass('pass');
		$("#hint_username").addClass('fail');
	}else{
		$("#hint_username").removeClass('fail');
		$("#hint_username").addClass('pass');
	}
	
	if(err_count == 0){
		//all is well, lets do it!
		$.ajax({
				type: "POST",
				url:	"ajax.get-password-hint.php",
				data:	"hint_username="+hint_username,
				success:	function(msg){
				
					if(msg == 'no-user'){
						$("#hint_username").removeClass('pass');
						$("#hint_username").addClass('fail');
						
						$("#password_hint_form #error_box").remove();
						
						$("#password_hint_form p").after('<p id="error_box">Sorry, the username you entered was not recognized. Please check it and try again.</p>');
						$("#password_hint_form #error_box").fadeIn('slow');
						

					}else if(msg == 'no-hint'){
						
						$("#hint_username").removeClass('pass');
						$("#hint_username").addClass('fail');
						$("#password_hint_form #error_box").remove();
						$("#password_hint_form p").after('<p id="error_box">Sorry, you do not have a password hint on file.</p>');
						var password_contact = getHintPasswordContact(hint_username,"#password_hint_form #error_box");
						$("#password_hint_form #error_box").fadeIn('slow');
						
					}else{
						$("#password_hint_form #error_box").remove();
						$("#password_hint_form").html('<p id="success_box">Thank you. Your password hint is: <strong>'+msg+'</strong></p>');
						$("#password_hint_form #success_box").fadeIn('slow');

					}
				}
		});
	}
	
	return false;
}

//function used to submit password reset form
function resetMyPassword(){
	
	//first, validate that the user has entered something in the field
	var reset_email = $("#reset_email").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(reset_email) || !isEmail(reset_email)){
		err_count++;
		$("#reset_email").removeClass('pass');
		$("#reset_email").addClass('fail');
	}else{
		$("#reset_email").removeClass('fail');
		$("#reset_email").addClass('pass');
	}
	
	if(err_count == 0){
		//all is well, lets do it!
		$.ajax({
				type: "POST",
				url:	"ajax.reset-password.php",
				data:	"reset_email="+reset_email,
				success:	function(msg){
				
					if(msg == 'no-email'){
						
						//there was no email that matched
						$("#reset_email").removeClass('pass');
						$("#reset_email").addClass('fail');
						
						//get rid of the current error - if exists
						$("#password_reset_form #error_box").remove();
						
						$("#password_reset_form p").after('<p id="error_box">Sorry, the email address you entered was not recognized. Please check it and try again.</p>');
						$("#password_reset_form #error_box").fadeIn('slow');
						

					}else if(msg == 'no-info'){
						
						//the email matched, but the user does not have a security question on file
						$("#reset_email").removeClass('pass');
						$("#reset_email").addClass('fail');
						
						//get rid of the current error - if exists
						$("#password_reset_form #error_box").remove();
						
						$("#password_reset_form p").after('<p id="error_box">Sorry, you do not have a password recovery question on file.</p>');
						$("#password_reset_form #error_box").fadeIn('slow');
					
					}else if(msg == 'not-allowed'){
						
						//the email matched, but the user does not have permission to edit their password
						$("#reset_email").removeClass('pass');
						$("#reset_email").addClass('fail');
						
						//get rid of the current error - if exists
						$("#password_reset_form #error_box").remove();
						
						
						
						$("#password_reset_form p").after('<p id="error_box">Sorry, you do not have permission to reset your password.</p>');
						//try to get the contact info to reset the user password
						var password_contact = getPasswordContact(reset_email,"#password_reset_form #error_box");
						$("#password_reset_form #error_box").fadeIn('slow');
						
					}else{

						//so far so good. Let's output the security question and a field to answer
						$("#password_reset_form").attr('onsubmit','return sendMyResetPassword()');
						$("#password_reset_form").html('<h4 class="copse">Reset my password</h4><p>Please answer the following security question:<br /><br /><strong><em>'+msg+'</em></strong></p><label class="input-item"><input type="text" class="txt" name="reset_answer" id="reset_answer" title="your answer" /><a href="#" onclick="return sendMyResetPassword()" class="btn28"><img src="im/btn_go_right.png" alt="GO" /></a></label>');
						$(this).formevents();
					}
				}
		});
	}
	
	return false;
}

//function to get the password admin contact info
function getHintPasswordContact(username,elem){
		
		$.ajax({
				type: "POST",
				url:	"ajax.get-password-contact.php",
				data:	"username="+username,
				success:	function(msg){
					if(msg == 0){ 
						var contact = '<br />Please contact your administrator for assistance.'; 
					}else{ 
						var contact = '<br />Please contact:<br /><em>'+msg+'</em><br />for assistance.'; 
					}
						$(elem).append(contact);
				}
		});
}

function getPasswordContact(reset_email,elem){
		
		$.ajax({
				type: "POST",
				url:	"ajax.get-password-contact.php",
				data:	"reset_email="+reset_email,
				success:	function(msg){
					if(msg == 0){ 
						var contact = '<br />Please contact your administrator for assistance.'; 
					}else{ 
						var contact = '<br />Please contact:<br /><em>'+msg+'</em><br />for assistance.'; 
					}
						$(elem).append(contact);
				}
		});
}


function sendMyResetPassword(){

	//first, validate that the user has entered something in the field
	var reset_answer = $("#reset_answer").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(reset_answer) || reset_answer == 'your answer'){
		err_count++;
		$("#reset_answer").removeClass('pass');
		$("#reset_answer").addClass('fail');
	}else{
		$("#reset_answer").removeClass('fail');
		$("#reset_answer").addClass('pass');
	}
	
	if(err_count == 0){
		//all is well, lets do it!
		$.ajax({
				type: "POST",
				url:	"ajax.reset-password.php",
				data:	"reset_answer="+reset_answer,
				success:	function(msg){
	
					if(msg!= '0'){
					
						$("#reset_answer").addClass('pass');
						$("#password_reset_form").html('<h4 class="copse">Reset my password</h4><p id="success_box">Thank you. The password reset instructions are being sent to your email address.</p>');
						$("#password_reset_form #success_box").fadeIn('slow');
					}else{
						$("#reset_answer").addClass('fail');
					}
				}
		});
	}
	
	return false;
}

function checkMyAnswer(){

	//first, validate that the user has entered something in the field
	var reset_answer = $("#reset_answer").val();
	var reset_user_id = $("#reset_user_id").val();
	
	//init an error message and count
	var err_count = 0;
	
	if(isEmpty(reset_answer) || reset_answer == 'your answer'){
		err_count++;
		$("#reset_answer").removeClass('pass');
		$("#reset_answer").addClass('fail');
	}else{
		$("#reset_answer").removeClass('fail');
		$("#reset_answer").addClass('pass');
	}
	
	if(err_count == 0){
		//all is well, lets do it!
		$.ajax({
				type: "POST",
				url:	"ajax.check-security-answer.php",
				data:	"reset_answer="+reset_answer+"&reset_user_id="+reset_user_id,
				success:	function(msg){
					if(msg == '1'){
						//the answer was correct
						$("#reset_answer").removeClass('fail');
						$("#reset_answer").addClass('pass');
						
						//show the reset form
						getPasswordResetForm(reset_user_id);	
					
					}else{
						//the answer was incorrect
						$("#reset_answer").removeClass('pass');
						$("#reset_answer").addClass('fail');
					}
				}
		});
	}
	
	return false;
}

//function to get the password reset form
function getPasswordResetForm(user_id){
	$.ajax({
				type: "POST",
				url:	"inc.password-reset-form.php",
				data:	"user_id="+user_id,
				success:	function(msg){
					$("#password_reset_forms_container").html(msg);
				}
		});
}


//function to bind to the URL hash tag, in order to parse it when it changes
var hashtag = '';
var hash_int = null;

function bindHashTag(dur){

	if(hash_int){ clearInterval(hash_int); }
	hash_int = setInterval('compareHash()',dur);
}

function compareHash(){
	var current_hash = window.location.hash;
	
	if(current_hash != hashtag){
		parseHashTag(current_hash);
		hashtag = current_hash;
	}

}


//function to parse and handle hashtag URLs
function parseHashTag(h){
	if(h.length > 0){
		if(h.substr(0,2) == '#/' ){
			var target = h.substr(2);
			
			switch (target){
				case 'help':
					showLoginHelp();
				break;
				case 'overview-video':
					showOverviewVideo()
				break;
			}
		}
	}
}

function clearHashTag(){
	window.location.hash = '/';
}
