/* 

  ==============================
   Acclaim Driving Academy
   Author: 9xb.com
  ==============================
  
*/

$(function()
	{
		$(".voucherform input[name='gv_amount']").change(
			function ()
			{
				var amount = Math.floor($(this).val());
				$(this).val(amount);
				if(isNaN(amount)) {
					$(this).val(20);
				} else if (amount < 20) {
					$(this).val(20);
				} else {
					amount = (Math.floor(amount/10))*10;
					$(this).val(amount);
				}
			}
		);
	}
);

function validator () 
{	

	var invalid_email = false;
	var show_invalid_email_warning = true;
	var mail_cmsg = "The highlighted email address you provided does not seem to be correct.\n\nWould you like to continue anyway?";
	
	/* required fields */
	var required = document.createElement("span");
	$(required).text("*");
	$(required).addClass("req");
	$(".mand").parent("div").append(required);
	
	/* zebra colouring */
	$("form div.row input[type!='submit'], form div.row select, form div.row textarea").focus(
		function ()
		{
			$(this).parent("div.row").addClass("altrow");
		}
	);
	$("form div.row input[type!='submit'], form div.row select, form div.row textarea").blur(
		function ()
		{
			$(this).parent("div.row").removeClass("altrow");
		}
	);
	
	$("form").submit(function() 
		{
								  
			var fields = $(this).find(".mand");
			var invalid_count = 0;
			var formoutput = $(this).find(".formoutput");
			
			if($(formoutput).css("display") !== "none") {
				if(formoutput.length > 0) $(formoutput).hide();
			}
			
			for(i=0; i<fields.length; i++) {
				$(fields[i]).css("border", "1px solid #999");
			}
			
			for(i=0; i<fields.length; i++) {
				
				switch(fields[i].type) {
					case "select-one":
					case "select-multiple":
						if(fields[i].selectedIndex == 0) {
							$(fields[i]).css("border", "1px solid #ff0000");
							invalid_count++;
						}
						break;
					case "text":
					case "textarea":
					case "password":
						if(!fields[i].value) {
							$(fields[i]).css("border", "1px solid #ff0000");
							invalid_count++;
						} else {
							var name = $(fields[i]).attr("name");
							if(name.indexOf("email") >= 0) {
								var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
								if(emailRegxp.test(fields[i].value) == false) {
									invalid_email = true;
									if(show_invalid_email_warning) {
										$(fields[i]).css("border", "1px solid #ff0000");
									}
								}
							}
						}
						break;
				}
				
				
				
			}
			// exception for the instructor page
			if(($('#ie_telephone').length > 0) && ($('#ie_email').length > 0)){
				if(($('#ie_telephone').val() == '') && ($('#ie_email').val() == '')){invalid_count++;
				var email_telephone_error = true;}
			}
				
				
			if( !invalid_count ) {

				if(invalid_email && show_invalid_email_warning) {
					if(!confirm(mail_cmsg)) { 
						return false;
					}
				}
				return true;
				
			} else {
				
				if(formoutput.length > 0) {
					
					$(formoutput).css("font-size", "0.9em");
					$(formoutput).css("color", "#ff0000");
					$(formoutput).css("font-weight", "bold");
					$(formoutput).text("Please ensure the highlighted fields are complete before proceeding.");
					$(formoutput).fadeIn(500);
					
				} else {
					
					alert("Please ensure the highlighted fields are complete before proceeding.");
					
				}
				
				if(email_telephone_error){
					alert('Please enter either your email address or a contact number.');
				}
				
				return false;
				
			}
		
		}
	);
	
}

