function newWindow(mypage, myname, w, h, scroll) {
     var winLeft = (screen.width - w) / 2;
     var winTop = (screen.height - h) / 2;
     winProps = "height=" + h + ",width=" + w + ",top=" + winTop + ",left=" + winLeft + "," + scroll;
     win = window.open(mypage, myname, winProps);
     if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); } 
     return win;    
}

var posicX =0;
var posicY =0;
	
if (!document.all) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = get_mouse_xy;

function get_mouse_xy(e) {
	posicX =(document.all)? event.x : e.pageX;
	posicY =(document.all)? event.y : e.pageY;
	posicX += 10;
	posicY += 12;
	
	return true;
}

function showToolTip(text){
	idToolTip = "toolTip";
	w = Math.min((text.length * 5),250);
	//w = 300;
	if(!document.getElementById(idToolTip)){
		divholder = document.createElement("div");
		divholder.id = idToolTip;
		divholder.style.display = 'none';
		divholder.style.width = w + 'px';
		divholder.style.height = "auto";
		divholder.style.border = "1px solid #CCCCCC";
		divholder.style.backgroundColor = "#FCFCBD";
		divholder.style.position = "absolute";
		divholder.style.color = "#222222";
		divholder.style.padding = "2px";
		divholder.style.fontSize = "10px";
		divholder.style.zIndex = "200";
		document.body.appendChild(divholder);
	}else{
		divholder = document.getElementById(idToolTip);
		divholder.style.width = w + 'px';
	}
	x = posicX;
	y = posicY;
	if(document.body.offsetWidth < posicX+w+20){
		x = document.body.offsetWidth - (w+20);
	}
	
	divholder.style.left = x+"px";
	divholder.style.top = y+"px";
	document.getElementById(idToolTip).innerHTML = ""+text+"";
	document.getElementById(idToolTip).style.visibility = "visible";
	document.getElementById(idToolTip).style.display = "";
}


function hideToolTip(){
	document.getElementById("toolTip").style.visibility = "hidden";
}

function showDivDateChoose(form,campo){
	idDiv = "DateChoose";
	w = 300;
	if(!document.getElementById(idDiv)){
		divholder = document.createElement("div");
		divholder.id = idDiv;
		divholder.style.width = "auto";
		divholder.style.height = "auto";
		divholder.style.border = "1px solid #CCCCCC";
		divholder.style.backgroundColor = "#DBEDF0";
		divholder.style.position = "absolute";
		divholder.style.color = "#222222";
		divholder.style.padding = "2px";
		divholder.style.fontSize = "10px";
		divholder.style.zIndex = "200";
		document.body.appendChild(divholder);
	}else{
		divholder = document.getElementById(idDiv);
	}
	//x = posicX;
	//y = posicY;
	x = findPosX(document.f.elements[campo]) + 110;
	y = findPosY(document.f.elements[campo]) - 40;
	
	if(document.body.offsetWidth < posicX+w){
		//x = document.body.offsetWidth - w;
	}
	
	divholder.style.left = x+"px";
	divholder.style.top = y+"px";
	
	var texts = getTextsDateChooser();
 	content = 	"<div id='dateChoose'>";
 	content += 	"<ul>";
	content += 	"<li class='sigue hand'><a href='javascript:showCalendar(\""+form+"\",\""+campo+"\");javascript:hideDivDateChoose();'>"+texts[0]+"</a></li>";
	content += 	"<li class='sigue hand'><a href='javascript:hideDivDateChoose();'>"+texts[1]+"</a></li>";
	content += 	"</ul>";
	
	content += 	"<ul>";
	content += 	"<li class='sign'><a href='javascript:addDayToDate(-1,\""+form+"\",\""+campo+"\");'><b>-</b></a></li>";
	content += 	"<li><a href='javascript:calculateDate(0,\""+form+"\",\""+campo+"\");javascript:hideDivDateChoose();'>"+texts[2]+"</a></li>";
	content += 	"<li class='sign'><a href='javascript:addDayToDate(1,\""+form+"\",\""+campo+"\");'><b>+</b></a></li>";
	content += 	"</ul>";
	
	content += 	"<ul>";
	content += 	"<li class='sign'><a href='javascript:addDayToDate(-7,\""+form+"\",\""+campo+"\");'><b>-</b></a></li>";
	content += 	"<li class='semana'>"+texts[3]+"</li>";
	content += 	"<li class='sign'><a href='javascript:addDayToDate(7,\""+form+"\",\""+campo+"\");'><b>+</b></a></li>";
	content += 	"</ul>";
	
	content += 	"</div>";
	content += 	"";
	document.getElementById(idDiv).innerHTML = ""+content+"";
	document.getElementById(idDiv).style.visibility = "visible";
}

function getTextsDateChooser(){
	var lang = getCookie('lang');
	if (lang == '' || lang == undefined){
		lang = 'es';
	}
	if (lang == 'es'){
		texts = new Array('Elegir','Cerrar','Hoy','Semana');
	}
	
	if (lang == 'eu'){
		texts = new Array('Aukeratu','Itxi','Gaur','Astea');
	}
	return texts;
}


