

function addEvent(type, method) {
	if(typeof window.addEventListener != 'undefined')
	{
		window.addEventListener(type, method, false);
	}
	//.. opera 7
	else if(typeof document.addEventListener != 'undefined')
	{
		document.addEventListener(type, method, false);
	}
	//.. win/ie
	else if(typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('on' + type, method);
	}
}


var oldValue = '';
var foundNothing = false;
var divId;
var currentInputId;
var currentText = '';
var isRunning = false;

function doSearch(inputId, currentDivId) {
	divId = currentDivId;
	currentInputId = inputId;
	currentText = jQuery.trim($('#' + inputId).val());

	if (currentText != oldValue) {
		if (!isRunning && currentText.length > 2) {
			if (!foundNothing) {
				isRunning = true;
				$.get("/cust/search.aspx", 
					{
						s: currentText
					},
					function(data) {
						if (data != '') {
							document.getElementById(divId).innerHTML = data;
							document.getElementById(divId).style.display = 'block';
							document.getElementById(divId).style.marginTop = '16px';
							document.getElementById(divId).zIndex = 80;
						} else {
							document.getElementById(divId).style.display = 'none';
						}

						oldValue = currentText;
						isRunning = false;
					}
				);

			}
		} else if (currentText.length < 3) {
			foundNothing = false;
			document.getElementById(divId).style.display = 'none';
			isRunning = false;
		}
	}
}

function closeSearch() {
	document.getElementById(divId).style.display = 'none';
}


function loadHowto(howtoid) {
	var url = '/howto/howto.aspx';
	var pars = 'ajaxJs=1&howtoid=' +  howtoid;

	$.get("/howto/howto.aspx", 
		{
			ajaxJs: 1,
			howtoid: howtoid
		},
		function(data) {
			var div = document.createElement('div');
			div.style.top = '250px';
			div.style.left = '150px';
			div.style.position = 'absolute';
			div.style.backgroundColor = '#ffffff';
			div.style.width = '550px';
			div.style.padding = '4px';
			div.style.border = '1px solid #000000';
			div.id = 'howtoDiv'
			div.zIndex = 100;

			var closeDiv = document.createElement('div');
			closeDiv.style.textAlign = 'right';
			closeDiv.innerHTML = '<a href="javascript:document.getElementById(\'howtoDiv\').style.display=\'none\';if (document.getElementById(\'val\')) { document.getElementById(\'val\').style.visibility=\'visible\';}void(0);"><img src="/images/close.gif" alt="" /></a>';
			div.appendChild(closeDiv);

			if (document.getElementById('val')) document.getElementById('val').style.visibility = 'hidden';
			div.innerHTML += data;
			document.getElementById('centercontent').appendChild(div);		
		}
	);
}


var lastWindow = null;
function createWindowAndClose(position, id) {
	var div;
	if (lastWindow != null) {
		div = lastWindow;
		div.innerHTML = '';
	} else {
		div = document.createElement('div');
	}
	div.style.position = position;
	div.style.backgroundColor = '#ffffff';
	div.style.width = '550px';
	div.style.padding = '4px';
	div.style.border = '1px solid #000000';
	div.style.maxHeight = '300px';
	div.style.overflow = 'auto';
	if (typeof(id) == 'undefined') {
		div.id = 'howtoDiv';
	} else {
		div.id = id;
	}
	div.style.display = 'block';

	var closeDiv = document.createElement('div');
	closeDiv.style.textAlign = 'right';
	closeDiv.innerHTML = '<a href="javascript:document.getElementById(\'' + div.id + '\').style.display=\'none\';void(0);"><img src="/images/close.gif" alt="" /></a>';
	div.appendChild(closeDiv);

	return div;
}

