var xmlShout = createXmlHttpRequestObject();

function shoutFocus() {
    //Check to see if the text is message
    if (window.document.forms['shoutbox'].shout_text.value && window.document.forms['shoutbox'].shout_text.value == "Message") {
        //If so clear it
        window.document.forms['shoutbox'].shout_text.value = "";
    }
}

function nameFocus() {
    if (window.document.forms['shoutbox'].shout_name.value && window.document.forms['shoutbox'].shout_name.value == "Name") {
        window.document.forms['shoutbox'].shout_name.value = "";
    }
}

function shoutOut() {
    var text;

    if (!window.document.forms['shoutbox'].shout_text.value || window.document.forms['shoutbox'].shout_text.value == "Message") {
        return;
    }

    text = window.document.forms['shoutbox'].shout_text.value;
    ajaxSend(text);
}

function ajaxSend(text) {
    if(xmlShout) {
        try {
            // don't start another server operation if such an operation
            //   is already in progress
            if (xmlShout.readyState == 4 || xmlShout.readyState == 0) {
                // call the server page to execute the server-side operation
                var url = "modules.php?name=Shoutbox&file=ajax";
                var params = "shout_text=" + text;

                xmlShout.open("POST", url, true);

                //Send the proper header information along with the request
                xmlShout.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                xmlShout.setRequestHeader("Content-length", params.length);
                xmlShout.setRequestHeader("Connection", "close");

                xmlShout.onreadystatechange = function() {
                    if (xmlShout.readyState==4 || xmlShout.readyState=="complete") {
                        readInfo();
                    }
                }
                xmlShout.send(params);
            } else {
                return;
            }
        } catch(e) {
            return ;
        }
    }
}

function readInfo() {
    // retrieve the server's response
    var response = xmlShout.responseText;
    var response_xml = xmlShout.responseXML;

    //If there is an error
    if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 ) {
        alert('Sever Error ' + response);
        return ;
    }

    if (response.length > 2) {
        try {
            //Get the shoutbox XML
            var shoutbox = response_xml.getElementsByTagName("shoutbox");
            //Get the shouts XML
            var shouts = shoutbox[0].getElementsByTagName("shouts")[0].childNodes[0].nodeValue;
            var shout_shouts = document.getElementById("shout_shouts");
            var shout_error = document.getElementById("shout_error");
            shout_shouts.innerHTML = shouts;
            window.document.forms['shoutbox'].shout_text.value = "";
            window.document.forms['shoutbox'].shout_text.focus();
            shout_error.innerHTML = "";
            var error = shoutbox[0].getElementsByTagName("error")[0].childNodes[0].nodeValue;
            if (error || error.length > 2) {
                shout_error.innerHTML = error;
            }
            shout_shouts.scrollTop = 0;
        } catch(e) {
            return ;
        }
    }
}

/*var SBSet = 0;
var SBiedom = document.all || document.getElementById;
var SBcross_marquee;
var SBactualheight;
var SBheight = 75;
var SBspeed = 2;

function SBscroll(){

    if(SBiedom){
        if(parseInt(SBcross_marquee.style.top) > (SBactualheight*(-1)+2)){
            SBcross_marquee.style.top = parseInt(SBcross_marquee.style.top)-SBspeed+"px";
        }else{
            SBcross_marquee.style.top = parseInt(SBheight)+2+"px";
        }
    }
    else{
        if(SBns_marquee.top > (SBactualheight*(-1)+2)){
            SBns_marquee.top -= SBspeed;
        }else{
            SBns_marquee.top = parseInt(SBheight)+2;
        }
    }
    SBSet = 1;
}

function allonloads() {
    SBcross_marquee = document.getElementById("shout_shouts");

     if(SBiedom){
        //SBcross_marquee=document.getElementById? document.getElementById("SBiemarquee") : document.all.SBiemarquee;
        SBcross_marquee.style.top=parseInt(SBheight)+8+"px";
        SBactualheight=SBcross_marquee.offsetHeight;
    } else {
       // SBns_marquee=document.SBns_marquee.document.SBns_marquee2;
        SBns_marquee.top=parseInt(SBheight)+8;
        SBactualheight=SBns_marquee.document.height;
    }
    this.setInterval("SBscroll()",50);
}

if(window.addEventListener)
    window.addEventListener("load",allonloads,false);
else if (window.attachEvent)
    window.attachEvent("onload", allonloads)
else if (document.getElementById)
    womAdd('allonloads()');*/