/**
 * Created by TEMESOFT
 * Sep 28, 2010   12:23:57 AM
 * <p/>
 * WARNING - PLEASE READ
 * This software is the confidential and proprietary information ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with the author.
 * <p/>
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE
 * OR ITS DERIVATIVES.
 */

/**
 * Usage:
 * <pre>
 *
 *      <script type="text/javascript">
 *          var _ajax_tracking_call = {
 *                requestUrl: "/index.ajax",
 *                requestMethod: "get",
 *                responseDetails: [
 *                    {localElementId: "h94f89g", remoteJsonObject: "response.errorcode"},
 *                    {localElementId: "ku9h2j0", remoteJsonObject: "response.errormessage"},
 *                    {localElementId: "fj83g43", remoteJsonObject: "response.billinginfo.invoice.id"},
 *                    {localElementId: "392wgf0", remoteJsonObject: "response.billinginfo.invoice.duedate"},
 *                    {localElementId: "f893g4g", remoteJsonObject: "response.billinginfo.invoice.ref"},
 *                    {localElementId: "f39g4n9", remoteJsonObject: "response.billinginfo.invoice.carriername"},
 *                ]
 *            };
 *        </script>
 *
 *      <a href="javascript:ajaxCall(_ajax_tracking_call);">Call Ajax Servlet</a>
 * 
 * </pre>
 * @param call
 */
function __ajaxCall(call, debug) {
//    alert("1");
//    __clearLocalElements(call);
//    alert("2");
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();    
    if (x) {
        x.onreadystatechange = function() {
            if (x.readyState == 4 && x.status == 200) {
                var responseText = x.responseText;
                var jsonResponseObject = null;
                if (responseText.indexOf("<?xml ") >= 0) {
                    jsonResponseObject = xml2json.parser(responseText, call.ignoreTags);
                } else {
                    jsonResponseObject = eval(responseText);
                }
                if (debug) {
                    alert("responseText: \n____________________________________________________________\n\n\n" +
                            responseText);
                    alert("jsonResponseObject: \n____________________________________________________________\n\n\n" +
                            xml2json.show_json_structure(jsonResponseObject, true, false));
                }
                for (i = 0; i < call.responseDetails.length; i++ ) {
                    localElementId = call.responseDetails[i].localElementId;
                    remoteJsonObjectName = call.responseDetails[i].remoteJsonObject;
                    if (document.getElementById(localElementId) != null) {
                        document.getElementById(localElementId).value = eval("jsonResponseObject." + remoteJsonObjectName);
                    }
                }
                toggleVisibility(document.getElementById('loadingImageTD_top'))
            }
        }
        x.open(call.requestMethod, call.requestUrl, true);
        x.send(null);
    }
}

function __debugDisplay(jsonObject) {
    alert(xml2json.show_json_structure(jsonObject, true, false));
}

function __clearLocalElements(call) {
    for (i = 0; i < call.responseDetails.length; i++ ) {
        localElementId = call.responseDetails[i].localElementId;
        remoteJsonObjectName = call.responseDetails[i].remoteJsonObject;
        if (document.getElementById(localElementId) != null) {
            document.getElementById(localElementId).innerHTML = "";            
        }
    }
}
