/*********************************************/
/*              SHARED FUNCTIONS             */
/*********************************************/

function writeDisclaimer(){
	var s = '';
	
	s += '<p class="mp_disclaimer">The Meal Planner is meant to be used along with guidance from your doctor to help you build an individualized program.</p>'
	s += '<br style="clear:both;" /br>';
	
	document.write(s);
}

function clickForAlternatives(pAnchor){
	
	p = {};
	p.loc = "subslistPF.aspx#" + pAnchor;
	p.nochrome = true;
	p.chrome = {}
	p.chrome.scrollbars = "yes";
	p.w = 850;
	openNewWindow(p);
	//subslistPF.aspx#
}


function mp_advancedSearch(){
	
	//alert("function mp_advancedSearch() called in mp_functions.js\n NOT ACTIVE YET");
	
	p = {};
	p.loc = "mpsearch.aspx";
	p.nochrome = true;
	/*p.chrome = {}
	p.chrome.status = "yes";
	p.chrome.scrollbars = "yes";
	*/
	p.h = 450;
	openNewWindow(p);
	

}

// ********************************* //
//        WRITE BLUE BOX	         //
// ********************************* //
function writeTableBox(pTop,pWrite){

	var ws = "";
	if(pTop){
		ws += '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
		ws += '<tr><td class="table_box_top_left"></td><td class="table_box_top"></td><td class="table_box_top_right"></td></tr>';
		ws += '<tr><td class="table_box_left"></td><td class="table_box_back">'
	}else{
		ws += '</td><td class="table_box_right"></td></tr>';
		ws += '<tr><td class="table_box_bottom_left"></td><td class="table_box_bottom"></td><td class="table_box_bottom_right"></td></tr>';
		ws += '</table>';
	}

	if(pWrite){
		document.write(ws);
	}else{
		return ws;
	}
	
}

// ******************************************** //
// PRINT POPUP WINDOW   
// ******************************************** //
function mp_print(){
	window.print();
}

// ******************************************** //
// WRITE A BODY CONTENT LINK   
// p = {};
// p.arrow = boolean will add the orange arrow
// p.id = passed to doBodyContentClick();
// p.write = boolean write or return string
// can redirect or call openNewWindow();
// ******************************************** //
function writeBodyLink(p){

	var ws = (p.arrow)? '<p class="mp_daylinks">' : '';
	ws += '<a class="bodyContentLinks" ';
	//ws += 'href="javascript:void(0);" ';
	ws += 'href="#" ';
	ws += 'onfocus="this.blur();" ';
	ws += 'onclick="doBodyContentClick(\'' + p.id + '\');return false;" ';
	//ws += 'onclick="doBodyContentClick(\'' + p.id + '\', \'' + p.href + '\')" ';
	ws += '>' + p.title + '</a>';
	ws += (p.arrow)? '</p>' : '';
	
	
	if(p.write){
		document.write(ws);
	}else{
		return ws;
	}
	
}

// ******************************************** //
// EXEC A BODY CONTENT LINK   
// p_id = string
// can redirect or call openNewWindow();
// ******************************************** //
function doBodyContentClick(p_id){
	
	pLoc = null;
	
	switch(p_id){
		case "register":
			//pLoc = "/c/account/signup.aspx";
			pLoc = "/c/account/signup.aspx";
			break;
		case "azprivacystatement":
			openNewWindow({loc:"http://www.azprivacystatement.com",name:"az"});
			break;
		case "adobe_reader":
			FollowURL(2, 'http://www.adobe.com/products/acrobat/readstep2.html'); 
			//openNewWindow({loc:"http://www.adobe.com",name:"adobe"});
			//openNewWindow({loc:"http://www.adobe.com/products/acrobat/readstep2.html",name:"adobe"});
			break;
		case "calorie_level":
			p = {};
			p.loc = "calorie_level.aspx";
			p.nochrome = true;
			p.chrome = {};
			p.chrome.scrollbars = "yes";
			p.h = 520;
			openNewWindow(p);
			break;
		default:
		 	//alert("set branching logic for the \"" + p_id + "\" link in mp_functions.js")
			pLoc = (p_id.indexOf(".aspx") == -1)? p_id + ".aspx" : p_id;
			break;
	}
	
	
	if(pLoc != null){
		location.href = pLoc;
	}
	
}


