function openwin(URL) {
   myWin = window.open(URL,'','width=800,height=520,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no');
}

function isPostCode(entry){ // CANADIAN CODES ONLY
	entry = entry.replace(" ","");
	strlen=entry.length; if (strlen!=6) {return false}
	entry=entry.toUpperCase();    // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if ('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {return false}
	if ('0123456789'.indexOf(entry.charAt(1))<0) {return false}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {return false}
	if ('0123456789'.indexOf(entry.charAt(3))<0) {return false}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {return false}
	if ('0123456789'.indexOf(entry.charAt(5))<0) {return false}
	return true; 
}


function fixPhoneSin(sStr) {
	var validPhonSineChar = "0123456789";
	var phoneSinChar=""; 
	var returnPhoneSinStr="";
	
	for (var i=0; i<sStr.length; i++) {
		 phoneSinChar = sStr.substr(i,1);
		 if (validPhonSineChar.indexOf(phoneSinChar)!=-1) {
			 returnPhoneSinStr += phoneSinChar;
		 }
	}
	return returnPhoneSinStr;
}

function toCapitalize(thisField) {
	thisField.value = (thisField.value.substr(0,1)).toUpperCase() + thisField.value.substr(1);

	var val = thisField.value;
	var newVal = '';
	val = val.split(' ');
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
	}
	thisField.value = newVal.replace(/^\s*|\s*$/g,"");
}

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validateClientInformation(thisForm) {
	if (thisForm.rgt_fname.value=="") {
		alert("First name must be entered.");
		thisForm.rgt_fname.focus();
		return false;
	}
	toCapitalize(thisForm.rgt_fname);

	if (thisForm.rgt_lname.value=="") {
		alert("Last name must be entered.");
		thisForm.rgt_lname.focus();
		return false;
	}
	toCapitalize(thisForm.rgt_lname);
	
	if (thisForm.rgt_email.value!="") {
		if (!isEmailAddr(thisForm.rgt_email.value)) {
			alert("Invalid email address.");
			thisForm.rgt_email.focus();
			return false;
		}
	}
	
	if (thisForm.rgt_home_phone.value=="") {
		alert("Phone number must be entered.");
		thisForm.rgt_home_phone.focus();
		return false;
	}
	
	thisForm.rgt_home_phone.value = fixPhoneSin(thisForm.rgt_home_phone.value)
	if (thisForm.rgt_home_phone.value.length!=10) {
		alert("Invalid phone number.  The phone number should be 10 digits long including the area code.");
		thisForm.rgt_home_phone.focus();
		return false;
	}
	
	if (thisForm.rgt_city.value=="") {
		alert("City must be entered.");
		thisForm.rgt_city.focus();
		return false;
	}
	toCapitalize(thisForm.rgt_city);
	
	if (thisForm.rtg_postalcode.value!="") {
		if (!isPostCode(thisForm.rtg_postalcode.value)) {
			alert("Invalid Postal Code.");
			thisForm.rtg_postalcode.focus();
			return false
		}
	}
	
	if (thisForm.rgt_know_about.value=="") {
		alert("Please answer the question 'How do you know about this event?'");
		thisForm.rgt_know_about.focus();
		return false;
	}

	if (!thisForm.rgt_is_success_member[0].checked && !thisForm.rgt_is_success_member[1].checked) {
		alert("Please answer the question 'Are you a Member of SUCCESS?'")
		thisForm.rgt_is_success_member[0].focus();
		return false
	}
	if (!thisForm.rgt_is_success_emp_client[0].checked && !thisForm.rgt_is_success_emp_client[1].checked) {
		alert("Please answer the question 'Are you receiving Employment Services at SUCCESS?'")
		thisForm.rgt_is_success_emp_client[0].focus();
		return false
	}
	if (!thisForm.rgt_is_student[0].checked && !thisForm.rgt_is_student[1].checked) {
		alert("Please answer the question 'Are you receiving Settlement Services at SUCCESS?'")
		thisForm.rgt_is_success_emp_client[0].focus();
		return false
	}
	if (!thisForm.rgt_is_success_volunteer[0].checked && !thisForm.rgt_is_success_volunteer[1].checked) {
		alert("Please answer the question 'Are you receiving other services at SUCCESS?'")
		thisForm.rgt_is_success_emp_client[0].focus();
		return false
	}
	return true;
}

/*
ensure at least one applicant email is selected before opening the send email page
*/
function email_selected(thisForm) {
	var o = thisForm.elements;
	if (o==null) {	//need to click the apply button
		return false;
	} else {
		for (var i=0; i<o.length; i++) {
			if (o[i].name.indexOf('rgt_bulk_mail_') > -1) {
				if (o[i].checked) {
					return true
				}
			}
		}
	}
	alert('Please select at least one registrant.');
	return false;
}