$(function()
	{		
		
		/* Turn links marked as external into target blanks (strict). */
		$("a[rel='external']").attr("target", "_blank");
		
		
		/* Page tabs rollover. */
		$("#content_submenu li[class!='last']").hover(
			function ()
			{
				$(this).addClass("hover");
			},
			function ()
			{
				$(this).removeClass("hover");
			}
		);
		
		$("input#fi_postcode, input.postcode_box").focus(
		                             function ()
		                             {
		                                 if($(this).val() == "Enter postcode...") $(this).val("");
		                             }
		                            )
		                      .blur(
		                            function ()
		                             {
		                                 if($(this).val() == "") $(this).val("Enter postcode...");
		                             }
		                            );
		
	}
);



/* "Show me tell me bits." */
$(function()
   {
	   
	   $("#showmetellme ul li ul").hide();
	   
	   var answers = $("#showmetellme ul li ul");
	   for(var i=0; i<=answers.length; i++) {
		   
		   var this_ul = $("#showmetellme ul li ul:eq("+i+")");
		   
		   var revealbutton_text_off = "What's the answer?";
		   var revealbutton_text_on = "Hide the answer";
		   
		   var revealbutton = document.createElement("input");
		   $(revealbutton).attr("type", "button")
		                  .attr("value", revealbutton_text_off)
		                  .addClass("reveal")
		                  .click(
								function ()
								{
									if($(this).parent("li").children("ul").css("display") == "block") {
										$(this).parent("li").children("ul").fadeOut();
										$(this).val(revealbutton_text_off);
									} else {
										$(this).parent("li").children("ul").fadeIn();
										$(this).val(revealbutton_text_on);
									}
								}
			);
		   
		   $(this_ul).before(revealbutton);
		   
	   }
	   
	   
	   
   }
);


/* -- jQuery: Document Ready. -- */
$(document).ready(function()
{
	validator();
});


function updateTotal(){

	$.ajax(
	{
	   type: "POST",
	   url: "js/updatetotal.php",
	   success: function(msg)
	   {
	   		document.getElementById('theTotal').innerHTML = msg;
	   },
	   error: function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus); alert(errorThrown);}
	});

}


function deletefrombasket(product_id){

	$.ajax(
	{
	   type: "POST",
	   url: "js/deletefrombasket.php",
	   data:"product_id="+product_id,
	   success: function(msg)
	   {
		   	// update basket
			
			//alert($("#basketrow"+product_id).html);			
			jqRow = $("#basketrow" + product_id);
			jqRow.css("background", "#ffdede");
			jqRow.fadeOut("slow", function(){

										   		// the return value from the php is the amount of rows left in the db
												// 
												var amountleft = (msg.substring(1, msg.length)) * 1;

										   		if(amountleft == 0){
										   			location.reload(true);
												}

										   });
			updateTotal();

	   },
	   error: function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus); alert(errorThrown);}
	});

}


function updateminibasket(){
	$.ajax(
	{
	   type: "POST",
	   url: "js/updateminibasket.php",
	   success: function(msg)
	   {
		   	// update basket
			
			$("#basketcontent").fadeOut("slow", function(){$("#basketcontent").html(msg);});
			$("#basketcontent").fadeIn("slow", function(){alert("Item added to your basket");});

			
	   },
	   error: function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus); alert(errorThrown);}
	});
}


function addtobasket(product_id){
	
	var qty = '';

	if(document.getElementById('qty')){
		qty = document.getElementById('qty').value;
	}else{
		qty = '1';
	}
	
	if(parseInt(qty) && qty != '0'){
		
		$.ajax(
		{
		   type: "POST",
		   url: "js/addtobasket.php",
		   data:"product_id="+product_id+"&qty="+qty,
		   success: function(msg)
		   {
				// update basket
				
				if(msg == 'complete'){
					updateminibasket();
				}
	
				
		   },
		   error: function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus); alert(errorThrown);}
		});

	}else{

		alert('Please input a valid amount to add to your basket.')

	}

}




/* -- Window On-Load Manager -- */
var woms = new Array();
function womOn(){
  window.onload = womGo;
}
function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}
function womAdd(func){
  woms[woms.length] = func;
}