/**
  <summary>Validates and then posts the Process form.</summary>
  <param name="asButton" type="string">The button (button1, button2, button3, button4) that the user pressed.</param>
  <returns></returns>
*/
function ProcessPageSubmit( asButton )
{

  //1.  Validate the form.

  for ( var i=0; i<document.frmProcessPage.elements.length; i++ )
  {
    var lsId = document.frmProcessPage.elements[i].id;

    if ( lsId != "" )
    {
      if ( ! ValidateField(document.frmProcessPage.elements[i]) )
        return;
    }
  }

  //2.  Tell the backend what button was pressed.

  document.frmProcessPage.htxtButton.value=asButton;

  //3.  Post the form.

  document.frmProcessPage.submit();
}


/**
  <summary>Checks to see if the form element passed is a valid date.</summary>
  <param name="elem" type="element">The form element to validate.</param>
  <returns></returns>
*/
function CheckDate( elem )
{
  if ( ! isDate( elem.value ) )
    elem.focus();
}


/**
  <summary>Checks to see if the form element passed is a valid integer.</summary>
  <param name="elem" type="element">The form element to validate.</param>
  <returns></returns>
*/
function CheckInteger( elem )
{
    if ( ! isInteger( elem.value ) )
      elem.focus();
}


/**
  <summary>Closes the process window.</summary>
  <returns></returns>
*/
function CloseProcessWindow()
{
  window.opener=self;
  window.close();
}


/**
  <summary>Pops open a window and launches a COSMO process.</summary>
  <param name="asProcessId" type="integer">The ProcessId of the process to open.</param>
  <param name="asCriteria" type="string">Any criteria we wish to pass to the process.</param>
  <param name="asWidth" type="string">The width of the popped window.</param>
  <param name="asHeight" type="string">The height of the popped window.</param>
  <param name="asOther " type="string">Any other poperties of the window.</param>
  <returns></returns>
*/
function PopProcess( asProcessId, asCriteria, asWidth, asHeight, asOther )
{
  if ( asCriteria != "" )
    asCriteria = "&" + asCriteria;

  lsProcessUrl = gsBaseUrl+ "/cosmo.aspx?mSessionId=" + gsSessionId + "&mRequest=Process.Begin&ProcessId=" + asProcessId + asCriteria;
  wind = window.open( lsProcessUrl, "", "width=" + asWidth + ",height=" + asHeight + " " + asOther ); 
  wind.focus();

}

function OpenProcess( asProcessId, asCriteria, asOnClose )
{
  if ( asCriteria != "" )
    asCriteria = "&" + asCriteria;

  if ( asOnClose != "" )
    asOnClose = "&OnClose=" + asOnClose ;

  lsProcessUrl = gsBaseUrl+ "/cosmo.aspx?mSessionId=" + gsSessionId + "&mRequest=Process.Begin&ProcessId=" + asProcessId + asCriteria + asOnClose;
  this.document.location = lsProcessUrl;

}


/**
  <summary>Pops open a window and loads the passed URL.</summary>
  <param name="asUrl" type="string">The URL to open in the window.</param>
  <param name="asWidth" type="string">The width of the popped window.</param>
  <param name="asHeight" type="string">The height of the popped window.</param>
  <param name="asOther " type="string">Any other poperties of the window.</param>
  <returns></returns>
*/
function OpenWindow( asUrl, asWidth, asHeight, asOther )
{

  lsProcessUrl = gsBaseUrl+ asUrl;
  wind = window.open( lsProcessUrl, "", "width=" + asWidth + ",height=" + asHeight + " " + asOther ); 
  wind.focus();

}


function OpenUrl( asUrl )
{
  this.document.location = asUrl;
}



var gsEditorFieldName = "";
var gsEditorHTML ="";


function HTMLEditor ( asFieldName )
{


  //1.  Save the name of the field we are editing.

  gsEditorFieldName = asFieldName;


  //2.  Get the content of the field we wish to edit and put it into gsEditorHTML (the editor will grab it from this variable).

  for ( var i=0; i<document.frmProcessPage.elements.length; i++ )
  {
    if ( document.frmProcessPage.elements[i].name == gsEditorFieldName )
    {
      gsEditorHTML = document.frmProcessPage.elements[i].value;
      break;
    }
  }


  //3.  Open the editor.

  wind = window.open( gsBaseUrl + "/tools/editor/editor.html", "", "width=800,height=600,scrollbars,resizable,status" ); 
  wind.focus();

}


function EditHtmlCallback( asHtml )
{

  //1.  Save the HTML back to the field that we were editing.

  for ( var i=0; i<document.frmProcessPage.elements.length; i++ )
  {
    if ( document.frmProcessPage.elements[i].name == gsEditorFieldName )
    {
      document.frmProcessPage.elements[i].value = asHtml;
      break;
    }
  }

}




