
function AddSubscription()
{
	this.oFeedOption = this.oMessage = this.oFeedText = this.oSubmitFeed = 
	this.oFeedImportDisplay = this.oSubmitFeedSec = this.oProgDialog = this.oForm = null;
	this.sAjaxUtilsUrl = _GL.getBaseUrl() + 'AjaxServices/utils.asmx/';
	this.sDefFeedText = 'http://';
	this.sAjaxUtilsUrl = _GL.getBaseUrl() + 'AjaxServices/utils.asmx/';
	this.sAjaxMyStuffUrl = _GL.getBaseUrl() + 'AjaxServices/MyStuff.asmx/';
	this.sErrMsgs = {'rss':'Feed URL cannot be empty.', 'feedType':'Please choose a feed type.','cat':'Please choose a category.','signin':'You need to <a href="'+_GL.getBaseUrl()+'login.aspx?refer='+encodeURIComponent(location.href)+'">login</a> to submit feed'};

	this.init = function()
	{
		if (typeof oFilterDD != 'undefined')
            oFilterDD.init();
		
		this.oMessage = new HTMLLayer('submitmsg');
		this.oFeedText = new HTML('RssFeedUrlBox');
		this.oSubmitFeed = new HTML('submitFeedBtn');
		this.oForm = document.forms['submitFeedFrm'];
		this.oDisplayLoader = new HTMLLayer('displayLoader');
		this.oContent = new HTMLLayer('mb-content');
		this.oShadow = new HTMLLayer('shadowBG');
		
		if (typeof aFocusElems != 'undefined')
		    setTextFocus(aFocusElems);
		    
	    parent.oOverlay.resize();
	}

	this.clearFeedText = function()
	{
		if (this.oFeedText.getValue().is(this.sDefFeedText))
			this.oFeedText.setValue('');
	}

	this.showFeedsOptions = function()
	{
	    var v = this.oFeedText.getValue();
		if (v != '' || v.length > 0)
		{
			this.oFeedOption.show(true);
			if (_GL.isAthenticated())
				this.oSubmitFeed.enable(true);
			else
				this.showMessage(this.sErrMsgs.signin);
		}
		else
		{
		    this.oSubmitFeed.enable(false);
		    this.oMessage.show(false);
		}
	}

	this.showMessage = function(pMsg)
	{
		this.oMessage.setValue(pMsg);
		this.oMessage.show(true);
	}

    this.showImportFeed = function()
    {
        this.oFeedImportDisplay.show(false);
        this.oSubmitFeedSec.show(true);
    }

	this.postFeed = function()
	{
	    var elems = this.oForm.elements, sFU = this.oFeedText.getValue(),
	        oFeedOption = elems['feedTypeList'], sVF = oFeedOption.options[oFeedOption.selectedIndex].value,
	        oCatList = elems['categoryList'], sCL = oCatList.options[oCatList.selectedIndex].value,
	        bVF = sVF.is('-1'), bCL = sCL.is('-1'), s = '', sOPML = elems['opmlupload'].value;
	    
	    if (!sOPML && sOPML.length == 0)
	    {
	        s = (!sFU || sFU.length == 0) ? 'Please provide feed url' :
	            (bVF && bCL) ? 'Please select feed type and category' :
	            bVF ? 'Please select feed type' :
	            bCL ? 'Please select a category' : '';
    	    
	        if (s.length > 0)
	        {
	            this.showMessage(s);
	            return false;
	        }
	    }
	    else
        {
                this.oFeedText.setValue("");
        } 
        
	    this.oShadow.show(true);
		this.oDisplayLoader.show(true);
	    this.oForm.submit();
	}

	this.getCatListByType = function(pObj)
	{
		var v = pObj.options[pObj.selectedIndex].value;
		var url = this.sAjaxUtilsUrl + 'getCategoryListByType?type=' + v;
		var oAjax = new Ajax('getCategoryListByType', url, true, ['oAS.processCatList']);
		oAjax.iResponseType = oAjax.RESPONSE_XML;
		oAjax.send();
	}

	this.processCatList = function(pResp)
	{
		var oXml = pResp.oResponseXml, 
		    aStrings = oXmlHelper.getNodes(oXml,'string'), len = aStrings ? aStrings.length : 0,
		    oCat = this.oForm.elements['categoryList'];
		
		for (var i = oCat.options.length-1; i >-1; i--)
		    oCat.options[i] = null;
    	
	    //Add new states category list combo box.
	    for (var i = 0; i < len; i++)
	    {
   		    var val = oXmlHelper.getNodeValue(aStrings[i])  	 
		    oCat.options[i] = new Option( val, val,  false, false);
	    }
	    
	    oCat.disabled = false;
	}
	
	this.processImportFeed = function(pImpType,pFeedResp,pChannelId,pChannelTitle,pOpmlStatus,pOpmlResp)
    {
        var bError = false, sErrMsg = '', p = parent,
            oR = p.oReader, oM = p.oMyStuff;
        switch (pImpType)
        {
            case "feed":
                if (pChannelId)
                {
                    if (oR)
                        oR.getChannelData(pChannelId, typeof pChannelTitle != 'undefined' ? pChannelTitle : '','Source');
                    else
                        p.location.href = 'people/mystuff.aspx?username='+p.username+'&channelid='+pChannelId+'&channeltitle='+pChannelTitle;
                }
                else
                {
                    bError = true;
                    sErrMsg = pFeedResp;
                }
                break;
                
            case "opml":
                if (pOpmlStatus)
                {
                    if (oR)
                    {
                        var oSS = p._GL.getUIElem('snapShotLink')
                        if (oSS)
                            oSS.onclick();
                    }
                    else
                        p.location.href = 'people/mystuff.aspx?username='+p.username;
                }
                else
                {
                    bError = true;
                    sErrMsg = pFeedResp;
                }
                break;
        }
        
        if (bError)
        {
            this.oShadow.show(false);
		    this.oDisplayLoader.show(false);
		    this.oMessage.setValue(sErrMsg);
		}
    }
}

var oAS = new AddSubscription();
var sPageName = 'feeds';
window.onload = function()
{
    oAS.init();
}
