﻿//Used to save comments on server
//http://commentServer.ar.halvarsson.se/remotesave/

function CommentManager() {
	
	// --
	this.islogin = false;
	this.docomment = false;
	
	
	// --
	
	
    this.comments = 0; //make array
    
    this.createComment = function()
    {
        var remote = new RemoteComments();
        
        if(this.comments == 0)
        {
            this.comments = new Array();
        }
        this.comments.length++;
        this.comments[this.comments.length-1] = remote;
        return remote;
    }
    
    this.setId = function(rand, id)
    {
        var i=0;
        var max = this.comments.length;
        for(i = 0; i < max; i=i+1)
        {
            var c = this.comments[i];
            if(c.CommentId == rand)
            {
                this.comments[i].id = id;
                //alert("ID set");
                return this.comments[i];
            }
        }
    }
    
    this.getFromId = function(rand)
    {
        var i=0;
        var max = this.comments.length;
        for(i = 0; i < max; i=i+1)
        {
            var c = this.comments[i];
            if(c.CommentId == rand)
            {
                return this.comments[i];
            }
        }
    }
    
    this.removeComment = function(rand)
    {
        var i=0;
        var max = this.comments.length;
        var startRemove = false;
        for(i = 0; i < max; i=i+1)
        {
            var c = this.comments[i];
            if(c.CommentId == rand)
            {
                startRemove = true;
            }
            
            if(startRemove)
            {
                this.comments[i] = this.comments[i+1];
            }
        }
        if(startRemove)
        {
            this.comments.length = this.comments.length -1;
        }
    }
    
    this.parseCommentaArray = function()
    {
		try
        {
//			if(comments)
//            {
				for(var i = 0; i < comments.length; i++)
                {
                    var strValue = comments[i];
                    var values = strValue.split('|');
                    
                    var comment = this.createComment();
                    comment.CommentId = values[0];
                    comment.Comment = values[1];
                    comment.wordlength = values[2];
                    comment.path = values[3];
                    comment.start = values[4];
                    comment.pl = values[5];
                    comment.row = values[6];
                    comment.col = values[7];
                    try
                    {
                        comment.pageName = values[8];
                    }
                    catch (ex)
                    {
                        //page name not set
                    }
                }
                
                return true;
            //}
        }
        catch(e)
        {
            //alert("this.parseCommentaArray");
            //error reading comments
        }
        return false;
    }
    
    this.getComments = function()
    {
        return this.comments;
    }
    
    this.IFrameDoc = 0;
//    if(this.parseCommentaArray())
//    {
//        //populate the page with written comments
//        //alert("comments read: " + this.getComments().length);
//    }
}

var commentManager = new CommentManager();

