AutoValidator.addCustomType('bookingDay', function (n) {
	var frm = n.form;
	var result = true;
	var chkIn = frm.chkInDate.value;
	var chkOut = frm.chkOutDate.value;
	
	if((chkIn == '') || (chkOut == '')) {
		result = false;
	} else {
	
		var tmpChkIn = chkIn.split("/");
		var tmpChkOut = chkOut.split("/");
		
		if(dateBookingCompare(tmpChkIn[0],tmpChkIn[1],tmpChkIn[2],tmpChkOut[0],tmpChkOut[1],tmpChkOut[2]) == 1)
		{
         	result = false;
		}
	}
	
	return result;
});

var BookingDevice = {
	init : function(basePath) {
        $(document).ready( function() {
			BookingDevice._init(basePath);
		});
    },
    
    _init : function (basePath) {
		$('#chkInDate').datepicker({ 
			showOn: 'both', 
			buttonImage: basePath + 'images/calendar.gif', 
			buttonImageOnly: true,
			dateFormat: "dd/mm/yy",
			altField : '#calArrivalDateField',
			altFormat : 'yy-mm-dd',
			minDate: new Date(),
			onSelect: function(dateText, inst) {
				var outDate = new Date($('#chkInDate').datepicker("getDate").getTime() + 86400000);
				if ($('#chkOutDate').datepicker("getDate") == null) {
					$('#chkOutDate').datepicker("setDate",outDate);
				}else if ($('#chkInDate').datepicker("getDate") > $('#chkOutDate').datepicker("getDate")) {
					$('#chkOutDate').datepicker("setDate",outDate);
				}
			}
		});		
		
		$('#chkOutDate').datepicker({ 
			showOn: 'both', 
			buttonImage: basePath + 'images/calendar.gif', 
			buttonImageOnly: true,
			dateFormat: "dd/mm/yy",
			altField : '#calDepartureDateField',
			altFormat : 'yy-mm-dd',
			minDate: new Date()
		});
		
		$('#frmBooking').submit(function() {
			return AutoValidator.validate($(this)[0]);
		});
	}
}


/*
Validate two Date is valid range
1: date1 > date2
2: date1 < date 2
3: date1 = date2
*/
function dateBookingCompare(iDay1,iMonth1,iYear1,iDay2,iMonth2,iYear2) {
	iDay1 = parseInt(iDay1,10);
	iMonth1 = parseInt(iMonth1,10);
	iYear1 = parseInt(iYear1,10);
	
	iDay2 = parseInt(iDay2,10);
	iMonth2 = parseInt(iMonth2,10);
	iYear2 = parseInt(iYear2,10);
	
	var result = 0;
	if (iYear1 > iYear2) {
		result = 1;
	}else if (iYear1 < iYear2) {
		result = 2;
	}else {
		if (iMonth1 > iMonth2) {
			result = 1;
		}else if (iMonth1 < iMonth2) {
			result = 2;
		}else {
			if (iDay1 > iDay2) {
				result = 1;
			}else if (iDay1 < iDay2){
				result =  2;
			}else {
				result = 3;
			}
		}
	}
	return result;
}