//Version: 1.00, 11/04/2005
//Version: 1.01, 13/04/2005 Corrected view bug
//Version: 1.02, 11/07/2005 Netscape implementation
//Version: 1.03, 19/10/2005 View independence
//Version: 1.03, 15/02/2006 Direct link
//Version: 1.05, 05/07/2006 Confirmaton
//Version: 1.06, 20/02/2007 Class & Redirect
//Version: 1.07, 25/07/2007: Aggiunta funzione function GetElementsName
//Version: 1.08, 29/04/2009: Cleanup: removed object and improved redirection
//Version: 1.09, 01/09/2010: Aggiunta parametro strCurrentLocation

var Altamira_Web_UI_Button_ScriptVersion = 109;
var AltamiraWebUIButton = "AltamiraWebUIButton";
var glViewState = null;

function OnButtonClick(
  strValue,
  strName,
  strConfirmationText,
  strURL,
  strTarget,
  strNewModeLoad,
  strStartupViewID,
  strParentForm,
  strRunDefaultAction,
  strType,
  strCurrentLocation) {

  //Unescape the URL
  strURL = unescape(strURL);
    
  //eventuale decodifica dell'utf8
  strURL = Utf8.decode(strURL);
  
  //Do we navigate?
  if (strType == 'Navigate') {
    //Create the form
    var objForm = document.createElement("FORM");
    objForm.action = strURL;
    objForm.target = strTarget;
    //Do we have a query string?
    if (strURL.indexOf("?") > -1) {
      //Create the fields
      var strQueryString = strURL.substring(strURL.indexOf("?") + 1);
      var arParameters = strQueryString.split("&");
      for (var i = 0; i < arParameters.length; i++) {
        //Split the value form the parameter
        var arPair = arParameters[i].split("=");
        CreateField(objForm,
          arPair[0],
          arPair[1].replace("+", " "))
      }
    }
    document.body.appendChild(objForm);
    objForm.submit();
    document.body.removeChild(objForm);

    return;
  }

  //Confirm post action?
  if (strConfirmationText != "") {
    if (!confirm(strConfirmationText))
      return;
  }

  var objHidden = document.getElementById(AltamiraWebUIButton);
  var objForm = document.forms[0];

  objHidden.name = strName;
  objHidden.value = strValue;

  CreateField(objForm,
          "StartupViewID",
          strStartupViewID)
  CreateField(objForm,
          "ParentForm",
          strParentForm)
  CreateField(objForm,
          "RunDefaultAction",
          strRunDefaultAction)
  CreateField(objForm,
          "RedirectURL",
          strURL)
  CreateField(objForm,
          "NewModeLoad",
          strNewModeLoad)

  //Do we redirect this post ot a new form?
  if (strURL != '') {
    //Get the viewstate so we can clean it
    ClearViewState("__VIEWSTATE");
    document.forms[0].action = strURL;
  }
  else {
    if (strCurrentLocation != null && strCurrentLocation != '')
      var strLocation = strCurrentLocation;
    else
      var strLocation = document.URL;
    var strQueryString = "";
    if (strLocation.toString().indexOf("?") > 0) {
      //Get the query string
      strQueryString = strLocation.toString().substring(strLocation.toString().indexOf("?"))
      //Get the URL
      strLocation = strLocation.toString().substring(0, strLocation.toString().indexOf("?"))
    }
    document.forms[0].action = strLocation;
  }
  //Set the form target
  if (strTarget != '')
    document.forms[0].target = strTarget;
  else
    document.forms[0].target = '';
  //Set the submit type
  if (strType == 'PostToPage')
    document.forms[0].method = 'post';
  else if (strType == 'GetToPage')
    document.forms[0].method = 'get';

	//Fire the necessary events
  FireEventHandlers(document.forms[0], "submit");
	//Submit the form
  document.forms[0].submit();

  DeleteFields();
  //Reinstate the viewstate if necessary
  if (glViewState) {
    objForm.appendChild(glViewState);
    glViewState = null;
  }
}

function ClearViewState(strViewSTate) {
  var objViewState = document.getElementById(strViewSTate);
  if (objViewState != null) {
    glViewState = objViewState;
    objViewState.parentNode.removeChild(objViewState);
  }
}

function DeleteFields() {
  DeleteField("StartupViewID")
  DeleteField("ParentForm")
  DeleteField("RunDefaultAction")
  DeleteField("RedirectURL")
  DeleteField("NewModeLoad")

}

function DeleteField(strName) {
  //Eliminiamo l'eventuale vecchio campo
  var objOldItem = document.getElementById(AltamiraWebUIButton + strName);
  if (objOldItem == null)
    return;
  objOldItem.parentNode.removeChild(objOldItem);
}

function CreateField(objForm, strName, strValue) {
  if (strValue == null || strValue == '')
    return null;
  //Delete the old field if present
  DeleteField();

  var objHidden = null;
  objHidden = document.createElement("INPUT");
  objHidden.type = 'hidden';
  objHidden.name = strName;
  objHidden.id = AltamiraWebUIButton + strName;
  objHidden.value = strValue;
  objForm.appendChild(objHidden);
  return objHidden;
}

