/* function to include other .js files on page; thanks to phpied.com */
function include_js(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
};

//Error messages for invalid input
error = new Object();
error.EMPTY = '<li>One or more required fields are missing values. The fields are marked with an asterisk.</li>';
error.BAD_EMAIL = '<li>You must enter a valid email.</li>';
error.DIFF_EMAIL = '<li>The email addresses you entered do not match.</li>';
error.EMAIL_IN_USE = '<li>The email address you entered is already in use.</li>';
error.DIFF_PASS = '<li>The passwords you entered did not match.</li>';
error.PASS_LENGTH = '<li>Your password must be between 4 and 16 characters long.</li>';
error.BAD_CAP = '<li>You answered the last question incorrectly.  Please try again.</li>';

var last_email = '';

$(document).ready(function() {
	highlightOnFocus('.register_input');
	$('#success_container').hide();
});

function doRegister(){

	if(!validate()){
		return false;
	}
	var fname = $('#fname').val();
	var lname = $('#lname').val();
	var email = $('#email').val();
	var passwd = $('#passwd').val();
	var phone = $('#phone').val();
	var fax = $('#fax').val();
	var cap = $('#cap').val();
	
	//Store the email in case they want to resend the verification
	last_email = email;

	$.ajax({
		type: 'POST',
		url: 'ajax/doRegister.php',
		data: { fname: fname,
				lname: lname,
				email: email,
				passwd: passwd,
				phone: phone,
				fax: fax,
				cap: cap},
		success: function(data){
			if(data.length > 1 && data != ''){
				error_msg = '';
				if(data == 'BAD_CAP'){
					error_msg = error.BAD_CAP;
					$('#cap').html('');
					flagField('cap',true);
				}
				if(data == 'EMAIL_IN_USE'){
					error_msg = error.EMAIL_IN_USE;
					$('#cap').html('');
					flagField('email',true);
					flagField('email2',false);
				}
				if(data == 'EMPTY'){
					error_msg = error.EMPTY;
				}
				showError(error_msg,1);
			}else{
				showSuccess();
			}
		},
		failure: function(){
			alert('There was a error while logging in.  If the problem persists, please contact the administrator.');
		}
	});
}

function validate(){
	//Reset any errors from last checked
	$('.bad').removeClass('bad');
	$('#error_message').html('');
	var valid = true;
	
	//Error messages
	var field_msg = '';
	var email_msg = '';
	var passwd_msg = '';
	//Check all required fields for proper input
	$('.required').each(function(i){
		if(this.value==null||this.value==''){
			//The field is emtpy
			flagField(this.id, valid);
			valid = false;
			field_msg = error.EMPTY;
		}
		else{
			//Check specialty fields to verify they are in the format they are supposed to be in
			var filter= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if((this.id == 'email' || this.id == 'email2') && !filter.test(this.value)){
				//Invalid email address
				flagField(this.id, valid);
				valid = false;
				email_msg = error.BAD_EMAIL;
			}else if(this.id == 'email2' && this.value != $('#email').val()){
				//Email addresses don't match
				flagField('email', valid);
				valid = false;		
				flagField(this.id, valid);
				if(field_msg=='' && email_msg==''){
					email_msg = error.DIFF_EMAIL;
				}
			}else if(this.id == 'passwd' && (this.value.length < 4 || this.value.length > 16)){
				//Password too short
				flagField(this.id, valid);
				valid = false;
				passwd_msg = error.PASS_LENGTH;
			}else if(this.id == 'passwd2' && this.value != $('#passwd').val()){
				//Passwords don't match
				flagField('passwd', valid);
				valid = false;
				flagField(this.id, valid);
				if(passwd_msg==''){
					passwd_msg = error.DIFF_PASS;
				}
			}
		}
	});
	if(!valid){
		var error_msg = field_msg + email_msg + passwd_msg;
		showError(error_msg,3);
	}
	return valid;
}

function flagField(id, valid){
	//If the field has no data, show an error message, flag the form as invalid and mark the field
	if(valid){
		$("#"+id).focus();
		showError('requiredFields',true);
	}
	$("#"+id).addClass('bad');	
}

function showError(msg,size){
	$('#user_container').height(415 + size*20);
	$('#error_message').html('<ul>'+msg+'</ul>');
	$('#error_message').focus();
}

function showSuccess(){
	$('#user_container').hide();
	$('#success_container').show();
}


function resendVerifyEmail(){
	$.ajax({
		type: 'POST',
		url: 'ajax/resendVerification.php',
		data: {email: last_email},
		dataType: "json",
		success: function(data){
			if(data.error == ''){
					alert('A verification email has been sent.  Please follow the instructions in the email.');
			}else if(data.error=='BLOCKED'){
					alert('This account is blocked.  If you would like to know why please contact the administrator.');
			}else{
					alert('There was an error sending the email.  If the problem persists please contact the administrator.');
			}
		},
		failure: function(){
			alert('There was a error while logging in.  If the problem persists, please contact the administrator.');
		}	
	});
}
