﻿// JScript File

Type.registerNamespace("DragAndDrop");

    
DragAndDrop.DraggableProductBehavior = function(element) 
{
    // initialize base class
    DragAndDrop.DraggableProductBehavior.initializeBase(this, [element]);
    
    // mousedown event handler
    this._mouseDownHandler = Function.createDelegate(this, this._handleMouseDown);
    
    // the product object attached to this draggable item
    this._item = null;
    
    // the transparent element when dragging the item
    this._visual = null;
}

DragAndDrop.DraggableProductBehavior.prototype = 
{
    // IDragSource methods
    // returns the data type of this draggable item - "DragItem"
    get_dragDataType: function() {
        return "DragItem";
    },

    // get the data object attached to this draggable item
    getDragData: function(context) {
        return this._item;
    },

    // drag mode is set to copy
    get_dragMode: function() {
        return Sys.Preview.UI.DragMode.Copy;
    },

    // this method will be called when user starts dragging
    onDragStart: function() {
    },

    // this method will be called when user is dragging
    onDrag: function() {
    },

    // this method will be called when user finishes dragging
    onDragEnd: function(canceled) {
        if (this._visual)
            // remove the transparent element
            this.get_element().parentNode.removeChild(this._visual);
    },
   
    // 'item' property
    get_item: function(item) {
        return this._item
    },
    
    set_item: function(item) {
        this._item = item;
    },
     
    initialize: function() {
        $addHandler(this.get_element(), "mousedown", this._mouseDownHandler);
    },

    // mousedown event handler
    _handleMouseDown: function(ev) 
    {
        // DragDropManager needs this setting
        window._event = ev; 

        // set the style of the transparent element which follows the cusor
        this._visual = this.get_element().cloneNode(true);
        this._visual.style.opacity = "0.7";
        this._visual.style.filter = 
            "progid:DXImageTransform.Microsoft.BasicImage(opacity=0.7)";
        this._visual.style.zIndex = 99999;
        this.get_element().parentNode.appendChild(this._visual);
        var location = Sys.UI.DomElement.getLocation(this.get_element());
        Sys.UI.DomElement.setLocation(this._visual, location.x, location.y);

        // start dragging 
        Sys.Preview.UI.DragDropManager.startDragDrop(this, this._visual, null);
    },

    dispose: function() {
        if (this._mouseDownHandler)
            $removeHandler(this.get_element(), "mousedown", this._mouseDownHandler);
        this._mouseDownHandler = null;
        
        // dispose base class
        DragAndDrop.DraggableProductBehavior.callBaseMethod(this, 'dispose');
    }
}

DragAndDrop.DraggableProductBehavior.registerClass(
    "DragAndDrop.DraggableProductBehavior",
    Sys.UI.Behavior, 
    Sys.Preview.UI.IDragSource
);

DragAndDrop.ShoppingCartBehavior = function(element) 
{
    // initialize base class
    DragAndDrop.ShoppingCartBehavior.initializeBase(this, [element]);
    
    // item list of this shopping cart
    this._items = new Object();
}

