var isIE = document.all != null ? true : false;

function setFormChanged(e,changed)
{
   e = (e) ? e : ((window.event) ? window.event : null);

   if (e)
   {
      if (isIE)
      {
         if (e.srcElement.form.modified)
            e.srcElement.form.modified.value = true;
         else if (document.getElementById(e.srcElement.form.id + ':modified'))
            document.getElementById(e.srcElement.form.id + ':modified').value = "true";
      }
      else
      {
         if (e.target.form.modified)
            e.target.form.modified.value = true;
         else if (document.getElementById(e.target.form.id + ':modified'))
            document.getElementById(e.target.form.id + ':modified').value = "true";
      }
   }

}

function textFieldChanged(e)
{
  setFormChanged(e,true);
}

function textKeyPressed(e)
{
   setFormChanged(e,true);

   return true;
}

function integerKeyPressed(e)
{
   setFormChanged(e,true);

   e = (e) ? e : ((window.event) ? window.event : null);

   if (e)
   {
      var charCode = (e.charCode || e.charCode == 0) ? e.charCode : ((e.keyCode) ? e.keyCode : e.which);

      if (e.altKey || e.ctrlKey || charCode == 0 || charCode == 32)
         return true;

      if (charCode == 45)
         return true;

      if (charCode >= 48 && charCode <= 57)
         return true;
   }

   return false;
}

function floatKeyPressed(e)
{
   setFormChanged(e,true);

   e = (e) ? e : ((window.event) ? window.event : null);

   if (e)
   {
      var charCode = (e.charCode || e.charCode == 0) ? e.charCode : ((e.keyCode) ? e.keyCode : e.which);

      if (e.altKey || e.ctrlKey || charCode == 0 || charCode == 32)
         return true;

      if (charCode == 45 || charCode == 46)
         return true;

      if (charCode >= 48 && charCode <= 57)
         return true;
   }

   return false;
}

function dateKeyPressed(e)
{
   setFormChanged(e,true);

   e = (e) ? e : ((window.event) ? window.event : null);

   if (e)
   {
      var charCode = (e.charCode || e.charCode == 0) ? e.charCode : ((e.keyCode) ? e.keyCode : e.which);

      if (e.altKey || e.ctrlKey || charCode == 0 || charCode == 32)
         return true;

      if (charCode == 47)
         return true;

      if (charCode >= 48 && charCode <= 57)
         return true;
   }

   return false;
}

function timeKeyPressed(e)
{
   setFormChanged(e,true);

   e = (e) ? e : ((window.event) ? window.event : null);

   if (e)
   {
      var charCode = (e.charCode || e.charCode == 0) ? e.charCode : ((e.keyCode) ? e.keyCode : e.which);

      if (e.altKey || e.ctrlKey || charCode == 0 || charCode == 32)
         return true;

      if (charCode == 58)
         return true;

      if (charCode >= 48 && charCode <= 57)
         return true;
   }

   return false;
}

function closeButtonClicked(promptToClose,reloadOpener)
{
   if (reloadOpener && opener != null)
      opener.location.reload();

   if (this.parent != null)
      this.parent.close();
   else
      this.close();
}

function closeButtonClickedWithAction(promptToClose,control,action)
{
   if (promptToClose && control.form && control.form.modified && control.form.modified.value == "true")
   {
      if (confirm("The data has been changed. Do you want to save the changes?"))
         submitForm(control,action);
      else
         return;
   }
   else
   {
      submitForm(control,action);
   }
}

function closeButtonClickedWithActions(promptToClose,control,closeAction,saveAction)
{
   if (promptToClose)
   {
      var modified = false;

      if (control.form.modified)
         modified = control.form.modified.value == "true";

      if (modified && confirm("The data has been changed. Do you want to save the changes?"))
         submitForm(control,saveAction);
      else
         submitForm(control,closeAction);
   }
   else
   {
      submitForm(control,closeAction);
   }
}

