// center popup in browser window
function launchCenter(url, name, height, width, scroll) {
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;
  if (window.screen) {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;
    
    if (scroll)
        str += ",toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes";
    else
        str += ",toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=no";
  }
  return window.open(url, name, str);
}

function launchCenterMessage(url, name, showScrollbars, showStatus, showToolbar, resizeable) 
{
  var height = screen.availHeight - 180;
  var width = screen.availWidth - 120;
  
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;

  if (window.screen) 
  {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;
    
    str += ",dependent=yes"
    str += ",scrollbars="   + ((showScrollbars)? "yes" : "no");
    str += ",status="       + ((showStatus)?     "yes" : "no");
    str += ",toolbar="      + ((showToolbar)?    "yes" : "no");
    str += ",resizable="    + ((resizeable)?     "yes" : "no");
  }
  
  return window.open(url, name, str);
}

function launchCenterModal(url, name, height, width, center, resizeable, showScroll, showStatus, unadorned ) 
{
//window.showModalDialog("SMD_target.htm","Dialog Arguments Value",
//    "dialogHeight: 110px; dialogWidth: 1155px; dialogTop: 154px; dialogLeft: 1269px; 
//    edge: Sunken; center: Yes; help: No; resizable: No; status: No;");
  
  var str = "dialogHeight: " + height + "px; dialogWidth:" + width + "px";
    
  if (window.screen) 
  {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += "; dialogTop=" + yc + "px";
    str += "; dialogLeft=" + xc + "px";
        
    str += "; center: yes"
    str += "; resizable: "    + ((resizeable)?     "yes" : "no");
    str += "; scroll: "   + ((showScroll)? "yes" : "no");
    str += "; status: "       + ((showStatus)?     "yes" : "no");
    str += "; unadorned: "      + ((unadorned)?    "yes" : "no");
  }
  
  return window.showModalDialog(url, name, str);
}

function ow(url, type)
{
    var vWinCal = launchCenter(url, type, 500, 450, true);
    if (window.focus) {vWinCal.focus();}
    vWinCal.opener = self;

    //window.open( url, type, "toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=450");
}

function owWithSize(url, type, height, width)
{
    var vWinCal = launchCenter(url, type, height, width, true);
    if (window.focus) {vWinCal.focus();}
    vWinCal.opener = self;
}

function owWithSizeNoScroll(url, type, height, width)
{
    var vWinCal = launchCenter(url, type, height, width, false);
    if (window.focus) {vWinCal.focus();}
    vWinCal.opener = self;
}

function owModal(url, type, height, width)
{
    var vWinCal = launchCenterModal(url, type, height, width, true, false, false, false, false);
}

function owMessage(url, type, showScrollbars, showStatus, showToolbar, resizeable)
{
    var vWinCal = launchCenterMessage(url, type, showScrollbars, showStatus, showToolbar, resizeable)
    if (window.focus) {vWinCal.focus();}
    vWinCal.opener = self;
}

// check for a valid date
function dateCheck(field)
{
    var dateStr = field.value;

    if(dateStr == 'mm/dd/yyyy')
    {
        return false;
    }

    var mmStr = dateStr.substr(0,2);
    var ddStr = dateStr.substr(3,2);
    var yyyyStr = dateStr.substr(6,4);

    var delim1 = dateStr.substr(2,1);
    var delim2 = dateStr.substr(5,1);

    if(dateStr.length != 10 || delim1 != '/' || delim2 != '/')
    {
        if (dateStr.length == 0) {
            field.focus();
            field.value = 'mm/dd/yyyy';
        }
        errorHandling(field, 'Invalid date format. Please enter date using the mm/dd/yyyy notation');
        return false;
    }

    var mmInt = parseInt(mmStr);
    var ddInt = parseInt(ddStr);
    var yyyyInt = parseInt(yyyyStr);

    if(isNaN(mmStr) || mmStr < 1 || mmStr > 12)
    {
        errorHandling(field, 'Invalid month. Valid values are 1-12');
        return false;
    }

    if(isNaN(ddStr) || ddStr < 1 || ddStr > 31)
    {
        errorHandling(field, 'Invalid day. Valid values are 1-31');
        return false;
    }

    if(isNaN(yyyyStr) || yyyyInt < 1900 || yyyyInt > 2100)
    {
        errorHandling(field, 'Invalid year format. Valid format is yyyy (e.g. 2004)');
        return false;
    }

    var currentDate = new Date();
    var currentDd = currentDate.getDate();
    var currentMm = currentDate.getMonth() + 1;
    var currentYyyy = currentDate.getYear();

    if(yyyyInt > currentYyyy)
    {
        errorHandling(field, 'Invalid date. The date you specified is a future date. Please enter todays date or an earlier date');
        return false;
        }
    if(yyyyInt == currentYyyy && mmInt > currentMm)
    {
        errorHandling(field, 'Invalid date. The date you specified is a future date. Please enter todays date or an earlier date');
        return false;
    }
    if(yyyyInt == currentYyyy && mmInt == currentMm && ddInt > currentDd)
    {
        errorHandling(field, 'Invalid date. The date you specified is a future date. Please enter todays date or an earlier date');
        return false;
    }

    return true;
}

// display error message
function errorHandling(field, errorMessage)
{
    alert(errorMessage);
    field.focus();
}


