﻿// JScript File
function ParseAjResponse(res,showLogin)
{
    if(showLogin==null||(showLogin!=null&&showLogin))
        if(res.error!=null)
            RunDebugForError(res.error,res.request);
    
    if(res.value!=null)
    {
        var ret = res.value;
        if(ret.isException!=null && ret.isException)
        {
            // Is Exception
            switch(ret.val)
            {
            case 0: //Session Expire
               // alert("Session Expire");
               // EHRMS.BasePage.SignOut();
               // window.location = "login.aspx";
                if(showLogin==null||(showLogin!=null&&showLogin))
                    ShowLoginFramePanel();
                break;
            case 1: //No Access
                break ;
            }
        }
        //! not an exception
        else
            return ret.val;
    }
    else return res.value;
}

function ShowLoginFramePanel()
{
    alert("Here should be a login dialog");
}

function AjCall(callback)
{
    return function(res)
    {
        var ret  = ParseAjResponse(res);
        if(ret!=null)
        {
            if(typeof callback != 'undefined')
            {
                callback(ret);
            }
        }
    }
}

function RunDebugForError(error,request)
{
  var msg       =   "Server side Exception!\n"
  msg           +=  "\nMethod:\t"+ ( typeof request == "undefined" ? "Method unknown": request.method );
  msg           +=  "\nMessage:\t"+error.Message;
  msg           +=  "\nType:\t"  + error.Type;
  alert(msg);
}



function RefreshTopMenuFromParent(action)
{
    parent.RefreshTopMenu(action);
}


//
// Create an Ajax.Web.Dictionary object for JSON Converting to the 
//  .Net Dictionary<KeyType,ValueType> Type
//
//      Ajax.Web.Dictionary Members: 
//
//		toJSON		
//		setValue		
//		getValue		
//		getKeys		
//		containsKey		
//		add		
//		keys		
//		values		

function GetNewDictionary(KeyType,ValueType)
{
    // alloc the dictionary
    var obj                                             = new Ajax.Web.Dictionary();
   
    // specify the Dictionary<ValueType,ValueType> type
    obj.__type                                          = "System.Collections.Generic.Dictionary`2[["+KeyType+", mscorlib],["+ValueType+", mscorlib]]";
    
    obj.setValue                                        = setValue_DictionaryFunc;
    obj.remove                                          = remove_DictionaryFunc;
    
    return obj;
}



// remove element by key
function remove_DictionaryFunc ( key )
{
    with(this)
    {
        for(var i=0; i < keys.length ; i++)
        {
            if ( keys[i] == key)
                break;
        }

        // the key wasn't found
        if(i < keys.length)
        {
            keys.splice ( i, 1 );
            values.splice ( i, 1 );
        }
    }
}

function setValue_DictionaryFunc(key,value) 
{
    with(this)
    {
        for(var i=0; i < keys.length ; i++)
        {
            if ( keys[i] == key)
                break;
        }

        // the key wasn't found
        if(i < keys.length)
        {
            // set the new value
            values[i]           = value;
        }
        else
        {
            // add the new value
            add(key,value);
        }
    }
}




