
function _val(ctl, type){
	
	// live client-side validation called from onBlur for ssn, phone and zip, date

	var str = ctl.value;
	var out = '';
	var invalid = false;
	var i = 0;

	// quit if there's no input
	if(str.length == 0)
		return;

	// get numbers
	arr = str.match(/[0-9]/g);
	
	// make sure we have an array and the size is right
	if(!arr)
		invalid = true;
	else if(arr.length != 9 && type == 'ssn')
		invalid = true;
	else if((!(arr.length >= 10 && arr.length <= 15)) && type == 'phone')
		invalid = true;
	else if((!(arr.length == 5 || arr.length == 9)) && type == 'zip')
		invalid = true;

	// display error and quit
	if(invalid){
		if(type == 'ssn')
			alert('Invalid Social Security Number');
		else if(type == 'phone')
			alert('Invalid Phone Number');
		else if(type == 'zip')
			alert('Invalid ZIP Code');
		return;
	}

	// reformat output
	if(type == 'ssn')
		for(i = 0; i < arr.length; i++){
			if(i == 3 || i == 5)
				out = out + '-';
			out = out + '' + arr[i];
		}

	// reformat phone
	if(type == 'phone')
		for(i = 0; i < arr.length; i++){
			if(i == 0)
				out = out + '('; 
			else if(i == 3)
				out = out + ')'; 
			else if(i == 6)
				out = out + '-'; 
			else if(i == 10)
				out = out + 'x'; 
			out = out + '' + arr[i];
		}

	// reformat zip
	if(type == 'zip')
		for(i = 0; i < arr.length; i++){
			if(i == 5)
				out = out + '-';
			out = out + '' + arr[i];
		}

	// send value back to input
	ctl.value = out;

}


function _val_date(ctl){

	var str = ctl.value;

	// regexp check format
	str = str.replace(" ", "");

	// skip empty strings
	if(str.length == 0)
		return;

	// regexp validation
	var re_date = /^(\d+)\/(\d+)\/(\d{4})$/;
	if(!re_date.exec(str)){
		alert("Invalid Date Format - Please Format mm/dd/yyyy");
		return;
	}

	// use Date object to validate further
	var day = RegExp.$2;
	var month = (RegExp.$1) - 1;
	var year = RegExp.$3;
	d = new Date(year, month, day);
	if(!(day == d.getDate() && month == d.getMonth() && year == d.getFullYear()))
		alert("Invalid Date - Please Format mm/dd/yyyy");
}

// allow a value to be a number or a token (unknown)
function _val_number_tokens(ctl) {
	var str = ctl.value;
	
	// quit if there's no input
	if(str.length == 0)
		return;
		
	// remove space
	str = str.replace(' ', '');
	
	if(parseInt(str, 10) != str && str != "unknown") {
		alert('Invalid Number or Value');
	}
}

function _val_number(ctl){
	
	var str = ctl.value;

	// quit if there's no input
	if(str.length == 0)
		return;

	// remove space
	str = str.replace(' ', '');

	// match
	if(parseInt(str, 10) != str)
		alert('Invalid Number Value' + " = " + ctl.name);

}


function _word_count(ctl){

	// form name
	var form_name = ctl.form.name;

	// get textarea and count input box names
	var button_name = ctl.name;
	var textarea_name = button_name.replace("button_", "");
	var count_name = "count_" + textarea_name;

	// get input
	var str = "";
	eval("str = document." + form_name + "." + textarea_name + ".value;");
	
	// get count
	arr = str.split(/\s+/);
	if(str.length == 0)
		cnt = 0;
	else if(!arr)
		cnt = 0;
	else
		cnt = arr.length;

	// send count back to the count field
	eval("document." + form_name + "." + count_name + ".disabled = false;");
	eval("document." + form_name + "." + count_name + ".value = " + cnt + ";");
	eval("document." + form_name + "." + count_name + ".disabled = true;");

}

function _popup(pic) {
	window.open("popup.php?" + pic, "_popup", "resizable=1,height=200,width=200");
}

function _any(ctl){
	if(ctl.value == "any")
		ctl.value = "";
}

function _compare(f, staff_or_recipient){

	var	cnt = 0;
	var str = "";

	// count and make list of donors
	for(var i = 0; i < f.elements.length; i++)
		if(f.elements[i].type == "checkbox")
			if(f.elements[i].name == "donor_id[]" && f.elements[i].checked){
				cnt++;
				str = str + "" + f.elements[i].value + ",";
			}

	// quit if not enough are selected
	if(cnt < 2){
		alert("At least two donors must be selected to compare.");
		return;
	}

	// popup compare window
	window.open(staff_or_recipient + "_donor_compare.php?donor_id_list=" + str, "_compare", "resizable=1,height=700,width=700,scrollbars=1,menubar=1,location=1,toolbars=1");

}

function toggleBox(nid,on)
{
  $(nid).toggle();
}

