/*
 * DMDatePicker.js
 * © Digimaker AS. All rights reserved.
 *
 * Author: Alan (04.02.2004) 
 *
 * Comments:
 * Alan (04.02.2004) Initial code copied from Calendar selector website.  Altered
 * various calls to make it suit our needs.  Some of the functions can probably be
 * pruned
 *
 */

// gets rid of data within row when clear button hit
function clearRow (selectedDate,listhours,listminutes){
	document.getElementById(selectedDate).value = "";
	document.getElementById(listhours).selectedIndex = [0];
	document.getElementById(listminutes).selectedIndex = [0];
}
//clear the selected date field.
function clearSelected(selectedDate) {
	document.getElementById(selectedDate).value = "";
}

function InitDmModifications()
{
    if(typeof(ValidatorOnChange) == "function"){
        // FUNCTION OVERRIDE FROM WEBRESOURCES.AXD
        ValidatorOnChange = function (evt) {
            if (!evt) {
                evt = window.event;
            }
            if(typeof(evt) != "undefined")
            {
                Page_InvalidControlToBeFocused = null;
                var targetedControl;
                if ((typeof(evt.srcElement) != "undefined") && (evt.srcElement != null)) {
                    targetedControl = evt.srcElement;
                }
                else {
                    targetedControl = evt.target;
                }
                var vals;
                if (typeof(targetedControl.Validators) != "undefined") {
                    vals = targetedControl.Validators;
                }
                else {
                    if (targetedControl.tagName.toLowerCase() == "label") {
                        targetedControl = document.getElementById(targetedControl.htmlFor);
                        vals = targetedControl.Validators;
                    }
                }
                var i = 0;
                if(typeof(vals) != "undefined"){
                    for (i = 0; i < vals.length; i++) {
                        ValidatorValidate(vals[i], null, evt);
                    }
                }
                ValidatorUpdateIsValid();
            }
        }
    }
}

// this call is put at the en of the pages Digimaker.Web.Controls.MasterPage
//InitDmModifications();


// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  // 4th Aug, 2006 - Joel : Only trigger DateClicked if an actual date 
  // in the calendar grid is explicitly clicked.
  if (cal.dateClicked)
  {
    // if we add this call we close the calendar on single-click.
    cal.callCloseHandler();
    
    // Added this function call which pages that use the calendar control can implement.
	// It's important that the pages creates a function called DateClicked, or an error will be thrown.	
	try{
		DateClicked();
	}catch(e){
		//alert(e.description + " [ the custom function: DateClicked() ]");
	}
  }
}

// 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();
  calendar = null;
}

// 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(id, format, showsTime) {	
  var el = document.getElementById(id);
  if (typeof(calendar) != "undefined") { //calendar != null
    // we already have some calendar created
    //calendar.hide();                 // so we hide it first.
    calendar = null;
  } //else
  { // .hide() did not work after upgrading to v1.5 so always deleting the object and creating a new one when used in e-Forms
    // first-time call, create the calendar.
    var cal = new Calendar(true, null, selected, closeHandler);    
    
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  calendar.setDateFormat(format);    // set the specified date format
  calendar.parseDate(el.value);      // try to parse the text in field
  calendar.sel = el;                 // 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.
  calendar.showAtElement(el.nextSibling, "BR");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

