<!--
	// Grabs all elements by their classname, very handy!
	function getElementsByClassName(el){
		if(!el) el = document.getElementsByTagName("body")[0];
		var a = [];
		var els = el.getElementsByTagName("*");
		for(i=0; i < els.length; i++){
			if(els[i].className.indexOf("va") == 1)	a.push(els[i]);
		}
		return a;
	}

	// Define which of the form elements are up for validation
	function getElementsByValidation(arr){
		// Loop through the defined array
		var validationArray = [];
		var v = 0;
		for(i=0;i<arr.length;i++){
			classes = arr[i].className.split(" ");
			// Search for the class with 'val' which indicates our validation identifier
			for(j=0;j<classes.length;j++){
				// If we find it...
				if(classes[j].indexOf("val") == 1){ //----------------------------------------------------Our identifier "val"
					// Append our multi-dimensional array with
					var elValidation = [];
					
					// Exceptions:
					addToArray = true;
					//If the user hasn't selected to enter a message, don't attempt to validate
					if(arr[i].id == "ship_message"){
						if(!document.getElementById('ship_gift').checked){
							addToArray = false;
						}
					}
					
					// If we avoided exception, add to our array
					if(addToArray){
						// The validation criteria
						elValidation[0] = arr[i].id;
						values = classes[j].replace("[val","").replace("]",""); // discard identifier
						values = values.split(",");
						elValidation[1] = values[1];
						elValidation[2] = values[2];
						// Now update our array
						validationArray[v] = elValidation;
						v++;
					}
					
				}
			}
		}
		return validationArray;
	}
	// Start our validation script
	function validate(divs){
		invalidForms = new Array();
		for(d=0;d<divs.length;d++){
			if(divs[d] != "terms"){
				div = document.getElementById(divs[d]);
				// Get all the elements we need to validate and their conditions and pop them in an array
				elementsToValidate = getElementsByValidation(getElementsByClassName(div));
				// Now go through our returned items 1 by 1 and validate!
				if(d > 0){
					if(document.getElementById('validation'+d)){
						document.body.removeChild(document.getElementById('validation'+d));
					}
					validationDiv = document.getElementById('validation0').cloneNode(true);
					document.body.appendChild(validationDiv);
					validationDiv.id = 'validation'+d;
					validationDiv.childNodes[1].id = 'validation_content'+d;
				}else{
					validationDiv = document.getElementById('validation0');
				}
				validationDiv.style.display = "none";
				validationErrors = new Array();
				for(var i=0;i<elementsToValidate.length;i++){
					// Make an object
					el = document.getElementById(elementsToValidate[i][0]);
					// Style
					if(el.type !== "radio")	el.style.backgroundColor = '#FFFFFF';
					valType = parseFloat(elementsToValidate[i][1]);
					ex = elementsToValidate[i][2]; // Extra validation requirements
					if(ex == null) ex = 1;
					// Now run through our regular expression check and return either true or false
					if(!validationParser(el, valType, ex)){
						// Style
						if(el.type !== "radio")	el.style.backgroundColor = '#e9b0b9';
						validationErrors.push(document.getElementById(elementsToValidate[i][0]).title);
					}
				}
				// After we have validated everything, do we display an error or submit?
				if(validationErrors.length > 0){
					// Custom validation error here
					var valContent = document.getElementById('validation_content'+d)
					if(valContent.hasChildNodes()){
						while(valContent.childNodes.length >= 1){
							valContent.removeChild(valContent.firstChild);
						}
					}
					var errorUL = document.createElement('ul');
					valContent.appendChild(errorUL);
					for(var i=0;i<validationErrors.length;i++){
						errorLI = document.createElement('li');
						errorUL.appendChild(errorLI);
						errorLI.innerHTML = validationErrors[i];
					}
					// Show validation div
					validationDiv.style.top = (findPosition(div,'y')-1)+"px";
					validationDiv.style.left = (findPosition(div,'x')+div.offsetWidth)+"px";
					validationDiv.style.display = "block";
					invalidForms.push(d)
				}
			}else{
				// Exception for Terms & Conditions link as it shows a different validation message in design
				terms = document.getElementById('terms');
				div = document.getElementById('validation_terms');
				if(!terms.checked){
					div.style.display = "block";
					return false;
				}else{
					div.style.display = "none";
				}
			}
		}
		if(invalidForms.length > 0){
			return false;
		}else{
			return true;
		}
	}
	// Holds all our regular expressions that evaluate the element
	function validationParser(el, n, ex){
		v = el.value;
		switch(n){
			case 1 : // numeric
				if(v.length < ex) return false;
				return !/[^\d]/.test(v);
				break;
			case 2 : // alpha only or default first name
				if(v == "first name" || v.length < ex) return false;
				return /^[a-zA-Z ]+$/.test(v);
				break;
			case 3 : // alpha only or default family name
				if(v == "family name" || v.length < ex) return false;
				return /^[a-zA-Z ]+$/.test(v);
				break;	
			// case 3: // alphanumeric
			//	if(v.length < ex) return false;
			//	return /^[a-zA-Z0-9_-]+$/.test(v);
			//	break;
			case 4: // emailaddress
				return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v);
				break;
			case 5: // not-empty or default
				if(v != "tell us why you should win in 25 words or less" && v.length > 0){
					return true;
				}else{
					return false;
				}
				break;
			case 6: // one-required
				var userHasSelected = [];
				var p = el.parentNode;
				var options = p.getElementsByTagName('input');
				for(var x=0;x<options.length;x++){
					if(options[x].checked) userHasSelected.push('found');
				}
				if(userHasSelected.indexOf('found') >= 0) return true;
				break;
			case 7: // selected
				return (el.checked);
				break;
			case 8: // not-empty
				if(v.length > 0){
					return true;
				}else{
					return false;
				}
				break;
			case 9: // DOB day
			//alert('ex:'+ex);
			//alert('v: '+v);
			if(ex==1){
				if (v == "day"){
						return false;
					}
				}
			if(ex==1){
				if (v == "month"){
						return false;
					}
				}
			if(ex==1){
				if (v == "year"){
						return false;
					}
				}
				return true;  // date is valid
				break;
			default:
				break;
		}
	}

	// Get the position of our element (for the love of ie6)
	function findPosition(el,pos) {
		if(typeof(el.offsetParent) != 'undefined') {
			for(var posX = 0, posY = 0; el; el = el.offsetParent) {
				posX += el.offsetLeft;
				posY += el.offsetTop;
			}
			if(pos == "x"){ return posX; }else{ return posY };
		} else {
			if(pos == "x"){ return el.x; }else{ return el.y };
		}
	}
	function checkAgeCheckBox(d,m,y){
		//Shorthand vars
		var year = parseInt(y);
		var month = m - 1;
		var day = d;
		var date1 = new Date((year + 16), month, day);
		var now = new Date;
		//Are they old enough?
		if ((now.getTime() - date1.getTime()) < 0) {
			return false;
		} else {
			return true;
		}
	}
	function hideAllVal(){
		for(d=0;d<5;d++){
			if(document.getElementById('validation'+d)){
				document.getElementById('validation'+d).style.display = "none";
			}
		}
	}
//-->
