//***************** COMMUNICATION AND PARSING *******************************************
function getMethodCallStartTag() {
    return '<methodcall time="' + new Date().getTime() + '" />';
}

//Get the communication object. 
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Error in GetXmlHttpObject()! Your browser does not support AJAX.");
                return false;
            }
        }
    }
    return xmlHttp;
}


//Get an XML DOM object for handeling parsing XML.
function GetXMLDomObject(sXML) {
    // code for IE
    var doc = new ActiveXObject("Microsoft.XMLDOM");
    if (window.ActiveXObject) {        
        doc.async = "false";
        doc.loadXML(sXML);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        doc = parser.parseFromString(sXML, "text/xml");
    }
    return doc;
}

function sortReviews( order_number, article_id, nodeid, dynamic_nodeid, AgentId ) {

//    alert(order_number + "," + article_id + "," + nodeid + "," + dynamic_nodeid);
        
    try {

        var strurl = "";

        if (dynamic_nodeid.length != 0 && document.getElementById('reviewsWD')) {

            var popuptext = "";


            var sortVal = document.getElementById("cib_review_sort").value;

            var NoToReturn = document.getElementById("cib_review_NoToReturn").value;

            var theUrl = "/cds/showpage.asp";

       //     alert(sortVal + "-" +  NoToReturn);

            
            theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

       //     theUrl += "&community=sort_avail_list";

            theUrl += "&sortorder=" + sortVal;

            theUrl += "&order_number=" + order_number;

            theUrl += "&article_id=" + article_id;

            theUrl += "&avail_article_id=" + article_id;

            theUrl += "&NoToReturn=" + NoToReturn;

            theUrl += "&isPostBack=true";

            if (AgentId != "0") theUrl += "&agentid=" + AgentId;

       //     alert(theUrl);

            updateDynamicContainer(theUrl, 'reviewsWD', popuptext, true);
            
            
            
        } else {
            //alert('no valid container, not critical error');
            throw 'no valid container';
        }
    } catch (e) {
        // Dynamic update failed, call normal     sort_avail_list' and ./@id = 'community
     //   alert(e);
      //  buyProduct_org(article_id, order_number, price_grp, nodeid, thetarget);
    }     
     

}
function sortLists(order_number, article_id, nodeid, dynamic_nodeid, VisitorAgentId, OwnerAgentId) {

    try {

        var strurl = "";

        if (dynamic_nodeid.length != 0 && document.getElementById('listsWD')) {

            var popuptext = "";


            var sortVal = document.getElementById("cib_list_sort").value;

            var NoToReturn = document.getElementById("cib_list_NoToReturn").value;

            var theUrl = "/cds/showpage.asp";

            theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

            theUrl += "&sortorder=" + sortVal;
            
            theUrl += "&order_number=" + order_number;

            theUrl += "&article_id=" + article_id;

            theUrl += "&VisitorAgentId=" + VisitorAgentId;

            theUrl += "&OwnerAgentId=" + OwnerAgentId;

            theUrl += "&avail_article_id=" + article_id;
            
            theUrl += "&NoToReturn=" + NoToReturn;

            theUrl += "&isPostBack=true";


         //   alert(theUrl);

            updateDynamicContainer(theUrl, 'listsWD', popuptext, true);
            
            
            
        } else {
            //alert('no valid container, not critical error');
            throw 'no valid container';
        }
    } catch (e) {
        // Dynamic update failed, call normal     sort_avail_list' and ./@id = 'community
      //  alert(e);
      //  buyProduct_org(article_id, order_number, price_grp, nodeid, thetarget);
    }  

}

function submitReview(article_id, nodeid, thetarget, popuptext, dynamic_nodeid, setTagName) {
    try {

        var popuptext = "";

        
        if (dynamic_nodeid.length != 0 && document.getElementById('reviewsWD')) {

            popuptext = document.getElementById("review_added_text").value;

            var sTitle = document.getElementById("cib_review_title").value;


            var sAlias = "";
            if ( setTagName == "true" ) sAlias = document.getElementById("cib_review_alias").value;


            var sRating = document.getElementById("cib_review_rating").value;

            var bPublic = document.getElementById("cib_review_is_public").value;

            var strText = document.getElementById("cib_review_text").value;
   
            if (sTitle.length == 0 || strText.length == 0 ) return;
            
            // pipe is text separator this removes it, avails separator is ; and that we fix with pipe
            strText = strText.replace(/[\|]/g, "");

            var sText = strText.substr(0, 1000);

            var sProdId = document.getElementById("cib_review_actionID").value;

            var theUrl = "/cds/showpage.asp";

            theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

            theUrl += "&community=create_avail_review";

            theUrl += "&article_id=" + article_id;

            theUrl += "&avail_article_id=" + article_id;

            theUrl += "&cib_review_is_public=" + bPublic;
            
            theUrl += "&cib_review_title=" +  escape(sTitle) + "&cib_review_rating=" + sRating;

            theUrl += "&cib_review_text=" + escape(sText) + "&cib_review_actionID=" + sProdId;

            theUrl += "&cib_review_alias=" + escape(sAlias);

            theUrl += "&setTagName=" + setTagName;


    //       alert(document.getElementById('write_review_form') != null);
     

            updateDynamicContainer(theUrl, 'reviewsWD', popuptext, true);




            if (document.getElementById('write_review_form') != null) {
            
                document.getElementById("cib_review_title").value = "";
                document.getElementById("cib_review_alias").value = "";
                document.getElementById("cib_review_rating").value = "1";
                document.getElementById("cib_review_text").value = "";            
            
            
             
                ReverseDisplay("show_hide_write_review_link");
                ReverseDisplay("write_review_link");

            }

            if (document.getElementById("alias_top_node") != null) {
                var y = document.getElementById("alias_top_node");
                var x = document.getElementById("change_alias");
                y.removeChild(x);
            }
            
       
            
        } else {
            //alert('no valid container, not critical error');
            throw 'no valid container';
        }
    } catch (e) {
        // Dynamic update failed, call normal
        alert(e);
      //  buyProduct_org(article_id, order_number, price_grp, nodeid, thetarget);
    }
}