function hideDivDateChoose(){
	document.getElementById("DateChoose").style.visibility = "hidden";
}

function calculateDate(dif,form,campo){
	MINUTE = 60 * 1000;
 	HOUR = MINUTE * 60;
 	DAY = HOUR * 24;

	
	today = (new Date()).getTime();
 	newFecha = new Date(today + (DAY * (dif)));
 	newFechaFormat = newFecha.getDate() + '-' + (newFecha.getMonth()+1) + '-' + (newFecha.getFullYear());
 	
	setDate(form,campo,newFechaFormat);
}

function addDayToDate(dif,form,campo){
	MINUTE = 60 * 1000;
 	HOUR = MINUTE * 60;
 	DAY = HOUR * 24;

	elInputVal = eval("document." + form + "['" + campo + "'].value");
	if (elInputVal == ''){
		today = (new Date()).getTime();
	}else{
		fecha = elInputVal.split("-");
		d = fecha[0];
		m = fecha[1]-1;
		y = fecha[2];
		today = (new Date(y,m,d)).getTime();
	}
 	newFecha = new Date(today + (DAY * (dif)));
 	newFechaFormat = newFecha.getDate() + '-' + (newFecha.getMonth()+1) + '-' + (newFecha.getFullYear());
 	
	setDate(form,campo,newFechaFormat);
}

function setPointer(theRow, thePointerColor)
{
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }
	
	var theCells = null;
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = thePointerColor;
    }

    return true;
} // end of the 'setPointer()' function

function changeHeightTextArea(elName,inc)
{
	tArea = document.getElementById(elName);
	if(parseInt(tArea.style.height) <= 30 && inc < 0) return;
	tArea.style.height = parseInt(tArea.style.height) + inc +'px';
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}


var detect = navigator.userAgent.toLowerCase();

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	return place;
}


// encuentra la posicion x en pixels del objeto pasado
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

// encuentra la posicion y en pixels del objeto pasado
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y){
		curtop += obj.y;
	}
	
	if (checkIt('safari')){
		curtop += 25;
	}
	
	return curtop;
}


function clickDiaCalMini(idTd){
	s.ejecuta('agenda');
}
function clickDiaCalSearch(idTd){
	//alert(idTd);
	document.f.fechaCal.value = idTd;
	s.ejecuta('buscarPublic');
}


var idEnRoll = -1;
var classEnRoll = '';

function rollOverDiaCalMini(idTd){
	return;
	if(idEnRoll != -1){
		document.getElementById(idEnRoll).className = classEnRoll;
	}
	classEnRoll = document.getElementById(idTd).className;
	document.getElementById(idTd).className = "diasRoll";
	idEnRoll = idTd;
}





// XHTMLHTTPREQUEST
var req;
var resultsDivId;
var fieldToSet;
var fieldToSet2;
var myFunction = 'showData';

function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, null);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            if(myFunction == 'showData'){
            	showData(req.responseText);
            }
            if(myFunction == 'showContent'){
            	showContent(req.responseText);
            }
        } else {
            // alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function getFromRequest(accion, resultsDiv, field, field2) {
	resultsDivId = resultsDiv;
	document.getElementById(resultsDivId).style.display = 'none';
	fieldToSet = field;
	if(document.getElementById(field2)){
		fieldToSet2 = document.getElementById(field2);
	}else{
		fieldToSet2 = '';
	}
	var texto = fieldToSet.value;
	if(texto.length < 2){
		document.getElementById(resultsDivId).style.display = 'none';
		return;
	}
	var azar=Math.round(Math.random()*5000);
	var url = "../utilidades/ctrl_XMLHttpRequest.php?azar="+azar+"&accion="+ accion +"&q=" + texto;
	
	loadXMLDoc(url);
}

function getContentFromRequest(url, resultsDiv) {
	resultsDivId = resultsDiv;
	myFunction = 'showContent';
	//document.getElementById(resultsDivId).style.display = 'none';
	//document.getElementById(resultsDivId).innerHTML = 'Calculando...';
	var azar=Math.round(Math.random()*5000);
	url += "&azar="+azar;
	loadXMLDoc(url,'content');
}

function showContent(contenido) {
	var resultsContent = document.getElementById(resultsDivId);
	resultsContent.innerHTML = contenido;
	resultsContent.style.display = 'block';
}

function showData(contenido) {
	var resultsContent = document.getElementById(resultsDivId);
	resultsContent.innerHTML = contenido;
	alert("ddd");
	var l = findPosX(document.getElementById(fieldToSet.id));
	var t = findPosY(document.getElementById(fieldToSet.id)) + 20;
	
	if (checkIt('safari')){
		t += 25;
	}
	
	resultsContent.style.left = l +'px';
	resultsContent.style.top = t +'px';
	resultsContent.style.display = 'block';
}

function setValInput(valTxt,idVal) {
	fieldToSet.value = valTxt;
	if(fieldToSet2 != ''){
		fieldToSet2.value = idVal;
	}
	document.getElementById(resultsDivId).style.display = 'none';
}