function checkProp(elementID,newElement1,newElement2) {
if (document.getElementById(elementID).currentStyle) {
var cssProp = document.getElementById(elementID).currentStyle["backgroundColor"];
} else if (window.getComputedStyle) {
var elstyle = window.getComputedStyle(document.getElementById(elementID), "");
cssProp = elstyle.getPropertyValue("background-color"); }
document.getElementById(newElement1).style.borderColor=cssProp;
document.getElementById(newElement2).style.borderColor=cssProp;
}

function turnOnClass(menuName,menuClass) {
    newMenuClass = menuClass + '_on';
    document.getElementById(menuName).className=newMenuClass;
}

function turnOffClass(menuName,menuClass) {
    newMenuClass = menuClass + '_off';
    document.getElementById(menuName).className=newMenuClass;
}

function showDiv(divName) {
    document.getElementById(divName).className='visDiv';
}

function hideDiv(divName) {
    document.getElementById(divName).className='hidDiv';
}

function clearFormSearchCriteria( formName ) {
  with ( formName ) {
    for ( var i = 0; i < elements.length; i++ ) {
      if ( elements[i].type == "text" )
        elements[i].value = "";
      else {
        for ( var j = 0; j < elements[i].options.length; j++ ) {
          if ( elements[i].options[j].selected )
            elements[i].options[j].selected = false;
        }
      }
    }
  }
}

function clearSearchCriteria( formName )
{
    with( formName )
    {
        for( var i = 0; i < elements.length; i++ )
        {
            if( elements[i].type == "text" )
            {
                elements[i].value = "";
            }
            else if( elements[i].type == "checkbox" || elements[i].type == "radio" )
            {
                elements[i].checked = false;
            }
            else if( elements[i].type == "select-one" )
            {
                elements[i].options[0].selected = true;
            }
            else if( elements[i].type != "hidden" )
            {
                for( var j = 0; j < elements[i].options.length; j++ )
                {
                    // if ( elements[i].options[j].selected )
                    elements[i].options[j].selected = false;
                }
            }
        }
    }
}

function populateSelectBoxes( stringField, selBox1, selBox2 ) {
  var strng = stringField.value;
  var substrng = null;
  var newLength = 0;
  while ( strng.length > 1 ) {
    if ( strng.indexOf(",") > 0 ) {
      substrng = strng.substring( 0, strng.indexOf(",") );
      strng = strng.substring( strng.indexOf(",") + 1, strng.length );
    }
    else {
      substrng = strng;
      strng = "";
    }
    for ( var i = 0; i < selBox1.length; i++ ) {
      if ( selBox1.options[i].value == substrng ) {
        selBox1.options[i].selected = true;
        newLength++;
      }
    }
  }
  move( selBox1, selBox2 );
  for ( var j = 0; j < selBox1.length - newLength; j++ )
    selBox1.options[j].selected = false;
}

function createCommaDelimitedStringFromSelectBox( box, stringField ) {
  stringField.value = "";
  for ( var i = 0; i < box.length; i++ ){
    stringField.value = stringField.value + box.options[i].value + ",";
  }
  stringField.value = (stringField.value).substring( 0, (stringField.value).length - 1 );
}

function createCommaDelimitedStringFromSelectBoxText( box, stringField ) {
  stringField.value = "";
  for ( var i = 0; i < box.length; i++ ){
    stringField.value = stringField.value + box.options[i].text + ",";
  }
  stringField.value = (stringField.value).substring( 0, (stringField.value).length - 1 );
}

function createNewLineDelimitedStringFromSelectBox( box, stringField ) {
  stringField.value = "";
  for ( var i = 0; i < box.length; i++ ){
    stringField.value = stringField.value + box.options[i].value + "\n";
  }
  stringField.value = (stringField.value).substring( 0, (stringField.value).length - 1 );
}

function createNewLineDelimitedStringFromSelectBoxText( box, stringField ) {
  stringField.value = "";
  for ( var i = 0; i < box.length; i++ ){
    stringField.value = stringField.value + box.options[i].text + "\n";
  }
  stringField.value = (stringField.value).substring( 0, (stringField.value).length - 1 );
}