function GetFieldValue( asFieldName )
{
  for ( var i=0; i<document.frmProcessPage.elements.length; i++ )
  {
    if ( document.frmProcessPage.elements[i].name == asFieldName )
      return document.frmProcessPage.elements[i].value;
  }
  return "";
}


function SetFieldValue( asFieldName, asFieldValue )
{
  for ( var i=0; i<document.frmProcessPage.elements.length; i++ )
  {
    if ( document.frmProcessPage.elements[i].name == asFieldName)
    {
      document.frmProcessPage.elements[i].value = asFieldValue ;
      break;
    }
  }
}


/**
  <summary>Pops open a window and loads the passed URL.</summary>
  <param name="asUrl" type="string">The URL to open in the window.</param>
  <param name="asWidth" type="string">The width of the popped window.</param>
  <param name="asHeight" type="string">The height of the popped window.</param>
  <param name="asOther " type="string">Any other poperties of the window.</param>
  <returns></returns>
*/
function OpenWindow( asUrl, asWidth, asHeight, asOther )
{

  lsProcessUrl = gsBaseUrl+ asUrl;
  wind = window.open( lsProcessUrl, "", "width=" + asWidth + ",height=" + asHeight + " " + asOther ); 
  wind.focus();

}



/**
  <summary>Pops a view open in a new window.</summary>
  <param name="asViewId" type="integer">The ID of the view to open.</param>
  <param name="asCriteria" type="string">The criteria to apply to the view.</param>
  <param name="asWidth" type="integer">The width of the view window.</param>
  <param name="asHeight" type="integer">The height of the view window.</param>
  <param name="asWindowProperties" type="string">Other properties of the view window.</param>
  <param name="asOnClose" type="string">The JavaScript to run when the view window closes.</param>
  <param name="asRefreshViewIndex" type="string">The Index of the currently selected master view.</param>
  <returns></returns>
*/
function PopView( asViewId, asCriteria, asWidth, asHeight, asWindowProperties, asOnClose, asRefreshViewIndex )
{

  if ( asCriteria != "" )
    asCriteria = "&" + asCriteria;

  var lsUrl = gsBaseUrl+ "/cosmo.aspx?mSessionId=" + gsSessionId + "&mRequest=view.open&ViewId=" + asViewId + "&OnClose=" + asOnClose + "&RefreshViewIndex=" + asRefreshViewIndex + asCriteria;
  wind = window.open( lsUrl, "", "width=" + asWidth + ",height=" + asHeight + " " + asWindowProperties ); 
  wind.focus();
}

/**
  <summary>Opens a View in the current window.</summary>
  <param name="asViewId" type="integer">The ID of the view to open.</param>
  <param name="asCriteria" type="string">The criteria to apply to the view.</param>
  <returns></returns>
*/
function OpenView( asViewId, asCriteria )
{

  var lsViewId = "";
  var lsCriteria = "";


  if ( asViewId != "" )
    lsViewId = "ViewId=" + asViewId;

  if ( asCriteria != "" )
  {
    var lsaCriteria = asCriteria.split( "|" );

    for (var i=0; i<lsaCriteria.length; i++ )
    {
      if ( lsCriteria != "" )
        lsCriteria += "&";

      lsCriteria += lsaCriteria[i];
    }

    if ( asViewId != "" )
      lsCriteria = "&" + lsCriteria;

/*
*/
  }


  var lsOpenUrl = gsBaseUrl + "/cosmo.aspx?mSessionId=" + gsSessionId + "&mRequest=view.open&" + lsViewId + lsCriteria;

//TOOK THIS OUT FOR NOW - NEED TO FIGURE OUT HOW TO SEND THIS ALONG

//  if ( gsOnClose != "" )
//    lsOpenUrl += "&onClose=" + gsOnClose;

//END NEW


  this.location.href = lsOpenUrl;

}



/**
  <summary>Pops a URL open in a new window.</summary>
  <param name="asUrl" type="string">The URL to pop open.</param>
  <param name="asQueryString" type="string">The QueryString to pass to the URL.</param>
  <param name="asWidth" type="integer">The width of the new window.</param>
  <param name="asHeight" type="integer">The height of the new window.</param>
  <param name="asOther" type="string">Other properties of the new window.</param>
  <returns></returns>
*/
function GetPrintFile( asProcessSessionId )
{


  var lsUrl = gsBaseUrl + "/cosmo.aspx?mSessionId=" + gsSessionId + "&mRequest=Process.GetPrintFile&ProcessSessionId=" + asProcessSessionId;
  this.location.href = lsUrl;

}