/**********************************************************************************   
PageScroll 
*   Copyright (C) 2001 <a href="/dhtmlcentral/thomas_brattli.asp">Thomas Brattli</a>
*   This script was released at DHTMLCentral.com
*   Visit for more great scripts!
*   This may be used and changed freely as long as this msg is intact!
*   We will also appreciate any links you could give us.
*
*   Made by <a href="/dhtmlcentral/thomas_brattli.asp">Thomas Brattli</a> 
*********************************************************************************/

function lib_bwcheck(){ //Browsercheck (needed)
  this.ver=navigator.appVersion;
  this.agent=navigator.userAgent;
  this.dom=document.getElementById?1:0;
  this.opera5=this.agent.indexOf("Opera 5")>-1;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; ;
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
  this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6||this.ie7||this.ie8;
  this.mac=this.agent.indexOf("Mac")>-1;
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
  return this;
}
var bw = new lib_bwcheck()


/*********************************************************************************
These are the variables you have to set:
*********************************************************************************/

//The speed of the timeout between each scroll.
timSpeed = 50

//The height of the container (change this when it scrolls to much or to little)
contHeight = 100

//Default image align when uploading image to Peter Blog
imageAlign = "left"

//Which textBlog is being edited
actTextForm = ""
actBlogNr = 0

// Number of letters used for Slow Fat question
letterCount_que_add = 140;
letterCount_ans_add = 30;

