/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Balans Javascript
//  BY: Roeland Lengers at Rotterdam CS
//
// disableAllLinks(message)
// Disables ALL links on the page.
// If no message provided, use the default "Eén keer klikken is genoeg, de klik wordt al verwerkt..."
function disableAllLinks(message) {
    if (typeof(message) == "undefined") {
      message="Eén keer klikken is genoeg, de klik wordt al verwerkt...";
    }
    for (index = 0; index < document.links.length; index++) {
       document.links[index].href='javascript:alert(message);';
    }
}
function disableAllLinks() {
    for (index = 0; index < document.links.length; index++) {
       document.links[index].href='javascript:alert("Een keer klikken is genoeg, de klik wordt al verwerkt...");';
    }
}


// Function that uses the writetxt function to display a nice hovertext. This script positions an image on top.      
function writetxt2(text, fadethis) {
   writetxt("<img src=\'/balans/balans/images/balans_bg_header_50pct.gif\'><br>" + text, fadethis);
}

// Function to easily set all checkboxes on a form to "on".
function selectAll(formname) {
    var elements = $(formname).elements;
    for (i = 0; i < elements.length; i++) {
       if (elements[i].type == 'checkbox') {
           elements[i].checked = true;
       }
    }
}

// Function to easily set all checkboxes on a form to "off".
function deSelectAll(formname) {
    var elements = $(formname).elements;
    for (i = 0; i < elements.length; i++){
       if (elements[i].type == 'checkbox'){
          elements[i].checked = false;
       }
    }
}

// Function to easily toggle all checkboxes on a form to either "on" or "off" depending on state of check.
function checkedAll(check,formname) {
    checked = $(check).checked;
    if (checked == true) {
      selectAll(formname)
    } else {
      deSelectAll(formname);
    }
}

function toggleCheckbox(elementid) {
    var checkbox = document.getElementById(elementid);
    checkbox.checked = (checkbox.checked ? false : true );
}

// confirmAction(message, cancelurl, okurl, newwindowname)
// Pops up an alert box so a user can indicate "cancel" or "ok".
// The message is used as the alert message (no html markup, use \\n as newline,
// that's right: use TWO \'s, followed by a "n")
// If cancel is clicked, the cancelurl is loaded in the current window.
// If ok is clicked, the okurl is loaded in the currentwindow (if newwindowname
//    is not provided), or in a new window with name = newwindowname.
//
// Example:
// <% String actionlink = "confirmAction('Weet u zeker dat u dit product wilt inleveren?', '" +
//           thisPage + "', '" +
//           producthandinlink + "'" +
//           ", 'productprintwindow'" +
//           ")";
//  %>
//  <html:link href="#"
//             onclick="<%= actionlink %>"
//             onmouseover="javascript:window.status=this.getAttribute('title');return true"
//             onmouseout="javascript:window.status='';return true"
//             styleClass="buttonLink"
//             style="width: 10em; height: 4.5em;"
//             bundle="vignet"
//             title="Product inleveren en restitutie bepalen">Inleveren + restitutie bepalen</html:link>
//
function confirmAction(message, cancelurl, okurl, newwindowname) {
    if (!confirm(message)) {
       this.href=cancelurl;
       return false;
    } else {
       if (!newwindowname) {
         window.location.href=okurl;
       } else {
         window.open(okurl, newwindowname);
       }
       return false;
    }
  }

// Roughly the same, but now a "submit"-action will be issued on the formname provided
// The parameter "actionrequest" will first be set into the form.action value 
// hidden field (if that exists)
function confirmSubmitAction(message, cancelurl, formname, actionrequest) {
    form = document.getElementById(formname);

    if (!confirm(message)) {
       this.href = cancelurl;
    } else {
       if (typeof(actionrequest) != "undefined") {
          if (typeof(form.actionrequest) != "undefined") {
             form.actionrequest = actionrequest;
          } else {
            alert("Error on page, no 'actionrequest' parameter defined on form.\nPlease notify System Administrator of this");
          }
       }
       form.submit();
    }
  return false;
}

// Roughly the same, but now a "submit"-action will be issued on the formname provided
// The parameter "actionparameter" will first be set into the form.action value 
// hidden field (if that exists)
function confirmSubmitActionWithParameter(message, cancelurl, formname, actionparameter, actionvalue) {
    form = document.getElementById(formname);

    if (!confirm(message)) {
       this.href = cancelurl;
    } else {
       if (typeof(actionparameter) != "undefined") {
          if (typeof(document.getElementById(actionparameter)) != "undefined") {
             document.getElementById(actionparameter).value = actionvalue;
          } else {
            alert("Error on page, no '" + actionparameter + "' parameter defined on form.\nPlease notify System Administrator of this");
          }
       }
       form.submit();
    }
  return false;
}