function prenta(sid) {window.print();}

		function senda(sid) {
			var someWin = window.open('/main/senda.aspx?sid=' + sid,'sw2','resizable=no,dependent=yes,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,width=300,height=300');
			someWin.focus();
		}


		function loadPayment(serviceId) {
			window.open('/pers/form1.aspx?serviceId=' + serviceId, 'payment', 'width=600,height=450,status=yes,scrollbars=yes,location=yes');
		}

		function loadTvProgram() {
			window.open('/adv.aspx?href=www.sjonvarp.is/barnaland/detail/mFrameDetailType.php3?Type=CH', 'tv', 'width=500,height=500');
		}

		function konnunCheck() {
			document.konnunform.takki.disabled = true;
			return true;
		}

		function RatePage(sid, nr) {
			$.get("/main/rate.aspx", 
				{
					sid: sid,
					r : nr
				},
				function(data) {
					document.getElementById('rateResult').innerHTML = 'Thank you for your help';
				}
			);
		}

		var alphaNr		= 0;
		var counter		= 0;
		var hideTable	= false;

		function lightMessageNotify() {
			if (getCookie('notify') != '1') {
				var mnt = document.getElementById('messageNotifyTable');
				mnt.style.filter	 = 'alpha(opacity=' + alphaNr + ')';
				mnt.style.MozOpacity = alphaNr/100;
				document.getElementById('messageNotifyTable').style.display='block';

				if (++counter < 10 && !hideTable) {
					alphaNr += 10;
					setTimeout('lightMessageNotify()', 40);
				} else if (counter < 101 && !hideTable) {
					setTimeout('lightMessageNotify()', 40);
				} else if (counter > 100) {
					alphaNr		-= 10;
					hideTable	 = true;
					counter		 = 10;
					setTimeout('lightMessageNotify()', 40);
				} else if (alphaNr > 0 && hideTable) {
					alphaNr -= 4;
					setTimeout('lightMessageNotify()', 40);
				} else if (alphaNr <= 0) {
					document.getElementById('messageNotifyTable').style.zIndex=-1;
					document.getElementById('messageNotifyTable').style.display='none';
				}
			} else {
				document.getElementById('messageNotifyTable').style.display='none';
			}
		}

		function closeNotify(disable) {
			document.getElementById('messageNotifyTable').style.zIndex=-1;
			document.getElementById('messageNotifyTable').style.display='none';
			alphaNr = -1;
			if (disable) {
				createCookie('notify', '1', 200);
			}

		}


	function colorCell(advid, setOn) {
		if (setOn) {
			toggleMessage(advid, true);
			document.getElementById('mbAnswerTr_' + advid).style.backgroundColor = '#FFFFDD';
			setTimeout("colorCell(" + advid + ", false)", 2500);
		} else {
			document.getElementById('mbAnswerTr_' + advid).style.backgroundColor = 'transparent';
		}
	}

function hasOptions(obj){if(obj!=null && obj.options!=null){return true;}return false;}
function moveOptionUp(obj){if(!hasOptions(obj)){return;}for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}}
function moveOptionDown(obj){if(!hasOptions(obj)){return;}for(i=obj.options.length-1;i>=0;i--){if(obj.options[i].selected){if(i !=(obj.options.length-1) && ! obj.options[i+1].selected){swapOptions(obj,i,i+1);obj.options[i+1].selected = true;}}}}
function swapOptions(obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}


