$(document).ready(function(){
	//standard AJAX mail form submission (requires mail2_plus.php, and captcha)
	//add form name to selector for submit event, the rest will take care of itself!
	$("input[type=submit]").removeAttr("disabled");
	$("#order_form").submit(function(e){
		e.preventDefault();
		var form = $(this);
		var btn = $(this).find("input[type=submit]");
		btn.attr("disabled","disabled");
		var formtype = $(this).find("input[name=form_type]").val();
		
		$.post("/ajax/order-submit.php",form.serialize(),function(data){
			var err = data.split("|");
			if (err[0] == "error") {
				alert(err[1]);
				btn.removeAttr("disabled");
			} else {
				if (formtype == "English") {
					window.location = "/thankyou";
				} else if (formtype == "French") {
					window.location = "/french/thankyou";
				}
			}
		});
		
	});
	
	$(".order_cat").hide();
	$("#openall").click(function(e){
		e.preventDefault();
		$(".order_cat").show();
	});
	$("#closeall").click(function(e){
		e.preventDefault();
		$(".order_cat").hide();
	});
	
	$(".togglecat").click(function(e){
		e.preventDefault();
		var link = $(this);
		var id = extractId($(this).attr("id"));
		$("#oc_" + id).slideToggle();
	});
	
	$("#of_delivertime").change(function(){
		var tot = $(this).val();
		var m="";
		var h="";
		var t="";
		
		if(tot.charAt(0)=="0"){
			h = tot.charAt(1);
		}else{
			h = tot.charAt(0) + tot.charAt(1);
		}
		m = tot.charAt(3) + tot.charAt(4);
		if(tot.charAt(6) == "A"){
			t=1;
		}else{ t=2;}

		m=parseInt(m)+30;
		if(m>59){
			m=parseInt(m)-60;
			h++;
		}

		if(m==0){
			m="00";
		}

		if(h==13){
			h=1;
		}
		if(h==12 && parseInt(m)<29 && t==1){
			t=2;
		}else{
			if(h==12 && parseInt(m)<29 && t==2){
				t=1;
			}
		}
		if(t==1){
			t="AM";
		}else{t="PM";}
		
		$("#of_delivertime2").val(h+":"+m+" " + t);
	});
});

function extractId(id){
	var newid = id.split("_");
	return newid[1];
}
