var actualpos = 0;
var posMov = 9;
var maxHeigth = -10;
var countItems;
var inter;
var changeCategoria = false;
var showStatus = false;
function mov(x)
{
	var laymov = document.getElementById("menuOpt");
	if ((!((x>0) &&(actualpos >= 0)))
	&& (!((x<0) && (actualpos < countItems*maxHeigth))))
	{
		
		actualpos += (x*posMov);
		laymov.style.top = actualpos+"px";
	}
	
}

function up()
{mov(-1);inter = window.setTimeout("up()",200);}
function down(){
	mov(1);inter = window.setTimeout("down()",200);
}
function salir(){
	window.clearTimeout(inter);
}
function hideMenu()
{
	var menuOpt = document.getElementById('contCat');
	menuOpt.style.visibility='hidden';
}
function showMenu()
{
	var menuOpt = document.getElementById('contCat');
	menuOpt.style.visibility='visible';
	menuOpt.style.display='block';
}
function checkOpt(chk){
	inCh = document.getElementById(chk);
	changeCategoria = true;
	if (inCh.checked)
	{
		inCh.checked = false;
		return 'noSelected';
	}
	inCh.checked = true;
	return 'catSelected';
}

function initCat(cItems){
	document.getElementById("contCat").style.height = "160px";
	document.getElementById("listOpt").style.height = "120px";
	document.getElementById("contCat").style.position='absolute';
	document.getElementById("contCat").style.visibility='hidden';
	frm = document.getElementById("filtroProd");
	for (n=0;n<frm.elements.length;n++)
	{
		if (frm.elements[n].id != "btEnviarCat")
		{
			frm.elements[n].style.display = 'none';
		}
	}
	if (cItems <= 0)
	{countItems = 1;}else{countItems = cItems;}
}
function getPageSize(){
	
	var xScroll, yScroll;

	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;
}

function overlay(status)
{
	var objOverlay = document.getElementById("overlay");
	if (objOverlay == undefined)
	{
		//document.getElementById("log").innerHTML = "AHO";
		var objBody = document.getElementsByTagName("body").item(0);
		
		// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
		objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '90';
		objOverlay.style.width = '100%';
		objBody.insertBefore(objOverlay, objBody.firstChild);
	}
	if (status)
	{
		var arrayPageSize = getPageSize();
		// set height of Overlay to take up whole page and show
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.display = 'block';
		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;
		}	
		h = parseInt(windowWidth/2);//document.body.innerWidth/2;
		v = parseInt(windowHeight/2);//document.body.innerHeight/2;
		
		objOverlay.innerHTML = "<a href=\"javascript:void(0)\" onClick=\"cancelOvly()\"><img src=\"common/loading.gif\" alt=\"Loading...\" style=\"float:left;margin-top:"+v+"px;margin-left:"+h+"px;\"/></a>";
	}else{objOverlay.style.display = 'none';}

}

function cancelOvly()
{
	overlay(false);
}
function listCat()
{
	var frm = document.getElementById("filtroProd");
	var list = "";
	for (n=0;n<frm.elements.length;n++)
	{
		v = frm.elements[n];
		if ((v.type == "checkbox") && (v.checked))
		{
			list += ","+v.value;
		}
	}
	if (list.length > 0)
	{
		list = list.substr(1);
	}
	return list;
}
/* ENVIANDO FORMULARIO */
function sendFormCat()
{
	hideMenu();
	if ((http == null) || (http == undefined)) {return true;}
	if (changeCategoria)
	{
		lst=listCat();
		fic = 1;
		if (lst.length >0)
		{
			sndReq("index.php?fic="+fic+"&cat="+lst+"&ajax=true");
		}else{
			sndReq("index.php?fic="+fic+"&ajax=true");
		}
		return false;
		changeCategoria = false;
	}else{//document.getElementById("log").innerHTML = "";
		return false;
	}
	return false;
}


function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();


function sndReq(action) {
	//action = encodeURIComponent(action);
    http.open('get', action);
    http.onreadystatechange = handleResponse;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.setRequestHeader("Accept-Charset", "UTF-8");
    http.send(null);
}

function handleResponse() {
	if (http.readyState == 1)
	{
		overlay(true);
	}
    if(http.readyState == 4){
        var response = http.responseText;
        document.getElementById("contentProductos").innerHTML = response;
		overlay(false);
    }
}
