//$Header: /export/www/htdocsdev/columbiafunds/efp/RCS/initefp.js,v 1.3 2008/04/01 17:34:24 eric Exp $
    //global strings that can be set by external validation routine;
    //  non-empty value for errors2 will print & return false (fail);

    //return value of checked radio button
function tgetradioval(s) {
    for (var i=0; i<s.length; ++i) {
	if (s[i].checked) return s[i].value;
    }
    return (-1);
}

function getnum(v) {
    if (isNaN(v) || v == null || parseInt(v) == 0) {
	return 0;
    } else {
	return parseInt(v);
    }
}

function nonnegval(v) {
    if (isNaN(v) || v == null || parseInt(v) <= 0) {
	return false;
    } else {
	return true;
    }
}

function count_chars(e, ch) {
    var ch_count = 0;
    var temp;

    for (var i=0; i<e.value.length; i++) {
	temp = "" + e.value.substring(i, i+1);
	if (temp == ch) {
	     ch_count++;
	}
    }

    return ch_count;
}

function chk_str(e) {
	var valid = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var temp;
	var badchars = "";

	valid += e.addchars;
	for (var i=0; i<e.value.length; i++) {
	    temp = "" + e.value.substring(i, i+1);
	    if (valid.indexOf(temp) == "-1") {
		badchars += e.value.substring(i, i+1)
	    }
	}

	if (badchars != "") {
	  return "'" + e.ename + "' may not include <"+ badchars +">\n";
	} else {
	  return "";
	}
}

function chk_num(val, add_chars, sname) {
	var valid = ".0123456789";
	var temp;
	var badchars = "";

	valid += add_chars;
	for (var i=0; i<val.length; i++) {
	    temp = "" + val.substring(i, i+1);
	    if (valid.indexOf(temp) == "-1") {
		badchars += val.substring(i, i+1)
	    }
	}

	if (badchars != "") {
	  return "'" + sname + "' may not include <"+ badchars +">\n";
	} else {
	    return "";
	}
}

function chk_decimal(e, sname) {

var ret = "";
decallowed = 2;  // how many decimals are allowed?

if (isNaN(e.value) || e.value == "") {
	ret = "'" + sname + "' is not a valid number";
}
else {
	if (e.value.indexOf('.') == -1) {
		e.value += ".";
	}
	dectext = e.value.substring(e.value.indexOf('.')+1, e.value.length);

	if (dectext.length > decallowed)
	{
		ret = "'" + sname + "' must have " + decallowed + " or fewer digits after decimal point";
	}
}
return ret;
}

	// remove chars in schars from value of f
function strip_str (f, schars) {
	s = f.value;
	var i;
	var ret = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (schars.indexOf(c) == -1) ret += c;
	}
	return ret;
}

function check_amount_field(e, fstr, cn_loc) {
    var tmpval = "";
    var fname = fldname(e);
    var estr = "";
    var e1 = "'" + fstr +", child "+fname.substring(cn_loc, cn_loc+1)+"'";
    if (count_chars(e, "$") > 1) {
	tmpval = e1 + " may not have more than one dollar sign\n";
	estr += tmpval;
    }
    if (count_chars(e, ".") > 1) {
	tmpval = e1 + " may not have more than one decimal sign\n";
	estr += tmpval;
    }
    if (tmpval == "") {
	e.value = strip_str(e, "$,");
	tmpval = chk_num(e.value, "", e1);
	estr += tmpval;
    } else {			// don't change input, easier to fix
	t = strip_str(e, "$,");
	tmpval = chk_num(t, "", e1);
	estr += tmpval;
    }
    return estr;
}

    //return value of SINGLE selected option
function getselectval(s) {
    for (var i=0; i<s.length; ++i) {
	if (s[i].selected) return s[i].value;
    }
    return (-1);
}

    // A utility function that returns true if a string contains only 
    // whitespace characters.
function isblank(s) {
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

    // Do some rudimentary checking on an email address
function check_efp_email(s) {
    disallowed = "()<>,;:\\\"[ ]"
    txt = s.value;
    if (txt.indexOf("@") < 3) {
	return false;
    }
    for (i=0; i<12; ++i)
	if (txt.indexOf(disallowed.substring(i,i+1)) != -1)
	    return false;
    if ((txt.indexOf(".com") < 5)
	    && (txt.indexOf(".org") < 5)
	    && (txt.indexOf(".gov") < 5)
	    && (txt.indexOf(".net") < 5)
	    && (txt.indexOf(".mil") < 5)
	    && (txt.indexOf(".int") < 5)
	    && (txt.indexOf(".edu") < 5)) {
	return false;
    }
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s.value)){
	return (true);
    } else {
	return false;
    }
    return true;
}   //check_efp_email

    // Validation for a birthdate; set s.bdate=true to trigger this check
    // Set var.minage and var.maxage to control range of acceptable dates

    // This is the function that performs initialization and product-specific
    // validations. It will be invoked
    // from the onSubmit() event handler. The handler should return whatever
    // value this function returns.
