var FIELD_UNMODIFIED = 0;
var FIELD_MODIFIED_BY_USER = 1;
var FIELD_MODIFIED_BY_SCRIPT = 2;

var field2Modified = FIELD_UNMODIFIED; // 0 = original value (either nothing or
										// from DB), 1 = modified by user, 2 =
										// filled in by script
										
var field2Increment = 0;

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {

  cal.sel1.value = date; // just update the date in the input field.
  if (cal.sel2){

		//alert("field2Modified = " + field2Modified);
		var okToReplaceDate = true;
		if (!cal.sel2.value || cal.sel2.value == 'mm/dd/yyyy') { //if second date is empty, ok to replace
			okToReplaceDate = true;
		} else if (field2Modified == FIELD_UNMODIFIED) {
			okToReplaceDate = false;
		} else if (field2Modified == FIELD_MODIFIED_BY_SCRIPT) {
			okToReplaceDate = false;
		} else if (field2Modified == FIELD_MODIFIED_BY_USER) {
			okToReplaceDate = false;
		}
		//alert("okToReplaceDate = " + okToReplaceDate);
		
		if (okToReplaceDate == true) {
	 	var valueofNewDate = date.valueOf();
		var newDate = new Date(valueofNewDate);
		newDate.setDate(newDate.getDate() + field2Increment);
			cal.sel2.value = (((newDate.getMonth() + 1) < 10) ? ("0" + (newDate
					.getMonth() + 1)) : ("" + (newDate.getMonth() + 1)))
					+ "/"
					+ (((newDate.getDate()) < 10) ? ("0" + (newDate.getDate()))
							: ("" + newDate.getDate()))
					+ "/"
					+ newDate.getFullYear(); // '%m/%d/%Y'
			field2Modified = FIELD_MODIFIED_BY_SCRIPT;
	 }
  }
  if (cal.dateClicked)
  //{
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
   // closeHandler(cal);
   // }
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
 // cal.destroy(); 
  _dynarch_popupCalendar = null;
}


function findPos(obj) { 
	 
	var curleft = curtop = 0; 
	if (obj.offsetParent) { 
	        curleft = obj.offsetLeft;
	        curtop = obj.offsetTop;
	        while (obj = obj.offsetParent) { 
	                curleft += obj.offsetLeft; 
	                curtop += obj.offsetTop; 
	        } 
	} 
	return [curleft,curtop]; 
	}



// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id1, format, id2, field2DateIncrement) {
  var el1 = document.getElementById(id1);
  
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);

    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    
    
    if(location.pathname=="/notice/list")
    	cal.setDisabledHandler(isDisabled);
    if(location.pathname=="/notice/listing")
    	cal.setDisabledHandler(isDisabled);
    
    if(location.pathname=="/secure/admin/notify_marketingemailmessage")
    	cal.setDisabledHandler(isDisabled);
    
    cal.create();
  }
      if (id2) {
    	var el2 = document.getElementById(id2);
		_dynarch_popupCalendar.sel2 = el2; // inform it what input2 field we
											// use
		field2Increment = field2DateIncrement;
    }
    
	_dynarch_popupCalendar.setDateFormat(format); // set the specified date
	_dynarch_popupCalendar.parseDate(el1.value); 	// format
  _dynarch_popupCalendar.sel1 = el1;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
	//_dynarch_popupCalendar.showAtElement(el1.nextSibling, "Br"); // show the
																	// calendar

	//_dynarch_popupCalendar.showAtElement(el1.nextSibling, "BR"); // show the
	// calendar

var myPos = findPos(document.getElementById(id1));
x = myPos[0]; 
y = myPos[1]; 

if((location.pathname=="/notice/list") || (location.pathname=="/notice/listing"))
	_dynarch_popupCalendar.showAt((x+124),(y+22));
else
	_dynarch_popupCalendar.showAtElement(el1.nextSibling, "BR"); // show the calendar

//_dynarch_popupCalendar.showAt((x+124),(y+22));



// cal.setDateFormat("%A, %B %e");



  
  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;
var one_day=1000*60*60*24;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.


// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  if((location.pathname=="/notice/list") || (location.pathname=="/notice/listing")){
	  if(today.getTime()>date.getTime()){
		  //alert(today.getTime());
		  return false;
	  }else{
		  return true;
	  }
  }else if(location.pathname=="/secure/admin/notify_marketingemailmessage"){
	  if(((today.getTime()-2*one_day)>date.getTime()) || ((today.getTime()+7*one_day)<date.getTime())){
		  //alert(today.getDay());
		  return true;
	  } else{
		  return false;
	  }
}
}
