function setSelectedIndex(list, value){
	for(i=0; i<list.length; i++){
	  if(list[ i].value == value){
	  	list.selectedIndex = i;
	  	break;
	  }
	}
}

function setRadio(radio, value){
   for(i=0; i<radio.length; i++){
      if(radio[i].value == value){
         radio[i].checked = true;
      } else {
         radio[i].checked = false;
      }
   }
}

function checkRegForm(){
	if(document.getElementById('regmail').value== ''){
		setColor('regmail');
		alert('Please fill in your email address, then submit the form again.');
		document.getElementById('regmail').focus();
		return false;
	}
	if( validate('register', 'regmail') == 'invalid' ){
		setColor('regmail');
		alert('The \'To\' email address doesn\'t appear to be valid. Please correct it.');
		document.getElementById('regmail').focus();
		return false;
	}
	if(document.getElementById('cap_text').value== ''){
		setColor('cap_text');
		alert('Please fill in the anti-spam word, then submit the form again.');
		document.getElementById('cap_text').focus();
		return false;
	}

}

function checkLoginForm(){
	if(document.getElementById('username').value== ''){
		setColor('username');
		alert('Please fill in your login email, then submit the form again.');
		document.getElementById('username').focus();
		return false;
	}
	if( validate('login', 'username') == 'invalid' ){
		setColor('username');
		alert('The Login email address doesn\'t appear to be valid. Please correct it.');
		document.getElementById('username').focus();
		return false;
	}
	if(document.getElementById('password').value== ''){
		setColor('password');
		alert('Please fill your password, then submit the form again.');
		document.getElementById('password').focus();
		return false;
	}
}

// color & uncolor form fields
function setColor(field){
	document.getElementById(field).style.backgroundColor='#ffffaa';
}
function resetColor(field){
	document.getElementById(field).style.backgroundColor='#ffffff';
}

// check email format 
function validate(form_id, email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.forms[form_id].elements[email].value;
		if(reg.test(address) == false) {
			return 'invalid';
		}else{
			return 'valid';
		}
}


function textCounter(textarea, counterID, maxLen) {
	cnt = document.getElementById(counterID);
	if (textarea.value.length > maxLen){
		textarea.value = textarea.value.substring(0,maxLen);
	}
	cnt.innerHTML = maxLen - textarea.value.length;
}


