// page fns
var page = new Page();
page.add(referrer_init);
page.add(shoppingcart_init);
page.add(height_init);

ignoreCheckLogin = false;

// growers form functions
function checkLogin() {
  var form = $("login");
  var ok = true;
  var msg = [];
  var foc = null;
  if (form && !ignoreCheckLogin) {
    if (form.email && !form.email.value) {
      msg[msg.length] = "Please enter your email address.";
      foc = foc ? foc : form.email;
    } 
    if (form.password && !form.password.value) {
      msg[msg.length] = "Please enter your password.";
      foc = foc ? foc : form.password;
    }
    if (msg.length) {
      ok = false;
      alert(msg.join("\n"));
      if (foc) {
        foc.focus();
      }
    }    
  }
  return ok;
}

function checkDetails(ignoreCheckPassword) {
  var form = $("details");
  var ok = true;
  var msg = [];
  var foc = null;
  if (form) {
    if (form.firstName && !form.firstName.value) {
      msg[msg.length] = "Please enter your first name.";
      foc = foc ? foc : form.firstName;
    } 
    if (form.lastName && !form.lastName.value) {
      msg[msg.length] = "Please enter your last name.";
      foc = foc ? foc : form.lastName;
    } 
    if (form.email && !form.email.value) {
      msg[msg.length] = "Please enter your email address.";
      foc = foc ? foc : form.email;
    } 
    if (form.password && !form.password.value && !ignoreCheckPassword) {
      msg[msg.length] = "Please enter your password.";
      foc = foc ? foc : form.password;
    }
    if (form.confirmPassword && !form.confirmPassword.value && !ignoreCheckPassword) {
      msg[msg.length] = "Please confirm your password.";
      foc = foc ? foc : form.confirmPassword;
    }
    if (form.password && form.confirmPassword && form.password.value != form.confirmPassword.value) {
      msg[msg.length] = "Your password and confirmed password do not match.";
      foc = foc ? foc : form.confirmPassword;
    }
    var da_foc = checkAddress(form, "da");
    if (da_foc) {
      msg[msg.length] = "Please enter your delivery address details.";
      foc = foc ? foc : da_foc;
    }
    var ba_foc = checkAddress(form, "ba");
    if (ba_foc) {
      msg[msg.length] = "Please enter your billing address details.";
      foc = foc ? foc : ba_foc;
    }
    if (msg.length) {
      ok = false;
      alert(msg.join("\n"));
      if (foc) {
        foc.focus();
      }
    }    
  }
  return ok;
}

function checkAddress(form, post) {
  var foc = null;
  if (!form["asAbove_"+post] || !form["asAbove_"+post].checked) {
    if (form["name_"+post] && !form["name_"+post].value) {
      foc = foc ? foc : form["name_"+post];
    }
    if (form["street_"+post] && !form["street_"+post].value) {
      foc = foc ? foc : form["street_"+post];
    }
    if (form["townCity_"+post] && !form["townCity_"+post].value) {
      foc = foc ? foc : form["townCity_"+post];
    }
    if (form["state_"+post] && !form["state_"+post].selectedIndex) {
      foc = foc ? foc : form["state_"+post];
    }
    if (form["postCode_"+post] && !form["postCode_"+post].value) {
      foc = foc ? foc : form["postCode_"+post];
    }
    if (form["country_"+post] && !form["country_"+post].value) {
      foc = foc ? foc : form["country_"+post];
    }
  }
  return foc;
}

// growers util functions
function referrer_init() {
  var referrer = $("referrer");
  if (referrer && !referrer.value) referrer.value = document.location;
}

function shoppingcart_init() {
  var cart = $("cart");
  var details = $("details");
  var login = $("login");
  var logout = $("logout");
  var member = $("member");
  var content = $("content");
  var name = readCookie("account");
  
  if (cart) cart.style.visibility = readCookie("shoppingcart") ? "visible" : "hidden";
  if (details) details.style.visibility = name ? "visible" : "hidden";
  
  if (login) login.style.display = name ? "none" : "block";
  if (logout) logout.style.display = name ? "block" : "none";
  if (member) member.innerHTML = name ? name.split("+").join(" ") : "Guest";
  
  if (content && !readCookie("multipleaddresses")) {
    var div = content.getElementsByTagName("TR");
    for (var i = 0; i < div.length; i++) {
      if ("address" == div[i].className) {
        div[i].style.display = "none";
      }
    }
  }
}

function height_init() {
  var right = $("right");  
  var left = $("left");  
  var content = $("content");
  if (left && content) {
    var lh = left.offsetHeight ? left.offsetHeight : left.style.pixelHeight;
    var ch = content.offsetHeight ? content.offsetHeight : content.style.pixelHeight; 
    if (right) {
      right.style.height = Math.max(lh, ch) + "px";
    } else {
      left.style.height = Math.max(lh, ch) + "px";
    }
  }
}

// cookie functions
// http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days)	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
    var expires = "";
  }
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// page object
function Page() {
  var init = [];
  
	this.init = function() {
		for (var i = 0; i < init.length; i++) {
			init[i]();
		}
	}
	
	this.add = function(fn) {
		init[init.length] = fn;
	}
	
	return this;
}

// dhtml fns
function x(obj) {
	var x = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			x += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		x += obj.x;
	return x;
}

function y(obj) {
	var y = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		y += obj.y;
	return y;
}

function isIe() {
	return navigator.userAgent.toLowerCase().indexOf("msie") != -1;
}

function $(id) {
  return document.getElementById(id);
}

