jQuery(function($)
{
	var frm = document.forms["frmorder"];
	/*
	var customerRegion = $("#customer-errors");
	$(frm.OrderCustomerFirstName).attachValidator("nonempty",         {region: customerRegion, failMessage: "Please fill in your first name."});
	$(frm.OrderCustomerLastName).attachValidator("nonempty",          {region: customerRegion, failMessage: "Please fill in your last name."});
	//$(frm.OrderCustomerPersonNo).attachValidator(["nonempty", "ssn"], {region: customerRegion, failMessage: function(type) { if(type=="ssn") return "Ifyllt personnummer verkar inte vara korrekt."; return "Vänligen fyll i ditt personnummer"; }});
	$(frm.OrderCustomerCity).attachValidator("nonempty",              {region: customerRegion, failMessage: "Please fill in your city."});
	$(frm.OrderCustomerAddress).attachValidator("nonempty",           {region: customerRegion, failMessage: "Please fill in your street address."});
	$(frm.OrderCustomerZip).attachValidator("nonempty",           	  {region: customerRegion, failMessage: "Please fill in your post code."});
	$(frm.OrderCustomerEmail).attachValidator(["nonempty", "email"],  {region: customerRegion, failMessage: function(type) { if(type=="email") return "The e-mail address you supplied appears to be incorrect."; return "Please fill in your e-mail address."; }});
	$(frm.OrderPrescribed).attachValidator("checked",                 {region: customerRegion, failMessage: "You have to verify that you have been prescribed your contact lenses."});
	
	$(frm).attachValidator();
	*/
	
	//Used to validate article parameters
	$(frm).attachValidator();
	
	//Used to validate the Customer information
	$(frm).validate({	errorLabelContainer: $('#customer-errors'),
						errorClass: 'validationError',
						rules: {
							OrderCustomerFirstName: 'required',
							OrderCustomerLastName: 'required',
							OrderCustomerCity: 'required',
							OrderCustomerAddress: 'required',
							OrderCustomerZip: 'required',
							OrderPrescribed: 'required',
							OrderCustomerEmail: {required: true, email: true},
								
						},
						messages: {
							OrderCustomerFirstName: 'Please fill in your first name.',
							OrderCustomerLastName: 'Please fill in your last name.',
							OrderCustomerCity: 'Please fill in your city.',
							OrderCustomerAddress: 'Please fill in your street address.',
							OrderCustomerZip: 'Please fill in your post code.',
							OrderPrescribed: 'You have to verify that you have been prescribed your contact lenses.',
							OrderCustomerEmail: {required: "Please fill in your e-mail address.",
												email: "Your email address must be in the format of name@domain.com"
												}
						}
	 });
	
	
	// Save any changes to customer info to session, only for 'show'..
	$("#customer-block input, #customer-block select").change(function()
	{
		var em = $(this);
		var value = em.is(":checkbox")?em.is(":checked"):em.val();
		if(em.data("onSave")) value = em.data("onSave")(value);
		
		$.get("/_session.asp", { "var": em.attr("name"), "val": escape(value) });
	});
	
	
	$(frm.OrderCustomerPersonNo).defaultvalue("ĊĊMMDDXXXX")
	// var personNo = $(document.frmorder.OrderCustomerPersonNo);
	// personNo.click(function()
	// {
		// if(this.value == "ĊĊMMDD-XXXX") this.value = "";
	// });
	// personNo.change(function() {0);
	
	$(".hide").hide();
});

(function($) {
	$.fn.defaultvalue = function(value)
	{
		this.each(function()
		{
			var el = $(this);
			el.focus(function()
			{
				if(el.val() == value) el.val("");
				el.removeClass("dim");
			});
			el.blur(function()
			{
				if(el.val() == "")
				{
					el.addClass("dim");
					el.val(value);
				}				
			});
			if(el.val()=="") el.val(value);
			if(el.val()==value) el.addClass("dim");
			
		});
		return this;
	}
})(jQuery)

///                 
/// Subscription    
///                 
jQuery(function($)
{
	// find subscription information regions
	var subscriptionCheckbox = $("#order-subscription-checkbox");
	var subscriptionDetails = $("#SUB_1");
	
	// if the subscriptioncheckbox isnt checked, hide the details region.
	if(!subscriptionCheckbox.is(":checked")) subscriptionDetails.hide();
	
	// when the subscription checkbox is toggled, toggle the details region.
	subscriptionCheckbox.click(function()
	{
		subscriptionDetails.hide();
		if(this.checked) subscriptionDetails.fadeIn(250);
	});
	// Next delivery date
	var nextDelivery = $("#nextDeliveryDate");
	var updateDeliveryDate = function(diff)
	{
		diff = new Number(diff||0);
		var date = new Date();
		date.setDate(date.getDate() + diff);
		var dd = date.getDate().toString();
		var mo = (date.getMonth()+1).toString();
		
		nextDelivery.html(date.getFullYear() + "-" + (mo.length==1?"0":"") + mo + "-" + (dd.length==1?"0":"") + dd);
	}
	
	// add date picker
	//var calendarButton = $("#calbutton"); //calendarButton.show();
	var getMin = function() { return document.frmorder["MinimumDeliveryTime"].value||10; }
	var calendarField = $("#custom-days-until-delivery");
	var calendarTB = $("#custom-days-until-delivery");
	calendarField.change(function() { updateDeliveryDate(this.value); });
	calendarField.data("onSave", function(value) { return calendarField[0].value = Math.max(value,getMin())||0; });
	calendarField.date_input({ validate: function(date) { return Math.max(date,getMin()); } });
	
	// handle the subscription type radio buttons
	var methodRadios = subscriptionDetails.find("input[name=OrderSubscribeMethod]");
	var methodHighlight = function()
	{
		methodRadios.each(function(i)
		{
			$(this).parent().addClass(    this.checked?"subactive":"subinactive");
			$(this).parent().removeClass(!this.checked?"subactive":"subinactive");
			
			if(this.checked)
				updateDeliveryDate(i==0?$("#ORDERCONSUMATION").html():calendarField.val());
		});
	}
	methodHighlight();
	methodRadios.click(methodHighlight);

	calendarTB.click(function(){methodRadios.filter("[value=1]").click();methodHighlight();});
	
});

