// This file is used for javascript functions that have no real use outside of typensave

// the following variable is used in any situation where something is going to use the bottomleftpanel, 
// it should always be identical to the layout in function loadingbar which is found in common.asp
// make changes to function loadingbar when join the lines and escape out quotes; to update this variable
$leftbottomlayout = "<body bgcolor=\"#ffffff\" text=\"#000000\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\"><table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td background=\"/images/layout/sidebar.jpg\" valign=\"top\"><img src=\"/images/loading.gif\" width=\"100%\" border=\"0\" height=\"5\" /></td></tr></table><span name=\"scriptArea\" id=\"scriptArea\"></span></body>";

var ns4=(document.layers)?true:false;
var ns6=(document.getElementById&&!document.all)?true:false;
var ie4=(document.all)?true:false;


function setRecWindow($obj){
	top.$recWindow = $obj;
}

function hide_select_lists($formName,$show,$hide){
	$form = document[$formName];
	for($ii=0;$ii<$form.length;$ii++){
		$this = $form[$ii];
		if($this.type=="select-one"){
			if(($hide) && (typeof($hide)=="string") && ($this.name!=$hide)) {continue;}
			if(($hide) && (typeof($hide)=="object") && (inArray($hide,$this.name))) {continue;}
			throwStyle($this.name,"visibility",((!$show)?((ns4)?"hide":"hidden"):((ns4)?"show":"visible")));			
		}
	}
}

function disable_select_lists($formName,$show,$hide){
	$form = document[$formName];
	for($ii=0;$ii<$form.length;$ii++){
		$this = $form[$ii];
		if($this.type=="select-one"){
			if(($hide) && (typeof($hide)=="string") && ($this.name!=$hide)) {continue;}
			if(($hide) && (typeof($hide)=="object") && (inArray($hide,$this.name))) {continue;}
			$form[$this.name].disabled = ((!$show)?1:0);			
		}
	}
}


function updateList($xf){
	if(checkaction(true)){ return; } else { $action_in_progress = 1; }
	$theForm = document.theListForm;
	$valList = makeList("theList","theListForm","value");
	$textList = makeList("theList","theListForm","text");
	$output = "";
	$output += $leftbottomlayout;
	$output += "<form name=\"updateForm\" action=\"/recipemgmt.asp\" method=\"post\">";
	$output += "	<input type=\"hidden\" name=\"layout\" value=\"none\" />";
	$output += "	<input type=\"hidden\" name=\"act\" value=\"update\" />";
	$output += "	<input type=\"hidden\" name=\"xf\" value=\"" + $xf + "\" />";
	$output += "	<input type=\"hidden\" name=\"formView\" value=\"1\" />";
	$output += "	<input type=\"hidden\" name=\"valueList\" value=\"" + $valList + "\">";
	$output += "	<input type=\"hidden\" name=\"textList\" value=\"" + $textList + "\">";
	$output += "</form>";
	$output += "<script>document.updateForm.submit();</script>";
	top.bottomFrame.document.write($output);
}

function deleteSubCat($id,$xf,child,$saveRecipes){
	if(checkaction(true)){ return; } else { $action_in_progress = 1; }
	local = (child)?window.opener.top.bottomFrame.document:top.bottomFrame.document;
	local.location = "/recipemgmt.asp?layout=none&id="+$id+"&act=del&xf="+$xf+"&saveRecipes=" + $saveRecipes;
}

function addCatSubMenuItem($newName,$xf,$parent){
	// if this function is being called from anywhere other then the bottomFrame, then address the call TO the bottomFrame. 
	var $doc = (window.name=="bottomFrame")?document:top.bottomFrame.document;
	$doc.location = "/recipemgmt.asp?layout=none&act=add&xf="+$xf+"&formView=1&parent="+$parent+"&newName="+escape($newName);	
}


/*
	******************************************************************
		FUNCTION:		makeList
		PURPOSE: 			builds ``|| seperated list of the items of a select box
		AUTHOR:			Scott McDonald
		VERSION: 			1.0
		LAST EDIT:		Monday, July 22, 2002
		RETURNS:			string list
		ARGUMENTS: 
			oName: object name
			fName: form name
			valText: build list from which part of the list
				'text': 		make list from visible text in selected list
				'value': 	make list from tag value of select list
	*******************************************************************
*/

