var bUseShortDateFormat = true;// this to use short date format from locale user settings   localedate.js shoulb be included
var xmlhttp;

var myimages=new Array();
function preloadimages()
{
  for (i=0;i<preloadimages.arguments.length;i++)
  {
    myimages[i]=new Image();
    myimages[i].src=preloadimages.arguments[i];
  }
}

function GetBrowserVersion() // check IE version to be more or equal to IE5.5 SP2
{
  var ua = window.navigator.userAgent;
  var ver = parseFloat(ua.substr(ua.indexOf("MSIE")+5))
  if (ver < 5.5) return false;
  if (ver == 5.5)
  {
    var mv = window.navigator.appMinorVersion;
    var ind = mv.indexOf("SP");
    if (ind < 0) return false;
    var sp = parseInt(mv.substr(ind+2));
    if (sp < 2) return false;
  }
  return true;
}

/******************************* "Wait While Searching" PoPUp *******************************/
var waitPopUp;
var waitPopUp_body;
var iWaitW = 430;
var iWaitH = 100;
InitwaitPopUp();

function InitwaitPopUp()
{
  waitPopUp = window.createPopup();
  waitPopUp.document.createStyleSheet("", 0);
  waitPopUp.document.createStyleSheet("ui_css/style.css");
  waitPopUp.document.styleSheets[0].addRule("body", "background-color: #fffbf7; padding: 0; margin: 0;");
  waitPopUp_body = waitPopUp.document.body;
  waitPopUp_body.innerHTML = '\
                    <div style="border: #90a0b0 1px solid; width: 100%; height: 100%">\
                    <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0" style="padding: 5;">\
                      <tr>\
                        <td valign=middle align=center background="ui_img/forumheader.gif" \
                          style="color: #fffbf7; font-size: 9pt; letter-spacing: 2;">\
                          gathering required information\
                        </td>\
                      </tr>\
                      <tr>\
                        <td valign=middle align=center style="font-weight: bold; padding: 10;" class="darkfont">\
                          in process&nbsp;&nbsp;<img src="ui_img/ell2.gif" align="bottom" border="0">\
                        </td>\
                      </tr>\
                      <tr>\
                        <td valign=middle align=center class="darkfont">\
                          This may take a moment depending on the search details.<br>\
                        </td>\
                      </tr>\
                    </table>\
                    </div>';
}

function ShowWaitPopUp()
{
  if (waitPopUp.isOpen) return;
  var ix = (window.screen.availWidth-iWaitW)/2;
  var iy = (window.screen.availHeight-iWaitH)/3;
  waitPopUp.show(ix,iy,iWaitW,iWaitH,top.document.body);
}
function HideWaitPopUp()
{
  if (!waitPopUp.isOpen) return;
  waitPopUp.hide();
}
/********************************************************************************************/
function SetMemoryOnlyCookie(sName, sValue)
{
  top.document.cookie = sName + "=" + escape(sValue);
}
function SetCookie(sName, sValue)
{
  top.document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2100 23:59:59 GMT; path=/;";
}
function GetCookie(sName)
{
  var aCookie = top.document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return ((aCrumb[1])?unescape(aCrumb[1]):null);
  }
  return null;
}
function DelCookie(sName, sValue)
{
  top.document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/;";
}
/**************************** Calendar PopUp *********************************/
var popUp;
var popup_body;
var oCall;
var iW = 300;
var iH = 275;

