// DIALOG CODE 
//
//	HELPER FUNCTIONS
//		-- setDialogFocus: 	sets focus to dialog window in case it is hidden behind
//		-- updater: 		tells opening window to refresh itself because data has changed
//		-- limitText: 		checks field named TEXT to see if it has too many characters
//		-- initDlgWaitMsg:  sets up wait message box on initPage()
//		-- showDlgWaitMsg:  shows wait message box
//		-- dlgPopGroup:		opens/closes a control group
//		-- dlgShowInfo:		opens/closes an info box
//
	function setDialogFocus() {
		window.focus();
		
	}
	function updater() {
		if (window.opener != null) {
			window.opener.refreshPage();
		}
	}
	function updaterSite() {
		if (window.opener != null) {
			window.opener.refreshSite();
		}
	}
	function limitText(cchMaxTextChars) {
		// limits the amount of text that can be typed into text field
		if (document.frm.TEXT.value.length > cchMaxTextChars) {
			var szT = document.frm.TEXT.value;
			document.frm.TEXT.value = szT.substring(0, (cchMaxTextChars-1));
		}
	}
	function initDlgWaitMsg() {
		var d = getDOM('dialogwaitmsg', 1);
		d.display = "none";
	}
	function showDlgWaitMsg() {
		var d = getDOM('dialogwaitmsg', 1);
		d.display = "block";
		d = getDOM('formbox', 1);
		//d.visibility = "hidden";
		window.scroll(0,0);
	}
	// open/close group of controls in a dialog
	function dlgPopGroup(id) {
		var d = getDOM(id, 1);
		if (d.display == 'block') { 
			d.display = 'none';
		} else {
			d.display = 'block';			 
		}

	}
	// open/close infobox in a dialog
	function dlgShowInfo(id) {
		var d = getDOM(id, 1);
		if (d.display =='block') { 
			d.display = 'none';
		} else {
			d.display = 'block';			 
		}
	}	
	function showOptions(id) {
		var d = getDOM(id,1);
		d.visibility = "visible";
	}
	function hideOptions(id) {
		var d = getDOM(id,1);
		d.visibility = "hidden";
	}
    function email_parse(strEmail) { 
        if(strEmail.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) { 
            return true; 
        } else { 
            return false; 
        } 
    } 



