
/** FÖR POLISMEDDELANDE **/
document.write("<style type=\"text/css\">.MessageBlock { display:none; }</style>");

function showPoliceMessages(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className))
		{
			oElement.className = "updated";
		}
	}
	return false
}
/** SLUT **/

function popUp(winURL) 
{
    window.open(winURL,"popup","width=700,height=500,toolbar=yes,menubar=yes,scrollbars=yes,location=yes,directories=yes,status=yes,resizable=1");
}

function getElementsByClassNameFunction(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className))
		{
			oElement.onclick = function ()
			{
				popUp(this);
				return false
			}
		}
	}
	return false
}

function wopen(url, name, w, h) 
{
    var win = window.open(url, name,
    'width=' + w + ', height=' + h + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=yes, resizable=yes');
    win.focus();
}

window.onload = function() 
{
	if (!document.getElementsByTagName) return false;
	getElementsByClassNameFunction(document, "a", "popup");
	
	checkCookie();
	
	showPoliceMessages(document, "div", "MessageBlock");
}

function returnObjById(id)
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}


// MESSAGE ALARM COOKIE FUNCTIONS START
function checkCookie(){
	var tempArray;
	var delim = "|";
	var currentCookie = readCookie("RPSProfile");
	var updatedDate = "";
	
	if (currentCookie){
		var messageIDs = currentCookie.indexOf("AlarmPageIds=");
		var cookieLength = currentCookie.length;
        
		if (messageIDs != -1){
			// id to expiry date dictionary
			var cookieIDs = currentCookie.substring((messageIDs + "AlarmPageIds=".length), cookieLength)
			
			if (cookieIDs.indexOf("&") != -1){
				tempArray = cookieIDs.substring(0, cookieIDs.indexOf("&")).split(",");
			}
			else{
				tempArray = cookieIDs.split(",");
			}
			
			for(i = 0; i < tempArray.length; i++){
				var id = tempArray[i].substring(0, tempArray[i].indexOf(delim));
				var date = tempArray[i].substring(tempArray[i].indexOf(delim) + 1 , tempArray[i].length);
				
				if(id != "") {
					try {
						updatedDate = document.getElementById("hiddenUpdated" + id).value;
					
						if(updatedDate > date){
							returnObjById("divMessage" + id).className = "updated";
						}
						else {
							returnObjById("divMessage" + id).className = "hidden";
						}
					}
					catch(e) { 
						//I.E if page holds no message.
					}
				}
			}
		}
	}
}

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 deleteCookie(name) {
	createCookie(name,"",-1);
}

function idDictionary() 
{
    this.Delete = Delete
    this.Add = Add
}
function Delete(strKeyName)
{
    this[strKeyName] = null
}
function Add(strKeyName,value)
{
    this[strKeyName] = value;
}

function GetIdDateDic(str, delim)
{
    var tempArray;
    if (str.indexOf("&") != -1)
    {
        tempArray = str.substring(0, str.indexOf("&")).split(",");
    }
    else
    {
        tempArray = str.split(",");
    }
    
    var idDateDic = new idDictionary;
    for(i = 0; i < tempArray.length; i++)
    {
        var id = tempArray[i].substring(0, tempArray[i].indexOf(delim));
        var date = tempArray[i].substring(tempArray[i].indexOf(delim) + 1 , tempArray[i].length);
        idDateDic.Add(id, date);
    }
    return idDateDic;
}

function CloseMessageAlarm(id, idExpiryDate)
{
    var delim = "|";
    var currentCookie = readCookie("RPSProfile");
    if (currentCookie)
    {
        var messageIDs = currentCookie.indexOf("AlarmPageIds=");
        var cookieLength = currentCookie.length;
        
        if (messageIDs != -1)
        {
            // The cookie holds one or more message ids, so the new id must be concatinated to the 
            // existing list of id and date pairs unless it already exists in the cookie.
            
            // id to expiry date dictionary
            var cookieIDs = currentCookie.substring((messageIDs + "AlarmPageIds=".length), cookieLength)
            var idDateDic = GetIdDateDic(cookieIDs, delim);

            // Check if the id has been already entered
            if (!idDateDic[id])
            {
                var x = currentCookie.substring(messageIDs, cookieLength);
                var y = x.indexOf("&");
                
                // check if the AlarmPageIds value is in the begining of the cookie
                if (messageIDs == 0)
                {
                    deleteCookie("RPSProfile");

                    if (y == -1)
                    { 
                        createCookie("RPSProfile", currentCookie + id + delim + idExpiryDate + ",", 30);
                    }
                    else
                    {
                        createCookie("RPSProfile", currentCookie.substring(messageIDs, y) + id + delim + idExpiryDate + "," + currentCookie.substring(y, cookieLength), 30);
                    }
                }
                // check if the '&' character comes after the AlarmPageIds declaration
                // if so then it means that the AlarmPageIds value is in the middle
                else if ((messageIDs != 0) && (y != -1))
                {
                   deleteCookie("RPSProfile");
                   createCookie("RPSProfile", currentCookie.substring(0, messageIDs + "AlarmPageIds=".length) + id + delim + idExpiryDate + "," + currentCookie.substring(testIds + "AlarmPageIds=".length, cookieLength), 30);
                }
                // in the end
                else
                {
                    deleteCookie("RPSProfile");
                    createCookie("RPSProfile", currentCookie + id + delim + idExpiryDate + ",", 30);
                }
            }
        }
        else
        {
            // No ids have been stored in the cookie
            deleteCookie("RPSProfile");
	        createCookie("RPSProfile", currentCookie + "&AlarmPageIds=" + id + delim + idExpiryDate + ",", 30);
        }
    }
    else
    {
        // The cookie doesn't exist on the client, create a new one.
        createCookie("RPSProfile", "AlarmPageIds=" + id + delim + idExpiryDate + ",", 30);
    }
    
    returnObjById("divMessage" + id).className = "hidden";
}
// MESSAGE ALARM COOKIE FUNCTIONS END