// JavaScript Document
var alerts = Array();
var browserWindowHeight = 0;
var browserWindowWidth = 0;
var sessionCookieName = 'IPSID';
var siteCookieName = 'memberSiteId';

function setCurSite() {
	var siteId = document.getElementById('userSites').value;
	
	if (Number(siteId)) {
		createCookie(siteCookieName, siteId);
		window.location = 'stats.php';
	}
	else {
		window.location = 'main.php';
	}
}

var http_request = false;
function createRequest() {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	return http_request;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// Login functions 
function checkLogin() {
	var myForm = document.forms.loginForm;
	
	if (!myForm.user.value || !myForm.pass.value)
		return false;
		
	return true;
}

function login() {
	var myForm = document.forms.loginForm;
	
	http_request = createRequest();
	if (!http_request) {
		alert( 'Error: No se pudo crear objeto HttpXmlRequest.' )
		return;
	}
	
	document.getElementById('loginButton').setAttribute('disabled', 'disabled');
	document.getElementById('loginButton').setAttribute('value', 'Espere...');
	
	http_request.open( 'POST', 'member/xml/xml_login.php', true );
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = performLogin;	
	http_request.send( 'user=' + myForm.user.value + '&pass=' + myForm.pass.value);
		
}

function performLogin() {
	if (http_request.readyState == 4) {
		document.getElementById('loginButton').removeAttribute('disabled');
		document.getElementById('loginButton').setAttribute('value', 'Ingresar');
		if (http_request.status == 200) {
			var root = http_request.responseXML.getElementsByTagName('root').item(0);			
			if (root.getAttribute('success') == '1') {
				createCookie( sessionCookieName, root.getAttribute('sessionid') );
				window.location = '/member/main.php';
			}
			else {
				alert( 'Nombre de usuario o contraseńa no válidos.' );
				document.forms.loginForm.pass.value = '';
			}		
		}
		else {
			alert( 'Se ha producido un error enviando la información.\nInténtelo más tarde (' + http_request.status + ')' );
		}
	}
}

function logoff() {
	http_request = createRequest();
	if (!http_request) {
		alert( 'Error: No se pudo crear objeto HttpXmlRequest.' )
		return;
	}
	
	invalidate(); 
	
	http_request.open( 'POST', '/member/xml/xml_logoff.php', true );
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	http_request.onreadystatechange = performLogoff;	
	http_request.send( 'sessionId=' + readCookie(sessionCookieName) );
	
}

function performLogoff() {	
	if (http_request.readyState == 4) {
		revalidate();
		if (http_request.status == 200) {
			eraseCookie(sessionCookieName);
			window.location = '/index.php';
		}
		else {
			alert( 'Se ha producido un error enviando la información.\nInténtelo más tarde (' + http_request.status + ')' );
		}
	}
}

var onsetmainstyle = false;
function setMainStyle() {
	
	_calculateBrowserSize();
	
	
}
window.onresize=setMainStyle;

function _calculateBrowserSize() {
	if (navigator.userAgent.indexOf('MSIE') != -1) {
		if (document.documentElement.clientWidth == 0)
			browserWindowWidth = document.body.clientWidth;
		else	
			browserWindowWidth = document.documentElement.clientWidth;
			
		if (document.documentElement.clientHeight == 0)
  			browserWindowHeight = document.body.clientHeight;
		else
			browserWindowHeight = document.documentElement.clientHeight;
	}
	else {
		browserWindowWidth = window.innerWidth;
  		browserWindowHeight = window.innerHeight;
	}
}

//invalidate and set popup windows
function makeTransparent( obj, ndx ) { 
	if (obj.style) {		
		if (obj.style.filter !== undefined) 
			obj.style.filter = 'alpha(opacity=' + ndx + ');';
		else {	
			if (obj.style.MozOpacity !== undefined) {			
				obj.style.MozOpacity = '0.' + ndx;
			}
			else {	
				if (obj.style.opacity !== undefined)
					obj.style.opacity = '0.' + ndx;
			}
		}
	}
}

var invDiv = false;
var waitDiv = false;
function invalidate() {
	if (!browserWindowHeight || !browserWindowWidth) 
		_calculateBrowserSize();
		
	if (!invDiv) {
		invDiv = document.createElement('div');
		invDiv.id = 'invalidatorPanel';
		invDiv.style.zIndex = '500';
		invDiv.style.backgroundColor = '#FFFFFF';
		makeTransparent(invDiv, 50);
		invDiv.style.position = 'absolute';
		invDiv.style.top = '0';
		invDiv.style.left = '0';
		if (navigator.userAgent.indexOf('MSIE') != -1) {
			invDiv.style.width = browserWindowWidth + 'px';
			invDiv.style.height = browserWindowHeight + 'px';
		}
		invDiv.style.bottom = '0';
		invDiv.style.right = '0';
	}
	
	document.body.appendChild( invDiv );
}

function revalidate() {
	if (document.getElementById('invalidatorPanel')) 
		document.body.removeChild( document.getElementById('invalidatorPanel') );
}

function setWait() {
	if (!browserWindowHeight || !browserWindowWidth) 
		_calculateBrowserSize();
		
	if (!waitDiv) {
		waitDiv = document.createElement('div');
		waitDiv.id = 'waitWindow';
		waitDiv.style.zIndex = '501';
		waitDiv.style.backgroundColor = '#CCC8B6';
		waitDiv.style.border = '4px solid #FFFFFF';
		makeTransparent(waitDiv, 90);
		waitDiv.style.position = 'absolute';
		waitDiv.style.height = '30px';
		waitDiv.style.width = '200px';
		waitDiv.style.left = ((browserWindowWidth / 2) - 100) + 'px';
		waitDiv.style.top = ((browserWindowHeight / 2) - 15) + 'px';
		
		var tempP = document.createElement('p');
		tempP.style.fontFamilty = 'Arial';
		tempP.style.fontSize = '12px';
		tempP.style.color = '#5D5D5D';
		tempP.style.margin = '7px';
		tempP.style.textAlign = 'center';
		
		tempP.appendChild( document.createTextNode( 'Espere por favor...' ));
		waitDiv.appendChild(tempP);
	}
	
	invalidate();
	document.body.appendChild( waitDiv );
}

function stopWait() {
	if (document.getElementById('waitWindow')) {		
		document.body.removeChild( document.getElementById('waitWindow') );
		revalidate();
	}
}

//window dragging functions
var dragSys = {'activeObj' : false, 'x' : 0, 'y' : 0, 'offsetX' : 0, 'offsetY': 0 };
function startDragging(e) {
	if (!e)
		e = window.event;
	
	if  (e.target ) 
		dragSys.activeObj = e.target;
	else
		dragSys.activeObj = e.srcElement;
	
	if (dragSys.activeObj.nodeType == 3) 
		dragSys.activeObj = dragSys.activeObj.parentNode.parentNode;
	else
		dragSys.activeObj = dragSys.activeObj.parentNode;
	
	if (e.offsetX) {
		dragSys.offsetX = e.offsetX;
		dragSys.offsetY = e.offsetY
	}
	else {
		dragSys.offsetX = e.pageX - document.getBoxObjectFor(dragSys.activeObj).x;
		dragSys.offsetY = e.pageY - document.getBoxObjectFor(dragSys.activeObj).y;
	}
	
	if (e.pageX) {
		dragSys.x = e.pageX;
		dragSys.y = e.pageY;
	}
	else {
		if (e.x) {
			dragSys.x = e.x + document.documentElement.scrollLeft;
			dragSys.y = e.y + document.documentElement.scrollTop;
		}
		else {
			dragSys.x = 0;
			dragSys.y = 0;
		}
	}
	
	dragSys.x = dragSys.x - dragSys.offsetX;
	dragSys.y = dragSys.y - dragSys.offsetY;
	
	document.onmousemove = doDragging;
}

function stopDragging(e) {
	document.onmousemove = null;
}

function doDragging(e) {
	if (!e)
		e = window.event;
	
	var x, y;
	if (e.pageX) {
		x = e.pageX;
		y = e.pageY;
	}
	else {
		if (e.x) {
			x = e.x + document.documentElement.scrollLeft;
			y = e.y + document.documentElement.scrollTop;
		}
		else {
			x = 0;
			y = 0;
		}
	}
	
	dragSys.x = x - dragSys.offsetX;
	dragSys.y = y - dragSys.offsetY;
	
	dragSys.activeObj.style.left = dragSys.x + 'px';
	dragSys.activeObj.style.top = dragSys.y + 'px';
}

Date.prototype.toString = function() {
	return this.getDate() + '/' + (this.getMonth()+1) + '/' + this.getFullYear();
}
