
	  $(document).ready(function(){

		//$('a[rel*=facebox]').facebox();	  
		//  var $tabs = $("#userSettings").tabs();
		var $tabs = $("#uniformList").tabs();
		  
		$('#submitUserDetails').click(function() {
			
			msg = '';
			
			if($('#student_fname').val() == '') {
				msg = 'Please enter a student First name';
			} else if($('#student_surname').val() == '') {
				msg = 'Please enter a student Surname';
			} else if($('#sel_gender').val() == '') {
				msg = 'Please select a gender';
			} else if($('#sel_year').val() == '0') {
				msg = 'Please select a year level that the student will be when they start at King\'s';
			} else if($('#sel_house').val() == '0') {
				msg = 'Please select the boarding house the student will be in.';
			} else if($('#caregiver_name').val() == '') {
				msg = 'Please enter your name';
			} else if($('#caregiver_relationship').val() == '') {
				msg = 'Please enter your relationship to the student';
			} else if($('#contactPh').val() == '') {
				msg = 'Please enter a contact phone number';
			} else if($('#email').val() == '') {
				msg = 'Please enter a valid email address';
			} else if($('#confirm_Email').val() == '') {
				msg = 'Please re-enter a valid email address';
			} else if($('#confirm_Email').val() != $('#email').val()) {
				msg = 'The confirm email address field does not match the email address.';
			}
			
			if(msg == '') {
				document.studentDetailsForm.submit();
			} else {
				alert(msg);
			}
			
		 }
		);
		  
		$('#finishUniformSelection').click( function() {
		
			window.location = '/uniform_shop/summary/';
		
		 }
		);
		
		$('.monthChange').live('click', function() {
			changeMonth($(this).attr('id'));
		 }
		);
		
		$('#finishBooking').live('click', function() {
			window.location = '/uniform_shop/bookingConfirmed/';
		 }
		);
		
		$('.emptyDay').live('click', function() {
			changeMonth($(this).attr('id'));
		 }
		);
		
		$('.productDetails').live('click', function() {
			$('.productDetails').fadeIn('fast');
			$(this).fadeOut('fast');
			$('.productDetailsPannel').slideUp('fast');
			$(this).siblings('.productDetailsPannel').slideDown('slow');
		 }
		);

		$('.monthDaySelect').live('click', function() {
			$.post('/uniform_shop/aj_selDay', {
				ref: $(this).attr('id')
				}, function(data) {
					openIrisModal(data);
				}
		  	);
		 }
		);
		
		$('#pu_cancelbk').live('click', function() {
			$.post('/uniform_shop/aj_selDay', {
				ref: $('#bk_date_ref').val()
				}, function(data) {
					openIrisModal(data);
				}
		  	);
		 }
		);
		
		$('.delselection').live('click', function() {
				pm_id = $(this).attr("id");
				boxcontent = "<div id=\"modalMessageContent\"><p>Are you sure you want to delete this item?</p><input type=\"button\" value=\"Cancel\" class=\"closeIrisModal\"/>&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"Yes\" id=\""+pm_id+"\" class=\"delstudentSelectionConf\"/></div>";
				openIrisModal(boxcontent);
		 }
		);

		$('.closeIrisModal').live('click', function() {
			closeIrisModal();
		 }
		);

		$('.delstudentSelectionConf').live('click', function() {
			$.post('/uniform_shop/aj_rmStuSel', {
				ref: $(this).attr('id')
				}, function(data) {
					closeIrisModal();
					$('#uniformSelectionItems').html(data).slideDown('fast');
				}
		  	);
		 }
		);
		
		$('.addselectionButton').live('click', function() {
			idref = $(this).attr('id');
		
			$.post('/uniform_shop/aj_addItemtoSelection', {
				pm_id: $('#pr'+idref+' .sizePannel').val(), 
				qnty: $('#pr'+idref+' .productQuantity').val(),
				bsel: $('#pr'+idref+' .brefpm').val(),
				app_notes: $('#addInfr_appnt').val(),
				cl_id: $('#pr'+idref+' .colourPannelopt').val()
				}, function(data) {
					$('#uniformSelectionItems').html(data).slideDown('fast');
				}
		  	);
		
		 }
		);
		
		$('#pu_savebkn').live('click', function() {
			$.post('/uniform_shop/aj_saveappointment', {
				tm: $('#bk_time_ref').val(),
				dt: $('#bk_date_ref').val(),
				app_notes: $('#addInfr_appnt').val()
				}, function(data) {
					openIrisModal(data);
				}
		  	);
		 }
		);
		
		$('.colourPannelopt').live('change', function() {
			pref = $(this).attr('id');
		
			$.post('/uniform_shop/aj_updateProductSize', {
				pid: $(this).attr('id'),
				cref: $(this).val()		
				}, function(data) {
					$("#prt-r"+pref).html(data);
				}
		  	);
		  	
			nwref = $(this).val();
			
			$(".dsplay_sizeSel").removeClass("dsplay_sizeSel").addClass("nodsplay_sizeSel");
			$('#sbc'+nwref).addClass("dsplay_sizeSel").removeClass("nodsplay_sizeSel");
		 }
		);
		
		$('.bk_sht_fr').live('click', function() {
			$.post('/uniform_shop/aj_bookappt', {
				tm: $(this).attr('id'),
				dt: $('#bk_time_ref').val()			
				}, function(data) {
					openIrisModal(data);
				}
		  	);
		 }
		);
		
		$('.closeModalButton').live('click', function() {
			//window.location.reload();
			dc = $('#bk_time_ref').val()

			window.location = 'http://www.kingscollege.school.nz/uniform_shop/bookAppointment?ref='+dc;
		 }
		);
		
		function changeMonth(ref) {
			$.post('/uniform_shop/aj_calendarGrid', {
				ref: ref
				}, function(data) {
					$('#calendarGrid').fadeOut('fast').html(data).fadeIn('fast');
				}
		  	);
		}
		
		function openIrisModal(data) {
			if (!$(".opacityPageLock").length > 0){
				$("body").prepend("<div class=\"opacityPageLock\"></div><div id=\"displayBox\"></div>");
				$(".opacityPageLock").css({"opacity": "0.5"});
				$(".opacityPageLock").fadeIn('fast');
				$("#displayBox").html(data);
			}else {
				$("#displayBox").html(data);
			}

			//$("#displayBox").draggable( { handle: '.popUpTitle', containment: 'document' });
			
			var w = $(window); 
			var top = w.scrollTop()
			
			var windowWidth = $(window).width();  
			var windowHeight = $(window).height();
			var popupHeight = $("#displayBox").height();  
			var popupWidth = $("#displayBox").width(); 

			//centering  
			$("#displayBox").css({  
				"position": "absolute",  
				"top": (windowHeight/2-popupHeight/2)+top,
				"left": windowWidth/2-popupWidth/2 
			 });
			//only need force for IE6  
   
			$(".opacityPageLock").css({"height": windowHeight});  
			$("#displayBox").fadeIn('fast');
		}
		
		function closeIrisModal() {
			$('#displayBox').fadeOut('slow');
			$(".opacityPageLock").fadeOut('slow');
			$("#displayBox").remove();
			$(".opacityPageLock").remove();
		}
		
	  })
