var count=0;
var vWin;
function show_calendar()
{
  var lNow = new Date();
  /* 
    arguments
    1: p_item  : Return Item.
    2: p_usetime : Default time to use, to make value easier to use w/DB Datetime cols
    3: p_timezone : Time zone of user
    4: p_hidden  : Return Item in ANSI / ISO format
    5: p_month : 0-11 for Jan-Dec
    6: p_year  : 4-digit year
    7: p_locale_pref: Locale Pref Id to be sent to server to get Dates & other data for calendar in local format
    8: p_yearly: Show Year Calendar ('Y') or Show month calendar (!='Y')
    9: p_onchangeevent_func: Function to call if date is changed
  */

  var p_item = arguments[0];
  //alert(p_item);
  var p_usetime = arguments[1];
  if (p_usetime == null) {
    p_usetime = "";
  }
  //alert(p_usetime);
  var p_timezone = arguments[2];
  if (p_timezone == null) {
    p_timezone = "";
  }
  else {
   p_timezone = p_timezone.replace("+","%2B");
  }
  //alert(p_timezone);
  var p_hidden = arguments[3];
  var p_month = null;
  //alert(p_hidden);

  if (arguments[4] == "" || arguments[4] == null) {
    p_month = new String(lNow.getMonth());
  }
  else {
    p_month = arguments[4];
  }

  if (arguments[5] == "" || arguments[5] == null) {
    p_year = new String(lNow.getFullYear().toString());
  } 
  else {
    p_year = arguments[5];
  }

  //alert(p_year);
  if (p_hidden != null && p_hidden != "") {
    var hi = p_hidden.split('.');
    var oHid = eval("self.document." + hi[0] + ".elements[\"" + hi[1] + "\"]");
    if (oHid.value.length > 0) {
      var aDate = oHid.value.split("-");
      p_year = aDate[0];
      p_month = (aDate[1] - 1);
    }
  }

  var p_locale_pref = "";
  if (arguments[6] != null) {
    p_locale_pref = arguments[6];
  }
  //alert(p_locale_pref);

  var p_yearly = "";
  if (arguments[7] != null) {
    p_yearly = arguments[7];
  }

  //alert(p_yearly);
  var p_onchangeevent_func = "";
  if (arguments[8] != null) {
    p_onchangeevent_func = arguments[8];
  }
  if(count !=0) {
    if(vWin.closed)
      count=0;
    else
      if (vWin.focus)
        vWin.focus();
    }

  if(count==0)
    {
      count++;
  if (p_yearly != "Y") {
    vWin = window.open("/sdccommon/asp/cal.asp?ret_date="+p_item+"&hidden_date="+p_hidden+"&context_month="+p_month+"&context_year="+p_year+"&context_timezone="+p_timezone+"&context_usetime="+p_usetime+"&context_lcid="+p_locale_pref+"&onchange_eventfunc="+p_onchangeevent_func,'', 'width=300,height=250');
  }
  else {
    vWin = window.open("/sdccommon/asp/cal.asp?ret_date="+p_item+"&hidden_date="+p_hidden+"&context_month="+p_month+"&context_year="+p_year+"&yearly=Y&context_timezone="+p_timezone+"&context_usetime="+p_usetime+"&context_lcid="+p_locale_pref+"&onchange_eventfunc="+p_onchangeevent_func,'', 'scrollbars=yes,resizable=yes');
  }
}
}


function sprt_clear_date(fldName) {
  document.all(fldName).value = "";
  document.all(fldName+"_display").value = "";
}

// this is the primary function for having date - time entry
// it creates two input fields
// and relies upon being in the first form
// the argName is the name for the HIDDEN (real data) field. Formname can be appended to Hidden field name, if it is not posible to resolve form index.
// and argName with "_display" appended is the visible field
// argClass can be passed in to control style sheet for the visible input
// and the ANSI date string should be in YYYY-MM-DDTHh:Mi:Ss.mmm+/-Th:Tm format 
// Its better to pass DispDateString as this helps it to be JS date object independent
// Its better to pass p_usetime (00:00:00 for low & 23:59:59 to high) as it makes more meaningful display
// Timezone & locale pref (LCID) info will be passed to calender for rendering dates in user preferred formats
// Following is most acceptable call
//    sprt_date_text("testing", "entry", "2002-02-04T07:59:59", "02/03/2002 11:59:59 PM","23:59:59", "-08:00", "1033", "MyFunc()");
function sprt_date_text()
{
  var dt;
  var stdDateString = "";
  var displayString = "";
  var appendArg = "";

  /* 
    arguments
    1: argName
    2: argClass
    3: argANSIDateString
    4: argDispDateString
    5: p_usetime
    6: p_timezone
    7: p_locale_pref
    8: p_onchangeevent_func
  */ 
  var argName = arguments[0]; 
  var argClass = arguments[1];
  var argANSIDateString = arguments[2];
  var argDispDateString = arguments[3];
  var p_usetime = arguments[4];
  var p_timezone = arguments[5];
  var p_locale_pref = arguments[6];  
  var p_onchangeevent_func = "";
  
  // read event function pointer if passed in
  if (arguments.length == 8) 
  {
    p_onchangeevent_func = arguments[7];
  }

  if (argANSIDateString != null && argANSIDateString != "")
  {
    var ansiDate = argANSIDateString;
    if (ansiDate.indexOf("T", 0) > 0) {
      ansiDate = argANSIDateString.substring(0, argANSIDateString.indexOf("T", 0));
    }
    if (argDispDateString == null || argDispDateString == "") {
      var aDate = ansiDate.split("-");
      if (aDate.length >= 2) {
        dt = new Date(aDate[0], aDate[1]-1, aDate[2]);
        displayString = dt.toDateString();
        stdDateString = argANSIDateString;
      } 
    }
    else {
      displayString = argDispDateString;
      stdDateString = argANSIDateString;
    }
  }
  //Form Name can be appended to argName by caller of sprt_date_text in case it
  //is difficult to resolve form index.
  var aName = argName.split(".");
  var aNameLen = aName.length;
  if (aNameLen >= 2 ) {
    appendArg = aName[aNameLen - 2];
    argName = aName[aNameLen - 1];
  }
  else {
    appendArg = "form(0)";
  }
  // to debug, it is useful to change the hidden to edit
  document.write('<input type="text" name="'+ argName +'_display" id="'+ argName +'_display" size="30" maxlength="30" class="'+argClass+'" readonly value="'+ displayString +'"><span>'+p_timezone+'</span><input type="hidden" name="'+argName+'" value="'+stdDateString+'">');
  var href = "javascript:show_calendar('"+appendArg+"\\."+argName+"_display','"+p_usetime+"','"+p_timezone+"','"+appendArg+"\\."+argName+"',null,null,'"+p_locale_pref+"','N','"+p_onchangeevent_func+"');";
  document.write('&nbsp;<img class="clsCalendarButton" onclick="'+href+'" border="0" src="/sdccommon/images/bttn_calendar.gif" alt="Set Date">&nbsp;');
  document.write('<span title="Clear date field"><button class="clsTextSmallButton1" onclick="javascript:sprt_clear_date(\'' + argName + '\');">Clear</button></span>');
}