function doUp(l) {
	moveOptionUp(l);
	/*
	var n = l.selectedIndex;
	if ( n < 1 ) { return; }

	for ( var i=0; i<l.options.length; i++) {
		l.options[i].selected=false;
	}
	var iu = new objBox(l.options[n].text, l.options[n].value);
	var id = new objBox(l.options[n-1].text, l.options[n-1].value);

	l.options[n-1] = new Option(iu.text, iu.value);
	l.options[n] = new Option(id.text, id.value);
	l.selectedIndex = n - 1; */
}
function doDown(l) {
	moveOptionDown(l);
	/*var n = l.selectedIndex; if ( (n < 0) || (n == l.length-1) ) return; for ( var i=0; i<l.options.length; i++ ) { l.options[i].selected=false; } var id = new objBox(l.options[n].text, l.options[n].value); var iu = new objBox(l.options[n+1].text, l.options[n+1].value); l.options[n] = new Option(iu.text, iu.value); l.options[n+1] = new Option(id.text, id.value); l.selectedIndex = n+1; */

}
function objBox(x, v)
{
this.text = x;
this.value = v;
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function jumpUp(l) {
	var n = l.selectedIndex;
	if ( n < 1 ) { return; }
	for ( var i=0; i<l.options.length; i++) {
		l.options[i].selected=false;
	}
	var opt = l.options[n];
	l.remove(n);
	if (window.navigator.userAgent.indexOf('IE') > -1) {
		l.add(opt, 0);
	} else {
		l.add(opt, l.options[0]);
	}
}

function jumpDown(l) {

	var n = l.selectedIndex;

	if ( n < 0  || (n == l.length-1)) { return; }
	for ( var i=0; i<l.options.length; i++) {
		l.options[i].selected=false;
	}
	var opt = l.options[n];
	l.remove(n);
	if (window.navigator.userAgent.indexOf('IE') > -1) {
		l.add(opt, l.options.length);
	} else {
		l.add(opt, l.options[l.options.length]);
	}
}

function moveBetweenSelect(objIn, objOut){
	for(x = 0;x<(objIn.length);x++) {
		if(objIn.options[x].selected) {
			with(objOut) {
				options[options.length] = new Option(objIn.options[x].text,objIn.options[x].value);
			}
			objIn.options[x] = null;
			x = -1;
		}
	}
	return true;
}

// "DHTML MicroAPI" building block from quirksmode.org.
// Returns an object representing a named element in HTML.
// Properties of the object: obj and style.
// Usage : var x = new getObj(id);

function getObj(name) {
  if (document.getElementById) {
  	this.obj = document.getElementById(name);
	if (this.obj) {
		this.style = document.getElementById(name).style;
    }
  }
  else if (document.all) {
	this.obj = document.all[name];
	if (this.obj) {
	    this.style = document.all[name].style;
    }
  }
  else if (document.layers) {
   	this.obj = document.layers[name];
	if (this.obj) {
   	    this.style = document.layers[name];
    }
  }
}

// =======================================
// yellow fade courtesy of Garrett Dimon at:
// http://www.yourtotalsite.com/archives/javascript/yellowfade_technique_for/Default.aspx
// =======================================
// This technique is a combination of a technique I used for highlighting FAQ's using anchors
// and the ever popular yellow-fade technique used by 37 Signals in Basecamp.
// This is the amount of time (in milliseconds) that will lapse between each step in the fade
var FadeInterval = 50;

// This is where the fade will start, if you want it to be faster and start with a lighter color, make this number smaller
// It corresponds directly to the FadeSteps below
var StartFadeAt = 13;

// This is list of steps that will be used for the color to fade out
var FadeSteps = new Array();
	FadeSteps[1] = "ffffff";
	FadeSteps[2] = "F9FFE6";
	FadeSteps[3] = "F2FFCC";
	FadeSteps[4] = "ECFFB3";
	FadeSteps[5] = "E6FF99";
	FadeSteps[6] = "DFFF80";
	FadeSteps[7] = "D9FF66";
	FadeSteps[8] = "D2FF4D";
	FadeSteps[9] = "CCFF33";
	FadeSteps[10] = "C6FF1A";
	FadeSteps[11] = "BFFF00";
	FadeSteps[12] = "ACE600";
	FadeSteps[13] = "99CC00";

function DoFade(colorId, targetId) {
	if (colorId >= 1)
	{
		document.getElementById(targetId).style.backgroundColor = "#" + FadeSteps[colorId];
		  // If it's the last color, set it to transparent
		  if (colorId==1) document.getElementById(targetId).style.backgroundColor = "transparent";
		  colorId--;
		  // Wait a little bit and fade another shade
		  setTimeout("DoFade("+colorId+",'"+targetId+"')", FadeInterval);
	}
}

var nextImages = 2;
function addImages() {
	if (nextImages < 11) {
		var div		= document.createElement('div');
		var input	= document.createElement('input');
		input.type	= 'file';
		input.name	= 'advImage_' + nextImages;
		input.id	= 'advImage_' + nextImages;
		input.size	= '40';
		input.className = 'itemImage';
		nextImages += 1;

		div.appendChild(input);
		document.getElementById('itemImages').appendChild(div);
	}
}

function removeCookie(name) {
	if (getCookie(name)) createCookie(name, '', newDate(1, 1, 1970), true);
}

function setCookie(name, value, daysExpire) {
	createCookie(name, value, daysExpire);
}
function createCookie(name,value,date, isDate) {
  var expires = "; expires="+date.toGMTString();
  document.cookie = name+"="+value+expires+"; path=/";
}
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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
// this deletes the cookie when called

function getCookie(Name) {
  var search = Name + "="
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset)
      // set index of end of cookie value
      if (end == -1)
        end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
    }
  }

}