function closeClicked(control)
{
   var modified = false;

   if (document.getElementById(control.form.id + ':modified'))
      modified = document.getElementById(control.form.id + ':modified').value == "true";

   if (modified && confirm("The data has been changed. Do you want to save the changes?"))
   {
      var idaction = control.form[control.form.id + ':_idaction'];

      idaction.value = control.form.id + ':saveAndCloseAction';

      control.form.submit();

      return false;
   }

   return true;
}

function buttonClickedWithActions(promptToClose,control,action,saveAction)
{
   if (promptToClose && control.form && control.form.modified && control.form.modified.value == "true")
   {
      if (confirm("The data has been changed. Do you want to save the changes?"))
      {
         submitForm(control,saveAction);
      }
      else
      {
         control.form.modified.value = "false";
         submitForm(control,action);
      }

   }
   else
   {
      submitForm(control,action);
   }
}

function closeButtonClickedWithHREF(promptToClose,href)
{
   location.href = href;
}

function cancelButtonClicked()
{
   this.close();
}

function buttonMouseOver(button)
{
   button.className = "rollover";
}

function buttonMouseOut(button)
{
   button.className = "normal";
}

function submitForm(control,action)
{
   var form = control.form;

   if (form != null)
   {
      form.action = action;
      form.submit();
   }
   else
   {
      document.location = action;
   }
}

function submitOtherForm(form,action)
{
   if (form != null)
   {
      if (action != null)
         form.action = action;

      form.submit();
   }
}

function popUpSelected(dateField)
{
}

function moveDateBackward(dateField,scrollBy)
{
   var millisPerHour = 3600000;
   var millisPerDay  = 86400000;
   var scrollMillis  = scrollBy * 3600000 * 24;
   var oldDate       = new Date(dateField.value);
   var newDate       = new Date(oldDate.getTime() - scrollMillis);

   newDate = new Date(newDate.getFullYear(),newDate.getMonth(),newDate.getDate());

   var diffMillis = oldDate.getTime() - newDate.getTime();

   if (diffMillis > scrollMillis && diffMillis - scrollMillis >= millisPerHour * 23)
   {
      newDate = new Date(newDate.getFullYear(),newDate.getMonth(),newDate.getDate() + 1);
   }

   dateField.value = (newDate.getMonth() + 1) + '/' +  newDate.getDate() + '/' + newDate.getFullYear();
}

function moveDateForward(dateField,scrollBy)
{
   var millisPerHour = 3600000;
   var millisPerDay  = 86400000;
   var scrollMillis  = scrollBy * 3600000 * 24;
   var oldDate       = new Date(dateField.value);
   var newDate       = new Date(oldDate.getTime() + scrollMillis);

   newDate = new Date(newDate.getFullYear(),newDate.getMonth(),newDate.getDate());

   var diffMillis = newDate.getTime() - oldDate.getTime();

   if (diffMillis < scrollMillis && scrollMillis - diffMillis >= millisPerHour * 23)
   {
      newDate = new Date(newDate.getFullYear(),newDate.getMonth(),newDate.getDate() + 1);
   }

   dateField.value = (newDate.getMonth() + 1) + '/' +  newDate.getDate() + '/' + newDate.getFullYear();
}

function moveDatesBackward(startDateField,endDateField)
{
   var millisPerHour   = 3600000;
   var millisPerDay    = 86400000;
   var startDateMillis = new Date(startDateField.value).getTime();
   var endDateMillis   = new Date(endDateField.value).getTime();

   if (endDateMillis - startDateMillis >= 0)
   {
      var diffMillis = endDateMillis - startDateMillis;

      var scrollBy = Math.floor(diffMillis / millisPerDay) + 1;

      if (diffMillis % millisPerDay > millisPerHour)
         scrollBy++;

      moveDateBackward(startDateField,scrollBy);
      moveDateBackward(endDateField,scrollBy);
   }
   else
   {
      alert("The End Date must be after the Start Date");
   }
}