/*********************************************************************************
This is the object constructor function, which applies 
methods and properties to the Cross-browser layer object
*********************************************************************************/
function makeScrollObj(obj,nest){
  nest=(!nest) ? "":'document.'+nest+'.';
  this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;		
  this.height=bw.ns4?this.css.document.height:this.el.offsetHeight;
  this.top=b_gettop;			
  this.left=b_getleft;
  return this;
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

//Getting the top for the top method
function b_gettop(){
  var gleft=(bw.ns4 || bw.ns6)?parseInt(this.css.top):eval(this.css.pixelTop);
  return gleft;
}

//Getting the left for the left method
function b_getleft(){
  var gleft=(bw.ns4 || bw.ns6)?parseInt(this.css.left):eval(this.css.pixelLeft);
  return gleft;
}

//Variables
var scrollTim = 1;
var active = 1;
var fastSpeed = 1;
/*********************************************************************************
The scroll function. Checks what way to scroll and checks if the
layer is not already on top or bottom.
*********************************************************************************/
function scroll(speed){
  clearTimeout(scrollTim);
  way = speed>0?1:0;
  orgSpeed = speed;
  speed = fastSpeed*speed;
  if ((!way && oScroll[active].top()>-oScroll[active].height+contHeight) || (oScroll[active].top()<0 && way)){
    oScroll[active].css.top = (oScroll[active].top()+speed)+px;
    scrollTim = setTimeout("scroll("+orgSpeed+")",timSpeed);
  }
}

function doubleSpeed() {
  fastSpeed = 2;
}

function goToOrgSpeed() {
  fastSpeed = 1;
}

/*********************************************************************************
The scroll function to the left. Checks what way to scroll and checks if the
layer is not already on top or bottom. var Active is set to 0
*********************************************************************************/
function scrollLeftTmp(speed){
  clearTimeout(scrollTim);
  way = speed>0?1:0;
  orgSpeed = speed;
  speed = fastSpeed*speed;
  if ((!way && oScroll[0].top()>-oScroll[0].height+contHeight) || (oScroll[0].top()<0 && way)){;
  oScroll[0].css.top = (oScroll[0].top()+speed)+px;
  scrollTim = setTimeout("scrollLeftTmp("+orgSpeed+")",timSpeed);
  }
}

//Clears the timeout so the scroll stops, this is called onmouseout.
function noScroll(){
  clearTimeout(scrollTim);
}
/*********************************************************************************
Changes the active layer. Hides the one that's visible and
shows the "new" one. Also set's the new layers top to
0 so it starts at top.
*********************************************************************************/
function changeActive(num){
  oScroll[active].css.visibility = "hidden";
  active = num;
  oScroll[active].css.top = 0+px;
  oScroll[active].css.visibility = "visible";
}
/*********************************************************************************
Initilizes the page, makes a oScroll Array and calls the object constructor.
Here you can add as many scrollObjects as you want
*********************************************************************************/
function scrollInit(isGuestBook, nr){
  if (isGuestBook === true) {
    firstVisible = 3;//2;
    active = 3;//2;
    nr++;
  }
  else {
    firstVisible = 2;
  }
  oScroll = new Array();
  // skapa objekt för menyn till vänster
  oScroll[0] = new makeScrollObj('divScroll0','divContL');
  // skapa objekt för varje inlägg till höger
//  alert(nr+1);
  for(var i=1; i<=nr+1; i++) {
    oScroll[i] = new makeScrollObj('divScroll'+i,'divCont');
  }
  
  // Gör menyn till vänster synlig
  oScroll[0].css.left = 0+px;
  oScroll[0].css.top = 0+px;
  oScroll[0].css.visibility = "visible";
  
  if (isGuestBook === true) {
    firstVisible = 3;//2;
    active = 3;//2;
  }
  else {
    firstVisible = 1;
    active = 1;
  }
  if (nr != -1) {
	  // Gör lagret med alla inlägg på varandra synlig
  	oScroll[firstVisible].css.left = 0+px;
  	oScroll[firstVisible].css.top = 0+px;
  	oScroll[firstVisible].css.visibility = "visible";
  }
  
}
/*********************************************************************************
Multiple Scripts
If you have two or more scripts that use the onload event, probably only one will run (the last one).
Here is a solution for starting multiple scripts onload:
   1. Delete or comment out all the onload assignments, onload=initScroll and things like that.
   2. Put the onload assignments in the body tag like in this example, note that they must have braces ().
   Example: <body onload="initScroll(); initTooltips(); initMenu();">
*********************************************************************************/

var w2 = 0;
function display(fil, extra) {
  if (w2 != 0) w2.close();
  w2 = window.open(fil, "PictureWindow", extra);
  w2.focus();
}

/*********************************************************************************
Function to make sure valuable info is put when posting to
the guestbook
*********************************************************************************/
function validate(form) {
  if (form.kod != null) {
    if (form.kod.value.length == 0) {
      alert ('Fyll i fältet med siffrorna till vänster');
      form.kod.focus();
      return false;
    }
    form.submit();
  }
  else if (form.namn.value.length == 0) {
    alert ('Hitta på ditt namn!');
    form.namn.focus();
    return false;
  }
  else if (form.message.value.length == 0 ) {
    alert ('Inget meddelande');
    form.message.focus();
    return false;
  }
  else	{
    targetForm = form;
    srcForm = document.message_form_final;
    srcForm.namn.value = targetForm.namn.value;
    srcForm.www.value = targetForm.www.value;
    srcForm.message.value = targetForm.message.value;
    if (targetForm.id != null) {
      srcForm.id.value = targetForm.id.value;
    } else {
      srcForm.id.value = 0;
    }
    changeActive(1);
    return false;
  }

}

function validate_old(form) {
	alert('validate, kod: '+form.kod.value);
  if (form.kod != null) {
    if (form.kod.value.length == 0) {
      alert ('Fyll i fältet med siffrorna till vänster');
      form.kod.focus();
      return false;
    }
    return true;
  }
  else if (form.namn.value.length == 0) {
    alert ('Hitta på ditt namn!');
    form.namn.focus();
    return false;
  }
  else if (form.message.value.length == 0 ) {
    alert ('Inget meddelande');
    form.message.focus();
    return false;
  }
  else	{
    targetForm = form;
    srcForm = document.message_form_final;
    srcForm.namn.value = targetForm.namn.value;
    srcForm.email.value = targetForm.email.value;
    srcForm.www.value = targetForm.www.value;
    srcForm.message.value = targetForm.message.value;
    if (targetForm.id != null) {
      srcForm.id.value = targetForm.id.value;
    } else {
      srcForm.id.value = 0;
    }
    changeActive(1);
    return false;
  }

}

/*********************************************************************************
Function to make sure valuable info is put when posting to
the guestbook
*********************************************************************************/
function validateComment(form) {
  if (form.kod != null) {
    if (form.kod.value.length == 0) {
      alert ('Fyll i fältet med siffrorna till vänster');
      form.kod.focus();
      return false;
    }
    return true;
  }
  else if (form.namn.value.length == 0) {
    alert ('Hitta på ditt namn!');
    form.namn.focus();
    return false;
  }
  else if (form.message.value.length == 0 ) {
    alert ('Inget meddelande');
    form.message.focus();
    return false;
  }
  else	{
    targetForm = form;
    srcForm = document.comment_form_final;
    srcForm.namn.value = targetForm.namn.value;
    srcForm.message.value = targetForm.message.value;
    srcForm.journal_id.value = targetForm.journal_id.value;
    if (targetForm.id != null) {
      srcForm.id.value = targetForm.id.value;
    } else {
      srcForm.id.value = 0;
    }
    changeActive(1);
    return false;
  }

}

/*********************************************************************************
Function to show form to add image
*********************************************************************************/
function showAddImage(textForm, blogNr, selectedIndex) {
        selectedAligns = ["left", "right", "bottom", "center"];
        imageAlign = selectedAligns[selectedIndex];
        document.alignpblogpic.src = "images/alignpblogpic_"+imageAlign+".jpg";
        document.image_upload.align[selectedIndex].checked = true;
        actBlogNr = blogNr;
        actTextForm = textForm;
        changeActive(1);
        return false;
}

/*********************************************************************************
Function to add image information to textform
*********************************************************************************/
function validateAddImage() {
        actTextForm.imageAlign.value = imageAlign;
        actTextForm.addImage.value = "placera : " + imageAlign;
        changeActive(actBlogNr);
        return false;
}
/*********************************************************************************
Function to exchange line breaks with <br> to be better
published 
*********************************************************************************/
function sendSubmit(form){
  //textarea is reference to that object, <br> is string that will replace the encoded return
  form.blogText.value = escape(form.blogText.value); //encode blogText string's carriage returns

  for(i=0; i<form.blogText.value.length; i++){ 
    //loop through string, replacing carriage return encoding with HTML break tag    
    if(form.blogText.value.indexOf("%0D%0A") > -1){ 
      //Windows encodes returns as \r\n hex
      form.blogText.value=form.blogText.value.replace("%0D%0A","<br>");
    }
    else if(form.blogText.value.indexOf("%0A") > -1){ 
      //Unix encodes returns as \n hex
      form.blogText.value=form.blogText.value.replace("%0A","<br>");
    }
    else if(form.blogText.value.indexOf("%0D") > -1){ 
      //Macintosh encodes returns as \r hex
      form.blogText.value=form.blogText.value.replace("%0D","<br>");
    }
  }
  form.blogText.value=unescape(form.blogText.value); //unescape all other encoded characters
  //form.submit();
  return true;
}

function centerMain() {
  winW = 0;
  if (parseInt(bw.ver) > 3) {
    if (bw.ns4 || bw.ns6) {
      winW = window.innerWidth-16;
      //winH = window.innerHeight-16
    }
    if (bw.ie) {
      winW = document.body.offsetWidth-20;
      //winH = document.body.offsetHeight-20
    }
  }
  leftCenterPos = Math.max((winW-700)/2);
  mainCont.css.left = leftCenterPos+px;
  mainCont.css.visibility = "visible";
}

function initCenterMain() {
  mainCont = new makeScrollObj('divMain', 'divContMain');
  centerMain();
  window.onresize = centerMain;

}

function removeNewLine(text) {
	  //textarea is reference to that object, <br> is string that will replace the encoded return
  text = escape(text); //encode blogText string's carriage returns

  for(i=0; i<text.length; i++){ 
    //loop through string, replacing carriage return encoding with HTML break tag    
    if(text.indexOf("%0D%0A") > -1){ 
      //Windows encodes returns as \r\n hex
      text=text.replace("%0D%0A","<br>");
    }
    else if(text.indexOf("%0A") > -1){ 
      //Unix encodes returns as \n hex
      text=text.replace("%0A","<br>");
    }
    else if(text.indexOf("%0D") > -1){ 
      //Macintosh encodes returns as \r hex
      text=text.replace("%0D","<br>");
    }
  }
  text=unescape(text); //unescape all other encoded characters
  //form.submit();
  return text;
}

function letterCount(numberLetter, sourceArea, target_oScroll) {
	if (numberLetter == 0) {
		newLetterCount = letterCount_que_add - sourceArea.value.length;
	}
	else if (numberLetter == 1) {
		newLetterCount = letterCount_ans_add - sourceArea.value.length;
	}
	target_oScroll.css.font = '10px/16px verdana, sans-serif';
  target_oScroll.css.color = newLetterCount < 0 ? 'red' : '#000';  
	target_oScroll.el.innerHTML = '<table width="100%" border="0"><tr height="100%" valign="center"><td align="left" class="textLeft" width="100%">';
	target_oScroll.el.innerHTML += 'Tecken: '+newLetterCount;
	target_oScroll.el.innerHTML += '</td></tr></table>';
}

function showComment(who) {
	if (document.getElementById(who) != null) {
		if (document.getElementById(who).style.display == 'inline') {
			hideComment(who);
			return;
		}
		document.getElementById(who).style.display = 'inline';
	}
}

function hideComment(who) {
	if (document.getElementById(who) != null) {
		document.getElementById(who).style.display = 'none';
	}
}

function loadFormParameter(form_parameter, para_value) {
	form_parameter.value = para_value;
}

function answerThread(thread_id, rubrik) {
	document.message_form.thread_id.value = thread_id;
	if (rubrik.indexOf("re: ") != 0) {
		rubrik = "re: " + rubrik;
	}
	document.message_form.rubrik.value = rubrik;
	changeActive(2);
}

function newSpam() {
	document.message_form.thread_id.value = 0;
	document.message_form.rubrik.value = '';
	changeActive(2);
}
