
var reqObject;


function hideMainResults() {
  document.getElementById("mainProducts").style.display = "none";
}

function showMainResults() {
  document.getElementById("mainProducts").style.display = "block";
}

function hideSearchResults() {
  document.getElementById("searchCat").style.display = "none";
}

function showSearchResults() {
  document.getElementById("searchCat").style.display = "block";
}

function updateQuoteStateChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {
    // It's finished, we don't care.
    //document.getElementById("topHeading").innerHTML = reqObject.responseText;
    document.getElementById("quoteTotalValue").innerHTML = "£" + reqObject.responseText;
  } 
}

function updateQuote(mode, productId, price, other, ascGroup, ascMutex) {
  reqObject = createRequestObject();
  var url="quotations.php";
  url=url+"?mode="+ mode + "&product=" + productId  
        + "&price=" + price + "&sid="+Math.random();
  if (other != null) {
    url += "&associated=" + other + "&ascGroup=" + ascGroup + "&ascMutex=" + ascMutex;
  }
  url += "&processingInst=product120"; 
  //alert ("AscGroup = " + ascGroup);
  reqObject.onreadystatechange=updateQuoteStateChanged
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}

function associatedClicked(element, productId, associatedId, price, ascGroup, ascMutex) {
    
  // Throw in check to ensure element is checkbox
  var associated = element.parentNode.parentNode;
  var mode = "";
  if (element.checked) {
    mode = "add";
  }
  else {
    //alert ("Removing product " + associatedId);
    mode = "del";
  }
  //var wIndex = associated.id.indexOf("w");
  //var productId = associated.id.substring(1,wIndex);
  //var associatedId = associated.id.substring(wIndex + 1);
  
  //alert("ProductId = " + productId + ". associatedId = " + associatedId + ", price = " + price);
  updateQuote(mode, productId, price, associatedId, ascGroup, ascMutex);
}


function productClicked(element, productId) {
  // First parent is just CheckBoxDiv, grandparent is Product
  //alert ("Got the check: " + element.id + "\nParent Node = " + element.parentNode.parentNode.id);
  
  // Throw in check to ensure element is checkbox
  var product = element.parentNode.parentNode;
  var mode = "";
  if (element.checked) {
    mode = "add";
    // Have to add items to cart
    showAllElementsByClass(product,"productExtras");
  }
  else {
    mode = "del";
    // Have to remove items from cart
    hideAllElementsByClass(product,"productExtras");
  }
  //alert("Id = " + product.id + ", productId = " + productId);
  updateQuote(mode, productId, element.value,null,null,null);
}

function deliveryClicked(element, productId) {
  // First parent is just CheckBoxDiv, grandparent is Product
  //alert ("Got the check: " + element.id + "\nParent Node = " + element.parentNode.parentNode.id);
  
  // Throw in check to ensure element is checkbox
  var product = element.parentNode.parentNode;
  var mode = "";
  if (element.checked) {
    mode = "addDelivery";
  }
  else {
    mode = "delDelivery";
  }
  //alert("Id = " + product.id + ", productId = " + productId);
  updateQuote(mode, productId, element.value,null,null,null);
}

function productClickedOld(element) {
  // First parent is just CheckBoxDiv, grandparent is Product
  //alert ("Got the check: " + element.id + "\nParent Node = " + element.parentNode.parentNode.id);
  
  // Throw in check to ensure element is checkbox
  var product = element.parentNode.parentNode;
  var mode = "";
  if (element.checked) {
    mode = "add";
    // Have to add items to cart
    showAllElementsByClass(product,"productExtras");
  }
  else {
    mode = "del";
    // Have to remove items from cart
    hideAllElementsByClass(product,"productExtras");
  }
  var productId = product.id.substring(7);
  var name = document.getElementById("name" + productId).innerHTML;//
  var taxable = "T";
  //alert("Id = " + product.id + ", productId = " + productId);
  updateQuote(mode, productId, name, element.value, taxable, null);
}

