function fixUglyIE() {
	for ( a in document.links )
		document.links[a].onfocus = document.links[a].blur;
}

if ( document.links )
	document.onmousedown = fixUglyIE;

var mac = navigator.appVersion.indexOf("Macintosh") > -1;

function checkCustomer() {
	var aPCid = getParameter("cId");
	var aPMsisdn = getParameter("msisdn");
	var aCCid = readCookie("cId");
	var aCMsisdn = readCookie("msisdn");
	if (aPCid != "") {
    document.fPrenum.cId.value = aPCid;
    writeCookie("cId", aPCid);
	} else {
    document.fPrenum.cId.value = aCCid;
	}
	if (aPMsisdn != "") {
    document.fPrenum.msisdn.value = aPMsisdn;
    writeCookie("msisdn", aPMsisdn);
	} else {
    document.fPrenum.msisdn.value = aCMsisdn;
	}
}

function validatePrenumFields(){
	var aErrors = '';
	if (document.fPrenum.msisdn.value == '')
		aErrors += "* Du måste fylla i ditt mobiltelefonnummer!\n";
	if (document.fPrenum.listId[0].checked == false && document.fPrenum.listId[1].checked == false && document.fPrenum.listId[2].checked == false && document.fPrenum.listId[3].checked == false)
		aErrors += "* Du måste välja ett område!";
	if (aErrors != '')
		alert("Du har inte fyllt i formuläret rätt!\n\n" +aErrors);
	else {
		document.fPrenum.submit();
	}
}

function validateRemoveFields() {
	var aErrors = "";
	
	if (document.fAvsl.unsubscribe[1].checked == true) {
		if (document.fPrenum.msisdn.value == '') 
			aErrors += "* Du måste fylla i ditt mobiltelefonnummer!";
		if ( aErrors != "" ) {
			alert("Du har inte fyllt i formuläret rätt!\n\n" + aErrors);
			return;
		} else {
			document.fRemove.msisdn.value = document.fPrenum.msisdn.value;
			if ( !confirm("Är du säker på att du vill avsluta din/dina prenumerationer?") )	return;
			else document.fRemove.submit();
		}
	}
	else if (document.fAvsl.unsubscribe[0].checked == true) {
		if (document.fPrenum.msisdn.value == '') 
			aErrors += "* Du måste fylla i ditt mobiltelefonnummer!\n";
		if (document.fPrenum.listId[0].checked == false && document.fPrenum.listId[1].checked == false && document.fPrenum.listId[2].checked == false && document.fPrenum.listId[3].checked == false)
	    aErrors += "* Du måste välja minst ett område och energiform!";
	  if ( aErrors != "" ) {
			alert("Du har inte fyllt i formuläret rätt!\n\n" + aErrors);
			return;
		} else {
			document.fAvsl.msisdn.value = document.fPrenum.msisdn.value;

			if (document.fPrenum.listId[0].checked) { 
			  document.fAvsl.listId[0].value = document.fPrenum.listId[0].value;
			  document.fAvsl.listId[0].disabled = false;
			} else { 
			  document.fAvsl.listId[0].disabled = true;
			}
			if (document.fPrenum.listId[1].checked) {
			  document.fAvsl.listId[1].value = document.fPrenum.listId[1].value; 
			  document.fAvsl.listId[1].disabled = false;
			} else {
			  document.fAvsl.listId[1].disabled = true;
			}
			if (document.fPrenum.listId[2].checked) {
			  document.fAvsl.listId[2].value = document.fPrenum.listId[2].value;
			  document.fAvsl.listId[2].disabled = false;
			} else {
			  document.fAvsl.listId[2].disabled = true;
			}
			if (document.fPrenum.listId[3].checked) {
			  document.fAvsl.listId[3].value = document.fPrenum.listId[3].value;
			  document.fAvsl.listId[3].disabled = false;
			} else {
			  document.fAvsl.listId[3].disabled = true;
			}
			if ( !confirm("Är du säker på att du vill avsluta din/dina prenumerationer?") )	return;
			else document.fAvsl.submit();
		}
	}
}

function saveForm() {
	writeCookie("msisdn", fPrenum.msisdn.value);
	writeCookie("cId", fPrenum.cId.value);
}

function modifyFormValueMsisdn(theValue) {
	document.fPrenum.msisdn.value = theValue;
}