//
//	CREATE DIALOG HTML
//		-- drawDlgSubHead: 			 creates Title and Info subheads at top of dialog box
//		-- drawDlgGroupHead: 		 creates Control Group heading with dropdown arrow and link to pop-open the control group
//		-- drawDlgRowIntroText			 
//		-- drawDlgRowTitle			 
//		-- drawDlgRowText			 
//		-- drawDlgRowAuthor			 
//		-- drawDlgRowReplyEmail		 
//		-- drawDlgRowWebLink	
//		-- drawDlgDistributionListNew	
//		-- drawDlgDistributionListEdit	
//		-- getDlgConformityNote: 	 returns Conformity note just above submit buttons in dialogs
//		-- drawDlgPasswordAndSubmit: creates bottom part of dialog with Editing Password and Submit/Cancel buttons
//		-- drawDlgCancelOnly: 		 creates bottom part of simple dialog with just single Cancel button
//		-- drawDlgDividerRow		 single row with divider line
//		-- drawInfoBoxHTML: 		 creates an Info icon and pop-open text box for right column
//		-- getInfoBoxHTML:			 same as above but just returns HTML string
//		-- drawWaitMessage:			 creates the Wait Message box that is displayed with user hits submit button
//
	
	// Create Group Heading used to pop-open a group of controls in a dialog
	function drawDlgSubHead(strTitle) {
		var str = '<div class="dialog_subtitle">Oregon Wool</div>';
		str += '<div class="dialog_title">'+strTitle+'</div>';
		document.writeln(str);
	}
	// Create Group Heading used to pop-open a group of controls in a dialog
	function drawDlgGroupHead(strTitle, strClickText, iPopGroup) {
		var str = getDlgGroupHeadHTML(strTitle, strClickText, iPopGroup);
		document.writeln(str);
	}
	function getDlgGroupHeadHTML(strTitle, strClickText, iPopGroup) {
		var str = '';
		// dotted line
		str += '<img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0"><br>';
		// bold heading
		str += '<a href="javascript:dlgPopGroup(';
		str += "'dlg_popgroup"+iPopGroup+"'";
		str += ')" class="dlg_subhead">';
		str += strTitle;
		str += ' <img src="bits/dlg_pop_down.gif" width="10" height="6" alt="" border="0">';
		str += '</a><br>';
		// "click here to ..." link
		str += '<a href="javascript:dlgPopGroup(';
		str += "'dlg_popgroup"+iPopGroup+"'";
		str += ')" class="dlgnotesmall">';
		str += 'Click here to '+strClickText+'...';
		str += '</a><br>';
		str += '';
		return (str);
	}


	// Create rows in dialogs for:
	//		-- IntroText
	//		-- Author/Edited By
	//		-- Title
	//		-- Text
	//		-- Reply Email
	//		-- Website Link
	
	function drawDlgRowIntroText(strText) {
		var str = '';
		// <!-- ROW: IntroText -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<div class="dlg_introtext">';
						str += strText;
					str += '</div>';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
			str += '</tr>';
		document.writeln(str);
	}

	function drawDlgRowAuthor(strAuthor) {
		drawDlgRowCreator(strAuthor,"Author");
	}
	function drawDlgRowCreator(strCreator, strLabel) {
		var str = '';
		// <!-- ROW: Author/EditsBy -->
			str += '<tr>';
				str += '<td class="dlg_tdL" >'+strLabel+'</td>';
				str += '<td class="dlg_tdC" >';
					str += '<input type="text" name="BYNAME" readonly class="dlg_noedit" value="'+strCreator+'">';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">'
				str += '&nbsp;';
				str += '</td>';
			str += '</tr>';
		document.writeln(str);
	}

	function drawDlgRowTitle(strCurTitle, strLabel, fRequired) {
		var str = '';
		var strReq = '';
		if (fRequired) { strReq = '<span class="dlgrequired">*</span>'; }
		// <!-- ROW: Title -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">'+strReq+''+strLabel+'</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<input type="text" name="TITLE" maxlength="40" class="dlg_edit" style="font-weight:bold;" value="'+strCurTitle+'">';
				str += '</td>';
				str += '<td class="dlg_tdR">'
				str += '40 characters  max.';
				str += '</td>';
			str += '</tr>';
		document.writeln(str);
	}

	function drawDlgRowText(strCurText, strLabel, fRequired, cchMax) {
		drawDlgRowTextAll(strCurText, strLabel, fRequired, cchMax, false, 0);
	}
	function drawDlgRowTextSpecial(strCurText, strLabel, fRequired, cchMax, fSpecialChars) {
		drawDlgRowTextAll(strCurText, strLabel, fRequired, cchMax, fSpecialChars, 0);
	}
	function drawDlgRowTextAll(strCurText, strLabel, fRequired, cchMax, fSpecialChars, dyText) {
 		var str = '';
		var strReq = '';
		if (dyText <= 1) { dyText = 4; }
		if (fRequired) { strReq = '<span class="dlgrequired">*</span>'; }
		// <!-- ROW: Text -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">'+strReq+''+strLabel+'</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<textarea wrap="soft" rows="'+dyText+'" cols="11" name="TEXT" class="dlg_textarea" onKeyPress="limitText('+cchMax+');" >';
					str += strCurText;
					str += '</textarea>';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">'
					if (cchMax >= 1000) {
						cchMax += '';
						cchMax = cchMax.substring(0,cchMax.length-3) + ',' + cchMax.substring(cchMax.length-3,cchMax.length);
					}
					str += cchMax+' characters max.<br>';
					if (fSpecialChars) {
					
						var strM = '**sample** = <b>sample</b><br>!!sample!! = <i>sample</i><br>__sample__ = <u>sample</u><br>';
						strM += '<br>www.website.com will be clickable.';
						str += getInfoBoxHTML(strM, 104);
					}
				str += '</td>';
			str += '</tr>';
		document.writeln(str);
	}

					
	function drawDlgRowReplyEmail(strCurEmail) {
		var str = '';
		// <!-- ROW: Reply Email -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">Reply E-Mail</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<input type="text" name="EMAIL" maxlength="256" class="dlg_edit" value="'+strCurEmail+'">';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">'
				str += getInfoBoxHTML("PRESET-ReplyEmail", 101);
				str += '</td>';
			str += '</tr>';
		document.writeln(str);

	}

	function drawDlgRowWebLink(strCurLink) {
		drawDlgRowWebLinkPlus(strCurLink,-1);
	}
	function drawDlgRowWebLinkPlus(strCurLink,nLinkType) {
		// nLinkType values: -1=OnlyExternalLinks(noOptionsGiven), 0=InternalLink, 1=ExternalLink
		var str = '';
		// <!-- ROW: Reply Email -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">Website</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<input type="text" name="URLLINK" maxlength="256" class="dlg_edit" value="'+strCurLink+'">';
					if (nLinkType >= 0) {
						var strInternalChecked = '';
						var strExternalChecked = '';
						if (nLinkType == 0) { strInternalChecked=' CHECKED '; }
						if (nLinkType == 1) { strExternalChecked=' CHECKED '; }
						str += '<br><input type="Radio" value="0" class="dlg_radio" '+strInternalChecked+' name="INTERNALURL"> This link is to a webpage on this site<br>';
						str += '<input type="Radio" value="1" class="dlg_radio" '+strExternalChecked+' name="INTERNALURL"> This link is to an external website';
					}
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">'
				if (nLinkType >= 0) {
					str += getInfoBoxHTML("PRESET-WebLinkPlus", 102);
				} else {
					str += getInfoBoxHTML("PRESET-WebLink", 102);
				}
				str += '</td>';
			str += '</tr>';
		document.writeln(str);
	}

	// Create Assignment List HTML for Product Sub-Category 
	//		-- aPTypes: array of all product sub-categories: "Id|Name"
	//		-- aMyPTypes: array of all product sub-categories this article is assigned to. format: "Id|Name"
	function drawDlgDistributionListEdit(aPTypes, aMyPTypes) {
		var str = '';
		// <!-- TABLE: Broadcast Header ---------------------------- -->
		str += '<table cellpadding="0" cellspacing="0" class="dlg_table">';
			str += '<tr>';
				str += '<td class="dlg_tdL" valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC" valign="top">';
					str += getDlgGroupHeadHTML('Assign To Product Categories','link this article to particular products',100);
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">&nbsp;';
				str += '</td>';
			str += '</tr>';
		str += '</table>';
		// <!-- POP-TABLE: Broadcast Controls ---------------------------- -->
		str += '<div id="dlg_popgroup100">';
		str += '<table cellpadding="0" cellspacing="0" class="dlg_table">';
			// <!-- ROW: Current Distribution Groups -->
			str += '<tr>';
				str += '<td class="dlg_tdL">';
					str += 'Assign To These Product Categories';
				str += '</td>';
				str += '<td class="dlg_tdC">';
				// RECIPIENTS:
				//		-- create 'checked' checkbox for each current recipient group
				var strRecipList = '';
				for (var i = 0; i < aMyPTypes.length - 1; i++) {
					var aR = aMyPTypes[i].split('|');
					var idR 	 = aR[0];
					var strRName = aR[1];
					str += '<input type="checkbox" name="RECIPIENTID" class="dlg_checkbox" value="'+ idR +'" checked >';
					str += '<span class="dlg_checkboxtext">'+strRName+'</span><br>';
					if (i != 0) { strRecipList += ':'; }
					strRecipList += idR;
				}
				// <!-- ROW: Other Distribution Groups -->
				var nGroupsToList = aPTypes.length - aMyPTypes.length;
				if (nGroupsToList < 24) {
					// OTHER POSSIBLE RECIPIENTS:
					//		-- create 'unchecked' checkbox for each of the user's owned groups 
					//		-- BUT only if a group is not already in recipient list
					for (var i = 0; i < aPTypes.length - 1; i++) {
						var aOG = aPTypes[i].split('|');
						var idOG 	  = aOG[0];
						var strOGName = aOG[1];
						if (!idInList(idOG,strRecipList)) {
							str += '<input type="checkbox" name="GROUPIDLIST" value="'+ idOG + '" class="dlg_checkbox" >';
							str += '<span class="dlg_checkboxtext">';
							str += strOGName;
							str += '</span><br>';
						}
					}
					str += '</td>';
					str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
				} else {
					// list as select control -- if more than 24 groups
					str += '<select name="GROUPIDLIST" size="10" multiple class="dlg_select">';
						for (var i = 0; i < aPTypes.length - 1; i++) {
							var aOG = aPTypes[i].split('|');
							var idOG 	  = aOG[0];
							var strOGName = trimStringCenter(aOG[1],45);
							if (!idInList(idOG,strRecipList)) {
								str += '<option value="'+idOG+'">'+strOGName;
							}
						}
					str += '</select>';
					str += '</td>';
					str += '<td class="dlg_tdR" valign="top">';
						str += 'Products in selected categories will get a link to this article.<br><br>Select multiple categories by holding down the control key while you click.';
					str += '</td>';
				}
			str += '</tr>';
		str += '</table>';
		str += '</div>';
		document.writeln(str);
	}
	
	
	// Creates bottom part of dialog with Editing Password and Submit/Cancel buttons
	function drawDlgPasswordAndSubmit(fDrawPassword, strSubmitButton, strSubmitNotice) {
		var str = '';
		// <!-- TABLE: Ending Controls ---------------------------- -->
		str += '<table cellpadding="0" cellspacing="0" class="dlg_table">';
		if (fDrawPassword) {
		// <!-- ROW: divider bar -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					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>';
		// <!-- ROW: Editing Password -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">';
				str += 'Password';
				str += '</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<input type="password" name="PASSWORD" maxlength="30" class="dlg_password">';					
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">';
				str += getInfoBoxHTML("PRESET-EditPassword", 100);
				str += '</td>';
			str += '</tr>';
		}
		// <!-- ROW: divider bar -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					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>';
		// <!-- ROW: Buttons -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<div class="dlg_conformity">';
						if ((strSubmitNotice == null) || (strSubmitNotice == '')) {
							str += "The information I am about to post conforms with the rules for appropriate use of the site.<br>";
						} else {
							str += strSubmitNotice;
						}
					str += '</div>';
					str += '<input type="SUBMIT" value="'+strSubmitButton+'" class="dlg_button">&nbsp;';
					str += '<input type="button" name="Cancel" value="Cancel" onClick="window.close();" class="dlg_button">';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
			str += '</tr>';
		str += '</table>';
		document.writeln(str);
	}

	//	Creates bottom part of simple dialog with just single Cancel button
	function drawDlgCancelOnly(nMsgType) {
		var str = '';
		var strButtonTitle = "Cancel";
		if (nMsgType == 1) { strButtonTitle = "Close This Window";	}
		// <!-- TABLE: Ending Controls ---------------------------- -->
		str += '<table cellpadding="0" cellspacing="0" class="dlg_table">';
		// <!-- ROW: Buttons -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0"><br>';
					str += '<input type="button" name="Cancel" value="'+strButtonTitle+'" onClick="window.close();" class="dlg_button">';
				str += '</td>';
				str += '<td class="dlg_tdR" valign="top">&nbsp;</td>';
			str += '</tr>';
		str += '</table>';
		document.writeln(str);
	}
	
	function drawDlgDividerRow() {
		var str = '';
		// <!-- ROW: divider bar -->
			str += '<tr>';
				str += '<td class="dlg_tdL"  valign="top">&nbsp;</td>';
				str += '<td class="dlg_tdC"  valign="top">';
					str += '<img src="dialogs/dlg_div.gif" width="301" height="7" alt="" border="0">';
				str += '</td>';
				str += '<td class="dlg_tdR"  valign="top">&nbsp;</td>';
			str += '</tr>';
		document.writeln(str);
	}
	
	// Create Info Icon used to pop-open info text in a dialog
	function drawInfoBoxHTML(strInfoText, iBox) {
		document.writeln(getInfoBoxHTML(strInfoText, iBox));
	}
	// Create Info Icon used to pop-open info text in a dialog
	function getInfoBoxHTML(strInfoText, iBox) {
		var str = '';
		var strT = strInfoText;
		// look for preset messages and fill in accordingly
		if 		  (strInfoText == "PRESET-ReplyEmail") {
			strT = "E-Mail address associated with this posting (optional). Readers may send e-mail to this address."; 
		} else if (strInfoText == "PRESET-MultiSelect") {
			strT = "Select multiple items by holding down the control key while you click."; 
		} else if (strInfoText == "PRESET-WebLink") {
			strT = "Web address related to this posting (optional). Readers will be able to link to this address in a new browser window."; 
		} else if (strInfoText == "PRESET-WebLinkPlus") {
			strT = "Web address related to this posting (optional). These links will appear in the main window. External site links will show up in a new browser window."; 
		} else if (strInfoText == "PRESET-EditPassword") {
			strT = "Required to make changes to this page."; 
		} 
		// create info icon
		str += '<a href="javascript:dlgShowInfo(';
		str += "'dlg_popinfo"+iBox+"'";
		str += ')" class="dlg_infohead">';
		str += '<img src="dialogs/info-icon.gif" width="14" height="14" alt="" border="0">';
		str += '</a><br>';
		// create popopen info text
		str += '<div id="dlg_popinfo'+iBox+'">';
		str += '<div class="dlg_infotext">'+strT+'</div>';
		str += '</div>';
		return (str);
	}


	// Creates the Wait Message box that is displayed with user hits submit button
	function drawWaitMessage(strTitle) {
		var str = '';
		str += '<div id="dialogwaitmsg">';
			str += '<div class="dialogwait">';
			str += strTitle+'<br>';
			str += 'In Progress...<br>';
			str += '<br>';
			str += 'Please wait until the page changes...<br>';
			str += '<br>';
			str += 'This can take a minute depending ';
			str += 'on how slow your internet connection is right now...';
			str += '</div>';
		str += '</div>';
		document.writeln(str);
	}



