var JOSC_http = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
var JOSC_operaBrowser = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var JOSC_rsearchphrase_selection="any";
/* in case of modify */
var JOSC_userName = ''; 
var JOSC_userEmail = ''; 
var JOSC_userWebsite = '';
var JOSC_userNotify = '';
/* ***************** */
var JOSC_XmlErrorAlert = false; /* will be redefined by setting */
var JOSC_AjaxDebug = false; /* will be redefined by setting */
var JOSC_AjaxDebugLevel = 2; /* will be redefined by setting */


var JOSC_clientPC = navigator.userAgent.toLowerCase();
var JOSC_clientVer = parseInt(navigator.appVersion);

var JOSC_is_ie = ((JOSC_clientPC.indexOf("msie") != -1) && (JOSC_clientPC.indexOf("opera") == -1));
var JOSC_is_nav = ((JOSC_clientPC.indexOf('mozilla')!=-1) && (JOSC_clientPC.indexOf('spoofer')==-1)
                && (JOSC_clientPC.indexOf('compatible') == -1) && (JOSC_clientPC.indexOf('opera')==-1)
                && (JOSC_clientPC.indexOf('webtv')==-1) && (JOSC_clientPC.indexOf('hotjava')==-1));
var JOSC_is_moz = 0;

var JOSC_is_win = ((JOSC_clientPC.indexOf("win")!=-1) || (JOSC_clientPC.indexOf("16bit") != -1));
var JOSC_is_mac = (JOSC_clientPC.indexOf("mac")!=-1);

var JOSC_scrollTopPos = 0;
var JOSC_scrollLeftPos = 0;

function JOSC_insertAdjacentElement( object, where, parsedNode ) {
        if (!object.JOSCinsertAdjacentElement)
				object.insertAdjacentElement(where, parsedNode);
        else
                object.JOSCinsertAdjacentElement(where, parsedNode);
}

function JOSC_insertAdjacentHTML( object, where, htmlStr ) {
        if (!object.JOSCinsertAdjacentHTML)
				object.insertAdjacentHTML(where, htmlStr);
        else
                object.JOSCinsertAdjacentHTML(where, htmlStr);
}

if (typeof HTMLElement != "undefined" && !
    HTMLElement.prototype.JOSCinsertAdjacentElement) {
    HTMLElement.prototype.JOSCinsertAdjacentElement = function
    (where, parsedNode)
    {
        switch (where) {
            case 'beforeBegin':
                this.parentNode.insertBefore(parsedNode, this)
                break;
            case 'afterBegin':
                this.insertBefore(parsedNode, this.firstChild);
                break;
            case 'beforeEnd':
                this.appendChild(parsedNode);
                break;
            case 'afterEnd':
                if (this.nextSibling)
                    this.parentNode.insertBefore(parsedNode, this.nextSibling);
                else this.parentNode.appendChild(parsedNode);
                break;
        }
    }

    HTMLElement.prototype.JOSCinsertAdjacentHTML = function
    (where, htmlStr)
    {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML = r.createContextualFragment(htmlStr);
        this.JOSCinsertAdjacentElement(where, parsedHTML)
    }

/*    HTMLElement.prototype.JOSCinsertAdjacentText = function
    (where, txtStr)
    {
        var parsedText = document.createTextNode(txtStr)
        this.JOSCinsertAdjacentElement(where, parsedText)
    }
    */
}

/***************************
 * F U N C T I O N S
 ***************************/
 
function JOSC_HTTPParam()
{
}

JOSC_HTTPParam.prototype.create = function(command, id)
{
    this.result = 'option=com_comment';
    this.insert('no_html', 1);
    this.insert('command', command);
    this.insert('comment_id', id);
    return this.result;
}

JOSC_HTTPParam.prototype.insert = function(name, value)
{
    this.result += '&' + name + '=' + value;
    return this.result;
}

JOSC_HTTPParam.prototype.encode = function(name, value)
{
    return this.insert(name, encodeURIComponent(value));
}

function JOSC_BusyImage()
{
}

