function CheckCountry()
{
	ddlElem = document.getElementById('ddCountries');

	if(ddlElem.value == '0')
	{
		alert('You must select a country!');
	}
	else if(ddlElem.value == 'OC')
	{
		window.location = "/?pageid=482";
	}
	else
	{
		var expires = new Date();
		var today = new Date();
		expires.setTime(today.getTime() + 3600000*24*1000);

		setCookie('Investorstatus', 'PI', expires, "/");
		setCookie('FundCountry', ddlElem.value, expires, "/" );

		
		if(QueryString('change') == 'false'){
			window.location = "/?pageid=503&country=" + ddlElem.value;
		}else{
			window.location = "/?pageid=503&country=" + ddlElem.value + "&change=true";
		}
	}
}

function CheckFirst()
{
	var investorstatus = getCookie('Investorstatus');
	var fundcountry = getCookie('FundCountry');

	if(investorstatus == null && fundcountry == null)
	{
		var expires = new Date();
		var today = new Date();
		expires.setTime(today.getTime() + 3600000*24*1000);

		setCookie('First', 'true', expires, "/");
	}
}

function HandleEnter (e, enterFunction){
     if (!e) e = window.event;
     if (e.keyCode == 13){
          e.cancelBubble = true;
          if (e.returnValue) e.returnValue = false;
          if (e.stopPropagation) e.stopPropagation();
          if (enterFunction) eval(enterFunction);
          return false;
     } else {
          return true;
     }     
}


function Confirm(type)
{
	if(type)
	{
		var invStatus = getCookie('Investorstatus');
		var country = QueryString('country');

		showWaitBox(1100);
		

		if(invStatus == 'II'){
			window.location = "/?pageid=484";
		}else{
			if(country == 'false'){
				window.location = "/?pageid=62";
			}else{
				var expires = new Date();
				var today = new Date();
				expires.setTime(today.getTime() + 3600000*24*1000);

				setCookie('FundCountry', country, expires, "/");
				if(country == 'UK')
				{
					window.location = "/?pageid=509";
				}
				else
 				{
					window.location = "/?pageid=484";
				}
			}
		}
	}
	else
	{
		window.location = "/?pageid=482";
	}

	deletecookie('First', 'false', -1);
}

function deletecookie(name,value,duration){
	cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
	document.cookie=cookiestring;
}

function getexpirydate(nodays){
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}

function displayItem(key){

	if(QueryString(key)=='false')
	{
		alert("you didn't enter a ?name=value querystring item.");
	}else{
		alert(QueryString(key));
	}
}

function SetInvestor(type)
{
	var changes = '';
	var countrydiv = document.getElementById('selectcountry');
	
	if(QueryString('change') != 'false'){
		changes = '&change=true';	
	}

	if(CheckCookies())
	{	
		if(type == 'II'){
			var expires = new Date();
			var today = new Date();
			expires.setTime(today.getTime() + 3600000*24*1000);

			setCookie('Investorstatus', type, expires, "/");
			if(changes != ''){window.location = "/?pageid=504&change=true";}
			else {window.location = "/?pageid=504";}
		}else{
			countrydiv.style.display = 'block';
		}
	}else{
		alert('Your browser does not support Cookies. We will now redirect you to an information page where you find all informations about enabling cookies. When you have enabled cookies please come back and try again.');
		window.location = "/?pageid=507";
	}
}

function ShowCountrylink(){
	var elem = document.getElementById('countrylink');
	var link = '';

	var country = getCookie('FundCountry');
	var investor = getCookie('Investorstatus');

	if(investor == 'PI')
	{
		if(country == 'Denmark'){
			link = "<a href='http://www.temakapital.dk/dansk' target='_blank'>Further information</a>";
		}else if(country == 'Faroe'){
			link = "<a href='http://www.kaupthing.fo/Default.aspx?pageid=973' target='_blank'>Further information</a>";
		}else if(country == 'Finland'){
			link = "<a href='http://www.kaupthing.fi/?PageID=2084' target='_blank'>Further information</a>";
		}else if(country == 'Iceland'){
			link = "<a href='http://www.kbsjodir.is' target='_blank'>Further information</a>";
		}else if(country == 'Luxemborg'){
			link = "<a href='http://www.kaupthing.net/?PageID=3108' target='_blank'>Further information</a>";
		}else if(country == 'Norway'){
			link = "<a href='http://www.kaupthing.no/?PageID=1065' target='_blank'>Further information</a>";
		}else if(country == 'Sweden'){
			link = "<a href='http://www.kaupthing.se/jsp?page=/jsp/publik/fond/fonder.jsp' target='_blank'>Further information</a>";
		}else if(country == 'UK'){
			link = "<a href='http://www.singers.co.uk/individual/individualinvestors.htm' target='_blank'>Further information</a>";
		}
	
	
		elem.innerHTML = link;
	}
}

