/**
 * Contains functions and attributes to get position of mousecursor and visible window rect.
 */

var mousex = 0;
var mousey = 0;

/**
 * Get the current location of the mouse pointer. Store info in 'mousex' and 'mousey'.
 */
function getMouseXY(e)
{
	if ( !e )
	{
		e = window.event;
	}

	if ( e )
	{
		if ( e.pageX || e.pageY )
		{
			mousex = e.pageX;
			mousey = e.pageY;
		}
		else if ( e.clientX || e.clientY )
		{
			mousex = e.clientX + document.body.scrollLeft;
			mousey = e.clientY + document.body.scrollTop;
		}
	}
}

var frameWidth = 1000;
var frameHeight = 750;

/**
 * Get the current viewable area of the browser window. Store info in 'frameWidth' and 'frameHeight'.
 */
function getDocumentSize()
{
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
}

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

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

function resizeMain(minHeight, minIncrease)
{
	var mainTable = document.getElementById('mainTable');
	if ( mainTable.offsetHeight < minHeight )
	{
		mainTable.style.height = minHeight+'px';
		return;
	}
	
	var nextStep = mainTable.offsetHeight - (mainTable.offsetHeight % minIncrease) + minIncrease;
	mainTable.style.height = nextStep+'px';
}

function loginDemanded()
{
    alert('Du m\345ste vara inloggad f\366r att kunna anv\344nda denna funktion.');
}

function checkSubmitForm(event)
{
	var anv = document.getElementById('anvnamn');
	var pass = document.getElementById('losenord');
	
	if (event.keyCode == 13 || event.keyCode == 10)
	{
		if ( anv.value == "" )
		{
			anv.focus();
			return false;
		}
		else if ( pass.value == "" )
		{
			pass.focus();
			return false;
		}
		else
		{
			document.getElementById('frm_login').submit();
			return false;
		}
	}
}

function trim(str)
{
	return rtrim(ltrim(str));
}

function ltrim(str)
{
	var i = 0;
	while(i < str.length && str[i] == ' ' || str[i] == '\n')
	{
		i++;
	}
	return str.substring(i, str.length);
}

function rtrim(str)
{
	var i = str.length - 1;
	while(i > 0 && str[i] == ' ' || str[i] == '\n')
	{
		i -= 1;
	}
	return str.substring(0, i + 1);
}

function loggedOut()
{
	location.href = '/includes/misc/redirect.php';
}

/**
 * A field where only numbers are allowed to be entered, strip anything else.
 * Optionally negative character (-) may be entered, but then only FIRST
 */
function onlyNumbers(fieldId, negativeAllowed)
{
	var field = document.getElementById(fieldId);
	var value = field.value;
	
	// Strip anything that is not a number
	var fixedValue = '';
	for (var i = 0; i < value.length; i++)
	{
		var curChar = value.charCodeAt(i);
		if (curChar >= 48 && curChar <= 57 || (negativeAllowed && i == 0 && value.charAt(i) == '-'))
		{
			fixedValue += value.charAt(i);
		}
	}
	
	if (value != fixedValue)
	{
		field.value = fixedValue;
	}
}

/**
 * A value must be inside the interval (or possibly empty).
 * A value above max will result in max, and below min will result in min
 */
function insideInterval(fieldId, min, max, emptyAllowed)
{
	var field = document.getElementById(fieldId);
	var value = field.value;
	
	if (emptyAllowed && value == '')
	{
		return;
	}
	
	if (value > max)
	{
		field.value = max;
		return;
	}
	
	if (value < min)
	{
		field.value = min;
		return;
	}
}

function replaceAllOccurances(string, find, replace)
{
	// If replace contains find, an infinite loop will occur... prevent this by doing no replacing at all
	if ( replace.indexOf(find) != -1 )
	return;
	
	var matchPos = string.indexOf(find);
	
	while (matchPos != -1)
	{
		string = string.replace(find, replace);
		matchPos = string.indexOf(find);
	}
	
	return string;
}

function getMultipleChar(charStr, num)
{
	var returnStr = '';
	for (var i = 0; i < num; i++)
	{
		returnStr += ''+charStr;
	}
	
	return returnStr;
}

function correctDate(dateString)
{
	// Take out the parts
	var dateParts = dateString.split('-');
	
	if (dateParts.length != 3 || isNaN(parseInt(dateParts[0])) || isNaN(parseInt(dateParts[1])) || isNaN(parseInt(dateParts[2])))
	{
		// Not three parts OR any of them is not a number
		return false;
	}
	
	if (dateParts[0].length != 4 || dateParts[1].length != 2 || dateParts[2].length != 2)
	{
		// Any part isn't of the correct length
		return false;
	}
	
	var year = parseInt(dateParts[0]);
	var month = (dateParts[1].substring(0, 1) == '0' ? parseInt(dateParts[1].substring(1)) : parseInt(dateParts[1]));
	var day = (dateParts[2].substring(0, 1) == '0' ? parseInt(dateParts[2].substring(1)) : parseInt(dateParts[2]));
	
	if (year < 1900 || year > 2100)
	{
		// Year not inside interval
		return false;
	}
	
	if (month < 1 || month > 12)
	{
		// Month not correct
		return false;
	}
	
	if (day < 1)
	{
		// Day not correct (1)
		return false;
	}
	
	var leapyear = new Date(year, 1, 29).getDate() == 29;
	var daysMax = new Array(31, (leapyear ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (day > daysMax[month-1])
	{
		// Day not correct (2)
		return false;
	}
	
	return true;
}

// Initiate XML-object
var xmlhttp=false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch (e)
	{
		xmlhttp=false;
	}
}

if (!xmlhttp && window.createRequest)
{
	try
	{
		xmlhttp = window.createRequest();
	}
	catch (e)
	{
		xmlhttp=false;
	}
}
