
var form = {
	check : function($form, $fields) {

		$check = Array();
		for(i=0; i<$fields.length; i++) {
			$field_orig = $fields[i];
			$field = $field_orig.toLowerCase();
			$f = document.forms[$form];
			$elem = $f.elements[$field_orig];
			$value = $elem.value;
			$default = $elem.defaultValue;
			$length = 1;
			$f.elements[$field_orig].className = "textbox";

			if($field.indexOf("email") != -1) {
				if(!this.email($form, $field_orig)) {
					$check.push($field_orig);
					$f.elements[$field_orig].className = "required";	
					continue;
				}
			}
			
			if($value.length < $length || $value == $default) {
				$f.elements[$field_orig].className = "required";
				$check.push($field_orig);	
			}
			
		}
		
		if($check.length == 0) {
			return true;	
		}
		else {
			$tocheck = "Please amend the following before submitting:\n";
			for(c=0; c<$check.length; c++) {
				$tocheck +=  $check[c]+" \n";
			}
			alert($tocheck);
			$f.elements[$check[0]].focus();
			return false;	
		}
		
	}
	,
	email : function($form, $field) {
		
		var email = document.forms[$form].elements[$field].value;
		var at_sym = email.indexOf('@');
		var dot = email.lastIndexOf('.');
		var space = email.indexOf(' ');
		var len = email.length;
		
		if (at_sym < 1 || dot < at_sym || len - dot <= 2 || space != -1) {
			return false;
		}
		else { 
			return true; 
		}
		
	}
	,
	values : function($form) {
		
		$f = document.forms[$form];
		out = "";
		
		for(f=0; f<$f.elements.length; f++) {
			elem = $f.elements[f].name;
			value = $f.elements[f].value;
			out += "&"+elem+"="+value;
		}
		
		return out;
		
	}
	,
	prep : function($form) {
		$f = document.forms[$form];
		for (var i=0; i<$f.elements.length; i++) {
			var elem = $f.elements[i];
			if (elem.type == "reset"||elem.type == "submit"||elem.type == "radio"||elem.type == "checkbox") continue;
			if (!elem.defaultValue) continue;
			elem.onfocus = function() {
				if (this.value == this.defaultValue) {
					this.value = "";
				}
			}
			elem.onblur = function() {
				if (this.value == "") {
					this.value = this.defaultValue;
				}
			}
	
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function startList() {
	//alert("Starting list func");
	if (document.all && document.getElementById) {
		navRoot = document.getElementById("nav");
		
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				
				node.onmouseover=function() {
				this.className+=" over";
  				}
 				node.onmouseout=function() {
	 				//alert("Hello russ");
  					this.className=this.className.replace(" over", "");
   				}
  			}
  		}
  		
 	}
 	
}

//addLoadEvent(startList());
window.onload = function() {
	startList();	
}

$$ = function(target) {
	return document.getElementById(target);	
}

function Gallery(target, images) {

	this.target = target;
	this.images = images;
	this.current = 1;
	this.delay = 4000;
	
	//Statics
	this.interval;
	var self;
	
	this.next = function() {
		this.render(this.images[this.current]);
		if(this.current == this.images.length-1) { 
			this.current = 0; 
		}
		else {
			this.current++;	
		}
		
	}
	
	this.previous = function() {
		alert("then "+this.current);
		if(this.current < 0) { 
			this.current = this.images.length; 
		}
		else {
			this.current--;	
		}
		alert("now "+this.current);
		this.render(this.images[this.current]);
	}
	
	this.render = function(imgSource) {
		var imgContainer = $$(this.target);
		if(imgContainer.childNodes.length > 2) {
			imgContainer.removeChild(imgContainer.firstChild);	
		}
		var lastElement = imgContainer.lastChild;
		var newElement = document.createElement("img");
		var id = "image_" + this.current;
		var newImage = new Image();
		newImage.src = imgSource;
		newElement.id = id;
		newElement.src = newImage.src;
		newElement.className = "newImage";
		this.insert(newElement, lastElement);
		this.transition(id);
	}
	
	this.transition = function(target) {
		Effect.Appear(target);
	}
	
	this.insert = function(newElement, targetElement) {
		var parent = targetElement.parentNode;
		if(parent.lastChild == targetElement) {
			parent.appendChild(newElement);
		}
		else {
			parent.insertBefore(newElement, targetElement.nextSibling);	
		}
	}
	
	this.play = function() {
		self = this;
		this.interval = setInterval(handlePlay, this.delay);
	}
	
	function handlePlay() {
		self.next();	
	}
	
	this.stop = function() {
		clearInterval(this.interval);	
	}

}

var imgs = Array("http://www.stevensandcarter.co.uk/gfx/pic1.jpg", "http://www.stevensandcarter.co.uk/gfx/pic2.jpg", "http://www.stevensandcarter.co.uk/gfx/pic3.jpg", "http://www.stevensandcarter.co.uk/gfx/pic4.jpg", "http://www.stevensandcarter.co.uk/gfx/pic5.jpg", "http://www.stevensandcarter.co.uk/gfx/pic6.jpg", "http://www.stevensandcarter.co.uk/gfx/pic7.jpg");
var myGal = new Gallery("holder", imgs);



