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 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;
}

function resetColor(field){
	document.getElementById(field).style.backgroundColor='#ffffff';
}

function checkCaptcha(){
	if(document.getElementById('cap_text').value== '' && nocaptcha != '1'){
		setColor('cap_text');
		alert('Please fill in the anti-spam word, then submit the form again');
		document.getElementById('cap_text').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';
}

// sets card panel background color from color picker
function setPanel(){
	var bcol = document.getElementById('bcolor').value;
	document.getElementById('cardpanel').style.backgroundColor = bcol;
}

// checks send date in createcard.php
function checkdate(){
	var sendDate = document.getElementById('send_year').value + '/' + document.getElementById('send_month').value + '/' + document.getElementById('send_day').value;
	sendDate = new Date(Date.parse(sendDate.replace(/-/g,' ')))
	today = new Date();
	today.setHours(0,0,0,0)
	if (sendDate < today) {
		alert('The send date can\'t be in the past. Please pick another date.');
		setColor('sdate');
		return false;
	} 
}

// 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';
		}
}

/* removes extra recipients */
function clearRecipient(field){
	var nfield = 'to_name_' + field;
	var mfield = 'to_mail_' + field;
	var rfield = 'recip' + field;
	document.getElementById(nfield).value='';
	document.getElementById(mfield).value='';
	document.getElementById(rfield).style.display='none';
	recip_count--;
}



