var bodyMouseX = 0;
var bodyMouseY = 0;
var prevBodyMouseX = 0;
var prevBodyMouseY = 0;

var dontKillNext = false;

var blocChangePage = false;

function getObjectZoneLeft(obj)
{
	if (obj.offsetParent && obj.offsetParent.id != "map_map" )
		return (obj.offsetLeft + getObjectZoneLeft(obj.offsetParent));
	else
		return (obj.offsetLeft);
}

function getObjectZoneTop(obj)
{
	if (obj.offsetParent && obj.offsetParent.id != "map_map" )
		return (obj.offsetTop + getObjectZoneTop(obj.offsetParent));
	else
		return (obj.offsetTop);
}

function getCorrectPopCoord(obj, pop_width, pop_height)
{
	var obj_width = obj.clientWidth;
	var obj_height = obj.clientHeight;
	var obj_map_x = getObjectZoneLeft(obj);
	var obj_map_y = getObjectZoneTop(obj);
	var resX, resY;
	
	var bestWidth, bestHeight;
	
	bestWidth = (map_zone_width - (obj_width + obj_map_x));
	
	if( obj_map_x > bestWidth )
	{
		resX = obj_map_x - pop_width - 10;	// Left big
		bestWidth = obj_map_x;
	}
	else
		resX = obj_map_x + obj_width + 10;	// Right big
	
	bestHeight = (map_zone_height - (obj_height + obj_map_y));
	
	if( obj_map_y > bestHeight )
	{
		resY = obj_map_y - pop_height - 10;	// Top big
		bestHeight = obj_map_y;
	}
	else
		resY = obj_map_y + obj_height + 10;	// Bottom big
	
	if( (bestWidth - pop_width) > (bestHeight - pop_height) )
	{
		// Width big
		resY = map_zone_height / 2 - pop_height / 2;
		if( resY < 10 )
			resY = 10;
		else if( resY > map_zone_height - (pop_height + 10) )
			resY = map_zone_height - (pop_height + 10);
	}
	else
	{
		// Height big
		resX = map_zone_width / 2 - pop_width / 2;
		if( resX < 10 )
			resX = 10;
		else if( resX > map_zone_width - (pop_width + 10) )
			resX = map_zone_width - (pop_width + 10);
	}
	
	return new Array(parseInt(resX), parseInt(resY));
}

function makeFlash(parent, id, top, left, width, height, file, flashvar, trans)
{
	var flashObj = document.createElement("object");
	
	var allow = document.createElement("param");
	allow.name = "allowScriptAccess";
	allow.value = "always";
	
	var movie = document.createElement("param");
	movie.name = "movie";
	movie.value = file;
	
	var quality = document.createElement("param");
	quality.name = "quality";
	quality.value = "high";
	
	var base = document.createElement("param");
	base.name = "base";
	base.value = ".";
	
	var gc = document.createElement("param");
	gc.name = "flashvars";
	gc.value = flashvar;
	
	flashObj.appendChild(allow);
	flashObj.appendChild(movie);
	if( trans )
	{
		var alpha = document.createElement("param");
		alpha.name = "wmode";
		alpha.value = "transparent";
		flashObj.appendChild(alpha);
	}
	flashObj.appendChild(quality);
	flashObj.appendChild(base);
	flashObj.appendChild(gc);

	parent.appendChild(flashObj);
	
	flashObj.setAttribute("id", id);
	
	flashObj.type = "application/x-shockwave-flash";
	
	flashObj.style.top = top;
	flashObj.style.left = left;
	flashObj.style.position = "absolute";
	
	flashObj.data = file;
	flashObj.width = width;
	flashObj.height = height;
	
	return flashObj;
}

function DateTimeToFrStr(date)
{
	var split1 = date.split(' ');
	var split2 = split1[0].split('-');
	if( split2.length == 3 )
		return split2[2] + "/" + split2[1] + "/" + split2[0];
	return "";
}