var lastSub = -1;
	var lastSid = -1;
	function loadPages() {
		var div = document.getElementById('scrollPages');
		if (div.style.display == 'block') {
			if (document.getElementById('Aug2')) document.getElementById('Aug2').style.visibility = 'visible';
			div.style.display = 'none';
			/*
			if (lastSid != -1 && document.getElementById('subpage_' + lastSid)) {
				document.getElementById('subpage_' + lastSid).style.backgroundColor = '#fff';
			}
			var counter = 1;
			while (document.getElementById('sub' + counter)) {
				document.getElementById('scrollPages').removeChild(document.getElementById('sub' + counter));
				counter++;
			}
			lastSid = -1;
			lastSub = -1;
			document.getElementById('columnViewBack').style.display = 'none';
			*/
		} else {
			if (document.getElementById('Aug2')) document.getElementById('Aug2').style.visibility = 'hidden';
			div.style.display = 'block';
		}

	}
	function loadSubPage(sid, sub) {
		lastSub = sub;
		if (lastSid != -1 && document.getElementById('subpage_' + lastSid)) {
			document.getElementById('subpage_' + lastSid).style.backgroundColor = '#fff';
		}
		lastSid = sid;
		document.getElementById('subpage_' + lastSid).style.backgroundColor = '#eaccda';

		createSubDiv('<br /><br /><img style="margin-left:20px;" src="http://babyworld.net/images/loading_ani2.gif" alt="" />');
		
		$.get("/main/subpages.aspx", 
			{
				parentId: sid,
				sub: (sub+1)
			},
			function(data) {
				createSubDiv(data);
			}
		);

	}

	function loadSubPageGen(txt, sid, sub) {
		lastSid = sid;
		lastSub = sub;
		createSubDiv(txt);
	}

	function createSubDiv(txt) {
		if (txt != '') {
			var counter = lastSub+1;
			while (document.getElementById('sub' + counter)) {
				document.getElementById('scrollPages').removeChild(document.getElementById('sub' + counter));
				counter++;
			}
			counter = lastSub;
			if (counter >= 1 && document.getElementById('sub' + counter) && parseInt(document.getElementById('sub' + counter).style.left.replace('px', '')) > 50) {
				while (document.getElementById('sub' + counter)) {
					if (document.getElementById('sub' + counter).style.left != '') {
					document.getElementById('sub' + counter).style.left = (parseInt(document.getElementById('sub' + counter).style.left.replace('px', ''))-200) + 'px';
					}
					counter--;
				}
				document.getElementById('columnViewBack').style.display = 'block';
			}
			var subMainDiv = document.createElement('div');
			subMainDiv.id = 'sub' + (lastSub+1);
			subMainDiv.style.left = '200px';
			subMainDiv.className = 'columnsView';

			var div = document.createElement('div');
			div.innerHTML = txt;
			div.style.top = '0px';
			div.style.height = '450px';
			div.style.position = 'absolute';
			div.id = 'subText' + (lastSub+1);

			var scrollDiv = document.createElement('div');
			scrollDiv.innerHTML = '<a href="javascript:upScroll(' + (lastSub+1) + ');"><img src="/images/scroll_up.gif" alt="" /></a> <div style="height:180px;background-color:#DEDEDE;width:10px;margin-left:3px;">&nbsp;</div> <a href="javascript:downScroll(' + (lastSub+1) + ');"><img src="/images/scroll_down.gif" alt="" /></a>';
			scrollDiv.style.position = 'absolute';
			scrollDiv.style.top = '0px';
			scrollDiv.style.left = '180px';
			scrollDiv.style.height = '450px';
			scrollDiv.style.width = '20px';


			subMainDiv.appendChild(div);
			subMainDiv.appendChild(scrollDiv);

			var mainDiv = document.getElementById('scrollPages');
			mainDiv.appendChild(subMainDiv);
		} else {
			var counter = lastSub+1;
			while (document.getElementById('sub' + counter)) {
				document.getElementById('scrollPages').removeChild(document.getElementById('sub' + counter));
				counter++;
			}
		}
	}

	function columViewBackBtn() {
		if (document.getElementById('sub' + (lastSub+1))) {
			document.getElementById('scrollPages').removeChild(document.getElementById('sub' + (lastSub+1)));
		}
		var counter = lastSub;
		if (counter >= 1) {
			while (document.getElementById('sub' + counter)) {
				document.getElementById('sub' + counter).style.left = (parseInt(document.getElementById('sub' + counter).style.left.replace('px', ''))+200) + 'px';
				counter--;
			}
		}
		if (lastSub == 1) {
			document.getElementById('columnViewBack').style.display = 'none';
		}
		lastSub--;
	}

	function downScroll(id) {
		var div = document.getElementById('subText' + id);

		//var top = parseInt(div.style.top.replace('px', ''));
		//var height = parseInt(div.style.height.replace('px', ''));
		//if (height+top > 150) {
			div.style.top = (parseInt(div.style.top.replace('px', ''))-20) + 'px';
		//}
	}
	function upScroll(id) {
		var div = document.getElementById('subText' + id);
		if (div.style.top != '0px') {
			div.style.top =(parseInt(div.style.top.replace('px', ''))+20) + 'px';
		}
	}

