function redirectOnVSError() {
  url = ''+window.location;
  if(url.match('o//')){
    window.location = 'http://www.sonans.no/'+url.substring(url.indexOf('o//')+3,url.length);
  } 
}

function newPosting(qkey, qid) {
  // Legg inn cookie
  createCookie('author',document.formPosting['author'].value,31);
  createCookie('email',document.formPosting['email'].value,31);
  // Validering
  var ok=true;
  if (document.formPosting['title'].value == '')
  ok=false;
  if (document.formPosting['author'].value == '')
  ok=false;
  if (document.formPosting['body'].value == '')
  ok=false;
  if (!ok) {
    alert("Du må fylle inn alle feltene merket med rød stjerne");
    return false;
  }

  
  // Captcha-juksemetode
  var qurl = 'page?id='+qid+'&key='+qkey+'&code='+document.getElementById('captcha').value+'&cache='+Math.random();
 xmlHttp1=GetXmlHttpObject();

  xmlHttp1.onreadystatechange=captchaVerified
 xmlHttp1.open("GET",qurl, false)
  xmlHttp1.send(null) 
//if (xmlHttp1.readyState==4){captchaVerified();}
if (xmlHttp1.status==200){
	captchaVerified();
}

}
var is_submitted_stupid_fix = 0;
function captchaVerified() {
  var no = 0;
  if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete") { 
    // Sjekk innkommende resultat fra verifisering av captcha
    var qanswer = '';
    qanswer = xmlHttp1.responseText;
    if(qanswer!='true') {
      alert('Du har ikke skrevet inn riktig verifiseringskode.');
      return false;
    } else {
      document.formPosting['ticket'].value = document.formPosting['ticket1'].value;
      document.formPosting['handler'].value = 'content_sendmail';
      document.formPosting['url'].value = document.formPosting['redirect'].value;
if (! is_submitted_stupid_fix){
   		   is_submitted_stupid_fix=1;		     
document.formPosting.submit();
}
    }
  }
}

function slogan(phrase) {
  document.getElementById("slogan").innerHTML = phrase;
  return false;
}
function openURL(formObj) { 
  // Rullegardin-fagvalg på forside og artikkelsider
  selInd = formObj.SelectURL.selectedIndex; 
  goURL = formObj.SelectURL.options[selInd].value;
  top.location.href = goURL;
}
function openURLNewWindow(formObj) { 
  // Rullegardin-fagvalg på forside og artikkelsider
  selInd = formObj.SelectURL.selectedIndex; 
  goURL = formObj.SelectURL.options[selInd].value;
  window.open(goURL);
}

function faqSearch(formObj) {
  formObj.query.value = '(data/* LIKE "%'+formObj.q.value+'%")';
  return true;
}
function detectFlash() {
  // Husk å inkludere swfobject.js
  flash_enabled = (deconcept.SWFObjectUtil.getPlayerVersion().major > 0);
  if (!flash_enabled) {
    document.getElementById("flash").style.display = 'none';
    document.getElementById("noflash").style.display = 'inline';
  }
}
function popUp(url) {
  newwindow=window.open(url,'name','height=380,width=500');
  if (window.focus) {newwindow.focus()}
  return false;
}
function viewSchool(formObj) {
  document.getElementById("soknad_drammen").style.display = "none";
  document.getElementById("soknad_bergen").style.display = "none";
  document.getElementById("soknad_oslo").style.display = "none";
  document.getElementById("personalia").style.display = "none";
  document.getElementById("soknad_bunn").style.display = "none";

  skole = formObj.skoleDrop.options[formObj.skoleDrop.selectedIndex].value;
  if (skole != '#') {
    //document.getElementById("skolevelger").style.display = 'none';
    document.getElementById(skole).style.display = 'inline';
    document.getElementById("personalia").style.display = 'inline';
    document.getElementById("soknad_bunn").style.display = 'inline';
  }
}

 function formPostingCheck(qid,qkey,qcode) {
      // Legg inn cookie
      createCookie('author',document.formPosting['author'].value,31);
      createCookie('email',document.formPosting['email'].value,31);
      // Validering
      var ok=true;
      if (document.formPosting['title'].value == '')
      ok=false;
      if (document.formPosting['author'].value == '')
      ok=false;
      if (document.formPosting['body'].value == '')
      ok=false;
      if (!ok) {
      alert("Du må fylle inn alle feltene merket med rød stjerne");
      return false;
      }
     // Captcha-juksemetode
     var qurl = 'page?id='+qid+'&key='+qkey+'&code='+qcode+'&cache='+Math.random();
     xmlHttp1=GetXmlHttpObject();
     xmlHttp1.onreadystatechange=captchaLoaded
     xmlHttp1.open("GET",qurl, false)
     xmlHttp1.send(null)      
}

