/***********/
/* General */
/***********/

var httpRequest = null;

/* Login */
function adminLogin () {
  var username = getElement("username");
  var password = getElement("password");
  httpRequest = new HttpRequest("login_script.php", adminLoginCallback);
  var params = "username=" + username.value + "&password=" + password.value;
  httpRequest.post(params);
}

function adminLoginCallback () {
  var response = httpRequest.getResponse();
  response = trim(response);
  // console.log(response);
  if (response == "0") {
    var errorDiv = getElement("error");
    var password = getElement("password");
    errorDiv.innerHTML = "<FONT size=-1 color=\"#CC0000\">Sorry, the username and password you entered were not recognized.</FONT>";
  }
  else window.location = "admin_main.php";
}

function cancel () {
  closeInnerWindow();
}

function select (elementName) {
  var elementStyle = getStyleObject(elementName);
  elementStyle.fontSize = "12pt";
  elementStyle.backgroundColor = "#CCCCDD";
}

function deselect (elementName) {
  var elementStyle = getStyleObject(elementName);
  elementStyle.fontSize = "11pt";
  elementStyle.backgroundColor = "#BBBBCC";
}

function shiftBanner (direction, min, max) {
  var backgroundStyle = getStyleObject("background");
  var top = backgroundStyle.top;
  top = stripTrailing(top, 2);

  if ((top < min && direction < 0) || (top > max && direction > 0)) {
    // console.log(top + " - " + min + " : " + top + " - " + max + " : " + direction);
    direction *= -1;
  }
  
  backgroundStyle.top = (Number(top) + direction) + "px";
  // console.log(backgroundStyle.top);
  setTimeout("shiftBanner(" + direction + ", " + min + ", " + max + ")", 100);
}

function loadContent (page, params) {
  openLoadingMessage();
  httpRequest = new HttpRequest(page, loadContentCallback);
  if (!params || params == null)
    params = " ";
  httpRequest.post(params);
}

function loadContentCallback () {
  var response = httpRequest.getResponse();
  var content = getElement("content");
  //console.log(response);
  content.innerHTML = response;
  closeLoadingMessage();
}

function editEntity (entity, entityID) {
  httpRequest = new HttpRequest("", editEntityCallback);
  var params = "entity=" + entity + "&entity_ID=" + entityID;
  httpRequest.post(params);
}

function editEntityCallback () {
  var response = httpRequest.getResponse();
}

function saveEntity (entity, entityID) {
  httpRequest = new HttpRequest("save_entity.php", saveEntityCallback);
  httpRequest.setParameter("entity", entity);
  httpRequest.setParameter("entity_ID", entityID);
  var entityName = getElement(entity + "_name");
  var params = "entity=" + entity + "&entity_name=" + entityName.value;
  httpRequest.post(params);
}

function saveEntityCallback () {
  var entity = httpRequest.getParameter("entity");
  var entityID = httpRequest.getParameter("entity_ID");
  var entitySelect = getElement(entity);

  // console.log(entityID);
  var response = httpRequest.getResponse();
  // console.log(response);
  if (entityID == "0") {
    var responseParts = response.split("|");
    var numOptions = entitySelect.options.length;
    entitySelect.options[numOptions] = new Option(responseParts[0], responseParts[1]);
  }
  closeInnerWindow();
}

function loadWine (wine) {
  var params = "wine=" + wine;
  loadContent("get_wine_by_name.php", params);
}

function loadWineByCategory (categoryID) {
  var params = "category_ID=" + categoryID;
  loadContent("get_wine_by_category.php", params);
}

function saveWine () {
  var wineForm = getElement("wine_form");
  wineForm.submit();
  return;

  httpRequest = new HttpRequest("save_wine.php", saveWineCallback);
  var wineID = getElement("wine_ID");
  var year = getElement("year");
  var name = getElement("name");
  var category = getElement("category");
  var bottleMls = getElement("bottle_mls");
  var price = getElement("price");
  var params = "wine_ID=" + wineID.value + "&description=" + tinyMCE.getContent() + "&year=" + year.value + "&name=" + name.value + "&bottle_mls=" + bottleMls.value + "&price=" + price.value + "&category=" + category.value;
  httpRequest.post(params);

}

function saveWineCallback () {
  var response = httpRequest.getResponse();
  window.location = "edit_wine.php";
  // console.log(response);
  
}