function doQuickLink(url) {
	document.location.href=url;
}


function check() {
	if (document.getElementById('search').value == '') {
		alert('Please type in search word');
		return false;
	}
	if (document.getElementById('search').value.length < 2) {
		alert('Please type in a search word that contains more than one letter');
		return false;
	}
	return true;
}


function merkja(sid, advBool) {
	try {
		if (advBool) {

			document.getElementById('hiddenFeatures').src = "/pers/bookmark.aspx?advid=" + sid;
			var texti = document.getElementById('merkjaUmraedu');

			if (texti.innerHTML == 'Bookmarked') {
				texti.innerHTML = 'Bookmark';
			} else {
				texti.innerHTML = 'Bookmarked';
			}
		} else {
			document.getElementById('hiddenFeatures').src = "/pers/bookmark.aspx?sid=" + sid;
			var texti = document.getElementById('merkjaSidu');
			if (texti.innerHTML == 'Bookmarked page') {
				texti.innerHTML = 'Bookmark page';
			} else {
				texti.innerHTML = 'Bookmarked page';
			}
		}
	} catch (e) {
		if (advBool) {
			location.href = '/pers/bookmark.aspx?red=1&advid=' + sid;
		} else {
			location.href = '/pers/bookmark.aspx?red=1&sid=' + sid;
		}
	}
}

function hideEvent(name){
	document.getElementById(name).style.display = 'none';
	setCookie(name, 1, 2);

	var div = document.getElementById('eventBox');
	var nodeList = div.childNodes;
	var someOpen = false;
	for (var i=0;i<nodeList.length;i++) {
		if (nodeList[i].tagName != null) {
	//alert(nodeList[i].tagName + " | " + (nodeList[i].tagName.toLowerCase() == "div") + " | " + nodeList[i].style.display);
			if (document.all) {
				if (nodeList[i].tagName.toLowerCase() == "div" && nodeList[i].style.display != 'none') {
					someOpen = true;
				}
			} else {
				if (nodeList[i].tagName.toLowerCase() == "div" && nodeList[i].style.getPropertyValue("display") != 'none') {
					someOpen = true;
				}
			}
		}
	}
	if (!someOpen) {
		document.getElementById('eventBox').style.display = 'none';
	}
}

function closeAnnouncement() {
	createCookie('anouncement', '1', 2);
	document.getElementById('anouncement').style.display = 'none';
}
