// portal.js

function show(id)
{
	document.getElementById(id).style.visibility = "visible";
	document.getElementById(id).style.display = "block";
}

function hide(id)
{
	document.getElementById(id).style.visibility = "hidden";
	document.getElementById(id).style.display = "none";
}

function setValue(name, value) {
	selectedField = document.getElementById(name);
	selectedField.value = value;
}

function setDisplay(elementId, display) {
	var element = document.getElementById(elementId);
	element.style.display = display;
}

function setChecked(elementId) {
	var element = document.getElementById(elementId);
	element.checked = true;
}

function enableElement(elementId) {
	var element = document.getElementById(elementId);
	element.disabled = false;
}

function disableElement(elementId) {
	var element = document.getElementById(elementId);
	element.disabled = true;
}


/***
 * Other functions
 ***/

// Receives dd/mm/aaaa returns "aaaammdd"
function get_YMD_fromFormatedDate(pDate, pSep)
{
   var sep = pSep || ".";
   var theDate = pDate.split(sep);
   var mm = parseInt(theDate[1],10);
   if (mm < 10) mm = "0"+mm;
   var dd = parseInt(theDate[0],10);
   if (dd < 10) dd = "0"+dd;
   return (theDate[2] + "" + mm + "" + dd);
}

function cfgAjaxMssg(pStr)
{
  var tError = new String(pStr);
  tError = tError.split("\\");
  var txt = new String();
  for (var i=0; i < tError.length; i++)
  {
   if ("n" == tError[i].charAt(0))
   {
     tError[i] = tError[i].substr(1);
   }
   txt += tError[i] + "\n";
  }
  return txt;
}

/***
 * DHTML modal dialog box
 ***/

function showAddEditPopupBase(wrapperId, titleId, windowId, subject) {
  popUpOn = true;
  setPopupDimsBase(wrapperId, windowId);
  document.getElementById(titleId).innerHTML = subject;
  show(wrapperId);
  show(windowId);
}

function closePopupBase(wrapperId, windowId)
{
  popUpOn = false;
  hide(wrapperId);
  hide(windowId);
}

function setPopupDimsBase(wrapperId, windowId)
{
  if (! popUpOn)
  { return; }
  var tWidth  = document.body.clientWidth;
  var tHeight = document.body.clientHeight;  
  var tOuterDiv = document.getElementById(wrapperId);
  tOuterDiv.style.width  = tWidth  + "px";
  tOuterDiv.style.height = tHeight + "px";
  var tInnerPopup = document.getElementById(windowId);
  var tLeft = tInnerPopup.clientWidth  || parseInt(tInnerPopup.style.left);
  var tTop  = tInnerPopup.clientHeight || parseInt(tInnerPopup.style.top);
  tInnerPopup.style.left = ((tWidth  / 2) - (tLeft / 2)) + "px";
  tHeight = ((tHeight / 2) - (tTop / 2));
  if (tHeight < 1) 
  { tHeight = 40; }
  tInnerPopup.style.top = tHeight + "px";
}

/***
 * AJAX functions
 ***/
 
function getNodeValue(pId)
{
  return "string" == typeof(pId)
    ? xmlDoc.getElementsByTagName(pId)[0].firstChild.nodeValue
    : (pId.firstChild) ? pId.firstChild.nodeValue : "";
}

function getChildNode(pNode, pLabel)
{
  var aVal = pNode.getElementsByTagName(pLabel);
  return (! aVal.length) ? "????" : getNodeValue(aVal[0]);
}

function importXML(pXML, pFunc)
{
	if (document.implementation && document.implementation.createDocument)
	{
    var objDOMParser = new DOMParser();
    xmlDoc = objDOMParser.parseFromString(pXML, "text/xml");
		eval(pFunc+"()");
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () 
		{
			if (xmlDoc.readyState == 4) eval(pFunc+"()");
		};
		xmlDoc.loadXML(pXML);
 	}
	else
	{
		alert("Su navegador internet no es compatible con AJAX.");
		return;
	}
}

function checkResponse(pXML)
{
  // Loads xmlDoc with the DOM structure of the string PXML
  importXML(pXML, "checkResponseXML");  
}

function checkResponseXML()
{
  var nResponse = xmlDoc.getElementsByTagName('response');
	nResponse = nResponse[0];
	responseError = ("0" == nResponse.getAttribute("errNo")) 
	   ? 0
	   : responseError = new Error(nResponse.getAttribute("errMssg"));
}