function deleteWine (wineID) {
  deleteEntity("wine", wineID, deleteWineCallback);
}

function deleteEntity (entity, entityID, callbackFunction) {
  var confirmation = confirm("Are you sure you want to delete this " + entity + "? This operation cannot be undone.");
  if (!confirmation)
    return;

  httpRequest = new HttpRequest("delete_entity.php", callbackFunction);
  var params = "entity=" + entity + "&entity_ID=" + entityID;
  httpRequest.post(params);
}

function deleteWineCallback () {
  window.location = "edit_wine.php";
}

function addCategory () {
  editCategory("");
}

function editCategory (entityID) {
  openInnerWindow("edit_entity.php", 300, "", "entity=category&entity_ID=" + entityID);
}

function addFruit () {
  openInnerWindow("add_entity.php", 300, "", "entity=fruit");
}

function addColor () {
  openInnerWindow("add_entity.php", 300, "", "entity=color");
}

function editWine (wineID) {
  window.location = "edit_wine.php?wine_ID=" + wineID;
/*
  httpRequest = new HttpRequest("get_wine_form.php", editWineCallback);
  var params = "wine_ID=" + wineID;
  httpRequest.post(params);
*/
}

function populateEndDate () {
  var date = getElement("date");
  var endDate = getElement("end_date");
  if (endDate.value == "")
    endDate.value = date.value;
}

function saveEvent () {
  var form = getElement("event_form");
  form.submit();
/*
  httpRequest = new HttpRequest("save_event.php", saveEventCallback);
  var eventID = getElement("event_ID");
  var title = getElement("title");
  var date = getElement("date");
  var time = getElement("time");
  var amPm = getElement("am_pm");
  var visible = getElement("visible");
  var params = "event_ID=" + eventID.value + "&title=" + title.value + "&description=" + tinyMCE.getContent() + "&date=" + date.value + "&time=" + time.value + "&am_pm=" + amPm.value + "&visible=" + visible.checked;
  httpRequest.post(params);
*/
}

function saveEventCallback () {
  var response = httpRequest.getResponse();
  // console.log(response);
  window.location = "edit_events.php";
}

function deleteEvent (eventID) {
  deleteEntity("event", eventID, deleteEventCallback);
}

function deleteEventCallback () {
  window.location = "edit_events.php";
}

function saveCopy () {
  httpRequest = new HttpRequest("save_copy.php", saveCopyCallback);
  var page = getElement("page");
  var description = getElement("description");
  var params = "page=" + page.value + "&description=" + tinyMCE.getContent();
  httpRequest.post(params);
}

function saveCopyCallback () {
  var response = httpRequest.getResponse();
  // console.log(response);
}

function saveFeatured () {
  httpRequest = new HttpRequest("featured_copy_script.php", saveFeaturedCallback);
  var wine = getElement("wine_ID");
  var params = "wine_ID=" + wine.value + "&featured_copy=" + tinyMCE.getContent();
  httpRequest.post(params);
}

function saveFeaturedCallback () {
  var response = httpRequest.getResponse();
//  console.log(response);
  var previewDiv = getElement("preview");
  previewDiv.innerHTML = response;
}

function preview () {
  var descriptionInput = getElement("description");
  var previewDiv = getElement("preview");
  previewDiv.innerHTML = tinyMCE.getContent();
}

function addWine () {
  window.location = "edit_wine.php";
}

function editWineCallback () {
  var wineFormContainer = getElement("wine_form_divide");
  var response = httpRequest.getResponse();
  wineFormContainer.innerHTML = response;
}

function addToCart (wineID) {
  var params = "wine_ID=" + wineID;
  openInnerWindow("add_to_cart.php", 400, "", params);
}

function cancelOrder () {
  var quantity = getElement("quantity");
  quantity.value = 0;
  saveAndContinue();
}

function saveAndContinue () {
  save();
  closeInnerWindow();
}

function save (callbackFunction) {
  var callback = callbackFunction;
  if (!callbackFunction)
    callback = saveCallback;
  var wineID = getElement("wine_ID");
  var quantity = getElement("quantity");
  httpRequest = new HttpRequest("add_to_cart_script.php", callback);
  var params = "wine_ID=" + wineID.value + "&quantity=" + quantity.value;
  httpRequest.post(params);
}

