/**
 * Requires dojo.js
 */
var offlineMessage = "Nuestro servicio está; en mantenimiento. Por favor reintente en unos minutos.";

/**
 * Ajax call
 */

function postUrlIHS(url, load)
{
	if (load == undefined)
	{
		load = updateContentIHS;
	}
	var xhrArgs =
	{
	    url: url,
	    handleAs: "text",
	    load: load,
	    timeout: 10000,
	    error: function (err){ console.error(err); }
	};
	dojo.xhrPost(xhrArgs);
}

function updateContentIHS(contentHtml, ioArgs, divContentId)
{
	if (divContentId == undefined || dojo.byId(divContentId) == null || error)
	{
		divContentId = "pagumContent";
	}
	dojo.byId(divContentId).innerHTML = contentHtml;
	parseJavaScript(dojo.byId(divContentId));
}

function toggleDivs(show, hide)
{
	dojo.byId(show).style.display = "";
	dojo.byId(hide).style.display = "none";
}

function displayDiv(divId, display)
{
  var displayValue = display ? "" : "none";
  dojo.byId(divId).style.display = displayValue;
}

/**
 * Abre una ventana cargando una pagina
 * @param name		Nombre de la ventana
 * @param width		Ancho
 * @param height	Alto
 * @param url_form	Url de la accion
 * @param form		Formulario a cargar (opcional)
 * @return
 */
function openWindowIHS(name, width, height, url, formId)
{
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 3;
	var options = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
	options = options + ",menubar=no,toolbar=no,location=no,status=no,directories=no,personalbar=no,resizable=yes,scrollbars=yes";
	
	if(formId == null)
	{
		try
		{
			var documentHtml = "<html><body><form method='POST' action='"+ url +"'></form></body><script>document.forms[0].submit();</script></html>";
			var ventana = window.open("", name, options, false);
			ventana.focus();
			ventana.document.write(documentHtml);
		}
		catch(e)
		{
		}
	}
	else
	{
		var form = dojo.byId(formId);
		window.open("", name, options);
		form.action = url;
		form.method = "POST";
		form.target = name;
		form.submit();
	}
}

/**
 * Other
 */

function trimIHS(string)
{
	string = string.replace(/^\s\s*/, "");
	var ws = /\s/;
	var i = string.length;
	while (ws.test(string.charAt(--i)));
	return string.slice(0, i + 1);
} 

/**
 * Se busca el tag <script/> y se evalua su contenido.
 * 
 * @return
 */
function parseJavaScript(mydiv)
{
	var elements = mydiv.getElementsByTagName('script');
	for (i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		newScript = document.createElement('script');
		newScript.text = element.innerHTML;
		newScript.type = 'text/javascript';
		if (element.src != null && element.src.length > 0)
		{
			newScript.src = element.src;
		}
		element.parentNode.replaceChild(newScript, element);
	}
}

function showOffLineMessageIHS()
{
	alert(offlineMessage);
}

function loadIframeLoginIHS()
{
	postUrlIHS("pagumAppURL.txt", function (contentHtml, ioArgs, divContentId)
	{
		if(trimIHS(contentHtml)!="")
		{
			dojo.byId("iframeLogin").src = (trimIHS(contentHtml)+ "inicio.htm");
		}
		else
		{
			dojo.byId("iframeLogin").src = "offlineLogin.html";
		}
	});
}

function registrateIHS()
{
	postUrlIHS("pagumAppURL.txt", function (contentHtml, ioArgs, divContentId)
	{
		if(trimIHS(contentHtml)!="")
		{
			openWindowIHS("REGISTRATE", 690, 550, contentHtml + "registrate.htm");
		}
		else
		{
			showOffLineMessageIHS();
		}
	});
}

function recomiendaIHS()
{
	postUrlIHS("pagumAppURL.txt", function (contentHtml, ioArgs, divContentId)
	{
		if(trimIHS(contentHtml)!="")
		{
			openWindowIHS("RECOMIENDANOS", 460, 530, contentHtml + "recomiendanos.htm");
		}
		else
		{
			showOffLineMessageIHS();
		}
	});
}

function contactenosIHS()
{
	postUrlIHS("pagumAppURL.txt", function (contentHtml, ioArgs, divContentId)
	{
		if(trimIHS(contentHtml)!="")
		{
			openWindowIHS("CONTACTENOS", 650, 550, contentHtml + "contactenos.htm");
		}
		else
		{
			showOffLineMessageIHS();
		}
	});
}

function comprasInternetIHS()
{
	postUrlIHS("pagumAppURL.txt", function (contentHtml, ioArgs, divContentId)
	{
		if(trimIHS(contentHtml)!="")
		{
			openWindowIHS("COMPRAS", 790, 550, contentHtml + "comprasInternet.htm");
		}
		else
		{
			showOffLineMessageIHS();
		}
	});
}