function InitInput()
{
	var day = QueryString('day');
	var month = QueryString('month');
	var year = QueryString('year');

	var orderby = QueryString('order');
	
	var tbElem = document.getElementById('tbDate');
	var ddlElem = document.getElementById('ddlorderby');

	if(day != 'false' && month != 'false' && year != 'false')
	{
		tbElem.value = month + '/' + day + '/' + year;
	}else{
		var dt = new Date();
		var theday = dt.getDate();
		var themonth = dt.getMonth() + 1;
		var theyear = dt.getFullYear();
		tbElem.value = themonth + '/' + theday + '/' + theyear;
	}

	if(orderby != 'false')
	{
		if(orderby == 'type'){ ddlElem.selectedIndex = 0; }
		else if(orderby == 'name'){ ddlElem.selectedIndex = 1; }
		else if(orderby == 'risk'){ ddlElem.selectedIndex = 2; }
	}else{
		ddlElem.selectedIndex = 0; 
	}		
}

//////////////////////////////////////
//  Sækir sjóði eftir dagsetningu ////
//////////////////////////////////////

function GetFundsByDate()
{
	var tbElem = document.getElementById('tbDate');
	var ddlElem = document.getElementById('ddlorderby');

	if (isDate(tbElem.value) == false){
		return false;
	}else{
		var dtsplit = tbElem.value.split("/");
		var location = "/?month=" + dtsplit[0] + "&day=" + dtsplit[1] + "&year=" + dtsplit[2];
		
		
		if(ddlElem.selectedIndex == 0){
			location += "&pageid=484";
		}else{
			location += "&pageid=2854&order=" + ddlElem.options[ddlElem.selectedIndex].value;
		}

		window.location = location;
		return true;	
	}
}


//////////////////////////////////////
//  Dagsetningar check ///////////////
//////////////////////////////////////

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}

//////////////////////////////////////
//  Sækir gildi úr querystreng ///////
//////////////////////////////////////

function QueryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function PageQuery(q) {
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else this.q = null;
	
	this.keyValuePairs = new Array();
	
	if(q){
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() { return this.keyValuePairs; }

	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) {
	
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
			}
		return false;
	}
	
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() { return this.keyValuePairs.length; }
}

function CheckCookies()
{
	setCookie('cookiecheck', 'cookiecheck');
	
	var isEnabled = getCookie('cookiecheck');

	if(isEnabled != null){return true;}
	else{return false;}
}

function ShowHideDownload(area, row)
{
	var elem = document.getElementById('fund' + area);
	var tableElem = document.getElementById('fundrow' + area);

	if(elem != null){
		if(elem.style.display == 'block'){
			elem.style.display = 'none';
			
			if(row == '1'){
				tableElem.style.background = '#F4F4F4';
			}else{
				tableElem.style.background = '#E3E3E3';
			}
		}else{
			elem.style.display = 'block';
			tableElem.style.background = '#D4E7F7';
		}
	}
}

function ShowPrintInfo()
{
	var elem = document.getElementById('printinfo');

	var printcookie = getCookie('printinfo');

	if(printcookie == null)
	{
		var expires = new Date();
		var today = new Date();
		expires.setTime(today.getTime() + 3600000*24*1000);

		setCookie('printinfo', 'true', expires, "/");
		if(elem.style.display == 'none' || elem.style.display == ''){
			elem.style.display = 'block';
		}
	}else{
		if(elem.style.display == 'block'){
			elem.style.display = 'none';
		}
		window.print();
	}
}

function GE(e)
	{
	       var d = document;
	       return o = (!e)?false:(d.getElementById)?d.getElementById(e):(d.all)?d.all(e):(d.layers)?d.layers[e]:false;
	}
		
function doCurrency( )
{
	var ddlCurrencies=  GE("currencyid");
	var pageid = GE("pageid").value;

	var url = "/pages/" + pageid
	url += "/" + ddlCurrencies.name+"/"+ddlCurrencies[ddlCurrencies.selectedIndex].value;
		
	location.href = url;
}