JOSC_BusyImage.prototype.create = function()
{
    //var form = document.joomlacommentform;
    //var image = document.createElement('img');
    //image.setAttribute('src', JOSC_liveSite + '/images/busy.gif');
    //image.setAttribute('id', "busyImage");
    //var element = document.getElementById('busy');
    //if (!element.innerHTML) element.appendChild(image);
	window.addEvent('domready', function(){	
		$('busy').setHTML('&nbsp;');	
		$('busy').addClass('ajax-loading');	
	});	 
}

JOSC_BusyImage.prototype.destroy = function()
{
    //var image = document.getElementById("busyImage");
    //image.parentNode.removeChild(image);
	window.addEvent('domready', function(){	
		$('busy').removeClass('ajax-loading');	
		$('busy').setHTML('<strong>Posted! Thank you.</strong>');	
	});	
}

var JOSC_busyImage = new JOSC_BusyImage();

function JOSC_ajaxSend(data, onReadyStateChange)
{
    document.joomlacommentform.bsend.disabled = true;
    JOSC_busyImage.create();
    /* 
     * comment:  URL with only index2.php cause error when mode SEF AND Opera navigator (strange !!!!return with a strange tab in xml...)
     */
    var URL = JOSC_ConfigLiveSite+'/index2.php';
    JOSC_http.open("POST", URL , true);
    //JOSC_http.onreadystatechange = onReadyStateChange;
	 JOSC_http.onreadystatechange = function() 
				{
					if (JOSC_http.readyState==4 || JOSC_http.readyState=="complete")
					{ 
						if(JOSC_http.responseText == 'Captcha Failed') {
							window.addEvent('domready', function(){	
								$('busy').removeClass('ajax-loading');	
								$('busy').setHTML('<strong>Failed! Wrong security code.</strong>');	
								document.joomlacommentform.bsend.disabled = false;
							});	
						} else {
							onReadyStateChange();						
						}
					} 
				} 
    JOSC_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    if (JOSC_AjaxDebug) alert('###AJAXSEND:\n##URL=' + URL + ' ?' + data  + '\n##onReadyStateChange=' + onReadyStateChange);
    JOSC_http.send(data);
}

function JOSC_ajaxReady()
{
    if (JOSC_http.readyState == 4) {
    	/* received */
        JOSC_busyImage.destroy();
        document.joomlacommentform.bsend.disabled = false;
        if (JOSC_http.status == 200) {
        	/* response is ok */
          	if (JOSC_AjaxDebug) alert('AJAXREADY: OK !' );
            return true;
        } else {
          	if (JOSC_AjaxDebug) alert('AJAXREADY: KO ! Status=' + JOSC_http.status );
           	document.joomlacommentform.tcomment.value = 'Ajax request failed.';          	
        }	
    }
    return false;
}

function JOSC_goToAnchor(name)
{
    clearTimeout(self.timer);
    action = function()
    {
		var url = window.location.toString();
		var index = url.indexOf('#');
		if (index == -1) { window.location = url + '#' + name; }
		else { window.location = url.substring(0, index) + '#' + name; }
        if (JOSC_operaBrowser) window.location = '##';
    }
    if (JOSC_operaBrowser) self.timer = setTimeout(action, 50);
    else action();
}

function JOSC_getXmlResponse(withalert) {
/* return DOM (W3C) if no parsing xml error else null  (alert will show a javascript alert) */
  if (JOSC_http.responseXML && JOSC_http.responseXML.parseError &&(JOSC_http.responseXML.parseError.errorCode !=0)) {
    error = JOSC_getXmlError(withalert);
    return null; 
  } else {
  	if (JOSC_AjaxDebug) alert('###GETXMLRESPONSE:\n' + JOSC_http.responseText );
/*    if (JOSC_operaBrowser && JOSC_AjaxDebug && JOSC_AjaxDebugLevel>1) {
         txt = '';
         for (prop in JOSC_http.responseXML)
         {
             txt = txt + '\n' + prop + '=' + JOSC_http.responseXML[prop];
         }
         alert('JOSC_getXmlResponse:http.responseXML='+txt);
    }*/
    return JOSC_http.responseXML;
  }
}

