//
//	COPYRIGHT NOTICE:
// 	Copyright 2002 - 2006 Barry Saxifrage. Stonebreaker Designs. All Rights Reserved.
//	By using this code you agree to indemnify Barry Saxifrage and Stonebreaker Designs 
//	from any liability that might arise from its use. Selling this code or distributing 
//	this code without prior written consent is expressly forbidden. Copyright notice  
//	must remain with this code. For more information: www.stonebreakerdesigns.com	
//

// list of accepted file types (non-images)
//   IMPORTANT: this list must be kept in sync with same list for cgi
//	 Gets filled for articles by db query sent to article dialogs
var aFileExtsOKGeneral = new Array(
	);
	
// list of accepted image file types
//   IMPORTANT: this list must be kept in sync with same list for cgi
var aFileExtsOKImage = new Array(
 	"jpg|JPEG Image (.jpg)",
 	"jpeg|JPEG Image (.jpeg)",
 	"gif|GIF Image (.gif)",
 	"png|PNG Image (.png)"
	);
	
	//
	//	Returns array of info about the file
	//		[0] = fIsAcceptedType : true if either accepted general or image type
	//		[1] = fIsImage : one of the accepted image types
	//		[2] = strExt : file extension found	on end of filename
	//		[3] = strExtName : description of file extension
	
	function getFiletypeInfo(strFilename) {
		// set up data to fill in
		var fIsAcceptedType = false;
		var fIsImage = false;
		var strExt = '';
		var strExtName = '';
		var aReturn = new Array();
		// find extension if there is on
		var reExt = /\.(.{2,4})$/i;
		var reMatch = reExt.exec(strFilename);
		if (reMatch) {
			// record extenstion found
			strExt = reMatch[1];
			strExt = strExt.toLowerCase();
			// check if accepted general
			for (var i = 0; i < aFileExtsOKGeneral.length; i++) {
				var aE = aFileExtsOKGeneral[i].split('|');
				if (aE[0].toLowerCase() == strExt) {
					fIsAcceptedType = true;
					strExtName = aE[1];
					break;
				}
			}
			// check if accepted image
			for (var i = 0; i < aFileExtsOKImage.length; i++) {
				var aE = aFileExtsOKImage[i].split('|');
				if (aE[0].toLowerCase() == strExt) {
					fIsAcceptedType = true;
					fIsImage = true;
					strExtName = aE[1];
					break;
				}
			}
		}
		// save data into array and return it
		aReturn[0] = fIsAcceptedType;
		aReturn[1] = fIsImage;
		aReturn[2] = strExt;
		aReturn[3] = strExtName;
		return aReturn;
	}

	//	EMPTY file upload control
	//		-- for NEW dialogs
	//		-- iFile: 0=toppix, 1+ = FILE 
	//		-- nImageLevel: 0=all filetypes, 1=images only, 2=jpg only, 3=gif only
	function getUploadControlHTML(iFile, nImageLevel) {
		var str = '';
		if (iFile == 0) { 
			str += '<input type="file" name="TOPPIX" onChange="javascript:checkBulletinFUType(0, '+nImageLevel+')"><br>';
			str += '<div id="ftSelect0">';
				str += getFiletypeSelectorHTML(0, nImageLevel);
			str += '</div>';
			str += '<div id="ftName0"><input type="text" name="FILETYPE0" readonly  class="dlg_invisitext" value=""></div>';
		} else {
			str += '<input type="hidden" name="FILEACTION'+iFile+'" value="0">';
			str += '<input type="hidden" name="CURFILE'+iFile+'" value="">';
			str += '<input type="file" name="FILE'+iFile+'" onChange="javascript:checkBulletinFUType('+iFile+', '+nImageLevel+')"><br>';
			str += '<div id="ftSelect'+iFile+'">';
			str += getFiletypeSelectorHTML(iFile, nImageLevel);
			str += '</div>';
			str += '<div id="ftName'+iFile+'"><input type="text" name="FILETYPE'+iFile+'" readonly  class="dlg_invisitext" value=""></div>';
		}
		return str;
	}
	

	//	EMPTY file upload and caption for TopPix
	//		-- for NEW dialogs
	//		-- FIXED iFile: 0=toppix
	//		-- FIXED nImageLevel: 1=images only
	function getTopPixControlsHTML(labelPhoto, labelCaption, fShowCaption) {
		var str = '';
		// ROW: FileUpload Picture 
		str += '<tr>';
			str += '<td class="dlg_tdL" valign="top">'+labelPhoto+'</td>';
			str += '<td class="dlg_tdC" valign="top">';
				str += '<input type="file" name="TOPPIX" onChange="javascript:checkBulletinFUType(0, 1)"><br>';
				str += '<div id="ftSelect0">';
					str += getFiletypeSelectorHTML(0, 1);
				str += '</div>';
				str += '<div id="ftName0"><input type="text" name="FILETYPE0" readonly  class="dlg_invisitext" value=""></div>';
			str += '</td>';
			str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
		str += '</tr>';
		// ROW: Caption
		if (fShowCaption) {
			str += '<tr>';
				str += '<td class="dlg_tdL" valign="top"><span class="dlgnote">'+labelCaption+'</span></td>';
				str += '<td class="dlg_tdC" valign="top">';
					str += '<input type="text" name="TOPPIXCAPTION" maxlength="200" class="dlg_edit">';
					//str += '<img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0"><br>';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
			str += '</tr>';
		}
		return str;
	}
	

	//	EMPTY file upload control and caption
	//		-- for NEW dialogs
	//		-- iFile: 1+ = FILE 
	//		-- nImageLevel: 0=all filetypes, 1=images only, 2=jpg only, 3=gif only
	function getAttachmentControlsHTML(iFile, iLabel, nImageLevel) {
		var str = '';
		// ROW: FileUpload  
		str += '<tr>';
			str += '<td class="dlg_tdL">';
				if (nImageLevel > 0) { str += 'Photo-'+iLabel; }
				else 				 { str += 'Attachment-'+iLabel; }
			str += '</td>';
			str += '<td class="dlg_tdC" valign="top">';
				str += '<input type="hidden" name="FILEACTION'+iFile+'" value="0">';
				str += '<input type="hidden" name="CURFILE'+iFile+'" value="">';
				str += '<input type="file" name="FILE'+iFile+'" onChange="javascript:checkBulletinFUType('+iFile+', '+nImageLevel+')"><br>';
				str += '<div id="ftSelect'+iFile+'">';
				str += getFiletypeSelectorHTML(iFile, nImageLevel);
				str += '</div>';
				str += '<div id="ftName'+iFile+'"><input type="text" name="FILETYPE'+iFile+'" readonly  class="dlg_invisitext" value=""></div>';
			str += '</td>';
			str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
		str += '</tr>';
		// ROW: Caption
		str += '<tr>';
			str += '<td class="dlg_tdL" >';
				if (nImageLevel > 0) { str += '<span class="dlgnote">Caption-'+iLabel+'</span>'; }
				else 				 { str += '<span class="dlgnote">Title-'+iLabel+'</span>'; }
			str += '</td>';
			str += '<td class="dlg_tdC" >';
				str += '<input type="text" name="CAPTION'+iFile+'" maxlength="200" class="dlg_edit">';
			str += '</td>';
			str += '<td class="dlg_tdR" valign="top">';
				str += '&nbsp';
			str += '</td>';
		str += '</tr>';
		//str += '<tr><td>&nbsp;</td><td><img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0"></td><td>&nbsp;</td></tr>';
		return str;
	}

	//	POSSIBLY FULL file upload control and caption
	//		-- for EDIT dialogs
	//		-- iFile: 0=toppix, 1+ = FILE 
	//		-- nImageLevel: 0=all filetypes, 1=images only, 2=jpg only, 3=gif only
	function getEditPixAttachmentControlsHTML(iFile, labelPhoto, urlPhoto, labelCaption, strCaption, fShowCaption) {
		// pix only
		return getEditAttachmentControlsHTML(iFile, labelPhoto, urlPhoto, labelCaption, strCaption, fShowCaption, 1);
	}
	function getEditAttachmentControlsHTML(iFile, labelPhoto, urlPhoto, labelCaption, strCaption, fShowCaption, nImageLevel) {
		var str = '';
		var strLabelFile 	= 'Photo-'+iFile;
		var strLabelCaption = '<span class="dlgnote">Caption-'+iFile+'</span>';
		var strNameCurFile 	= 'CURFILE'+iFile;
		var strNameFile 	= 'FILE'+iFile;
		var strNameCaption 	= 'CAPTION'+iFile;
		if (iFile == 0) { 
			strLabelFile 	= 'Top Photo';
			strLabelCaption = '<span class="dlgnote">Top Caption</span>';
			strNameCurFile 	= 'CURTOPPIXFILE'; 
			strNameFile 	= 'TOPPIX'; 
			strNameCaption 	= 'TOPPIXCAPTION';
		}
		if (labelPhoto   != '') { strLabelFile    = labelPhoto;   }
		if (labelCaption != '') { strLabelCaption = '<span class="dlgnote">'+labelCaption+'</span>'; }

		// ROW: Photo
		//		IF empty then just create a fileUpload box...else create full file choices
		str += '<tr>';
			str += '<td class="dlg_tdL" valign="center"><div style="margin-bottom:5px;">'+ strLabelFile +'</div></td>';
		str += '<td class="dlg_tdC" valign="top">';
				str += '<input type="hidden" name="'+strNameCurFile+'" value="'+urlPhoto+'">';
				if (urlPhoto) {
					str += '<div style="border-left: 2px solid black; padding-left:2px">';
					str += '<input type="Radio" name="FILEACTION'+iFile+'" value="1" onclick="javascript:setFileActionN('+iFile+')" checked>';
					str += ' Keep&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; // original '+strLabelFile+'<br>';
					str += '<input type="Radio" name="FILEACTION'+iFile+'" value="3" onclick="javascript:setFileActionN('+iFile+')">';
					str += ' Replace&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '; //+strLabelFile+'<br>';
					str += '<input type="Radio" name="FILEACTION'+iFile+'" value="2" onclick="javascript:setFileActionN('+iFile+')">';
					str += ' Delete'; // original '+strLabelFile+'<br>';
					str += '<div id="rb'+iFile+'">';
						str += '<input type="file" name="'+strNameFile+'" class="dlg_fileupload" onChange="javascript:checkBulletinFUType('+iFile+', '+nImageLevel+')"><br>';
						str += '<div id="ftSelect'+iFile+'">';
						str += getFiletypeSelectorHTML(iFile, nImageLevel);
						str += '</div>';
					str += '</div>';
					str += '<div id="fb'+iFile+'">';
						str += '<input type="text" name="curfilename'+iFile+'" readonly class="dlg_noedit" value="'+urlPhoto+'"><br>';
					str += '</div>';
					str += '<div id="ftName'+iFile+'"><input type="text" name="FILETYPE'+iFile+'" readonly  class="dlg_invisitext" value=""></div>';

					str += '</div>';

				} else {
					str += '<input type="file" name="'+strNameFile+'" class="dlg_fileupload" onChange="javascript:checkBulletinFUType('+iFile+',  '+nImageLevel+')"><br>';					
					str += '<div id="ftSelect'+iFile+'">';
					str += getFiletypeSelectorHTML(iFile, nImageLevel);
					str += '</div>';
					str += '<div id="ftName'+iFile+'"><input type="text" name="FILETYPE'+iFile+'" readonly  class="dlg_invisitext" value=""></div>';
				}
			str += '</td>';
			str += '<td class="dlg_tdR" valign="top">';
				str += '&nbsp';
			str += '</td>';
		str += '</tr>';

		// ROW: Caption
		if (fShowCaption) {
			str += '<tr>';
				str += '<td class="dlg_tdL" >'+strLabelCaption+'</td>';
				str += '<td class="dlg_tdC">';
					str += '<input type="text" name="'+strNameCaption+'" maxlength="200" class="dlg_edit" value="'+strCaption+'">';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">';
					str += '&nbsp';
				str += '</td>';
			str += '</tr>';
			//str += '<tr><td>&nbsp;</td><td><img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0"></td><td>&nbsp;</td></tr>';
		}

		return str;
	}
	
	// 
	//	Draws a Select control using info from the accepted filetype arrays
	function getFiletypeSelectorHTML(iSelector, nImageLevel) {
		var str = '<select name="FILETYPESELECT'+iSelector+'" size="1" class="dlg_selectImportant">';
			str += '<option value="noext">UNKNOWN FILE TYPE: Please Select From This List';
			str += fillFiletypeSelector(nImageLevel);
		str += '</select>';	
		return (str);
	}
		
	function fillFiletypeSelector(nImageLevel) {
		var str = '';
		if (nImageLevel <= 0) {
			// add accepted general types
			for (var i = 0; i < aFileExtsOKGeneral.length; i++) {
				var aE = aFileExtsOKGeneral[i].split('|');
				str += '<option value="'+aE[0]+'">'+aE[1];
			}
		}
		// add accepted image types
		for (var i = 0; i < aFileExtsOKImage.length; i++) {
			var aE = aFileExtsOKImage[i].split('|');
			var ext = aE[0];
			var def = aE[1];
			if (nImageLevel <= 1 || (nImageLevel == 2 && ext == 'jpg') || (nImageLevel == 3 && ext == 'gif') ){
				str += '<option value="'+aE[0]+'">'+aE[1];
			}
		}
		return (str);
	}

	//
	//  Code to check FileUpload controls for correct filetype
	//		-- nImagesCheck 
	//			0=general files and all image files
	//			1=all image files only
	//			2=jpg only
	//			3=gif only
	function checkBulletinFUType(iControl, nImagesCheck) {
		var strObj1 = 'document.frm.FILE'+iControl;
		var strObj2 = 'document.frm.FILETYPE'+iControl;
		var strObj3 = 'document.frm.FILETYPESELECT'+iControl;
		if (iControl == 0) { 
			strObj1 = 'document.frm.TOPPIX';
		}
		var objFileUp = eval(strObj1);
		var objFileType = eval(strObj2);
		var objFileTypeSel = eval(strObj3);
		
		var strFilename = objFileUp.value;
		var aF = getFiletypeInfo(strFilename);
		var fIsAcceptedType = aF[0];
		var fIsImage 		= aF[1];
		var strExt 			= aF[2];
		var strExtName 		= aF[3];
		var domSel = eval("getDOM('ftSelect"+iControl+"', 1)");
		var domName = eval("getDOM('ftName"+iControl+"', 1)");
		// SCENARIOS
		//		1. No filename : clear typename and hide selector
		//		2. Filename with OK ext : list typename and hide selector
		//		3. Filename with BAD ext : show ERROR alert, show ERRORText in typename and hide selector
		//		4. Filename with no ext : hide typename and show selector
		//		5. Filename contains illegal characters : show ERROR alert;
		//alert(strFilename);
		// -- S1. No filename
		if (isEmpty(strFilename)) {
			objFileType.value = '';
			domSel.display = 'none'; 
			domName.display = 'block';
		} 
		// -- S4. Filename with illegal characters	
		else if (strFilename.search(/[\|"'<>=^`\{\}]/i) != -1) {
			alert('FILENAME HAS UNSUPPORTED CHARACTERS IN IT: Uploaded filenames are not allowed to have any of the following characters: | " \' < > = ^ ` { }  Please close this dialog, rename your file and try again. Thanks.'); 
		}
		// -- S2.a IMAGE ONLY Filename with any ext...must be image...or error
		else if ((nImagesCheck == 1) && !(isEmpty(strExt)) && !fIsImage) {
			objFileType.value = 'PICTURE FILES ONLY: Please change.';
			domSel.display = 'none';
			domName.display = 'block'; 
			showAlertFiletypeWrong(strExt, 'pixOnly', nImagesCheck);
		}
		// -- S2.b JPG IMAGE ONLY 
		else if ((nImagesCheck == 2) && !(isEmpty(strExt)) && (strExt != 'jpg') && (strExt != 'jpeg')) {
			objFileType.value = 'JPG FILES ONLY: Please change.';
			domSel.display = 'none';
			domName.display = 'block'; 
			showAlertFiletypeWrong(strExt, 'jpgOnly', nImagesCheck);
		}
		// -- S2.c GIF IMAGE ONLY 
		else if ((nImagesCheck == 3) && !(isEmpty(strExt)) && (strExt != 'gif')) {
			objFileType.value = 'GIF FILES ONLY: Please change.';
			domSel.display = 'none';
			domName.display = 'block'; 
			showAlertFiletypeWrong(strExt, 'gifOnly', nImagesCheck);
		}
		// -- S2.c Filename with OK ext		
		else if (fIsAcceptedType) {
			objFileType.value = '';
			domSel.display = 'none';
			domName.display = 'block'; 
		}
		// -- S3. Filename with BAD ext	
		else if (!fIsAcceptedType &&  !(isEmpty(strExt)) ) {
			objFileType.value = 'UNSUPPORTED FILE TYPE: Please change.';
			domSel.display = 'none';
			domName.display = 'block'; 
			showAlertFiletypeWrong(strExt, 'Unsupported', nImagesCheck);
		}
		// -- S4. Filename with no ext	
		else if (isEmpty(strExt)) {
			objFileType.value = '';
			objFileTypeSel.selectedIndex = 0;
			domName.display = 'none'; 
			domSel.display = 'block';
			showAlertFiletypeWrong(strExt, 'NoExt', nImagesCheck);
		}
	}


	function showAlertFiletypeWrong(strExt, nMsgType, nImageLevel) {
		var str = '';
		if 		(nMsgType == 'pixOnly') 	{ str += "IMAGE FILES ONLY -- The type of the file you chose (."+ strExt + ") is not currently supported. Please choose a different type or delete the filename from the control. "; }
		else if (nMsgType == 'jpgOnly') 	{ str += "JPEG IMAGE FILES ONLY (*.jpg) -- The type of the file you chose (."+ strExt + ") will not work in this situation. Please choose a different type or delete the filename from the control."; }
		else if (nMsgType == 'gifOnly') 	{ str += "GIF IMAGE FILES ONLY (*.gif) -- The type of the file you chose (."+ strExt + ") will not work in this situation. Please choose a different type or delete the filename from the control."; }
		else if (nMsgType == 'Unsupported') { str += "FILE TYPE *NOT* SUPPORTED -- The type of the filename you chose (."+ strExt + ") is not currently supported. Please choose a different type or delete the filename from the control."; }
		else if (nMsgType == 'NoExt') 		{ str += "MISSING EXTENSION -- Each file to be uploaded must have an extension indicating its file type. To append an extension to your file, choose a file type from the pop-up menu on the form."; }

		//if ((nMsgType != 'NoExt')) {
		//	str += "SUPPORTED FILE TYPES:";
			// add accepted general types
		//	if (nImageLevel <= 0)  	{
		//		for (var i = 0; i < aFileExtsOKGeneral.length; i++) {
		//			var aE = aFileExtsOKGeneral[i].split('|');
		//			str += "     "+aE[1]+"";
		//		}
		//	}
		//	// add accepted image types
		//	for (var i = 0; i < aFileExtsOKImage.length; i++) {
		//		var aE = aFileExtsOKImage[i].split('|');
		//		var ext = aE[0];
		//		var def = aE[1];
		//		if (nImageLevel <= 1 || (nImageLevel == 2 && ext == 'jpg') || (nImageLevel == 3 && ext == 'gif') ){
		//			str += "     "+aE[1]+"";
		//		}
		//	}
		//}
		alert(str);
	}
	
	function setFileActionN(nFile) {
		var d1 = getDOM('fb'+nFile, 1);
		//var d2 = getDOM('fnb'+nFile, 1); //caption...not used
		var d3 = getDOM('rb'+nFile, 1);
		var fAction1 = eval('document.frm.FILEACTION'+nFile+'[2].checked');
		var fAction2 = eval('document.frm.FILEACTION'+nFile+'[1].checked');
		var nAction = 1;
		if 		(fAction1) {nAction = 2;}
		else if (fAction2) {nAction = 3;}
		if 		  (nAction == 1) { //keep file: show fb and fnb and hide rb
			d3.display 		= 'none';
			d1.display 		= 'block';
			d1.visibility 	= 'visible';
		} else if (nAction == 2) { //del file: hide fb and fnb and hide rb
			d3.display 		= 'none';
			d1.visibility 	= 'hidden';
			d1.display 		= 'block';
		} else if (nAction == 3) { //replace file: hide fb and show rb and fnb 
			d1.display 		= 'none';
			d1.visibility	= 'visible';
			d3.display 		= 'block';
		}
	}
	
	// called in dialogs to jump to attachment dialog
	function manageAttachType() {
		var strCGI = "dlg_attachtype_list.cgi";
		showDialogCGI("ManageAttachmentType",590,800,strCGI);
	}

