// ----------------------
// Web service calls
// ----------------------
function moveContent(containerId, contentId, type, redirectUrl, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/MoveContent",
            data: "containerId=" + containerId + "&contentId=" + contentId + "&type=" + type,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res) 
            {
				if(!DisplayError(res)){
					window.location.href = redirectUrl;
				}
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}

function editContent(contentId, title, desc, profit, type, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/EditContent",
            type: "POST",
            data: "contentId=" + contentId + "&title=" + title + "&desc=" + desc + "&profit=" + profit + "&type=" + type,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res) 
            {
                if(!DisplayError(res)){
					ShowHide('EditTitle','ShowTitle');
					ShowHide('EditDescription','ShowDescription');
					ShowHide('EditDownload','ShowDownload')
                }
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}    
    
function setTags(tags, contentId, type, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/SetTags",
			type: "POST",
            data: "tags=" + tags + "&contentId=" + contentId + "&type=" + type,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res) 
            {
                if(!DisplayError(res)){
					getTags(contentId, type, networkErrorMessage);
					ShowHide('EditTags','ShowTags');
                }
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}

function setCatSubcat(containerId, containerTitle, containerDesc, catId, subcatId, cat, subcat, type, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/SetCatSubcat",
			type: "POST",
            data: "containerId=" + containerId + "&containerTitle=" + containerTitle + "&containerDesc=" + containerDesc + "&catId=" + catId + "&subcatId=" + subcatId,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res)
            {
				if(!DisplayError(res)){
					var divToRender = '#divCatSubcat';
					var photoCatUrl = '/Media/BrowseContent.aspx?node=photos&amp;CategoryId=' + catId;
					var videoCatUrl = '/Media/BrowseContent.aspx?node=videos&amp;CategoryId=' + catId;
					var photoCatHTML = '<a href=\"' + photoCatUrl + "\">" + cat + "</a>";
					var videoCatHTML = '<a href=\"' + videoCatUrl + "\">" + cat + "</a>";
	                
					var photoSubCatUrl = '/Media/BrowseContent.aspx?node=photos&amp;CategoryId=' + catId + "&amp;SubCategoryId=" + subcatId;
					var videoSubCatUrl = '/Media/BrowseContent.aspx?node=videos&amp;CategoryId=' + catId + "&amp;SubCategoryId=" + subcatId;
					var photoSubCatHTML = '<a href=\"' + photoSubCatUrl + "\">" + subcat + "</a>";
					var videoSubCatHTML = '<a href=\"' + videoSubCatUrl + "\">" + subcat + "</a>";
	                
					if (type == 'Photo')           
						$(divToRender).html(photoCatHTML + "&nbsp;/&nbsp;" + photoSubCatHTML);
					else
						$(divToRender).html(videoCatHTML + "&nbsp;/&nbsp;" + videoSubCatHTML);
					ShowHide('EditCategories','ShowCategories');
				}
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}

function getTags(contentId, type, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/GetTags",
			type: "POST",
            data: "contentId=" + contentId + "&type=" + type,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res)
            {
				if(!DisplayError(res)){
					$(res).find("Message").each(function() {
						var divToBeWorkedOn = '#divTagsList';
						var html = $(this).text();
						$(divToBeWorkedOn).html(html);
						});
				}
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}

function updateContentCredits(contentId, type, profit, controlIdToUpdate, divIdToHideOnSuccess, divIdToShowOnSuccess, refreshCreditsCallback, networkErrorMessage) //contentId, type, profit, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/SaveDownloadProfit",
			type: "POST",
            data: "contentId=" + contentId + "&type=" + type + "&profit=" + profit,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res)
            {
				if(!DisplayError(res)){
					//...no error...
					document.getElementById(controlIdToUpdate).innerHTML = profit;
					ShowHide(divIdToHideOnSuccess, divIdToShowOnSuccess);
					refreshCreditsCallback();
					}
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}

