$(document).ready(function() {

	//Captcha Refresh
	$(".refresh").click(function(){
		$('#captcha img').attr('src', 'captcha?rand='+ Math.random() +'');
	})
	
	// Popup Controls
	$("a[@rel=popup]").click(function(){
		popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=500,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
		popupWindow.focus()
		return false;
	})
	$("a[@rel=external]").click(function(){
		popupWindow = window.open(this.href,this.title,'width=800,height=600,toolbar=yes,status=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
		popupWindow.focus()
		return false;
	}) 

						   
						   
	// Form Errors					   
	var originalBG = $("input").css("background-color");
	var orgColor = $("input").css("color");
	$("button").click(function() {
		$("input").removeClass('issue');
		$("select").removeClass('issue');
		
		//Text Boxes
		$("input[@type=text].required").each(function (i) {
			if($(this).val() == '') {
				$(this).animate({ backgroundColor: "red" }, 750).css("color", "#ffffff");
				$(this).addClass("issue");
			}  else {
				$(this).animate({ backgroundColor: originalBG }, 1000).css("color", orgColor);
			}
		})
		
		//Select Boxes
		$("select.required").each(function(i) {
			if($(this).val() == '') {
				$(this).animate({ backgroundColor: "red" }, 750).css("color", "#ffffff");
				$(this).addClass("issue");
			}  else {
				$(this).animate({ backgroundColor: originalBG }, 1000).css("color", orgColor);
			}
		})
		
		//Checkboxes
		$("input[@type=checkbox].required").each(function (i) {
			if ($(this).is(':not(:checked)')) {
				$(this).siblings('label')
				.animate({ backgroundColor: "red" }, 750);
				$(this).parent().addClass("issue");
			}  else {
				$(this).siblings()			
				.animate({ backgroundColor: originalBG }, 1000)
				.css("color", orgColor);
				$(this).parent().removeClass('issue');
			}
		})

		if ($(".issue").get() == '') {
			return true;
		} else {
			$(".issue").each(function (i) {
				var num = i+1;
				if (num == '1') {
					$('#error').html('We found '+num+' error with your registration.');
				} else {
					$('#error').html('We found '+num+' errors with your registration.');
				}
				
			})
			return false;
		}
	})
});