﻿// JScript File
function NoteBook(divlayer, _onunload) {
    this.divlayer=divlayer;
    this.showAddMessage=true;
    this.showIcons=true;
    this.OnUnload=_onunload
    this.visiblenotes;
    this.maxnr=0;
    this.maxperpage=10;
    this.pagenr=1;
}
NoteBook.prototype.Show = function  (){
    this.Create(0,0)
}
NoteBook.prototype.Create=function(prev, next){
    setScrollTop()
    this.visiblenotes=new Array();
    this.divlayer.innerHTML=""
    var params="prefix="+new Date().getTime();
    if (prev>0) params+="&action=prev&nr="+prev;
    if (next>0) params+="&action=next&nr="+next;
    
    var html=htmlCreateElement("div", null, null)
    html.id="htmllayer";
    if (this.showAddMessage) html.appendChild(this.GetTableHTML(this.ShowMessagebox())); 
    params="getNotes.asp?"+params
    var xml = xmlLoad(urlHttp+params);
    var nodes=xmlXPath(xmlXPath(xml, "/root")[0], "node");
    if (nodes != null && nodes.length > 0) {
        for (var i = 0; i < nodes.length; i++) {
            var mess=(nodes[i].text==null) ? null:nodes[i].text
            if(mess==null) mess=nodes[i].childNodes[0].nodeValue
            if(mess==null) mess="";
            
             do{
                mess = mess.replace("[:)]" , "<img src='image/a.gif' border='0' />");
                mess = mess.replace("[:).]" , "<img src='image/b.gif' border='0' />");
                mess = mess.replace("[:|]" , "<img src='image/c.gif' border='0' />");
                mess = mess.replace("[:(]" , "<img src='image/d.gif' border='0' />");
           }while ( mess.indexOf("[:)]") > 0 || mess.indexOf("[:).]") > 0 || mess.indexOf("[:|]") > 0 || mess.indexOf("[:(]") > 0)
            if( this.maxnr==0) this.maxnr=parseInt(nodes[i].getAttribute("max"));
            var note=new NoteBookItem(nodes[i].getAttribute("id"), mess, nodes[i].getAttribute("name"), nodes[i].getAttribute("ipadress"), nodes[i].getAttribute("date"))
           this.visiblenotes.push(note);
           html.appendChild(this.GetTableHTML(this.ConvertItemToHTML(note.name+"<br />"+note.date.toString(), note.message)));
        }
    }
    if ( this.maxnr > 10) html.appendChild(this.ShowNavigation());
    this.divlayer.appendChild(html);
    uiSetContentHeight()
}
NoteBook.prototype.ClearHtml=function(){
    this.divlayer.innerHTML="";
}
NoteBook.prototype.ClearAll=function(){
    this.ClearHtml();
    this.divlayer=null;
    this.OnUnload=null;
    this.visiblenotes=null;
}
NoteBook.prototype.GetTableHTML=function(elems){
    var table =htmlCreateElement("table", null, "notbook")
    table.cellSpacing=0;
    table.cellPadding=0;
    var tbody=htmlCreateElement("tbody", null, null);
    var tr=htmlCreateElement("tr", null, null)
    var td=htmlCreateElement("td", null, "notetop")
    td.border=0;
    tr.appendChild(td)
    tbody.appendChild(tr)
    
    tr=htmlCreateElement("tr", null, null)
   td=htmlCreateElement("td", null, "notemiddle");
   td.border=0;
   td.align="center"
   td.appendChild(elems);
   tr.appendChild(td)
   tbody.appendChild(tr)
   
   tr=htmlCreateElement("tr", null, null)
   td=htmlCreateElement("td", null, "notebottom")
   td.border=0;
   tr.appendChild(td)
   tbody.appendChild(tr)
   table.appendChild(tbody)
   return table;
}
NoteBook.prototype.ConvertItemToHTML=function(head, message){
var table =htmlCreateElement("table", null, "notbookItem")
 table.cellSpacing=4;
    table.cellPadding=4;
    var tbody=htmlCreateElement("tbody", null, null);
    var tr=htmlCreateElement("tr", null, null)
    var td=htmlCreateElement("td", null, "notbookItemHead")
    td.innerHTML=head;
    td.border=0;
    tr.appendChild(td)
   tbody.appendChild(tr)
   
   tr=htmlCreateElement("tr", null, null)
   td=htmlCreateElement("td", null, "notbookItemMessage")
   td.innerHTML=message;
    td.border=0;
    tr.appendChild(td)
   tbody.appendChild(tr)
    table.appendChild(tbody)
    return table;
}
NoteBook.prototype.ShowMessagebox=function(head, message){

var table =htmlCreateElement("table", null, "notbookItem")
 table.cellSpacing=4;
    table.cellPadding=4;
    var tbody=htmlCreateElement("tbody", null, null);
    var tr=htmlCreateElement("tr", null, null)
    var td=htmlCreateElement("td", "Namn:", "notbookaddbox")
    td.width=110;
    td.border=0;
    td.border=0;
    tr.appendChild(td)
    
    td=htmlCreateElement("td", null, "notbookaddbox")
    td.border=0;
    td.coolspan=2
    td.innerHTML="<input type='text' size='70' id='fname' name='fname' />";
    td.border=0;
    tr.appendChild(td)
    tbody.appendChild(tr)
   
    tr=htmlCreateElement("tr", null, null)
    td=htmlCreateElement("td", "Meddelande", "notbookaddbox")
    td.vAlign="top";
    td.width=110;
    td.border=0;
   td.appendChild (this.GetTableSmiles());
    tr.appendChild(td)
    
    td=htmlCreateElement("td", null, "notbookaddbox")
    td.border=0;
    td.innerHTML="<textarea id='message' rows='5' style='width: 450px'></textarea>";
    td.border=0;
    tr.appendChild(td)
    
    td=htmlCreateElement("td", null, "notbookaddbox")
    td.border=0;
    td.width=200;
    td.vAlign="bottom";
    td.align="left";
    var bu=htmlCreateElement("input", null, null)
    bu.type="button";
    bu.name="Lägg till"
    bu.value="Lägg till"
    bu.id="myButt"
    bu.onclick=utilBind(this.OnAdd, this);
    td.appendChild(bu)
    tr.appendChild(td)
     tbody.appendChild(tr)
    table.appendChild(tbody)
    return table;
}
NoteBook.prototype.Convert=function(str){
    if (str=="0" || str==0){
        return 1
    }
    var res=0
    var tal=0
    var a=0;
    if (str.indexOf(".") > 0){
        a=str.indexOf(".")
        res=parseInt(str.substr(0,a))
        tal=parseInt(str.substr(a+1,1));
    }else if (str.indexOf(",") > 0){
        a=str.indexOf(",")
        res=parseInt(str.substr(0,a))
        tal=parseInt(str.substr(a+1,1));
    }
    if (tal==0) {
        res=parseInt(str)
    }else{
        res+=1;
    }
 return res;

}
NoteBook.prototype.ShowNavigation=function(){
    var pages=this.Convert(this.maxnr/10 + " ")
    var thisobj=this;
    var table =htmlCreateElement("table", null, null)
    table.cellSpacing=2;
    table.cellPadding=2;
    var tbody=htmlCreateElement("tbody", null, null);
    var tr=htmlCreateElement("tr", null, null)
    var td=htmlCreateElement("td", "< Föregående", null)
    
    if (this.pagenr > 1){
        td.className="clickableText"
        td.onclick=function(){
            thisobj.OnPrevious();
        }
    }else{
        td.className="unclickableText"
    }
    tr.appendChild(td)
    
    
    td=htmlCreateElement("td", "Första ", null)
    
    if (this.pagenr > 1){
        td.className="clickableText"
        td.onclick=function(){td=htmlCreateElement("td", "av ", "unclickableText")
    tr.appendChild(td)
            thisobj.OnMinNum();
        }
    }else{
        td.className="unclickableText"
    }
    tr.appendChild(td)
    td=htmlCreateElement("td", "sida "+this.pagenr, "unclickableText")
    tr.appendChild(td)
    td=htmlCreateElement("td", "av ", "unclickableText")
    tr.appendChild(td)
    
    td=htmlCreateElement("td", pages+" " , "unclickableText")
    
    
    tr.appendChild(td)
    
    td=htmlCreateElement("td", "Nästa >", null)
     if (this.pagenr != pages){
        td.className="clickableText"
        td.onclick=function(){
            thisobj.OnNext();
        }
    }else{
        td.className="unclickableText"
    }
    tr.appendChild(td)
    
    tr.appendChild(td)
    tbody.appendChild(tr)
    table.appendChild(tbody)
    return table;
}
NoteBook.prototype.OnMinNum=function(){
    this.pagenr=1;
    if(this.OnUnload!=null) this.OnUnload();
}