function populateCheckBoxes( stringField, form ) {
  var strng = stringField.value;
  var substrng = null;
  while ( strng.length > 1 ) {
    if ( strng.indexOf(",") > 0 ) {
      substrng = strng.substring( 0, strng.indexOf(",") );
      strng = strng.substring( strng.indexOf(",") + 1, strng.length );
    }
    else {
      substrng = strng;
      strng = "";
    }
    for ( var i = 0; i < form.elements.length; i++ ) {
      if ( form.elements[i].type == "checkbox" ) {
        if ( form.elements[i].value == substrng ) {
          form.elements[i].checked = true;
        }
      }
    }
  }
}

function createDisplayOrderFromChk( form, displayOrder ) {
  displayOrder.value = "";
  for ( var i = 0; i < form.elements.length; i++ ) {
    if ( form.elements[i].type == "checkbox" ) {
      if ( (form.elements[i].name).indexOf( "chk" ) < 0 &&
            form.elements[i].checked == true ) {
        displayOrder.value = displayOrder.value + form.elements[i].value + ",";
      }
    }
  }
  displayOrder.value = (displayOrder.value).substring( 0, (displayOrder.value).length - 1 );
}

function selectCorrectOption( sel, strng ) {
  if ( sel.options )
  {
    for ( var i = 0; i < sel.options.length; i++ ) {
        if ( sel.options[i].value == strng )
            sel.options[i].selected = true;
    }
  }
}

function selectCorrectRadio( rdo, strng ) {
  for ( var i = 0; i < rdo.length; i++ ) {
    if ( rdo[i].value == strng )
      rdo[i].checked = true;
  }
}

function getSelectedValue( sel ) {
  for ( var i = 0; i < sel.options.length; i++ ) {
    if ( sel.options[i].selected == true )
      return sel.options[i].value;
  }
}

function getSelectedText( sel ) {
  for ( var i = 0; i < sel.options.length; i++ ) {
    if ( sel.options[i].selected == true )
      return sel.options[i].text;
  }
}

function getCheckedValue( rad ) {
  for ( var i = 0; i < rad.length; i++ ) {
    if ( rad[i].checked )
      return rad[i].value;
  }
}

function createDateIn( frm, attID ) {
   var month = getSelectedValue( frm[ "selMonth" + attID ] );
   var date = getSelectedValue( frm[ "selDay" + attID ] );
   var year = frm[ "txxtYear" + attID ].value;
   if ( isDate( year, month, date ) ){
      var dateIn = "";
      dateIn += year;
      dateIn += month;
      dateIn += date;
      if ( frm[ "selHour" + attID ] ) {
        var hour = getSelectedValue( frm[ "selHour" + attID ] );
        if ( hour != "12" && getSelectedValue( frm[ "selAMorPM" + attID ] ) == "PM" ) {
          hour = parseInt( hour );
          hour += 12;
        }
        else if ( hour == "12" && getSelectedValue( frm[ "selAMorPM" + attID ] ) == "AM" ) {
            hour = "00";
        }
        dateIn += hour
        dateIn += getSelectedValue( frm[ "selMin" + attID ] );
      }
      else {
        dateIn += "0000";
      }
      frm[ "txt" + attID ].value = dateIn;
      return true;
   }
   alert("Invalid Date Entered");
   return false;
}

function fillInDate( frm, dateString, attID ) {
  if ( dateString.length == 12 ) {
    frm[ "txxtYear" + attID ].value = dateString.substring( 0, 4 );
    selectCorrectOption( frm[ "selMonth" + attID ], dateString.substring( 4, 6 ) );
    selectCorrectOption( frm[ "selDay" + attID ], dateString.substring( 6, 8 ) );
    if ( frm[ "selHour" + attID ] ) {
      var hour = dateString.substring( 8, 10 );
      if ( parseInt( hour ) > 12 ) {
        hour = parseInt( hour );
        hour -= 12;
        if ( hour < 10 ) {
          hour = "0" + hour;
        }
        else {
          hour = "" + hour;
        }
      }
      else if ( parseInt( hour ) == 0 ) {
        hour = "12";
      }
      selectCorrectOption( frm[ "selHour" + attID ], hour );
      selectCorrectOption( frm[ "selMin" + attID ], dateString.substring( 10, 12 ) );
      if ( parseInt( dateString.substring( 8, 10 ) ) < 12 )
        selectCorrectOption( frm[ "selAMorPM" + attID ], "AM" );
      else
        selectCorrectOption( frm[ "selAMorPM" + attID ], "PM" );
    }
  }
}