function JOSC_getXmlError(withalert) {
  if (JOSC_http.responseXML.parseError.errorCode !=0 ) {
     line = JOSC_http.responseXML.parseError.line;
     pos = JOSC_http.responseXML.parseError.linepos;
     error = JOSC_http.responseXML.parseError.reason;
     error = error + "Contact the support ! and send the following informations:\n error is line " + line + " position " + pos;
	 error = error + " >>" + JOSC_http.responseXML.parseError.srcText.substring(pos);
         error = error + "\nGLOBAL:" + JOSC_http.responseText;
     if (withalert)
       alert(error);
     return error;
   } else {
     return "";
   }
}

/*
 * Form type function
 */
function JOSC_modifyForm(formTitle, buttonValue, onClick)
{
    document.getElementById('CommentFormTitle').innerHTML = formTitle;
    button = document.joomlacommentform.bsend;
    button.value = buttonValue;
    button.onclick = onClick;
}

function JOSC_xmlValue(xmlDocument, tagName)
{
    try {
        var result = xmlDocument.getElementsByTagName(tagName).item(0).firstChild.data;
    }
    catch(e) {
        var result = '';
    }
    return result;
}

function JOSC_removePost(post)
{
    document.getElementById('Comments').removeChild(post);
}

/********************* 
 * ajax call functions
 */
function JOSC_deleteComment(id)
{
    if (window.confirm(_JOOMLACOMMENT_MSG_DELETE)) {
        var data = new JOSC_HTTPParam().create('ajax_delete', id);
        JOSC_ajaxSend(data, function()
            {
                if (JOSC_ajaxReady()) {
                    if (JOSC_http.responseText != '') alert(JOSC_http.responseText);
                    else JOSC_removePost(document.getElementById('post' + id));
                }
            }
            );
    }
}

function JOSC_deleteAll(id)
{
	if (window.confirm(_JOOMLACOMMENT_MSG_DELETEALL)) {
        var form = document.joomlacommentform;
		var param = new JOSC_HTTPParam();
		param.create('ajax_delete_all', id);
        JOSC_ajaxSend(param.insert('content_id',form.content_id.value), function()
            {
                if (JOSC_ajaxReady()) {
                    if (JOSC_http.responseText != '') alert(JOSC_http.responseText);
                    else {
                    	JOSC_addNew();
                    	document.getElementById('Comments').innerHTML='';
					}
                }
            }
            );
    }
}


function JOSC_editComment(id)
{
    JOSC_modifyForm(_JOOMLACOMMENT_EDITCOMMENT, _JOOMLACOMMENT_EDIT,
        function(event)
        {
            JOSC_editPost(id, -1);}
        );
    JOSC_goToAnchor('CommentForm');
    var data = new JOSC_HTTPParam().create('ajax_modify', id);
    JOSC_ajaxSend(data, JOSC_editResponse);
}

function JOSC_quote(id)
{
    var data = new JOSC_HTTPParam().create('ajax_quote', id);
    JOSC_goToAnchor('CommentForm');
    JOSC_ajaxSend(data, JOSC_quoteResponse);
}

function JOSC_voting(id, yes_no)
{
    var data = new JOSC_HTTPParam().create('ajax_voting_' + yes_no, id);
    JOSC_ajaxSend(data, JOSC_votingResponse);
}

function JOSC_searchForm()
{
	JOSC_removeSearchResults();
	var form = document.joomlacommentsearch;
    if (form) {
        form.parentNode.removeChild(form);
        if (!JOSC_operaBrowser) document.joomlacommentsearch = null;
    } else {
        JOSC_ajaxSend(new JOSC_HTTPParam().create('ajax_insert_search', 0), JOSC_searchFormResponse);
    }
}

