/*
 * clearyQuery :: Clearycomm AJAX/PHP Bridge
 * Author: Clearycomm LLC
 * Technologies Used: PHP, AJAX, JavaScript
 * Copyright (c) 2009 Clearycomm LLC
 *
 * Date: 2009-05-19 17:34:21 -0500
 * Revision: 0001
 * File: clearyQuery.js
 */
var clx_xml;
var xpanel = new String ();

function cleary(str,panel){
    clx_xml=gatherObject();
    xpanel = panel;
	
    if (clx_xml==null){
        alert ("HTTP services are not supported with your current browser!");
        return;
    }

    var url="_core/libs/clearyQuery/clearyQuery.php";
    url=url+"?query="+str;
    clx_xml.onreadystatechange=stateChanged;
    clx_xml.open("GET",url,true);
    clx_xml.send(null);
}

function searchSiteCore(str,panel){
    clx_xml=gatherObject();
    xpanel = panel;
	
    if (clx_xml==null){
        alert ("HTTP services are not supported with your current browser!");
        return;
    }

    var url="_core/libs/clearyQuery/clearySearchSiteCore.php";
    url=url+"?query="+str;
    clx_xml.onreadystatechange=stateChanged;
    clx_xml.open("GET",url,true);
    clx_xml.send(null);
}

function savePage(str,contents,title,panel){
    clx_xml = gatherObject();
    xpanel = panel;
	
    if (clx_xml==null){
        alert ("HTTP services are not supported with your current browser!");
        return;
    }

    var url="../_core/libs/clearyQuery/clearyQuery.php";
    var params = "query=~U_";
    params += "&page_title="+title;
    params += "&page_contents="+encodeURIComponent(contents);
    params += "&page_id="+str;

    clx_xml.onreadystatechange=stateChanged;
    clx_xml.open("POST",url,true);
    clx_xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    clx_xml.setRequestHeader("Content-length", params.length);
    clx_xml.setRequestHeader("Connection", "close");

    clx_xml.send(params);
}


function stateChanged(){ 
    if (clx_xml.readyState==4 || clx_xml.readyState=="complete"){
        var response = clx_xml.responseText;
        var mode = response.substr(0, 3);
        if(mode=="~U_"){
            response = response.replace(mode, "");
            alert(response);
        }
        if(mode=="~H_"){
            response = response.replace(mode, "");
            window.location = "index.php"+response;
        }
        if(mode=="~L_"){
            response = response.replace(mode, "");
            window.location =response;
        }
        if(mode=="TW_"){
            document.getElementById("tWidget").innerHTML = response;
        }
        if(mode=="CL_"){
            document.getElementById("chatLiveDiv").innerHTML = response;
        }else{
            try {
                //tinyMCE.activeEditor.setContent(response, {format : 'raw'});
                document.getElementById(xpanel).innerHTML = response;
            }catch (e){
			
            }
        }
    }
}

function gatherObject(){
    var clx_xml=null;
    try{
        // Firefox, Opera 8.0+, Safari
        clx_xml=new XMLHttpRequest();
    }
    catch (e){
        //Internet Explorer
        try{
            clx_xml=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            clx_xml=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return clx_xml;
}