function doFundSubmitSE()
{
	var pageid = GE("pageid").value;
	var filter = GE("filter").value;

	var url = "/pages/1024/currencyid/1107/filter/" + filter;

	var from = document.getElementById('f_date_c').value;
	var to = document.getElementById('t_date_c').value;

	var valid = 0;

	var date1, date2;
   	var month1, month2;
	var year1, year2;
	
	year1 = from.substring (0, from.indexOf ("-"));
	month1 = from.substring (from.indexOf ("-")+1, from.lastIndexOf ("-"));
	date1 = from.substring (from.lastIndexOf ("-")+1, from.length);
		
	year2 = to.substring (0, to.indexOf ("-"));
	month2 = to.substring (to.indexOf ("-")+1, to.lastIndexOf ("-"));
	date2 = to.substring (to.lastIndexOf ("-")+1, to.length);

	if (year1 > year2) valid = 1;
	else if (year1 < year2) valid = -1;
	else if (month1 > month2) valid = 1;
	else if (month1 < month2) valid = -1;
	else if (date1 > date2) valid = 1;
	else if (date1 < date2) valid = -1;
	else valid = 0;

	if(valid == 0 || valid == -1) //Dates are the same or First date earlier
	{
		var dateTimeFrom = year1+"-"+month1+"-"+date1;
		var dateTimeTo = year2+"-"+month2+"-"+date2;
		url += "/timabilfra/" + dateTimeFrom + "/timabiltil/" + dateTimeTo ;
		
		//url += "/timabilfra/" + from + "/timabiltil/" + to ;

		
		location.href = url;
	}
	else if(valid == 1){ //First date is a later date
		alert("From date can´t be later then the To date");
	}
	else
	{
		alert("You have to enter valid dates!");
	}
}

function doFundSubmit()
{
	var pageid = GE("pageid").value;
	var filter = GE("filter").value;

	var url = "/pages/485/filter/" + filter;

	var from = document.getElementById('f_date_c').value;
	var to = document.getElementById('t_date_c').value;

	var valid = 0;

	var date1, date2;
   	var month1, month2;
	var year1, year2;
	
	date1 = from.substring (0, from.indexOf ("."));
	month1 = from.substring (from.indexOf (".")+1, from.lastIndexOf ("."));
	year1 = from.substring (from.lastIndexOf (".")+1, from.length);
		
	date2 = to.substring (0, to.indexOf ("."));
	month2 = to.substring (to.indexOf (".")+1, to.lastIndexOf ("."));
	year2 = to.substring (to.lastIndexOf (".")+1, to.length);

	if (year1 > year2) valid = 1;
	else if (year1 < year2) valid = -1;
	else if (month1 > month2) valid = 1;
	else if (month1 < month2) valid = -1;
	else if (date1 > date2) valid = 1;
	else if (date1 < date2) valid = -1;
	else valid = 0;

	
	if(valid == 0 || valid == -1) //Dates are the same or First date earlier
	{
		var dateTimeFrom = year1+"-"+month1+"-"+date1;
		var dateTimeTo = year2+"-"+month2+"-"+date2;
		url += "/timabilfra/" + dateTimeFrom + "/timabiltil/" + dateTimeTo ;
		
		//url += "/timabilfra/" + from + "/timabiltil/" + to ;

		
		location.href = url;
	}
	else if(valid == 1){ //First date is a later date
		alert("From date can´t be later then the To date");
	}
	else
	{
		alert("You have to enter valid dates!");
	}
}

function doFundSubmitUK()
{
	var pageid = GE("pageid").value;
	var filter = GE("filter").value;

	var url = "/pages/2519/filter/" + filter;

	var from = document.getElementById('f_date_c').value;
	var to = document.getElementById('t_date_c').value;

	var valid = 0;

	var date1, date2;
   	var month1, month2;
	var year1, year2;
	
	date1 = from.substring (0, from.indexOf ("."));
	month1 = from.substring (from.indexOf (".")+1, from.lastIndexOf ("."));
	year1 = from.substring (from.lastIndexOf (".")+1, from.length);
		
	date2 = to.substring (0, to.indexOf ("."));
	month2 = to.substring (to.indexOf (".")+1, to.lastIndexOf ("."));
	year2 = to.substring (to.lastIndexOf (".")+1, to.length);

	if (year1 > year2) valid = 1;
	else if (year1 < year2) valid = -1;
	else if (month1 > month2) valid = 1;
	else if (month1 < month2) valid = -1;
	else if (date1 > date2) valid = 1;
	else if (date1 < date2) valid = -1;
	else valid = 0;

	
	if(valid == 0 || valid == -1) //Dates are the same or First date earlier
	{
		var dateTimeFrom = year1+"-"+month1+"-"+date1;
		var dateTimeTo = year2+"-"+month2+"-"+date2;
		url += "/timabilfra/" + dateTimeFrom + "/timabiltil/" + dateTimeTo ;
		
		//url += "/timabilfra/" + from + "/timabiltil/" + to ;

		
		location.href = url;
	}
	else if(valid == 1){ //First date is a later date
		alert("From date can´t be later then the To date");
	}
	else
	{
		alert("You have to enter valid dates!");
	}
}


