// Copyright 2003-2006, Weboom Design & Development, Inc.

function init() {
	form = document.forms['regform'];
	do_form_init();
}

function needs() {
	var target_field = arguments[0];
	var target_field_pv = picked_value(target_field);
	var yes_re = new RegExp('^y','i');

	if ((target_field_pv == '1') || (yes_re.test(target_field_pv))) {
		return true;
	} else {
		return false;
	}
}

function is_picked() {
	var got_a_hit = false;
	var target_string = arguments[0];
	var this_re = new RegExp("\\b" + target_string + "\\b","i");

    for (arg_count = 1;arg_count<arguments.length;arg_count++) {
		var this_field_name = arguments[arg_count];
        var this_field = form[this_field_name];
		if (this_field) {

			if ((this_field.type == "select") || (this_field.type == "select-one") || (this_field.type == "select-multiple")) {
				if (this_field) {
					var selected_words = this_field[this_field.selectedIndex].text;
					if (this_re.test(selected_words)) {got_a_hit = true; break;}
				}
			} else if ((this_field[0] != null) && ((this_field[0].type == "radio") || (this_field[0].type == "checkbox"))) {
				for (var i=0; i<this_field.length; i++)  {
					if (this_field[i].checked) {
						var selected_words = this_field[i].value;
					}
				}

				if (this_re.test(selected_words)) {got_a_hit = true; break;}
			} else if (((this_field.type == "radio") || (this_field.type == "checkbox")) && (this_field.checked == false)) {
				if (this_field.checked) {
					var selected_words = this_field.value;
				}

				if (this_re.test(selected_words)) {got_a_hit = true; break;}
			}

		}
	}

	return got_a_hit;
}

function picked_value() {
	var target_field = arguments[0];
	var this_field = form[target_field]
	var picked_value = '';


	if (this_field) {
		if ((this_field.type == "select") || (this_field.type == "select-one") || (this_field.type == "select-multiple")) {
			return this_field[this_field.selectedIndex].text;
		} else if ((this_field[0] != null) && ((this_field[0].type == "radio") || (this_field[0].type == "checkbox"))) {
			for (var i=0; i<this_field.length; i++)  {
				if (this_field[i].checked) {
					return this_field[i].value;
				}
			}
		} else if ((this_field.type == "radio") || (this_field.type == "checkbox")) {
			if (this_field.checked) {
				return this_field.value;
			}
		} else if ((this_field.type == "text") || (this_field.type == "textarea") || (this_field.type == "hidden")) {
			return this_field.value;
		} else {
			return '';
		}
	} else {
		return '';
	}
}

function picked_index() {
	var target_field = arguments[0];
	var this_field = form[target_field]
	var picked_index = '';

	if ((this_field.type == "select") || (this_field.type == "select-one") || (this_field.type == "select-multiple")) {
		return this_field.selectedIndex;
	} else if ((this_field[0] != null) && ((this_field[0].type == "radio") || (this_field[0].type == "checkbox"))) {
		for (var i=0; i<this_field.length; i++)  {
			if (this_field[i].checked) {
				return i;
			}
		}
	}

	return '0';
}

function fields_on_nogroups() { fields_on_off_nogroups('on', arguments); }
function fields_off_nogroups() { fields_on_off_nogroups('off', arguments); }
function fields_on_off_nogroups() {
	var onoff = arguments[0];
	var targets = arguments[1];

    for (targ_count = 0; targ_count < targets.length; targ_count++) {
		$this_input_reference = n = form[targets[targ_count]];
		if ($this_input_reference) {
			$this_input = $this_input_reference.name;

			if (onoff == 'on') {

				if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
					for (j=0; j < n.length; j++) {
						n[j].disabled = false;
					}
				}

				if ((n.type == "radio") || (n.type == "checkbox")) {
					n.disabled = false;
				}

				if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
					n.disabled = false;
				}

				if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
					n.disabled = false;
				}
			} else if (onoff == 'off') {

				if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
					for (j=0; j < n.length; j++) {
						n[j].checked = false;
						n[j].disabled = true;
					}
				}

				if ((n.type == "radio") || (n.type == "checkbox")) {
					n.checked = false;
					n.disabled = true;
				}

				if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
					n.selectedIndex = 0;
					n.disabled = true;
				}

				if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
					n.value = '';
					n.disabled = true;
				}
			}
		} else {
			continue;
		}

    }
}

