﻿window.onload = function() {
  initCustomerNotes();
  setEditMode("instructions", false);
}

// it is assumed that the editor input element exist and is preloaded with data
function setEditMode(elId, enableEdit){
  var elEditor = byId(elId);
  if(!elEditor) return false;
  var elShow = byId(elId + "_show");
  if (enableEdit) {
    elEditor.rows=lineCount(elEditor.value, elEditor.cols) + 1;
    if(elShow) elShow.style.display = "none";
    elEditor.style.display = "";
    }
  else {
    elEditor.style.display = "none";
    var html = cleanHtml(elEditor.value).replace(/\n/g,"<br>");
    if(html) { // is there any data to show?
      if(!elShow) { // does the elShow element already exist?
        // no, so create a pre element for displaying
        var showPre = document.createElement('pre');
        showPre.id = elId + "_show";
        showPre.style.margin="0px";
        showPre.style.padding="0px";
        elEditor.parentNode.appendChild(showPre);
        elShow = byId(elId + "_show");
        }
      //alert("'"+elShow.innerHTML+"'")
      elShow.style.display = "";
      elShow.innerHTML = html
      }
    }
  //alert("'"+elShow.innerHTML+"'")
  return true;
  }

function cleanHtml(text){
  return text
    .replace(/\&/g,"&amp;")
    .replace(/\</g,"&lt;")
    .replace(/\>/g,"&gt;");
  }

function lineCount(text, maxLine){
  // counts lines caused by autowrap and newline char
  var i = 0, pos2 = 0, pos1, len;
  if(!text) return 1;
  do {
    pos1 = text.indexOf("\n", pos2)
    // compute line length
    if (pos1 < 0) len = text.length - pos2;
    else len = pos1 - pos2
    // if over length ...
    if(len > maxLine) i = i + parseInt(len/maxLine)
    pos2 = pos1 + 1
    i++
  } while (pos1 >= 0)
  return i
}


function initCustomerNotes(){
  var i = 0
  do {
    var ok = setEditMode("cart_notes_" + i, false);
    i++;
    } while(ok)
  }

function onQuantityChange(){
  disableOrderQuoteButtons();
  hideTotals();
  highlightUpdate();
}

function onInstructionsChange(){
  disableOrderQuoteButtons();
  highlightUpdate();
}

function onRegionCodeChange(){
  disableOrderQuoteButtons();
  hideTotals();
  highlightUpdate();
}

function onDiscountCodeChange() {
  disableOrderQuoteButtons();
  //hideTotals();
  highlightUpdate();
}

function onNoteChange(){
   disableOrderQuoteButtons();
   highlightUpdate();
}

function onEmailChange(){
   disableOrderQuoteButtons();
   highlightUpdate();
}

function onImageSizeChange(){
  highlightUpdate();
}

function disableOrderQuoteButtons(){
  //document.getElementById("quoteOrderButtons").style.display="none";
  var quote = byId("quote")
  if(quote) quote.disabled=true
  var order = byId("order")
  if(order) order.disabled=true
  //var userMsg =document.getElementById("userMessage");
  //userMsg.innerHTML += "<div>Click <i>Update</i> to view totals, taxes and shipping.</div>";
}  
function highlightUpdate(){
  var updateSurround = byId("updateSurround")
  updateSurround.style.backgroundColor = "yellow"
  // try to auto submit document
  document.forms[0].submit();
}
function hideTotals(){
  var tDiv = byId("totalsDiv");
  if(tDiv) {
    //tDiv.innerHTML = "";
    //tDiv.style.display="none";
    tDiv.innerHTML = "<div>Click <i>Update</i> to recalculate totals, taxes and shipping.</div>";
    tDiv.style.color = "red";
    }
}

function byId(nodeId){
  return document.getElementById(nodeId);
}