function makeList(oName,fName,valText) { // build value list, seperated by comma's
	var j, val;
  val = "";
  for (j=0; j<document[fName][oName].length; j++) {
    if (val > "")val += "``||";
    if (document[fName][oName].options[j][valText] > "") val += formEncode(document[fName][oName].options[j][valText]);
  } 
  //alert(val);
   return val;
}

function closeIt() {
	event.returnValue = "Any values here forces a dialog box to appear before closing the window.";
}

function markRecipeAsProofed($recid, adding_proof){
	
	var local = (window.opener)?window.opener.top.mainFrame:top.mainFrame;
	if(!local.MM_findObj("div_r" + $recid + "_actions")){return;}
	var innertext = local.getInner("div_r" + $recid + "_actions");
	if(adding_proof && innertext.indexOf("(proofed)")==-1) {
		local.alterText("div_r" + $recid + "_actions",innertext + " <span class=\"subs\" style=\"color:blue;font-style:italic\">(proofed)</span>");
	} else if (!adding_proof && innertext.indexOf("(proofed)")!=-1) {
		local.alterText("div_r" + $recid + "_actions",innertext.replace("(proofed)",""));
	}
}

function markRecipeAsEdited($recid){
	var local = (window.opener)?window.opener.top.mainFrame:top.mainFrame;
	if(!local.MM_findObj("div_r" + $recid + "_actions")){return;}
	var innertext = local.getInner("div_r" + $recid + "_actions");
	
	if(innertext.indexOf("(edited)")==-1) {
		local.alterText("div_r" + $recid + "_actions",innertext + " <span class=\"subs\" style=\"color:blue;font-style:italic\">(edited)</span>");
	} 
}

/******************************************

*******************************************/
function checkaction(showmessage){
	if(!$action_in_progress){
		return false;
	} else {
		// alert(top.bottomFrame.location.toString());
		if(showmessage) alert("An previous action is in progress, please wait a moment and retry.");
		return true;
	}
}

function open_recipeWindow(demo,recipeid,closeFirst,view,$title,$link){
	if(view) {
		var $address = "display_recipe.asp?layout=alt&recipeid="+recipeid+"&title="+escape($title) + "&_link_=" + $link;
	} else {
		var $address = "addrecipe.asp?layout=alt" + ((demo)?"&demo=true":"") + ((recipeid>0)?"&recipeid=" + recipeid:"") + "&_link_=" + $link;
	}


		
		
	var $opened = false;
	$opened = (!!top.$recWindow)?true:$opened;
	$opened = (typeof(top.$recWindow)!='undefined')?true:$opened;
	if($opened){$opened = !top.$recWindow.closed;}

	if($opened) { 
		if(closeFirst){
			top.$recWindow.close();
		} else {
			if (confirm('Recipe window already open. Do you want to close the current recipe and open another one?')) {
				top.$recWindow.document.location = "/addrecipe.asp?clearRecipe=1";
			} else {
				top.$recWindow.focus();
				return false;
			}
		}
	}

	// attempt to open the new window and then assign the pointer to that window to the main frame page wherre
	// all frames will be able to access it convienently
	try { 
		top.$recWindow = window.open($address,'recipeWindowPopup','width=750,height=700,scrollbars=yes,resizable=yes,location=no,status');
		top.$recWindow.focus();
		return top.$recWindow;
	} 
	// otherwise, we need to re-establish connection with all the windows. the easiest way to do this is to reload the frameset... 
	// so lets set a cookie to let the main window know that this is what we're doing, so it can trigger an addrecipe window as soon
	// as the reload is done
	catch(e) {
		document.cookie = 'openAddRecipeOnLoad=true'
		top.location.reload();
		return;
	}
}