function initefp(f)
{
    var msg;
    var empty_fields = "";
    var tmpval = "";
//    var num_essential_child_vbls = 5;	// includes both parts of radio button
    var num_essential_child_vbls = 3;	// no radio button for now
    var child1_count = 0;
    var child2_count = 0;
    var child3_count = 0;
    var child4_count = 0;
    var child5_count = 0;
    var child1_oth_count = 0;
    var child2_oth_count = 0;
    var child3_oth_count = 0;
    var child4_oth_count = 0;
    var child5_oth_count = 0;

    var child_count = Array();
    var child_oth_count = Array();
    var ch_age = Array();
    var ch_start_age = Array();
    var is_child_vbl = Array();
    var max_children = 4;
    var min_children = 1;
    var num_children = 0;

    errors2 = "";
	// begin field initializations
    f.email.required = true;
    f.email.email = true;
	// end field initializations
    for (j = 1; j <= max_children; j++) {
	child_count[j] = child_oth_count[j] = 0;
    }

    // Loop through the elements of the form.
    // Catenate an error message string for fields that are wrong.
    for (var i = 0; i < f.length; i++) {
        var e = f.elements[i];
	var fname = fldname(e);
	tmpval = "";
	is_missing = 1;

        if (e.type == "text" || e.type == "textarea") {
		// check for empty field
	    is_missing = (e.value == null || e.value == "" || isblank(e.value));
	    if (is_missing) {
                continue;		//proceed regardless
            }

//	    if (e.email) {
//		if (check_efp_email(e) == false) {
//		    errors2 += fname + " is invalid.\n";
//		}
//	    }

	    if (e.addchars != null && e.addchars != "") {
		errors2 += chk_str(e);
	    }
	    

	} else if (e.type == "select-one") {
	    tmpval = getselectval(e.options);
		// check for empty field
	    is_missing = (tmpval == -1 || tmpval == "");
	} //end text check

	if (fname.substring(0, 17) == "Client name short") {
	    e.value = strip_str(e, '\"');
	}

		// don't know how to give array members attributes, do by name
	if (fname.substring(0, 10) == "Child cost") {
	    errors2 += check_amount_field(e, "Annual Cost", 10 + 1);
	}
	if (fname.substring(0, 13) == "Child savings") {
	    errors2 += check_amount_field(e, "Amount saved", 13 + 1);
	}
	if (fname.substring(0, 13) == "Child monthly") {
	    errors2 += check_amount_field(e, "Planned Savings", 13 + 1);
	}

	is_essential = false;
	for (j = 1; j <= max_children; j++) {
	  is_child_vbl[j] = false;
	}
	if (fname.substring(0, 10) == "Child name") {
	    is_child_vbl[parseInt(fname.substring(11, 12))] = true;
	    is_essential = true;
	}
//	if (fname.substring(0,  9) == "Child sex") {
//	    is_child_vbl[parseInt(fname.substring(10, 11))] = true;
//	    is_essential = true;
//	}
	if (fname.substring(0, 9) == "Child age") {
	    is_child_vbl[parseInt(fname.substring(10, 11))] = true;
	    is_essential = true;
	    if (isNaN(e.value)) {
		errors2 += "'Current Age, child " + fname.substring(10, 11) + "' is not a number\n";
	    } else if (getval(e) < 0) {
		errors2 += "'Current Age, child " + fname.substring(10, 11) + "' is less than zero\n";
	    } else {
		s = fname.substring(10, 11);
		tmpval = getnum(fname.substring(10, 11));
		ch_age[tmpval] = e.value;
	    }
	}
	if (fname.substring(0, 15) == "Child start age") {
	    is_child_vbl[parseInt(fname.substring(16, 17))] = true;
	    is_essential = true;
	    if (isNaN(e.value)) {
		errors2 += "'Current Age, child " + fname.substring(16, 17) + "' is not a number\n";
	    } else if (getval(e) < 0) {
		errors2 += "'Current Age, child " + fname.substring(16, 17) + "' is less than zero\n";
	    } else {
		tmpval = getnum(fname.substring(16, 17));
		ch_start_age[tmpval] = eval(e.value);
	    }
	}

	if (!is_missing) {
	    for (j = 1; j <= max_children; j++) {
		if (is_child_vbl[j] && is_essential) {
		    child_count[j] = child_count[j] + 1;
		} else if (is_child_vbl[j]) {
		    child_oth_count[j] = child_oth_count[j] + 1;
		}
	    }
	}
    }//end loop for each vbl

	// cross-variable checks
    for (j = 1; j <= max_children; j++) {
	if (nonnegval(ch_start_age[j]) && nonnegval(ch_age[j]) && ch_start_age[j] <= ch_age[j]) {
	    errors2 += "Child "+j+" College Start Age is not greater than Current Age\n";
	}

	    // if default start age is only vbl filled in, delete
	if (child_count[j] == 1 && ch_start_age[j] == 18) {
	    child_count[j] = 0;
	}

	if ((child_count[j] > 0 || child_oth_count[j] > 0)
		&& (child_count[j] < num_essential_child_vbls)) {
//	    errors2 += "Name, sex, age and college start age for child #" + j + " must all be filled in.\n";
	    errors2 += "Name, age and college start age for child #" + j + " must all be filled in.\n";
	}

	    // last after possible adjustments
	if (child_count[j] > 0) {
	    num_children++;
	}
    }

    if (num_children <= 0) {
	errors2 += "At least one child's information must be filled in.\n";
    }

}  // initefp
