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 ProcessProductAction(strAction)
{
    strSelectedProduct = GetSelectValue("section_product_list");
    //alert("Processing " + strSelectedProduct + " in category " + strSelectedDiv);
    oCurrentObject = document.getElementById("section_product_list");
    if(strAction=="moveup")
    {   
        moveOptionUp(oCurrentObject);
        document.getElementById("reorder_btn").disabled=false;
        return;
    }
    else if (strAction == "movedown")
    {
        moveOptionDown(oCurrentObject);
        document.getElementById("reorder_btn").disabled=false;
        return;
    }
    else if (strAction == "add")                                    // if add, reset the form, and focus on name
    {
        ResetForm("product_section_edit_form", "product_name");     // defined in AJAX_Base.js
        setMCEText("");                                             // defined in ccWEbContentGenerator.cs
        return;
    }
    else if (strAction == "delete")
    {
        if(!confirm("Are you sure you want to delete selected item?"))
            return;
        var strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=METAProdSection&target_file=" + GetSelectValue('section_product_list') + "&";
        BuildPostQuery("product_section_edit_form", strQueryPrefix, false);    // defined in ccWEbContentGenerator.cs
    }
    else if (strAction == "reorder")
    {   // build a query specifically for reordering
        //alert(getAllOptionValues(oCurrentObject, ","));
        strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=METAProdSection&target_file=&index_order="+getAllOptionValues(oCurrentObject, ",");
        IssueAJAXQuery(strQueryPrefix, 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(oCurrentObject.value == "")
            strAction="add";                                        // add a new item
        else                
            strAction="edit";                                       // edit an existing item
        var strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=METAProdSection&target_file=" + GetSelectValue('section_product_list') + "&";
        BuildPostQuery("product_section_edit_form", strQueryPrefix, true);    // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
    else    // this is likely detail acquisition
    {
        var strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=METAProdSection&target_file=" + GetSelectValue('section_product_list') + "&";
        BuildPostQuery("product_section_edit_form", strQueryPrefix, false);    // defined in ccWEbContentGenerator.cs
    }    
}

function ProcessBlogAction(strAction, strBlogName)
{    
    strSelectedProduct = GetSelectValue("blog_item_list");    
    oCurrentObject = document.getElementById("blog_item_list");
    //alert("Processing " + strSelectedProduct + " in category " + strSelectedDiv);
    if (strAction == "add")                                         // if add, reset the form, and focus on name
    {
        ResetForm("blog_edit_form", "blog_title");                  // defined in AJAX_Base.js
        setMCEText("");                                             // defined in ccWEbContentGenerator.cs
        return;
    }
    else if (strAction == "delete")
    {
        if(!confirm("Are you sure you want to delete selected item?"))
            return;
        var strQueryPrefix = "../ccWebContentProcessor.php?action=" + strAction + "&type=BlogItem&target_file=" + GetSelectValue('blog_item_list') + "&blog=" + strBlogName + "&";
        BuildPostQuery("blog_edit_form", strQueryPrefix, false);    // defined in ccWEbContentGenerator.cs
    }
    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(oCurrentObject.value == "")
            strAction="add";                                        // add a new item
        else                
            strAction="edit";                                       // edit an existing item
        var strQueryPrefix = "../ccWebContentProcessor.php?action=" + strAction + "&type=BlogItem&target_file=" + GetSelectValue('blog_item_list') + "&blog=" + strBlogName + "&";
        BuildPostQuery("blog_edit_form", strQueryPrefix, true);    // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
}

function ProcessCommentAction(strAction, strTargetID, strBlogName)
{
    if (strAction == "add_comment")                           // this is the actual form save/submit
    {   // edit an existing item
        var strQueryPrefix = "include/ccWebContentProcessor.php?action=" + strAction + "&type=BlogComments&target_file=" + strTargetID + "&blog=" + strBlogName + "&";
        BuildPostQuery("blog_comment_form", strQueryPrefix, true);    // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
    else
    {
        var strQueryPrefix = "include/ccWebContentProcessor.php?action=" + strAction + "&type=BlogComments&target_file=" + strTargetID + "&blog=" + strBlogName + "&";
        BuildPostQuery("blog_comment_form", strQueryPrefix, true);    // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
}

function ProcessUserAction(strAction)
{    
    strSelectedProduct = GetSelectValue("user_item_list");    
    oCurrentObject = document.getElementById("user_item_list");
    //alert("Processing " + strSelectedProduct + " in category " + strSelectedDiv);
    if (strAction == "add")                                         // if add, reset the form, and focus on name
    {
        ResetForm("user_edit_form", "user_name");                  // defined in AJAX_Base.js
        document.getElementById("password_div").style.display = "block";
        // make password field visible
        setMCEText("");                                            // defined in ccWEbContentGenerator.cs
        return;
    }
    else if (strAction == "delete")
    {
        if(!confirm("Are you sure you want to delete selected item?"))
            return;
        var strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=UserItem&target_file=" + GetSelectValue('user_item_list') + "&";
        BuildPostQuery("user_edit_form", strQueryPrefix, false);    // defined in ccWEbContentGenerator.cs
    }
    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(oCurrentObject.value == "")
        {            
            strAction="add";                                        // add a new item
        }
        else                
            strAction="edit";                                       // edit an existing item
        var strQueryPrefix = "../ccWebContentProcessor.php?action="+strAction+"&type=UserItem&target_file=" + GetSelectValue('user_item_list') + "&";
        BuildPostQuery("user_edit_form", strQueryPrefix, true);    // defined in ccWEbContentGenerator.cs (USE post for this one)
    }
}


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, "@ccAMP@") + "&";
            else                // get the TinyMCE textareas            
                strQuery += oFormElements[i].id + "=" + getMCEText(oFormElements[i].id).replace(/&/g, "@ccAMP@") + "&";            
        }
        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].name=="product_recommendations" || oFormElements[i].name=="user_products")
            {
                strQuery += oFormElements[i].name + "=" + getAllOptionValues(oFormElements[i], ",") + "&";
            }            
        }
        else if (oFormElements[i].type=="checkbox")
        {            
            if(oFormElements[i].checked)
                strQuery += oFormElements[i].name + "=true&";
            else
                strQuery += oFormElements[i].name + "=false&";
        }         
        else
        {
            //alert(oFormElements[i].id + "=" + oFormElements[i].value + " of type " + oFormElements[i].type);
            strQuery += oFormElements[i].name + "=" + oFormElements[i].value.replace(/&/g, "@ccAMP@") + "&";
        }        
    }
    if(bPost)
    {
        //alert("ccWebContentGenerator.js::BuildPostQuery - " + strQuery);
        IssueAJAXPost(strURL, strQuery, ProcessContentMangerResponsePOST);
    }
    else
        IssueAJAXQuery(strURL+strQuery, ProcessContentMangerResponse);         // the query response is responsible for refresh if needed
    // alert('BuildPostQuery() in ccWebContentGenerator.js');
    // 
    // 
}

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=text&target_file="+strSectionName;
    var strQuery = "content_text="+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;	
}