jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

$(document).ready(function(){
	$('input[type="text"]').addClass('idleField');
	
	var validation = new Array;
	validation['firstName'] = new Array(/^([A-Za-z\. '-]+)$/,'First Name must be at least one alpha character long with no special characters');
	validation['lastName'] = new Array(/^([A-Za-z\. '-]+)$/,'Last Name must be at least one alpha character long with no special characters');
	validation['apt'] = new Array(/^([A-Za-z0-9-'# \.]*)$/,'Apartment/Suite must not have any special characters');
	validation['address'] = new Array(/^([A-Za-z0-9-' \.]{3,})$/,'Address must be at least three characters long with no special characters');			
	validation['address2'] = new Array(/^([A-Za-z0-9-'# \.]*)$/,'Address 2 must not have any special characters');			
	validation['city'] = new Array(/^([A-Za-z0-9-' \.-]+)$/,'City must not have any special characters');		
	validation['province'] = new Array(/^([A-Za-z0-9-' \.]+)$/,'State must be selected.  If your state is not listed select \'Other\'');
	validation['prov2'] = new Array(/^([A-Za-z0-9-' \.]+)$/,'Enter a state/province');						
	validation['postal'] = new Array(/(^(\d{5}-\d{4})$)|(^(\d{5})$)|(^(\d{9})$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)/,'Zip Code must be 5 or 9 numeric characters (i.e., 12345 or 12345-6789)');			
	validation['country'] = new Array(/^([A-Za-z0-9-' \.]+)$/,'Country must be selected.  If your country is not listed select \'Other\'');
	validation['area'] = new Array(/^([0-9]{3})$/,'Enter three digit area code');
	validation['tel1'] = new Array(/^([0-9]{3})$/,'Enter first three digits of your phone number');
	validation['tel2'] = new Array(/^([0-9]{4})$/,'Enter last four digits of your phone number');
	validation['email'] = new Array(/^.*@.*\..*$/,'Enter valid email address');
	validation['edit'] = new Array(/^(.*)$/,"asdf");
	validation['creditcardnumber'] = new Array(/^(\d*)$/,'Enter a valid credit card number');
	validation['cctype'] = new Array(/^([A-Za-z\. '-]+)$/,'Select a credit card type');
	validation['expiremonth'] = new Array(/([0-9]$)/,'Select a valid expiry date');
	validation['expireyear'] = new Array(/([0-9]$)/,'Select a valid expiry date');
	validation['nameoncard'] = new Array(/^([A-Za-z\. '-]+)$/,'Enter a cardholder name');


	function createError(input,msg){
		var offset = $(input).position();
		var width = $(input).width();
		$(input).removeClass("idleField").removeClass("focusField").addClass("errorField");
		$(input).parent().append("<div class='box' id='expBox'>" + msg + "</div>");
		$(input).closest("li").children("label").removeClass("errorImage");
		$(input).closest("li").children("label").addClass("errorImage");
		var box = $(input).parent().children("#expBox");
		var height = $(box).height();
		box.css('left', width + offset.left + 15).css('top', offset.top -5);
	}
	
	function removeError(input){
		$(input).parent().children("#expBox").hide("normal", function () { $(this).remove(); });
		$(input).removeClass('errorField').removeClass("focusField").addClass("idleField");
		$(input).closest("li").children("label").removeClass("errorImage");

	}
	
	function validateReg(input, patt, msg){
		var box = $(input).parent().children("#expBox");
		var error = false;
		if(box.length != 0){
			removeError(input);
		}
		var promptMsg = '';
		if(input.className.indexOf('requiredField') >= 0 && input.value.length == 0){
			promptMsg += "<span class='errorText'>This is a required field</span>";
		}
		if($(input).val().length > 0 && input.value.search(patt) == -1){
			promptMsg += "<span class='errorText'>" + msg + "</span>";
		} 
		if(promptMsg.length == 0 && $(input).attr('name') == 'emailConfirm'){
			var con = $.trim($(input).val());
			var em = $.trim($('#email').val());
			if(em != con){
				promptMsg += "<span class='errorText'>Email addresses do not match</span>";
			}
		}
		if(promptMsg.length > 0){
			createError(input,promptMsg)
			error = true;
		}
		return error;
	}
	
	$(":input",$('#studentInfo, #ccvalues')).focus(function(event){
		if(this.id != 'continueButton'){
			event.preventDefault();
			$(this).removeClass('errorField').removeClass("idleField").addClass("focusField");
			var box = $(this).parent().children("#expBox");
			if(box.length != 0){
				box.fadeIn();			
			}
		}
	});

	$(":input",$('#studentInfo, #ccvalues')).blur(function(event){
		if(this.id != 'continueButton'){
			var box = $(this).parent().children("#expBox");
			$(this).removeClass('errorField').removeClass("focusField").addClass("idleField");		
			if(box.length != 0){
				box.fadeOut();			
			}
			if(this.name == "emailConfirm"){
				validateReg(this, validation['email'][0], validation['email'][1]);
			} else {
				validateReg(this, validation[this.name][0], validation[this.name][1]);
			}
		}
	});
	
	$('#agree').click(function(){
		if($(this).attr('checked')){
			$('#agree').closest("td").removeClass('errorField');
			$('#agreeError').remove();
		}
    });
	
	$("#selectpayment,#studentInfo").submit(function(event){
		var error=false;
		$(":input",$('#studentInfo, #ccvalues')).each(function(){
			if(this.id != 'continueButton'){
				var box = $(this).parent().children("#expBox");
				$(this).removeClass('errorField').removeClass("focusField").addClass("idleField");		
				if(box.length != 0){
					box.fadeOut();			
				}
				if(this.name == "emailConfirm"){
					error = validateReg(this, validation['email'][0], validation['email'][1]);
				} else {
					error = validateReg(this, validation[this.name][0], validation[this.name][1]);
				}
			}
		});
		if($('#agree').length != 0){
			$('#agree').closest("td").removeClass('errorField');
			$('#agreeError').remove();

			if($("#agree:checked").val() == null){
				$('#agree').before("<div id='agreeError'>Check this box to confirm that you have read, understood, and agreed to our Terms and Conditions</div>");
				$('#agree').closest("td").addClass('errorField');
				error=true;
			}
		}
		if($('#country').length != 0){
			if($('#country').val() != 'United States' && $('#country').val() != 'Canada'){
				$('#countryError').remove();
				$('#continueButton').after("<div id='countryError'>Registrants from outside the US and Canada must register by calling a Course Advisor at 310-820-2359 (Los Angeles) or 212-213-8978 (New York).</div>").show();
				error=true;
			} else {
				$('#countryError').remove();
			}
		}
		if(error){
			return false;	
		}
	});
	//Hide the submit confirmation button so that the user can't click it multiple times.
	$('#confirmation').submit(function(event) {
		$(this).children('input').hide();								
		$(this).children('img').show();
		//return false;
    });

	$(".faq").click(function(event){
		event.preventDefault();
		var height = $(document).height();

		$("body").append("<div class='fixZ'><div class='faqWrapper'></div></div");
		$("body").append("<div class='faqAnswer'><div class='faqTop'></div><div class='faqBody'><h3>" + $(this).html() + "</h3><p>" + $(this).closest("li").children("span").html() + "</p></div><div class='faqBottom'></div><a class='faqClose' href='#'>Close</a></div>");
		$(".fixZ").bgiframe();
		var ansHeight = $(".faqAnswer").height();
		var ansWidth = $(".faqAnswer").width();
		var offset = $(this).offset();
		//$(".faqAnswer").center();
		$(".faqWrapper,.fixZ").css('height',height);
		$(".faqWrapper").css("opacity",0.6);
		$(".faqAnswer").css("top",offset.top - ansHeight+5).css("left", offset.left - ansWidth - 10);
		$(".faqWrapper,.faqAnswer").fadeIn("fast");
		$(".faqWrapper, .faqClose").bind('click',function(){
			$(".faqWrapper,.faqAnswer").fadeOut("fast",function () { 
				$(".faqWrapper").unbind("click");
				$(".faqWrapper,.faqAnswer,.fixZ").remove()
			});									
			return false;
		});

		return false;
	});
/*	$(".faqClose").click(function(event){
		$(".faqWrapper,.faqAnswer").fadeOut("fast",function () { 
			$(".faqWrapper").unbind("click");
			$(".faqWrapper,.faqAnswer,.fixZ").remove()
		});									  
	});*/
$(".infoBox").mouseover(function(event){
		$(this).children('.infoBoxContent').fadeIn();
	});
 
 	$(".infoBox").mouseleave(function(event){
		$(this).children('.infoBoxContent').fadeOut();
	});
});