﻿var rateWin;
function IsImage(fileName)
{
	var extent = GetExtension(fileName).toLowerCase();
	if(extent==".jpg" || extent==".jpeg" || extent==".gif" || extent==".bmp")
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function IsMusic(fileName)
{
	var extent = GetExtension(fileName).toLowerCase();
	if(extent==".mp3" || extent==".mid" || extent==".midi" || extent==".wav")
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function GetExtension(fileName)
{
	if(fileName=="")
		return "";
	var pos = fileName.lastIndexOf('.');
	if(pos<0)
		return "";
	else
		return fileName.substring(pos, fileName.length);
}


function GetNumImageSelect()
{
	var i;
	var numImageSelect	= 0;
	var images = document.getElementsByName("chkFile");
	for(i=0; images!=null && i<images.length; ++i)
	{
		if(images[i].checked)
		{
			if(IsImage("" + images[i].nextSibling.value))
				numImageSelect++;
		}	
	}
	return numImageSelect;
}


// check if the folder contains images
function GetNumImages()
{
	var i;
	var numImageSelect	= 0;
	var images = document.getElementsByName("chkFile");
	for(i=0; images!=null && i<images.length; ++i)
	{
		if(IsImage("" + images[i].nextSibling.value))
			numImageSelect++;		
	}
	return numImageSelect;
}

function GetNumMusics()
{
	var i;
	var numMusics	= 0;
	var musics = document.getElementsByName("chkFile");
	for(i=0; musics!=null && i<musics.length; ++i)
	{
		if(IsMusic("" + musics[i].nextSibling.value))
			numMusics++;		
	}
	return numMusics;
}

function OpenSlideShow()
{

	if(includeIn=="folder")
	{
		var numImages = GetNumImages();
		
		if(numImages < 2) {
			alert("A folder must have at least 2 images to run Slide Show!");
			return;
		}
	}	
		
	var pLeft = (window.screen.availWidth - 750)/2;
	var pTop  = (window.screen.availHeight - 550)/2;
	
	var url = "/file/PowerSlideShow.aspx?";
	if(share!="")
		url = url + "&share=" + share;
	if(isGallery=="true")
		url = url + "&isGallery=true";
	if(shareID>0)
		url = url + "&shareID=" + shareID;
	if(parentID>0)
		url = url + "&parentID=" + parentID;	
	
	window.open (url, 'newwindow' + shareID + parentID, 'height=600, width=800, toolbar=no, left=' + pLeft + ', top=' + pTop + ', menubar=no, scrollbars=yes, resizable=yes,location=no, status=no') ;

}

function CheckClicked()
{
	var i;
	if(document.ShowFolderID.selectAllFlag.value=="folder" ) {
		for(i=0; document.ShowFolderID.FileChecks!=null && i<document.ShowFolderID.FileChecks.length; ++i)
		{
			if(!document.ShowFolderID.FileChecks[i].checked) {
				if(window.confirm("This will cancel \"Select In All Pages\"?"))
					document.ShowFolderID.selectAllFlag.value=="";
				else
					document.ShowFolderID.FileChecks[i].checked = true;
			}
		}
	
		for(i=0; document.ShowFolderID.FolderChecks!=null && i<document.ShowFolderID.FolderChecks.length; ++i)
		{
			if(!document.ShowFolderID.FolderChecks[i].checked) {
				if(window.confirm("This will cancel \"Select In All Pages\"?"))
					document.ShowFolderID.selectAllFlag.value=="";
				else
					document.ShowFolderID.FolderChecks[i].checked = true;
			}
		}
	
	}

}

function GetNumFileSelect()
{
	var i;
	var numFileSelect	= 0;
	var allFiles		= document.getElementsByName("chkFile");
	for(i=0; allFiles!=null && i<allFiles.length; ++i)
	{
		if(allFiles[i].checked) 
			numFileSelect++;
	}
	return numFileSelect;
}

function GetNumFolderSelect()
{
	var i;
	var numFolderSelect	= 0;
	var allFolders		= document.getElementsByName("chkFolder");
	for(i=0; allFolders!=null && i<allFolders.length; ++i)
	{
		if(allFolders[i].checked) 
			numFolderSelect++;
	}
	return numFolderSelect;
}

function GetCheckedFileID()
{
	var i;
	var allFiles		= document.getElementsByName("chkFile");
	for(i=0; allFiles!=null && i<allFiles.length; ++i)
	{
		if(allFiles[i].checked) 
			return allFiles[i].value;
	}
	return "0";
}

function GetCheckedFolderID()
{
	var i;
	var allFiles		= document.getElementsByName("chkFolder");
	for(i=0; allFiles!=null && i<allFiles.length; ++i)
	{
		if(allFiles[i].checked) 
			return allFiles[i].value;
	}
	return "0";
}

function filterFileNameForURL(filename)
{
	while(filename.indexOf("&")>=0)
		filename = filename.replace('&', 'A');
	while(filename.indexOf("%")>=0)
		filename = filename.replace('%', 'P');
	while(filename.indexOf("#")>=0)
		filename = filename.replace('#', 'N');
	return filename;

}


function Action(action)
{
	var numImageSelect	= GetNumImageSelect();
	var numFileSelect	= GetNumFileSelect();
	var numFolderSelect = GetNumFolderSelect();
	var numImages = GetNumImages();

	if (action == "down") {
	    if (includeIn == "folder") {
	        if (numFolderSelect > 0) {
	            if (confirm("Browser only supports download files. DriveHQ FileManager makes download folders much easier. Do you want to install it?")) {
	                var newwin = window.open("/downloads/DownloadFileManager.aspx");
	                if (newwin == null)
	                    alert("A pop window that you requested was blocked by 3rd party software on your computer!");
	                return;
	            }
	            else
	                return;
	        }

	        if (numFileSelect > 1) {

	            if (confirm("Your browser only supports downloading files one-by-one. DriveHQ FileManager makes upload and download much easier. Do you want to install it?")) {
	                var newwin = window.open("/downloads/DownloadFileManager.aspx");
	                if (newwin == null)
	                    alert("A pop window that you requested was blocked by 3rd party software on your computer!");
	                return;
	            }
	            else
	                return;

	        }
	        else if (numFileSelect == 0) {
	            alert("Please select a file to download.");
	            return;
	        }

	        var selectedFileID = GetCheckedFileID();
	        if (selectedFileID > 0) {
	            var url = dfURL.replace("{fileid}", selectedFileID) + "&forcedDownload=true";
	            document.location = url;
	        }
	    }
	    else {
	        document.location = dfURL + "&forcedDownload=true";
	    }
	    return;
	} else if (action == "zipdown") {
	    if (numFileSelect == 0 && numFolderSelect == 0) {
	        alert("Please select files/folders that you want to download!");
	        return;
	    }
	    var fileIDs = "";
	    var folderIDs = "";
	    var files = document.getElementsByName("chkFile");
	    for (i = 0; files != null && i < files.length; ++i) {
	        if (files[i].checked) {
	            fileIDs += files[i].value + ",";
	        }
	    }

	    var folders = document.getElementsByName("chkFolder");
	    for (i = 0; folders != null && i < folders.length; ++i) {
	        if (folders[i].checked) {
	            folderIDs += folders[i].value + ",";
	        }
	    }

	    if (fileIDs != "" || folderIDs != "") {
	        var url = dfURL + "&zipdownload=true&fileIDs=" + fileIDs + "&folderIDs=" + folderIDs;
	        top.document.location = url;
	    }
	    return;
	}
	else if (action == "selectoneimage" || action == "selectimages") {
	    if (numImageSelect == 0 && action == "prints") {
	        alert("Please select photos for prints!");
	        return;
	    }
	    else if (action == "prints" && (numFileSelect - numImageSelect > 0 || numFolderSelect > 0)) {
	        alert("Please select only photos for prints!");
	        return;
	    }
	    else if (numImageSelect == 0 && action == "selectoneimage") {
	        alert("Please select one image!");
	        return;
	    }
	    else if (numImageSelect == 0 && action == "selectimages") {
	        alert("Please select images!");
	        return;
	    }
	    else if (numImageSelect > 1 && action == "selectoneimage") {
	        alert("Please select only one image!");
	        return;
	    }
	    else if ((numFileSelect - numImageSelect > 0 || numFolderSelect > 0) &&
				!document.ShowFolderID.selectAllFlag.value == "folder") {
	        alert("Please select image(s) only!");
	        return;
	    }
	}
	else if (action == "selectonefile" || action == "selectfiles") {
	    if (numFileSelect == 0 && action == "selectonefile") {
	        alert("Please select one file!");
	        return;
	    }
	    else if (numFileSelect == 0 && action == "selectfiles") {
	        alert("Please select files!");
	        return;
	    }
	    else if (numFileSelect > 1 && action == "selectonefile") {
	        alert("Please select only one file!");
	        return;
	    }
	    else if (numFolderSelect > 0) {
	        alert("Please select file(s) only.");
	        return;
	    }
	}
	else if (action == "selectonefolder" || action == "selectfolders") {
	    if (numFolderSelect == 0 && (action == "selectonefolder")) {
	        alert("Please select one folder!");
	        return;
	    }
	    else if (numFolderSelect == 0 && action == "selectfolders") {
	        alert("Please select folders!");
	        return;
	    }
	    else if (numFolderSelect > 1 && (action == "selectonefolder")) {
	        alert("Please select only one folder!");
	        return;
	    }
	    else if (numFileSelect > 0) {
	        alert("Please select folder(s) only");
	        return;
	    }
	}
	else if (action == "select" || action == "copy" || action == "cut" || action == "delete") {
	    if (includeIn == "folder") {
	        if (numFileSelect == 0 && numFolderSelect == 0) {
	            alert("Please select files or folders.");
	            return;
	        }

	        if (action == "delete") {

	            var folderLib1 = "|foldermy documents|folderwwwhome|folderpublicfolder|folderrecycle bin|";
	            var folderLib2 = "|folderdrivehqdata|";
	            var folderLib3 = "|\\drivehqdata\\|\\my documents\\my encrypted data\\|";
	            var parentFolder = inputParentPath.value;
	            if (parentFolder.toLowerCase() == "\\") {
	                var sysNum = 0, proNum = 0;
	                var folders = document.getElementsByName("chkFolder");
	                for (var i = 0; folders != null && i < folders.length; ++i) {//alert(folders[i].id.toLowerCase());
	                    if (folders[i].checked) {
	                        if (folderLib1.indexOf("|" + folders[i].parentNode.childNodes[1].value.toLowerCase() + "|") >= 0)
	                            sysNum++;
	                        if (folderLib2.indexOf("|" + folders[i].parentNode.childNodes[1].value.toLowerCase() + "|") >= 0)
	                            proNum++;
	                    }
	                }
	                var result;
	                if (sysNum > 0) {
	                    if (sysNum == 1)
	                        result = confirm("It is not recommended to delete a System Default folder. Are you sure to delete it?");
	                    else
	                        result = confirm("It is not recommended to delete System Default folders. Are you sure to delete them?");
	                    if (!result)
	                        return;
	                }
	                if (proNum > 0) {
	                    if (proNum == 1)
	                        result = window.confirm("The folder contains important application data or backup data, are you sure to delete it?");
	                    else
	                        result = window.confirm("These folders contains important application data or backup data, are you sure to delete them?");
	                    if (!result)
	                        return;
	                    result = confirm("After you delete the folder(s), DriveHQ client applications may experience some problem.");
	                    if (!result)
	                        return;
	                }
	                else {
	                    if (!window.confirm("Are you sure to delete the selected files and/or folders?"))
	                        return;
	                }
	            }
	            else {
	                if (folderLib3.indexOf("|" + parentFolder.toLowerCase() + "|") >= 0) {
	                    var result;
	                    result = window.confirm("This folder contains important application data or backup data, are you sure to delete something in it?");
	                    if (!result)
	                        return;
	                    result = window.confirm("After you delete the file(s) or folder(s), DriveHQ client applications may experience some problem.");
	                    if (!result)
	                        return;
	                }
	                else {
	                    if (!window.confirm("Are you sure to delete the selected files and/or folders?"))
	                        return;
	                }
	            }
	        }
	    }
	}
	else if (action == "share" || action == "publish") {
	    if (numFileSelect > 0 || numFolderSelect > 1) {
	        alert("Please select one folder only!");
	        return;
	    }

	    if (numFolderSelect < 1) {
	        alert("Please select a folder to " + action + "!");
	        return;
	    }

	    var selectedFolderID = GetCheckedFolderID();

	    if (action == "publish")
	        document.location = "/sharing/SetPermission.aspx?publish=true&action=load&objTypeID=0&objID=" + selectedFolderID;
	    else
	        document.location = "/sharing/SetPermission.aspx?share=true&action=load&objTypeID=0&objID=" + selectedFolderID;

	    return;
	}
	else if (action == "folder") {
	    var url = "/file/NewEditFolder.aspx?action=load";
	    if (share != "")
	        url = url + "&share=" + share;
	    if (shareID > 0)
	        url = url + "&shareID=" + shareID;
	    url = url + "&parentID=" + parentID;
	    document.location = url;
	    return;
	}
	else if (action == "file") {
	    var url = "/file/EditFile.aspx?action=load&isNew=true";
	    if (share != "")
	        url = url + "&share=" + share;
	    if (shareID > 0)
	        url = url + "&shareID=" + shareID;
	    url = url + "&parentID=" + parentID;
	    document.location = url;
	    return;
	}
	document.getElementById("FolderAction").value = action;
	if(action=="delete"||action=="paste")
	{
		ShowProgress(action);
		return;  
	}
	btnAction.click();
}



function ShowProgress(action)
{
    var container = document.getElementById("divStatus");
    if(container)
    {
		var frmStatus	= document.createElement("iframe");
		frmStatus.setAttribute("frameborder", "0");
		frmStatus.setAttribute("scrolling","no");
		frmStatus.setAttribute("src","/file/ActionProgress.aspx?progressID="+SetProgressID()+"&action="+action);
		frmStatus.frameBorder = 0;
		frmStatus.style.borderWidth = 0;
		frmStatus.style.width = 400;
		frmStatus.style.height = 150;
		container.style.display = "";
		container.style.left = 250;
		container.style.top = 300;
		if(container.childNodes.length<=1)
		{
			container.appendChild(frmStatus); 
		}
    }
}

function SetProgressID()
{
    var left = Math.round(Math.random() * 100000);
    var right = Math.round(Math.random() * 100000);
    var progressID = "" + left + right;
    document.getElementById("ProgressID").value= progressID;
    return progressID;
}

function DoSelectAll()
{	
	var isCheck;
	var checkValue = "" + document.getElementById("SelectAll").value;
	if(checkValue.toLowerCase()=="true")
		isCheck = false;
	else
		isCheck = true;	
	var i;
	var allFiles		= document.getElementsByName("chkFile");
		
	for(i=0; allFiles!=null && i<allFiles.length; ++i)
	{
		allFiles[i].checked = isCheck;
		if(view=="detail")
		{
			if(isCheck)
			{
				if(allFiles[i].parentNode.parentNode.className)
					allFiles[i].parentNode.parentNode.className = "SelectedRow";
				allFiles[i].parentNode.parentNode.setAttribute("class", "SelectedRow");
			}
			else
			{
				if(allFiles[i].parentNode.parentNode.className)
					allFiles[i].parentNode.parentNode.className = "UnSelectedRow";
				allFiles[i].parentNode.parentNode.setAttribute("class", "UnSelectedRow");	
			}
		}
	}
	var allFolders		= document.getElementsByName("chkFolder");
	for(i=0; allFolders!=null && i<allFolders.length; ++i)
	{
		allFolders[i].checked = isCheck;
		if(view=="detail")
		{
			if(isCheck)
			{
				if(allFolders[i].parentNode.parentNode.className)
					allFolders[i].parentNode.parentNode.className = "SelectedRow";
				allFolders[i].parentNode.parentNode.setAttribute("class", "SelectedRow");
			}
			else
			{
				if(allFolders[i].parentNode.parentNode.className)
					allFolders[i].parentNode.parentNode.className = "UnSelectedRow";
				allFolders[i].parentNode.parentNode.setAttribute("class", "UnSelectedRow");	
			}
		}
	}
	document.getElementById("SelectAll").value ="" + isCheck;
}


function OpenMusicShow()
{
	var numMusics	= GetNumMusics();
	
	if(numMusics < 1) {
		alert("No music to play!");
		return;
	}
	var pLeft = (window.screen.availWidth );
	var pTop  = (window.screen.availHeight);	
	var url = "/file/Play.aspx?parentID="+ parentID;
	if(isGallery=="true")
		url = url + "&isGallery=true";
	if(share=="true")
		url = url + "&share=true";
	if(shareID>0)
		url = url + "&shareID=" + shareID;
	window.open (url, '_top', 'height=0, width=0, toolbar=no,left='+pLeft+',top='+pTop+',  menubar=no, scrollbars=no, resizable=no,location=no, status=no') ;	
}

function ChangeLineColor(box)
{
	if(box.checked)
	{
		if(box.parentNode.parentNode.className)
			box.parentNode.parentNode.className = "SelectedRow";
		box.parentNode.parentNode.setAttribute("class", "SelectedRow");
	}
	else
	{
		if(box.parentNode.parentNode.className)
			box.parentNode.parentNode.className = "UnSelectedRow";
		box.parentNode.parentNode.setAttribute("class", "UnSelectedRow");	
	}
}

function AdaptScreen()
{
	var mainDiv = document.getElementById("AdaptPanel");
	if(mainDiv)
	{
		var width, height;
		var screenWidth, screenHeight;
		var margin = 0;
		var isIE	= (window.navigator.userAgent.indexOf("MSIE")>=0);
		if (isIE)
			margin = 120;//996
		else
			margin = 270;//996		
	
		screenHeight= screen.availHeight - margin;
		screenWidth	= screen.availWidth;
				
		if(document.body)
		{
			height	= document.body.offsetHeight;
			width	= document.body.offsetWidth;
		}
		else
		{
			height	= screen.availHeight;
			width	= screen.availWidth;
		}
		
		var inHeight	= screenHeight - height;

		if(height<screenHeight && isIE)
		{
			mainDiv.style.height	= (mainDiv.offsetHeight + inHeight) + "px";
		}		
	}
}

function OpenWindow(url)
{
	if(rateWin!=null)
		rateWin.close();
	rateWin = window.open(url, "Rate", "resizable,width=516,height=368,scrollbars=yes");
	return;
}

function ClickSelectText(sender)
{
	var rng = document.body.createTextRange();
	rng.moveToElementText(sender);
	rng.select();
}

function CopyToClipBoard(sender)
{   
    try
    {
	    if(sender.parentNode)
	    {
		    var content = sender.parentNode.childNodes[0].value;
		    window.clipboardData.setData("Text", content);
		    alert("Copy this link successfully!");
	    }
	}
	catch(Error)
	{
	    //alert(Error);
	}
		
}

function ShowEmailConfirm()
{
//	var container	= document.createElement("div");
//	container.setAttribute("id", "divWin");
//	container.id					= "divWin";
	
	var container	= document.getElementById("divWin");
	var frmStatus	= document.createElement("iframe");
	frmStatus.setAttribute("frameborder", "0");
	frmStatus.setAttribute("scrolling","no");
	frmStatus.setAttribute("src","/sharing/VerifyEmail.aspx");
	frmStatus.frameBorder = 0;
	frmStatus.style.borderWidth = 0;
	frmStatus.style.borderWidth = 0;
	frmStatus.style.width	= "100%";
	frmStatus.style.height	= "100%";
	
	if(container.childNodes.length>0)
	{
		for(var i=container.childNodes.length-1; i>=0; i--)
		{
			container.removeChild(container.childNodes[i]);
		}
	}
				
	if(container.childNodes.length<1)
	{
		container.appendChild(frmStatus);  
	}
	
	var windowInfo	= GetWindownInfo();
	var mask = document.getElementById("divMask");
	if(mask)
	{
		mask.style.width	= windowInfo.Width + "px";
		mask.style.height	= windowInfo.Height + "px";
		mask.style.display	= "";
	}	
	container.style.display = "";
}



function GetWindownInfo()
{
	var scrollX=0,scrollY=0,width=0,height=0,contentWidth=0,contentHeight=0;
	if(typeof(window.pageXOffset)=='number')
	{
		scrollX=window.pageXOffset;
		scrollY=window.pageYOffset;
	}
	else if(document.body&&(document.body.scrollLeft||document.body.scrollTop))
	{
		scrollX=document.body.scrollLeft;
		scrollY=document.body.scrollTop;
	}
	else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop))
	{
		scrollX=document.documentElement.scrollLeft;scrollY=document.documentElement.scrollTop;
	}
	if(typeof(window.innerWidth)=='number')
	{
		width=window.innerWidth;height=window.innerHeight;
	}
	else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight))
	{
		width=document.documentElement.clientWidth;height=document.documentElement.clientHeight;
	}
	else if(document.body&&(document.body.clientWidth||document.body.clientHeight))
	{
		width=document.body.clientWidth;height=document.body.clientHeight;
	}
	if(document.documentElement&&(document.documentElement.scrollHeight||document.documentElement.offsetHeight))
	{
		if(document.documentElement.scrollHeight>document.documentElement.offsetHeight)
		{
			contentWidth=document.documentElement.scrollWidth;
			contentHeight=document.documentElement.scrollHeight;
		}
		else
		{
			contentWidth=document.documentElement.offsetWidth;
			contentHeight=document.documentElement.offsetHeight;
		}
	}
	else if(document.body&&(document.body.scrollHeight||document.body.offsetHeight))
	{
		if(document.body.scrollHeight>document.body.offsetHeight)
		{
			contentWidth=document.body.scrollWidth;
			contentHeight=document.body.scrollHeight;
		}
		else
		{
			contentWidth=document.body.offsetWidth;
			contentHeight=document.body.offsetHeight;
		}
	}
	else
	{
		contentWidth=width;
		contentHeight=height;
	}
	if(height>contentHeight)
		height=contentHeight;
	if(width>contentWidth)
		width=contentWidth;
	var rect=new Object();
	rect.ScrollX=scrollX;
	rect.ScrollY=scrollY;
	rect.Width=width;
	rect.Height=height;
	rect.ContentWidth=contentWidth;
	rect.ContentHeight=contentHeight;
	return rect;
}
