var oddity = new Object();                                                                                                              /* instantiate our base oddity object. */
oddity.commentor = new Object();                                                                                                /* instantiate our commentor class which will be used to add and view user comments. */
oddity.commentor.div = new Object();


/***
 * Global oddity class functions and variables are defined below.
 */
oddity.IE = document.all? true:false;
oddity.http = null;                                                                                                                             /* the http token is used to hold the users xmlHTTPrequest object; but first we define it as null. */
oddity.mousex = null;
oddity.mousey = null;
oddity.get_httpobject = function() {                                                                                    /* this function will return the correct xmlHTTPrequest object based upon the browser client that is making the request. */
        oddity.http = null;
        if(window.XMLHttpRequest)
                return new XMLHttpRequest();    // FireFox, Safari, Opera, etc...
        else if(window.ActiveXObject)
                return new ActiveXObject("Microsoft.XMLHTTP");  // IE
}
oddity.update_cursor = function(e) {
        if (oddity.IE) {
            oddity.mousex = event.clientX + document.body.scrollLeft;
            oddity.mousey = event.clientY + document.body.scrollTop;
        }
        else {
            oddity.mousex = e.pageX;
            oddity.mousey = e.pageY;
        }  
        if (oddity.mousex < 0) oddity.mousex = 0;
        if (oddity.mousey < 0) oddity.mousey = 0;  
        return true;
}


/***
 * Sub-object oddity.commentor functions and variables are defined below.
 */
oddity.commentor.key = null;                                                                                                    /* this key token is used to hold the current id of the selected item whose comments we are grabbing. */
oddity.commentor.div.wrapper = null;                                                                                    /* this wrapper token is used to hold the link to the parent div which contains the commentors html forms. */
oddity.commentor.div.ccomment = null;                                                                                   /* this ccomment token is used to hold the link to the comment form which is never directly visible via the ccomment div. */
oddity.commentor.div.rcomment = null;                                                                                   /* this rcomment token is used to hold the link to the read comment data which is never directly visible via the rcomment div. */
oddity.commentor.cmnttxtarea = null;
oddity.commentor.svpath = '/_oddity_javascript/sv_comment.php';
oddity.commentor.launch_commentor = function($cid) {                                                    /* this function will initiate the process of loading and positioning the commentors html forms. */
        oddity.commentor.div.wrapper = document.getElementById("jsoddcommwrapper");
        oddity.commentor.div.ccomment = document.getElementById("TB-CREATECOMMENT");
        oddity.commentor.div.rcomment = document.getElementById("TB-READCOMMENTS");
        oddity.commentor.cmnttxtarea = document.getElementById("cmnttxtarea");
        oddity.commentor.key = $cid;
        oddity.commentor.load_ccomment();
}
oddity.commentor.show_wrapper = function() {
        oddity.commentor.div.wrapper.style.visibility = "visible";
}
oddity.commentor.load_ccomment = function() {
        oddity.commentor.div.wrapper.style.left = (oddity.mousex-200)+'px';
        oddity.commentor.div.wrapper.style.top = (oddity.mousey-60)+'px';
        oddity.commentor.show_wrapper();
        oddity.commentor.cmnttxtarea.value = "";
        oddity.commentor.div.ccomment.style.visibility = 'visible';
        oddity.commentor.cmnttxtarea.select();
        oddity.commentor.cmnttxtarea.focus();
}
oddity.commentor.hide_wrapper = function() {
        document.location = document.location;
        oddity.commentor.div.wrapper.style.visibility = 'hidden';
        oddity.commentor.div.ccomment.style.visibility = 'hidden';
        oddity.commentor.div.rcomment.style.visibility = 'hidden';
        oddity.commentor.div.wrapper.style.right = '0px';
        oddity.commentor.div.wrapper.style.top = '0px';
}
oddity.commentor.save_comment = function() {
        if ((oddity.commentor.cmnttxtarea.value==" ") || (oddity.commentor.cmnttxtarea.value=="")) {
                alert("The comment you have provided is invalid, please try again.");
        }
        else {
                alert('Thanks, adding your comment now..');
                
                var parameters = 'cmnt='+oddity.commentor.cmnttxtarea.value+'&id='+oddity.commentor.key;
                oddity.http = oddity.get_httpobject();
                oddity.http.onreadystatechange = oddity.commentor.notify;
                oddity.http.open('POST', oddity.commentor.svpath, true);
                oddity.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                oddity.http.setRequestHeader("Content-length", parameters.length);
                oddity.http.setRequestHeader("Connection", "close");
                oddity.http.send(parameters);
        }
}

