function validateEmail(theAddress)
{
	var returnValue = true;
	var AtSym       = theAddress.indexOf('@');
	var Period      = theAddress.lastIndexOf('.');
	var Space       = theAddress.indexOf(' ');
	var Length      = theAddress.length - 1;  // Array is from 0 to length-1

	// '@' cannot be in first position, Must be at least one valid char btwn '@' and '.'
	// Must be at least one valid char after '.', No empty spaces permitted
	if((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
		returnValue = false;

	return returnValue;
}

function validateDollar(fld) 
{
	var returnValue = true;
	var temp_value = fld.value;
	var Chars = "0123456789.,$";
	
	if (temp_value == "")
	{
		fld.value = "0.00";
		return returnValue;
	}
	
	for (var i = 0; i < temp_value.length; i++)
	{
		if (Chars.indexOf(temp_value.charAt(i)) == -1)
		{
			returnValue = false;
		}
	}
	return returnValue;
} 

function validatePhone(fld)
{
	var returnValue = true;
	var temp_value = fld.value;
	do {
		temp_value = temp_value.replace(" ", "");
	}
	while(temp_value.indexOf(" ") != -1);
	var Chars = "0123456789.-()";
	
	if (temp_value == "")
	{
		return returnValue;
	}
	
	for (var i = 0; i < temp_value.length; i++)
	{
		if (Chars.indexOf(temp_value.charAt(i)) == -1)
		{
			returnValue = false;
		}
	}
	return returnValue;
} 

var remote;
function launchWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "Trotters", size+", scrollbars=1, menubar=1, resizable=1, toolbar=1, left="+yPos+",top="+xPos);
	remote.focus();
}

function launchPlainWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "Trotters", size+", scrollbars=0, menubar=0, resizable=0, toolbar=0, left="+yPos+",top="+xPos);
	remote.focus();
}

var remotePrint;
function launchPrintWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remotePrint = window.open(helpURL, "Trotters", size+", scrollbars=1, menubar=1, resizable=1, toolbar=1, left="+yPos+",top="+xPos);
	remotePrint.focus();
}

var remoteMenu;
function launchPrintMenu(categoryID, menuID)
{
	//alert(chapterID);
	var w = 600;
	var h = 425;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remoteMenu = window.open("/togo/cuisine/printableMenu.asp?menuID=" + menuID + "&categoryID=" + categoryID + "&default=off", "printablemenu", "width=" + w + ",height=" + h + ",toolbar=1,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remoteMenu.focus();
}

function trimString(val)
{
	var strLen = val.length;
	
	if(val.lastIndexOf(" ") == strLen-1)
		val = val.substring(0, strLen-1);
	
	if(val.indexOf(" ") == 0)
		val = val.substring(1, strLen);
	
	return val;
}

function ltrim (s){
	return s.replace(/^\s*/, "");
}

function rtrim (s){
	return s.replace(/\s*$/, "");
}

// Combine the rtrim() and ltrim() functions to make the trim() function
function trimString(s){
	return rtrim(ltrim(s));
}

// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	Callouts.fix();
	DocLinks.init();
});

/*------------------------------------------------------------------------+
 | Callouts - Adjust widths of callouts depending on size of image within |
 +------------------------------------------------------------------------*/
var Callouts = {
	fix : function() {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		var bin = document.getElementById("content-primary");
		var arrBins = bin.getElementsByTagName("*");
		var classRE = /call-[lr]/gi;
		
		// Set div width = largest image width
		for (var i = 0; i < arrBins.length; i++) {
			if (classRE.test(arrBins[i].className)) {
				var images = arrBins[i].getElementsByTagName("img");
				
				if (images.length >= 1) {
					var maxWidth = images[0].offsetWidth;
					
					for (var j = 0; j < images.length; j++) {
						var curWidth = images[j].offsetWidth;
						if (curWidth > maxWidth) maxWidth = curWidth;
					}
					
					if (maxWidth > 100) {
						arrBins[i].style.width = maxWidth + "px";
					}
				}
			}
		}
		
		return false;
	}
};


/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "icon";
				newImg.src = "/images/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					return false;
				};
			}
		}
	}
};


/*--------------------------------------------------+
 | Tog - Toggle visibility of two opposing elements |
 +--------------------------------------------------*/
var Tog = {
	swap : function (a, b) {
		a = document.getElementById(a);
		b = document.getElementById(b);
		
		if (!a || !b) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
			
			b.className += " closed";
		} else {
			oldClass = b.className;
			newClass = oldClass.replace(/closed/g, "");
			b.className = newClass;
			
			a.className += " closed";
		}
	},
	toggle : function (a) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
		} else {
			a.className += " closed";
		}
	},
	togglePropertyValue : function (a, prop, value1, value2) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a[prop].indexOf(value1) != -1) {
			a[prop] = value2;
		} else {
			a[prop] = value1;
		}
	}
};
