/*  © Copyright 2001 - 2006 Excira Technologies, Inc. All Rights Reserved.
 * -----------------------------------------------------------------------
 *
 * JS used by sites only
 */

  /**
   *
   */
function meToo(meTooId, uri) {

	// Increment count on UI
	var meTooObj = document.getElementById(meTooId);
	if ( meTooObj ) {
		var count = meTooObj.innerHTML;
		count++;
		meTooObj.innerHTML = count;
	}

	// Increment count on server
	var url = "/gruntsFormList/meToo.do?viewName=gruntForm&subjectURI=" + uri;

	var xmlhttp = getXMLHTTP();
	xmlhttp.open('POST', encodeURI(url), true);
	xmlhttp.onreadystatechange = function() {
	}

	xmlhttp.send(null);
}

 /**
  * 
  */
function save(formName) {

	var widgetName = formName + ".form";
	var form = document.getElementById(widgetName);

	var invalidValues = new Array();
//	invalidValues.push("viagra");
//	invalidValues.push("levitra");
//	invalidValues.push("cialis");

	if ( isValid(form, false, invalidValues) )
		form.submit();
}

 /**
  * 
  */
function ajaxSave(formName) {

	var widgetName = formName + ".form";
	var form = document.getElementById(widgetName);

	var invalidValues = new Array();
//	invalidValues.push("viagra");
//	invalidValues.push("levitra");
//	invalidValues.push("cialis");

	if ( !isValid(form, false, invalidValues) )
		return;

	var iframe = document.getElementById("formHandler");

	form.target = "formHandler";
	form.submit();
}

 /****************************************************************************
 * 
 ***************************************************************************/
var popup = null;
function invite(siteName, path) {

	var id = "invitation";

	// Do not proceed if a popup already exists
	// with this id
	if ( popup && popup.id == id )
		return;

	// If a popup exists, but with a different id,
	// close it!
	if ( popup )
		popup.close();

	// Create new popup
	var titleText = siteName + " : Tell a Friend";
	var defaultText = "Email this page to a friend or associate.";
	popup = new Popup(id, "490px", "popup", titleText, defaultText);

	// Create form
	var form = popup.dom.appendForm(popup, "", "", "", "post", "", "");

	// Create div to contain table
	var tableContainer = popup.dom.appendDiv(form, "", "", "");

	// Create table
	var table = popup.dom.appendTable(tableContainer, "popupForm", "", "");
	var tbody = popup.dom.appendTBody(table, "", "", "");

	// Create row and first cell
	var tr0 = popup.dom.appendTR(tbody, "", "", "");
	var td00 = popup.dom.appendTD(tr0, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td00, "", "", "", "Your Name:");

	// Create next cell
	var td01 = popup.dom.appendTD(tr0, "", "", "");

	// Create name input and append to form
	var requiredRule = new Array();
	requiredRule.push("required");
	popup.dom.appendInput(td01, "text", "fromName", "", "", "", requiredRule);

	// Create next row and cell
	var tr1 = popup.dom.appendTR(tbody, "", "", "");
	var td10 = popup.dom.appendTD(tr1, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td10, "", "", "", "Your Email:");

	// Create next cell
	var td11 = popup.dom.appendTD(tr1, "", "", "");

	// Create from email input and append to form
	var requiredEmailRule = new Array();
	requiredEmailRule.push("required");
	requiredEmailRule.push("email");
	popup.dom.appendInput(td11, "text", "fromEmailInput", "", "", "", requiredEmailRule);

	// Create next row and cell
	var tr2 = popup.dom.appendTR(tbody, "", "", "");
	var td20 = popup.dom.appendTD(tr2, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td20, "", "", "", "Friend's Email:");

	// Create next cell
	var td21 = popup.dom.appendTD(tr2, "", "", "");

	// Create to email input and append to form
	popup.dom.appendInput(td21, "text", "toEmailInput", "", "", "", requiredEmailRule);

	// Create next row and cell
	var tr3 = popup.dom.appendTR(tbody, "", "", "");
	var td30 = popup.dom.appendTD(tr3, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td30, "", "", "", "Message:");

	// Create next cell
	var td31 = popup.dom.appendTD(tr3, "", "", "");

	// Create message text area and append to form
	popup.dom.appendTextArea(td31, "message", "", "", "");

	// Create next row and cell
	var tr4 = popup.dom.appendTR(tbody, "", "", "");
	var td40 = popup.dom.appendTD(tr4, "", "", "popupLeftCol");

	// Create next cell
	var td41 = popup.dom.appendTD(tr4, "", "", "");

	// Create hidden field for path
	var pathInput = popup.dom.appendInput(form, "hidden", "documentPath", "", "", path, new Array());

	// Create submit
	var submit = popup.dom.appendA(td41, "sendButton", "", "", "Send!", "#");
	submit.onclick = function () {
		sendInvitation(popup, form);
		return false;
	}

	// Create privacy container
	var privacyDiv = popup.dom.appendDiv(popup, "", "", "popupPrivacy");

	// Add "show" link to privacy div
	var privacyShowLink = popup.dom.appendA(privacyDiv, "", "", "", "Privacy Policy", "#");

	// Create hidden privacy text
	var privacyP = popup.dom.appendP(privacyDiv, "", "", "popupPrivacyText");
	privacyP.innerHTML = "We hate spam too!<br/><br/>Your privacy is very important to us." +
		" The names and email addresses collected on this form are not saved and not shared. " +
		siteName + " will not send you unsolicited emails.";
	privacyP.style.display = "none";

	// Add br to privacy text
	//popup.dom.appendBR(privacyP);

	// Add "hide" link to privacy text
	//var privacyHideLink = popup.dom.appendA(privacyP, "", "", "", "hide", "#");
	//privacyHideLink.onclick = function() {
	//	popup.toggleChildDisplay(privacyP);
	//}
	
	// Add onclick event to show link
	privacyShowLink.onclick = function() {
		popup.toggleChildDisplay(privacyP);
	}
}

 /****************************************************************************
 * 
 ***************************************************************************/