function modifyFormArea(theArea) {
	var text = getAllAreaInfo(theArea);
	if(mac) text = removeDotAndRing(text);
	with (document.fAvsl) {
		listId[0].value = "";
		listId[1].value = "";
		listId[2].value = "";
		listId[3].value = "";
	}
	with (document.fPrenum) {
		area.value = text;
		listId[0].disabled = true;
		listId[1].disabled = true;
		listId[2].disabled = true;
		listId[3].disabled = true;
		listId[0].checked = false;
		listId[1].checked = false;
		listId[2].checked = false;
		listId[3].checked = false;
		listId[0].value = "";
		listId[1].value = "";
		listId[2].value = "";
		listId[3].value = "";
		var temp1 = theArea + 100;

		if (getAreaListId(theArea) != "") {
			listId[0].disabled = false;
			listId[0].checked = true;
			listId[0].value = getAreaListId(theArea);
		}
		if (getAreaListId(theArea + 100) != "") {
			listId[1].disabled = false;
			listId[1].checked = true;
			listId[1].value = getAreaListId(theArea + 100);
		}
		if (getAreaListId(theArea + 200) != "") {
			listId[2].disabled = false;
			listId[2].checked = true;
			listId[2].value = getAreaListId(theArea + 200);
		}
		if (getAreaListId(theArea + 300) != "") {
			listId[3].disabled = false;
			listId[3].checked = true;
			listId[3].value = getAreaListId(theArea + 300);
		}
		if (getAreaListId(elecAreas[theArea]) != "") {
			listId[0].disabled = false;
			listId[0].checked = true;
			listId[0].value = getAreaListId(elecAreas[theArea]);
		}
		if (getAreaListId(elecAreas[temp1]) != "") {
			listId[1].disabled = false;
			listId[1].checked = true;
			listId[1].value = getAreaListId(elecAreas[temp1]);
		}
		temp1 += 100;
		if (getAreaListId(elecAreas[temp1]) != "") {
			listId[2].disabled = false;
			listId[2].checked = true;
			listId[2].value = getAreaListId(elecAreas[temp1]);
		}
	}
}

function modifyFormRemove() {
}

function modifyNavArea(theArea) {
	with ( document.fPrenum ) {
		area.value = getAreaInfo(theArea);
	}
}

var popupBorderSize = mac? 0 : 1;
var popupBorderColor = "black";

var popupTitleColor = "#AAAAAA";
var popupTitleFont = "arial,helvetica,geneva,verdana";
var popupTitleFontColor = "white";
var popupTitleFontSize = mac? "12px" : "10pt";

var popupTextColor = "white";
var popupTextFont = "arial,helvetica,geneva,verdana";
var popupTextFontColor = "black";
var popupTextFontSize = mac? "10px" : "8pt";

var warningTextColor = "#FF8473";
var warningTextFont = "arial,helvetica,geneva,verdana";
var warningTextFontColor = "black";
var warningTextFontSize = mac? "10px" : "8pt";
var warningTitleColor = "red";

function HTMLEncode(text) {
	text = text.replace(/å/g, "&aring;");
	text = text.replace(/Å/g, "&Aring;");
	text = text.replace(/ä/g, "&auml;");
	text = text.replace(/Ä/g, "&Auml;");
	text = text.replace(/ö/g, "&ouml;");
	text = text.replace(/Ö/g, "&Ouml;");	
	return text;	
}

function removeDotAndRing(text) {	
	text = text.replace(/å/g, "a");
	text = text.replace(/Å/g, "A");
	text = text.replace(/ä/g, "a");
	text = text.replace(/Ä/g, "A");
	text = text.replace(/ö/g, "o");
	text = text.replace(/Ö/g, "O");	
	return text;		
}

