﻿var selected = new Array(0,0,0,0);
var bagNum = 0;
var fieldNames = new Array('f_first','f_last','f_email','f_phone','f_address','f_suburb','f_state','f_postcode','s_address','s_suburb','s_state','s_postcode');

$(document).ready(function() {
	
	for(var i=0;i<selected.length;i++){
		selected[i] = setSelections(i);
		if(selected[i] == 0){ $(".courses:eq("+i+")").html($("#store_1").html());
			$("#i"+i).hide();
			$("#i"+i).removeClass("hidden");
			addItem(i,false);
		}else{	
			if(selected[i] == 1 || selected[i] == 2){
				$(".courses:eq("+i+")").html($("#store_2").html());
			}else{
				$(".courses:eq("+i+")").html($("#store_2_print").html());
			}
			addItem(i,true);
		}
	}
	
	$("#validate_form").click(function(){
		validate_form();			   
	});
	
	$("#checkdelivery").click(function(){
		var proceed = false;
		var type = 1;
		if($("#delivery_1").attr('checked')){
			type = 2;
			proceed = true; 
		}
		if($("#delivery_2").attr('checked')){
			proceed = true;   
		}
		for(var i=0;i<4;i++){
			if($.cookie('l_selected'+i)!=0){
				$.cookie('l_selected'+i,type);
			}
		}	
//		alert($.cookie('l_selected1'));
		proceed ? window.open("store.php?p=4", "_self") : null;
	});	
	
	
	$("#select_course").click(function(){
		select_course();			   
	});
	
	$("#submit_info").click(function(){
		if (confirm('You are about to enter our Secure Payment Facility.\nPlease confirm your details are correct and press OK when you are ready to continue.')){window.open("store.php?p=5", "_self")}
	});
	
	$("#shipping_check").click(function(){
		$(this).attr('checked') ? $("#shipping_details").slideUp() : $("#shipping_details").slideDown();
		$.cookie("shipping_provided", $(this).attr('checked'));
	});
	
	$(".cardNumber").keyup(function(e){
		var num = parseInt(e.target.id.substring(10,11));
		$(this).val().length == 4 ? $("#cardNumber"+(num+1)).focus() : null;
		$(this).val().length == 0 ? $("#cardNumber"+(num-1)).focus() : null;
	});

	update();
	
	//bagNum == 0 ? $("#enroll_content").html($("#store_3").html()) : null;

	function update(){
		updateBag();	
		setData();
		showShipping();
		showCourses();
		showTotal();
		$(".addCourse").click(function(e) {
			var cID = (parseInt($(this).parent().attr("id").substring(1, 2)));
			$(this).parent().html($("#store_2").html());
			addItem(cID,true);
			selected[cID] = 1;
			update();
		});
		$(".addCoursePrint").click(function(e) {
			var cID = (parseInt($(this).parent().attr("id").substring(1, 2)));
			$(this).parent().html($("#store_2_print").html());
			addItem(cID,true);
			selected[cID] = 2;
			update();
		});
		$(".removeCourse").click(function(e) {
			var cID = (parseInt($(this).parent().attr("id").substring(1, 2)));
			$(this).parent().html($("#store_1").html());
			addItem(cID,false);
			selected[cID]=0;
			update();
		});
	}
	
	function addItem(num, pink){
		if(pink){
			$("#price"+num).css("background-image", "url('images/pink_square.png')");
			$("#price"+num).css("border", "1px solid #FFF");
		}else{
			$("#price"+num).css("background-image", "url('images/black_square.png')");
			$("#price"+num).css("border", "1px solid #777");
		}
	}
	
	function updateBag(){
		bagNum = 0;
		for(var i=0;i<selected.length;i++){
			parseInt(selected[i]) != 0 ? bagNum++ : null;
		}
		$(".bag_number").html(bagNum);
		for(var i=0;i<selected.length;i++){
			$.cookie(('l_selected'+i), selected[i]);
		}
	}
	
	function setSelections(n){
		return $.cookie('l_selected'+n) != null ? parseInt($.cookie('l_selected'+n)) : 0;
	}
	
	function validate_form(){
		$("#e0").addClass("hidden");
		$("#e1").addClass("hidden");
		$("#e2").addClass("hidden");
		$("#e3").addClass("hidden");
		var count = 0;
		$("#f_first").val() == "" ? $("#e0").removeClass("hidden") : count++;
		$("#f_last").val() == "" ? $("#e1").removeClass("hidden") : count++;
		!checkEmail($("#f_email").val()) ? $("#e2").removeClass("hidden") : count++;
		$("#f_phone").val() == "" ? $("#e3").removeClass("hidden") : count++;
		
		for(var i=0;i<fieldNames.length;i++){
			$.cookie(fieldNames[i],$("#"+fieldNames[i]).val());
		}
		count >3 ? window.open("store.php?p=2", "_self") : $("#error_message").removeClass("hidden");
	}
	
	function select_course(){
		var show = true;
		for(var i=0;i<selected.length;i++){
			if(selected[i]==1){
				show = false;	
			}
		}
		if(show){
			alert("Please select the course you wish to enroll in.");
		}else{
			window.open("store.php?p=3", "_self")
		}
	}
	
	
	function checkEmail(s){
		var count = 0;
		for(var i=0;i<s.length;i++){
			s.substr(i,1) == "@" ? count++ : null;
			s.substr(i,1) == "." ? count++ : null;			
		}
		if(count>1){
			return true; 
		}else{ 
			return false;
		}
	}
	
	function setData(){
		for(var i=0;i<fieldNames.length;i++){
			if($.cookie(fieldNames[i]) != null){
				$("#"+fieldNames[i]).val($.cookie(fieldNames[i]));
				// causes error in ie7
				$("#"+fieldNames[i]).html($.cookie(fieldNames[i]));
			}
		}
	}
	
	function showCourses(){
		var cost = 0;
		for(var i=0;i<4;i++){
			if(selected[i] == 1){
				$("#selected_course_"+i).html($("#course_"+i).html()+$("#small_ebook").html());
				cost+=coursePrice;
			}else if(selected[i] == 2){
				$("#selected_course_"+i).html($("#course_"+i).html()+$("#small_print").html());
				cost+=coursePrintPrice;
			}else{
				$("#selected_course_"+i).addClass('hidden');
			}
		}
		$.cookie("total_cost",cost);
		return cost;
	}
	
	function showTotal(){
		$("#total_cost").html($("#total_1").html()+showCourses()+$("#total_2").html());
	}
	
	function showShipping(){
		if($.cookie("shipping_provided")=="false"){
			$("#shipping_check").attr('checked', false);
			$("#shipping_confirm").removeClass('hidden');
		}else{
			if($.cookie("shipping_provided")!=null){
				$("#shipping_check").attr('checked', true);
				$("#shipping_details").hide();
			}
		}
	}
});

function removeUser(s){
	if(confirm('Are you sure you want to remove these user details?')){
		window.open("admin_list_incomplete.php?d="+s, "_self")
	}
};

function shippingSent(s){
	if(confirm('Are you sure you have sent this user their material?')){
		window.open("admin_list_incomplete.php?s="+s, "_self")
	}
};