NoteBook.prototype.OnMaxNum=function(){

}
NoteBook.prototype.OnPrevious=function(){
    
    if (this.visiblenotes.length > 0){
        
        this.pagenr=this.pagenr-1;
        this.Create(this.visiblenotes[0].id), 0;
    }
}
NoteBook.prototype.OnNext=function(){
    this.pagenr=this.pagenr+1;
    this.Create(0, this.visiblenotes[this.visiblenotes.length-1].id);
}
NoteBook.prototype.OnAdd=function(){
     var elem=document.getElementById("message")
     var message="";
     var name="";
     if (elem.innerHTML.length < 2){
        alert("Add a message in the message field.")
        return;
     }
     message=elem.innerHTML;
     elem=document.getElementById("fname")
     if (elem.value.length < 2){
        alert("Add a name in the name field.")
        return;
     }
     name=elem.value;
     var res=xmlSend(urlHttp+"insertNotes.asp", "message="+escape(message)+"&name="+escape(name)) 
     if (res!="true"){
        alert("You can not use HTML in the guestbook.")
     }else{
        if(this.OnUnload!=null) this.OnUnload();
    }
}
NoteBook.prototype.OnSmileClick=function(obj){
    var elem=document.getElementById("message")
    elem.innerHTML=elem.innerHTML+obj;
}

