﻿// JScript File
var jsImageExtension="gif,jpeg,jpg,bmp,png"
var jsFileExtension="hju"
var jsAddToImageLib="true"
var jsAddToFileLib="false"

function CheckLibraryForm(LibType) {
    regexp1 = /"/gi;
    
    //*** This is the control contaning the file to upload
    var fileNameObj = document.forms[0].ctl00$mainContentHolder$frm_filename;
    //***
    
    if ((LibType == "quicklinks") || (LibType == "hyperlinks")) {
	    fileNameObj.value = Trim(fileNameObj.value);
	    if (fileNameObj.value == "") {
		    SetMessageText ("A URL Link is required for this library item.");
		    fileNameObj.focus();
		    return false;
	    }
    }
    else
    {
	    var szTempFilename = Trim(fileNameObj.value);
	    if (szTempFilename == "") {
		    //alert ("You must specify a file to upload");
		    SetMessageText("You must specify a file to upload");
		    fileNameObj.focus();
		    return false;
	    }
    }
    if (LibType == "quicklinks") {
	    document.forms.LibraryItem.frm_content_id.value = Trim(document.forms.LibraryItem.frm_content_id.value);
	    if (document.forms.LibraryItem.frm_content_id.value == "") {
		    SetMessageText ("A content ID is required for a library quicklink.");
		    document.forms.LibraryItem.frm_content_id.focus();
		    return false;
	    }
    }
    return true;
}

function Trim (string) {
	if (string.length > 0) {
		string = RemoveLeadingSpaces (string);
	}
	if (string.length > 0) {
		string = RemoveTrailingSpaces(string);
	}
	return string;
}

function RemoveLeadingSpaces(string) {
	while(string.substring(0, 1) == " ") {
		string = string.substring(1, string.length);
	}
	return string;
}

function RemoveTrailingSpaces(string) {
	while(string.substring((string.length - 1), string.length) == " ") {
		string = string.substring(0, (string.length - 1));
	}
	return string;
}


function trim(text){			
	while (' ' == text.charAt(0)) {
		text = text.subString(1);
	}
	while (' ' == text.charAt(text.length-1)) {
		text = text.subString(0, text.length-1);
	}
	return (text);
}

/*function CheckLibSettings() {
    var msg = "";
    var imgDir = document.forms[0].imagedirectory;
    var imgExt = document.forms[0].imageextensions;
    var fileDir = document.forms[0].filedirectory;
    var fileExt = document.forms[0].fileextensions;
    imgDir.value = Trim(imgDir.value);
    imgExt.value = Trim(imgExt.value);
    fileDir.value = Trim(fileDir.value);
    fileExt.value = Trim(fileExt.value);
    if (imgExt.value.length != 0) {
	    if ((imgDir.value.length == 0) && (document.forms[0].relativeimages.checked == false)) {
		    msg = "An image upload path is required when\nvalid image extensions are supplied.\n\nPlease supply a valid image upload path.\n";
		    alert(msg);
		    imgDir.focus();
		    return false;
	    }
    }
    if (fileExt.value.length != 0) {
	    if ((fileDir.value.length == 0) && (document.forms[0].relativefiles.checked == false)) {
		    msg = "A file upload path is required when\nvalid file extensions are supplied.\n\nPlease supply a valid file upload path.\n";
		    alert(msg);
		    fileDir.focus();
		    return false;
	    }
    }
    if (!ConfirmPath(imgDir.value.substring(0,1), document.forms[0].relativeimages)) {
	    imgDir.focus();
	    return false;
    }

    if (!ConfirmPath(fileDir.value.substring(0,1), document.forms[0].relativefiles)) {
	    fileDir.focus();
	    return false;
    }
    return (IsDemo());
}*/

function ConfirmPath(firstChar, relativeObj) {
    msg = "The first character in the supplied upload path\nis not a `/`. This may result in unpredictable results.\n\nIt is strongly recommended that the supplied\npath be relative to the web root.\n\nContinue?\n";
    if (((firstChar != "/") && (firstChar != "\\")) && (relativeObj.checked == false)) {
	    return(confirm(msg));
    }
    return true;
}

