var $m = function(id) {
  return document.getElementById(id);
};

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var ddparent = 0;

// open hidden layer
function mopen(id) {	
  // cancel close timer
  mcancelclosetime();

  // close old layer
  mclose();


  // get new layer and show it
  ddparent = $m('main-link' + id);
  ddmenuitem = $m('m' + id);
  var left = (id - 1) * 139 + (id - 1) * 3;
  if (id == 6) { left -= 81; }
  if (id == 7) { left -= 223; }
  ddmenuitem.style.left = left + "px";
  ddmenuitem.style.display = 'block';
}

// close showed layer
function mclose() {
  if (ddmenuitem) ddmenuitem.style.display = 'none';
  if (ddparent && !ddparent.getAttribute("rel")) {
    ddparent.className = '';
  }
}

// go close timer
function mclosetime() {
  closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
  if (closetimer) {
    window.clearTimeout(closetimer);
	closetimer = null;
  }
  if (ddparent) {
    ddparent.className = 'sel';
  }
}

// close layer when click-out
document.onclick = mclose; 

function getFormattedDate() {
  var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  var now = new Date();
  return days[now.getDay()] + ' ' + months[now.getMonth()] + ' ' + now.getDate() + ', ' + now.getFullYear();
}

/**
 * Swap the main prodct image
 */
function swapImg(newImg) {
  var mimg = $m('main-img');
  if (mimg) {
    mimg.src = newImg;
  }
}

function switchTab(id) {
  for (var i = 0; i < 3; i++) {
    var title = $m('title' + i);
	var tab = $m('tab' + i);
	if (title && tab) {
	  title.className = (i == id ? "selected" : "");
	  tab.style.display = (i == id ? "block" : "none");
	  if (i == id) {
	    title.blur();
	  }
	}
  }
}

function emailPage(t) {
  var script = "I thought you might be interested in " + t + ".\n\nYou can view it at\n" + window.location;
  var subject = encodeURI(t + " from Mower Magic");
  window.location.href = 'mailto:?subject=' + subject + '&body=' + encodeURI(script);
}

function printPage() {
  window.print();
}

String.prototype.trim = function() { 
  return this.replace(/^\s+|\s+$b/, '');
};

function updateProductPrice(theForm, productPrice) {
  var p = $m('main-product-price');
  var total = 0;
  if (p && theForm) {
    var elementCount = theForm.elements.length;
	for (var i = 0; i < elementCount; i++) {
	  if (theForm.elements[i].type == "checkbox" && theForm.elements[i].getAttribute("rel") && theForm.elements[i].checked) {
	    var pr = theForm.elements[i].getAttribute("rel");//.replace(/\./, "");
	    total += parseFloat(pr);
	  }
	}
  }
  
  total += productPrice;
total = total.toString();
if (total.indexOf(".") == -1) total += ".00";
    else if (total.length == 3 && total.indexOf(".") != -1) total += "0";
	if (total.indexOf(".") == total.length - 2) total += "0";

if (total.substring(total.indexOf(".") + 1).length > 2) {
  total = total.substring(0, total.indexOf(".") + 3);
}

  p.innerHTML = "&pound;" + total;
}

function gotoforgot(frm) {
  frm.action="http://www.mowermagic.co.uk/helpdesk/forgot_password.php";
  frm.submit();
}