function JOSC_search()
{
	JOSC_removeSearchResults();
	var keyword = document.joomlacommentsearch.tsearch.value;
	if (keyword=='') return 0;
	param = new JOSC_HTTPParam();
	param.create('ajax_search', 0);
	param.encode('search_keyword', keyword)
	JOSC_ajaxSend(param.insert('search_phrase',JOSC_rsearchphrase_selection), JOSC_searchResponse);
}

function editPost(id, parentid) {
	/* for backward compatibility with templates */
	return JOSC_editPost(id, parentid);
}

function JOSC_editPost(id, parentid)
{
	var form = document.joomlacommentform;
    if (form.tcomment.value == '') 
    {
        alert(_JOOMLACOMMENT_FORMVALIDATE);
        return 0;
    }
    if  ( document.getElementsByName('tnotify')[0]  && document.getElementsByName('temail')[0] )
    {   if ( form.tnotify.selectedIndex && form.temail.value == '') {
            alert(_JOOMLACOMMENT_FORMVALIDATE_EMAIL);
            return 0;
        }
    }
    if (JOSC_captchaEnabled && form.security_try.value == '')
    {
        alert(_JOOMLACOMMENT_FORMVALIDATE_CAPTCHA);
        return 0;
    }
  
    
    if (JOSC_ajaxEnabled) 
    {
        var param = new JOSC_HTTPParam();
        param.create(id == -1 ? 'ajax_insert' : 'ajax_edit', id);
        param.insert('content_id', form.content_id.value);
        if (JOSC_captchaEnabled) 
        {
            param.insert('security_try', form.security_try.value);
            param.insert('security_refid', form.security_refid.value);
        }
        if (parentid != -1) param.insert('parent_id', parentid);
        param.encode('tname', form.tname.value);
	    /* optional */
        if (document.getElementsByName('tnotify')[0])  { if (form.tnotify.selectedIndex) param.encode('tnotify', '1'); else param.encode('tnotify', '0'); };
        if (document.getElementsByName('temail')[0])    param.encode('temail', form.temail.value);
        if (document.getElementsByName('twebsite')[0])  param.encode('twebsite', form.twebsite.value);
        /************/
        param.encode('ttitle', form.ttitle.value);
		JOSC_ajaxSend(param.encode('tcomment', form.tcomment.value), CustomHandler);
    } 
    else 
    {
        form.action = './index.php';
        form.submit();
    }
}
/*
 * END of ajax call functions
 */
 
/********************
 * response functions
 */
function JOSC_editResponse()
{
    if (JOSC_ajaxReady()) {
        if (JOSC_http.responseText.indexOf('invalid') == -1) {
            var form = document.joomlacommentform;
            var xmlDocument = JOSC_getXmlResponse(JOSC_XmlErrorAlert);; /*JOSC_http.responseXML;*/
            if (xmlDocument) {

    	        JOSC_userName = form.tname.value;
    	        form.tname.value = JOSC_xmlValue(xmlDocument, 'name');

          		form.ttitle.value = JOSC_xmlValue(xmlDocument, 'title');
          	  	form.tcomment.value = JOSC_xmlValue(xmlDocument, 'comment');

           		/* optional values of the templates ! */
            	if (document.getElementsByName('tnotify')[0]) {
            	   JOSC_userNotify = form.tnotify.selectedIndex; 
            	   form.tnotify.selectedIndex = new Boolean(JOSC_xmlValue(xmlDocument, 'notify')*1);
            	}
            	if (document.getElementsByName('temail')[0]) {
            	  JOSC_userEmail = form.temail.value; 
            	  form.temail.value = JOSC_xmlValue(xmlDocument, 'email');
            	}
            	if (document.getElementsByName('twebsite')[0]) {
            		JOSC_userWebsite = form.twebsite.value;
            	    form.twebsite.value = JOSC_xmlValue(xmlDocument, 'website');
            	}
            	/* ********************** */
            } else {
            	form.tcomment.value = 'failed to retrieve datas';
            }
        }
    }
}

