/*
Date:
Author:
Description:Parse xml file based on jquery.js
*/

var JXml = {
    GetData: function(request) {
        if (request.Url == "" || request.OnSuccess == null || !$.isFunction(request.OnSuccess))
            return;
        $.ajax({
            type: request.Type,     /* GET,POST,DELETE etc.*/
            url: request.Url,      /* The URL to request*/
            data: request.Data,     /* Data to be sent to the server */
            cache: true,    /* if set to false it will force the pages that you request to not be cached by the browser.*/
            dataType: request.DataType, /* text or xml*/
            async: request.Async,    /* send request asynchronously(true,default) or synchronously(false)*/
            success: function(data) {   /* hanlder when success,the parameter is the response data */
                
                var xml;
                if (typeof data == "string") { /*handling for ie*/
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(data);
                } else {
                    xml = data;
                }
                request.OnSuccess(xml);
            },
            error: function(xhr, err, errthrown) {
                //throw new Error("Error occurs,the type is:" + err);
                if (request.error != null)
                    request.error();
            }
        });
        
    },
    /*
    * param:xml,a kind of XmlDocument,e.g. the argument in the callback function when success
    * param:expr,case sensitive,e.g. RACE[NUM='4']/OUT,according to the syntax of jquery,it's 
    better to write like this: RACE[NUM='4']>OUT
    */
    SelectNodes: function(xml, expr) {
        try {
            if (expr.indexOf('/') >= 0) {
                var arrtmp = [];
                arrtmp = expr.split('/');
                expr = arrtmp.join('>');
            }
            return $(xml).find(expr);
        }
        catch (err) {
            //throw new Error("Error occurs when parsing,please check data from the server.");
        }
    },
    /*
    * param:xml,the same as SelectNodes
    * param:expr,the same as SelectNodes
    */
    SelectSingleNode: function(xml, expr) {
        var returnNode = null;

        if (xml != null & expr != null) {
            var arrNodes = JXml.SelectNodes(xml, expr);
            if ((arrNodes != null) && arrNodes.length > 0) {
                returnNode = arrNodes[0];
            }
        }
        return returnNode;
    },
    /*
    * param:pNode,the node from which the value for attribute will be retrieved
    * param:attrName,the attribute name
    */
    GetAttribute: function(pNode, attrName) {
        var attrValue = '';
        if (pNode != null && attrName != null && attrName != "") {
            attrValue = $(pNode).attr(attrName);
        }
        return attrValue;
    },
    /*
    * param:pNode
    */
    GetText: function(pNode) {
        var nodeText = '';
        if (null != pNode && typeof pNode != "undefined") {
            if ($(pNode).length > 0) {
                nodeText = $($(pNode)[0]).text();
            }
        }
        return nodeText;
    }
}