function modifyPopup(theArea) {
	if ( theArea == 0 ) {
		popupHeight = 60;
		hidePopup();
	} else {
		var popupTitle = "Mälarenergi";
		var popupText = getAllAreaInfo(theArea);
		var popupHTML = "<table border='0' " + (document.layers? "" : "width='" + popupWidth + "'") + " cellspacing='0'  cellpadding='" + (document.layers? popupBorderSize + "' bgcolor='" + popupBorderColor + "'" : "0' style='border: " + popupBorderSize + "px solid " + popupBorderColor + "; '") + "><tr><td>" +
						"<table width='100%' border='0' cellspacing='0' cellpadding='2'>" + 
						"<tr><th style='padding: 2px; text-align: center; font-weight: bold; background-color: " + popupTitleColor + "; font-family: " + popupTitleFont + "; font-size: " + popupTitleFontSize + "; color: " + popupTitleFontColor + ";'>" + popupTitle + "</th></tr>" + 
						"<tr><td style='padding: 2px; background-color: " + popupTextColor + "; font-family: " + popupTextFont + "; font-size: " + popupTextFontSize + "; color: " + popupTextFontColor + ";'>" + popupText + "</td></tr>";
		var popupLoopHTML = "";
		var hasMerged = false;
		var popupWarning = "";
			var aType = "";
			for (var i = 0; i < warnings.length; i++) {
				if (warnings[i].id > 99 && warnings[i].id < 200) aType = "Vatten";
				else if (warnings[i].id > 199 && warnings[i].id < 300) aType = "Fjärrvärme";
				else if (warnings[i].id < 100) aType = "El";
				else if (warnings[i].id > 299) aType = "Stadsnät";

			var theLoopArray = loopAreas[warnings[i].id];
			var loopWarningType = loopAreas[warnings[i].code];
			if (typeof theLoopArray != "undefined") {
				for (var j = 0; j < theLoopArray.length; j++ ) {
					if (theLoopArray[j] == theArea) {
						var popupWarning = "";

						popupWarning += warnings[i].place + "<br>";
						popupWarning += "<b>" + aType + "</b><br>"
						warningTextColor = getPopupColor(warnings[i].code)

						popupWarning += "<b>Status:</b>&nbsp;" + warnings[i].description + "<br>";
						if (warnings[i].finish != null && warnings[i].finish != "") {
							if (warnings[i].started.length != 0 && warnings[i].code == 8) popupWarning += "<b>Start:</b>&nbsp;" + warnings[i].started + "<br>";
							if (warnings[i].finish.length != 0 && warnings[i].code != 9) popupWarning += "<b>Klart:</b>&nbsp;" + warnings[i].finish + "<br>";
						}
						if (warnings[i].area != "" || warnings[i].area.length != 0) popupWarning += "<b>Berörda kunder: </b>" + warnings[i].area + "<br>"
						if (warnings[i].text != "" || warnings[i].text.length != 0) popupWarning += "<i>" + warnings[i].text + "</i><br>"
						popupLoopHTML += "<tr><td style='padding: 2px; background-color: " + warningTextColor + "; font-family: " + warningTextFont + "; font-size: " + warningTextFontSize + "; color: " + warningTextFontColor + "; border-top: 1px solid black;'>" + popupWarning + "</td></tr>";
						popupHeight += 70;
					}
				}
			}
			if (warnings[i].id == theArea && getZone(warnings[i].id) > 0) {
				var popupWarning = "";

				popupWarning += warnings[i].place + "<br>";
				popupWarning += "<b>" + aType + "</b><br>";
				warningTextColor = getPopupColor(warnings[i].code);

				if (hasMerged) popupWarning += getAreaInfo(warnings[i].id) + "<br>";
				popupWarning += "<b>Status:</b>&nbsp;" + warnings[i].description + "<br>";
				if (warnings[i].finish != null && warnings[i].finish != "") {
					if (warnings[i].started.length != 0 && warnings[i].code == 8) popupWarning += "<b>Start:</b>&nbsp;" + warnings[i].started + "<br>";
						if (warnings[i].finish.length != 0 && warnings[i].code != 9) popupWarning += "<b>Klart:</b>&nbsp;" + warnings[i].finish + "<br>";
				}
				if (warnings[i].area != "" || warnings[i].area.length != 0) popupWarning += "<b>Berörda kunder: </b>" + warnings[i].area + "<br>";
				if (warnings[i].text != "" || warnings[i].text.length != 0) popupWarning += "<i>" + warnings[i].text + "</i><br>";
				popupHTML += "<tr><td style='padding: 2px; background-color: " + warningTextColor + "; font-family: " + warningTextFont + "; font-size: " + warningTextFontSize + "; color: " + warningTextFontColor + "; border-top: 1px solid black;'>" + popupWarning + "</td></tr>";
				popupHeight += 70;
			}
		}

		popupHTML += popupLoopHTML + "</table></td></tr></table>";
		popupHTML = HTMLEncode(popupHTML);

		showPopup(popupHTML);
	}
}