function CheckAddSubmission(LibraryType) {
	if (!CheckLibraryForm(LibraryType)) {
		return false;
	}
	if ((LibraryType == "quicklinks") || (LibraryType == "hyperlinks")) {
		return true;
	}
	if (CheckFileType(LibraryType)) {
		var VerifiedType = CheckExtensions();
		if (VerifiedType == "images") {
			if (VerifiedType != LibraryType) {
				if (VerifiedType == "images") {
					var msg = "You have chosen to upload an image into the Files folder.";
				}
				else {
					var msg = "You have chosen to upload a file into the Images folder.";
				}
				msg += "\n" + "If you choose to continue the uploaded file will be sent to the correct folder...\nNOT to the selected folder.\n\nContinue?";
				if (confirm(msg)) {
					if (VerifiedType == "images") {
						document.forms[0].frm_libtype.value = "images";
					}
					else {
						document.forms[0].frm_libtype.value = "files";
					}
					return true;
				}
				return false;
			}
			return true;
		}else{
		    return false;
		}
	}
	else {
		return false;
	}
}

function IsExtensionValid(libType, filename) {
	if (libType == "images") {
		var ExtensionList = jsImageExtension;
	}
	else if (libType == "files") {
		var ExtensionList = jsFileExtension;
	}
	if (ExtensionList.length > 0) {
		var ExtensionArray = ExtensionList.split(",");
		var FileExtension = filename.split(".");
		for (var i = 0; i < ExtensionArray.length; i++) {
			if (FileExtension[FileExtension.length - 1].toLowerCase() == Trim(ExtensionArray[i].toLowerCase())) {
				return true;
			}
		}
		return false;
	}
}

function CheckExtensions(operation) {
	var fileNameObj = document.forms[0].ctl00$mainContentHolder$frm_filename;
	var szTempFilename;
	szTempFilename = Trim(fileNameObj.value);
	if (szTempFilename == "") {
		return 'empty';
	}
	if ((!jsAddToImageLib) && (!jsAddToFileLib)) {
		return 'noupload';
	}
	if (IsExtensionValid("images", szTempFilename)) {
		if ((operation == "overwrite") || (jsAddToImageLib)) {
			return 'images';
		}
		else {
			return 'nouploadimage';
		}
	}
	if (IsExtensionValid("files", szTempFilename)) {				
		if ((operation == "overwrite") || (jsAddToFileLib)) {
			return 'files';
		}
		else {
			return 'nouploadfile';
		}
	}
	return 'false';
}

function CheckFileType(operation) {
    var VerifiedType = CheckExtensions(operation);				
    if ((VerifiedType == "images") || (VerifiedType == "files")) {
    return true;
    }
    if (VerifiedType != "empty") {
    if (VerifiedType == "nouploadimage") {
	    SetMessageText("Sorry, you do not have permissions to upload an image to this folder.");
	    return false;
    }
    if (VerifiedType == "nouploadfile") {
	    SetMessageText("Sorry, you do not have permissions to upload a file to this folder.");
	    return false;
    }
    if (VerifiedType == "noupload") {
	    SetMessageText("Sorry, you do not have permissions to upload images or files to this folder.");
	    return false;
    }
    if (VerifiedType == "noselection") {
	    SetMessageText("Please select a category or folder.");
	    return false;
    }
    var msg = "The file you have selected has an invalid extension.\nPlease choose a file with one of the following extensions:";
    msg += "\n\n";
    var msg1 = "";
    if (jsAddToImageLib) {
	    //msg1 += "For Images:";
	    msg1 += "\n";
	    msg1 += jsImageExtension;
	    msg1 += "\n";
    }
    SetMessageText(msg + msg1);
    }
    document.forms.LibraryItem.frm_filename.focus();
    return false;
}

function SubmitForm(FormName, Validate, ReqVal) {
    if (Validate.length > 0) {
	    if (eval(Validate)) {
		   //run any pre processing functions in here
			 FinallySubmitForm();
		       		
		    return false;
	    }
	    else {
		    return false;
	    }
    }
    else {
	    /*if (m_jsIsMacNoIE)
	    {
		    eLibDesc.GetBody('moveTeaserData');
	    }
	    else
	    {
		    moveTeaserData(FormName);
		    FinallySubmitForm();
	    }*/
	    return false;
    }
}

function FinallySubmitForm(){
	try {
        document.forms[0].submit();
	}
	catch(e) {
		alert(e.message + '\nCheck the file name and upload again.');
	}
}

function SetMessageText(message){

    document.getElementById("ctl00_mainContentHolder_Label1").innerHTML = message;
    //document.messagebox.innerhtml = message;

}