function sendInvitation(popup, form) {

	if ( isValid(form, false) ) {
		popup.close();
		processInvitation(popup, form);
	}
}



 /****************************************************************************
 * 
 ***************************************************************************/
function processInvitation(popup, form) {

	var fromName = form.elements[0].value;
	var fromEmail = form.elements[1].value;
	var toEmail = form.elements[2].value;
	var message = form.elements[3].value;
	var path = form.elements[4].value;

	var queryString = "fromName=" + fromName + "&fromEmail=" 
		+ fromEmail + "&toEmail=" + toEmail + "&message=" + message + "&path=" + path;

	var xmlhttp = getXMLHTTP();
	xmlhttp.open('GET', encodeURI("/handleInvitation.ajax?" + queryString), true);
	xmlhttp.onreadystatechange = function() {
	}

	xmlhttp.send(null);
 }

 /****************************************************************************
 * 
 ***************************************************************************/
function ask(siteName) {

	var id = "ask";

	// Do not proceed if a popup already exists
	// with this id
	if ( popup && popup.id == id )
		return;

	// If a popup exists, but with a different id,
	// close it!
	if ( popup )
		popup.close();

	// Create new popup
	var titleText = siteName + " : Inquiry"
	var defaultText;
	if ( siteName == "Doing Atkins") {

		defaultText = "Questions or Comments?"
			+ "<br/><br/>"
			+ "If you have a question or comment about the site content "
			+ "or the Atkins Diet, please use the contact form below to post your inquiry."
			+ "<br/><br/>"
			+ "Also please feel free to share any diet tips, problems, or solutions "
			+ "that you have discovered along your way. I'll include them on the site as time permits."
			+ "<br/><br/>"
			+ "Any contact info collected will not be shared with third parties."
			+ "<br/><br/>"
			+ "Thank You,"
			+ "<br/>"
			+ "Dave Rogers"
			+ "<br/>"
			+ "Editor, DoingAtkins.com";
	}
	else {

		defaultText = "Questions or Comments?";
	}
	popup = new Popup(id, "490px", "popup", titleText, defaultText);

	// Create form
	var form = popup.dom.appendForm(popup, "", "", "", "post", "", "");

	// Create div to contain table
	var tableContainer = popup.dom.appendDiv(form, "", "", "");

	// Create table
	var table = popup.dom.appendTable(tableContainer, "popupForm", "", "");
	var tbody = popup.dom.appendTBody(table, "", "", "");

	// Create row and first cell
	var tr0 = popup.dom.appendTR(tbody, "", "", "");
	var td00 = popup.dom.appendTD(tr0, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td00, "", "", "", "Your Name:");

	// Create next cell
	var td01 = popup.dom.appendTD(tr0, "", "", "");

	// Create name input and append to form
	var requiredRule = new Array();
	requiredRule.push("required");
	popup.dom.appendInput(td01, "text", "fromName", "", "", "", requiredRule);

	// Create next row and cell
	var tr1 = popup.dom.appendTR(tbody, "", "", "");
	var td10 = popup.dom.appendTD(tr1, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td10, "", "", "", "Your Email:");

	// Create next cell
	var td11 = popup.dom.appendTD(tr1, "", "", "");

	// Create from email input and append to form
	var requiredEmailRule = new Array();
	requiredEmailRule.push("required");
	requiredEmailRule.push("email");
	popup.dom.appendInput(td11, "text", "fromEmailInput", "", "", "", requiredEmailRule);

	// Create next row and cell
	var tr2 = popup.dom.appendTR(tbody, "", "", "");
	var td20 = popup.dom.appendTD(tr2, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td20, "", "", "", "Telephone:");

	// Create next cell
	var td21 = popup.dom.appendTD(tr2, "", "", "");

	// Create to email input and append to form
	popup.dom.appendInput(td21, "text", "telephoneNumber", "", "", "", new Array());

	// Create next row and cell
	var tr3 = popup.dom.appendTR(tbody, "", "", "");
	var td30 = popup.dom.appendTD(tr3, "", "", "popupLeftCol");

	// Create name label and append to form
	popup.dom.appendSpan(td30, "", "", "", "Message:");

	// Create next cell
	var td31 = popup.dom.appendTD(tr3, "", "", "");

	// Create message text area and append to form
	popup.dom.appendTextArea(td31, "message", "", "", "");

	// Create next row and cell
	var tr4 = popup.dom.appendTR(tbody, "", "", "");
	var td40 = popup.dom.appendTD(tr4, "", "", "popupLeftCol");

	// Create next cell
	var td41 = popup.dom.appendTD(tr4, "", "", "");

	// Create submit
	var submit = popup.dom.appendA(td41, "", "", "", "Send!", "#");
	submit.onclick = function () {
		sendQuestionComment(popup, form);
		return false;
	}

	// Create privacy container
	var privacyDiv = popup.dom.appendDiv(popup, "", "", "popupPrivacy");

	// Add "show" link to privacy div
	var privacyShowLink = popup.dom.appendA(privacyDiv, "", "", "", "Privacy Policy", "#");

	// Create hidden privacy text
	var privacyP = popup.dom.appendP(privacyDiv, "", "", "popupPrivacyText");
	privacyP.innerHTML = "We hate spam too!<br/><br/>Your privacy is very important to us." +
		" The names and email addresses collected on this form are not saved and not shared. " +
		siteName + " will not send you unsolicited emails.";
	privacyP.style.display = "none";

	// Add br to privacy text
	//popup.dom.appendBR(privacyP);

	// Add "hide" link to privacy text
	//var privacyHideLink = popup.dom.appendA(privacyP, "", "", "", "hide", "#");
	//privacyHideLink.onclick = function() {
	//	popup.toggleChildDisplay(privacyP);
	//}
	
	// Add onclick event to show link
	privacyShowLink.onclick = function() {
		popup.toggleChildDisplay(privacyP);
	}
}

 /****************************************************************************
 * 
 ***************************************************************************/
