/*  SCSGDatePicker - provide default functionaliti for using
 *  JSCalendar with date as a text-box with html-button to lounch.
 * 1 may 2006 - Sveinn S. Erlendsson - Habilis ehf.
 * -----------------------------------------------------------
 *
*/

 var SCSGddfield = null;
 var SCSGpostform = null;
 var SCSGPostFunctionRun = false;
 var SCSGPostFunction = function(){};  //This function can get overritten i.e. SCSGPostFunction = function(){alert('hello');}; SCSGPostFunctionRun = true; 


 function SCSGDateCloseHandler(cal) {
   cal.hide();       
  _dynarch_popupCalendar = null;
 }
 
  function SCSGDateSelected(cal, date) {
    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.sel.value = date;
    cal.callCloseHandler();
    //To work vith asp-validators we nee to do some tricks
    //SCSGddfield.value = '';
    //SCSGddfield.focus();
    //alert('new date: '+date);
    SCSGddfield.value = date;
    //SCSGddfield.blur();
    if (SCSGpostform != null){
      SCSGpostform.submit();
    }
    if (SCSGPostFunctionRun){
       SCSGPostFunction();
    }
  }  
  //alert('Year: '+cal.date.getFullYear());
 }
 
 //Run the calendar, optionally we can add min and max date to activate date ranger
 function SCSGRunCalendar(dateTxtBoxId,postform,minYear,minMon,minDay,maxYear,maxMon,maxDay){
    if (postform == true){
     SCSGpostform = document.forms[0];
    }
    SCSGddfield = document.getElementById(dateTxtBoxId);
    d = new Date();
    if (_dynarch_popupCalendar != null) {
      _dynarch_popupCalendar.hide();                 // so we hide it first.
    } else {
      // first-time call, create the calendar.
      var cal = new Calendar(1, null, SCSGDateSelected, SCSGDateCloseHandler);
      cal.showsOtherMonths = true;
      _dynarch_popupCalendar = cal;    // remember it in the global var
      if (minYear == null)
        minYear = 1900;
      if (maxYear == null)
        maxYear = 2070;
      if (minMon == null)
        minMon = 1;
      if (minDay == null)
        minDay = 1;
      if (maxMon == null)
        maxMon = 12;
      if (maxDay == null)
        maxDay = 31;
      cal.setRange(minYear, maxYear);        // min/max year allowed.
      //alert(maxMon+'+'+maxDay);
      cal.setupDateRange(minYear,minMon,minDay,maxYear,maxMon,maxDay);
      cal.create();
      cal.sel = SCSGddfield;
      if (SCSGddfield != null && SCSGddfield.value != null && SCSGddfield.value != ''){
         cal.parseDate(SCSGddfield.value); 
      }else{
         if (cal.minDate > d)
           d = new Date(cal.minDate.getFullYear(),cal.minDate.getMonth(),cal.minDate.getDate());
         if (cal.maxDate < d)
           d = new Date(cal.maxDate.getFullYear(),cal.maxDate.getMonth(),cal.maxDate.getDate());
         cal.setDate(d);
      }
     _dynarch_popupCalendar.showAtElement(SCSGddfield, "Br");        // show the calendar
    }
    
   
 }                                   




