//$Id: common.js 1078 2007-07-26 09:32:14Z ingvig $
//(c) Siminn.is

//crud cookies
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 eraseCookie(name) {
	createCookie(name,"",-1);
}

//insert after element
function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
	Element.addClassName( element, value );
}


//highlight page
function highlightPage() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("navigation")) return false;
  var nav = document.getElementById("navigation");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      links[i].className = "here";
      var linktext = links[i].lastChild.nodeValue.toLowerCase();
      document.body.setAttribute("id",linktext);
    }
  }
}

//pop up
function siminn_popup(link, window_width, window_height) {
  var theTarget = "_blank"; // Set default value for the link target
  if ((link.target != null) && (link.target != "")) {
    theTarget = link.target;    
  }

  newWin = window.open(link.href, theTarget, "width="+ window_width +", height="+ window_height);
  newWin.focus(); // make sure the new window has focus.
  return false;
}

function contactme(slod)  {
open(slod, "displayWindow", "toolbar=0,location=0, status=0, menubar=0, resizable=0, copyhistory=0, scrollbars=1, width=600,height=620");
}
function kuba(slod)  {
open(slod, "displayWindow", "toolbar=0,location=0, status=0, menubar=0, resizable=0, copyhistory=0, scrollbars=1, width=770,height=650");
}

//styling ie for abbr
 function styleAbbr() {
  var oldBodyText, newBodyText, reg
  if (isIE) {
    oldBodyText = document.body.innerHTML;
    reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
    newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2</span></abbr>');
    document.body.innerHTML = newBodyText;
  }
}
isIE = (document.all) ? true:false;

//eq-height
function setHeight(id){
	single = id.length <= 1 ? true : false;
	cell = new Array();
		if (single && document.getElementById( id[0] ) == null) {
			return;
			}

id = single ? document.getElementById( id[0] ).childNodes : id;

	for(cellCount=0; cellCount<id.length; cellCount++){
		if(String(id[cellCount].tagName).toLowerCase()=="div" && single)
		cell.push( id[cellCount] );
		else if( !single )
		cell.push( document.getElementById(id[cellCount]) );
		}
		for(checkHeight=0, maxHeight=0; checkHeight<cell.length; checkHeight++) {
			if (cell[checkHeight] != null) {
				maxHeight = cell[checkHeight].offsetHeight>maxHeight ? cell[checkHeight].offsetHeight : maxHeight;
				}
			}
			for(var i=0; i<cell.length; i++) {
				if (cell[i] != null) {
					cell[i].style.height = String(maxHeight)+"px";
					}
			}
}
//changeActionAndSubmit
function changeActionAndSubmit(url,elementID)
{
var form = $( elementID );
   form.action=url;
   form.submit();
}

//Alternating row color
function alternate_init() {
	if (!document.getElementsByTagName) return;
	tbls = document.getElementsByTagName("table");
	for (ti=0;ti<tbls.length;ti++) {
		thisTbl = tbls[ti];
		if (((' '+thisTbl.className+' ').indexOf("alternate_rows") != -1) && (thisTbl.id)) {
			alternate(thisTbl);
		}
	}
}

function addEvent(elm, evType, fn, useCapture)
{
	Event.observe( elm, evType, fn, useCapture );
} 

function replace(s, t, u) {
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
}

function alternate(table) {
	var tableBodies = table.getElementsByTagName("tbody");
	for (var i = 0; i < tableBodies.length; i++) {
		var tableRows = tableBodies[i].getElementsByTagName("tr");
		for (var j = 0; j < tableRows.length; j++) {
			if ( (j % 2) == 0  ) {
				if (tableRows[j].className == 'odd' || !(tableRows[j].className.indexOf('odd') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'odd', 'even');
				} else {
					tableRows[j].className += " even";
				}
			} else {
				if (tableRows[j].className == 'even' || !(tableRows[j].className.indexOf('even') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'even', 'odd');
				}
				tableRows[j].className += " odd";
			} 
		}
	}
}

//buttons
var btn = {
    init : function() {
        if (!document.getElementById || !document.createElement || !document.appendChild) return false;
        as = btn.getElementsByClassName('btn(.*)');
        for (i=0; i<as.length; i++) {
            if ( as[i].tagName == "INPUT" && ( as[i].type.toLowerCase() == "submit" || as[i].type.toLowerCase() == "button" ) ) {
                var tt = document.createTextNode(as[i].value);
                var a1 = document.createElement("a");
                a1.className = as[i].className;
                a1.id = as[i].id;
                as[i] = as[i].parentNode.replaceChild(a1, as[i]);
                as[i] = a1;
                as[i].style.cursor = "pointer";
            }
            else if (as[i].tagName == "A") {
                var tt = as[i].firstChild;
            }
            else { return false };
            var i1 = document.createElement('i');
            var i2 = document.createElement('i');
            var s1 = document.createElement('span');
            var s2 = document.createElement('span');
            s1.appendChild(i1);
            s1.appendChild(s2);
            s1.appendChild(tt);
            as[i].appendChild(s1);
            as[i] = as[i].insertBefore(i2, s1);
        }
    },
    addEvent : function(obj, type, fn) {
        Event.observe( obj, type, fn, false );
        },
    getElementsByClassName : function(className, tag, elm) {
        var testClass = new RegExp("(^|\s)" + className + "(\s|$)");
        var tag = tag || "*";
        var elm = elm || document;
        var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
        var returnElements = [];
        var current;
        var length = elements.length;
        for(var i=0; i<length; i++){
            current = elements[i];
            if(testClass.test(current.className)){
                returnElements.push(current);
            }
        }
        return returnElements;
    }
}
btn.addEvent(window,'load', function() { btn.init();} );

//find form
findForm : function(f) {
	while(f.tagName != "FORM") {
	f = f.parentNode;
	}
	 return f;
}

btn.addEvent(document.getElementById('reset_btn'),'click',function() {
	 var form = btn.findForm(this);
	 form.reset();
});

btn.addEvent(document.getElementById('submit_btn'),'click',function() {
	 	var form = btn.findForm(this);
	 	form.submit();
});


//focus on inputs
	sfFocus = function() {
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}
}

//hints on forms
function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	//repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}
Event.observe(window, 'load', prepareInputsForHints, false);
Event.observe(window, 'load', highlightpage, false);
Event.observe(window, 'load', sfFocus, false);
Event.observe(window, 'load', styleAbbr, false );
Event.observe(window, "load", alternate_init, false );