function protest(listtype, article_id, nodeid, review_id, dynamic_nodeid, agentid) {
    try {

   //     alert(article_id + "-" + nodeid + "-" + actionID + "-" + dynamic_nodeid );

        var popuptext = "";  //document.getElementById("review_protest_text").value;
        
        var theUrl = "/cds/showpage.asp";

        theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

        theUrl += "&community=ProtestFeedback";

        theUrl += "&listtype=" + listtype;

        theUrl += "&reviewID=" + review_id;

        if (article_id.indexOf("_") != -1) {

            var arr = article_id.split("_");

            theUrl += "&article_id=" + arr[1];
            theUrl += "&MasterActionId=" + article_id;
        //    theUrl += "&actionid=" + article_id;

        } else {

            theUrl += "&article_id=" + article_id;

        }

        theUrl += "&agentid=" + agentid;

    //    alert(theUrl);

        updateDynamicContainer(theUrl, 'reviewsWD', popuptext, true);

   //     alert(document.getElementById("review_protest_text").value);

    } catch (e) {
        // Dynamic update failed, call normal
        alert(e);
        //  buyProduct_org(article_id, order_number, price_grp, nodeid, thetarget);
    }

}
function feedback(listType, article_id, nodeid, typeOfFeedback, id, bPosNeg, dynamic_nodeid, OwnerAgentId) {

    try {
        
        var theUrl = "/cds/showpage.asp";
        
        var popuptext = "";
        var dynContName = "";

        if (typeOfFeedback == "ListFeedback") {
           // popuptext = document.getElementById("list_feedback_text").value;
            dynContName = "listsWD";

        } else if (typeOfFeedback == "ReviewFeedback") {
        
              //  popuptext = document.getElementById("review_feedback_text").value;
                dynContName = "reviewsWD";               
                
        }

        theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

        theUrl += "&community=" + typeOfFeedback;

        //      theUrl += "&order_number=" + order_number.substr(order_number.indexOf("_")+1, order_number.length);
        
  //      var strArr = order_number.split("_");

        if (article_id.indexOf("_") != -1) {

            var arr = article_id.split("_");

            theUrl += "&article_id=" + arr[1];
            theUrl += "&MasterActionId=" + article_id;

        } else {

        theUrl += "&article_id=" + article_id;
        
        }

        theUrl += "&id=" + id;

        theUrl += "&listtype=" + listType;

        theUrl += "&OwnerAgentId=" + OwnerAgentId;
        
        theUrl += "&pos=" + bPosNeg;

     //   alert(theUrl);


        
        updateDynamicContainer(theUrl, dynContName, popuptext, true);

       // alert(document.getElementById("review_feedback_text").value);
        

    } catch (e) {
        // Dynamic update failed, call normal
       // alert(e);
        //  buyProduct_org(article_id, order_number, price_grp, nodeid, thetarget);
    }

}

