var page = 	{
				contextPath : "",
				timeout: 50000,
				isStringEmpty: function(value)
							{
								if(value == undefined)
									return true;
								if(value instanceof String)
									return value.length == 0;
								return true;
							}
		 	};

function pageOnLoad()
{
	getFeedsRSS();
}

function getFeedsRSS()
{
	var setting;
    setting = 	{
    				url: page.contextPath + "/getFeedsRSS.do",
   		 			handle: getFeedsRSSCallBack,
   		 			timeout: page.timeout
            	};
    dojo.xhrPost(setting);
}

function getFeedsRSSCallBack(response)
{
	var error, feeds;	
    error = response instanceof Error;    
    if(error)
    {
    	if(response.message == "timeout exceeded")
    		alert("Se ha excedido el tiempo de espera de comunicación con el servidor. \nVerifique su conexión de red y actualice la página.");
    	else if(response.status != undefined)
    		alert(getErrorMsgFromStatus(response.status) + "\nIntente actualizar la página nuevamente.");
    	else
    		alert(response.message);
        return;
    }                       
    
    try
    {
    	feeds = dojo.fromJson(response);
    }
    catch(e)
    {
    	alert("Los RSS devueltos por el Servidor estan incorrectos. \nImposible cargarlos, intente nuevamente.");
    	return;
    }
    
    if(feeds.errorMsg != undefined)
    {
    	alert(feeds.errorMsg);
        return;
    }
    fillFeeds(feeds);
}


function fillFeeds(feeds)
{
	var html, ctrl;
	
	html = "<br/><br/><br/>"
	html += "<table cellpadding=\"10px\" cellspacing=\"10px\">";
	
	html += "	<tr>";
	html += "		<td style=\"width:100px;\">";
	html += "		</td>";
	html += "		<td>";
	html += "			<a type=\"application/rss+xml\" href=\""  + page.contextPath + "/rss.do\">";
	html += "				<img src=\"" + page.contextPath + "/images/cms/RSS/RSS.png\" style=\"width:30px; height:30px;\">";
	html += "			</a>";
	html += "		</td>";
	html += "		<td>";
	html += "			<a type=\"application/rss+xml\" href=\"" + page.contextPath + "/rss.do\">";
	html += "				<span> <b>SIICYT</b></span>";
	html += "			</a>";
	html += "		</td>";
	html += "	</tr>";
	
	for(var i = 0; i < feeds.length; i++)
	{
		html += "	<tr>";
		html += "		<td style=\"width:100px;\">";
		html += "		</td>";
		html += "		<td>";
		html += "			<a type=\"application/rss+xml\" href=\"" + feeds[i].url + "\">";
		html += "				<img src=\"" + page.contextPath + "/images/cms/RSS/RSS.png\" style=\"width:30px; height:30px;\">";
		html += "			</a>";
		html += "		</td>";
		html += "		<td>";
		html += "			<a type=\"application/rss+xml\" href=\"" + feeds[i].url + "\">";
		html += "				<span>" + feeds[i].descripcion + "</span>";
		html += "			</a>";
		html += "		</td>";
		html += "	</tr>";
	}
	
	html += "</table>";
	ctrl = dojo.byId("feedsContainer");
	ctrl.innerHTML = html;	
}

/**
 * Muestra un mensaje de alerta dependiendo del status del HTTP que se obtiene del Request.
 * @param status Codigo State de respuesta del Servidor.
 */
function getErrorMsgFromStatus(status)
{
	switch(status)
	{
		case 400:
			return "El servidor no se encuentra disponible.";
			break;
		case 500:
			return "Ocurrio un error en el servidor.";
			break;
		default:
			return "Ocurrio un error en la comunicación con el servidor.";
	}
}