function fields_on() { fields_on_off('on', arguments); }
function fields_off() { fields_on_off('off', arguments); }
function fields_on_off() {
	var groups = new Object();
	var fields = new Object();

	var onoff = arguments[0];
	var targets = arguments[1];

    for (targ_count = 0; targ_count < targets.length; targ_count++) {
		if ((targets[targ_count].lastIndexOf('_') + 1) == targets[targ_count].length) {
	        groups[targets[targ_count]] = 1;
		} else {
	        fields[targets[targ_count]] = 1;
		}
	}

	for ($j=0; $j<form.elements.length; $j++) {
		$this_input_reference = n = form.elements[$j];
		$this_input = $this_input_reference.name;
		$this_input_prefix_length = $this_input.indexOf('_') + 1;
		$this_input_group = $this_input.substring(0, $this_input_prefix_length);

		$this_input_prefix_extended_length = $this_input.indexOf('_', $this_input_prefix_length) + 1;
		$this_input_group_extended = $this_input.substring(0, $this_input_prefix_extended_length);

		$this_input_prefix_extended2_length = $this_input.indexOf('_', $this_input_prefix_extended_length) + 1;
		$this_input_group_extended2 = $this_input.substring(0, $this_input_prefix_extended2_length);

		if ((groups[$this_input_group] != null) || (groups[$this_input_group_extended] != null) || (groups[$this_input_group_extended2] != null) || (fields[$this_input] != null)) {
			if (onoff == 'on') {

				if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
					for (j=0; j < n.length; j++) {
						n[j].disabled = false;
					}
				}

				if ((n.type == "radio") || (n.type == "checkbox")) {
					n.disabled = false;
				}

				if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
					n.disabled = false;
				}

				if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
					n.disabled = false;
				}
			} else if (onoff == 'off') {

				if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
					for (j=0; j < n.length; j++) {
						n[j].checked = false;
						n[j].disabled = true;
					}
				}

				if ((n.type == "radio") || (n.type == "checkbox")) {
					n.checked = false;
					n.disabled = true;
				}

				if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
					n.selectedIndex = 0;
					n.disabled = true;
				}

				if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
					n.value = '';
					n.disabled = true;
				}
			} else {
				var bad_field_message = "invalid on/off argument: " + onoff + ". Should be on or off";
				alert(bad_field_message);
				return false;
			}
		}
    }
}

function fields_off_except() {
	var fields = new Object();

    for (targ_count = 0; targ_count < arguments.length; targ_count++) {
        fields[arguments[targ_count]] = 1;
	}

	for ($j=0; $j<form.elements.length; $j++) {
		$this_input_reference = n = form.elements[$j];
		$this_input = $this_input_reference.name;

		if (fields[$this_input] != null) {
			continue;
		} else {
			if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
				for (j=0; j < n.length; j++) {
					n[j].checked = false;
					n[j].disabled = true;
				}
			}

			if ((n.type == "radio") || (n.type == "checkbox")) {
				n.checked = false;
				n.disabled = true;
			}

			if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
				n.selectedIndex = 0;
				n.disabled = true;
			}

			if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
				n.value = '';
				n.disabled = true;
			}
		}
    }
}

function fields_on_except() {
	var fields = new Object();

    for (targ_count = 0; targ_count < arguments.length; targ_count++) {
        fields[arguments[targ_count]] = 1;
	}

	for ($j=0; $j<form.elements.length; $j++) {
		$this_input_reference = n = form.elements[$j];
		$this_input = $this_input_reference.name;

		if (fields[$this_input] != null) {
			continue;
		} else {
			if ((n[0] != null) && ((n[0].type == "radio") || (n[0].type == "checkbox"))) {
				for (j=0; j < n.length; j++) {
					n[j].disabled = false;
				}
			}

			if ((n.type == "radio") || (n.type == "checkbox")) {
				n.disabled = false;
			}

			if ((n.type == "select") || (n.type == "select-one") || (n.type == "select-multiple")) {
				n.disabled = false;
			}

			if ((n.type == "text") || (n.type == "textarea") || (n.type == "password")) {
				n.disabled = false;
			}
		}
    }
}