function setWatermark(hasWatermark, contentId, type, networkErrorMessage)
{
    $.ajax({ url: "/WebServices/Media/Remote/UpdateMediaContentService.asmx/SetWatermark",
			type: "POST",
            data: "hasWatermark=" + hasWatermark + "&contentId=" + contentId + "&type=" + type,
            contentType: "application/x-www-form-urlencoded",
            processData: false,
            dataType: "xml",
            success: function(res) 
            {
                if(!DisplayError(res)){
					location.reload(true);
                }
            },
            error: function() 
            {
                DisplayErrorMessage(networkErrorMessage);
            }
    });
}
// ----------------------
// End Web service calls
// ----------------------

// ----------------------
// Error message handling
// ----------------------
function DisplayError(ajaxRes){
	var error = null;
	$(ajaxRes).find("IsError").each(function(){error = $(this).text(); });
	if (error == 0){
		//...no error...
		return false;
	}
	//ERROR
	else{
		$(ajaxRes).find("Message").each(function() {
								DisplayErrorMessage($(this).text());
							});
		if(error == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function DisplayErrorMessage(errorMessage){        
		$("#ErrorPopUp").attr("style", "display:block");
		$("#ErrorPopUpMsg").html(errorMessage); 
		LayoutErrorPopUp();
}
// --------------------------
// End Error message handling
// --------------------------

// --------------------------
// Helpers
// --------------------------
function CollapseExpand(divToCollapse, divToExpand, imgCollapseExpand, ExpandTooltip, CollapseTooltip){
	var a=document.getElementById(divToExpand); 
	var b=document.getElementById(divToCollapse); 
	var img=document.getElementById(imgCollapseExpand); 
	if(a.style.display == "none")
	{
		//expand:
		a.style.display="block";
		b.style.display="none";
		img.src = "c_images/collapse.gif";
		img.alt = CollapseTooltip;
		img.title = CollapseTooltip;
	}
	else
	{
		//collapse:
		a.style.display="none";
		b.style.display="block";
		img.src = "c_images/expand.gif";
		img.alt = ExpandTooltip;
		img.title = ExpandTooltip;
	}
	return false;
}

function CollapseExpand2(divToCollapseExpand, imgCollapseExpand, ExpandTooltip, CollapseTooltip){
	var a=document.getElementById(divToCollapseExpand); 
	var img=document.getElementById(imgCollapseExpand); 
	if(a.style.height == "100%")
	{
		//collapse:
		a.style.height="17px";
		img.src = "c_images/expand.gif";
		img.alt = ExpandTooltip;
		img.title = ExpandTooltip;
	}
	else
	{
		//expand:
		a.style.height="100%";
		img.src = "c_images/collapse.gif";
		img.alt = CollapseTooltip;
		img.title = CollapseTooltip;
	}
	return false;
}

<!-- REPLACE DIV CONTENT -->
function ShowHide(divHide,divShow){
	var a=document.getElementById(divHide); 
	var b=document.getElementById(divShow); 
	a.style.display="none";
	b.style.display="block";
	return false;
}

function escapeHTML(str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}

function findAllOpenEditsAndSaveContent(evt){
    // process only the Enter key
    if (evt.keyCode == 13)
    {
        // cancel the default submit
        evt.returnValue=false;
        evt.cancel = true;
        
        // for specific Edit div, specific saving function  
        if(document.getElementById('EditTitle').style.display == "block")
	        actionTitleSaveContentDetails();
        if(document.getElementById('EditDescription').style.display == "block")
	        actionDescriptionSaveContentDetails();
        if(document.getElementById('EditAlbumInfo').style.display == "block")
	        actionAlbumInfoSaveContentDetails();
        if(document.getElementById('EditCategories').style.display == "block")
	        actionCategorySubcategorySaveContentDetails();
        if(document.getElementById('EditTags').style.display == "block")
	        actionTagsSaveContentDetails();
        if(document.getElementById('EditDownload').style.display == "block")
	        actionDownloadProfitSaveContentDetails();

     }
	return false;
}

// --------------------------
// End Helpers
// --------------------------

// -------------------------------
// Move To
// -------------------------------
function moveContentTo(strIsModerated, moveToControlId, currentContainerId, contentId, contentType, redirectUrl, networkErrorMessage)
{
	if (strIsModerated.toLowerCase() == 'false')
	{
		if (document.getElementById(moveToControlId).value == currentContainerId)
			ShowHide('EditAlbumInfo','ShowAlbumInfo');
		else
			moveContent(document.getElementById(moveToControlId).value, contentId, contentType, redirectUrl, networkErrorMessage);
	}
	return false;
}
// -------------------------------
// End Move To
// -------------------------------

// -------------------------------
// Category / Subcategory
// -------------------------------
function save_category(categoryControlId, subCategoryControlId, invalidCategoryControlId, invalidSubCategoryControlId, containerId, containerTitle, containerDescription, contentType, networkErrorMessage)
{
	var guidEmpty = '00000000-0000-0000-0000-000000000000';
	var guidCat = guidEmpty;
	if(document.getElementById(categoryControlId).selectedIndex >= 0)
		guidCat = document.getElementById(categoryControlId).options[document.getElementById(categoryControlId).selectedIndex].value;
	var guidSubcat = guidEmpty;
	if(document.getElementById(subCategoryControlId).selectedIndex >=0)
		guidSubcat = document.getElementById(subCategoryControlId).options[document.getElementById(subCategoryControlId).selectedIndex].value;
    
	if (guidCat == guidEmpty)
		document.getElementById(invalidCategoryControlId).innerHTML = '*' ;
        
	if (guidSubcat == guidEmpty)
		document.getElementById(invalidSubCategoryControlId).innerHTML = '*' ;
    
	if (guidCat != guidEmpty && guidSubcat != guidEmpty)
		setCatSubcat(containerId, containerTitle, containerDescription, guidCat, guidSubcat, document.getElementById(categoryControlId).options[document.getElementById(categoryControlId).selectedIndex].text, document.getElementById(subCategoryControlId).options[document.getElementById(subCategoryControlId).selectedIndex].text, contentType, networkErrorMessage);
}
// -------------------------------
// End Category / Subcategory
// -------------------------------

// --------------------
// Download Profit
// --------------------
function saveProfit(contentId, contentType, downloadProfitControlId, networkErrorMessage)
{
	var contentId = contentId;
	var type = contentType;
	var profit = document.getElementById(downloadProfitControlId).value;
    updateContentCredits(contentId, type, profit, 'spanProfit', 'EditDownload','ShowDownload', cbRefreshDownload, networkErrorMessage);
}
							
function refreshDownload(downloadProfitDropDownId, disabledDownloadProfit)
{
	var profit = document.getElementById(downloadProfitDropDownId).value;
	if (profit == disabledDownloadProfit)
		{
			document.getElementById('spanDownloadEnabled').style.display = 'none';
			document.getElementById('spanDownloadDisabled').style.display = 'block';
		}
	else
		{
			document.getElementById('spanDownloadEnabled').style.display = 'block';
			document.getElementById('spanDownloadDisabled').style.display = 'none';
			document.getElementById('spanProfit').innerHTML = profit;
		}
}
// --------------------
// End Download Profit
// --------------------

// -------------------------
// Edit Content
// -------------------------
function editDescription(contentId, contentType, contentTitleControlId, contentDescriptionControlId, downloadProfitControlId, networkErrorMessage)
{
	var_1 = contentId;
	var_2 = document.getElementById(contentTitleControlId).value;
	var_3 = document.getElementById(contentDescriptionControlId).value;
	var_4 = document.getElementById(downloadProfitControlId).value;
	var_5 = contentType;
   
	editContent(var_1, var_2 , var_3, var_4, var_5, networkErrorMessage); 
	
	var s = document.getElementById(contentDescriptionControlId).value;
    while (s.indexOf('\n') > -1)
        s = s.replace('\n','<br />');
	document.getElementById('myCollapsableText').innerHTML = s;
	document.getElementById('collapseComment').innerHTML = escapeHTML(document.getElementById(contentDescriptionControlId).value);
}
// -------------------------
// End Edit Content
// -------------------------
