﻿function EVOControl(windowElement)
{
    this.element = windowElement;

    this.get_element = function() {
        return this.element;
    }
    
    this.set_DataSource = function(value) {
        this._DataSource = value;
    }
    this.get_DataSource = function() {
        return this._DataSource;
    }

    this._findcontrol = function(name) {
        var control = $(this.get_element()).find("#" + name)[0];
        if(control == undefined || control == null) control = this._findASPNETcontrol(name);
        return control;
    }

    this._findjcontrol = function(name) {
        var control = $($(this.get_element()).find("#" + name)[0]);
        if(control[0] == undefined) control =this._findASPNETjcontrol(name);
        return control;
    }

    this._findASPNETcontrol = function(name) {
        return $(this.get_element()).find("*[id*=" + name + "]")[0];
    }

    this._findASPNETjcontrol = function(name) {
        return $($(this.get_element()).find("*[id*=" + name + "]")[0]);
    }

    this.hide = function() {
        $(this.get_element()).hide();
    }

    this.show = function() {
        $(this.get_element()).show();
    }

    this.get_Service = function() {
        return this._service;
    }
    this.set_Service = function(value) {
        this._service = value;
        this._service.set_defaultFailedCallback(this._FailedCallback);
    }    

    this.Lock = function()
    {
        $(this.get_element()).loading({ align: 'center', text: 'Loading...', mask: true, effect:'ellipsis update' });
    }
    
    this.UnLock = function()
    {
        $(this.get_element()).loading(false);
    }

    this.LockControl = function(jItem)
    {
        jItem.loading({ align: 'center', text: 'Loading...', mask: true, effect:'ellipsis update' });
    }
    
    this.UnLockControl = function(jItem)
    {
        jItem.loading(false);
    }
    
    this._ResetEditor = function(jControl)
    {
        jControl.find(":input").val("");
        jControl.find(":checkbox").attr('checked', false);
    }
    
    this.ReplaceNullWithEmptyString = function(value)
    {
        if(value == null) value = "";
        return value;
    }
    
    this.BindElement = function(elementid, value)
    {
        var myself = this;
        var jElement = myself._findjcontrol(elementid);
        jElement.val(myself.ReplaceNullWithEmptyString(value));
        return jElement;
    }

    this.GetElementValue = function(elementid)
    {
        var myself = this;
        return myself._findjcontrol(elementid).val();
    }
    
    this.Alert = function(stitle, message)
    {
        var myself = this;
		$("<div></div").html(message).dialog({
			modal: true,
			title: stitle,
			buttons: {
				Ok: function() {
					$(this).dialog('close').remove();					
				}
			}
		});
    }
    
    this.Confirm = function(stitle, message, okbtntext, cancelbtntext, callback)
    {
        var myself = this;
        var btns = {};
        btns[okbtntext] = function(){ $(this).dialog('close').remove(); callback(); };
        btns[cancelbtntext] = function(){ $(this).dialog('close').remove(); };
		$("<div></div").html(message).dialog({
			modal: true,
			title: stitle,
			buttons: btns
		});
    }
    
    this._FailedCallback = function(error)
    {
        var controlReference = new EVOControl(this);//i dont know why but we lose the scope of this object?
        //controlReference.UnLock();
        var stackTrace = error.get_stackTrace();
        var message = error.get_message();
        var statusCode = error.get_statusCode();
        var exceptionType = error.get_exceptionType();
        var timedout = error.get_timedOut();
        controlReference.Alert('ERROR!', 
            "Service Error: " + message + "<br/>" +
            "Status Code: " + statusCode + "<br/>" +
            "Exception Type: " + exceptionType + "<br/>" +
            "Timedout: " + timedout + "<br/>" +
            "Stack Trace: " +  stackTrace
            );
    }
}