function captchaLoaded() { 
    var no = 0;
    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete") { 
       // Sjekk innkommende resultat fra verifisering av captcha
       var qanswer = '';
       qanswer = xmlHttp1.responseText;
       if(qanswer!='true') {
         alert('Du har ikke skrevet inn riktig verifiseringskode.');
         return false;
       } else {
         document.getElementById("submitbutton").style.display = 'none';
         window.status = "Loading..";
         document.body.style.cursor = 'wait';
         
         // Send det skjulte skjemaet vha ajax
         document.getElementById("mailForm").Innlegg.value = document.getElementById("formPosting").body.value;
         mottakere = document.getElementById("formPosting").receivers.value;
         mottakerarray = mottakere.split(",");
        
         if(mottakerarray.length > 1) {
               no = 0;
               tid = document.getElementById("formPosting").tid.value;
               document.getElementById('mailForm').to.value = mottakerarray[no];
               var turl = 'page?id='+tid+'&cache='+Math.random();
               xmlHttp2=GetXmlHttpObject();
               xmlHttp2.open("GET",turl,false)
               xmlHttp2.send(null)
               xmlHttp2.onreadystatechange=sendmailstate(0)
         } else {
            document.getElementById('mailForm').to.value = mottakere;
            $('#mailForm').ajaxSubmit()
         }
         // Send det viste skjemaet på vanlig måte
         setTimeout("document.formPosting.submit();",8000);
         return false;
       }
    } 
}

function sendmailstate(no) {
    window.onerror = stopError;
    if (isNaN(no)) {no = 0; }
    if (xmlHttp2.readyState==4) { 
       var ticket = '';
       ticket = xmlHttp2.responseText;
       document.getElementById('mailForm').ticket.value = ticket;
       
       $('#mailForm').ajaxSubmit()

       no++;
       
       if(no < mottakerarray.length) {
          try {
            document.getElementById('mailForm').to.value = mottakerarray[no];
            var turl = 'page?id='+tid+'&cache='+Math.random();
            xmlHttp2=GetXmlHttpObject();
            xmlHttp2.open("GET",turl,false)
            xmlHttp2.send(null)
            xmlHttp2.onreadystatechange=sendmailstate(no)
          } catch (e) { 
            setTimeout("document.formPosting.submit();",6000);
          }
       }
   }
}

function GetXmlHttpObject() {
var xmlHttp=null;
try {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e) {
 // Internet Explorer
 try {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e) {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function stopError() {
  return true;
}

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);
}

function getCookies() {
  document.formPosting['author'].value = readCookie('author');
  document.formPosting['email'].value = readCookie('email');
}
function addTag(myValue1, myValue2) {
  myField = document.getElementById("body");
  //IE
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue1+sel.text+myValue2;
    myField.focus();
  }
  //MOZILLA/NETSCAPE
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + myValue1 + myField.value.substring(myField.selectionStart, myField.selectionEnd)
    + myValue2 +myField.value.substring(endPos, myField.value.length);
    //myField.selectionStart = startPos+3;
    //myField.selectionEnd = endPos + myValue1.length + startPos;
    myField.focus();
  } else {
    myField.value += myValue1+myValue2;
  }
}

function addUrl() {
  var url = 'http://';
  for (;;) {
    url = prompt('Skriv inn URL', url);
    if (url == null) return; // Har bruker klikket avbryt? Steike!
    copy = url.toLowerCase();
    if (copy == 'http://' || (
      copy.substring(0,7) != 'http://' &&
      copy.substring(0,8) != 'https://' &&
      copy.substring(0,6) != 'ftp://' &&
      copy.substring(0,7) != 'mailto:')) {
      alert("Ugyldig lenke");
      continue;
    }
    break;
  }
  var urltitle;
  urltitle = prompt('Skriv inn navn på lenken', '');
  if (urltitle == '') {
    urltitle = url;    
  }
  addTag('','[url=' + url + ']' + urltitle + '[/url]');
}
function loadeditors (){
  oFCKeditor = new FCKeditor('text');

  oFCKeditor.ToolbarSet='Basic';
  oFCKeditor.BasePath = "/FCKeditor/";
  oFCKeditor.Height = 550 ;
  oFCKeditor.Width = 600;
  oFCKeditor.ReplaceTextarea();
}
function HideShow(id){
    var element = document.getElementById(id);
   element.style.display == 'none'  ? element.style.display = '' : element.style.display='none';
}
function ChangeClass(element,class1,class2){
element.className == class1  ? element.className = class2 : element.className=class1;
}

function deleteFile(inputEl,textEl,btnEl) {
    if (inputEl && textEl && btnEl) {
        var conf = confirm('Er du sikker på at du vil slette filen ' + document.getElementById(textEl).innerHTML + '?');
        if (conf == true) {
            if (inputEl) {
                document.getElementById(inputEl).value = -1;
            }
            if (textEl) {
                document.getElementById(textEl).innerHTML = '';
            }
            if (btnEl) {
                document.getElementById(btnEl).disabled = true;
                document.getElementById(btnEl).onmouseup = '';
            }
        }
    }
}

function display_list(id,nr){
	this.id = id;
	this.nr = nr;
	this.current = 1;
	this.nextElement = function(){
		//hide element

		object = document.getElementById(this.id + this.current);
		object.style.display = 'none';
           		if (this.current== this.nr){
			this.current = 1;
		}
		else{
			this.current=this.current + 1;
		} 
	              		object= document.getElementById(this.id + this.current);
		object.style.display='block';
	}
	this.previousElement = function(){
		//hide element                      
object = document.getElementById(this.id + this.current);
		object.style.display = 'none';

		if (this.current== 1){
			this.current = this.nr;
		}
		else{
			this.current= this.current - 1;
		} 
	                    
		object= document.getElementById(this.id + this.current);
		object.style.display='block';
	}
}

function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;}
 