function JOSC_quoteResponse()
{
    if (JOSC_ajaxReady()) {
        if (JOSC_http.responseText.indexOf('invalid') == -1) {
            var form = document.joomlacommentform;
            var xmlDocument = JOSC_getXmlResponse(true);
            if (xmlDocument) {
	            name = JOSC_xmlValue(xmlDocument, 'name');
    	        if (name == '') name = _JOOMLACOMMENT_ANONYMOUS;
        	    if (form.ttitle.value == '') form.ttitle.value = 're: ' +
            	    JOSC_xmlValue(xmlDocument, 'title');
	            form.tcomment.value += '[quote=' + name + ']' +
    	        JOSC_xmlValue(xmlDocument, 'comment') + '[/quote]';
        	} else {
            	form.tcomment.value = 'failed to retrieve datas';
            }
            
        }
    }
}

function JOSC_votingResponse()
{
    if (JOSC_ajaxReady()) {
        if (JOSC_http.responseText.indexOf('invalid') == -1) {
            var form = document.joomlacommentform;
            var xmlDocument = JOSC_getXmlResponse(JOSC_XmlErrorAlert); /*JOSC_http.responseXML;*/
            var id = JOSC_xmlValue(xmlDocument, 'id');
            var yes = JOSC_xmlValue(xmlDocument, 'yes');
            var no = JOSC_xmlValue(xmlDocument, 'no');
            document.getElementById('yes' + id).innerHTML = yes;
            document.getElementById('no' + id).innerHTML = no;
        }
    }
}

function JOSC_editPostResponse()
{
    if (JOSC_ajaxReady()) {
        if (JOSC_http.responseText.indexOf('invalid') == -1) {
            var form = document.joomlacommentform;
            var element = document.getElementById('Comments');
			var xmlDocument = JOSC_getXmlResponse(true); /*JOSC_http.responseXML;*/
			if (!xmlDocument) {
                return 0;
            }
            var id = JOSC_xmlValue(xmlDocument, 'id');
            var after = JOSC_xmlValue(xmlDocument, 'after');
            var captcha = JOSC_xmlValue(xmlDocument, 'captcha');
            if (captcha) {
                JOSC_refreshCaptcha(captcha);
                if (id == 'captcha') {
                	alert(_JOOMLACOMMENT_FORMVALIDATE_CAPTCHA_FAILED);
                 	return 0;
                }
            }
            anchor = 'josc' + id;
            id = 'post' + id;
            var body = JOSC_xmlValue(xmlDocument, 'body');
			var post = document.getElementById(id);
            JOSC_clearInputbox();
            var noerror = JOSC_xmlValue(xmlDocument, 'noerror');
			if (noerror==0) {
				alert(_JOOMLACOMMENT_REQUEST_ERROR);
				form.tcomment.value=JOSC_http.responseText;
				return 0;
            }
            var published = JOSC_xmlValue(xmlDocument, 'published');
			if (published==0) {
				alert(_JOOMLACOMMENT_BEFORE_APPROVAL);
				form.tcomment.value="";
				return 0;
            }
            if (post) {
				var className = JOSC_getPostClass(post);
                var indent = post.style.paddingLeft;
                JOSC_insertAdjacentHTML(post, 'beforeBegin', body);
                JOSC_removePost(post);
                newPost = document.getElementById(id);
                JOSC_setPostClass(newPost, className);
                newPost.style.paddingLeft = indent;
                JOSC_modifyForm(_JOOMLACOMMENT_WRITECOMMENT, _JOOMLACOMMENT_SENDFORM,
                    function(event)
                    {
                        JOSC_editPost(-1, -1);
                    });
                form.tname.value = JOSC_userName;
            	if (document.getElementsByName('temail')[0])   form.temail.value = JOSC_userEmail;
            	if (document.getElementsByName('website')[0])   form.website.value = JOSC_userWebsite;
            } else {
				if (JOSC_sortDownward != 0) JOSC_insertAdjacentHTML(element, 'afterBegin', body);
                else {
                    if (!after || after == -1) JOSC_insertAdjacentHTML(element, 'beforeEnd', body);
                    else JOSC_insertAdjacentHTML(document.getElementById('post' + after), 'afterEnd', body); 
                }
                JOSC_setPostClass(document.getElementById(id),
                    'sectiontableentry' + JOSC_postCSS);
                JOSC_postCSS == 1 ? JOSC_postCSS = 2 : JOSC_postCSS = 1;
            }
            JOSC_goToAnchor(anchor);
        }
	}
}