function aValueIsSelected( sel ) {
  for ( var i = 0; i < sel.length; i++ ) {
    if ( sel[i].selected ) {
      return true;
    }
  }
  return false;
}

function numericSort( ary ) {
  newAry = new Array();
  for( var l = 0; l < ary.length; l++ )
    newAry[l] = null;
  for( var i = 0; i < ary.length; i++ ) {
    j = 0;
    var num = ary[i];
    for ( var k = 0; k < ary.length; k++ ) {
      if ( parseInt( num ) > parseInt( ary[k] ) )
        j++;
    }
    var done = false;
    while ( !done ) {
      if ( newAry[j] == null ) {
        newAry[j] = num;
        done = true;
      }
      else {
        j++;
      }
    }
  }
  return newAry;
}

function stringInList( s, sel, isEdit ) {
  for ( var i = 0; i < sel.options.length; i++ ) {
//    alert( "String: " + s + "\nSelection: " + sel.options[i].value + "\nEquality: " + s == sel.options[i].value );
    if ( s == sel.options[i].value ) {
      if ( isEdit && sel.options[i].selected )
        continue;
      return true;
    }
  }
  return false;
}

function determineEnvironment() {
  var browser = navigator.appName.toLowerCase();
  var version = navigator.appVersion;
  var agent = navigator.userAgent.toLowerCase();
  if ( version.indexOf ( "MSIE" ) >= 0 ) {
    version = version.substring( version.indexOf( "MSIE" ) + 5, version.indexOf( "MSIE" ) + 9 );
    if ( version.indexOf( "5.5" ) >= 0 )
      version = 5.5;
    else
      version = parseInt( version );
  }
  else
    version = parseInt( version );
  if ( agent.indexOf( "mac" ) >= 0 ) {
    if ( browser == "netscape" ) {
      if ( version >= 4 && version < 5 )
        return "macNS4";
      if ( version >= 5 )
        return "macNS6";
      else
        return "macNS3";
    }
    else if ( browser == "microsoft internet explorer" ) {
      if ( agent.indexOf( "msie 5.1" ) >= 0 )
        return "macIE6";
      if ( version >= 4 && version < 5 )
        return "macIE4";
      if ( version >= 5 && version < 5.5 )
        return "macIE5";
      else
        return "macNS3";
    }
  }
  else {
    if ( browser == "netscape" ) {
      if ( version >= 4 && version < 5 )
        return "winNS4";
      if ( version >= 5 )
        return "winNS6";
      else
        return "winNS3";
    }
    else if ( browser == "microsoft internet explorer" ) {
      if ( version >= 4 && version < 5 )
        return "winIE4";
      if ( version >= 5 && version < 5.5 )
        return "winIE5";
      if ( version >= 5.5 )
        return "winIE6";
      else
        return "winNS3";
    }
  }
  return "macNS6";
}

function startDateBeforeEndDate( startM, startD, startY, endM, endD, endY ) {
  var start = startY + startM + startD;
  var end = endY + endM + endD;
  if ( parseInt ( start ) > parseInt ( end ) ) {
    return false;
  }
  return true;
}

function countCharacter( str, chr ) {
  var count = 0;
  for ( var i = 0; i < str.length; i++ ) {
    if ( str.charAt( i ) == chr )
      count++;
  }
  return count
}

function popup( uri, target, height, width ) {
      window.open(uri, target, "height=" + height + ",width=" + width + ",menubar=0,location=0,personalbar=0,toolbar=0,titlebar=0,status=0");
}

function setStatus(txt) {
    self.status=txt;
}

function resetStatus() {
    self.status="";
}

function popupImage( image )
{
    window.open( "/mc/common/viewImage.jsp?imageFile=" + image, "ViewImage",
                 "height=400,width=400,scrollbars=1,menubar=0,location=0,personalbar=0,toolbar=0,titlebar=0,status=0,resizable=1");
}

