function validator () 
{	
	$("form").submit(function() 
		{
			var fields = $(this).find(".mand");
			var invalid_count = 0;
			var formoutput = $(this).find(".formoutput");
			var invalid_email = false;
			var show_invalid_email_warning = true;
			var cmsg = "The highlighted email address you provided does not seem to be correct. Would you like to continue anyway?";
			if($(formoutput).css("display") !== "none") {
				if(formoutput.length > 0) $(formoutput).fadeOut(500);
			}
			for(i=0; i<fields.length; i++) {
				$(fields[i]).removeClass("invalid");
			}
			for(i=0; i<fields.length; i++) {
				switch(fields[i].type) {
					case "select-one":
					case "select-multiple":
						if(fields[i].selectedIndex == 0) {
							$(fields[i]).addClass("invalid");
							invalid_count++;
						}
						break;
					case "text":
					case "textarea":
					case "password":
						if(!fields[i].value) {
							$(fields[i]).addClass("invalid");
							invalid_count++;
						} else {
							var name = $(fields[i]).attr("name");
							if(name.indexOf("email") >= 0) {
								var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
								if(emailRegxp.test(fields[i].value) == false) {
									invalid_email = true;
									if(show_invalid_email_warning) {
										$(fields[i]).addClass("invalid");
									}
								}
							}
						}
						break;
				}
			}
			if( !invalid_count ) {
				if(invalid_email && show_invalid_email_warning) {
					if(!confirm(cmsg)) { 
						return false;
					}
				}
				return true;
			} else {
				if(formoutput.length > 0) {
					$(formoutput).css("font-size", "0.9em");
					$(formoutput).css("color", "#ff0000");
					$(formoutput).css("font-weight", "bold");
					$(formoutput).text("Please ensure the highlighted fields are complete before proceeding.");
					$(formoutput).fadeIn(500);
				} else {
					alert("Please ensure the highlighted fields are complete before proceeding.");
				}
				return false;
			}
		}
	);
	
	$("form input.checkStrength").keyup(
		function (e)
		{
			var pwsmeter = $(this).next(".passwordStrength");
			var score = 0;
			score = checkStrength($(this).val());
			
			if( score < 5 ) {
				
				$(pwsmeter).css("backgroundPosition", "0px 0px")
				           .html("Strength: weak");
			
			} else if ( score < 20 ) {
				
				$(pwsmeter).css("backgroundPosition", "0px -13px")
				           .html("Strength: medium");
				
				
			} else {
				
				$(pwsmeter).css("backgroundPosition", "0px -26px")
				           .html("Strength: strong");
				
			}
			
		}
	);
	
	function checkStrength(password)
	{

		var intScore = 0;
		
		// PASSWORD LENGTH
		if (password.length<5) {
			intScore = (intScore + 3)
		} else if (password.length>4 && password.length<8) {
			intScore = (intScore+6)
		} else if (password.length>7 && password.length<16) {
			intScore = (intScore+12)
		} else if (password.length>15) {
			intScore = (intScore+18)
		}
		
		// at least one lower case letter
		if (password.match(/[a-z]/)) intScore = (intScore+1)
		
		// at least one upper case letter
		if (password.match(/[A-Z]/)) intScore = (intScore+5)
		
		// at least one number
		if (password.match(/\d+/)) intScore = (intScore+5)
		
		// at least three numbers
		if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) intScore = (intScore+5)
		
		// at least one special character
		if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) intScore = (intScore+5)
		
		//at least two special characters
		if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) intScore = (intScore+5)
		
		// both upper and lower case
		if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) intScore = (intScore+2)
		// both letters and numbers
		if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) intScore = (intScore+2)

		// letters, numbers, and special characters
		if (password.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) intScore = (intScore+2)
		
		return intScore;
			
	}
	
	$("form#register-account #conf_user_password, form#register-account #user_password").blur(
		function ()
		{
			var primPassField = $("#user_password");
			if( $(primPassField).val().length > 0 && $("#conf_user_password").val().length > 0) { 
				if( $(primPassField).val() !== $("#conf_user_password").val() ) {
					$("#conf_pass_match").fadeOut("fast",
						function()
						{
							$(this).html("Passwords do not match!")
							.css("color", "#CC0000")
							.fadeIn("fast")
						});
				} else {
					$("#conf_pass_match").fadeOut("fast",
						function()
						{
							$(this).html("Passwords match!")
							.css("color", "#658F00")
							.fadeIn("fast")
						});
				}
			} else {
				$("#conf_pass_match").fadeOut("fast");
			}
		});
	
	$("form#register-account").submit(
		function ()
		{
			var passwordField = $("#user_password").val();
			var confPasswordField = $("#conf_user_password").val();
			
			if( passwordField.length > 0 && confPasswordField.length > 0 ) {
				
				if( passwordField == confPasswordField ) {
					
					return true;
					
				} else {
				
					alert("The passwords you have entered do not match. Please type the same password in both fields before continuing.");
					return false;
				
				}
				
			} else {
				
				alert("Please complete the password fields.");
				return false;
				
			}
			
		});
	
	
}

