/* $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) {
	
	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]);
		if ((obj.type == 'text' || obj.type == 'password' || obj.type == 'textarea') && !obj.value.search(/^[\s]*$/gi)) {
			if (lbl_required_field_is_empty != '') {
				//alert("Por favor introduzca su " + lFields[x][1]);
				ShowErrorMessage("Estimado(a) cliente", "Por favor introduzca su " + 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);
}

function isInteger (s){
	var i;

	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isDigit (c){
      return ((c >= "0") && (c <= "9"))
}
