//validation function for registration form
function validateTellUs(email){
	if(document.getElementById('first_name_textbox').value==''){
		alert("Please fill in your name");
		return false
	}
	if(document.getElementById('comment_text_textbox').value==''){
		alert("Please fill in your comment");		
		return false
	}
	if((document.getElementById('age_textbox')) && (document.getElementById('age_textbox').value=='')){
		alert("Please fill in your age");		
		return false
	}
	if(document.getElementById('country_dropdown').value=='--- Please select ---'){
		alert("Please select your country & county");		
		return false
	}
	//if(!validateEmailAddress(email))
	//	return false
	return true;
}


//check that all fields with classname 'required' are filled in
function validate(){
var elems=document.getElementsByTagName('input'); 
for(var i=0;i<elems.length;i++){
	if((elems[i].className=='required')&&(elems[i].value=="")){
		alert("Please fill in all required fields");
		return false;
	}
}
return true;
}


/*** This validation function checks that an email address contains both '@' and '.' **/
function validateEmailAddress(email_address){
var email = document.getElementById(email_address).value;
	if((email.indexOf('@')==-1)||(email.indexOf('.')==-1)){
		alert("Invalid email address");
		return false;
	}
	else
		return true;
}


function submitForm(theform){
	if(validateForm(theform))
	eval("document."+theform+".submit()");
}


//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=checkEmail(elems[i].value);
				}
			case "password" :
				if(elems[i].value==""){
					valid=false;
					alert("Please fill in all required fields");
				}
				break;
			case "select-one" :
				if(elems[i].value==''){
					alert("Please select an option from the dropdowns");
					valid=false;
				}
				break;
			case "radio" :
				valid = isSelected(formname, elems[i].name);
				break;
			case "checkbox" :
				//valid = isChecked(formname, elems[i].name);
				break;

      		}
		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) || (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;
	//if(radioname.substr(radioname.length-2, 2)=="[]"){
	//	radioname = radioname.substr(0, radioname.length-2);
	//}
	var chkbx=eval('document.'+formname+'.'+radioname);

		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;
}


/*** This validation function checks that an email address contains both '@' and '.' **/
function checkEmail(str){
	if((str.indexOf('@')==-1)||(str.indexOf('.')==-1)){
		alert("Invalid email address");
		return false;
	}
	else
		return true;
}

function validateEmailAddress(elem){
var email = document.getElementById(elem).value;
 return isEmail(email);
}

//Check that two fields have the same values, useful for password changes etc
function validateSame(type, retype){
if(document.getElementById(type).value == document.getElementById(retype).value)
	return true;
else{
	return false;
}
}

//Check if this element is an array
function isArray(obj) {
	if(!obj) return false;
return (obj.constructor.toString().indexOf("Array") != -1);
}

