var jcFadeTimer; /*ahndler to the fade timer*/

function jc_showBusyImage(){
    clearTimeout(jcFadeTimer);
    opacity('jc_busyDiv', 0, 100,0);
    xajax.$("jc_busyDiv").innerHTML = "";
    var newImg = document.createElement('img');
    newImg.setAttribute('src', jc_livesite_busyImg);
    newImg.setAttribute('id',"jc_busyImg");
    xajax.$("jc_busyDiv").appendChild(newImg);
}

function jc_removeBusyImage(){
    var t = xajax.$("jc_busyImg");
    if(t != null){
        t.parentNode.removeChild(t);
    }
}

function replaceText(text, textarea){
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}

function addComments(){
    jc_showBusyImage();
    xajax_jcxAddComment(xajax.getFormValues('jc_commentForm'));
    jc_disableForm(); 
    
    if(xajax.$("jc_name"))    createCookie('jc_name', xajax.$("jc_name").value, 14);
    if(xajax.$("jc_email"))   createCookie('jc_email', xajax.$("jc_email").value, 14);
    if(xajax.$("jc_website")) createCookie('jc_website', xajax.$("jc_website").value, 14);
    
    var rememberMe = true;
    if(xajax.$("jc_rememberInfo"))    rememberMe = xajax.$("jc_rememberInfo").checked;
    if(!rememberMe){
        eraseCookie("jc_name");
        eraseCookie("jc_email");
        eraseCookie("jc_website");
    }
    return;
}

function display_notification(divName, theMessage){
    var message = "<div class=\"infolevel1\"></div>";
    message += "<div class=\"infolevel2\"></div>";
    message += "<div class=\"infolevel3\">" +theMessage+ "</div>";
    message += "<div class=\"infolevel2\"></div>";
    message += "<div class=\"infolevel1\"></div>";
          
    var d = xajax.$(divName);
    if(d != null){
        d.innerHTML = message;
    }
}
    
function jc_update(){
    if(xajax.$("jc_contentid")!= null & xajax.$("jc_option") != null & xajax.$("jc_numComment")){
        xajax_jcxUpdateComment(xajax.$("jc_contentid").value, xajax.$("jc_option").value, xajax.$("jc_numComment").innerHTML);
    }
}

function jc_unpublishPost(postId, opt, id){
    xajax_jcxUnpublish(postId, opt);
    if(xajax.$(postId) != "undefined"){
        xajax.$(postId).style.border = "medium solid #FF0000";
        opacity(postId,  100, 50, 200);
    } 
}

function jc_commentAreaToggle(){
    if(typeof(jc_commentArea) != "undefined"){
        jc_commentArea.toggle();
        var img = xajax.$("toggleAreaImg");
        if(img != null){
            var imgSrc = img.src;
            if (jc_commentArea.el.offsetHeight > 0){
                imgSrc = imgSrc.replace("min.gif", "max.gif");
            } else{
                imgSrc = imgSrc.replace("max.gif", "min.gif");
            }
            img.src = imgSrc;
        }
    }
}

function jc_commentFormToggle(){
    if(typeof(jc_commentFormArea) != "undefined"){
        jc_commentFormArea.toggle();
        var img = xajax.$("toggleFormImg");
        if(img != null){
            var imgSrc = img.src;
            if (jc_commentFormArea.el.offsetHeight > 0){
                imgSrc = imgSrc.replace("min.gif", "max.gif");
            } else{
                imgSrc = imgSrc.replace("max.gif", "min.gif");
            }
            img.src = imgSrc;
        }
    }
}

function jc_disableForm(){
    if(xajax.$("jc_name")) xajax.$("jc_name").disabled = true;
    if(xajax.$("jc_email")) xajax.$("jc_email").disabled = true; 
    if(xajax.$("jc_website")) xajax.$("jc_website").disabled = true; 
    if(xajax.$("jc_title")) xajax.$("jc_title").disabled = true; 
    if(xajax.$("jc_comment")) xajax.$("jc_comment").disabled = true; 
    if(xajax.$("jc_password")) xajax.$("jc_password").disabled = true; 
    if(xajax.$("jc_submit")) xajax.$("jc_submit").disabled = true; 
}

function jc_enableForm(){
    if(xajax.$("jc_name")) xajax.$("jc_name").disabled = false;
    if(xajax.$("jc_email")) xajax.$("jc_email").disabled = false; 
    if(xajax.$("jc_website")) xajax.$("jc_website").disabled = false; 
    if(xajax.$("jc_title")) xajax.$("jc_title").disabled = false; 
    if(xajax.$("jc_comment")) xajax.$("jc_comment").disabled = false; 
    if(xajax.$("jc_password")) xajax.$("jc_password").disabled = false; 
    if(xajax.$("jc_submit")) xajax.$("jc_submit").disabled = false; 
}

function jc_insertNewEntry(entry, id){
    var comment_div = xajax.$("jc_commentsDiv");
    var newCmt = document.createElement('div');
    newCmt.innerHTML = entry;
    changeStyleOpac(0, newCmt.style);
    if(jc_orderBy == "0"){
        comment_div.insertBefore(newCmt,comment_div.firstChild);
    } else {
        comment_div.appendChild(newCmt);
    }
    newCmt.setAttribute("id", id);
    opacity(id, 0, 100, 500); 
}

function jc_fadeMessage(){
    clearTimeout(jcFadeTimer);
    jcFadeTimer = setTimeout("opacity('jc_busyDiv', 100, 0,1000)", 3000);
}

////////////////////////////////////////////////////////////////////////////////
function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++){
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = xajax.$(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function changeStyleOpac(opacity, object) {
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

function eraseCookie(name){
  createCookie(name, "", -1);
}

////////////////////////////////////////////////////////////////////////////////
function jc_init(){
    if((typeof jc_update_period != "undefined")  && jc_autoUpdate){
        setTimeout("jc_update()", jc_update_period);
    }
}

function jc_addText(text, textarea){
	 var comment = xajax.$(textarea).value;
	 comment = comment + text;
	 xajax.$(textarea).value = comment;
}

// Surrounds the selected text with text1 and text2.
function encloseText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

setTimeout("jc_init()", 20000);