function RemoteComments() {
    this.IFrameObj = null; // our IFrame object

    var addCommentPage = "http://commentServer.ar.halvarsson.se/remotesave/AddComment.aspx";
    var editCommentPage = "http://commentServer.ar.halvarsson.se/remotesave/UpdateComment.aspx";
    var deleteCommentPage = "http://commentServer.ar.halvarsson.se/remotesave/DeleteComment.aspx";
    
    //add comment
    this.path = ""; //path
    this.start = 0; //start at
    this.wordlength = 0; //wordLength
    this.pl= 0; //document.getElementById('searchid').value
    this.row = -1;
    this.col = -1;
    this.userId = "";
    this.pageName = "";
    
    //update & delete var
    //this.id = 0; //update & delete
    this.Comment = ""; //update (c=comment)
    this.CommentId = parseInt(Math.random()*99999999);
    this.URL = "";
    
    //function to create a comment in the DB
    this.addComment = function()
    {
        try
        {
            var page = new PageHandler();
            this.pageName = page.getTitle();
        }
        catch (ex)
        {
            this.pageName = "-";
        }
        this.URL = addCommentPage; //set target for send.
        this.URL = this.URL + "?p=" + this.path + "&s=" + this.start + "&l=" + this.wordlength + "&pl=" + this.pl + "&r=" + this.row + "&c=" + this.col + "&id=" + this.CommentId + "&hash=" + this.userId + "&pageName=" + this.pageName;
        this.callToServer();
        //setTimeout("getResponseId(" + this.CommentId + ")",300);
        //this.ajaxThis(false);
    }
    //function to create a comment in the DB
    this.updateComment = function()
    {
		if(!this.userId) this.userId = loadHash();
		this.URL = editCommentPage; //set target for send.
        this.URL = this.URL + "?id=" + this.CommentId + "&c=" + this.Comment + "&hash=" + this.userId; // + "&rand=" this.id+ ;
        this.callToServer();
        //this.ajaxThis(false);
    }
    
    this.deleteComment = function()
    {
		if(!this.userId) this.userId = loadHash();
		commentManager.removeComment(this.CommentId);
        this.URL = deleteCommentPage; //set target for send.
        this.URL = this.URL + "?id=" + this.CommentId + "&hash=" + this.userId; //this.id + "&rand=" + 
        this.callToServer();
        //this.ajaxThis(false);
    }
    
//    this.saveComment = function
    
    //used to create the iframe and use it to call the remote server
    this.callToServer = function () 
    {
	    if (!document.createElement) {return true};
	    var IFrameDoc;
	    var docId = 'RSIFrame' + parseInt(Math.random()*99999999);
	    
	    if (!this.IFrameObj && document.createElement) 
	    {
		    // create the IFrame and assign a reference to the
		    // object to our global variable IFrameObj.
		    // this will only happen the first time 
		    // callToServer() is called
		    try 
		    {
			    var tempIFrame=document.createElement('iframe');
			    tempIFrame.setAttribute('id',docId);
			    tempIFrame.style.border='0px';
			    tempIFrame.style.width='0px';
			    tempIFrame.style.height='0px';
			    this.IFrameObj = document.body.appendChild(tempIFrame);
    			
			    if (document.frames) 
			    {
				    // this is for IE5 Mac, because it will only
				    // allow access to the document object
				    // of the IFrame if we access it through
				    // the document.frames array
				    this.IFrameObj = document.frames[docId];
			    }
		    } 
		    catch(exception) 
		    {
			    // This is for IE5 PC, which does not allow dynamic creation
			    // and manipulation of an iframe object. Instead, we'll fake
			    // it up by creating our own objects.
			    iframeHTML='<iframe id="RSIFrame" style="';
			    iframeHTML+='display:none;border:0px;';
			    iframeHTML+='width:0px;';
			    iframeHTML+='height:0px;';
			    iframeHTML+='"><\/iframe>';
			    document.body.innerHTML+=iframeHTML;
			    this.IFrameObj = new Object();
			    this.IFrameObj.document = new Object();
			    this.IFrameObj.document.location = new Object();
			    this.IFrameObj.document.location.iframe = document.getElementById(docId);
			    this.IFrameObj.document.location.replace = function(location) {
				    this.iframe.src = location;
			    }
		    }
	    }
    	
//	    if (navigator.userAgent.indexOf('Gecko') !=-1 && !this.IFrameObj.contentDocument) {
//		    // we have to give NS6 a fraction of a second
//		    // to recognize the new IFrame
//		    setTimeout('this.callToServer()',10); //kommer nog inte att funka som obj
//		    return false;
//	    }
    	
	    if (this.IFrameObj.contentDocument) {
		    // For NS6
		    IFrameDoc = this.IFrameObj.contentDocument; 
	    } else if (this.IFrameObj.contentWindow) {
		    // For IE5.5 and IE6
		    IFrameDoc = this.IFrameObj.contentWindow.document;
	    } else if (this.IFrameObj.document) {
		    // For IE5
		    IFrameDoc = this.IFrameObj.document;
	    } else {
		    return true;
	    }
    	
	    IFrameDoc.location.replace(this.URL);
	    //setTimeout('getResponseId(this)',500);
	    //getResponseId(IFrameDoc);
	    
	    //reset obj.
	    this.IFrameObj = null;
	    return false;
    }
}

//function init