DragAndDrop.ShoppingCartBehavior.prototype = {
    // IDropTarget methods
    // return a DOM element represents the shopping cart
    get_dropTargetElement: function() {
        return this.get_element();
    },

    get_dropData : function(dropData) {
        return this._items[0];
    },
    
    // if this draggable item can be dropped into this drop target
    canDrop: function(dragMode, dataType, data) {
        return (dataType == "DragItem" && data);
    },

    onSuccess: function (result)
    {    
    },
        
    onFailure: function (result)
    {
       alert(result.get_message());
    },
       
    // drop this draggable item into this drop target
    drop : function(dragMode, dataType, data) {
        if (dataType == "DragItem" && data) {
            // the first product of this type, set quantity to 1           
            //if (this._items[data.Id] == null) {
                //this._items[data.Id] = {Product: data};
                this._items[0] = {Product: data};
                PageMethods.GetDropData(this._items[0].Product.Id, this._items[0].Product.Name, this._items[0].Product.ImageUrl, this._items[0].Product.Description, this._items[0].Product.Price, this._items[0].Product.VatRate, this._items[0].Product.MaxProjects);
            //}
            // refresh shopping cart UI
            this._refreshShoppingCart();
            
            // set background color of shopping cart element to white
            this.get_element().style.backgroundColor = "#fff";
        }
    },

    // this method will be called when a draggable item is entering this drop target
    onDragEnterTarget : function(dragMode, dataType, data) {
        if (dataType == "DragItem" && data) {
            // set background color of shopping cart element to gray
            this.get_element().style.backgroundColor = "#E0E0E0";
        }
    },
   
    // this method will be called when a draggable item is leaving this drop target
    onDragLeaveTarget : function(dragMode, dataType, data) {
        if (dataType == "DragItem" && data) {
            // set background color of shopping cart element to white
            this.get_element().style.backgroundColor = "#fff";
        }
    },

    // this method will be called when a draggable item is hovering on this drop target
    onDragInTarget : function(dragMode, dataType, data) {
    },
   
    // refresh shopping cart UI according to current products
    _refreshShoppingCart: function() {
        var cartBuilder = new Sys.StringBuilder();
        for (var id in this._items) {
            cartBuilder.append("<div>");
            cartBuilder.append("<table><tr><td><img src=\"" + this._items[id].Product.ImageUrl + "\"/></td>");
            cartBuilder.append("<td valign=middle><h3>" + this._items[id].Product.Name + "</h3></td></tr>");
            cartBuilder.append("<tr><td colspan=2 align=left>" + this._items[id].Product.Description + "</td></tr>");
            cartBuilder.append("</table></div>");            
        }
        
        this.get_element().innerHTML = cartBuilder.toString();
    },
    
    // get a dictionary object, the key is product Id and the value is product quantity
    getProductsToBeOrdered: function() {
        var productsToBeOrdered = new Object();
        
        for (var id in this._items) {
            productsToBeOrdered[_items[0].Id] = this._items[0].Name
        }
                
        return productsToBeOrdered;
    },
    
    initialize: function() {
        // initialize base class
        DragAndDrop.ShoppingCartBehavior.callBaseMethod(this, "initialize");
        
        // register this drop target in DragDropManager
        Sys.Preview.UI.DragDropManager.registerDropTarget(this);
    },
   
    dispose: function() {
        // unregister this drop target in DragDropManager
        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);
        
        // disponse base class
        DragAndDrop.ShoppingCartBehavior.callBaseMethod(this, "dispose");
    }
}

DragAndDrop.ShoppingCartBehavior.registerClass("DragAndDrop.ShoppingCartBehavior",
    Sys.UI.Behavior, Sys.Preview.UI.IDropTarget);

//function onSuccess(result, context, methodName)
//{
//   var totalLabel = document.getElementById(TotPrice.id);
//   totalLabel.innerText = result;
//}

//function updateTotal(price, vatRate, subPeriod)
//{
//    PageMethods.CalculateTotal(price, vatRate, subPeriod, onSuccess);
//}
 
function ProviderLoginCheck()
{
    var userNameTextBox = document.getElementById(userNameBox.Id);
    alert("Provider UserName is not registered");
}

function getProjectType(projectSelector)
{
    if (projectSelector)
    {
        Sys.Net.WebServiceProxy.invoke("Controls/DragDrop.asmx",
                                       "GetProjectTypes",
                                       false,
                                       { projectSelector: projectSelector },
                                       UpdateProjectTypes,
                                       UpdateFailed);
    }
}


function UpdateProjectTypes(data)
{
    var projType = document.getElementById(ddlProjectType);
    for (var k = 0; k < data.length; k++) 
    {   
            newOption = new Option(data[k].ProjectType, data[k].ProjectTypeID);   
            projType.options.add(newOption);   
    }    
}

function UpdateFailed(error)
{
    var x = error;
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();

