// ************************************************************************
// File:	/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Common/ResourceBrowserCommon.js
// Author:	Microsoft Corporation.
// Purpose:	Provide functions used by both Resouce Authoring and Resource Management
//
// ************************************************************************

// ---- [Public] Validates the local file, formats its full path to http
//				file protocol format and opens up a new window for the file.
function WBC_previewLocalResource(strResType) {
	var strResourceCommonDirectory = IDS_FRAMEWORK_VIRTUAL_PATH + "/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Common/"
	var strLocalPath = WBC_getLocalPath(strResType);
	if (strLocalPath != null) {
		// format path to url
		if (navigator.userAgent.toLowerCase().indexOf('mac') >= 0) {
			if (navigator.appName == "Netscape") {
				strLocalPath = escape(strLocalPath);
				strLocalPath = "file://" + WBC_replaceCharInStr(strLocalPath, "%5C", "/", "global");
				// This will not work for attachments that are not images. The preview link
				// should be disabled for attachments if the user is using a Mac Netscape
				// browser.
				strPreviewUrl = strResourceCommonDirectory + "LocalPreview.asp?" + IDS_QPARAM_URL + "=" + escape(strLocalPath);
				WBC_openWindowOnFocus(strPreviewUrl, IDS_WIN_PREVIEW, WBC_getPoppedUpWindowFeatures(IDS_WIN_PREVIEW_FEATURES, window.top));
			} else {
				strLocalPath = WBC_replaceCharInStr(strLocalPath, "%2F", "/", "global");
				strLocalPath = "file://" + WBC_replaceCharInStr(strLocalPath, "+", "%20", "global");
				WBC_openWindowOnFocus(strLocalPath, IDS_WIN_PREVIEW, WBC_getPoppedUpWindowFeatures(IDS_WIN_PREVIEW_FEATURES, window.top));
			}
		} else {
			strLocalPath = escape(strLocalPath);
			strLocalPath = WBC_replaceCharInStr(strLocalPath, "%3A", "|", "");
			strLocalPath = "file:///" + WBC_replaceCharInStr(strLocalPath, "%5C", "/", "global");
			strPreviewUrl = strResourceCommonDirectory + "LocalPreview.asp?" + IDS_QPARAM_URL + "=" + escape(strLocalPath);
			WBC_openWindowOnFocus(strPreviewUrl, IDS_WIN_PREVIEW, WBC_getPoppedUpWindowFeatures(IDS_WIN_PREVIEW_FEATURES, window.top));
		}
	}
}




// ---- [Private] This function replace characters in a string.
function WBC_replaceCharInStr(strOriginal, strFmString, strToString, strOccur) {
	var strNew = "";
	var nFmStringLength = strFmString.length;
	var nStringTraverseLength = strOriginal.length - nFmStringLength
	var i = 0;
	var j = 0;
	while (i <= nStringTraverseLength) {
		if (strOriginal.substring(i, i+nFmStringLength) == strFmString) {
			strNew = strNew + strOriginal.substring(j, i) + strToString;
			i = i + nFmStringLength;
			j = i;
			if (strOccur != "global") {
				break;
			}
		}
		i = i + 1;
	}
	if (j == 0) {
		return strOriginal;
	} else {
		strNew = strNew + strOriginal.substring(j);
		return strNew;
	}
}



// ---- [Private] This function retrieves the full path of the local file and
//					if it is for image, checks the file extension.
function WBC_getLocalPath(strResType) {
	document.NC_LocalForm.NC_FileData.focus();
	document.NC_LocalForm.NC_FileData.blur();
	var strLocalPath = document.NC_LocalForm.NC_FileData.value;
	
	// Check for empty or all blanks filename
	if (strLocalPath.split(" ").length == strLocalPath.length + 1) {
		alert(IDS_NO_FILE_SELECTED);
		return null;
	}
	
	if ( strResType == IDS_RESTYPE_IMAGE ) {
		// This array defines the valid image extensions
		var arrayImageExt = new Array("gif", "jpg", "jpeg", "png", "bmp");
		var strFileExt = strLocalPath.substring( strLocalPath.lastIndexOf(".")+1 ).toLowerCase();
		for (var i = 0; i < arrayImageExt.length; i++) {
			if (strFileExt == arrayImageExt[i]) {
				return strLocalPath;
			}
		}
		if (!confirm(IDS_INVALID_IMAGE_TYPE)) {
			return null;
		}
	}
	
	if ( strResType == IDS_RESTYPE_VIDEO ) {
		// This array defines the valid video extensions
		var arrayVideoExt = new Array("asf", "asx", "avi", "mov", "mpeg", "mpg", "qt");
		var strFileExt = strLocalPath.substring( strLocalPath.lastIndexOf(".")+1 );
		for (var i = 0; i < arrayVideoExt.length; i++) {
			if (strFileExt == arrayVideoExt[i]) {
				return strLocalPath;
			}
		}
		if (!confirm(IDS_INVALID_VIDEO_TYPE)) {
			return null;
		}
	}
	
	return strLocalPath;
}



var g_bUploading = false;

// ---- [Private] This function validates local resources
function WBC_validateLocalPath(strResType) {
	// check if we are uploading already
	if (g_bUploading)
	{
		alert("Upload in progress already.");
		return false;
	}
	
	// validate local path
	if (WBC_getLocalPath(strResType) == null) {
		return false;
	}
	
	g_bUploading = true;
	return true;
}

