// JavaScript Document

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

	function showText (num) {
		$("#f").load("frontText.php?num="+num);
	}
	
	$(document).ready(function () {

		$("#certInfo").change(function () {
			code = $(this).val();
			$.get('getCert.php',{code: code}, function (ret) {
				$("TD#pppContent").html(ret);
			});
		});

		if ($.browser.msie && $.browser.version < 7) {
			$("#lines").css("display","none");
		}

		$("#numAttendees").change(function () {
			$("#attendeeTd").html("");
			for (i=0;i<$(this).val();i++) {
				$("#attendeeTable").clone().appendTo("#attendeeTd");
				$("#attendeeTotal").html("<strong>"+ Math.floor($("#attendeeTd .ac").length/5) +" attendees</strong>");
			}
		});

		$(":radio").click(function () {
			if ($("#type:checked").val() == "Individual") {
				$("#org").hide();
				$("#jobtitle").hide();
				$("#jobtitle").val("n/a");
				$("#name").val("n/a");
			} else {
				$("#jobtitle").show();
				$("#jobtitle").val("");
				$("#org").show();
				$("#name").val("");
			}
		});

		$("#attendeeForms").submit(function () {
			flag = true;
			$("#attendeeTd .ac").each(function (i) {
				if ($(this).val() == "" && $(this).attr("name") != "fax[]") {
					flag = false;
				}
			});
			
			if (!flag)
				alert("You have not completed all the necessary fields");
				
			return flag;
		});

		$("#enrolmentForm").submit(function () {
			failed = false;
			$("#enrolmentForm input, #enrolmentForm select").each(function (i,n) {
				if ($(n).attr("name") == "jobtitle") return true;
				value = $(n).val();
				if ($(n).attr("name") == "email" && value.indexOf("@") == -1)
					failed = true;
				if ($(n).attr("name") == "postcode" && !IsNumeric(value))
					failed = true;
				if ($(n).val() == "")
					failed = true;
			});
			
			if (failed == true) {
				alert("All fields are mandatory and you must both a valid email and postcode.");
				return false;
			}
			return true;
		});
	
		$(".expand li a").click(function () {
			$(this).siblings().toggle();
		});

		$("#courses").accordion({autoHeight:false, animated: 0, header: 'a.header'});

		$("#dateRangeFrom").datepicker({minDate: '+1d', dateFormat: 'dd/mm/yy'});
		
		$("#dateRangeTo").datepicker({minDate: '+1d', dateFormat: 'dd/mm/yy', onSelect:function (dateText) {
			$("#dateResults").load("getCourses.php", {list:'byDate', dateRangeFrom: $("#dateRangeFrom").val(), dateRangeTo: $("#dateRangeTo").val()}, function () {
				$("#dateTable tr").hover(function () {
					$(this).css("background-color", "#CCCCCC");
				},function () {
					$(this).css("background-color", "#FFFFFF");
				});
			
			});
		}});

		$("#textForm").submit(function () {
			$("#textResults").load("getCourses.php",{list:"text",term:$("#textSearch").val()}, function () {
				$("#dateTable tr").hover(function () {
					$(this).css("background-color", "#CCCCCC");
				},function () {
					$(this).css("background-color", "#FFFFFF");
				});
			});
		});

		$("#category,#campus").change(function () {
			$.getJSON("getCourses.php",{list: 'short', campus: $("#campus").val(), category: $("#category").val(),ajax: 'true'}, function(j){
			  var options = '<option value="0"> - Select - </option>';
			  for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			  }
			  if (i>0)
			  	$("select#course").html(options);
			  else
			  	$("select#course").html("<option>No courses found.</option>");
			})
		  })
		
		  $("#course").change(function () {
		  	$("#courseInfo").load("getCourses.php", {list: 'course',course_id: $("#course").val()});
		  })

		$("#onlineCategory").change(function () {
			$.getJSON("getCourses.php",{list: 'online', category: $("#onlineCategory").val(),ajax: 'true'}, function(j){
			  var options = '<option value="0"> - Select - </option>';
			  for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			  }
			  if (i>0)
			  	$("select#onlineCourse").html(options);
			  else
			  	$("select#onlineCourse").html("<option>No courses found.</option>");
			})
		  })

		$("#schedule").change(function () {
			locs = new Array();
			locs['Milton'] = '07 3510 8018';
			locs['Townsville'] = '07 4772 3366';
			locs['Mackay'] = '07 4957 4711';
			locs['Cairns'] = '07 4031 2827';
			$(".phone").html(locs[$("#schedule").val()]);
			$.getJSON("getCourses.php",{list: 'schedule', location: $("#schedule").val(),ajax: 'true'}, function(j){
			  var options = '<option value="0"> - Select - </option>';
			  for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			  }
			  if (i>0)
			  	$("select#coursedate").html(options);
			  else
			  	$("select#coursedate").html("<option value=\"0\">No courses found.</option>");
			})
		  })
		
		  $("#enrolChoose").submit(function () {
				if ($("#coursedate").val() == 0) {
					alert("Please choose a date for the course.");
					return false;	
				}
		  });
		
		  $("#onlineCourse").change(function () {
		  	$("#onlineCourseInfo").load("getCourses.php", {list: 'course',course_id: $("#onlineCourse").val()});
		  })
		
		$("#traineeships").change(function () {
			$("#quals").load("getCourses.php", {list: 'traineeships',trtype: $("#traineeships").val()}, function () {
				$("#dateTable tr").hover(function () {
					$(this).css("background-color", "#CCCCCC");
				},function () {
					$(this).css("background-color", "#FFFFFF");
				});
			});
		});
		
		$("#contactOptions").change(function () {
			$(".forms").hide();
			$("#"+$(this).val()).show();
		});

		$(".sender").click(function () {
			submission = $(".forms:visible form").serialize();
			$.post('actions.php', {info:submission}, function (d) {
				$(".forms:visible").html("<br /><br /><center><strong>Thank you. Your request was successful.</strong></center>");
			});
		});
		
		$("#remind").datepicker({minDate: '+1d', dateFormat: 'dd/mm/yy'});
		
	});