function InitPopUp()
{
  popUp = window.createPopup();
  popUp.document.createStyleSheet("", 0);
  popUp.document.createStyleSheet("ui_css/style.css");
  popUp.document.styleSheets[0].addRule("body", "background-color: #fffbf7; padding: 0; margin: 0;");
  popup_body = popUp.document.body;
  popup_body.innerHTML = "<ns:calendar class='calendar' PrevNextMonth id='idCal' style='width: 100%; height: 100%;'></ns:calendar>";
  oCall = popup_body.all.idCal;
  oCall.style.behavior= "url('ui_js/calendar.htc')";
  InitCalendar();
}
function InitCalendar()
{
  oCall.attachEvent("onreadystatechange", CalendarReadyStateChanged);
  popUp.show(0,0,0,0,document.body);
}
function CalendarReadyStateChanged()
{
  if (oCall.readyState == "complete")
  {
    oCall.detachEvent("onreadystatechange", CalendarReadyStateChanged);
    hidePopup(popUp);
  }
}
function show_calendar(oInputField, oDocMaster)
{
  var oInput   = eval(oInputField);
  var oInput_v = eval(oInputField+"_visible");
  var oCal = top.oCall;
  var oPopUp = top.popUp;
  if (!oCal || !oPopUp)
  {
    alert("There is a problem using Calendar! Please contact us.");
    return;
  }
  try { oCal.value = oInput.value; }  catch(e) {}
  var ix = Math.max(0, (event.x - iW));
  var iy = event.y;
  oPopUp.show(ix,iy,iW,iH,document.body);
  oCal.onchange = function() { oInput.value = oCal.value; oInput_v.value = GetLocaleDateString(oInput.value); hidePopup(oPopUp); };
}
function hidePopup(popUp)
{
  popUp.hide();
}
function GetLocaleDateString(testdate)
{
  var t = Date.parse(testdate);
  if (isNaN(t)) return "";
  var d = new Date(t);
  if (bUseShortDateFormat)
  {
    var retVal;
    try { retVal = GetLocaleShortDateStringVB(d.getVarDate()); }
    catch(e) { retVal = testdate; }
    return retVal;
  }
  return d.toLocaleDateString();
}
function GetDateFromLocaleDateString(oInputField)
{
  var oInput   = eval(oInputField);
  var oInput_v = eval(oInputField+"_visible");
  var testdate = oInput_v.value;
  if (oInput_v.value == "")
  {
    oInput.value = "";
    return;
  }
  testdate = GetLocaleLongDateStringVB(testdate);

  var t = Date.parse(testdate);
  if (isNaN(t))
  {
    alert("The string you typed is not a valid date.\nPlease try again.");
    oInput_v.focus();
    return;
  }
  var d = new Date(t);
  var str = (d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear();
  oInput.value = str;
}
/******************** Show Comments For Transcriptions ****************************/
var oCommentWindow;
function ShowCommentForTranscription(strTranID)
{
  if ( (oCommentWindow) && (!oCommentWindow.closed) ) return;
  var strURL = "_InqTranComment.asp?DBID="+strTranID;
  var strDialogProp = "help: no; status: no; dialogWidth: 450px; dialogHeight: 150px;";
  oCommentWindow = window.showModelessDialog(strURL, "", strDialogProp);
  return oCommentWindow;
}
/*************************** Show User Preferences *******************************/
var oPreferencesWindow;
function ShowPreferences()
{
  if ( (oPreferencesWindow) && (!oPreferencesWindow.closed) ) return;
  var strURL = "_InqPreferences.asp";
  var strDialogProp = "help: no; status: no; dialogWidth: 520px; dialogHeight: 150px;";
  oPreferencesWindow = window.showModelessDialog(strURL, "", strDialogProp);
}
/****************************** Legend PopUp *************************************/
var popUpLegend;
function ShowLegend()
{
  if (!popUpLegend)
    InitLegendPopup();
  var iW = 400;
  var iH = 60;
  var ix = parseInt((document.body.offsetWidth-iW)/2);
  var iy = parseInt((document.body.offsetHeight-iH)/2);
  popUpLegend.show(ix,iy,iW,iH,document.body);
}
function InitLegendPopup()
{
  popUpLegend = window.createPopup();
  popUpLegend.document.createStyleSheet("", 0);
  popUpLegend.document.styleSheets[0].addRule("body", "background-color: #fffbf7; padding: 0; margin: 0; cursor: default; overflow: auto;");
  popUpLegend.document.styleSheets[0].addRule("td", "font-family : Verdana, Arial, sans-serif ; font-size : 8pt; color : #000066;");
  popUpLegend_body = popUpLegend.document.body;
  popUpLegend_body.innerHTML = '\
    <div style="width: 100%; height: 100%;">\
      <table cellSpacing="4" cellPadding="1" border="1" style="border-collapse: collapse" bordercolor="#90A0B0" width="100%" height="60">\
        <tr>\
          <td noWrap bgcolor="#ffcccc" align="center" style="color: #fffbf7; font-size: 9pt; letter-spacing: 3;">\
            pink bar\
          </td>\
          <td noWrap>\
            Stat Transcription\
          </td>\
        </tr>\
        <tr>\
          <td noWrap align="center" style="color: #990000;">\
            red (maroon) text\
          </td>\
          <td noWrap>\
            Transcription came from Review folder\
          </td>\
        </tr>\
      </table>\
    </div>';
  popUpLegend_body.onmouseleave = hideLegendPopup;
  popUpLegend_body.onclick      = hideLegendPopup;
}
function FirstInitLegendPopup()
{
  popUpLegend.attachEvent("onreadystatechange", LegendReadyStateChanged);
  popUpLegend.show(0,0,0,0,document.body);
}
function LegendReadyStateChanged()
{
  if (popUpLegend.readyState == "complete")
  {
    popUpLegend.detachEvent("onreadystatechange", LegendReadyStateChanged);
    hideLegendPopup();
  }
}
function hideLegendPopup()
{
  popUpLegend.hide();
}
/******************************************************************************/
function AttachDropDownBehavior()
{
  var oUrnColl = event.srcElement.behaviorUrns;
  if (oUrnColl != null)
    for (var i= 0; i< oUrnColl.length; i++)
      if (oUrnColl(i) == "dropdown")
        return;
  event.srcElement.addBehavior('ui_js/dropdown.htc');
}
/*****************************************************************************/
function CheckTranTemplates(TID)
{
  if (!xmlhttp)
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  var strUrl = "_InqCheckTranTempl.asp?TID="+TID;
  xmlhttp.open("GET", strUrl, false);
  xmlhttp.send();
  var strRes = xmlhttp.responseText;
  return parseInt(strRes);
}
function UpdateLog(res, app, errCode, pageName)
{
  if (!xmlhttp)
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  var strUrl = "_log.asp";
  xmlhttp.open("POST", strUrl, true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  
  var a = {};
  a["result"] = res;
  a["component"] = app;
  a["errorCode"] = (errCode==void(0))?"-":errCode;
  a["pageName"] = (pageName==void(0))?"no page name":pageName;
  a["minor"]  = window.navigator.appMinorVersion;

  xmlhttp.send(PackObject(a));
  function PackObject(ob) {
    var text = "";
    for ( p in ob )
      text = text.concat(p, "=", escape(ob[p]), "&");
    return text;
  }
}
/////////////////////// do not allow to input anything except the numbers
function KeyNumCheck()
{
  var iKeyCode = event.keyCode;
  if ( (iKeyCode != 9) && 
    ( ((iKeyCode > 57)&&(iKeyCode < 96)) || (iKeyCode > 105) || (event.shiftKey) || (iKeyCode == 32)) )
    event.returnValue = false;
}
function trim(sTmp) {
  return String(sTmp).replace(/^\s|\s+$/g,'');
}
function CheckForSpaces(strFieldName)
{
  var el = event.srcElement;
  el.value = trim(el.value);
  if (el.value.indexOf(" ") >= 0)
  {
    try{ el.focus(); event.cancelBubble=true; } catch(e) {}
    ShowCustomAlert(true, "unallowed symbol", "No Spaces Are Allowed in the <i>"+strFieldName+"</i> Field", 450, 145);
    return false;
  }
}

/////////////////////// add dash to ZIP box after 5th digit
function HandleDash()
{
  var el = event.srcElement;
  if ( (el.value.length == 5) && (el.value.indexOf("-") < 0) )
    el.value = el.value + "-";
  if ( (el.value.substr((el.value.length-2), 1) == "-") && (event.keyCode == 8) )
    el.value = el.value.substring(0, (el.value.length-2))+el.value.substr((el.value.length-1), 1);
}
/******************************** Custom Alert *************************************/
function ShowCustomAlert(bAlert, strHeader, strText, iW, iH)
{
  var strURL = (bAlert)?"_Alert.htm":"_Confirm.htm";
  var strDialogProp = "help: no; resizable: no; scroll: no; unadorned: no; status: no; dialogWidth: "+iW+"px; dialogHeight: "+iH+"px;";
  return window.showModalDialog(strURL, {alert: bAlert, text: strText, header: strHeader}, strDialogProp);
}
/******************************** Do not drag any images ***************************/
window.attachEvent("onload", DoNotDragAnyImages)
function DoNotDragAnyImages()
{
  var oImgColl = document.all.tags("img");
  for (var i= 0; i< oImgColl.length; i++)
    oImgColl[i].attachEvent("ondrag", function() {event.returnValue=false;})
}
/********************************* Attaching On_Printing_Complete ***************************/
function AttachPrinterOnComplete()
{
  factory.attachEvent("oncomplete", PrinterOnComplete);
}
function DetachPrinterOnComplete()
{
  factory.detachEvent("oncomplete", PrinterOnComplete);
}
function PrinterOnComplete()
{
  DetachPrinterOnComplete();
  if (GetCookie('EmdatInQuiryCompleteAfterPrint'))
    CompleteAllT();
}
function SetUserPrintingPreferences(parentObj)
{
  var obj;
  if (parentObj)
    obj = parentObj.factory.printing;
  else
    obj = factory.printing;
  if ( (GetCookie("EmdatInQuiryDefaultPrinter") != "") && (!!GetCookie("EmdatInQuiryDefaultPrinter"))
    && (GetCookie("EmdatInQuiryDefaultPrinter") != obj.defaultPrinter) )
  {
    obj.printer = GetCookie("EmdatInQuiryDefaultPrinter");
    obj.paperSource = GetCookie("EmdatInQuiryFirstPageTray");
  }
}