/*
 * jquery must be installed to work
 * required fields = class="required"
 * */



	function checkForm(){

		var formSubmit = false;
		
		// required fields
		// var fName = document.getElementById('name');
		// var fEmail = document.getElementById('email');
		// var fCityCountry = document.getElementById('citycountry');
		
		 
		
		return false;
		
		
		// formSubmit = true;
		return formSubmit;
		
		
	}	
	$(function() {
		
		
		
		$("#enrol").submit(function(){
			  $("#friendName").val($("#name").val());
			  $("#friendEmail").val($("#email").val());
		});
		
		$("#notnzbornextra").hide();
		$("#exchangeextra").hide();
		$("#feepayingextra").hide();
		
		$('#regular').click(function(){
			$("#exchangeextra").hide();
			$("#feepayingextra").hide();
			$('#notnzbornextra').show();
			
		});
		$('#exchange').click(function(){
			$("#notnzbornextra").hide();
			$("#feepayingextra").hide();		
			$('#exchangeextra').show();
			
		});
		$('#international').click(function(){
			$("#notnzbornextra").hide();
			$("#exchangeextra").hide();
			$('#feepayingextra').show();
			
		});
		
	});
	
	
	$(function() {
		
		// $(".required").focus().css("border","1px solid green");
		
		// required, cannot be empty
		$(".required").each(
			
			 // For each element, run this code. The "indIndex" is the
			 // loop iteration index on the current element.
			 function(intIndex){
				 var randomnumber = Math.floor(Math.random()*101);
				 // Bind the onblur event and check the field isn't empty
				 $(this).bind (
					 "blur",
						 function(){
						 	
							// standard empty check
						 	if($(this).val() == ""){
						 		
								if($(this).attr("title") != ""){
									var errNote = $(this).attr("title");
									// remove title - its annoying
									$(this).removeAttr('title');
								} else {
									// if the title is empty show a standard message
									var errNote = 'This is a required field.';
								}
								// check to see if there is an err div 
								if($("div#err"+intIndex+randomnumber).length==0){
									
									var errMessage = '<div id="err'+intIndex+randomnumber+'" class="formErrorMessage">'+ errNote +'</div>';
									$(this).after(errMessage);
									$(this).css('border','1px solid red');
								} 
								
							}
						 	
						 	// remove if not empty
						 	if($(this).val() != ""){
						 		$("div#err"+intIndex+randomnumber).remove();
						 	}
						 }
				 );
				 $(this).bind (
						 "focus",
							 function(){
							 	// $("div#err"+intIndex+randomnumber).remove();
							 	$(this).css('border','1px solid green');
						 	 }
				);
				 
				 
		});
		
		// number only

		$(".required-int").each(
				
				 // For each element, run this code. The "indIndex" is the
				 // loop iteration index on the current element.
				 function(intIndex){
					 var randomnumber=Math.floor(Math.random()*101);
					 // Bind the onblur event and check the field isn't empty
					 $(this).bind (
						 "blur",
							 function(){
							 	
							 	var fieldValue = $(this).val();
							 	
							 	
								// standard empty check
							 	if($(this).val() == ""){
							 		console.log(fieldValue);
									if($(this).attr("title") != ""){
										var errNote = $(this).attr("title");
										// remove title - its annoying
										$(this).removeAttr('title');
									} else {
										// if the title is empty show a standard message
										var errNote = 'This is a required field.';
									}
									// check to see if there is an err div 
									if($("div#err"+intIndex+randomnumber).length==0){
										
										var errMessage = '<div id="err'+intIndex+randomnumber+'" class="formErrorMessage">'+ errNote +'</div>';
										$(this).after(errMessage);
										$(this).css('border','1px solid red');
									} else {
										$("div#err"+intIndex+randomnumber).html(errNote);
									}
									
								}
							 	
							 	// check if number
						 	if(isNaN($(this).val()) && $(this).val() != ""){
									console.log(fieldValue);
									if($(this).attr("title") != ""){
										var errNote = $(this).attr("title");
										// remove title - its annoying
										$(this).removeAttr('title');
									} else {
										// if the title is empty show a standard message
										var errNote = 'Only numbers are allowed.';
									}
									// check to see if there is an err div 
									if($("div#err"+intIndex+randomnumber).length==0){
										var errMessage = '<div id="err'+intIndex+randomnumber+'" class="formErrorMessage">'+ errNote +'</div>';
										$(this).after(errMessage);
										$(this).css('border','1px solid red');
									} else {
										$("div#err"+intIndex+randomnumber).html(errNote);
									}
									
								} else {
									// remove if not empty
									if($(this).val() != ""){
										$("div#err"+intIndex+randomnumber).remove();
									}
							 	}
							 	
							 	
							 }
					 );
					 $(this).bind (
							 "focus",
								 function(){
								 	// $("div#err"+intIndex+randomnumber).remove();
								 	$(this).css('border','1px solid green');
							 	 }
					);
					 
					 
			});
		
		
		// required, cannot be empty
		$("#enrol").bind(
			"submit" , function(){
				
				var submitForm = true;
				// console.log($(".required").val());
				if($(".required").val() == ""){
					submitForm = false;
					var errNote = 'One or more required fields are empty!';
					var errMessage = '<div id="generalFormError" class="formErrorMessage">'+ errNote +'</div>';
					
					// console.log($("#generalFormError"));
					
					if($("#generalFormError").length == 0){
						$("#enrol").after(errMessage);
					} 
				}
				$(".required-radio-group").each(
					
					 // For each element, run this code. The "indIndex" is the
					 // loop iteration index on the current element.
					 function(intIndex){
						 
						 var randomnumber = Math.floor(Math.random()*101);
						 // console.log(intIndex+randomnumber);
						 // Bind the onblur event and check the field isn't empty
						 if(!$(this).children("input:checked").val()){
							// console.log("here: "+ $(this).children("input:checked").val());
								 	
							// standard empty check
						 	if(!$(this).children("input:checked").val()){
						 		
								if($(this).attr("title") != ""){
									var errNote = $(this).attr("title");
									// remove title - its annoying
									$(this).removeAttr('title');
								} else {
									// if the title is empty show a standard message
									var errNote = 'Make a selection.';
								}
								// check to see if there is an err div 
								if($("div#err"+intIndex+randomnumber).length==0){
									
									var errMessage = '<div id="err'+intIndex+randomnumber+'" class="formErrorMessage">'+ errNote +'</div>';
									$(this).after(errMessage);
									$(this).css({'border':'1px solid red','padding':'5px'});
									submitForm = false;
								} 
								
							} else {
								// remove if not empty
								if($(this).children("input:checked").val()){
									$("div#err"+intIndex+randomnumber).remove();
								}
								
						 	}	 	
					};
				});
				return submitForm;
				// return false;
			});
		
			$(".required-radio-group").children("input").each(
					function(){
						
						$(this).bind("click",function(){
							
							$(this).parent().css("border","none");
							
							if($(this).parent().next()){
								$(this).parent().next().remove();
							}
							
						});
						
						// console.log($(this).attr("id"));
					}
					// 
					
			);
		// end function
		});
		// $("#enrol").bind("submit", function() { return false; });
		
	// });
	
	