function doFundsearchEN()
{
	var searchstring = document.getElementById('fundsearchstr').value;

	if(searchstring.length == 0)
	{
		alert('You must enter a searchstring');
	}
	else
	{
		document.location = '/?pageid=3098&searchstring=' + searchstring;
	}

}

function CheckSelectedDates(tofield, fromfield)
{
	var timabilfra = QueryString('timabilfra');
	var timabiltil = QueryString('timabiltil');

	if(timabilfra != 'false' && timabiltil != 'false')
	{
		document.getElementById(tofield).value = timabiltil;
		document.getElementById(fromfield).value = timabilfra;
	}
}

function switchFund()
{
	var dd = document.getElementById('switchfund');
	
	if(dd)
	{
		if(dd.selectedIndex > 0)
		{
			showWaitBox();
			window.location = "/?PageID=2519&filter=" + dd.options[dd.selectedIndex].value;
		}
	}
}
function switchFundRight()
{
	var dd = document.getElementById('switchfundRight');
	
	if(dd)
	{
		if(dd.selectedIndex > 0)
		{
			showWaitBox();
			window.location = "/?PageID=2519&filter=" + dd.options[dd.selectedIndex].value;
		}
	}
}

function showWaitBox(top)
{
	var waitBox = document.getElementById('waitBox');
	
	if (document.documentElement && document.documentElement.scrollTop)
		theTop = document.documentElement.scrollTop;
	else if (document.body)
		theTop = document.body.scrollTop;
	else
		theTop = 0;

	if(waitBox)
	{
		if(top > 0)
			waitBox.style.top = top + "px";
		else
			waitBox.style.top = (300 + theTop) + "px";

		waitBox.style.left = "410px";
	}
}
function hideWaitBox()
{
	var waitBox = document.getElementById('waitBox');
	if(waitBox)
	{
		waitBox.style.left = "-410px";
	}
}
checkReload={     
	checker: function(x){           
		if (x && !this.ignorer && new Date().getTime()-x>1000){this.reloader()};           
		this.ignorer=false;
		setTimeout("checkReload.checker("+new Date().getTime()+")",800)
	},
	init: function(x){
		this.reloader=x;
		this.checker()
	},
	ignore: function(){
		this.ignorer=true
         } 
}; 

function __preload()
{
	var waitBox = document.createElement("div");
	waitBox.setAttribute("id", "waitBox");
	var body = document.getElementsByTagName("body").item(0);
	body.appendChild(waitBox);
	checkReload.ignore();
}

function show(id)
{
	el = document.getElementById(id);
	if (el.style.display == 'none')
	{
		el.style.display = '';
		el = document.getElementById('more' + id);
		el.innerHTML = 'Close information';
	} else {
		el.style.display = 'none';
		el = document.getElementById('more' + id);
		el.innerHTML = 'Print information';
	}
}
function doSubmit( )
{
	var url="Default.aspx";

	var ddlSjodir =  document.getElementById("fundscompid");
	var ddlTimabil =  document.getElementById("timabilid");
	var ddlGjaldmidlar =  document.getElementById("currencyid");

	var pageid = document.getElementById("pageid").value;
	var filter = document.getElementById("filter").value;

//	var flokkurid = document.getElementById("flokkurid").value;

	url += "?pageid="+pageid;
	url += "&amp;filter="+filter;
	url += "&amp;"+ddlTimabil.name+"="+ddlTimabil[ddlTimabil.selectedIndex].value
	url += "&amp;"+ddlGjaldmidlar.name+"="+ddlGjaldmidlar[ddlGjaldmidlar.selectedIndex].value

	if( ddlSjodir[ddlSjodir.selectedIndex].value!="#" )
		url += "&amp;"+ddlSjodir.name+"="+ddlSjodir[ddlSjodir.selectedIndex].value

	url += "&amp;nc=1";
	location.href = url;
 

	//alert( "do submit to '" + url +"'");
}
addEvent(window, "load", __preload);
//_onload += "__preload();";

checkReload.init(hideWaitBox);