/*
	******************************************************************
		FUNCTION:		openRecipe
		PURPOSE: 			open display recipe window
		AUTHOR:			Scott McDonald
		VERSION: 			2.0
		RETURNS:			none
		ARGUMENTS: 
			$id  					recipe idea?
			$title				title of the recipe, to use as the title of the pop-up
		Revision History:		
			Monday, July 22, 2002 - Built
			Thursday, September 09, 2004 - changed to alias to the open_recipeWindow function which will allow for tracking of recipe window and only allow one window at a time.
		
	*******************************************************************
*/
function oRec($id,$title,$link){return open_recipeWindow(false,$id,false,true,$title,$link);}	

/*
	******************************************************************
		FUNCTION:		aRec
		PURPOSE: 			build pop-up window for changing/deleting recipe
		AUTHOR:			Scott McDonald
		VERSION: 			1.0
		LAST EDIT:		Monday, July 22, 2002
		RETURNS:			none
		ARGUMENTS: 
			$id  					recipe idea?
			$act					what action to take? 'delete' or 'edit'
			$span				
	*******************************************************************

function aRec($id,$act,$span){
	$width=772;
	$height=500;
	$layout = "alt";
	top.editWindow = window.open("addrecipe.asp?layout=" + $layout + "&recipeid="+$id+"&recAction="+$act,"edit_window","width="+$width+",height="+$height+",scrollbars=yes,resizable=yes,location=no");
	top.editWindow.moveTo(0,0);
	return top.editWindow;
}
*/
function aRec($id,$title){
	open_recipeWindow(false,$id,false,false,$title,'js_a_rec');
}

// confirmation for recipe delete... then calls above aRec()
//deleteSubCat('<%=recipeid%>','recs'
function dRec($id,$act,$span){
	if(confirm("Are you sure you want to delete this recipe?")){
		deleteSubCat($id,'recs',0);
	} else {
		return false;
	}
}

function OpenMenus(){	
	$pagename = pagename(top.mainFrame.location);
	switch(_trim($pagename.toLowerCase())){
		case "booksettings.asp":
			top.menuFrame.P7_trigMenuMagic1('p7menu1',1);
			break;
		case "recipemgmt.asp":
			top.menuFrame.P7_trigMenuMagic1('p7menu1',1);
			break;
		case "submit.asp":
			top.menuFrame.P7_trigMenuMagic1('p7menu1',1);
			break;
		case "download.asp":
			top.menuFrame.Q_highlight_menu('p7menu5');
			break;
		case "listrecipes.asp":
			top.menuFrame.P7_trigMenuMagic1('p7menu3',1);
			break;
		case "requestpdf.asp":
			top.menuFrame.Q_highlight_menu('p7menu4');
			break;
		default:
	}			
}	

function recipe_selected($catid){
	$form = top.mainFrame.document["cat_" + $catid + "_form"];
	for($i=0;$i<$form.elements.length;$i++){if($form.elements[$i].type=="checkbox"){if($form.elements[$i].checked==1){return 1;}}}
	return 0;
}

function reloadFrames(){
	// first we're going to check the top frame. if the location of the main frame is
	// home.asp, emailsupport.asp or login.asp and the images are not rolled over, 
	// then we need to reload the page.
	thispage = pagename(document.location);
	// first check to see if the current page is one of the three pages that affect the header
	if((thispage == "home.asp") || (thispage == "emailsupport.asp") || (thispage == "faq.asp") || (thispage == "login.asp") || (thispage == "booksettings.asp")) {
		// automatically reload the header if the mainFrame page is login.asp
		top.headerFrame.location.reload();
		if(thispage != "booksettings.asp"){ top.menuFrame.Q_highlight_menu(); }
	} else {
		// if the current page is none of the pages in the headerfile then we need to make sure 
		// that we don't have any buttons showing as selected... if any are found selected, then 
		// we need to reload the frame. otherwise leave it alone.
		
		if(top.headerFrame.document.homeButton.src.indexOf("_over")!=-1) top.headerFrame.document.location.reload();
		if(top.headerFrame.document.emailSupportButton.src.indexOf("_over")!=-1) top.headerFrame.document.location.reload();
		if(top.headerFrame.document.faqButton.src.indexOf("_over")!=-1) top.headerFrame.document.location.reload();
	}
	
	// moving on to menuFrame. 
	// menu frame has set several variables which we will check to see if there is a need for a reload
	// these are rights, lock_add_recipe, allowcomm, allowcont and bookid
	// if any of these are set differently to the values on the mainFrame variables of the same 
	// name, then we need to reload the side frame. To change the variables which will trigger 
	// a reload besure to add them to both side_menu.asp AND layout.asp::main_layout()
	
	if (
			(top.menuFrame.$bookid != $bookid) ||
			(top.menuFrame.$rights != $rights) ||
			(top.menuFrame.$allowadd != $allowadd) ||
			(top.menuFrame.$allowcomm != $allowcomm) ||
			(top.menuFrame.$allowcont != $allowcont) ||
			(top.menuFrame.$bookreccount != $bookreccount) ||
 			(top.menuFrame.$setupdone != $setupdone) || 
			(top.menuFrame.$complete != $complete)
		) {top.menuFrame.document.location.reload();}
}