function sendQuestionComment(popup, form) {

	if ( isValid(form, false) ) {
		popup.close();
		processQuestionComment(popup, form);
	}
}

 /****************************************************************************
 * 
 ***************************************************************************/
function processQuestionComment(popup, form) {

	var fromName = form.elements[0].value;
	var fromEmail = form.elements[1].value;
	var telephoneNumber = form.elements[2].value;
	var message = form.elements[3].value;

	var queryString = "fromName=" + fromName + "&fromEmail=" 
		+ fromEmail + "&telephoneNumber=" + telephoneNumber + "&message=" + message;

	var xmlhttp = getXMLHTTP();
	xmlhttp.open('GET', encodeURI("/handleInquiry.ajax?" + queryString), true);
	xmlhttp.onreadystatechange = function() {
	}

	xmlhttp.send(null);
 }

 /****************************************************************************
 * 
 ***************************************************************************/
function openSiteToolLink(url, winTitle, winProps) {

	window.open(url, winTitle, winProps);
	return;
}

/****************************************************************************
 * 
 ***************************************************************************/
function openPrintWindow(uri) {

	var url = "/popupWidget/print.do?subjectURI=" + uri + "&popup=true";
	var printView = 
		window.open(url, "print", "width=500px,height=500px,status=0,toolbar=0,location=0,scrollbars=1,resizable=1");
	printView.print();
}

