

/* *******************************************************************************************
 *         Browser sniffer                                                                   *
 ******************************************************************************************* */

var platf     = navigator.platform;
var ua        = navigator.userAgent;

var hasDocumentLayers = (document.layers) ? 1 : 0;
var hasDocumentAll    = (document.all) ? 1 : 0;
var hasGetElementById = (document.getElementById) ? 1 : 0;

var isGecko   = inStr(ua,"Gecko");
var isOpera   = inStr(ua,"Opera");
var isSafari  = inStr(ua,"Safari");
var isWindows = inStr(platf,"Win32");
var isMac     = inStr(platf,"Mac");

var isIE55    = (isWindows && hasDocumentAll && hasGetElementById && (inStr(ua,"MSIE 5.5") || inStr(ua,"MSIE 6.0") || inStr(ua,"MSIE 6.5") || inStr(ua,"MSIE 7.0")) && !isOpera) ? 1 : 0;

function inStr(str,val,cas) {
	var ret;
	if (cas) { /* Case sensitive */
		ret = (str.indexOf(val) != -1) ? true : false;
	} else { /* Not Case sensitive */
		str = str.toUpperCase();
		val = val.toUpperCase();
		ret = (str.indexOf(val) != -1) ? true : false;
	}
	return ret;
}

/* *******************************************************************************************
 *         Functions                                                                         *
 ******************************************************************************************* */

function focusField(theFormName, theElementName) {
	var f = (!isNaN(theFormName)) ? eval("document.forms[" + theFormName + "]") : eval("document.forms." + theFormName);
	if (f) {
		var el = eval("f." + theElementName);
		if (el) el.focus();
	}
}

function getParam(attrib) {			// get querystring-parameters from document.location
	var sParams = location.search;
	var retVal = "";
	var params = new Array();
	if (sParams.indexOf('?') != -1) {
		var pairs = sParams.substring(1, sParams.length).split('&');
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split('=');
			if (nameVal[0] == attrib) {
				retVal = nameVal[1];
			}
		}
	}
	return retVal;
}

function showHideAdminPanel(show) {
	if (show) {
		setCookie("adminPanelShow", "true") ;
		document.getElementById('adminlinks').className = "adminlinks_out" ;
		document.getElementById('close').title = "Dölj" ;
	} else {
		setCookie("adminPanelShow", "false") ;
		document.getElementById('adminlinks').className = "adminlinks_in" ;
		document.getElementById('close').title = "Visa" ;
	}
}

function openExperience(obj) {
	var pageId = obj.options[obj.selectedIndex].value ;
	if (/^\d+$/.test(pageId)) document.location = "/?p=" + pageId ;
}


/* *******************************************************************************************
 *         Set Cookie                                                                        *
 ******************************************************************************************* */

function setCookie(name, value) {
	var sPath = '/';
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 1000*60*60*24*365); // 365 days
	var sCookieCont = name + "=" + escape(value);
	sCookieCont += (expire == null) ? "" : "\; expires=" + expire.toGMTString();
	sCookieCont += "; path=" + sPath;
	document.cookie = sCookieCont;
}


/* *******************************************************************************************
 *         Get Cookie                                                                        *
 ******************************************************************************************* */

function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) {
		var offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, end));
		}
	}
}


/* *******************************************************************************************
 *         POPUP functions                                                                   *
 ******************************************************************************************* */

function popWinOpen(winW,winH,sUrl,sName,iResize,iScroll) {
	if (screen) {
		if ((screen.height - winH) < 150) {
			var winX = (screen.width - winW) / 2;
			var winY = 0;
		} else {
			var winX = (screen.width - winW) / 2;
			var winY = (screen.height - winH) / 2;
		}
		var popWindow = window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,status=1,scrollbars=" + iScroll + ",width=" + winW + ",height=" + winH + ",top=" + winY + ",left=" + winX + "");
		if (popWindow) popWindow.focus();
	} else {
		window.open(sUrl,sName,"resizable=" + iResize + ",menubar=0,status=1,scrollbars=" + iScroll + ",width=" + winW + ",height=" + winH);
	}
}