function cc_validate(st) {
    if (st == 'test') {return (true);}

	st = numbersOnly(st);

    if (st.length > 19) {return(false);}
    if (st.length < 1) {return(false);}

    var sum=0;
    var mul=1;
    var l=st.length;
    var digit = '';
    var tproduct = '';

    for (var i=0; i<l; i++) {
        digit = st.substring(l-i-1,l-i);
        tproduct = parseInt(digit ,10)*mul;

        if (tproduct >= 10) {sum += (tproduct % 10) + 1;}
        else {sum += tproduct;}

        if (mul == 1) {mul++;}
        else {mul--;}
    }

    if ((sum % 10) == 0) {return(true);}
    else {return(false);}
}

function numbersOnly(st) {
    var numbers = "0123456789";
    var i;
    var returnString = "";

    for (i=0; i<st.length; i++) {
        var c = st.charAt(i);
        if (numbers.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function format_phone(field) {
    var this_data = field.value;
    var formatted_phone = '';

    var phone_10 = /^\d{10}$/;
    var phone_11 = /^1\d{10}$/;

    this_data = this_data.replace(/\D/g, '');

    if (this_data) {
        if (phone_10.test(this_data)) {
            var area_code = this_data.substring(0, 3);
            var prefix = this_data.substring(3, 6);
            var last_part = this_data.substring(6, 10);
            formatted_phone = area_code + '-' + prefix + '-' + last_part;
        } else if (phone_11.test(this_data)) {
            var area_code = this_data.substring(1, 4);
            var prefix = this_data.substring(4, 7);
            var last_part = this_data.substring(7, 11);
            formatted_phone = area_code + '-' + prefix + '-' + last_part;
        }
    }

    if (formatted_phone) {field.value = formatted_phone;}
}

function verify() {
    var msg;
    var empty_fields = '';
    var errors = '';
    var valid_email = /\S+@\S+\.\S+/;

	new_rf_array();

	do_conditional_reqs();

    for ($f in RF) {

        var $l = RF[$f];

        if ($f == null) { continue; }

        var $n = form[$f]

        if (!($n)) { continue; }

		if (($n[0] != null) && (($n[0].type == 'radio') || ($n[0].type == 'checkbox'))) {
			var option = 0;
			for (j=0; j < $n.length; j++) {
				if ($n[j].checked == true) { option = 1; }
			}
			if (option == 0) { empty_fields += "\n     " + RF[$f]; }
		}

		if ((($n.type == 'radio') || ($n.type == 'checkbox')) && ($n.checked == false)) {
			empty_fields += "\n     " + RF[$f];
		}

		if (($n.type == 'select') || ($n.type == 'select-one') || ($n.type == 'select-multiple')) {
			if (($n.value == '') || ($n.selectedIndex == -1)) { empty_fields += "\n     " + RF[$f]; }
		}

		if (($n.type == 'text') || ($n.type == 'textarea') || ($n.type == 'password') || ($n.type == 'hidden')) {
			if (($n.value == null) || ($n.value == '')) {
				empty_fields += "\n     " + RF[$f];
			}
		}

		if (($f.indexOf('email') > -1) && $n.value && !(valid_email.test($n.value)) ) {
			errors += "The field " + $l + " must be a valid E-mail address.\n\n";
		}

		if (($f.indexOf('payment_number') > -1) && $n.value && !(cc_validate($n.value)) ) {
			errors += "You have entered an invalid credit card number in  " + $l + " .\n\n";
		}

    }

    if (!empty_fields && !errors) {
//        return false;
        return true;
    } else {
        msg = "The form was not submitted because of the following error(s).\n";
        if (empty_fields) {
        msg += "The following required field(s) are empty:" + empty_fields + "\n";
        if (errors) msg += "\n"; }
        msg += errors;
        alert(msg);
        return false;
    }

}

function derequire() {
	var groups = new Object();
	var fields = new Object();

    for (arg_count = 0; arg_count < arguments.length; arg_count++) {
		if ((arguments[arg_count].lastIndexOf('_') + 1) == arguments[arg_count].length) {
	        groups[arguments[arg_count]] = 1;
		} else {
	        fields[arguments[arg_count]] = 1;
		}
	}

    for ($this_field in RF) {
		$this_prefix_length = $this_field.indexOf('_') + 1;
		$this_field_group = $this_field.substring(0, $this_prefix_length);

		$this_prefix_extended_length = $this_field.indexOf('_', $this_prefix_length) + 1;
		$this_field_group_extended = $this_field.substring(0, $this_prefix_extended_length);

		if ((groups[$this_field_group] != null) || (groups[$this_field_group_extended] != null) || (fields[$this_field] != null)) {
			delete RF[$this_field];
		}
    }
}
