
// What to do with the form
var g_ceFormSubmit = 0;
var g_ceFormUpdate = 1;
var g_eFormAction = g_ceFormSubmit;

// The bookark popup settings
var addthis_pub = 'cacolijn';
var addthis_options = 'favorites, email, digg, delicious, more';
var addthis_logo = 'http://loonmutatietool.twologs.com/images/logo.png';
var addthis_brand = 'De LoonmutatieTool';
var addthis_logo_background = 'ffffff';
var addthis_logo_color = '000000';

// Sets the bookmark popup settings
function SetBookmarkSettings(sSection) {
  // Get the color of the body text
  var oBody = document.body;
  var sBackColor;
  if (oBody.currentStyle) {
    sBackColor = oBody.currentStyle['backgroundColor'];
  } else if (window.getComputedStyle){
    sBackColor = document.defaultView.getComputedStyle(oBody, null).getPropertyValue('background-color');
  } else {
    sBackColor = '#e7f8fa';
  }

  // And override the default bookmark settings
  addthis_logo = 'http://loonmutatietool.twologs.com/images/logo.png';
  addthis_logo_background = "ffffff";
}

// Notes what form action to do
function NoteFormAction(eNewAction) {
  g_eFormAction = eNewAction;
  return true;
}

// Returns if this is a form submit (and not an update)
function IsFormSubmit() {
  return g_eFormAction == g_ceFormSubmit;
}

// Puts the focus to the given field in the given form
function GotoField(sForm, sField) {
  document.forms[sForm].elements[sField].focus();
}

// Corrects the given quantity
function CorrectQuantity(nQuantity) {
  // Look if it's valid
  if (/^\s*\d+\s*$/.test(nQuantity)) {
    // Yes -> make it numeric
    return parseInt(nQuantity);
  } else {
    // No -> no quantity
    return 0;
  }
}

// Makes the given price in the correct display format
function MakePrice(sCurrency, nPriceRaw, bMakeDutch) {
  var sResult;
  if (nPriceRaw == 0) {
    sResult = '';
  } else {
    sResult = sCurrency + '&nbsp;' + Math.round(nPriceRaw * 100) / 100;
    var nDotPos = sResult.indexOf('.');
    if (nDotPos < 0) {
      sResult += '.00';
    } else if (sResult.length - nDotPos <= 2) {
      sResult += '0';
    }
    if (bMakeDutch) {
      sResult = sResult.replace('.', ',');
    }
  }
  return sResult;
}

// Whether the given thing exists
function Exists(oThing) {
  return typeof oThing != 'undefined';
}

// Stores the page position in the given form for later repositioning
function StorePagePosition(oForm) {
  // Look if the page's position can be retrieved
  var nLeft = document.body.scrollLeft;
  var nTop = document.body.scrollTop;
  if (Exists(nLeft) && Exists(nTop)) {
    // Yes -> store the values in the form
    oForm.elements['pageposleft'].value = nLeft;
    oForm.elements['pagepostop'].value = nTop;
  }
}

// Restores the page position from the given form
function RestorePagePosition(sFormName) {
  // Look if the page's position is specified
  var aoFormFields = document.forms[sFormName].elements;
  var oLeftField = aoFormFields['pageposleft'];
  var oTopField = aoFormFields['pagepostop'];
  if (Exists(oLeftField) && Exists(oTopField)) {
    var nLeft = oLeftField.value;
    var nTop = oTopField.value;
    if (Exists(nLeft) && Exists(nTop)) {
      // Yes -> restore the page position
      document.body.scrollLeft = nLeft;
      document.body.scrollTop = nTop;
    }
  }
}

// Checks if the given email address is invalid
function EMailAddressValid(sAddress) {
  // Look if the proposed address is in the format x@y.z
  var nAtPosition = sAddress.indexOf('@');
  var bValid = false;
  if (nAtPosition > 0) {
    var nDotPosition = sAddress.indexOf('.', nAtPosition);
    if (nDotPosition > nAtPosition + 1) {
      nDotPosition = sAddress.lastIndexOf('.');
      if (nDotPosition < sAddress.length - 1) {
        bValid = true;
      }
    }
  }
  return bValid;
}

// Ensures that the given field on the given form has any content
function EnsureFieldHasContent(oForm, sFieldName, sEmptyMsg) {
  // Get the field
  var oField = oForm.elements[sFieldName];

  // Look if it has content
  var bHasContent = oField.value != '';
  if (!bHasContent) {
    // No -> place the focus on the field
    oField.focus();

    // And show the message
    alert(sEmptyMsg);
  }

  // And return whether the field has any content
  return bHasContent;
}