function JOSC_searchFormResponse()
{
	if (JOSC_ajaxReady()) {
        form = JOSC_http.responseText;
        if (form != '')
            JOSC_insertAdjacentHTML(document.getElementById('CommentMenu'), 'afterEnd', form);
    }
}

function JOSC_searchResponse()
{
    if (JOSC_ajaxReady()) {
        form = JOSC_http.responseText;
		if (form != '')
            JOSC_insertAdjacentHTML(document.joomlacommentsearch, 'afterEnd', form);
    }
}

/*
 * END of response functions
 */

/*
 * Template functions
 */
 function JOSC_goToPost(contentid, id)
{
	var form = document.joomlacommentform;
	if (form.content_id.value==contentid) JOSC_goToAnchor('josc'+id);
	else window.location = 'index.php?option=com_content&task=view&id=' + contentid + '#josc' + id;
	if (JOSC_operaBrowser) window.location = '##';
}

 
function JOSC_reply(id)
{
    var form = document.joomlacommentform;
    var post = document.getElementById('post' + id);
    var postPadding = post.style.paddingLeft.replace('px','')*1;
    form.style.paddingLeft = ( postPadding + 20 ) + 'px';
    JOSC_modifyForm(_JOOMLACOMMENT_WRITECOMMENT, _JOOMLACOMMENT_SENDFORM,
    function(event)
    {
        JOSC_editPost(-1, id);
	});
    JOSC_insertAdjacentElement(post, 'afterEnd', form);
}

function JOSC_insertUBBTag(tag)
{
    JOSC_insertTags('[' + tag + ']', '[/' + tag + ']');
}

function JOSC_fontColor(){
  var color = document.joomlacommentform.menuColor.selectedIndex;
  switch (color){
    case 0: color=''; break;
    case 1: color='aqua'; break;
    case 2: color='black'; break;
    case 3: color='blue'; break;
    case 4: color='fuchsia'; break;
    case 5: color='gray'; break;
    case 6: color='green'; break;
    case 7: color='lime'; break;
    case 8: color='maroon'; break;
    case 9: color='navy'; break;
    case 10: color='olive'; break;
    case 11: color='purple'; break;
    case 12: color='red'; break;
    case 13: color='silver'; break;
    case 14: color='teal'; break;
    case 15: color='white'; break;
    case 16: color='yellow'; break;
  }
  if (color!='') JOSC_insertTags('[color='+color+']','[/color]');
}

function JOSC_fontSize()
{
    var size = document.joomlacommentform.menuSize.selectedIndex;
    switch (size) {
        case 0: size = '';
            break;
        case 1: size = 'x-small';
            break;
        case 2: size = 'small';
            break;
        case 3: size = 'medium';
            break;
        case 4: size = 'large';
            break;
        case 5: size = 'x-large';
            break;
    }
    if (size != '') JOSC_insertTags('[size=' + size + ']', '[/size]');
}

function JOSC_emoticon(icon)
{
  var txtarea = document.joomlacommentform.tcomment;
  JOSC_scrollToCursor(txtarea, 0);
  txtarea.focus();
  JOSC_pasteAtCursor(txtarea, ' ' + icon + ' ');
  JOSC_scrollToCursor(txtarea, 1);
}
/*
 * END of template function
 */
 
/*
 * ALL OTHERS UTILS FUNCTION
 */