function saveCallback () {
  var response = httpRequest.getResponse();
  httpRequest = new HttpRequest("get_cart_linkbar.php", linkBarCallback);
  var params = "";
  httpRequest.post(params);
}

function linkBarCallback () {
  var response = httpRequest.getResponse();
  var cartLinkBar = getElement("cart_linkbar");
  cartLinkBar.innerHTML = response;
}

function quantityChanged (price) {
  var total = getElement("total");
  var quantity = getElement("quantity");
  var discount = getElement("discount");
  var discountValue = 0;
  var totalValue = Number(price) * Number(quantity.value);
  if (Number(quantity.value) >= 12)
    discountValue = totalValue*0.25;
  total.value = "$ " + (Number(totalValue) - Number(discountValue));
  if (quantity.value >= 12)
    discount.value = "$ " + discountValue;
  else discount.value = "";
}

function updateCart () {
  save(updateCartHelper);
}

function updateCartHelper () {
  httpRequest = new HttpRequest("get_cart_contents.php", updateCartCallback);
  var params = "";
  httpRequest.post(params);
}

function updateCartCallback () {
  var response = httpRequest.getResponse();
  var cart = getElement("shopping_cart");
  cart.innerHTML = response;
}

function viewCart () {
  openInnerWindow("get_cart_contents.php", 400, "");
}

function checkOut () {
  openInnerWindow("check_out.php", 600, "");
}

function completeCheckOut () {
  var name = getElement("name");
  var telephone = getElement("telephone");
  var email = getElement("email");
  if (telephone.value == "" && email.value == "") {
    alert("Please enter a telephone or email by which we may contact you.");
    return;
  }
  var params = "name=" + name.value + "&telephone=" + telephone.value + "&email=" + email.value;
  openInnerWindow("complete_checkout.php", 200, 100);
}

/***************/
/* Move Divide */
/***************/
function shiftHorizontal (divName, deltaX, leftStop, rightStop) {
  moveDivide(divName, deltaX, 0, leftStop, rightStop, 0, 0);
}

function shiftVertical (divName, deltaY, topStop, bottomStop) {
  moveDivide(divName, 0, deltaY, 0, 0, topStop, bottomStop);
}

function moveDivide (divName, deltaX, deltaY, leftStop, rightStop, topStop, bottomStop) {
  shiftDivide(divName, deltaX, deltaY, leftStop, rightStop, topStop, bottomStop);
}

function shiftDivide (divName, deltaX, deltaY, leftStop, rightStop, topStop, bottomStop) {
  leftStop = (typeof(leftStop) != "undefined") ? leftStop : 9999999999;
  rightStop = (typeof(rightStop) != "undefined") ? rightStop : -9999999999;
  topStop = (typeof(topStop) != "undefined") ? topStop : 9999999999;
  bottomStop = (typeof(bottomStop) != "undefined") ? bottomStop : -9999999999;
  var divideStyle = getStyleObject(divName);

  if (!divideStyle)
    return;

  var left = divideStyle.left;
  var top = divideStyle.top;
  if (bottomStop == "HEIGHT") {
    var obj = getElement(divName);
    var height;
    if (document.all) {
      var object = getElement(divName);
      height = document.all[divName].offsetHeight;
      height = 700;
    }
    else  {
      height = document.defaultView.getComputedStyle(obj, "").getPropertyValue("height");
      height = height.substring(0, height.length-2);  // Remove trailing "px"
    }
    bottomStop = -1 * (height-200); // 200: Leave some text in the display box when completely scrolled up
  }
  var leftPrime = Number(left.substring(0, left.length-2)) + Number(deltaX);
  var topPrime = Number(top.substring(0, top.length-2)) + Number(deltaY);
  divideStyle.position = "relative";
  // console.log(leftPrime + " vs " + leftStop + ", " + rightStop);
  if (deltaX && leftPrime >= rightStop && leftPrime <= leftStop) {
    // alert(leftPrime); return;
    divideStyle.left = leftPrime + "px";
    // console.log(divideStyle.position + ", left: " + divideStyle.left);
  }
  if (deltaY && topPrime <= topStop && topPrime >= bottomStop) {
    divideStyle.top = topPrime + "px";
  }
}

function moveDivideTo (divName, x, y) {
  var divideStyle = getStyleObject(divName);
  if (!divideStyle)
    return;
  divideStyle.position = "relative";
  divideStyle.left = x + "px";
  divideStyle.top = y + "px";
}

