var strSpecialDelim = "_cc!@@@!cc_";

function DebugAlertResponse()
{
	if(oHTTP.readyState == 4)			// once the response is received
	{
	    alert(oHTTP.responseText); //alert("AJAX RESPONSE (AJAX_Base.js): " + oHTTP.responseText);
	    RefreshPage();                                      // defined in AJAX_Base.js
        //alert(oFormElements.length);
	}
}

/*********************************************************************************************************
* ProcessAJAXResponse - processes the response of an AJAX query
*   This is a generic processor, it assumes the response string will have form element id names and their 
    values
    formelement_cc!@#$$cc_elementcontent
*********************************************************************************************************/
function ProcessFullResponse()
{
	if(oHTTP.readyState == 4)			// once the response is received
	{
	    //alert("ProcessFullResponse in AJAX_Base.js: " + oHTTP.responseText);
		var oRequestResults = oHTTP.responseText.split(strSpecialDelim);
		//alert(oRequestResults.length);
		for(i = 0; i < oRequestResults.length; i++)                                         // go through each element
		{
		    try
		    {
		        if(oRequestResults[i+1]!=null)
		        {		            
		            //alert("ID="+oRequestResults[i]+" and value is " + oRequestResults[i+1]);
		            var oElement = document.getElementById(oRequestResults[i]);
		            i++;
		            if(oElement==null)
		            {
		                //alert ("Cannot find '" + oRequestResults[i-1] + "'");
		                continue;
		            }		            
		            if(oElement.type == "textarea")
		            {
		                //alert("Content set" + oRequestResults[i]);
		                setMCEText(oRequestResults[i-1], oRequestResults[i]);		                
		            }
		            else if(oElement.type == "checkbox")
		            {
		                //alert("Setting checkbox '" + oRequestResults[i-1] + "' to " +  oRequestResults[i]);
		                if(oRequestResults[i]=="true")
		                    oElement.checked = true;
		                else
		                    oElement.checked = false;
		            }
		            else if(oElement.type == "select-one")
		            {		                
		                var oOptions = oRequestResults[i].split("_cc|cc_");		                
		                var oSelectElement = document.getElementById(oRequestResults[i-1]);
		                removeAllOptions(oSelectElement);                                             // remove all existing options from the list
		                if(oOptions.length>1)
		                {
		                    for(j = 0; j< oOptions.length; j++)                                         // go through each element
		                    {		                        
		                        if(oOptions[j+1])
		                            addOption(oSelectElement,oOptions[j+1],oOptions[j],false);
		                        j++;
		                       // addOption(obj,text,value,selected)
		                    }		                
		                }
		            }
		            else if(oElement.type == "text")		            
		                oElement.value = oRequestResults[i];	    // set the value to the next element		                
		            else if(oElement.type == "hidden")
		                oElement.value = oRequestResults[i];	    // set the value to the next element		            
		            //else
		              //  alert("ProcessFullResponse in AJAX_Base: " + oElement.type);
                }
		    }
		    catch (e)
		    {
		        //alert(e + " " + oRequestResults[i-1]);
		    }
        }
	}
}

/*********************************************************************************************************
* IssueAJAXQuery - issues an AJAX query
*********************************************************************************************************/
function IssueAJAXQuery(strQuery, strCallbackFunc)
{
	oHTTP = getHTTPObject();
	if(strCallbackFunc!=null)
	    oHTTP.onreadystatechange = strCallbackFunc;					
	else
	    oHTTP.onreadystatechange = ProcessFullResponse;
	//alert("IssueAJAXQuery (AJAX_Base.js): " + strQuery);
	oHTTP.open("GET", strQuery, true);									// prepare the GET query
	oHTTP.send(null);													// send the query
}

/*********************************************************************************************************
* IssueAJAXQuery - issues an AJAX query via a form post
*********************************************************************************************************/
function IssueAJAXPost(strURL, strQuery, strCallbackFunc)
{    
	//oHTTP = getHTTPObject();
	oHTTPost = getHTTPObject();
	var url = strURL; //"../ccWebContentProcessor.php?action=edit&type=METAProdSection&target_file=1";
    //var params = "lorem=ipsum&name=binny";
    oHTTPost.open("POST", url, true);

    //Send the proper header information along with the request
    oHTTPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oHTTPost.setRequestHeader("Content-length", strQuery.length);
    oHTTPost.setRequestHeader("Connection", "close");

    oHTTPost.onreadystatechange = strCallbackFunc;    
    oHTTPost.send(strQuery);    
}




/*********************************************************************************************************
* RefreshPage - refreshes page: depending on the method, it either does a light refresh (form element
*                                elements remain in tact or a hard refresh (form elements are cleared)
*********************************************************************************************************/
function RefreshPage()
{
    // window.location.reload();                               // light re-fresh
    // history.go(0);                                          // very light re-fresh
    // alert("refreshing");
    var strCurrentLoc = window.location.href;
    strCurrentLoc = strCurrentLoc.replace("#", "");
    window.location.href = strCurrentLoc;                      // hard re-fresh
}


// TinyMCE helper functions below
function getMCEText(strID)
{
    var content= tinyMCE.getContent();
    return content;//alert(content);
}

function setMCEText(strID, strText)
{
    //tinyMCE.activeEditor.setContent('<h1>This is Ronen Site</h1>');
    try { 
        //alert("herllo");
        //oEditor = tinyMCE.getInstanceById(strID);
        tinyMCE.setContent(strText);                
    }
    catch (e) { 
        alert (tinyMCE);
        alert ("Unable to finfd tinyMCE element '" + strID + "' - " + e);
    }
}

function ResetForm(strFormID, strFocusID)
{
	document.getElementById(strFormID).reset();
	if(strFocusID!=null)
	    document.getElementById(strFocusID).focus();
}


/*********************************************************************************************************
* getHTTPObject - instantiates an AJAX HTTP object for subsequent use
*********************************************************************************************************/
function getHTTPObject() 
{
    var xmlhttp;
    // use conditional compilation in the code below
    /*@cc_on
        @if (@_jscript_version >= 5)
            try 
            {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) 
            {
                try 
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (E) 
                {
                    xmlhttp = false;
                }
            }
        @else
            xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
    {
        try 
        {
            xmlhttp = new XMLHttpRequest();
        } 
        catch (e) 
        {
          xmlhttp = false;
        }
    }
    return xmlhttp;
}