function occasions ()
{

	$("#ot_id").change(
		function ()
		{
			
			$("#cost_per_tag").empty();
			var ot_id = $(this).val();
			var doesTypeNeedTags = $.ajax({
											  url: "libs/includes/ajax.php",
											  cache: false,
											  data: "action=doesTypeNeedTags&ot_id=" + ot_id,
											  async: false
										 }).responseText;
			if(parseInt(doesTypeNeedTags) == 1) {
				var costPerTag = $.ajax({
										  url: "libs/includes/ajax.php",
										  cache: false,
										  data: "action=getTagCost&ot_id=" + ot_id,
										  async: false
										}).responseText;
				if(parseFloat(costPerTag) > 0) {
					$("#cost_per_tag").html("(&pound;"+costPerTag+" per insert!)");
				}
				$("#tags-row").show();
			} else {
				$("#occ_tags").val("0");
				$("#tags-row").hide();
			}
			
		});
	
	$("#uploadimage").css("display", "none");
	$(".showUploadImage").click(
		function ()
		{
			if( $("#uploadimage").css("display") == "block") {
				$("#uploadimage").fadeOut();
			} else {
				$("#uploadimage").fadeIn();
			}
			return false;
		});
	$("#sub_upload_occ_image").click(function(){$(this).val("Please wait, uploading...")});
	$("#ocd_amount").blur(
		function ()
		{
			var amount = Math.floor($(this).val());
			if(isNaN(amount)) {
				$(this).val(0);
			} else {
				$(this).val(amount);
			}
		});
	
	var numAvatars = $("#innerscroll li").length;
	var totalWidth = numAvatars * 110;
	$("#innerscroll").css("width", totalWidth+"px");
	$("#innerscroll .av_cb").click(
		function ()
		{
		
			$(this).blur();
			var cb_state = $(this).attr("checked");
			var parent_li = $(this).parent("li");
			var all_lis = $(this).parent("li").parent("ul").children("li");
			
			if(cb_state) {
				$(all_lis).removeClass("selected");
				$(parent_li).addClass("selected");
			}
		
		});
	
	$(".closeOccasionForm").submit(
		function ()
		{
			if(!confirm("Are you sure you wish to close this event?")) {
				return false;
			}
		});
	
	$(".voucher_selection").change(
		function ()
		{
			
			var voucherId = $("#voucher_id").val();
			var parentFormObj = $(this).parent("form");
			var vid = $(this).val();
			var vsObj = $(this);
			var evObj = $(this).parent().children(".evoucher_amount");
			var voucher_id = $(this).parent().children("input[name='voucher_id']").val();
			$.get("libs/includes/ajax.php", {action:"isVoucherElectronic",id:vid},
				  function (data)
				  {
						$("#vval_id_" + voucherId).remove();
						$(evObj).fadeOut();
						if(parseInt(data)) {
							$(evObj).fadeIn();
						}
				  });
			
		});
	
	
	
	$(".remainingMoneyOptions input[name='remaining_money_option']").click(
		function ()
		{
			if($(this).val() == "charity") { 
				$("#charity_giftaid").css("display", "block");
			} else {
				$("#charity_giftaid").css("display", "none");
			}
			$(".remainingMoneyOptions .option").removeClass("selected");
			$(this).parent().addClass("selected");
		});
	
	$(".switchEditOccasion").click(
		function ()
		{
			$("#event-details").toggle();
			$("#event-details-edit").toggle();
			return false;
		});

}

$(document).ready(function() {
	
	validator();
	occasions();
	
	$("a[rel='external']").attr("target", "_blank");
	
	$(".textfield").focus(
						function ()
						{
							var parentRow = $(this).parent("div.row");
							$(parentRow).addClass("row-focus");
							
						})
	               .blur(
						function()
						{
							var parentRow = $(this).parent("div.row");
							$(parentRow).removeClass("row-focus");
						});

	$(function()
		{
			$("#faq-list li a").click(
				function ()
				{
					var faqNum = $(this).attr("rel");
					$("#Q" + faqNum).animate(
						{
							backgroundColor: "#000000"
						});
				});
		});
	
});