// returnFalse()
// Utility function to just return false. This is nice to have since a construct
// like: "javascript: return false" is actually NOT allowed by HTML 4.0
function returnFalse() {
  return false;
}

// returnTrue()
// Utility function to just return true. This is nice to have since a construct
// like: "javascript: return true" is actually NOT allowed by HTML 4.0
function returnTrue() {
  return true;
}

function numbersOnly(myfield) {
  newvalue = "";
  for (i = 0; i < myfield.value.length; i++) {
    //alert("I: " + i + " value: [" + myfield.value.charCodeAt(i) + "]");
    if (myfield.value.charCodeAt(i) >= 47 && myfield.value.charCodeAt(i) <= 58) {
      newvalue = newvalue + myfield.value.charAt(i);
    }
  }
  if (newvalue.length == 0) {
     myfield.value = "";
  } else {
     myfield.value = newvalue;
  }
}

function alertFormatErrors(errors) {
  var output = "";
  for (i = 0; i < errors.length; i++) {
    if (i != 0) {
      output = output + "\n"
    }
    output = output + errors[i];
  }
  return output;
}

function htmlFormatErrors(errors) {
  var output = "";
  for (i = 0; i < errors.length; i++) {
    if (i != 0) {
      output = output + "<br>"
    }
    output = output + errors[i];
  }
  return output;
}

function enableInputRow(rowrefid) {
  // Set the style to edit mode.
  for (i = 0; i < editablefields.length; i++) {
     document.getElementById(editablefields[i]+rowrefid).className = editablefields_edit_styles[i];
     document.getElementById(editablefields[i]+rowrefid).readOnly = false;
     document.getElementById(editablefields[i]+rowrefid).disabled = false;
  }
  // Hide the edit and remove button
  document.getElementById("edit_" + rowrefid).style.display = "none";
  document.getElementById("remove_" + rowrefid).style.display = "none";
  // Show the cancel and save buttons
  document.getElementById("canceledit_" + rowrefid).style.display = "";
  document.getElementById("save_" + rowrefid).style.display = "";
  document.getElementById("responserow_" + rowrefid).style.display = "none";
  document.getElementById("responsecell_" + rowrefid).innerHTML = "";
}

function showCreateResult(data) {
  if (data.errortext != '') {
    alert(alertFormatErrors(data.errortext));
  } else {
    alert(data.messagetext);
    if (data.newsearchparameters != '') {
       // NOTE: we are using a thisPageOriginalURI variable here, which should be set on each page
       // that uses this function.
       // The purpose of this variable is to a allow a browser redirect to show only the recently
       // created data.
       window.location.href = thisPageOriginalURI + "?" + data.newsearchparameters;
    }
  }
}

// Function to erase fields. The way to call this is:
// var erasefields = new Array("streetname", "housenumber", "suffix");
// var erasefieldvalues = new Array("", "", "");
// eraseFields(erasefields, erasefieldvalues);
function eraseFields(erasefields, erasefieldvalues) {
  for (i = 0; i < erasefields.length; i++) {
     document.getElementById(erasefields[i]).value = erasefieldvalues[i];
  }
}

function disableInputRow(rowrefid, resetRow) {
  // Set the style to no edit mode.
  for (i = 0; i < editablefields.length; i++) {
     document.getElementById(editablefields[i]+rowrefid).className = editablefields_noedit_styles[i];
     document.getElementById(editablefields[i]+rowrefid).readOnly = editablefields_noedit_readonlystatus[i];
     document.getElementById(editablefields[i]+rowrefid).disabled = editablefields_noedit_disabledstatus[i];
  }
  // Show the edit and remove button
  document.getElementById("edit_" + rowrefid).style.display = "";
  document.getElementById("remove_" + rowrefid).style.display = "";
  // Hide the cancel and save buttons
  document.getElementById("canceledit_" + rowrefid).style.display = "none";
  document.getElementById("save_" + rowrefid).style.display = "none";
  if (resetRow == "true") {
    // And now we need to retrieve the data as it was originally stored on the server.
    // This is needed because when someone edits, changes a value, then clicks "cancel"
    // the "changed" value would still be shown, whilst it is not really stored.
    refetchFunction(rowrefid);
  }
}