NoteBook.prototype.GetTableSmiles=function(elems){
    var thisObj=this;
    var table =htmlCreateElement("table", null, null)
    table.cellSpacing=3;
    table.cellPadding=3;
    table.width=70;
    var img;
    var tbody=htmlCreateElement("tbody", null, null);
    var tr=htmlCreateElement("tr", null, null)
    var td=htmlCreateElement("td", null, null)
    
    td.border=0;
    img=htmlCreateElement("img", null, "clickable")
    img.src="image/a.gif";
    img.onclick=function (){
        thisObj.OnSmileClick("[:)]");
       }
    td.appendChild(img)
    tr.appendChild(td)

   td=htmlCreateElement("td", null, null)
    td.border=0;
    img=htmlCreateElement("img", null, "clickable")
    img.src="image/b.gif";
    img.onclick=function (){
        thisObj.OnSmileClick("[:).]");
       }
    td.appendChild(img)
   tr.appendChild(td)
    tbody.appendChild(tr)
      tr=htmlCreateElement("tr", null, null)
   td=htmlCreateElement("td", null, null)
    td.border=0;
    img=htmlCreateElement("img", null, "clickable")
    img.src="image/c.gif";
    img.onclick=function (){
        thisObj.OnSmileClick("[:|]"); 
       }
    td.appendChild(img)
   tr.appendChild(td)
    
   td=htmlCreateElement("td", null, null)
    td.border=0;
    img=htmlCreateElement("img", null, "clickable")
    img.src="image/d.gif";
    img.onclick=function (){
        thisObj.OnSmileClick("[:(]");
       }
    td.appendChild(img)
   tr.appendChild(td)
    tbody.appendChild(tr)
   table.appendChild(tbody)
   return table;
}
function NoteBookItem(_id, _message, _name, _ipadress, _date){
    this.id=_id;
    this.name=_name;
    this.ipadress=_ipadress;
    this.date=_date;
    this.message=_message;
}