/********************************************
 * Archivo de utilidades y funciones.       *
 *                                          *
 * @author: Matías Schmid                   *
 * @date-created: April 4, 2007             *
 * powered by ©soho                         *
 *                                          *
 ********************************************/
 
/*************************************************
 Este archivo contiene funciones particulares
 para el manejo de las noticias del sitio y demas
 mediante el uso de Ajax
 *************************************************/ 

var http_request = false;


function makeRequest(url, functionName) {
	
	http_request = false;
	
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');	
		}
	} else if (window.ActiveXObject) { //IE
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");	
		} catch (ex) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");	
			} catch (ex) {}
		}
	}
	
	if (!http_request) {
		alert('Imposible crear la instancia del objeto XMLHTTP');
		return false;
	}
	
	switch (functionName){
		case "FillSubCatCombo":
		http_request.onreadystatechange = FillSubCatCombo;
		break;
		
		case "GenerateNewsList":
		http_request.onreadystatechange = GenerateNewsList;
		break;
		
		case "ShowItem":
		http_request.onreadystatechange = ShowItem;
		break;
		
		default:
		alert('Debe especificar una funcion a ejecutar');
	}	
	
	http_request.open('GET', url, true);
	http_request.send(null);
	
}

function FillSubCatCombo() {
	
	var container = document.getElementById('subCatContainer');
	
	if (http_request.readyState == 4){
		if (http_request.status == 200){
				var combo = http_request.responseText;
				heiddenBlockPantallaWarn();
				container.innerHTML = combo;
		} else {
				alert('Hubo problemas al realizar la petición');	
		}
	}
}

function GenerateNewsList() {
	
	var listCont = document.getElementById('list_container');
	
	if (http_request.readyState == 4){
		if (http_request.status == 200){
				var list = http_request.responseText;
				heiddenBlockPantallaWarn();
				listCont.innerHTML = list;
		} else {
				alert('Hubo problemas al realizar la petición');	
		}
	}
}

function ShowItem() {

	var artContainer = document.getElementById('list_container');
				
	if (http_request.readyState == 4){
		if (http_request.status == 200){
				var artResponse = http_request.responseText;
				heiddenBlockPantallaWarn();
				artContainer.innerHTML = artResponse;
		} else {
				alert('Hubo problemas al realizar la petición');	
		}
	}		
}


function loadArticle(idArt) {
	
	var param;
	var url;
	
	param = idArt;
	
	url = ("../inc/news_controller.asp?idNoticia=" + param);

	showBlockPantallaWarn();
	makeRequest(url, 'ShowItem');
		
}


function makeQueryString(idPag){
	
	var params = ''; 
	var elem;
	
	var tipoArt;
	
	elem = document.getElementById('cboFecha');
	if(elem)
	{
		if(params != '')
			params += "&";
		else
			params = "?";
		params += 'fecha='+elem.options[elem.selectedIndex].value;
	}
	
	elem = document.getElementById('cboCategoria');
	if(elem)
	{
		if(params != '')
			params += "&";
		else
			params = "?";
		params += 'catId='+elem.options[elem.selectedIndex].value;
	}
	
	elem = document.getElementById('cboSubCategoria');
	if(elem)
	{
		if(params != '')
			params += "&";
		else
			params = "?";
		params += 'subCatId='+elem.options[elem.selectedIndex].value;
	}
	
	elem = document.getElementById('section');
	if(elem){
		if(params != '')
			params += "&";
		else
			params = "?";
		
		if(elem.value == "noticias"){
			params += 'tipoArt=PR'; 
		}
		
		if(elem.value == "proyectos") {
			params += 'tipoArt=HE'; 	
		}		
	}	
	
	if (idPag == null){
		idPag = 1;
	}
	if(params != '')
		params += "&";
	else
		params = "?";			
	
	params += "PageNo=" + idPag;
	

	showBlockPantallaWarn();
	makeRequest('../inc/news_controller.asp'+params, 'GenerateNewsList');
	
}


/*Utilidades*/

function showBlockPantallaWarn()
{
	var block = document.getElementById('blockPantallaWarn');
	var msg;
	var alto;
	var ancho;
	var pageSize = getPageSize();
	var msgHeight;
	var msgWidth;
	
	var yScroll = getPageScroll();
	
	block.style.height = pageSize[1]+'px';
	block.style.width = pageSize[0]+'px';
	
	msg = document.getElementById('warningMessage');
		
	alto = pageSize[3];
	ancho = pageSize[2];
	
	
		msgWidth = msg.offsetWidth;
		msgHeight = msg.offsetHeight;

		msg.style.top = ((Math.floor(alto/2)) + yScroll - (9))+'px';
		msg.style.left = ((Math.floor(ancho/2)) + document.body.scrollLeft - (110))+'px';
		
		//alert(document.body.scrollTop);
		
	
	block.style.display = 'block';
}

function heiddenBlockPantallaWarn()
{
	var block = document.getElementById('blockPantallaWarn');
	if(block)
		block.style.display = 'none';
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	return yScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