function open_menu(){
	alert("not done yet");
}

function helptext($address){
	$url = "/helpfiles/" + $address + (($address.indexOf("?")==-1)?"?layout=help":"&layout=help");
	var $macnn = (agent().mac&&agent().netscape);
	msgbox($url,521,283,"","",true,true,false,true);
}

function pagehelp($name,$file){
	if(!getCookie($name) || getCookie($name)=="false"){
		helptext($file);
	}
}

function multiselected($obj){
	if(typeof($obj)=="undefined") return;
	var list = new Array();
	for(var $i=0;$i<$obj.length;$i++){if($obj[$i].selected) list[list.length] = $i;}
	return list.join(",");
}

// site spacific
function check_changes($form,$button){
	if(!$thisform.check_elements()){
		$form.changes_to_form.value=1;
		if($j == "1"){$form[$button].value='Save...';}
	} else {
		$form.changes_to_form.value=0;
		$form[$button].value='Next';
	}	
}
function help_security_issues(){
	if(!!top.mainFrame) {
		top.mainFrame.location = "/helpfiles/help_security_issues.asp";
	} else if(!!window.opener) {
		window.location = "/helpfiles/help_security_issues.asp?layout=alt";
	}
}

function helpbutton($address){	
	var $thisIndex = $arrHelpButtons.length
	$arrHelpButtons[$thisIndex] = $address;	
	var $output = "";
	$output += "<div name=\"helpbutton_" + $thisIndex + "_layer\" id=\"helpbutton_" + $thisIndex + "_layer\">";
	if($showHelpLinks){
		$output += "<table cellpadding=\"0\" cellspacing=\"0\">";
		$output += "<tr>";
		$output += "<td>";
		$output += "<a href=\"javascript:;\" onclick=\"helptext(\'" + $address + "\');return false;\">";
		$output += "<img name=\"helpbutton_" + $thisIndex + "_button\" id=\"helpbutton_" + $thisIndex + "_button\" src=\"/images/nav/question_up.gif\" border=\"0\">";
		$output += "</a>";
		$output += "</td>";
		$output += "<td>&nbsp;&nbsp;&nbsp;</td>";
		$output += "</tr>";
		$output += "</table>";
	} else {
		$output += "<span style=\"font-size:1px\">&nbsp;</span>";
	}
	$output += "</div>";
	return $output;
}



function helpButtonToggle(){
	$showHelpLinks = (($showHelpLinks)?false:true);
	for(var $i=0;$i<$arrHelpButtons.length;$i++){
		var $output = "";
		$output += "<table cellpadding=\"0\" cellspacing=\"0\">";
		$output += "<tr>";
		$output += "<td>";
		$output += "<a href=\"javascript:;\" onclick=\"helptext(\'" + $arrHelpButtons[$i] + "\');return false;\">";
		$output += "<img name=\"helpbutton_" + $i + "_button\" id=\"helpbutton_" + $i + "_button\" src=\"/images/nav/question_up.gif\" border=\"0\">";
		$output += "</a>";
		$output += "</td>";
		$output += "<td>&nbsp;&nbsp;&nbsp;</td>";
		$output += "</tr>";
		$output += "</table>";
		alterText("helpbutton_" + $i + "_layer",(($showHelpLinks)?$output:"<span style=\"font-size:1px\">&nbsp;</span>"));
		if($showHelpLinks){if(typeof(document["helpbutton_" + $i + "_button"])=="object"){setTimeout("document.helpbutton_" + $i + "_button.src=document.helpbutton_" + $i + "_button.src",200);}}
	}
	
	alterText("showHelpButtonToggleArea",(($showHelpLinks)?"Hide Help Buttons":"Show Help Buttons"));
	if($showHelpLinks){
		
		setTimeout("document.toggleHelpImage.src = qButton.src;",100);
	} else {
		setTimeout("document.toggleHelpImage.src = no_qButton.src;",100);
	}


	setCookie("showHelpLinks",$showHelpLinks);
}

