
/* Translatable messages. */
var ERROR_REQ = "Please fill out the following required fields: ";


/* Main function called when page is done loading. */
function LoadComplete()
{
	ClearRequired();
}

function ClearRequired()
{
  var Document = GetDocument();
  var FormElements = Document.ActionForm.elements;
  for(Index = 0; Index < FormElements.length; Index++)
  {
  	if (FormElements[Index].type == 'select')
  	{
  		
  	}
  	else
  	{
  		if(FormElements[Index].value != '')
  		{
  			ClassName = FormElements[Index].className;
  			FormElements[Index].className = ClassName.replace('req','reqoff');
  		}
  	}
  }
}

function OnChangeHandler(Object)
{
	if (Object.type == 'select')
	{
		
	}
	else
	{
		if (Object.value == '')
		{
			if (Object.className.indexOf('reqoff') != -1)
			{
				Object.className = Object.className.replace('reqoff','req');
			}
		}
		else
		{
			if (Object.className.indexOf('req') != -1)
			{
				Object.className = Object.className.replace('req','reqoff');
			}
		}
	}
}

/* Wrapper function to access the document in case it is located in different places. */
function GetDocument()
{
  return document;
}

/* Gets the current Date/Time as reported by the Client machine!
   and returns string in this format "YYYY/MM/DD HH:MM:SS.MMM"
 */
function GetClientTime()
{
  var Today = new Date();
  var DateString = "";
  DateString = Today.getFullYear();
  Today.getMonth() + 1 < 10 ? DateString += "/0" : DateString += "/";
  DateString += Today.getMonth() + 1;
  Today.getDate() < 10 ? DateString += "/0" : DateString += "/";
  DateString += Today.getDate();
  Today.getHours() < 10 ? DateString += " 0" : DateString += " ";
  DateString += Today.getHours();
  Today.getMinutes() < 10 ? DateString += ":0" : DateString += ":";
  DateString += Today.getMinutes();
  Today.getSeconds() < 10 ? DateString += ":0" : DateString += ":";
  DateString += Today.getSeconds();
  var MilliSeconds = Today.getMilliseconds();
  if (MilliSeconds < 10)
  {
    DateString += ".00";
  }
  else if (MilliSeconds < 100)
  {
    DateString += ".0";
  }
  else
  {
    DateString += ".";
  }
  DateString += MilliSeconds;
  return DateString;
}

/* Name: Name of the action we are performing.
   Method: Form submission method.
   Remaining parameters are parameters to the action. These parameters come in 
    pairs where the firs parameter of the pair is the name of the parameter and the
    second the value of the parameter.
 */
function SubmitAction(Name, Method, CheckRequiredFlds)
{
  var Document = GetDocument();
  var TheForm = Document.ActionForm;
  TheForm.ActionName.value = Name;
  TheForm.method = Method;
  if (TheForm.TimeStamp)
  {
  	//TheForm.TimeStamp.value = GetClientTime();
  }
  /* Loop through the remaining parameters to set all name/value pairs. */
  for (var Index = 3; Index < arguments.length; Index = Index + 2)
  {
    var Element = document.getElementById(arguments[Index]);
    if (Element)
    {
      if (Index + 1 < arguments.length)
      {
        Element.value = arguments[Index + 1];
      }
    }
  }
  if (CheckRequiredFlds == 'false' || !CheckRequired())
  {
 	  Document.ActionForm.submit();
  }
}

/* Returns false if not all required fields are filled out, true otherwise. 
   Todo: 1. Create "<div id="msgv"></div>"
         2. Id the labels.
 */
function CheckRequired()
{
  var bFound = false;
  var TheForm = document.ActionForm;
  var FormElements= TheForm.elements;
  var Document = GetDocument();
  var Msgv = Document.getElementById('msgv'); /* Returns table element. */
  if (Msgv && Msgv.innerHTML != "")
  {
  	Msgv.innerHTML = "";
  }
  var Html = "<table>";
  
  for (var Index = 0; Index < FormElements.length; Index++)
  {
    var Obj = FormElements[Index];
    if (Obj.className.indexOf('reqoff') == -1 && Obj.className.indexOf('req') != -1) 
    {
      if (bFound == false)
      {
      	Html += "<tr><td class='errh'>Errors</td></tr>";
      	Html += "<tr><td nowrap class='errtxt'>" + ERROR_REQ;
        bFound = true;
      }
      /* Finish date required field.
      if (Obj.name.indexOf('D_') != -1 || Obj.name.indexOf('Y_') != -1)
      {
      	Name = Obj.substring(2);
      }
      */
      var Label = Document.getElementById(Obj.name + '_lbl');
      if (Label)
      {
      	Html += Label.innerHTML + ", "; 
      }
    } 
  }
  if (Msgv && bFound)
  {
  	Html += "</td></tr></table>";
  	Msgv.innerHTML = Html;
  }
  return bFound;
}

/* Returns the HTML Element where the event originated. */
function GetEventSourceElement(e)
{
	if (!e)
	{
	    return null;
	}
    if (navigator.appName == 'Netscape')
	{
		Src = e.explicitOriginalTarget;
	}
	else
	{
		Src = e.srcElement;
	}
	return Src;
}