/****************************************************************************
 * 
 ***************************************************************************/
function highlightRow(obj, highlight) {

	if ( highlight )
		obj.className = "listBodyHighlight";
	else
		obj.className = "listBody";
}

 /****************************************************************************
 * 
 ***************************************************************************/
function apply(siteName, subjectURI, relationURI) {

	var id = "apply";

	// Do not proceed if a popup already exists
	// with this id
	if ( popup && popup.id == id )
		return;

	// If a popup exists, but with a different id,
	// close it!
	if ( popup )
		popup.close();

	// Create new popup
	var titleText = siteName + " : Apply";
	popup = new Popup(id, "584px", "popup", titleText, ""); 

	// Do it...
	var uri = "/apply?" + relationURI + "=" + subjectURI + "&client=true";

	// Execute call to link
	var xmlhttp = getXMLHTTP();
	xmlhttp.open('GET', uri, true);
	xmlhttp.onreadystatechange = function() {

		if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 500) ) {

			popup.innerHTML(xmlhttp.responseText);
		}
	}

	xmlhttp.send(null);
}

 /****************************************************************************
 * 
 ***************************************************************************/
function processFileSelected(obj) {

}

/**
 * 
 */
function Menu() {

	this.dom = new DOMManipulator();
	this.menuItems = new Array();
}


/**
 *
 */
Menu.prototype.addMenuItem = function (menuItem) {

	// Add menu item object to array of menu items
	this.menuItems[this.menuItems.length] = menuItem;
};


/**
 *
 */
Menu.prototype.hideMenu = function () {

	if ( !this.layer ) 
		return;

	this.obj.id = "menuItem";

	// Remve the layer and reset values
	this.dom.removeChild(document.body, this.layer);
	this.layer = null;
};


/**
 *
 */
Menu.prototype.showMenu = function (obj) {

	// Do not proceed if there are no menu
	// items
	if ( this.menuItems.length == 0 )
		return;

	// Setup values
	this.obj = obj;
	this.obj.id = "menuItemHover";
	this.createDropDown();
};


/**
 *
 */
