﻿//this is the default page that ajax requests will be sent to
//this url will change depending if the UserID field is set
//by asp.net or not


var protocol = window.location.protocol;
var host = window.location.host;
var path = window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/"));
/*
var protocol = "http:";
var host = "admin.dentalanywhere.com";
var path = "";
*/

var url = protocol+"//"+host+path+"/";
var POST_URL = url+"service/service.ashx";


function isIE()
{
    
    return (navigator.appName.indexOf("Microsoft") > -1);

}

function bVersion()
{
	return navigator.appVersion;
}	


var changed = false;

var callbacks = new Array();

var callID = 0;

function CallItem()
{
    this.Callback = null;
    this.CallID = 0;
}

function start()
{

    begin();
    initMenu();

}

function initMenu()
{

}


$(document).ready(start);


function onFormSubmit()
{


}



function begin()
{
    //override this function in the control's javascript page


}

function save(callback)
{
    
    //each control has a save function
    //if you pass a callback to it, the save
    //will pass it to the com(packet, callback)
    //function

}




function getPacket(className, methodName)
{
    var packet = new Object();
    packet.UserID = "0";
    packet.ClassName = className;
    packet.MethodName = methodName;
    
    return packet;

}

//sends the JSONPacket object to asp.net
//pass the resultFunction to receive the data 
//from the callback
function com(packet, resultFunction)
{
    callID++;
    var citem = new CallItem();
    citem.CallID = callID;
    citem.Callback = resultFunction;
    callbacks.push(citem);
    
    packet.CallID = callID;
    
    //callbacks.push(resultFunction);//add the callback function to the list
    $.post(POST_URL, { jsonData: JSON.stringify(packet) }, onCallback);
    

}

//data will be of type JSONResult
//check the result for errors, if it's
//good pass the data to the callback function
function onCallback(data)
{
     var jresult = JSON.parse(data);
    
    if (jresult.IsSuccess)
    {
        //find the correct callback with the callID

        var item = getCallback(Number(jresult.CallID));
        
        if (item != null)
        {
            item.Callback(jresult.Data);
        }
    
      
   }
   else
   {
        if (jresult.Message == "Your session has expired.")
        {
            window.location = "Default.aspx";
        }
        else
        {
            alert("Error: "+jresult.Message);
        }
        
        onServerError();
        
   }
   

}

function onServerError()
{

}


function getCallback(id)
{

    var index = -1;
    for(var i=0; i < callbacks.length; i++)
    {
        if (Number(callbacks[i].CallID) == id)
        {
            index = i;
        }
    }

    if (index > -1)
    {
        var item = callbacks[index];
        callbacks.splice(index, 1);
        return item;
    }
    
    return null;

}





