/*
 * SYSTEM_NAME: Guentersberg
 *
 * COMPONENT_NAME: Shop
 *
 * FUNCTIONS: Shopping cart, order form handling
 *
 * AUTHOR: Franz Fackelmann
 *
 * CREATED: 10.01.2010
 *
 */


var validationErrMsgId = "validationErrMsg";
var recaptchaErrMsgId = "recaptcha_response_fieldLbl";

var colorError = "#0000CC";
var colorDefault = "#000000";


function initCart()
{
  cleanLocation();

  showShipAddr();
  showErrMsg(validationErrMsgId, false);
}


/* switch shipping address table on/off */
function showShipAddr()
{
  var cb = document.getElementsByName("versandadr_")[0]; // the checkbox
  var tbl = document.getElementById("tblVersandAdr"); // the shipping address table

  // handle attribute CHECKED and VALUE
  if (cb != null) {
    var cbCheckedAttr = null;

    cb.checked = cb.checked; // avoid mismatch between status and display
    cbCheckedAttr = cb.getAttributeNode("checked");
    if (cbCheckedAttr != null) cb.removeAttributeNode(cbCheckedAttr);
  }

  if (cb != null && tbl != null) {
    var input = tbl.getElementsByTagName("input"); // all input fields in tbl

    // show/hide the whole table with the shipping address
    tbl.style.display = (cb.checked ? "block" : "none");

    // show/hide input fields too in order to be able to ignore them in checkOrder()
    for (var i = 0; i < input.length; ++i)
     input[i].style.visibility = (cb.checked ? "visible" : "hidden");
  }
}


/* check order form input fields for completeness before submitting the form */
function checkOrder()
{
  var form = document.getElementById("frmOrder");
  var input = form.getElementsByTagName("input");
  var haveErrors = false;

  /* mark label of empty required input fields
   * Notes:
   * - not required fields have a trailing "_" in the name
   * - hidden fields will not be checked
   * - the label of an input field has ID="<name>Lbl"
   */
  for (var i = 0; i < input.length; ++i) {
    if (input[i].getAttribute("type") != "submit" &&
        input[i].style.visibility != "hidden") {
      var name = input[i].getAttribute("name");
      var lbl = document.getElementById(name + "Lbl");
      var val = input[i].value;
      var modValue = false; // modified value to normalized form

      // normalize input value - if it's invalid nval will be ""
      var nval = normFormValue(name, val);

      if (nval != input[i].value && nval != "") {
        input[i].value = nval;
        modValue = true;
      }

      if (name.charAt(name.length - 1) != '_' && nval.length == 0) {
        if (!haveErrors) {
          haveErrors = true;

          // set focus on first erroneous one
          input[i].focus();
        }

        if (lbl != null) {
          lbl.style.fontWeight = "bold";
          lbl.style.color = colorError;
        }
      }

      else if (lbl != null) {
        if (modValue) haveErrors = true;

        lbl.style.fontWeight = "normal";
        lbl.style.color = (modValue ? colorError : colorDefault);
      }
    }
  }

  showErrMsg(validationErrMsgId, haveErrors);

  return !haveErrors;
}


function initCheckout(lang)
{
  cleanLocation();

  recaptchaInit(lang);

  showErrMsg(validationErrMsgId, false);
  showErrMsg(recaptchaErrMsgId, false);
}


function checkCheckout()
{
  var haveErrors = !recaptchaValidate();

  showErrMsg(recaptchaErrMsgId, haveErrors);

  return !haveErrors;
}


function normFormValue(name, val)
{
  var nval = val;

  nval = nval.replace(/\s+/g, " "); // multiple spaces to single spaces
  nval = nval.replace(/^\s+/, ""); // remove leading spaces
  nval = nval.replace(/\s+$/, ""); // remove trailing spaces

  if (name.match(/telefon/)) {
    // check for invalid phone#
    if (!nval.match(/^[0-9\s\-\/\(\)\[\]]+$/)) nval = "";
  }

  else if (name.match(/email/)) {
    nval = nval.replace(/\s+/g, ""); // remove all spaces

    // check for invalid e-mail addr
    if (!nval.match(/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) nval = "";
  }

  /* FIXME TODO ### PLZ-Prüfung muss internationaler werden
  else if (name.match(/plz/)) {
    nval = nval.replace(/\s+/g, ""); // remove all spaces

    // check for grossly invalid zip
    if (!nval.match(/^[0-9]{3,5}(-[0-9]{3,5})?$/)) nval = "";
  }
  */

  return nval;
}


function showErrMsg(id, show)
{
  var msg = document.getElementById(id);
  if (msg != null) msg.style.display = (show ? "block" : "none");
}

