/**
 * jillford.js
 * main Javascript file for jillford.com
 * contains functions to add items to an order (stored in a cookie)
 * @package jillford.com
 */

/**
 * @var prices object - this is generated by PHP and added to order form
 */
var prices = false;

/**
 * initChecklists
 * initialises scrollable checklists
 */
function initChecklist() {
	if (document.all && document.getElementById) {
		// Get all unordered lists
		var lists = document.getElementsByTagName("ul");
		for (i = 0; i < lists.length; i++) {
			var theList = lists[i];
			// Only work with those having the class "checklist"
			if (theList.className.indexOf("checklist") > -1) {
				var labels = theList.getElementsByTagName("label");
				// Assign event handlers to labels within
				for (var j = 0; j < labels.length; j++) {
					var theLabel = labels[j];
					theLabel.onmouseover = function() { this.className += " hover"; };
					theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
				}
			}
		}
	}
}
/**
 * showChecklist
 */
function showChecklist(id)
{
}
/**
 * update_total
 * updates the subtotal for a piece, p+p, and grand total on the contact form
 * @param string product code
 */
function update_total(which)
{
    var a = arguments;
    var which = a[0]||false;
    var update_cookie = a[1]||true;
    var qty = document.getElementById('quantity_'+which);
    var st = parseInt(qty.value) * prices[which];
    var st_hidden = document.getElementById('subtotal_'+which);
    var st_display = document.getElementById('subtotal_display_'+which);
    var sfx, st_txt, t_txt;
    if (!st) {
        qty.value = "";
        st_hidden.value = 0;
        st_display.value = "";
    } else {
        st_hidden.value = st;
        st_txt = st + '';
        sfx = (st_txt.indexOf(".") == -1)? ".00": "0";
        st_display.value = st + sfx;
    }
    if (update_cookie) {
        updateItem(which, parseInt(qty.value));
    }
    var total = 0;
    var subtotal = 0;
    for(items in prices) {
        subtotal = document.getElementById('subtotal_'+items).value;
        total += (subtotal * 1);
    }
    var totalval = "";
		var subtotalval = "";
		var pnpval = "";
    if (total > 0) {
		    // postage and packing
        pnp = Math.floor(total/10);
        p_txt = pnp + '';
        pfx = (p_txt.indexOf(".") == -1)? ".00": "0";
				pnpval = p_txt + pfx;
				// sub-total
        st_txt = total + '';
        stfx = (st_txt.indexOf(".") == -1)? ".00": "0";
        subtotalval = st_txt + stfx;
        // total
				t_txt = (total+Math.floor(total/10)) + '';
        sfx = (t_txt.indexOf(".") == -1)? ".00": "0";
        totalval = t_txt + sfx;
    }
    document.getElementById('subtotal').value = subtotalval;
    document.getElementById('pnp').value = pnpval;
    document.getElementById('total').value = totalval;
}
/**
 * fillForm
 * pre-fills the order form with any items which have been added in this session
 */
function fillForm()
{
		if (prices)
		{
				for (itm in prices) {
            var items = getCookie(itm);
            if (items) {
                items = parseInt(items);
								var qty = document.getElementById('quantity_' + itm);
                qty.value = items;
                update_total(itm, false);
            }
        }
		}
}
window.onload = function() {
    // fill the form when page loads
    fillForm();
	// replace titles
    if (typeof sIFR == "function") {
		sIFR.replaceElement(named({sSelector:".titletext", sFlashSrc: "/js/sifr/scribble.swf", sColor: "#444333"}));
		sIFR.replaceElement(named({sSelector:".titletext-centred", sFlashSrc: "/js/sifr/scribble.swf", sColor: "#444333", sFlashVars: "textalign=center"}));
		sIFR.replaceElement(named({sSelector:"#sidebar>h2", sFlashSrc: "/js/sifr/scribble.swf", sColor: "#444333"}));
    }
}
		

/**
 * addItem
 * adds a product to the basket
 * @param string item code
 */
function addItem(which)
{
    //var now = new Date();
    //fixDate(now);
    //now.setTime(now.getTime() + 60 * 60 * 1000);
    var items = getCookie(which);
    if (items==null||isNaN(items)) {
        items = 1;
        alert("This item has been added to your order");
    } else {
        items = parseInt(items) + 1;
        alert("You now have " + items + " of these items in your order");
    }
    updateItem(which, items);
}
/**
 * updateItem
 * updates the quantity of a particular piece in the basket
 */
function updateItem(which, value)
{
    //var now = new Date();
    //fixDate(now);
    //now.setTime(now.getTime() + 60 * 60 * 1000);
    deleteCookie(which);
    setCookie(which, value);
}
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  path = "/";
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
function productDropDown(listID)
{
    var list = document.getElementById(listID);
		list.style.display = (list.style.display == 'block')? 'none': 'block';
}