function moveDatesForward(startDateField,endDateField)
{
   var startDate = new Date(startDateField.value);
   var endDate   = new Date(endDateField.value);

   var millisPerHour   = 3600000;
   var millisPerDay    = 86400000;
   var startDateMillis = startDate.getTime();
   var endDateMillis   = endDate.getTime();

   if (endDateMillis - startDateMillis >= 0)
   {
      var diffMillis = endDateMillis - startDateMillis;

      var scrollBy = Math.floor(diffMillis / millisPerDay) + 1;

      if (diffMillis % millisPerDay > millisPerHour)
         scrollBy++;

      moveDateForward(startDateField,scrollBy);
      moveDateForward(endDateField,scrollBy);
   }
   else
   {
      alert("The End Date must be after the Start Date");
   }
}

function backwardSelected(dateField,scrollBy)
{
   alert("backwardSelected is deprecated");

   moveDateBackward(dateField,scrollBy);
}

function forwardSelected(dateField,scrollBy)
{
   alert("forwardSelected is deprecated");

   moveDateForward(dateField,scrollBy);
}

function linkClicked(windowName,href,popupOptions)
{
   window.open(href,windowName,popupOptions);
}

function createPopupWindow(name,href,width,height)
{
   if (window.showModelessDialog)
   {
      return createPopupWindowMozilla(name,href,width,height);
   }
   else
   {
      return createPopupWindowMozilla(name,href,width,height);
   }
}

function createPopupWindowIE(name,href,width,height)
{
   var options = "";

   options += "dialogWidth:";
   options += width;
   options += "px";
   options += ";dialogHeight:";
   options += height;
   options += "px";
   options += ";status:yes";

   return window.showModelessDialog(href,null,options);
}

function createPopupWindowMozilla(name,href,width,height)
{
   var left = (screen.width - width) / 2;
   var top  = (screen.height - height) / 2;

   var options = "dependent,menubar=no,toolbar=no,resizable=yes,status=yes,scrollbars=yes";

   options += ",left=";
   options += left;
   options += ",top=";
   options += top;
   options += ",width=";
   options += width;
   options += ",height=";
   options += height;

   var tempName = '';

   for (var i = 0; i < name.length; i++)
   {
      if (name.charAt(i) == ' ' || name.charAt(i) == '&')
         tempName += '_';
      else
         tempName += name.charAt(i);
   }

   name = tempName;

   var win = window.open(href,name,options);

   if (win != null)
      win.focus();
}

function toggleBar(panelName,imageName,expandedImage,collapsedImage)
{
   var table      = document.getElementById(panelName);
   var image      = document.getElementById(imageName);
   var contentRow = table.rows[1];

   if (contentRow.style.display != 'none')
   {
      contentRow.style.display = 'none';

      image.src = collapsedImage;
   }
   else
   {
      if (navigator.userAgent.indexOf("MSIE") != -1)
         contentRow.style.display = 'inline';
      else
         contentRow.style.display = 'table-row';

      image.src = expandedImage;
   }

   return false;
}

function checkBoxClicked(e)
{
   setFormChanged(e,true);
}

function comboBoxChanged(e, calcUrl)
{
   e = (e) ? e : ((window.event) ? window.event : null);

   setFormChanged(e,true);

   if (calcUrl)
   {
      if (isIE)
         submitForm(e.srcElement,calcUrl);
      else
         submitForm(e.target,calcUrl);
   }
}

function tableMouseOver(obj)
{
   if (obj.className == 'odd')
      obj.className = 'odds';
   else if (obj.className == 'even')
      obj.className = 'evens';
}

function tableMouseOut(obj)
{
   if (obj.className == 'odds')
      obj.className = 'odd';
   else if (obj.className == 'evens')
      obj.className = 'even';
}
