
var lSearchDateActive = 'none';
$("lSearchDate1").onclick = function(){ lSearchDateActive = 'lSearchDate1'; };
$("lSearchDate1").onfocus = function(){ this.style.background = "#f3f0e4"; $("lSearchDate2").style.background = "none"; };
$('lSearchDate2').onclick = function(){ lSearchDateActive = 'lSearchDate2'; };
$("lSearchDate2").onfocus = function(){ this.style.background = "#f3f0e4"; $("lSearchDate1").style.background = "none"; };
// onfocus onblur
// Event.observe(element, name, observer, [useCapture]);
// Event.observe('clicker', 'click', function(event){ alert(Event.element(event));});
function LZ(x) {return(x<0||x>9?"":"0")+x}
function lPastDateCheck(dt){
	if(dt != '' && dt != 'dd-mm-yyyy'){
		dtArr = dt.split("-");
		var nowDt = new Date();

		var Y = nowDt.getYear();
		if(Y<1900) Y=Y+1900;
		var M = LZ(nowDt.getMonth()+1);
		var D = LZ(nowDt.getDate());
		dt = dtArr[2]+dtArr[1]+dtArr[0];
		var now = Y+M+D;
		//alert(now);
		if(~~now >= ~~dt)
			return 0;
	}
	return 1;
}

function lSearchDateCheck(one,two){
	return 1;
}

function flatSelected(cal, date) {
	if(cal.dateClicked){
		if(lPastDateCheck(date) == 0){
			cal.tooltips.innerHTML = '<span style="color: #ED1B2E">Date must be in future.</span>';
			return 1;
		}
		if(lSearchDateActive == 'none' ){
			if($("lSearchDate1").value == '' || $("lSearchDate1").value == 'dd-mm-yyyy'){
				$("lSearchDate1").value = date;
				$("lSearchDate1").style.background = "none";
				$("lSearchDate2").style.background = "#f3f0e4";
			}
			else {
				if(lSearchDateCheck($("lSearchDate1").value,date) == 0){
					cal.tooltips.innerHTML = '<span style="color: #ED1B2E">Wrong date order</span>';
					return 1;
				}
				$("lSearchDate2").value = date;
				$("lSearchDate2").style.background = "#f3f0e4";
				$("lSearchDate1").style.background = "none";
			}
		}
		else if(lSearchDateActive == 'lSearchDate1'){
			if(lSearchDateCheck(date,$('lSearchDate2').value) == 0){
				cal.tooltips.innerHTML = '<span style="color: #ED1B2E">Wrong date order</span>';
				return 1;
			}
			$(lSearchDateActive).value = date;
		}
		else if(lSearchDateActive == 'lSearchDate2'){
			if(lSearchDateCheck($('lSearchDate1').value,date) == 0){
				cal.tooltips.innerHTML = '<span style="color: #ED1B2E">Wrong date order</span>';
				return 1;
			}
			$(lSearchDateActive).value = date;
		}
	}
}

function showFlatCalendar() {
  var parent = $("displayLeftCalendar");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;
  cal.setDateFormat("%d-%m-%Y");
  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

	if($("lSearchDate1").value != '' && $("lSearchDate1").value != 'dd-mm-yyyy'){
		//oneArr = $("lSearchDate1").value.split("-");
		//var date=new Date( oneArr[2],oneArr[1]-1,oneArr[0],12,0,0);
		cal.parseDate($("lSearchDate1").value);
		//cal.setDate(date);
	}
  // ... we can show it here.
  cal.show();
}

showFlatCalendar();