// ***************************************************************************** //
// OPEN A POPUP WINDOW = Default is status only
// pWin = {}
// pWin.name = "window name"
// pWin.w = integer window width
// pWin.h = integer window height
// pWin.chrome = {} contains all chrome settings ({toolbar:'yes',status:'yes'})
// pWin.center = boolean set to false to respect left and top window properties :)
// ***************************************************************************** //
var gWindow = false;
function openNewWindow(pWin){
 
	// close the existing window if one is open //
	/*
	if(gWindow){
		gWindow.close();
		gWindow = false;
	}
	*/
	
	var pChrome = {};
	pChrome.fullscreen = "no";
	pChrome.titlebar = (pWin.nochrome)? "no" : "yes";
	pChrome.toolbar = (pWin.nochrome)? "no" : "yes";
	pChrome.location = (pWin.nochrome)? "no" : "yes";
	pChrome.directories = (pWin.nochrome)? "no" : "yes";
	pChrome.status = "yes";
	pChrome.menubar = (pWin.nochrome)? "no" : "yes";
	pChrome.scrollbars = (pWin.nochrome)? "no" : "yes";
	pChrome.resizable = (pWin.nochrome)? "no" : "yes";
	pChrome.width = (pWin.w)? pWin.w : 800; // Default width for all popups //
	pChrome.height = (pWin.h)? pWin.h : 600; // Default height for all popups //
	pChrome.innerHeight = pChrome.height; // used with firefox //
	pChrome.top = "0";
	pChrome.left = "0";
 
 	// if the name is not defined then use the default name for all windows //
 	if(typeof(pWin.name) == "undefined"){
		pWin.name = "pWindow";
	}
	// loop and change properties //
	if(pWin.chrome){
		for(prop in pWin.chrome){
			pChrome[prop] = pWin.chrome[prop];
		}
	}

	// This will center the window on the screen //
	// center is enabled by default //
	if (pWin.center != false) {
		lMinus = 40;
		lPlus = 10;
		// fix for 800x600 center window //
		if(screen.height < 700){
			lMinus = 0;
			lPlus = 20;
		}

		var ah = screen.availHeight - lMinus;
		var aw = screen.availWidth - 10;
		var x = (aw - pChrome.width) / 2;
		var y = (ah - (pChrome.innerHeight + lPlus)) / 2;
		pChrome.left = x;
		pChrome.top = y;

	}
	// setup the window chrome //
	var lNewChrome = ""
	for(prop in pChrome){
		lNewChrome += prop + '=' + pChrome[prop] + ",";
	}

	// open window and set reference //
	gWindow = window.open(pWin.loc,pWin.name,lNewChrome);
	gWindow.focus();
	//gWindow = window.open(pWin.loc,"pWindow",lNewChrome).focus();
	//var gWindow = window.open(pWin.loc,pWin.name,lNewChrome);

}

// ******************************************** //
// CLOSE A POPUP WINDOW   
// p = {}
// p.id = opener new location?
// p.frame = the frame to redirect (parent,top)
// closes a popup and can redirect the opener
// ******************************************** //
function closeThisWindow(p){

	var pLocation = false;
	if(p){
		switch(p.id){
			case "register":
				//pLocation = "/c/account/signup.aspx";
				pLocation = "/c/account/signup.aspx";
				break;
			default:
				alert("p.id = " + p.id + "\nID not passed to closeThisWindow()");
				break;
		}
	}
	
	if(pLocation){
		if(p.frame){
			/* Break out of the frame? */
			window.opener[p.frame].location.href = pLocation;
		}else{
			window.opener.location.href = pLocation;
		}
	}
	
	window.close();

}

// ***************************************************************************** //
// OPEN A PDF In A POPUP WINDOW
// pLoc = page location
// Assumes form PDFLauncher exists in page (required to get dynamically
// generated PDFs to display.
// ***************************************************************************** //
function openPDFWindow(pLoc){
	
	// SSK 5.11.07 kludge for "Member not found" error with IE .focus() //
	window.open(pLoc,"printpdf","width=800,height=600");
	//openNewWindow({loc:pLoc,name:'printpdf',nochrome:true,w:800,h:600});
	
	document.PDFLauncher.target = "printpdf";
	document.PDFLauncher.action = pLoc;
	document.PDFLauncher.submit();
	
}

// ***************************************************************************** //
// PMG_img() Writes or returns an <img>
// p = {}
// p.graphic = the graphic name
// p.extension = jpg, png, gif
// p.path = allows for a custom path
// p.w = integer width
// p.h = integer height
// p.id = unique id default is ""
// p.cname = unique class default is ""
// p.title = the title tag default is ""
// p.alt = the alt tag default is ""
// p.write = boolean will write or return value
// ***************************************************************************** //

function PMG_img(p){

	var ws = ''
	var lExt = p.ext || "jpg";
	//var lPath = p.path || gPaths.images;
	var lPath = p.path;
	var lGraphic = (typeof(p.graphic) == "object")? p.graphic[Math.round(Math.random()*(p.graphic.length-1))] : p.graphic;
	var lImage = lPath + lGraphic + '.' + lExt;
	
	// Check for special styles //
	var lID = (p.id)? 'id="' + p.id + '"' : '';
	var lClass = (p.cname)? 'class="' + p.cname + '"' : '';
	var lAlt = (p.alt)? 'alt="' + p.alt + '"' : '';
	var lTitle = (p.title)? 'alt="' + p.title + '"' : '';
	
	var lSize = (p.w)? 'width="' + p.w + '" height= "' + p.h + '"': '';		
	ws = '<img ' + lID + lClass + lTitle + ' src="' + lImage + '" ' + lSize + ' vspace="0" hspace="0" border="0" ' + lAlt + ' />';
	
	if(p.inline){
		return ws;
	}else{
		document.write(ws);		
	}
	
	return false;
}


// ******************************************** //
// DEBUG TOOLS   
// p_obj = {}
// Loop and display all object properties
// ******************************************** //
function getVariable_debugTool(p_obj){
    // property check //
	var lA = [];
    for(var prop in p_obj){
		lA.push(prop + " = " + p_obj[prop]);
    }
	// put all the properties in order //
	lA.sort();
	
	var s = "";
	
	for(var i=0; i<lA.length; i++) {
		s += lA[i] + "\n";
	}
	
	if(lA.length > 30){
		//prompt(s,s);
		document.body.innerHTML = s;
	}else{
		alert(s);
	}
}