// $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'].jscalendar").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('jscalendar');

      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-depart"){
      Calendar.setup({
        inputField  : id,
        ifFormat    : settings['ifFormat'],
        button      : id + '-button',
        showsTime   : settings['showsTime'],
        timeFormat  : settings['timeFormat'],
		onUpdate       :    catcalc,
		dateStatusFunc : ourDateStatusDepart,
		weekNumbers : false,
		onSelect: ourSelect
      });
	  }else{
	  	if (id == "edit-arrive"){
		  Calendar.setup({
	        inputField  : id,
	        ifFormat    : settings['ifFormat'],
	        button      : id + '-button',
	        showsTime   : settings['showsTime'],
	        timeFormat  : settings['timeFormat'],
			onUpdate       :    catcalc,
			dateStatusFunc : ourDateStatusArrive,
			weekNumbers : false,
			onSelect: ourSelect
	      });
	    }else{
	          Calendar.setup({
		        inputField  : id,
		        ifFormat    : settings['ifFormat'],
		        button      : id + '-button',
		        showsTime   : settings['showsTime'],
		        timeFormat  : settings['timeFormat']
		      });
	    }
	  }
    });
  });
}
function catcalc(cal) {
    if(document.getElementById("edit-arrive")){
		var date = cal.date;
		var time = date.getTime()
		var field = document.getElementById("edit-depart");
		var field2a = document.getElementById("edit-arriveb");
		var field2d = document.getElementById("edit-departb");
		if (field != cal.params.inputField) {
//ImpactivChange
//			time += Date.WEEK; // add one week
			if (field2a!=null) field2a.value = date.print("%m/%d/%Y");
			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");
			if (field2d!=null) field2d.value = date2.print("%m/%d/%Y");
		} else {
			if (field2d!=null) field2d.value = date.print("%m/%d/%Y");
		}
	}
}

function ourDateStatusDepart(date, y, m, d) {
	  if(document.getElementById("edit-arrive")){
		  arrive = getArrive()
		  inputdate = getInput(y,m,d);
		  if (inputdate <= arrive){	  
			return true;
		  }else{
			return false;
		  }
	  }
  };
  
  function getArrive(){
    var field = document.getElementById("edit-arrive").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 ourDateStatusArrive(date, y, m, d) {
	if(document.getElementById("edit-arrive")){
	  today = getToday();
	  inputdate = getInput(y,m,d);
	  if (inputdate < today){	  
		return true;
	  }else{
		return false;
	  }
	}
  };
  
  function getToday(){
  today = new Date();
	  ty = today.getFullYear().toString();
	  tm = today.getMonth().toString();
	  if (tm.length == 1){
		tm = '0'+tm;
	  }
	  td = today.getDate().toString();
	  if (td.length == 1){
		td = '0' + td;
	  }
	  return ty + tm + td;
  }
  
  function getInput(y,m,d){
    dy = y.toString();
	  dm = m.toString();
	  if (dm.length == 1){
		dm = '0' + dm;
	  }
	  dd = d.toString();
	  if (dd.length == 1){
		dd = '0'+ dd;
	  }
	  return dy + dm + dd;
  }
  
  function ourSelect(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-arrive"))&& (document.getElementById("edit-arrive") != cal.params.inputField)){
				arrival = getArrive();
				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();
	};

