// Field Validator Functions
var validators = {
		text:function(n){ 		return !/^\s*$/g.test(n.value) },
		telephone:function(n){ 	return /^\s*[\d]{2,8}\s*(([\d]{2,})\s*)+\s*$/.test(n.value) },
		email:function(n){ 		return /^[A-Za-z0-9._%-]{3,}@[A-Za-z0-9.-]{3,}\.[A-Za-z]{2,6}$/.test(n.value)},
		number:function(n) { 	return /^\s*\d+\s*$/.test(n.value) },
		noblank:function(n) { 	return !/^\s*$/.test(n.value) },
		checked:function(n) {	return n.checked==true;	}
}
// Form validator
var Validate = function(){}
Validate.prototype = {

	fields:{},
	alerts:[],
	valid:true,
	attempt:function(formName)
	{
		formName = formName || document.forms[0];
		var me = this;
		//alert(typeof(formName));
		var f = typeof(formName) == 'string' ? document.forms[formName] : formName;
		var elements = f.elements;
		//alert(elements.length)
		for( var a = 0 ; a < elements.length; a++ ) {

			if( elements[a].type != 'submit'  ) {
				//alert("current:"+elements[a].type + " " + elements[a].id);
				var el = elements[a];

				try {
					if( this.fields[el.id] ) {
							if( !validators[this.fields[el.id][0]](el) ) {
								//alert(el.id+ " " + validators[this.fields[el.id][0]](el));
								this.valid = false;
								this.hint(el,true);
								if ( this.fields[el.id][1] != '') this.alerts.push(this.fields[el.id][1]);
							} else {
								this.hint(el,false);
							}


					}
				} catch(e) {
					//alert('error:'+e);
					continue;
				}

			}
		}

		return this.valid;
	},
	alertString:function(){
		return (this.alerts.length>0?'\n '+this.alerts.join('\n '):'')
	},
	hint:function(el,state){
		if( state == true ) {
			if( !/hint/.test(el.className) ) {
				el.className += ' hint';
			}
		}
		else {
			el.className = el.className.replace(/hint/,'');
		}
	}
}
// Submit functions
function onSubmitContact()
{
	var f = new Validate();
	f.fields = {
		name:['text',''],
		email:['email',''],
		subject:['text'],
		message:['text']
	}
	if( !f.attempt('contact-form') )
		alert('Please correct the relevant fields');
	else {
		var data = Form.serialize('contact-form');
		Element.hide('submit');
		Element.update('alert','Sending message...');
		Form.disable('contact-form');

		var ajax = new Ajax.Request(
		'contact.php',	{
					onComplete: function(t){
						Element.show('submit');
						new Effect.Fade('alert');
						Form.reset('contact-form');
						Form.enable('contact-form');
					},
					postBody:data,
					method:'post'
				});



	}

	return false;
}


function toggleDiv(mydivid, thedivid, numofcats, index) {
  
 	 // reset other cat divs
 	 for (var i= 0; i < numofcats; i++) {
 	 
 	 	if (i != index) {
 	 	
 	 		// document.write(mydivid);
 	 		
 	 		var arrow = 'thediv' + i;
 	 		
 	 		var disp = 'mydiv' + i;
 	 
  			document.getElementById(disp).style.display = 'none';
      			document.getElementById(arrow).style.background = '#f1f1f1 url(right_arrow.gif) no-repeat 247px 12px';
      			
      		}

   	}
  
    	if(document.getElementById(mydivid).style.display == 'none'){
    		document.getElementById(mydivid).style.display = 'block';
    		document.getElementById(thedivid).style.background = '#f1f1f1 url(down_arrow.gif) no-repeat 247px 12px';
    		
    	}else{
      		document.getElementById(mydivid).style.display = 'none';
      		document.getElementById(thedivid).style.background = '#f1f1f1 url(right_arrow.gif) no-repeat 247px 12px';
      		
    	}
}

