﻿/// <reference name="MicrosoftAjax.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

Type.registerNamespace('WIS.Web.Server');

WIS.Web.Server.SitesEditBehavior = function(element) {
    WIS.Web.Server.SitesEditBehavior.initializeBase(this, [element]);

    this._refID = null;
    this._type = null;
    this._url = null;
    this._description = null;
    this._rss = null;
    
    this._results = null;
    this._error = null;
    this._service = null;
}
WIS.Web.Server.SitesEditBehavior.prototype = {

    initialize : function() {
        WIS.Web.Server.SitesEditBehavior.callBaseMethod(this, 'initialize');
    },

    dispose : function() {
        WIS.Web.Server.SitesEditBehavior.callBaseMethod(this, 'dispose');
    },
    
    _getValidInput : function() {
        if (this._url.value === '') {
            alert('Please fill in the URL field!');
            return false;
        }
        if (this._url.value !== '' && !oView.getIsUrl(this._url.value)) {
            alert('URL not formatted properly. Please use the format http://sample.com');
            return false;
        }
        if (this._rss.value !== '' && !oView.getIsUrl(this._rss.value)) {
            alert('Feed not formatted properly. Please use the format http://sample.com');
            return false;
        }
        return true;
    },
    
    _resetFieldValues : function() {
        this._url.value = '';
        this._description.value = '';
        this._rss.value = '';
        this._type.selectedIndex = 0;
    },
    
    Submit : function() {
        this._url.value = oView.Trim(this._url.value);
        this._rss.value = oView.Trim(this._rss.value);

        if (this._getValidInput()){
            var data = {
                "refID" : this._refID,
                "type" : this._type.options[this._type.selectedIndex].value,
                "url" : this._url.value,
                "description" : this._description.value,
                "rss" : this._rss.value,
                "parent" : this.get_element().id
            }
            Sys.Net.WebServiceProxy.invoke(
                this._service, "SubmitSite", false, data,
                Function.createDelegate(this, this._onSubmitComplete)
            );
        }
    },
    
    _onSubmitComplete : function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		this._error.innerHTML = '';
		if (actualResult.error) {
		    this._error.innerHTML = actualResult.errorMsg;
		}
		else {
		    this._results.innerHTML += actualResult.rawHTML;
		    this._resetFieldValues();
		}
		if (actualResult.redirectURL != null && actualResult.redirectURL !== '') {
		    window.location.href = actualResult.redirectURL;
		}
	},
	
    Remove : function(source, parent, refID, confirmText) {
        if (confirm(confirmText)) {
            source.innerHTML = "deleting... please wait";
            var data = {
                "refID" : this._refID,
                "site" : refID, 
                "parent" : parent
            }
            Sys.Net.WebServiceProxy.invoke(
                this._service, "RemoveSite", false, data,
                Function.createDelegate(this, this._onRemoveComplete)
            );
        }
    },
    
    _onRemoveComplete : function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		if (actualResult.error) {
		    this._error.innerHTML = actualResult.errorMsg;
		}
		else {
		    $get(actualResult.parent).style.display = 'none';
		}
	},
	
	get_RefID : function() {
	    return this._account;
	},
	set_RefID : function(value) {
	    this._refID = value;
	},
	
	get_Results : function() {
	    return this._results;
	},
	set_Results : function(value) {
	    this._results = value;
	},
	
	get_Error : function() {
	    return this._error;
	},
	set_Error : function(value) {
	    this._error = value;
	},
    
    get_URL: function() {
        return this._url;
    },
    set_URL: function(value) {
        this._url = value;
    },
    
    get_Type: function() {
        return this._type;
    },
    set_Type: function(value) {
        this._type = value;
    },
    
    get_Description: function() {
        return this._description;
    },
    set_Description: function(value) {
        this._description = value;
    },
    
    get_RSS: function() {
        return this._rss;
    },
    set_RSS: function(value) {
        this._rss = value;
    },
    
    get_Service : function() {
        return this._service;
    },
    set_Service : function(value) {
        this._service = value;
    }
}
WIS.Web.Server.SitesEditBehavior.registerClass('WIS.Web.Server.SitesEditBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();