/*	
	------------------------------------------
	Crossbrowser-Helper javascript includefile
	Rickard Robin (C) 2008
	------------------------------------------
*/


// ---------------- GLOBALA STARTUPSCRIPT ----------------- //

// tar reda på var musen befinner sig.
var mouseX=0;var mouseY=0;
document.onmousemove=getMousePos;
function getMousePos(moveEvent)
	{
	mouseX=(window.event)? window.event.x : moveEvent.pageX;
	mouseY=(window.event)? window.event.y : moveEvent.pageY;
	}

// ----------------- BROWSER TESTER ----------------------- //


// -----------------  GLOBALA FUNKTIONER ------------------ //
function GetObjById(elmId){
	if (document.layers){return document.layers(elmId)}
	else if (document.all){return document.all(elmId)}
	else if (document.getElementById){return document.getElementById(elmId)}
	else {alert("Unsupported browser, Javascript will not function as expected.")}

}

// ---------- INTERCEPT FUNKTIONER FÖR ATT HINDRA SKICKA IN FARLIG KOD ------------ //
function CustomHtmlDecode(element_id) {
	try {
		var Obj = document.getElementById(element_id);
		var html = Obj.value;
	
		html = value.replace(/&lt;/gi, "<");
		html = value.replace(/&gt;/gi, ">");
		return html;
	}
	catch (err) {
		txt = "There was an error on this page (js:CustomCleanup).\n\n";
		txt += "Error description: " + err.description + "\n\n";
		txt += "Click OK to continue.\n\n";
		alert(txt);	
	}
	return;
}
function CustomHtmlEncode(element_id) {
	try {
		
		var Obj = document.getElementById(element_id);

		if (Obj == null)
		{ return; }
		
		
		var html = Obj.value;

		if (html == null)
		{ return; }

		html = html.replace(/</gi, "&lt;");
		html = html.replace(/>/gi, "&gt;");

		Obj.value = html;
		return Obj.innerHTML;
	}
	catch (err) {
		txt = "!There was an error on this page (js:CustomCleanup).\n\n";
		txt += "Error description: " + err.description + "\n\n";
		txt += "Click OK to continue.\n\n";
		alert(txt);
	}
	return;
}

//Göm eller visa beroende på parametrar.
function ShowHide(BoolShow, elmName){

		if (BoolShow == true) {
			GetObjById(elmName).style.visibility = "visible";
		}
		else {
			GetObjById(elmName).style.visibility = "hidden";
		}
}

//Göm eller visa beroende på parametrar.
function SwitchDisplay(elmName) {
    var objElem = document.getElementById(elmName);
    objElem.style.display = objElem.style.display == '' ? 'none' : ''; 
}



//funkar bara i IE
function moveToCenter(obj)
{
	GetObjById(obj).style.left=(10)+'px';
	GetObjById(obj).style.top=(parseInt(event.y - 7))+'px';
	GetObjById(obj).children[0].focus();
}


//Alerta ut med hjälp av Eval det man skickar in.
function EvalAlert(elmId){
	eval("alert(" + GetObjById(elmId).value + ")");
}


//flyttar objektet till där musen är, och om det finns något child, get det fokus.
function moveObj(objId,clicker, focusChildIndex)
{
	if (focusChildIndex == null) {focusChildIndex = 0}
	GetObjById(objId).style.left=(parseInt(mouseX))+'px';
	GetObjById(objId).style.top=(parseInt(mouseY - 7))+'px';
	if  (document.children) 
		{ 
			if  (GetObjById(objId).children[focusChildIndex] && GetObjById(objId).children[focusChildIndex].focus) {
				GetObjById(objId).children[0].focus(); 
			}
		}
	else {
		if  (GetObjById(objId).childNodes[focusChildIndex] && GetObjById(objId).childNodes[focusChildIndex].focus) {
				GetObjById(objId).childNodes[0].focus();
			}
		}
} 

function OpenWin(url, xwidth, yheight){
    window.open(url,'userviewer', 'status=0, toolbar=0, height='+ yheight +', width='+ xwidth +', resizable=1, scrollbars=1').focus()
}

function OpenWin(url, xwidth, yheight, windowname) {
    window.open(url, windowname, 'status=0, toolbar=0, height=' + yheight + ', width=' + xwidth + ', resizable=1, scrollbars=1').focus()
}


function OpenCustomWin(url, windowname, parameters){
    window.open(url,windowname, parameters).focus()
}

function ConvertNumber(inp){
    var beginsWithZero = inp.charAt(0) == 0;
    var res = parseInt(inp,10);
    if (beginsWithZero == true)  {
        res = String("0") + String(res)
    }
    return res
   }