function JOSC_insertTags(bbStart, bbEnd) {
  var txtarea = document.joomlacommentform.tcomment;
  JOSC_scrollToCursor(txtarea, 0);
  txtarea.focus();

  if ((JOSC_clientVer >= 4) && JOSC_is_ie && JOSC_is_win) {
    theSelection = document.selection.createRange().text;
    if (theSelection) {
      document.selection.createRange().text = bbStart + theSelection + bbEnd;
      theSelection = '';
      return;
    } else {
      JOSC_pasteAtCursor(txtarea, bbStart + bbEnd);
	}
  } else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + bbStart + s2 + bbEnd + s3;
    txtarea.selectionStart = selStart + (bbStart.length + s2.length + bbEnd.length);
    txtarea.selectionEnd = txtarea.selectionStart;
    JOSC_scrollToCursor(txtarea, 1);
    return;
  } else {
    JOSC_pasteAtCursor(txtarea, bbStart + bbEnd);
	JOSC_scrollToCursor(txtarea, 1);
  }
}

function JOSC_scrollToCursor(txtarea, action) {
  if (JOSC_is_nav) {
    if (action == 0) {
      JOSC_scrollTopPos = txtarea.scrollTop;
      JOSC_scrollLeftPos = txtarea.scrollLeft;
    } else {
      txtarea.scrollTop = JOSC_scrollTopPos;
      txtarea.scrollLeft = JOSC_scrollLeftPos;
    }
  }
}

function JOSC_pasteAtCursor(txtarea, txtvalue) {
  if (document.selection) {
    var sluss;
    txtarea.focus();
    sel = document.selection.createRange();
    sluss = sel.text.length;
    sel.text = txtvalue;
    if (txtvalue.length > 0) {
      sel.moveStart('character', -txtvalue.length + sluss);
    }
  } else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
    var startPos = txtarea.selectionStart;
    var endPos = txtarea.selectionEnd;
    txtarea.value = txtarea.value.substring(0, startPos) + txtvalue + txtarea.value.substring(endPos, txtarea.value.length);
    txtarea.selectionStart = startPos + txtvalue.length;
    txtarea.selectionEnd = startPos + txtvalue.length;
  } else {
    txtarea.value += txtvalue;
  }
}

function JOSC_clearInputbox()
{
    var form = document.joomlacommentform;
    form.ttitle.value = '';
    form.tcomment.value = '';
}

function JOSC_getPostClass(post)
{
    return post.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].className;
}

function JOSC_setPostClass(post, value)
{
    post.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].className = value;
}

function JOSC_refreshCaptcha(captcha)
{
    document.getElementById('captcha').innerHTML = captcha;
    document.joomlacommentform.security_try.value = '';
}

function JOSC_removeSearchResults()
{
    var searchResults = document.getElementById('SearchResults');
    if (searchResults) searchResults.parentNode.removeChild(searchResults);
}

function JOSC_addNew()
{
    var form = document.joomlacommentform;
    form.style.paddingLeft = '0px';
    form.bsend.onclick = function(event)
    {
        JOSC_editPost(-1, -1);
    } ;
    if (form.parentNode.id != 'comment')
        JOSC_insertAdjacentElement(document.getElementById('Comments'), 'afterEnd', form);
    JOSC_goToAnchor('CommentForm');
}

function CustomHandler() {	
	var form = document.joomlacommentform;
	
	if (JOSC_http.readyState == 4) {
		/* received */
		JOSC_busyImage.destroy();
		document.joomlacommentform.bsend.disabled = false;
		if (JOSC_http.status == 200) {
			/* response is ok */
			var JOSC_http2 = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
			
			var param = new JOSC_HTTPParam();
			param.create('updatelisting', -1);			
			
			var url = JOSC_ConfigLiveSite + "/index2.php";
			//url=url+"?pusat_id=" + pusat_id;
			JOSC_http2.onreadystatechange=
				function() 
				{
					if (JOSC_http2.readyState==4 || JOSC_http2.readyState=="complete")
					{ 
						document.getElementById("review").innerHTML = JOSC_http2.responseText;
					} 
				} 
				
			JOSC_http2.open("POST",url,true);
			JOSC_http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			JOSC_http2.send(param.insert('content_id', form.content_id.value));
			
			//alert('AJAXREADY: OK ! ' + JOSC_ConfigLiveSite );
			return true;
		} else {
			//alert('AJAXREADY: KO ! Status=' + JOSC_http.status );           	
		}	
	}
	return false;	
}