function submitToList(article_id, nodeid, thetarget, popuptext, dynamic_nodeid, setTagName) {

 //alert("kalle");


    try {


        var isNewList = false;
        var listTitle = "";

   //     strAlias = unescape(strAlias);

        if (document.getElementById("avail_select_list").value == "0") isNewList = true;

        if (dynamic_nodeid.length != 0 && document.getElementById('listsWD')) {

            if (isNewList) listTitle = document.getElementById("cib_list_title").value;

            else listTitle = document.getElementById("avail_select_list").value;
            
            var popuptext = document.getElementById("list_added_text").value;

            var sAlias = "";
            if ( setTagName == "true" ) sAlias = document.getElementById("cib_list_alias").value;

            var bPublic = document.getElementById("cib_list_is_public").value;


            var theUrl = "/cds/showpage.asp";
            theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;

            if (isNewList) {
            
               theUrl += "&community=create_avail_list";
            }
            else {

                theUrl += "&community=add_to_avail_list";

            }

            theUrl += "&article_id=" + article_id;

            theUrl += "&cib_list_title=" + escape(listTitle);

            theUrl += "&cib_list_is_public=" + bPublic;

            theUrl += "&cib_alias=" + escape(sAlias);

            theUrl += "&setTagName=" + setTagName;

            


         //  alert(theUrl);


           updateDynamicContainer(theUrl, 'listsWD', popuptext, true);



           document.getElementById("cib_list_title").value = "";


           ReverseDisplay('add_list_link');

           if (document.getElementById("cib_alias").value != "Tag Name") {

               var y = document.getElementById("alias_top_node");
               var x = document.getElementById("change_alias");
               y.removeChild(x);
               
           }


        } else {
            alert('no valid container, not critical error');
            throw 'no valid container';
        }
    } catch (e) {

        //alert(e.description + "<->" + e);

    }

}
function detele_from_list(listType, list_id, dynamic_masterpage_nodeid, nodeid, master_actionId, ListActionId, agentid) {

    try {
        var popuptext = "";
        
        var theUrl = "/cds/showpage.asp";
        theUrl += "?nodeid=" + nodeid + "&origin_nodeid=" + dynamic_masterpage_nodeid;
        theUrl += "&community=removeActionFromList";
        theUrl += "&list_id=" + list_id;
        theUrl += "&ListActionId=" + ListActionId;
        theUrl += "&MasterActionId=" + master_actionId;
        theUrl += "&agentid=" + agentid;
        theUrl += "&listType=" + listType;
    //    alert(list_id + "," + dynamic_masterpage_nodeid + "," + nodeid + "," + action_id + "\n\r" + theUrl);
        updateDynamicContainer(theUrl, 'listsWD', popuptext, true);


    } catch (e) {

    alert(e);
    }
    


}
function deleteReview( listType, reviewId, nodeid, dynamic_nodeid, nbrOfReviews, agentid, ActionID ) {

    try {
            
        
        var popuptext = "";
        
        var theUrl = "/cds/showpage.asp";
        theUrl += "?nodeid=" + nodeid + "&origin_nodeid=" + dynamic_nodeid;
        theUrl += "&community=deleteAvailReview";
        theUrl += "&reviewId=" + reviewId;
        theUrl += "&agentid=" + agentid;
        theUrl += "&MasterActionId=" + ActionID;
        theUrl += "&listType=" + listType;

       
        updateDynamicContainer(theUrl, 'reviewsWD', popuptext, true);
        
        if (nbrOfReviews == "1") {

            if (document.getElementById("reviews_filter") != null) {
                ReverseDisplay("reviews_filter");

            }
        }

        if (listType == "getAvailActionReviewsWD") {

            if (document.getElementById("cib_review_title") != null) {
                document.getElementById("cib_review_title").value = "";
                document.getElementById("cib_review_alias").value = "";
                document.getElementById("cib_review_rating").value = "1";
                document.getElementById("cib_review_text").value = "";
                ReverseDisplay("show_hide_write_review_link");

            }

            

        //    ReverseDisplay("write_review_link");

        }
        /**/
        if (document.getElementById('write_review_form') == null) {
            window.location.reload();
        }
        
    } catch (e) {

        alert("( " + e.description + " )" + e);
    
    }


}
function detele_list(listType, list_id, dynamic_masterpage_nodeid, nodeid, nbrOfLists, agentid, master_actionId) {

    try {
        var popuptext = "";

        var theUrl = "/cds/showpage.asp";
        theUrl += "?nodeid=" + nodeid + "&origin_nodeid=" + dynamic_masterpage_nodeid;
        theUrl += "&community=deleteList";
        theUrl += "&list_id=" + list_id;
        theUrl += "&agentid=" + agentid;
        theUrl += "&MasterActionId=" + master_actionId;
        theUrl += "&listType=" + listType;
        //    alert(list_id + "," + dynamic_masterpage_nodeid + "," + nodeid + "," + action_id + "\n\r" + theUrl);
        
        updateDynamicContainer(theUrl, 'listsWD', popuptext, true);

        
        if ( nbrOfLists == "1" ) {

            if (document.getElementById("agentlist_filter") != null) {
                ReverseDisplay("agentlist_filter");
                //ReverseDisplay('add_list_link');
            }
        }

    } catch (e) {

        alert(e);
    }
}
function setAgentTagName(agentid, nodeid, thetarget, popuptext, dynamic_nodeid) {


 //   alert(agentid + " : " + nodeid + " : " + thetarget + " : " + popuptext + " : " + dynamic_nodeid + " : " + document.getElementById("cib_alias").value);

    try {

        if (document.getElementById("cib_list_alias").value != null && document.getElementById("cib_list_alias").value.length > 0) {
            var popuptext = "";

            var theUrl = "/cds/showpage.asp";
            theUrl += "?nodeid=" + dynamic_nodeid + "&origin_nodeid=" + nodeid;
            theUrl += "&community=setAgentTagName";
            theUrl += "&tagname=" + escape(document.getElementById("cib_list_alias").value);
            theUrl += "&agentid=" + agentid;


            updateDynamicContainer(theUrl, 'avail_tagname', popuptext, true);

            document.getElementById("cib_list_alias").value = "";
            
         if (document.getElementById("change_alias_link") != null )   ReverseDisplay('change_alias_link');

        }
        else return;

    } catch (e) {

        alert(e.description);
    }

    
}