/* $Id: check_required_fields_js.js,v 1.3.2.3 2006/12/25 11:23:19 max Exp $ */

/*
	Check required fields
*/
function checkRequired(lFields, id) {
//alert('in checkReqd, lbl_reqd='+lbl_required_field_is_empty);

	if (!lFields || lFields.length == 0)
		return true;

	if (id) {
		for (var x = 0; x < lFields.length; x++) {
			if (lFields[x][0] == id) {
				lFields = [lFields[x]];
				break;
			}
		}
	}

	for (var x = 0; x < lFields.length; x++) {
		if (!lFields[x] || !document.getElementById(lFields[x][0]))
			continue;

		var obj = _getById(lFields[x][0]);
msg = '';
		// JB Fix below for mysterious disappearing password error--copy ConfirmPwd field into Pwd
		if (lFields[x][1] == 'Password' && obj.value.length == 0 && _getById('Confirm Password') ) {
			obj.value = _getById('Confirm Password').value;
		}

		if ((obj.type == 'text' || obj.type == 'password' || obj.type == 'textarea') && !obj.value.search(/^[\s]*$/gi)) {
			if (lbl_required_field_is_empty != '') {
				alert(substitute(lbl_required_field_is_empty, 'field', lFields[x][1]));
			} else {
				alert(lFields[x][1]);
			}

			if (!obj.disabled && obj.type != 'hidden') {
				checkRequiredShow(obj);
				obj.focus();
			}

			return false;

		}
	}

	return true;
}

/*
	Show hidden element and element's parents
*/
function checkRequiredShow(elm) {
	if (elm.style && elm.style.display == 'none') {

		if (elm.id == 'ship_box' && document.getElementById('ship2diff')) {
			/* Exception for Register page */
			document.getElementById('ship2diff').checked = true;
			document.getElementById('ship2diff').onclick();
			
		} else
			elm.style.display = '';
	}

	if (elm.parentNode)
		checkRequiredShow(elm.parentNode);

}