function checkyelling($value){
	$isYelling=isYelling($value,50);
	if($isYelling.yelling){
		if($isYelling.percentcaps==100){
			alert('Fields shouldn\'t be typed in all caps.');
			$value = $value.toLowerCase();
		} else {
			alert(_int($isYelling.percentcaps) + '% of this field is in Capital Letters. This may detract\nfrom the appearence of your cookbook and should be avoided.');
		}
	}
	return $value;
}

function recipemgmt_option($type,$id){
	// open up the alert.asp page and tell it what we're doing. 
	top.modless_window = window.open("alert.asp?layout=none&act=del&what=" + $type + "&id=" + $id,"alertwindow","width=500, height=250, scrollbars=yes, resizable=yes");
}

function edit_displayed_recipe($id,$title){
	window.name='thisWindowWillSelfDistruct';
	open_recipeWindow(false,$id,true,false,"","edit_displayed_recipe");
	top.$recWindow.opener = window.opener;
	window.close();
}

function checkValidWindow(){
    //In FireFox, sometines window.opener is null when it shouldn't be so I
    //removed the || !window.opener from the if so the edit recipe works correctly
    // on the preview page after adding a recipe - Brent L. 7/19/2007
    
	// check to make sure that the add/edit recipe window has a name... AND an opener
	//if(window.name=="typensave_main_window"||!window.opener)
	if(window.name=="typensave_main_window")
	{
		top.document.location = "/";		
		top.open_recipeWindow(0,0,0,0,0,'check_Valid_Window');
	} 
	else 
	{
		window.focus();
	}
}

function recipeWindow_onNewRecipe(recId,recTitle,recCat,recSub,arrRecContrib,recSymbol){
	if((!window.opener.closed) && (typeof(window.opener.top.$recWindow)=="object")){
		opener.top.bottomFrame.onNewRecipe(recId,recTitle,recCat,recSub,arrRecContrib,recSymbol);
	}
}

function onNewRecipe(recId,recTitle,recCat,recSub,arrRecContrib,recSymbol){
	//alert(recId + " : " + recTitle + " : " + recCat + " : " + recSub + " : " + arrRecContrib + " : " + recSymbol)
	// get the location of the mainFrame
	var mainLocal = top.mainFrame.location.toString();
	if(mainLocal.indexOf("home.asp")!=-1) top.mainFrame.location.reload();	
	if(mainLocal.indexOf("listrecipes.asp")!=-1){
		// the active page in the mainFrame is listrecipes we are now going to 
		// send this information to the $list object and let it do whatever it wants with 
		// the information.
		top.mainFrame.$objList.postAddRecipe(recId,recTitle,recCat,recSub,arrRecContrib,recSymbol);		
	}
}


/**
* function used to send a pdf proof without having to reload the page
* this is a beginning effort on ultamatly building TNS to work with 100%
* ajax like structure.
* @author Scott MCDonald
* @created Friday, April 14, 2006
*/
	function sendPDFEmail(outputLayer){
		alterText(outputLayer,"Sending Request...");
		var image = new Image()
		image.onerror = function(){
			alterText(outputLayer,"Request Failed...");
		}
		image.onload = function(){
			alterText(outputLayer,"Request Received: An email containing your PDF proof has been sent.");
		}

		image.src = "/images/ajax.asp?action=emailpdf&date="+ escape(new Date());
	}
//~