// $Id: jscalendar.js,v 1.8.2.1 2007/02/02 04:17:02 nedjo Exp $

if (Drupal.jsEnabled) {
  $(document).ready(function() {
    $("input[@type='text'].jscalendarb").each(function (){
      var id = $(this).id();
      var form = this.form;
      var button = document.createElement('button');
      $(button)
        .html(' ... ')
        .id(id + '-button')
        .addClass('jscalendar-icon');
      $(this)
        .after(button)
        .parent().addClass('jscalendarb');

      var settings = [];
      settings['ifFormat'] = $('#' + id + '-jscalendar-ifFormat', form).size() > 0 ? $('#' + this.id + '-jscalendar-ifFormat', form).val() : '%Y-%m-%d %H:%M:%S';
      // We use eval() because the result is a boolean while our input is a string.
      settings['showsTime'] = $('#' + id + '-jscalendar-showsTime', form).size() > 0 ? eval($('#' + this.id + '-jscalendar-showsTime', form).val()) : true;
      settings['timeFormat'] = $('#' + id + '-jscalendar-timeFormat', form).size() > 0 ? $('#' + this.id + '-jscalendar-timeFormat', form).val() : '12';
	  if (id == "edit-departb"){
      Calendar.setup({
        inputField  : id,
        ifFormat    : settings['ifFormat'],
        button      : id + '-button',
        showsTime   : settings['showsTime'],
        timeFormat  : settings['timeFormat'],
		onUpdate       :    catcalc_book,
		dateStatusFunc : ourDateBookDepart,
		weekNumbers : false,
		onSelect: ourBookSelect
      });
	  }else{
	  	if (id == "edit-arriveb"){
		  Calendar.setup({
	        inputField  : id,
	        ifFormat    : settings['ifFormat'],
	        button      : id + '-button',
	        showsTime   : settings['showsTime'],
	        timeFormat  : settings['timeFormat'],
			onUpdate       :    catcalc_book,
			dateStatusFunc : ourDateBookArrive,
			weekNumbers : false,
			onSelect: ourBookSelect
	      });
	    }else{
	          Calendar.setup({
		        inputField  : id,
		        ifFormat    : settings['ifFormat'],
		        button      : id + '-button',
		        showsTime   : settings['showsTime'],
		        timeFormat  : settings['timeFormat']
		      });
	    }
	  }
    });
  });
}
function catcalc_book(cal) {
    if(document.getElementById("edit-arriveb")){
		var date = cal.date;
		var time = date.getTime()
		var field = document.getElementById("edit-departb");
		if (field != cal.params.inputField) {
//ImpactivChange
//			time += Date.WEEK; // add one week
			time += Date.DAY * 2; // add two days
			var date2 = new Date(time);
//ImpactivChange
//			field.value = date2.print("%d/%m/%Y");
			field.value = date2.print("%m/%d/%Y");
		}
	}
}

function ourDateBookDepart(date, y, m, d) {
	  if(document.getElementById("edit-arriveb")){
		  arrive = getBookArrive()
		  inputdate = getInput(y,m,d);
		  if (inputdate <= arrive){	  
			return true;
		  }else{
			return false;
		  }
	  }
  };
  
  function getBookArrive(){
    var field = document.getElementById("edit-arriveb").value;
	  //check the year
//ImpactivChange
//	  ay = field.substr(6);
//	  am = field.substring(3,5)
//	  am = am - 1;
//	  am = am.toString();
//	  if (am.length == 1){
//		am = '0' + am;
//	  }
//	  ad = field.substring(0,2)
	  ay = field.substr(6);
	  am = field.substring(0,2)
	  am = am - 1;
	  am = am.toString();
	  if (am.length == 1){
		am = '0' + am;
	  }
	  ad = field.substring(3,5)
	  return ay + am + ad;
  }
  
  function ourDateBookArrive(date, y, m, d) {
	if(document.getElementById("edit-arriveb")){
	  today = getToday();
	  inputdate = getInput(y,m,d);
	  if (inputdate < today){	  
		return true;
	  }else{
		return false;
	  }
	}
  };
    
  function ourBookSelect(cal) {
		var p = cal.params;
		var update = (cal.dateClicked || p.electric);
		var today = getToday();
		ty = cal.date.getFullYear().toString();
			tm = cal.date.getMonth().toString();
			if (tm.length == 1){
				tm = '0'+tm;
			}
			td = cal.date.getDate().toString();
			//alert(tm)
			if (td.length == 1){
				td = '0' + td;
			}
		var caldate = ty + tm +td;
		if (update && p.inputField) {
			if(today < caldate){
				p.inputField.value = cal.date.print(p.ifFormat);
				if (typeof p.inputField.onchange == "function"){
					p.inputField.onchange()
				}
			}
		}
		if (update && p.displayArea)
			p.displayArea.innerHTML = cal.date.print(p.daFormat);
		if (update && typeof p.onUpdate == "function"){
			if((document.getElementById("edit-arriveb"))&& (document.getElementById("edit-arriveb") != cal.params.inputField)){
				arrival = getBookArrive();
				if((today < caldate) && (arrival < caldate)){
					p.onUpdate(cal)
				}
			}else{
				if(today < caldate){
					p.onUpdate(cal)
				}
			}
		}
		if (update && p.flat) {
			if (typeof p.flatCallback == "function")
				p.flatCallback(cal);
		}
		if (update && p.singleClick && cal.dateClicked)
			cal.callCloseHandler();
	};

