﻿// ***********************************************************************
// client scripts and Ajax calls for User Profile Page (UserProfile.aspx)
// ***********************************************************************

function ConfirmationFavourite(add)
{
    var textAdd = 'Please confirm whether you want to add this upload to your favorites.';
    var textRemove = 'Please confirm whether you want to remove this upload from your favorites.';

    return add ? confirm(textAdd) : confirm(textRemove);
}

function ConfirmationSubscription(add)
{
    var textAdd = 'Please confirm whether you want to subscribe to this upload area.';
    var textRemove = 'Please confirm whether you want to unsubscribe from this upload area.';

    return add ? confirm(textAdd) : confirm(textRemove);
}

// hide links in the link lists (LinkList.ascx)
function HideLink(divName)
{
    var divLink = document.getElementById(divName);
    divLink.style.display = "none";
}

// delete link from favourites
function DeleteFavouriteLink(pageID)
{
    var confirmation = confirm('Please confirm whether you want to remove this item from your favorites.');
    if(confirmation){
        TMVS.Services.ClientServices.DeleteFavouriteLink(pageID, onDeleteFavouriteCompleted, onError);
    }
}

// callback of delete favourites
function onDeleteFavouriteCompleted(result, userContext)
{
    var divLink = document.getElementById(result+'_linksFavourites');
    divLink.style.display = "none";
    var parent = divLink.parentNode;
    var hideParent = true;
    var childDivs = parent.getElementsByTagName("div");
    for( var x = 0; x < childDivs.length; x++ )
    {
        if( (childDivs[x].className!="header") && ( childDivs[x].style.display != "none"))
           hideParent = false; 
    }
    if(hideParent)
        parent.style.display = "none";
    //hide the favourites section if none are displayed
}

// delete link from favourites
function UnsubscribePage(pageID)
{
    var confirmation = confirm('Please confirm whether you want to remove this item from your subscriptions.');
    if(confirmation){
        TMVS.Services.ClientServices.UnsubscribePage(pageID, onUnsubscribeCompleted, onError);
    } 
}

// callback of delete favourites
function onUnsubscribeCompleted(result, userContext)
{
    var divLink = document.getElementById(result+'_linksSubscriptions');
    divLink.style.display = "none";
    var parent = divLink.parentNode;
    var hideParent = true;
    var childDivs = parent.getElementsByTagName("div");
    for( var x = 0; x < childDivs.length; x++ )
    {
        if( (childDivs[x].className!="header") && ( childDivs[x].style.display != "none"))
           hideParent = false; 
    }
    if(hideParent)
        parent.style.display = "none";
}

// error message if the delete favourites fails
function onError(result)
{
    alert('Error occured while calling webservice'+result);
}


// ***************************************************************************
// client scripts for Uploaded Item Category page (UploadedItemCategory.aspx)
// ***************************************************************************

function ShowHideUploader(youTube)
{
    if (youTube)
    {
        var trFileUpload = document.getElementById('trFileUpload');
        trFileUpload.style.display = "none";
    }
    var divUploader = document.getElementById('divUploader');
    divUploader.style.display = divUploader.style.display == "" ? "none" : "";
}

function openYouTubeWindow(parentId)
{
    var winl = (screen.width - 490) / 2;
    var wint = (screen.height - 240) / 2;
    if(confirm('The video will be uploaded to YouTube and will be public. Continue?'))
        window.open("/templates/LearningCenter/Pages/YouTubeForm.aspx?ParentId="+parentId, "youTubeUpload", "width=490px,height=240px,top="+wint+",left="+winl, false);
}
/* Text area char limiting*/

function TextareaKeyPress(event)
 {
 	//get pointer to text control:
 	var objTextArea=GetTextArea();
 	
 	//get value:
 	var strValue=objTextArea.value;
 	
 	//abort if no max chars defined:
 	if (!objTextArea.attributes['MaxChars'])
 		return false;
 	
 	//get max chars:
 	var maxChars=parseInt(objTextArea.attributes['MaxChars'].value);
 	
 	//check if exceeded:
 	if (strValue.length >= maxChars) {
 		//check if key pressed was arrow, backspace or such:
 		if ((!event)||(!event.keyCode))
 			return false;
 		
 		var arrAllowedChars=new Array(8, 9, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46);
 		return InArray(arrAllowedChars, event.keyCode);
 	}
 	
 	return true;
 }
 
 function TextareaKeyUp(event)
 {
 
 	//get pointer to text control:
 	var objTextArea=GetTextArea();
 	
 	//get id of control:
 	var strID=objTextArea.name+"_charsCount";
 	
 	//get pointer to text container:
 	var objSpan=document.getElementById(strID);
 	
 	//abort if not defined:
 	if (!objSpan)
 		return false;
 
 	//get value:
 	var strValue=objTextArea.value;
 	
 	//abort if no max chars defined:
 	if (!objTextArea.attributes['MaxChars'])
 		return false;
 	
 	//get max chars:
 	var maxChars=parseInt(objTextArea.attributes['MaxChars'].value);
 	
 	//check if exceeded:
 	if (strValue.length >= maxChars) {
 		if (_lastValue.length != strValue.length) {
 			//crop to maximum size:
 			objTextArea.value = strValue.substr(0, maxChars);
 		}
 		strValue=objTextArea.value;
 	}
 	
 	//store last value:
 	_lastValue = strValue;
 	
 	//set container text:
 	objSpan.innerHTML = (maxChars-strValue.length)+" chracters left.";
 }
 
 function TextareaPaste(event)
 {
 	//initiate keypress and keyup events:
 	var s1=setTimeout("TextareaKeyPress();", 100);
 	var s2=setTimeout("TextareaKeyUp();", 500);
 }
 
 function InArray(arr, key) {
 	for (var i=0; i<arr.length; i++) {
 		if (arr[i] == key)
 			return true;
 	}
 	return false;
 }