var isreply = 0;
oddity.commentor.notify = function() {
        if ((oddity.http.readyState==4)) {
                if ((oddity.http.status==200)) {
                        var result = oddity.http.responseText;

                        //alert('Thank you, your comment has been added to the system.');
//                      oddity.commentor.hide_wrapper();
//                      oddity.commentor.view_comments();
                        document.getElementById("commentssss").innerHTML = result;
                        if (isreply == 0) {
                                addcommentcount();
                        }
                        
                }
                        
        } else {
                //alert('OOPS! An error occured while saving your comment.');
        }
        
}
oddity.commentor.view_comments = function() {
        document.location.href = "/comments_"+oddity.commentor.key+".html#COMMENTS";
}

if (!oddity.IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = oddity.update_cursor;


oddity.commentor.save_comment2 = function(cid) {
        if ((document.getElementById("cmnttxtarea").value==" ") || (document.getElementById("cmnttxtarea").value=="")) {
                alert("The comment you have provided is invalid, please try again.");
        }
        else {
                alert('Thanks, adding your comment now..');
                oddity.commentor.check_reply();
                var parameters = 'cmnt='+URLEncode(document.getElementById("cmnttxtarea").value)+'&username='+document.getElementById("username").value+'&nolimit='+document.getElementById('nolimit').value;
                if (reply_id != 0) {
                        parameters += '&action=reply';
                        parameters += '&id='+reply_id;
                        isreply = 1;
                }else {
                        parameters += '&id='+document.getElementById("itemid").value;
                        isreply = 0;
                }              
                oddity.http = oddity.get_httpobject();
                oddity.http.onreadystatechange = oddity.commentor.notify;
                oddity.http.open('POST', oddity.commentor.svpath, true);
                oddity.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                oddity.http.setRequestHeader("Content-length", parameters.length);
                oddity.http.setRequestHeader("Connection", "close");
                oddity.http.send(parameters);
                document.getElementById("cmnttxtarea").value = '';
                flag = 1;
                showComment();
        }
}

var reply_id = 0;
oddity.commentor.reply = function(id) {
        if (document.getElementById("username").value == '') {
                alert("Please login first!");
        }else {
                flag = 0;
                showComment();
                var content = document.getElementById("cmnttxtarea");
                content.focus();
                content.value = "Reply to " + document.getElementById("reply_"+id).innerHTML + ":\r\n";
                reply_id = id;
                
                
                
        }
}

oddity.commentor.quote = function(id) {
        if (document.getElementById("username").value == '') {
                alert("Please login first!");
        }else {
                flag = 0;
                showComment();
                document.getElementById("cmnttxtarea").focus();
                var quote = document.getElementById("quote_"+id);
                document.getElementById("cmnttxtarea").value = document.getElementById("cmnttxtarea").value + oddity.commentor.trim(quote.innerHTML);
                
                
        }
}

oddity.commentor.check_reply = function() {
        if (document.getElementById("cmnttxtarea").value.indexOf("Reply to") == -1) reply_id = 0;
}

oddity.commentor.trim = function(s) {
        return s.replace(/(^\s*)|(\s*$)/g, "");
}
function URLEncode (clearString) {
        var output = '';
        var x = 0;
        clearString = clearString.toString();
        var regex = /(^[a-zA-Z0-9_.]*)/;
        while (x < clearString.length) {
                var match = regex.exec(clearString.substr(x));
                if (match != null && match.length > 1 && match[1] != '') {
                        output += match[1];
                        x += match[1].length;
                } else {
                        if (clearString[x] == ' ')
                                output += '+';
                        else {
                                var charCode = clearString.charCodeAt(x);
                                var hexVal = charCode.toString(16);
                                output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
                        }
                        x++;
                }
        }
        return output;
}
function URLEncode (clearString) {
        var output = '';
        var x = 0;
        clearString = clearString.toString();
        var regex = /(^[a-zA-Z0-9_.]*)/;
        while (x < clearString.length) {
                var match = regex.exec(clearString.substr(x));
                if (match != null && match.length > 1 && match[1] != '') {
                        output += match[1];
                        x += match[1].length;
                } else {
                        if (clearString[x] == ' ')
                                output += '+';
                        else {
                                var charCode = clearString.charCodeAt(x);
                                var hexVal = charCode.toString(16);
                                output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
                        }
                        x++;
                }
        }
        return output;
}

