	
	$(document).ready(function () {

		//transitions
		//for more transition, goto http://gsgd.co.uk/sandbox/jquery/easing/
		var style = 'easeOutElastic';
		
		if($('#lava li.selected').attr("name")){
		//Retrieve the selected item position and width
		var default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
		var default_width = $('#lava li.selected').width();
		$('#box').css({display: "block"});
		$('#box .head').css({display: "block"});
		}
		else{
		//Retrieve the selected item position and width
		var default_left = 0;
		var default_width = 0;
		$('#box').css({display: "none"});
		$('#box .head').css({display: "none"});
		}


		//Set the floating bar position and width
		$('#box').css({left: default_left});
		$('#box .head').css({width: default_width});
		//if mouseover the menu item
		$('#lava li').hover(function () {
			$('#box').css({display: "block"});
			$('#box .head').css({display: "block"});
			
			//Get the position and width of the menu item
			left = Math.round($(this).offset().left - $('#lava').offset().left);
			width = $(this).width(); 

			//Set the floating bar position, width and transition
			$('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});	
			$('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});	
		
		//if user click on the menu
		}).click(function () {
			
			//reset the selected item
			$('#lava li').removeClass('selected');	
			
			//select the current item
			$(this).addClass('selected');
	
		});
		
		//If the mouse leave the menu, reset the floating bar to the selected item
		$('#lava').mouseleave(function () {
		if($('#lava li.selected').attr("name")){

			//Retrieve the selected item position and width
			default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
			default_width = $('#lava li.selected').width();
			
			//Set the floating bar position, width and transition
			$('#box').stop(false, true).animate({left: default_left},{duration:1500, easing: style});	
			$('#box .head').stop(false, true).animate({width:default_width},{duration:1500, easing: style});		
		}
		});
		
	});








//check that all fields with classname 'required' are filled in
function validateForm(formname){
var elems=eval("document."+formname+".elements"); 
var valid=true;
for(var i=0;i<elems.length;i++){
	if(elems[i].className=='required'){
	switch(elems[i].type) {
			case "text" :
				if(elems[i].name=='email_address'){
					valid=isEmail(elems[i].value);
				}
			case "password" :
				if(elems[i].value==""){
					valid=false;
					alert("Please fill in all required fields");
				}
				break;
			case "checkbox" :
				valid = isChecked(formname, elems[i].name);
				break;
			case "radio" :
				valid = isSelected(formname, elems[i].name);
				break;
			case "select-one" :
				if(elems[i].value==''){
					alert("Please select an option from the dropdowns");
					valid=false;
				}
		}
		if(!valid){
			return valid;
		}
	}
}
return true;
}


/** isChecked
 * returns true if at least one of a group of checkboxes is checked
 **/
function isChecked(formname, checkboxname){
	var isChecked = false;
	var chkbx=eval('document.'+formname+'.'+checkboxname);
	if(!isArray(chkbx)){
		if (chkbx.checked) 
   	   	isChecked = true;

	}
	else{
	for (var i = 0; i < chkbx.length; i++) {
  	 	if (chkbx[i].checked) {
   	   	isChecked = true;
   		}
	}
	}
	if(!isChecked)
		alert("Please tick all required checkboxes");
	return isChecked;
}

/** isSelected()
 * Returns true if one of a group of radio buttons is selected
 **/
function isSelected(formname, radioname){
	var isChecked = false;
	var chkbx=eval('document.'+formname+'.'+radioname);
	if(!chkbx.length){
		if(chkbx.checked)
			isChecked=true;
	}
	else{
		for (var i = 0; i < chkbx.length; i++) {
			if (chkbx[i].checked) {
			isChecked = true;
			}
		}
	}
	if(!isChecked)
		alert("Please tick all required Radio buttons");
	return isChecked;
}

function isEmail(str){
	if((str.indexOf('@')==-1)||(str.indexOf('.')==-1)){
		alert("Invalid email address");
		return false;
	}
	else
		return true;
}

function isArray(obj) {
return (obj.constructor.toString().indexOf("Array") != -1);
}
