var strSpecialDelim = "_cc!@@@!cc_";                // this needs to match $SpecialDelims in ccWebContentManger
var strSpecialChars = "@ccAMP@";

function ProcessContentMangerResponse()
{
	if(oHTTP.readyState == 4)			// once the response is received
	{
	    if(oHTTP.responseText)
	        alert(oHTTP.responseText); //alert("AJAX RESPONSE (ccWebContentGenerator.js): " + oHTTP.responseText);
	    if((oHTTP.responseText) && ((oHTTP.responseText).toUpperCase().indexOf("ERROR")>=0))
	    {
	        //alert ("Error Condition, will not refresh!");
	    }
	    else
	    {
	        RefreshPage();                                      // defined in AJAX_Base.js
	    }        
	}
}

function ProcessContentMangerResponsePOST()
{
	if(oHTTPost.readyState == 4 && oHTTPost.status == 200)			// once the response is received
	{
	    alert(oHTTPost.responseText); //alert("AJAX RESPONSE (ccWebContentGenerator.js): " + oHTTPost.responseText);
	    if((oHTTPost.responseText).toUpperCase().indexOf("ERROR")>=0)// || (oHTTP.responseText).indexOf("error")>=0)
	    {
	        //alert ("Error Condition, will not refresh!");
	    }
	    else
	        RefreshPage();                                      // defined in AJAX_Base.js
        //alert(oFormElements.length);
	}
}

function ProcessListAction(strAction, strQuerySuffix)
{
    strSelectedListItem = GetSelectValue("section_list");               // get the currently selected item
    if (strAction == "add")                                             // if add, reset the form, and focus on name
    {
        ResetForm("content_edit_form", "");                             // defined in AJAX_Base.js
        setMCEText("");                                                 // defined in ccWEbContentGenerator.cs
        return;
    }
    else if (strAction == "delete")
    {
        if(strSelectedListItem == "")
        {
            alert("No Item Selected!");
            return;
        }
        if(!confirm("Are you sure you want to delete selected item?"))
            return;
        strQuerySuffix = "?action="+strAction+strQuerySuffix+"&";        
        IssueAJAXQuery(strQuerySuffix, ProcessContentMangerResponse);                           
        return;
    }
    else if (strAction == "save")                                       // this is the actual form save/submit
    {
        // if no items selected in the selector, it's an add otherwise it's an edit       
        if(strSelectedListItem == "")
            strAction="add";                                            // add a new item
        else                
            strAction="edit";                                           // edit an existing item
        var strQueryPrefix = "?action=" + strAction + strQuerySuffix + "&";
        BuildPostQuery("content_edit_form", strQueryPrefix, true);      // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
    else if(strAction=="moveup")
    {   
        moveOptionUp(document.getElementById("section_list"));
        document.getElementById("reorder_btn").disabled=false;
        return;
    }
    else if (strAction == "movedown")
    {
        moveOptionDown(document.getElementById("section_list"));
        document.getElementById("reorder_btn").disabled=false;
        return;
    }
    else if (strAction == "reorder")
    {   // build a query specifically for reordering        
        var strQueryPrefix = "?action=" + strAction + strQuerySuffix + "&index_order="+getAllOptionValues(document.getElementById("section_list"), ",");
        IssueAJAXQuery(strQueryPrefix, ProcessContentMangerResponse);
        return;
    }
}

function BuildPostQuery(strFormID, strQueryPrefix, bPost)
{
    var strURL = strQueryPrefix;
    var strQuery = "";
    var oSubmitForm = document.getElementById(strFormID);                   // get the form object
    if(oSubmitForm==null)
    {
        alert("Cannot find submited form!");
        return;
    }
    var oFormElements = oSubmitForm.elements;
    for(var i = 0; i < oFormElements.length; i++)
    {
        if(oFormElements[i].type=="textarea")
        {
            if(oFormElements[i].name.indexOf("NOTMCE")>=0)
                strQuery += oFormElements[i].id + "=" + oFormElements[i].value.replace(/&/g, strSpecialChars) + "&";
            else                // get the TinyMCE textareas            
                strQuery += oFormElements[i].id + "=" + getMCEText(oFormElements[i].id).replace(/&/g, strSpecialChars) + "&";            
        }
        else if(oFormElements[i].id.indexOf("mce_")>=0)                     // ignore TinyMCE forms
            continue;
        else if(oFormElements[i].type=="button" || oFormElements[i].type=="submit" || oFormElements[i].type=="reset")
            continue;
        else if (oFormElements[i].type=="select-one")
        {
            //alert("Foudn Select element...! " + oFormElements[i].id);
            if(oFormElements[i].id != "section_list")
                strQuery += oFormElements[i].id + "=" + oFormElements[i].value + "&";
            // if multi-select, get all items
            //{
            //    strQuery += oFormElements[i].name + "=" + getAllOptionValues(oFormElements[i], ",") + "&";
            //}            
        }
        else if (oFormElements[i].type=="checkbox")
        {            
            if(oFormElements[i].checked)
                strQuery += oFormElements[i].id + "=true&";
            else
                strQuery += oFormElements[i].id + "=false&";
        }         
        else
        {
            //alert(oFormElements[i].id + "=" + oFormElements[i].value + " of type " + oFormElements[i].type);
            strQuery += oFormElements[i].id + "=" + oFormElements[i].value.replace(/&/g, strSpecialChars) + "&";
        }        
    }
    if(bPost)
    {
        //alert("ccWebContentManager.js::BuildPostQuery - " + strQuery);
        IssueAJAXPost(strURL, strQuery, ProcessContentMangerResponsePOST);
    }
    else
    {
        alert('BuildPostQuery() in ccWebContentManager.js');
        IssueAJAXQuery(strURL+strQuery, ProcessContentMangerResponse);         // the query response is responsible for refresh if needed
    }
}


function ProcessTextEdit(strSrcID, strSectionName)
{
    var srcElement = document.getElementById(strSrcID);
    if(srcElement==null)
    {
        alert("Element " + strSrcID + " is not found!");
        return;
    }
    var strURL = "include/ccWebContentProcessor.php?action=edit&type=markup&target_file="+strSectionName;
    var strQuery = "content_markup="+srcElement.value;
    IssueAJAXPost(strURL, strQuery, ProcessContentMangerResponsePOST);
}

function openPopup(popUPurl, width, height) 
{
	var winOptions = "titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=" + height + ",width=" + width;		
	windowReference = window.open(popUPurl, "_blank", winOptions);	//,height=360,width=370');	
	if (!windowReference.opener)
		windowReference.opener = self;	
}