var formName = "__aspnetForm";


function confirmarBorrado(fieldMsg) {
  if (fieldMsg) {
    return confirm(getValue(fieldMsg))
  }
  else {
    return confirm(getValue('errMsgDel'))
  }
}

function formToNVC(excludes) {
	form = document.getElementById(formName);
  var nvc = new Ajax.Web.NameValueCollection();
  var okey = true;
  for(j = 0; j < form.elements.length; j++){
    nombre = form.elements[j].name;
    if ((excludes!=null) && (excludes.indexOf(nombre) >= 0)) {
      okey = false;
    }
    if (okey) {
      nvc.add(nombre,getValue(nombre));
    }
    else {
      okey = true;
    }
  }
  return nvc;
}


function hideElement(idElement) {
  document.getElementById(idElement).style.display = "none";
}

function showElement(idElement) {
  document.getElementById(idElement).style.display = "block";
}


function showLoading() {
	verticalCenter('cargando');
	showElement('cargando');
	showElement('f-Cargando');
	
	//overlib('',CSSCLASS,FGCLASS,'overLibFgClass',BGCLASS,'overLibBgOpacityClass',WIDTH,screen.width,HEIGHT,screen.height,FIXX,0,FIXY,0);
}


function hideLoading() {
	hideElement('cargando');
	hideElement('f-Cargando');	
	//overlib('',CSSCLASS,FGCLASS,'overLibFgClass',BGCLASS,'overLibBgOpacityClass',WIDTH,screen.width,HEIGHT,screen.height,FIXX,0,FIXY,0);
}


function setAction(accion) {
	formu = document.getElementById(formName);
	formu.action = accion;
}

function doSubmit() {
	formu = document.getElementById(formName);
	showLoading();
	formu.submit();
}


function doSubmitValidated() {
	if (isValidForm()) {
		doSubmit();
	}	
}


function goTo(url) {
  showLoading();
  myBaseHREF(url);
}

function isValidForm() {
	formu = document.getElementById(formName);
	return validateForm(formu,false,false,false,true,'5');
}

function doPestanha(urlAction,validarFunc) {
	okey = true;
	if (validarFunc) {
		okey = eval(validarFunc);
	}
	if (okey) {
		if  (urlAction.indexOf('javascript:') >= 0) {
			eval(urlAction);
		}
		else {
			setAction(urlAction);
			doSubmit();
		}
	}
}

function verticalCenter(element) {    
  obj = document.getElementById(element);
  obj.style.width = screen.width
  obj.style.height = screen.height;  
  
  /*
    var alcadaDiv = document.getElementById('cargando').offsetHeight;
    var alcadaImg = 16;
    
    
    if (alcadaDiv > alcadaImg) {
        var position = Math.round((alcadaDiv-alcadaImg)/2);
        document.getElementById("loading").style.marginTop=position+"px";
    } else {
        document.getElementById("loading").style.marginTop="0px";
    }
    return;
    */
}



function getValue(id) {
	if (id=="") return "";	
	obj = document.getElementById(id);
	valor = "-"
	if (obj) {
		try {
			switch (obj.nodeName.toUpperCase()) {
				case "INPUT": 
					attr = obj.getAttribute("type");
					switch (attr.toUpperCase()) {
						case "TEXT": valor = obj.value; break;
						case "CHECKBOX": 
							if (obj.checked){
								valor = obj.value; 
							}
							else {
								valor = "";
							}
						break;
						case "RADIO": 						
							objRadio = eval("document.forms[0]."+id);
							valor="";
							for (i=0; i < objRadio.length; i++)  {
								if (objRadio[i].checked){
									valor=objRadio[i].value;
								}
							}
						break;
						default: valor = obj.value; break;
					}				
				break;
				case "TEXTAREA": valor = obj.value;  break;
				case "SELECT": valor = obj.options[obj.selectedIndex].value; break;
			}
		}
		catch (e) {
			//alert("id="+id+"\n"+e.message);
		}
		//alert("("+obj.nodeName+")"+id+" = "+valor+"\n"+obj.getAttribute("type"));
	}
	else {
		alert(id);
	}
	return valor;
}


function setValue(id,valor) {
	if (id=="") return "";	
	obj = document.getElementById(id);
	if (obj) {
		try {
			switch (obj.nodeName.toUpperCase()) {
				case "INPUT": 
					attr = obj.getAttribute("type");
					switch (attr.toUpperCase()) {
						case "TEXT": obj.value = valor; break;
						case "CHECKBOX": 
							if (obj.checked){
								valor = obj.value; 
							}
							else {
								valor = "";
							}
						break;
						default: obj.value = valor; break;
					}				
				break;
				case "TEXTAREA": obj.value = valor;  break;
				case "SELECT": 
					for (i=0; i < obj.options.length; i++) {
						obj.options[i].selected=false;
					}
					obj.selectedIndex=0;
					for (i=0; i < obj.options.length; i++) {
						if (obj.options[i].value == valor) {
							obj.options[i].selected = true;
							obj.selectedIndex=i;		
							break;
						}
					}
				break;
			}
		}
		catch (e) {
			//alert("id="+id+"\n"+e.message);
		}
		//alert("("+obj.nodeName+")"+id+" = "+valor+"\n"+obj.getAttribute("type"));
	}
	else {
		alert(id);
	}
}

function clearAttribute(atributo) {
	formu = document.getElementById(formName);
  for(j = 0; j < formu.elements.length; j++){
   formu.elements[j].setAttribute(atributo,"");
  }
}

function setAttribute(id,atributo,valor) {
	obj = document.getElementById(id);
	if (obj) {
		obj.setAttribute(atributo,valor);
	}
}


function FillListValues(CONTROL){
	var arrNewValues;
	var intNewPos;
	var strTemp = GetSelectValues(CONTROL);
	arrNewValues = strTemp.split(",");
	for(var i=0;i<arrNewValues.length-1;i++){
		if(arrNewValues[i]==1){
			intNewPos = i;
		}
	}

	for(var i=0;i<arrOldValues.length-1;i++){
		if(arrOldValues[i]==1 && i != intNewPos){
			CONTROL.options[i].selected= true;
		}
		else if(arrOldValues[i]==0 && i != intNewPos){
			CONTROL.options[i].selected= false;
		}

		if(arrOldValues[intNewPos]== 1){
			CONTROL.options[intNewPos].selected = false;
		}
		else{
			CONTROL.options[intNewPos].selected = true;
		}
	}
}


function GetSelectValues(CONTROL){
	var strTemp = "";
	for(var i = 0;i < CONTROL.length;i++){
		if(CONTROL.options[i].selected == true){
			strTemp += "1,";
		}
		else{
			strTemp += "0,";
		}
	}
	return strTemp;
}

function GetCurrentListValues(CONTROL){
	var strValues = "";
	strValues = GetSelectValues(CONTROL);
	arrOldValues = strValues.split(",")
}


function changeSeccion(url,seccion) {
  setValue('seccion',seccion);
  setValue('pkAction','changeSeccion');
  goTo(url+"&seccion="+getValue('seccion')+"&pkAction="+getValue('pkAction'));
}