// Ensures that the given field on the given form has a numeric content
function EnsureFieldHasNumber(oForm, sFieldName, sEmptyMsg, sNotANumberMsg) {
  // Get the field
  var oField = oForm.elements[sFieldName];

  // Look if it has content
  var sFieldValue = oField.value;
  var bHasNumber = sFieldValue != '';
  if (!bHasNumber) {
    // No -> place the focus on the field
    oField.focus();

    // And show the message
    alert(sEmptyMsg);
  } else {
    // Yes -> look if it is numeric
    bHasNumber = /^\s*\d+\s*$/.test(sFieldValue);
    if (!bHasNumber) {
      // No -> place the focus on the field
      oField.focus();

      // And show the message
      alert(sNotANumberMsg);
    }
  }

  // And return whether the field contains a number
  return bHasNumber;
}

// Ensures that the given field on the given form has any content
function EnsureFieldHasEMail(oForm, sFieldName, sEmptyMsg, sIllegalAddressMsg) {
  // Get the field
  var oField = oForm.elements[sFieldName];

  // Look if it has content
  var sFieldValue = oField.value;
  var bHasEMail = sFieldValue != '';
  if (!bHasEMail) {
    // No -> place the focus on the field
    oField.focus();

    // And show the message
    alert(sEmptyMsg);
  } else {
    // Yes -> look if it matches an email address
    bHasEMail = EMailAddressValid(sFieldValue);
    if (!bHasEMail) {
      // No -> place the focus on the field
      oField.focus();

      // And show the message
      alert(sIllegalAddressMsg);
    }
  }

  // And return whether the field contains an email address
  return bHasEMail;
}

// Ensures that the given field on the given form has a valid email address, if any
function EnsureFieldHasValidEMail(oForm, sFieldName, sIllegalAddressMsg) {
  // Get the field
  var oField = oForm.elements[sFieldName];

  // Look if it has content
  var sFieldValue = oField.value;
  var bValidEMail = true;
  if (sFieldValue != '') {
    // Yes -> look if it matches an email address
    bValidEMail = EMailAddressValid(sFieldValue);
    if (!bValidEMail) {
      // No -> place the focus on the field
      oField.focus();

      // And show the message
      alert(sIllegalAddressMsg);
    }
  }

  // And return whether the field contains an email address
  return bValidEMail;
}

// Gets the e-mail address of the given TwoLogs recipient
function GetEMailAddress(sRecipient) {
  var sResult = sRecipient;
  var nDummy = 1;
  sResult += "@t";
  nDummy = 2;
  sResult += "wo";
  nDummy = 3;
  sResult += "lo";
  nDummy = 4;
  sResult += "gs";
  nDummy = 5;
  sResult += ".c";
  nDummy = 6;
  sResult += "om";
  nDummy = 7;
  return sResult;
}

// Writes out the email address for the given recipient in a safe
// way to circumvent automatic email address harvesters
function WriteEMailLink(sRecipient) {
  var sEMailAddress = GetEMailAddress(sRecipient);
  var sResult = '<a href="mailto:' +
    sEMailAddress +
    '" onclick="javascript:window.open(\'mailto:' +
    sEMailAddress +
    '\'); return false;">' +
    sEMailAddress +
    '</a>';
  document.write(sResult);
}

// Gets the given element
function GetElement(sElemID) {
  return document.getElementById(sElemID);
}

// Makes the given element highlight images when hovered over
// Specify any extra images to highlight as extra parameters in the order "ImgID", NormalURL", "HighlightURL"
function HighlightOnHover(sHoverElementID, sImgID, sNormalURL, sHighlightURL) {
  // Get the element to use
  var oElement = GetElement(sHoverElementID);

  // Build a list of all images to highlight
  var aoImages = [];
  for (var nImgNr = 1; nImgNr <= arguments.length - 3; nImgNr += 3) {
    // Ref the image to highlight
    var oImage = new Object;
    oImage.oElement = GetElement(arguments[nImgNr]);

    // Pre-load the images
    oImage.oNormalImage = new Image();
    oImage.oNormalImage.src = arguments[nImgNr + 1];
    oImage.oHighlightImage = new Image();
    oImage.oHighlightImage.src = arguments[nImgNr + 2];

    // And add it to the list
    aoImages[aoImages.length] = oImage;
  }

  // And attach custom event functions to the element to trigger the switch
  oElement.onmouseover = (
    function() {
      for (var nImgNr = 0; nImgNr < aoImages.length; ++nImgNr) {
        aoImages[nImgNr].oElement.src = aoImages[nImgNr].oHighlightImage.src;
      }
    }
  );
  oElement.onmouseout = (
    function() {
      for (var nImgNr = 0; nImgNr < aoImages.length; ++nImgNr) {
        aoImages[nImgNr].oElement.src = aoImages[nImgNr].oNormalImage.src;
      }
    }
  );
}

// Returns the current year
function ThisYear() {
  var nThisYear = new Date();
  nThisYear = nThisYear.getYear();
  if (nThisYear < 1900) {
    nThisYear += 1900;
  }
  return nThisYear;
}