function changeCurrentPage(file)
{
	if( blocChangePage )
		return;
	
	clearTimeout( topUpdaterNum );
	
	var i = toUpdateObjects.length;
	while( i > 0 )
	{
		toUpdateObjects.pop();
		i--;
	}
	
	cleanDataBase();
	
	if( openedFile != "" && openedFile != file )
		pagesHis.push( openedFile );
	
	if( !dontKillNext )
	{
		var max = pagesNext.length;
		var ki = 0;
		while( ki < max )
		{
			pagesNext.pop();
			ki++;
		}
	}
	
	loadXMLPage( file, 0, pageXHR );
}

function errorPopup(error)
{
	document.getElementById('popup_error').style.display = "block";
	document.getElementById('popup_error').firstChild.nodeValue = error;
	addEvent( document.getElementById('popup_error_close'), 'click', closeErrorPopup, false );
}

function closeErrorPopup()
{
	removeEvent( document.getElementById('popup_error_close'), 'click', closeErrorPopup, false );
	document.getElementById('popup_error').style.display = "none";
}

function trackSqlError( value )
{
	// Return true if there is no error
	split1 = value.split("&");
	if( split1.length == 2 )
	{
		split2 = split1[0].split("=");
		if( split2.length == 2 )
		{
			if( split2[0] == 'result' && split2[1] == '-1' )
			{
				split3 = split1[1].split("=");
				if( split3.length == 2 )
				{
					errorPopup(split3[1]);
					return false;
				}
			}
		}
	}
	return true;
}

function setBodyMouse(e)
{
	prevBodyMouseX = bodyMouseX;
	prevBodyMouseY = bodyMouseY;
	bodyMouseX = e.clientX;
	bodyMouseY = e.clientY;
}

function recursiveDeleteChildNodes( node )
{
	for(var i = node.childNodes.length - 1; i >= 0; i--)
	{
		if( node.childNodes[i].length > 0 )removeAllNodes( node.childNodes[i] );
		var currentDeleteNode = node.removeChild(node.childNodes[i]);
		delete( currentDeleteNode );
		currentDeleteNode = null;
	}
}

function removeAllNodes( node, deleteThisNode )
{
	recursiveDeleteChildNodes( node );
	if( deleteThisNode )
	{
		var currentDeleteNode = node.parentNode.removeChild(node);
		delete( currentDeleteNode );
		currentDeleteNode = null;
	}
}

function delFromArray( array, object )
{
	var result = new Array();
	for( var i in array )
	{
		if( array[i] != object )
			result.push( array[i] );
	}
	delete( array );
	return result;
}

function addEvent( obj, event, func, mode )
{ 
	if( obj.addEventListener ) 
		obj.addEventListener( event, func, mode); 
	else 
		obj.attachEvent( 'on'+event, func); 
}

function removeEvent( obj, event, func, mode )
{
	if( obj.removeEventListener ) 
		obj.removeEventListener( event, func, mode); 
	else 
		obj.detachEvent( 'on'+event, func);
}

function setOpacity( obj, opacity )
{
	if( obj.style.filter != null )
	{
		obj.style.filter = 'alpha(opacity=' + opacity + ')';
	}
	else
	{
		obj.style.setProperty( "-moz-opacity", opacity / 100, "" );
		obj.style.setProperty( "-khtml-opacity", opacity / 100, "" );
		obj.style.setProperty( "opacity", opacity / 100, "" );
	}
}

function getMouseX()
{
	return bodyMouseX;
}

function getMouseY()
{
	return bodyMouseY;
}

function ajaxRequest( file, xhr, cb, params )
{
	if(window.ActiveXObject)
		xhr.abort();
	
	xhr.onreadystatechange = function()
	{
		if( xhr.readyState == 4 )
		{
			if( xhr.status == 200 )
			{
				if( cb != null )
					cb( xhr.responseText );
			}
		}
	};
	
	xhr.open( "POST", file,  true );
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.send( params ); 
}

// Return false if error
function isCorrectEmail( email )
{
	var i = 0;
	var nbTilt = 0;
	var nbPoint = 0;
	var startCheck = false;
	while( i < email.length )
	{
		if( email.charAt(i) == '@' )
		{
			startCheck = true;
			nbTilt++;
		}
		else if( startCheck && email.charAt(i) == '.' )
			nbPoint++;
		i++;
	}
	if( nbPoint != 1 || nbTilt != 1 )
		return false;
	return true;
}