Menu.prototype.createDropDown = function () {

	// If the layer has already been created, return
	if ( this.layer )
		return;

	// Otherwise create the layer!
	var oThis = this;

	// Create the layer and assign styles
	this.layer = this.dom.appendDiv(document.body, "", "", "dropDownMenu");
	this.layer.style.width = (this.obj.offsetWidth - 2)+ "px";
	this.layer.style.left = this.getLeft(this.obj) + "px";
	this.layer.style.top = (this.getTop(this.obj) + this.obj.offsetHeight) + "px";
	this.layer.style.display = "block";

	for ( var i = 0 ; i < this.menuItems.length ; i++ ) {

		// Get the menu item and create div and link
		var menuItem = this.menuItems[i];
		var linkDiv = this.dom.appendDiv(this.layer, "", "", "dropDownMenuItem");
		linkDiv.style.width = (this.obj.offsetWidth - 22)+ "px"; // FIX THIS!! THIS IS BAAAAAAD!
		linkDiv.onmouseover = function() {
			this.className = "dropDownMenuItemHover";
		}
		linkDiv.onmouseout = function() {
			this.className = "dropDownMenuItem";
		}
		linkDiv.onclick = function() {
			if ( this.childNodes[0] && this.childNodes[0].href )
				window.location.href = this.childNodes[0].href;
		}
		var link = this.dom.appendA(linkDiv, "", "", "", menuItem.text, menuItem.command);
	}
};


/**
 * Gets the left coordinate of the menu obj.
 */
Menu.prototype.getLeft = function (obj) {

	var left = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		left += obj.x;
	}

	return left;
};


/**
 * Gets the top coordinate of the menu obj.
 */
Menu.prototype.getTop = function (obj) {

	var top = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			top += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		top += obj.y;
	}

	return top;
};


/**
 * 
 */
Menu.prototype.isHover = function (x, y) {

	var hoverObj = false;
	if ( this.obj ) {
		var objX = this.getLeft(this.obj);
		var objY = this.getTop(this.obj);
		hoverObj = ( (x >= objX && x <= (0 + objX + this.obj.offsetWidth)) &&
				(y >= objY && y <= (0 + objY + this.obj.offsetHeight)) );
	}

	var hoverLayer = false;

	if ( this.layer ) {
		var layerX = this.getLeft(this.layer);
		var layerY = this.getTop(this.layer);
		hoverLayer = ( (x >= layerX && x <= (0 + layerX + this.layer.offsetWidth)) &&
				(y >= layerY && y <= (0 + layerY + this.layer.offsetHeight)) );
	}

	return (hoverObj || hoverLayer);
};


 /****************************************************************************
 * 
 ***************************************************************************/

var menus = new Array();
var menu;
var menuItem;
var currentMenu;


/**
 *
 */
function showMenu(obj, menuId) {

	// First force all open menus to close
	for ( var i = 0 ; i < menus.length ; i++ )
		menus[i].hideMenu();

	currentMenu = menus[menuId];
	currentMenu.showMenu(obj);
}


/**
 *
 */
function addMenuListener() {

	if ( document.attachEvent )
		document.attachEvent("onmousemove", updateMenuStatus);
	else
		document.addEventListener("mousemove", updateMenuStatus, false);
}

/**
 *
 */
function removeMenuListener() {

	if ( document.detachEvent )
		document.detachEvent("onmousemove", updateMenuStatus);
	else
		document.removeEventListener("mousemove", updateMenuStatus, false);
}

/**
 *
 */
function updateMenuStatus(evt) {

	if ( !currentMenu )
		return;

	// Get the current x and y coordinate
	if ( window.event ) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	else {
		evt = (evt) ? evt : event;
		x = evt.clientX + window.scrollX;
		y = evt.clientY + window.scrollY;
	}

	// Check if they are within the current menu's area
	if ( !currentMenu.isHover(x, y) ) {
		currentMenu.hideMenu();
		currentMenu = null;
		removeMenuListener();
	}
}


 /****************************************************************************
 * 
 ***************************************************************************/

/**
 * 
 */
function MenuItem(text, command) {

	this.text = text;
	this.command= command;
}