function removeBlankOption(sel)
{
    if ( sel )
    {
        sel.options[0] = null;
    }
}

function submitToPopup( form, target, options, action )
{
    window.open( "", target, options);
    oldTarget = form.target;
    oldAction = form.action;
    form.target = target;
    form.action = action;
    form.submit();
    form.action = oldAction;
    form.target = oldTarget;
}

function setActionTaskValues(form,action,task){
  form.sfwAction.value = action;
  form.sfwTask.value = task;
  form.submit();
}

// This function sets the struts action on the form and submits it
function submitActionForm( form, action )
{
    form.action = action;
    form.submit();
}

// This function sets the struts action on the form and submits it via the onsubmit method
function onsubmitActionForm( form, action )
{
    form.action = action;
    form.onsubmit();
}

// Move Functions
function move(fbox,tbox) {
for(var i=0; i<fbox.options.length; i++) {
if(fbox.options[i].selected && fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
   }
}
BumpUp(fbox);
}

function moveAll(fbox,tbox) {
  for (var i = 0; i < fbox.options.length; i) {
    fbox.options[i].selected = true;
    move(fbox,tbox)
  }
}

function BumpUp(box)  {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "")  {
for(var j=i; j<box.options.length-1; j++)  {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
   }
}

if(ln < box.options.length)  {
box.options.length -= 1;
BumpUp(box);
   }
}


function MoveUp(combo)
{
    i=combo.selectedIndex;
    if (i>0)
    {
        swap(combo,i,i-1);
        combo.options[i-1].selected=true;
        combo.options[i].selected=false;
    }
}

function MoveDown(combo)
{
    i=combo.selectedIndex;

    if (i<combo.length-1 && i>-1)
    {
        swap(combo,i+1,i);
        combo.options[i+1].selected=true;
        combo.options[i].selected=false;
    }
}

//this function is used to swap between elements
function swap(combo,index1, index2)
{
    var savedValue=combo.options[index1].value;
    var savedText=combo.options[index1].text;

    combo.options[index1].value=combo.options[index2].value;
    combo.options[index1].text=combo.options[index2].text;

    combo.options[index2].value=savedValue;
    combo.options[index2].text=savedText;
}

// Mark all elements with the given name as checked or not based on the main check box
function checkAll( form, checkAllElement, basisElement )
{
    with ( form )
    {
        var assignElem = elements[ basisElement ];

        if ( assignElem.checked == true )
        {
            for ( var i = 0; i < elements.length; i++ )
            {
                if ( elements[i].name == checkAllElement )
                {
                    elements[ i ].checked = true;
                }
            }
        }
        else
        {
            for ( var i = 0; i < elements.length; i++ )
            {
                if ( elements[i].name == checkAllElement )
                {
                    elements[ i ].checked = false;
                }
            }
        }
    }
}

function showGroupNames( attNameString )
{
    groupString = document.getElementById( 'groupNameArea' );
    groupString.innerHTML = attGroupArray[attNameString];
}

function changeColor( tableRow, highLight )
{
    if( highLight )
    {
        tableRow.style.backgroundColor = '#FFFFCF';
        document.body.style.cursor = 'hand';
    }
    else
    {
        tableRow.style.backgroundColor = 'white';
        document.body.style.cursor = 'default';
    }
}

function changeColorShade( tableRow, highLight )
{
    if( highLight )
    {
        tableRow.style.backgroundColor = '#FFFFCF';
        document.body.style.cursor = 'hand';
    }
    else
    {
        tableRow.style.backgroundColor = '#EEEEEE';
        document.body.style.cursor = 'default';
    }
}

function printAdminContent()
{
    var content = document.getElementById('adminContainer');
    if (content != null)
    {
        var win = window.open("", "win", "width=300,height=200,resizable=1"); // a window object
        with (win.document) {
            open("text/html", "replace");
            write("<HTML><HEAD><TITLE>Print Friendly</TITLE></HEAD><BODY onload='window.print();'>" + content.innerHTML + "</BODY></HTML>");
            close();
        }
    }
}

function addLoadEvent( func )
{
    var oldonload = window.onload;
    if( typeof window.onload != 'function' )
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            if( oldonload )
            {
                oldonload();
            }
            func();
        }
    }
}
