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) {
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posicX = e.pageX;
		posicY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		if(document.body==null) return;
		if (document.documentElement && document.documentElement.scrollTop){
			var theTop = document.documentElement.scrollTop;
			var theLeft = document.documentElement.scrollLeft;
		}else if (document.body){
			var theTop = document.body.scrollTop;
			var theLeft = document.body.scrollLeft;
		}
		posicX = e.clientX + theLeft;
		posicY = e.clientY + theTop;
	}
	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,where)
{
		
	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);
	}
	
	
	
	var el = document.f.elements[campo];
	x = findPosX(el);
	y = findPosY(el);
	
	
	switch(where) {
	case 'bottom':
		x = x - 1;
		y = y + 19;
		break;
	case 'right':
	default:
		elWidth = parseInt(el.style.width);
		x = x + elWidth + 15;
		y = y - 50;
		break;
	}
	
	if(document.body.offsetWidth < posicX+w){
		//x = document.body.offsetWidth - w;
	}
	
	divholder.style.left = x+"px";
	divholder.style.top = y+"px";
	
    divholder.innerHTML = getDivDateChoose(form, campo);
    divholder.style.visibility = "visible";
}

function getDivDateChoose(form, campo)
{
    content = "<div id='dateChoose'>";
    content += "<ul>";
    content += "<li class='sigue hand'><a href='javascript:showCalendar(\""+form+"\",\""+campo+"\");javascript:hideDivDateChoose();'>Elegir</a></li>";
    content += "<li class='sigue hand'><a href='javascript:hideDivDateChoose();'>Cerrar</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();'>Hoy</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'>Semana</li>";
    content += "<li class='sign'><a href='javascript:addDayToDate(7,\""+form+"\",\""+campo+"\");'><b>+</b></a></li>";
    content += "</ul>";
    
    content += "</div>";
    content += "";

    return content;
}

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 setPtr(theRow, theClass)
{
     theRow.className = theClass;
}
function changeHeightTextArea(elName,inc)
{
	var tArea = document.getElementById(elName);
	var actualHeight = parseInt(tArea.style.height);
	if(actualHeight <= 60 && inc < 0) return;
	if((actualHeight + inc) < 60) inc = 60 - actualHeight;
	if(Effect){
		// cual es el porcentaje de los pixeles a mover?
		
		var porcentaje = 100 + ((inc * 100) / (Math.max(60,actualHeight)));
		
		var test = new Effect.Scale(tArea, porcentaje, Object.extend({ scaleContent: false, scaleX: false, scaleMode: 'box'}, {}));
	}else{
		tArea.style.height = actualHeight + 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';
	
	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;
	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';
}

var isExplorer=checkIt('msie');
function validaMouseOut(elem){
		if(isExplorer){
			if (!e) var e = window.event;
			if (e.target) tg = e.target;
			else if (e.srcElement) tg = e.srcElement;
			
			var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
			if(!reltg) return false;					
			while (reltg.className != elem.className && reltg.nodeName != 'BODY'){
				if(reltg.parentNode==null) return false;
				reltg= reltg.parentNode;
			}
			if (reltg.className == elem.className) return false;		
		}
		return true;
}

function checkDirEmailOnServer(dirEmail)
{
	var opt = {
					// Use POST
					method: 'post',
					asynchronous:true,
					// Send this lovely data
					postBody: 'accion=checkEmail&q='+dirEmail,
					// Handle successful response
					onSuccess: function(t) {
						//
						document.getElementById('resultsDiv').innerHTML = t.responseText;
						document.getElementById('resultsDiv').style.left = posicX +'px';
						document.getElementById('resultsDiv').style.top = posicY +'px';
						document.getElementById('resultsDiv').style.display = 'block';
						new Effect.Appear('resultsDiv');
						setTimeout("new Effect.Opacity(\'resultsDiv\',{ duration: 2.0, transition: Effect.Transitions.linear, from: 1.0, to: 0 })",3000);
					}
				};

			new Ajax.Request('../utilidades/ctrl_XMLHttpRequest.php', opt);
}
	function confirmEmptyCesta(url)
	{
		if(confirm('Se vaciará completamente su cesta de compra. Pulse ACEPTAR para seguir adelante o CANCELAR para cancelar la operación')){
			emptyCesta(url);
			return false;
		}else{
			return false;
		}
	}
