var ThePolicy = null;
var fldDelivery = "prefDate";
var MILLIS_HOUR = 1000*60*60;
var MILLIS_DAY = 24*MILLIS_HOUR;

function isDateSelectable(date) {
	if (null == ThePolicy || ThePolicy.bitmask == 0x7f) return [1];
	var dow = date.getDay();
	var selectable = (1 << dow) & ThePolicy.bitmask;
	return selectable ? [1] : [0];
}


function isPolicyValid(product, policy) {
	if (policy == null 
		|| typeof policy.bitmask == undefined
		|| typeof policy.minDate == undefined
		|| typeof policy.product == undefined
		|| typeof policy.dow     == undefined
		|| policy.product != product)
		return false;
	return true;
	// we'll check if it's fresh enough when we'll get treshhold etc...
	var now = new Date();
	var t_now = now.getTime();
	var t_dow = now.getDay();
	
	var outdated = (parseInt(t_now/MILLIS_DAY) != parseInt(fetched/MILLIS_DAY)) 
					|| (t_dow == policy.dow && (t_now % MILLIS_HOUR) >= policy.treshhold && (policy.fetched % MILLIS_HOUR) < policy.treshhold);
	return !outdated;
}


function findProductID() {
	var products = document.getElementsByName("product");
	var rslt = undefined;
	if (products.length > 0) {
		if (products.length > 1) {
			for (var i=products.length-1; i>=0; i--) {
				var p = products[i];
				if (p.checked || p.selected) {
					rslt = p.value;
					break;
				}
			}
		}
		else {
			rslt = products[0].value;
		}
	}
	return rslt;
}



function refreshPolicy(callback, cbParam) {
	var product_id = findProductID();
	if (product_id && !isPolicyValid(product_id, ThePolicy)) {
		var policy = null;
		
		var url = "/landers/order/policy.do?product=" + product_id;

		var resp = $.getJSON(url, undefined, function (data) {
			policy = data;
			
			var hasProps = false;
			for (var p in data) hasProps = true;
			policy.product = product_id;
			policy.fetched = new Date().getTime();

			if (!hasProps) {
				policy.bitmask = 0x7F;
				policy.dow = -1;
				policy.treshhold = 24;
				policy.minDate = parseInt(new Date().getTime()/1000);
			
			}
			
			if (!isPolicyValid(product_id, policy)) {
				policy = null;
			}
			
			if (policy) {
				var minDate = new Date(1000*parseInt(policy.minDate)); 
				$('#' + fldDelivery).datepicker('option', 'minDate', minDate);
			}
			ThePolicy = policy;
			if (callback) callback(cbParam);
		});
	}
	return ThePolicy != null;
}

/*
function _refreshPolicy() {
	var product_id = findProductID();
	if (product_id && !isPolicyValid(product_id, ThePolicy)) {
		var policy = null;
		
		var url = "/landers/policy.do?product=" + product_id;
		var info = new Connection().get(url);
		if (info) {
			policy = eval('(' + info + ')');
			policy.product = product_id;
			policy.fetched = new Date().getTime();
			if (info == "{}") {
				policy.bitmask = 0x7F;
				policy.dow = -1;
				policy.treshhold = 24;
				policy.minDate = parseInt(new Date().getTime()/1000);
			}
		}
		ThePolicy = policy;
	}
	return ThePolicy != null;
}
*/



function _setMinDate() {
	var date = ThePolicy != null ? new Date(1000*ThePolicy.minDate) : new Date();
	var firstDate = $.datepicker.formatDate("dd-mm-yy", date);
	var ctrl = 	$("#minDate")[0];
	if (ctrl) ctrl.value = firstDate;
	var pd = $("#prefDate");
	if (!pd.length) return;
	pd.val(firstDate);

	$("#prefDate_lbl").html(firstDate);
}

function initMinDate() {
	refreshPolicy(_setMinDate);
}

