//@autor: juansebastian.perezherrero@telefonica.es
/**
 * Funciones auxiliares
 */
 
function checkSelectValue(select_name) {
    var sel = document.getElementById(select_name);
    return (sel.options[sel.selectedIndex].value != "");
}

function getSelected(select_name) {
    var sel = document.getElementById(select_name);
    return sel.options[sel.selectedIndex];
}

function escape_js(str) {
	return str.replace(/\'/, "\'", str).replace(/\"/, '\"', str);
}

function displayDivOnChangeSelect(sel_id, div_id, label) {
	
	var sel = document.getElementById(sel_id);
	if (sel.options[sel.selectedIndex].innerHTML == label){
		document.getElementById(div_id).style.display= 'block';
	}
	else{
	   	document.getElementById(div_id).style.display= 'none';	
	}
}

function showHideDiv(capa, opener) {
    if (capa.style.display == "none") {
    	capa.style.display = "block";
    	opener.innerHTML = '<img src="/images/iconos/ico_collapse.gif" alt="cerrar"/>';
    }
    else {
		capa.style.display = "none";
    	opener.innerHTML =  '<img src="/images/iconos/ico_expand.gif" alt="expandir"/>';
	}
}

function deleteNode(name) {
	var obj = document.getElementById(name);
	if (obj) obj.parentNode.removeChild(obj);
}

/**
 * Funcion mostrar/ocultar sin enlace para expandir
 */
function showHideSpan(span_name) {
	var span = document.getElementById(span_name);
    if (span.style.display == "none") span.style.display = "block";
    else span.style.display = "none";
}

function showHideInline(span_name) {
	var span = document.getElementById(span_name);
    if (span.style.display == "none") span.style.display = "inline";
    else span.style.display = "none";
}


/**
 * Cliente prototipo ajax
 * @see classes/system/util/ViewHelper.php.php
 */
var AjaxHelper = Class.create();

AjaxHelper.prototype = {

  initialize: function() {},
	
	ajaxUpdate: function(ajaxResponse) {
		
			
		//Estado de la operación y mensaje			
		if ((status = ajaxResponse.childNodes[0].attributes[0].value) && (msg = RicoUtil.getContentAsString(ajaxResponse.childNodes[0]))) alert(status + ": " + msg);
		
		if ( scriptElement = ajaxResponse.childNodes[2] ) 
		{
		  eval(RicoUtil.getContentAsString(scriptElement));
		}
		
		if (updateElement = ajaxResponse.childNodes[1])
		{
		     
			for (i = 0; i < updateElement.childNodes.length; i++)
			{
				var elementName = updateElement.childNodes[i].attributes[0].value;

				var element = document.getElementById(elementName);
				var field = updateElement.childNodes[i].attributes[1];				

				if (field) {						
						//antes modify? por qué?
						if (field.value == "value"){ element.value = RicoUtil.getContentAsString(updateElement.childNodes[i]);}
						if (field.value == "delete" && element) element.parentNode.removeChild(element);
						if (field.value == "tinyMce") tinyMCE.setContent(RicoUtil.getContentAsString(updateElement.childNodes[i]));
                        if (field.value == "display"){ element.style.display = 'block';}						
						if (field.value == "spinner" && element) element.style.display = 'none';						
						if (field.value == "add") {							
							newEl = document.createElement(updateElement.childNodes[i].childNodes[0].nodeName);
				     	  for (j=0;j<updateElement.childNodes[i].childNodes[0].attributes.length;j++) 
				     	      newEl.setAttribute(updateElement.childNodes[i].childNodes[0].attributes[j].name,updateElement.childNodes[i].childNodes[0].attributes[j].value);

            	          newEl.innerHTML = RicoUtil.getContentAsString(updateElement.childNodes[i].childNodes[0]);

    					  var elementType = updateElement.childNodes[i].attributes[0].name;

            	           //Monta el contenido @todo especificar el número
  			               if (elementType == 'tag'){
                                mountPoint = document.getElementsByTagName(elementName);
                                mountPoint[0].appendChild(newEl);
                            }
                            else{
                                mountPoint = document.getElementById(elementName);
                                mountPoint.appendChild(newEl);
                            }
						}
					}
				else 
				{				    
					element.innerHTML = RicoUtil.getContentAsString(updateElement.childNodes[i]);						
				} 
			}
		}

	}	
};

