﻿
function ajaxRequest(action,params,callbackFun,sendData)
{
    requestPage("/AjaxEngine.aspx?action="+action+"&"+params,callbackFun,sendData);
}

function ajaxDomRequest(action,params,callbackFun,sendData)
{
    requestDom("AjaxEngine.aspx?action="+action+"&"+params,callbackFun,sendData);
}

function ajax_UpdatePlace(place,action,params,loadingImgTop,timeout,disablePlace)
{
    var e = typeof(place) == "string"?$(place):place;
    var e1 = typeof(disablePlace)=="string"?$(disablePlace):disablePlace;
    var loadingUI = new WaitingUI(e1?e1:e,null,1,"#cccccc",loadingImgTop);
    window.setTimeout(function() {
        requestPage("/AjaxEngine.aspx?action=" + action + "&" + params, function(content) { ajax_UpdatePlace_Callback(e, content, loadingUI);}, null);
    }, timeout ? timeout : 1000);
        
}

function ajax_UpdatePlace_Callback(place,content,loadingUI)
{
    loadingUI.dispose();
    delete loadingUI;
    loadingUI = null;
    setInnerHTML(place,content);
}

function setInnerHTML(el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();   
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {   
        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;   
        htmlCode = htmlCode.replace(/<script([^>]*)>/gi, '<script$1 defer>');   
        el.innerHTML = htmlCode;   
        el.removeChild(el.firstChild);   
    } else {   
        var el_next = el.nextSibling;   
        var el_parent = el.parentNode;   
        el_parent.removeChild(el);   
        el.innerHTML = htmlCode;
        if (el_next) {   
            el_parent.insertBefore(el, el_next)   
        } else {   
            el_parent.appendChild(el);   
        }
    }
} 

function checkUserLogin(callbackFun)
{
    ajaxRequest("userhaslogin","at=userhaslogin",callbackFun,null);
}

function ajaxUserLogin(name,pwd,nextlog,callbackFun)
{
   ajaxRequest("userlogin","at=userLogin",callbackFun,"username="+encodeURIComponent(name)+"&pwd="+encodeURIComponent(pwd)+"&nextlog="+encodeURIComponent(nextlog));
}

function getCurrentUserInfo(callbackFun)
{
    ajaxDomRequest("GetCurrentUserInfo","",function(doc){
    
                var e = doc.documentElement;
                if(!e || e.nodeName != "userInfo")
                    return null;
                
                var u = {
                            userId:e.getElementsByTagName("userId")[0].text,
                            username:e.getElementsByTagName("username")[0].text
                         }
                if(u.userId == "" || !u.userId)
                    u = null;
                delete doc;
                doc = null; 
                callbackFun(u);
                      
            });
}