/*
    ------------------------------------------------------
 	FILE:	/js/func.js
 	------------------------------------------------------
 	AUTHOR:	Brendon Ryniker - Standard Systems
 	EMAIL:	brendon@actrix.co.nz
 	DATE:	Fri Mar 31 16:26:59 2006
 	------------------------------------------------------
 	APPLICATION: totaralowlands.co.nz
 	------------------------------------------------------
 	DEPENDANCIES:
 	------------------------------------------------------
 	FUNCTIONALITY:

	* mouseover img-swap routine for navigation-gui	
	* validation/field incrementer for ordering
	* validation for contact form
	
 	------------------------------------------------------
*/



var Navs = ['home',
			'about',
			'garden',
			'orchard',
			'cherries',
			'hazelnuts',
			'bees',
			'berryfruit',
			'gifts',
			'recipes',
			'contact',
			'products'];

var Imgs = new Array();
var Loc  = 'images/navigation/';

if (document.images) {

	function Img(name) {

		// loc variable set on per-page basis to identify
		// which nav button should be 'live'
		var suff    = ''; 
		
		// Object encapsulating navigation images:			
		this.name   = name;
		this.fname  = 'icon-' + name;
		this.state  = 0;
		this.isLive = function() {return this.state == 0 ? false : true;};
		this.swap   = function() {return this.state = this.isLive() ? 0 : 1};		
		this.src    = function() {return Loc + this.fname + (this.isLive() ? '-on' : suff) + '.jpg';};

		// Preload required images:
		this.std      = new Image();
		this.std.src  = Loc + this.fname + suff + '.jpg';
	}

// Preload navigation images:	
// ===========================	
	for (var i = 0; i < Navs.length; i++) { Imgs[Navs[i]] = new Img(Navs[i]); }
// ===========================

}

// ===========================
	function subst(ord) {
// ===========================
	
//	Switch given navigation image:
		
		if (document.images) {

			var obj = Imgs[ord];
			obj.swap();		
			document['navimg' + obj.name].src = obj.src();
		}
	}

// ===========================
	function errf(err) {
// ===========================

		return "The form you submitted has the following error(s).\n" +
			"Please correct them and try again.\n\n" + err;
	}


// ===========================
	function chkOrder(frm) {
// ===========================
	
//	Initial validation on product order submission

		// Different treatment if submitting products.html form:
		var from_prod = (loc == 'products')? true : false;
		
		var els   = frm.elements;
		var err   = '';
		var start = from_prod ? 0 : 3;

		if (!from_prod) {
			
			if (!els[0].value.match(/[a-zA-Z]+/)) {
				err = "* You must enter your name\n\n";
			}
			if (!(els[1].value.match(/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]/)) || !(els[2].value.match(/[a-zA-Z]+/))) {

				err = err + "* You must enter a valid email address and a delivery address\n\n";
			}
		}

		for (var i = start; i < els.length; i++){

			if (els[i].type && (els[i].type == 'text')) {

				if (els[i].value != '') {

					if (!els[i].value.match(/^\s*[0-9]+\s*$/)) {

						alert(errf(err + '* "' + els[i].value + '" is not a number'));
						return false; 
					}
					else {

						if(els[i].value != 0) {

							if (err) {

								alert(errf(err));
								return false;
							}
							else {
								return true;
							}
						}
						}
				}
			}
		}
		alert(errf(err + '* You must enter a quantity value for ' +
				   'at least one item to submit this form'));
		
		return false;
	}

// ===========================
	function chkComment(frm) {
// ===========================
	
//	Validation for contact.html

		if (frm.elements[2].value != '') {

			if (!frm.elements[1].value.match(/[a-zA-Z]+@\w+\.\w/)) {

				return(confirm('You do not appear to have entered a valid email address. Continue?'));
			}
			
			return true;
		}
		alert('You must enter a message for this to be worthwhile');
		return false;
	}

// ===========================		
	function hitInc(id) {
// ===========================
	
//	Increment an order page field

		var fld = document.forms[0].elements[id]
		var val = fld.value || 0;
		fld.value = ++val;
	}

// ===========================
	function hitDec(ord) {
// ===========================
		
//	Decrement an order page field

		var fld = document.forms[0].elements[ord];
		var val = fld.value || 0;
		if (val == 0) {return}
		fld.value = --val;
	}