function showElement(elementId) {
 var element = document.getElementById(elementId);
 //element.style.display = (element.style.display == "none") ? "block" : "none";
 element.style.display = (element.style.display == "none") ? "inline" : "none";
 //alert ("Div top = " + element.offsetTop + "\nScroll Top = " + document.getElementById("mainProducts").scrollTop);
 document.getElementById("outerProducts").scrollTop = element.offsetTop - 170;
 //alert ("OffsetParent = " + element.offsetParent);
 hideSearchResults();
 showMainResults();
 return false;
}

function hideAllDetails() {
  var divs = document.getElementById("mainProducts").getElementsByTagName("DIV");
  var nextDiv;
  var items = "";
  //for each (nextDiv in divs) {
  for (var i = 0; i < divs.length; i++) {
    //items += "item: " + nextDiv.id + ", ";
    nextDiv = divs[i];
    if (nextDiv.className == "productDetails") {
        nextDiv.style.display = "none";
    }
  }
  hideSearchResults();
  showMainResults();
  document.getElementById("outerProducts").scrollTop = 0;
  return false;
}

function catClick(elementId) {
  return false;
}

function catListClick(elementId){
  hideAllDetails();
  return showElement(elementId);
}


function showAllDetails() {
  var divs = document.getElementById("mainProducts").getElementsByTagName("DIV");
  var nextDiv;
  var items = "";
  //for each (nextDiv in divs) {
  for (var i = 0; i < divs.length; i++) {
    //items += "item: " + nextDiv.id + ", ";
    nextDiv = divs[i];
    if (nextDiv.className == "productDetails") {
        //nextDiv.style.display = "block";
        nextDiv.style.display = "inline";
    }
  }
  // Show all, but hide Search.
  hideSearchResults();
  showMainResults();
  document.getElementById("outerProducts").scrollTop = 0;
  return false;
}

function productStateChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {

    var element = document.getElementById("mainProducts");
    var newContent = document.createElement('div');
    newContent.style.display = "block";
    newContent.innerHTML = reqObject.responseText;
    while (element.firstChild) {
      element.removeChild(element.firstChild);
    }
    element.appendChild(newContent);
    var loadingBlock = document.getElementById("productLoading");
    if (loadingBlock != null) {
      loadingBlock.style.display = "none";
    }
    hideSearchResults();
    showMainResults();
    
    //document.getElementById("mainProducts").innerHTML=reqObject.responseText;
  } 
}

function searchStateChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {
    hideAllDetails();
    var element = document.getElementById("searchCat");
    var newContent = document.createElement('div');
    newContent.style.display = "block";

    newContent.innerHTML = reqObject.responseText;
    while (element.firstChild) {
      element.removeChild(element.firstChild);
    }
    element.appendChild(newContent);
    element.style.display = "block";
    document.getElementById("outerProducts").scrollTop = 0;
    
    hideMainResults();
  } 
}

function loadProducts(category) {
  reqObject = createRequestObject();
  var url="getProducts.php";
  url=url+"?type=p&category=" + category + "&sid="+Math.random();
  url=url+"&processingInst=product110";
  reqObject.onreadystatechange=productStateChanged;
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}

function catStateChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {
    var element = document.getElementById("mainCats");
    var newContent = document.createElement('div');
    newContent.innerHTML = reqObject.responseText;
    if (element.firstChild) {
      element.removeChild(element.firstChild);
    }
    element.appendChild(newContent)
    //document.getElementById("mainCats").innerHTML=reqObject.responseText;
    loadProducts(0);
  } 
}

function loadCategories() {
  reqObject = createRequestObject();
  var url="getProducts.php";
  url=url+"?type=c&sid="+Math.random();
  url=url+"&processingInst=product110";
  reqObject.onreadystatechange=catStateChanged;
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}

function searchByName() {
  reqObject = createRequestObject();
  var nameText = document.getElementById("productSearch").value;
  var url="getProducts.php";
    url=url+"?name=" + nameText + "&sid="+Math.random();
    url=url+"&processingInst=product110";
  reqObject.onreadystatechange=searchStateChanged;
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}

function donnotCare() {

}

function clearQuoteProducts(form) {
  form.mode.value = "clearProducts";
  form.processingInst.value = "product100"; 
}