function resetRowDetails(data) {
   rowrefid = data[refid];
   if (data.errortext != '') {
     document.getElementById("responserow_" + rowrefid).style.display = "";
     document.getElementById("responsecell_" + rowrefid).innerHTML = htmlFormatErrors(data.errortext);
   } else {
     for (i = 0; i < editablefields.length; i++) {
       document.getElementById(editablefields[i]+rowrefid).value = data[resetfields[i]];
     }
   }
}

function cancelInputRow(rowrefid) {
   document.getElementById("responserow_" + rowrefid).style.display = "none";
   document.getElementById("responsecell_" + rowrefid).innerHTML = "";
   disableInputRow(rowrefid, "true");
}

function showCommitRowResult(data) {
  rowrefid = data[refid];
  if (data.errortext != '') {
     document.getElementById("responserow_" + rowrefid).style.display = "";
     document.getElementById("responsecell_" + rowrefid).innerHTML = htmlFormatErrors(data.errortext);
  } else {
    document.getElementById("responserow_" + rowrefid).style.display = "";
    document.getElementById("responsecell_" + rowrefid).innerHTML = data.messagetext;
    disableInputRow(rowrefid, "true");
  }
}

function showDeleteRowResult(data) {
  rowrefid = data[refid];
  if (data.errortext != '') {
     document.getElementById("responserow_" + rowrefid).style.display = "";
     document.getElementById("responsecell_" + rowrefid).innerHTML = htmlFormatErrors(data.errortext);
  } else {
    document.getElementById("responserow_" + rowrefid).style.display = "";
    document.getElementById("responsecell_" + rowrefid).innerHTML = data.messagetext;
    disableInputRow(rowrefid, "false");
    document.getElementById("edit_" + rowrefid).style.display = "none";
    document.getElementById("remove_" + rowrefid).style.display = "none";
    // Remove edit capabilities on row double click
    if (BrowserDetect.browser == "Explorer") {
      document.getElementById("datarow_" + rowrefid).ondblclick = null;
      document.getElementById("datarow_" + rowrefid).onmouseover = null;
      document.getElementById("datarow_" + rowrefid).onmouseout = null;
    } else {
      document.getElementById("datarow_" + rowrefid).ondblclick = returnTrue();
    }
  }
}

function deleteRow(rowrefid, question) {
  var element = document.getElementById("datarow_" + rowrefid);
  var oldclassname = "";
  var oldmouseover = "";
  var oldmouseout = "";
  var olddblclick = "";
  if (BrowserDetect.browser == "Explorer") {
    oldclassname = element.className;
    oldmouseover = element.onmouseover;
    oldmouseout = element.onmouseout;
    olddblclick = element.ondblclick;
    element.onMouseOver = returnTrue();
    element.onMouseOut = returnTrue();
    element.onDblClick = returnTrue();
    element.style.backgroundColor = "";
    element.className="rowToBeDeleted";
  } else {
    oldclassname = element.className;
    oldmouseover = element.onmouseover;
    oldmouseout = element.onmouseout;
    olddblclick = element.ondblclick;
    element.onmouseover = returnTrue();
    element.onmouseout = returnTrue();
    element.ondblclick = returnTrue();
    element.style.backgroundColor = "";
    element.className="rowToBeDeleted";
  }
  
  if (confirm(question)) {
    var identifier = "" + rowrefid;
    element.className="rowDeleted";
    deleteFunction(rowrefid);
  } else {
    element.style.backgroundColor = "";
    if (BrowserDetect.browser == "Explorer") {
      element.onMouseOver = oldmouseover;
      element.onMouseOut = oldmouseout;
      element.onDblClick = olddblclick;
      element.className = oldclassname;
    } else {
      element.onmouseover = oldmouseover;
      element.onmouseout = oldmouseout;
      element.ondblclick = olddblclick;
      element.className = oldclassname;
    }
    return false;
  }
}

function showDeleteDetailRowResult(data) {
  rowrefid = data[refid];
  if (data.errortext != '') {
     document.getElementById("responserow_" + rowrefid).style.display = "";
     document.getElementById("responsecell_" + rowrefid).innerHTML = htmlFormatErrors(data.errortext);
  } else {
     if (data.newsearchparameters != '') {
       // NOTE: we are using a thisPageOriginalURI variable here, which should be set on each page
       // that uses this function.
       // The purpose of this variable is to a allow a browser redirect to show only the recently
       // created data.
       window.location.href = thisPageOriginalURI + "?" + data.newsearchparameters;
    }

  }
}