function showResultMessage() {
	var aErrorLabel = getParameter("errorLabel");
	var aAddOk = getParameter("aok");
	var aRemoveOk = getParameter("rok");
	switch ( aErrorLabel ) {
	case "":
		if (readCookie("reload") == "") {
			if ( aAddOk == 1 ) {
				alert("Du prenumererar nu på avbrottsinformation till din mobiltelefon!");
			} else if ( aRemoveOk == 1 ) {
				alert("Du har avslutat din prenumeration!");
			}
		}
		writeCookie("reload", "");
		break;
	case "AuthFailure-TooManyLists":
		alert("Du får inte prenumerera på fler än 8 områden!");
		break;
	case "InvalidSubsriber":
	case "NoMSISDNSpecified":
	case "InvalidFormat-MSISDN":
	case "TooShort-MSISDN":
	case "TooLong-MSISDN":
		alert("Du har inte angett ett giltigt mobiltelefonnummer!");
		break;
	case "InvalidList":
		alert("Systemfel - området är inte definierat!");
		break;
	default:
		alert("Systemfel - " + aErrorLabel + "!");
		break;
	}
}

function refreshFrame() {
	writeCookie("reload", "1");
	document.location.reload(false);
}

function sortfunction(a, b){
  if(a.code < b.code)
   return 1
  else if(a.code > b.code)
   return -1;
  else
   return 0;
 
}

var w = 362;
var h = 489;
var rendered = false;
var aZone = getParameter("zone");

function writeNavMap() {

	if(rendered) return;
	var macFix = (mac && !document.layers ? " style='left: 0px; top: 0px;'" : "");
	var aImage = "malarenergi";
	var html;
	var someHtml = "";
	
	if ( document.all || document.layers ) {
		html = "<span class='relative'>";
	}
	 else
		html = "<div class='relative'>";
	if ( aZone != "" ) {
		aImage = "malarenergi_zone" + aZone;
		html += "<span class='absolute'><img src='maps/" + aImage + ".gif' width='" + w + "' height='" + h + "' alt='' border='1'></span>";

		
		warnings.sort(sortfunction);
		
		for ( var i = 0; i < warnings.length; i++ ) {
			someHtml = displayWarningAreas(warnings[i].id, getZone(warnings[i].id), warnings[i].code, macFix);
			if (typeof someHtml != "undefined")
				html += displayWarningAreas(warnings[i].id, getZone(warnings[i].id), warnings[i].code, macFix);
			
			if (getZone(warnings[i].id) < 0) {
				var theLoopArray = loopAreas[warnings[i].id];
				for (data in theLoopArray) {
					html += displayWarningZones(getZone(theLoopArray[data]), macFix, aImage, warnings[i].code, theLoopArray[data]);
				}
			}
			else {
				html += displayWarningZones(getZone(warnings[i].id), macFix, aImage, warnings[i].code);
			}
		}
		html += "<span class='absolute'" + macFix + "><img src='maps/" + aImage + "-names.gif' width='" + w + "' height='" + h + "' alt='' border='1' usemap='#" + aImage + "'></span>";

	} else {
		html += "<span class='absolute'><img src='maps/" + aImage + ".gif' width='" + w + "' height='" + h + "' alt='' border='1' usemap='#" + aImage + "'></span>";

		for ( var i = 0; i < warnings.length; i++ ) {
			if (getZone(warnings[i].id) < 0) {
				var theLoopArray = loopAreas[warnings[i].id];
				for (data in theLoopArray) {
					html += displayWarningZones(getZone(theLoopArray[data]), macFix, aImage, warnings[i].code, theLoopArray[data]);
				}
			}
			else {
				html += displayWarningZones(getZone(warnings[i].id), macFix, aImage, warnings[i].code);
			}
		}
	}
	html += document.all || document.layers ? "</span>" : "</div>";
	if(document.all) html += "<img src='images/spacer.gif' width='0' height='400'>";
	rendered = true;
//alert(html);
	document.write(html);
}

function reDir(){
	var found = "" + window.location + "";
	found = found.indexOf('zone=3');
	if (found != -1) location.replace('?zone=2');
	else location.replace('?overview');
}

