// JavaScript Document
// validate field with alert upon error and clear field
function validate_fix(type,fieldname){
//	alert("enter validate fix");
	var input;
	input=document.getElementById(fieldname).value;
	err_field=fieldname+"_error";
	if (!validate(type,input)){
//		alert("invalid "+type+": '"+input+"'");
		document.getElementById(err_field).style.visibility="visible";
		return false;
	}
	else {
//		alert("valid "+type+": '"+input+"'");
		document.getElementById(err_field).style.visibility="hidden";
		return true;
	}
	return false;
}
function show_field(check_field,value,field_to_show) {
//	alert("enter show match value to match: '"+value+"' value of field: '"+document.getElementById(check_field).value+"'");
//	alert(document.getElementById(field_to_show).value);
	if (document.getElementById(check_field).value==value)
	{
//		alert("have match field value: '"+document.getElementById(field_to_show).value+"'");
		document.getElementById(field_to_show).style.visibility="visible";
	}
	else
	{
//		alert("no match field value: '"+document.getElementById(field_to_show).value+"'");
		document.getElementById(field_to_show).style.visibility="hidden";
	}
	return true;
}
function isdollar(textin) {
	var test_text = new String(textin);
//	alert("original value: '"+test_text+"'");
	if (test_text.charAt(0)=='$') test_text=test_text.slice(1,test_text.length);
//	alert("dollar sign removed: '"+test_text+"'");
	if (isNaN(test_text)) return false;
//	alert("is a number");
	decimal_point=test_text.indexOf('.');
//	alert("decimal position: "+decimal_point+" total string length: "+test_text.length);
	if (decimal_point==-1) return true;
	if (test_text.length-decimal_point<=3) return true;
	return false;
}
function validate(type,input) {
//	alert("validate: '"+input+"'");
	if (isblank(input)) return false;
	if (type=="currency") {
		return isdollar(input);
	}
	return false; // undefined type causes false return
}
function verify_donation() {
	var amt = new String();
	if (document.getElementById("donation").value=="other") {
		if (!validate_fix("currency","other")) {return false;}
		amt=document.getElementById("other").value;
		if (amt.charAt(0)=='$') amt=amt.slice(1,amt.length);
	}
	else {
		amt=document.getElementById("donation").value;
	}
	document.getElementById("amount").value=amt;
	if (document.getElementById("purpose").value=="In memory of") {
		document.getElementById("item_name").value="In memory of "+document.getElementById("memory").value;
	}
	else {
		document.getElementById("item_name").value="General donation";
	}
	return true;
}
function isblank(instr){
	var local = new String(instr);
	for (i=1;i<local.length && local.charAt(i)==' ';i++);
	if (i==local.length) return true;
	return false;
}