function showQuoteStart() {
  //showDialog('Success','Your request has been successfully received.','success');
  showDialog('Prompt','Your request has been successfully received.','prompt');
  return false;
}

function quote1(form, action) {
  //document.quoteForm.mode.value = mode;
  //document.quoteForm.action = "quotes1.php";
  form.mode.value = action;
  form.action = "quotes1.php";
  return true;
}

function loadAndCopyQuotation(quotationId,mode) {
  document.quoteForm.mode.value = mode;
  document.quoteForm.quotation.value = quotationId;
  document.quoteForm.processingInst.value = "quotation210";
  if (mode == "copy")
    document.quoteForm.action = "quotes1.php";
  else
    document.quoteForm.action = "quoteSummary.php";
  //alert("Set quotation id to " + document.quoteForm.quotation.value);
  return true;
}

function startNewQuotation() {
  if (! document.quoteForm.quoteName.value.length > 0) {
    showDialog('Name Missing','Please specify a name for this quotation.','warning');
    //document.quoteForm.quoteName.value = "Prompted";
    return false;
  } 
  document.quoteForm.quotationNotesParsed.value = convertTextAreaOut(document.quoteForm.quotationNotes.value);
  document.quoteForm.mode.value = "initiate";
  document.quoteForm.quotation.value = "";
  document.quoteForm.action = "items.php";
  //alert("Set quotation id to " + document.quoteForm.quotation.value);
  return true;
}

function savedQuoteChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {
    // Check return for something before saying this - could be an error.
    if (reqObject.responseText != null && reqObject.responseText != "") {
      showDialog('Warning','An error occurred trying to save Quotation\n:' + reqObject.responseText,'warning');
    }
    else {
      showDialog('Success','Your Quotation has been saved.','success');
    }      
  } 
}

function saveQuotation() {
  // quotationNotes is now quotationNotesSmall on the Summary Page
  document.quoteForm.quotationNotesParsed.value = convertTextAreaOut(document.quoteForm.quotationNotesSmall.value);
  reqObject = createRequestObject();
  var url="quotations.php";
  url=url+"?mode=save" + "&sid="+Math.random();
  url += '&processingInst=quotation210&quotationNotesParsed=' + escape(document.quoteForm.quotationNotesParsed.value);
  reqObject.onreadystatechange=savedQuoteChanged
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}


function printQuotation() {
  miniWindow("quotationPrint.php",900,800);  
  return false;
}

function emailQuoteChanged() {
  if (reqObject.readyState==4 || reqObject.readyState=="complete") {
    // Check return for something before saying this - could be an error.
    var responseText = reqObject.responseText;
    if (responseText.length == 0) {
      showDialog('Success','Thank you. You should receive an e-mail shortly.','success');
    }      
    else {
      showDialog('Warning','Something went wrong - ' + responseText,'warning');
    }
  } 
}


function switchQuotationView() {
  document.quoteForm.mode.value = 'changeView';
  return true;
}

function emailQuotation() {
  reqObject = createRequestObject();
  var url="quotations.php";
  url += "?mode=email" + "&sid="+Math.random();
  url += "&processingInst=quotation210";
  reqObject.onreadystatechange=emailQuoteChanged
  reqObject.open("GET",url,true);
  reqObject.send(null);
  return false;
}

var currentInput = null;
var currentText = null;

function setTextClass(currentClass) {
  var elements = document.getElementsByName("profileText");
  for (var i = 0; i < elements.length; i++) {
    elements[i].className = currentClass;
  }

}

function submitComplaint() {
  document.complaintsForm.valid.value = true;
  return true;
}


function quoteNavigate(form, target) {
  setFormTarget(form, target);
  form.quotationNotesParsed.value = convertTextAreaOut(form.quotationNotes.value);
  form.submit();
  return false;
}

function showQuotation(quotationId) {
  miniWindow("quotationPrint.php?quotationId=" + quotationId,900,700);  
  return false;
}

function exportQuotation(quotationId) {
  document.quotationForm.mode.value = "export";
  document.quotationForm.quotation.value = quotationId;
  document.quotationForm.valid.value = "true";

  //showDialog('Success','The Quotation has been exported','success');
  return true;
}
