//use : ViewDoc.Label ( labelid, labelname, select );
//notes : if you include prototype.js (http://prototype.conio.net) then you can pass in the id of the select, or the select object

function NonAsyncAjaxHelper ( result, successFunction, errorFunction ) {
    if ( result.error ) {
        if ( errorFunction ) {
            errorFunction ( result );
        } else {
            alert ( "An Error has occurred\n\nDetails:\n" + result.error );   
        }
        return false;
    } else {
        successFunction ( result );
    }
    return true;
}

function NonAsyncAjaxUpdater ( result, element ) {
    return NonAsyncAjaxHelper ( result, function ( result ) { $(element).innerHTML = result.value; });
}

ViewDoc = {
        updateSuggestedLabels : function () {
            NonAsyncAjaxUpdater ( ASP.viewdoc_aspx.RenderSuggestions (), 'uiSuggestedLabelsList' );
        }
    ,
		removeLabel : function (labeldocumentid, labelid, labelname, s ) {
			var select = ( $ ) ? $(s) : s;
			if ( !s ) return;
			NonAsyncAjaxHelper ( ASP.viewdoc_aspx.RemoveLabel(labeldocumentid), 
			    function ( result ) {
			    var l = result.value;
			    ( l == null ) ? lblLabels.innerHTML = '<span class="textred">No Labels!</span>' : null;
			    select.options[0] = null; 
			    select.options[select.length]=(new Option(labelname, labelid));
			    SelectSorter.sort(select);
			    select.options.add(new Option('Select Label', '-1', true, true), 0);
			    Element.hide('label' + labelid); 
			});
			ViewDoc.updateSuggestedLabels ();
		}
	,
	    changeContent : function ( ) {
	        if ( NonAsyncAjaxUpdater ( ASP.viewdoc_aspx.ChangeContent(), 'lblText' ) ) 
	        {
	            Element.hide('EditContent');
	            Element.show('DisplayContent');
	            ViewDoc.updateSuggestedLabels (); 
	            ViewDoc.unpublishWebDocument ();
	        }
	    }
	,
		removeAttachment : function (id){
			ASP.viewdoc_aspx.RemoveAttachment(id);
			Element.hide('attach' + id);
		}
	,
		addLabel : function (label) {
			var element = ( $ ) ? $(label) : label;
			if ( !label) return;
			
			if ( ViewDoc.addLabelById ( element.options[element.selectedIndex].value ) ) {
			    element.options[element.selectedIndex] = null;
			    element.selectedIndex = -1;
			    ViewDoc.updateSuggestedLabels ();
			}
			
		}
	,
	    addLabelById : function ( labelId ) {
	        return NonAsyncAjaxUpdater ( ASP.viewdoc_aspx.AddLabel( labelId ), 'lblLabels' );
	    }
	,    
	    addSuggestedLabel : function ( labelId, selectSpan ) {
	        if ( ViewDoc.addLabelById ( labelId ) ) {
	            Element.hide ( selectSpan );
	            selectSpan.parentNode.className = 'hasLabel';
	            ViewDoc.updateSuggestedLabels ()
	        }
	    }
	,
	
		editAuthor : function (s) {
			var element = ( $ ) ? $(s) : s;
			if( !s ) return;

            var authorId = $F( element );
			if( authorId > -1 )
			{
				NonAsyncAjaxHelper ( ASP.viewdoc_aspx.EditAuthor( authorId ), 
				    function ( result ) {
				        $('uiAuthorLink').innerHTML = result.value[1];
				        $('uiAuthorLink').href = "mailto:" + result.value[0];
				    });
			}
			
			setTimeout ( function () {
			Element.hide( 'EditAuthor' );
			Element.show( 'AuthorName' );
			}, 5 );
			
			
		}
	,
	    effectDuration : 0.25
	,
		editTeam : function (s) {
			var element = ( $ ) ? $(s) : s;
			var author = $( 'EditAuthor' )
			if( !s ) return;
		
		    var teamId = $F('lstEditTeam');
			NonAsyncAjaxUpdater ( ASP.viewdoc_aspx.EditTeam(), 'uiTeamLink' );
			if ( teamId != -1 ) {
			    $('uiTeamLink').href = "team.aspx?tid=" + teamId;
			}
			NonAsyncAjaxUpdater ( ASP.viewdoc_aspx.RebindAuthors( $F(element) ), author );
			
			Element.hide( 'EditTeam' );
			Element.show( 'Team' );
		}
	,
	
	    startPublish : function ( location ) {
	        Effect.BlindDown('uiPublishToInfoBaseOptions', { duration: ViewDoc.effectDuration } ); 
	        $('uiInfobasePublishWhere').value= location;
	        
	    }
	,
	    publishInfoBaseDocument : function ( ) {
	        
	        NonAsyncAjaxHelper ( ASP.viewdoc_aspx.PublishInfobaseDocument ( $('uiFrontPageYes').checked, $('uiUpdatePublishYes').checked, $F('uiInfobasePublishWhere') ), 
	            function ( result ) {
	                var where = $F('uiInfobasePublishWhere');
	                var button = $('uiPublishTo' + where + 'Button' );
	                button.innerHTML = 'Remove from ' + where;
	                button.href = 'javascript: ViewDoc.unpublishInfoBase ( \'' + where +'\' ); void 0;';
	                $('lblPublishDate').innerHTML = result.value.getDate() + '/' + (result.value.getMonth() + 1) + '/' + result.value.getFullYear();
	                $('span' + where ).innerHTML = 'Published to ' + where;
	            }
	        );
	        ViewDoc.finishInfoBasePublish();
	    }
	,
	    finishInfoBasePublish : function () {
	        Effect.BlindUp ( 'uiPublishToInfoBaseOptions', { duration: ViewDoc.effectDuration } );
	    }
	,
	    unpublishInfoBase : function ( where ) {
	        if(!confirm("Are you sure you want to remove this document from " + where)) return;
	        NonAsyncAjaxHelper ( ASP.viewdoc_aspx.UnpublishInfobase ( where ),
	            function ( result ) {
	                var button = $('uiPublishTo' + where + 'Button' );
	                button.innerHTML = 'Publish to ' + where;
	                button.href = 'javascript: ViewDoc.startPublish ( \'' + where +'\' ); void 0;';
	                $('span' + where ).innerHTML = 'Not Published to ' + where;
	            }
	        );
	    }
	,
	    startWebPublish : function ( ) {
	        Effect.BlindDown ( 'uiPublishToPublicOptions', { duraction: ViewDoc.effectDuration } );
	    }
	,
	    publishWebDocument : function ( ) {
	        if ( 0 + $F('uiWebsiteSection') == 0 ) { alert ( 'Please select a Section' ); return; }
	        if ( 0 + $F('uiWebsiteType') == 0 ) {  alert ( 'Please select a Type' ); return; }
	        NonAsyncAjaxHelper ( ASP.viewdoc_aspx.PublishPublicDocument ( 
	        //int sectionId,            int typeId,             bool paperAvailable,                string paperCost,       bool videoAvailable,          string videoCost,     bool cdAvailable,          string cdCost,       bool audioAvailable,            bool audioCost
	        0 + $F(  'uiWebsiteSection'), 0 + $F('uiWebsiteType'), $('uiPaperCopyAvailable').checked, $F('uiPaperCopyCost'), $('uiVideoAvailable').checked, $F('uiVideoCost'), $('uiCdRomAvailable').checked, $F('uiCdRomCost'), $('uiAudioAvailable').checked, $F('uiAudioCost') ),
	            function ( result ) {
	                $('uiPublishToWebsiteButton').innerHTML = 'Remove From Public';
	                $('uiPublishToWebsiteButton').href = "javascript: ViewDoc.unpublishWebDocument ( true ); void 0;";
	                $('spanPublic' ).innerHTML = 'Published to Public<br/>(Pending Approval)';
	            }
	        );
	        ViewDoc.finishWebPublish ();
        }
	,
	    finishWebPublish : function () {
	        Effect.BlindUp ( 'uiPublishToPublicOptions', { duraction: ViewDoc.effectDuration } );
	    }
	,
	    unpublishWebDocument : function ( doConfirm ) {
	        if( doConfirm && !confirm("Are you sure you want to remove this document from Public") ) return;
	        NonAsyncAjaxHelper ( ASP.viewdoc_aspx.UnpublishPublicDocument ( ),
	            ViewDoc.itemNotPublishedToWeb
	        );
	    }
	,
	    itemNotPublishedToWeb : function ( result ) {
	                $('uiPublishToWebsiteButton').innerHTML = 'Publish To Public';
	                $('uiPublishToWebsiteButton').href = "javascript: ViewDoc.startWebPublish (); void 0;";
	                $('spanPublic' ).innerHTML = 'Not Published to Public';
	            }
	,
	    rejectWebDocument : function () { 
	        if ( NonAsyncAjaxHelper ( ASP.viewdoc_aspx.RejectWebDocument ( $F('uiRejectionReason' ) ), ViewDoc.itemNotPublishedToWeb ) ) {
	            Effect.BlindUp ( 'ApprovalToolbar', { duraction: ViewDoc.effectDuration } ); 
	        }
	        ViewDoc.finishRejectWebDocument ();
	    }
	,
	    finishRejectWebDocument : function () { 
	        Effect.BlindUp ( 'uiRejectWebOptions', { duraction: ViewDoc.effectDuration } ); 
	    },
	    approveWebDocument : function () { 
	        NonAsyncAjaxHelper ( ASP.viewdoc_aspx.ApproveDocumentForWeb(), 
	            function ( result ) { 
	                $('spanPublic' ).innerHTML = "Published To Web"; 
	                Effect.BlindUp ( 'ApprovalToolbar', { duraction: ViewDoc.effectDuration } ); 
	            } ); 
	        
	    }
	, 
	    startRejectWebDocument : function () { 
	        Effect.BlindDown ( 'uiRejectWebOptions', { duraction: ViewDoc.effectDuration } ); 
	    }
	,
		publishDocument : function (publishLocation) {
		
			if(confirm("Are you sure you want to publish to " + publishLocation))
			{
				var isMain;
				switch(publishLocation) {
				case "InfoBase":
					isMain = confirm("Do you want this document to appear on the front page of InfoBase?");
					break;
				case "Schools":
					isMain = confirm("Do you want this document to appear on the front page of InfoBase Schools?");
					break;
				}
				var element = $( 'span' + publishLocation )
				NonAsyncAjaxHelper ( ASP.viewdoc_aspx.PublishDocument(publishLocation, isMain), function ( result ) {
				    var s = result.value;
				    if(s=='true') {
					    element.innerHTML = "<span onclick='ViewDoc.unPublishDocument(\"" + publishLocation + "\")'>Published on " + publishLocation + "[x]</span>";
				    }
				} );
			}
			
		}
	,
		unPublishDocument : function (publishLocation) {
			if(confirm("Are you sure you want to remove this document from " + publishLocation))
			{
				var element = $( 'span' + publishLocation )
				var s = ASP.viewdoc_aspx.PublishDocument(publishLocation, false).value;
				if(s == 'true')
				{
					element.innerHTML = "<span onclick='ViewDoc.publishDocument(\"" + publishLocation + "\")'>Publish to " + publishLocation + "</span>";
				}
			}
			
		}
	}