function openAdminText(page, nbr, label, lang) {
	popWinOpen(800,550,"/admin/change_text.asp?p=" + page + "&nbr=" + nbr + "&label=" + escape(label) + "&lang=" + lang,"textAdmin",1,0) ;
}
function openAdminImage(page, nbr, label) {
	popWinOpen(800,550,"/admin/change_image.asp?p=" + page + "&nbr=" + nbr + "&label=" + escape(label),"imageAdmin",1,0) ;
}

function closeReloadParent() {
	try {
		parent.opener.location.reload() ;
		parent.opener.focus() ;
	} catch(e) {}
	window.close() ;
}
















/*Date.prototype.getWeek = function() {
	var firstDay = new Date(this.getFullYear(),0,1) ;
	return Math.ceil((((this - firstDay) / 86400000) + firstDay.getDay())/7) ;
}*/
Date.prototype.getWeek = function() {
	var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
	var DoW = d.getDay();
	d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
	var ms = d.valueOf(); // GMT
	d.setMonth(0);
	d.setDate(4); // Thu in Week 1
	return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
}

function setWeek(year,nbr) {
	var val = document.getElementById("date_" + year + "_" + nbr).value ;
	if (/^\d{4}-\d{2}-\d{2}$/.test(val)) {
		var arrData = val.split("-") ;
		var dDate = new Date() ;
		dDate.setFullYear(arrData[0]) ;
		dDate.setMonth(parseInt(arrData[1].replace(/^0/, "")) - 1) ;
		dDate.setDate(parseInt(arrData[2].replace(/^0/, ""))) ;
		var weekNbr = dDate.getWeek() ;
		//alert(val + " - " + dDate + " - " + weekNbr) ;
		var sWeekNbr = (weekNbr < 10) ? "0" + weekNbr : "" + weekNbr ;
		document.getElementById("week_" + year + "_" + nbr).value = arrData[0] + "" + sWeekNbr ;
	}
	checkDates(year,nbr) ;
}

function setMonday(year,nbr) {
	var week  = document.getElementById("week_" + year + "_" + nbr).value ;
	var oDate = document.getElementById("date_" + year + "_" + nbr) ;
	if (/^\d{6}$/.test(week)) {
		var mondayInWeek = getDatesByWeek(week)[0] ;
		oDate.value = mondayInWeek ;
	}
}

function checkDates(year,nbr) {
	var oWeek0 = document.getElementById("week_" + year + "_0") ;
	var oWeek1 = document.getElementById("week_" + year + "_1") ;
	var oWeek  = (!nbr) ? null : document.getElementById("week_" + year + "_" + nbr) ;
	var week0  = oWeek0.value ;
	var week1  = oWeek1.value ;
	if (/^\d{6}$/.test(week0) && /^\d{6}$/.test(week1)) {
		var iWeek0 = parseInt(week0) ;
		var iWeek1 = parseInt(week1) ;
		if (iWeek0 > iWeek1) {
			alert("Första veckan måste ligga före den andra veckan!") ;
			if (oWeek) oWeek.value = "" ;
			return false ;
		} else if (week0.substring(0,4) != week1.substring(0,4)) {
			alert("Båda veckorna måste ligga på samma år!") ;
			if (oWeek) oWeek.value = "" ;
			return false ;
		}
	}
	setMonday(year,nbr) ;
}

function getDatesByWeek(yyyyww) {
	var year = parseInt(yyyyww.substring(0,4),10) ;
	var week = parseInt(yyyyww.substring(4,6),10) ;
	var jan10 = new Date(year,0,10,12,0,0) ;
	var jan4 = new Date(year,0,4,12,0,0) ;
	var wk1mon = new Date(jan4.getTime()-(jan10.getDay())*86400000) ;
	var dateMon = new Date(wk1mon.getTime()+(week-1)*604800000) ;
	var dateSun = new Date(dateMon.getTime()+518400000) ;
	var arrDates = new Array() ;
	arrDates[0] = dateFormat(dateMon) ;
	arrDates[1] = dateFormat(dateSun) ;
	return arrDates ;
}

function dateFormat(dDate) {
	return "" +
		dDate.getFullYear() + "-" +
		(dDate.getMonth() + 101).toString().substring(1,3) + "-" +
		(dDate.getDate() + 100).toString().substring(1,3) ;
}

