// JavaScript Document

var comma=",";

function isUndefined(a) {
	try {
      	  return typeof a == 'undefined';
	} catch (ex) {
     	  alert('error: ' + ex.description );
	}
}

function SetLabel(docRef, labelId, newLabel) {
  var obj = docRef.getElementById( labelId );

  obj.innerText = newLabel;
}


//hidden  text password  textarea  radio select-one select-multiple button submit reset



function SetElementValue(obj, newValue) {
	/*
	alert('SetElementValue obj.length:' + obj.length);
	alert('SetElementValue newValue: '+ newValue );
	alert('SetElementValue obj.type: '+ obj.type );
	alert('SetElementValue obj.id: '+ obj.id );
	*/

	if (obj == null)
		alert( 'SetElementValue: obj == null')
	else if ( obj.length != null ) {

		for  ( var i = 0; i < obj.length; i++) {

			if (obj[i].type == 'radio')
				obj[i].checked = (obj[i].value == newValue);
			else
				obj[i].selected = (obj[i].value == newValue);

		}

	} else {


		if (isUndefined(obj.type) ) {
		//if ( obj.type == 'undefined' ) {//si no es un input, posiblemente sea un tag; por ejemplo un label
			//alert( 'innertText' + obj.innertText );
			obj.innertText = newValue
		} else if ( obj.type == 'hidden' || obj.type == 'text' || obj.type == 'password' || obj.type == 'textarea' || obj.type == 'button' || obj.type == 'submit' || obj.type == 'reset') {
	 		obj.value = newValue;
	 	} else if ( obj.type == 'checkbox' || obj.type == 'radio' ) {
			//si es un checkbox o un radio este se pondrá "checked" solo si el su value coincide
			//esto debido a la posibilidad de que existan varios de estos elementos con el mismo nombre
			//y por tanto deben "checkearse" solo los que corresponden
			if (obj.value == newValue)
				obj.checked = true
		} else if ( obj.type == 'select-one' || obj.type == 'select-multiple') {

	  			for ( var i = 0; i < obj.options.length; i++) {
	  				//alert(  obj.options[i].value );
	  				if (obj.options[i].value == newValue)
						obj.options[i].selected = true;
				}
		}
	}

}

//si los arrays son de tamaño 1 debe y el array de values esta formado por un entero deben usarse
//comillas para encerrar el entero para que la sintaxis sea diferente a la creación de array
//con X cantidad de entradas

function setFormFields(formName, names, values) {

  var obj = null;

  if (formName == '')
  	formName = 'forms[0]';

  for ( var i = 0; i < names.length; i++) {
      obj = eval ("document." + formName + "." + names[i]);

	  if (obj == 'undefined') {
	    alert(names[i]);
	  }

	  if (obj != null) {

	    if (obj.length == null) {  //si no es un arreglo de elementos con el mismo nombre
  	      SetElementValue( obj, values[i] )
		} else {

		  //para select-one y select-multiple length trae el número de options
		  if (obj.type == 'select-one' || obj.type == 'select-multiple') {
		    for (var j = 0; j < obj.options.length; j++) {
			  if ( obj.options[j].value == values[i] ) {
			    obj.options[j].selected = true;
			  }
			}
		  } else {
		    for (var j = 0; j < obj.length; j++) {
		      SetElementValue( obj[j], values[i] )
		    }
		  }
		}
	  }
  }
}


//xxx, Allan: 17 agosto del 2004. Está en fase de pruebas

function GetElementValue(obj) {
var resultado = null;

	//alert( 'GetElementValue obj.type=' + obj.type );
	//alert( 'GetElementValue obj.length=' + obj.length );

	if (obj == null)
		alert('GetElementValue, obj == null');

	if (isUndefined(obj))
		alert('GetElementValue, obj == undefined');

	if (obj.length != null)
		for ( var i = 0; i < obj.length; i++) {
			resultado = GetElementValue( obj[i]);
			if (resultado != null)
				return resultado
		}

	if ( obj.type == 'hidden' || obj.type == 'text' || obj.type == 'password' || obj.type == 'textarea' || obj.type == 'button' || obj.type == 'submit' || obj.type == 'reset') {
		return obj.value;
	} else if ( obj.type == 'checkbox' || obj.type == 'radio' ) {
		//alert( 'GetElementValue obj.value=' + obj.value );
		if (obj.checked)
			return obj.value;

	} else if ( obj.type == 'select-one' || obj.type == 'select-multiple') {

		//alert(obj.type);

	    for ( var i = 0; i < obj.options.length; i++) {
		  if (obj.options[i].selected) {
		    return obj.options[i].value
		  }
		}
	}

	return null;
}



function EnableFields(fields, enable)
{
	var elementos = fields.split(comma);


	for (var i=0; i<elementos.length; i++) {
		elemento = document.getElementById(elementos[i]);

		try
		{
			elemento.disabled = enable;
		} catch(ex)
		{
			alert("EnableFields ex "+ elementos[i]);
		}
	}
}

function ValidateRequiredFields(fields) {
	var elementos = fields.split(comma);
	var elemento = null;

	for (var i=0; i<elementos.length; i++) {
		elemento = document.getElementById(elementos[i]);

		if (GetElementValue(elemento) == "") {
			alert('Falta al menos un campo obligatorio');
			//alert(elemento.name);
			elemento.focus();
			return false;
		}
	}

	elemento.form.submit();
}
// agragado por moi
function HabilitarDatosClimaticos(combo)
{
	var nombres1 = "R1_a,R1_b,C3,T8,T10,T11,T6,T7,T9,C11,C12,C13,C14,C16,C17,C20,C21,S2,C18,T12";
	var nombres2 = "R1_a,R1_b,T9,T10";
	var nombres3 = "C3";

	if (combo.id == "C19")
		EnableFields(nombres1, !combo.checked);

	if (combo.id == "C2") {
		//alert(combo.checked);
		EnableFields(nombres1, !combo.checked);
		EnableFields(nombres2, combo.checked);
		EnableFields(nombres3, !combo.checked);
	}

	if (document.getElementById("C19").checked || document.getElementById("C12").checked) {
		EnableFields(nombres1, false);
	}
}

