function XHConn(){
  var xmlhttp;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) {
        fnDone(xmlhttp); }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function hentKommuner(str, liste){
 var myConn = new XHConn();
 if (!myConn) {
   alert('En feil oppstod, vennligst prøv igjen senere');
 }
 else {
   var fnWhenDone = function (oXML) { visKommuner(oXML.responseText, liste); };
   myConn.connect("geoId.rpc.php", "GET", "f="+str, fnWhenDone);
 }
}

function visKommuner(resultat, liste) {
	if (liste == 1) {
		res = document.getElementById('kommune1');
	} else {
		res = document.getElementById('kommune2');
	}
	
	for (var i=res.options.length-1; i>=0; i--) { res.options[i]=null; }
	
	if(resultat.length < 5){
		return;
	}
	resultat = resultat.split("|");

	for(x=0;x<resultat.length;x++){
		arr = resultat[x].split(";");
		arr[0] = arr[0].replace(/^\s+|\s+$/g, '');
		arr[1] = arr[1].replace(/^\s+|\s+$/g, '');
		
		addOption(res, arr[0], arr[1]);
	}
}

function addOption(selectmenu, val, txt, insertFirst) {
        insertFirst = (insertFirst == null) ? false : insertFirst;

        newIndex = (selectmenu[0]==null || selectmenu[0].value==0) ? 0 : selectmenu.length

        if(insertFirst && newIndex > 0) {
                // add a new Option at the end,
                // move down all and set new index to 0,
                // making the new Option to be added in top
                selectmenu[selectmenu.length]   = new Option("", "");
                for(i = selectmenu.length-1; i > 0; i--) {
                        selectmenu.options[i].value = selectmenu.options[i-1].value;
                        selectmenu.options[i].text  = selectmenu.options[i-1].text;
                }
                newIndex = 0;
        }
        var copyId      = new Option(txt,val)
        selectmenu[newIndex] = copyId
        return newIndex

}

// Jquery function
$(document).ready(function(){

  $('.innhold > h2').toggle(function(){
 		$('#showform').slideDown();
 		return false;
  },function(){
 		$('#showform').slideUp();
 		return false;
  });
  $(".report").click(function() {
	  var id = $(this).attr("dataId");
	  window.open ("report.php?id=" + id, "w","width=400,height=400");
  });
  
  $("#sendReport").click(function() {
	  var catcher = $("#egtekdeg").val();
	  //catching bots the simple way
	  if(catcher == '') {
		  var id = $("#reportId").val();
		  var txt = $("#reportText").val();
		  $.post("ajax.php", {action:'postReport',id:id,txt:txt}, function(data) {
			  msg = data.message;
			  if(msg == true) {
				  $("#message").html('V&aring;re moderatorer vil ta en titt p&aring; din klage');
			  } else {
				  $("#message").html('Vennligst fyll inn tekstfeltet');
			  }
		  }, 'json');
	  }
  });
});

function mt(ar, na) {
    str = eval('String.fromCharCode(' + ar + ')');

    if (na == '') {
        na = str;
    }

    document.write("<a href=\"javascript:void(window.open('http://www.vg.no/tips/?til=" + str + "', 'popup', 'width=450,height=500,scrollbars=no'));\">" + na + "</a>");
}

