var win

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount)
	if(isNaN(i))
	{	i = 0.00
	}
	var minus = ''
	if(i < 0)
	{	minus = '-'
	}
	i = Math.abs(i)
	i = parseInt((i + .005) * 100)
	i = i / 100
	s = new String(i)
	if(s.indexOf('.') < 0)
	{	s += '.00'
	}
	
	if(s.indexOf('.') == (s.length - 2))
	{	s += '0'
	}
	s = minus + s
	return "$" + CommaFormatted(s)
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}


function ToggleMenu(id)
{	var subMenu = document.getElementById("SubMenu" + id)
	if (subMenu.className == "Hide")
	{subMenu.className = "Show"}
	else
	{subMenu.className = "Hide"}
	
	var rpc = new Image()
	rpc.src = "/common/rpc.aspx?m=" + id
}

function Toggle(contID)
{	var answer = document.getElementById(contID)
	if (answer.className == "Hide")
	{answer.className = "Show"}
	else
	{answer.className = "Hide"}
}

function ToggleDownloads(contID)
{	var product = document.getElementById(contID)
	if (product.className == "Show")
	{product.className = "Hide"}
	else
	{	var products = document.getElementById("products")
		for (var i=0; i<products.childNodes.length; i++)
		{	if (products.childNodes[i].className == "Show")
			{	products.childNodes[i].className = "Hide"
				break
			}
		}
		product.className = "Show"
	}
}

function ToggleFAQ2(link)
{	var answer = GetNextSibling(GetParent(link))
	if (answer.className == "Hide")
	{	answer.className = "Show"
	}
	else
	{	answer.className = "Hide"
	}
}

function GetParent(child)
{	if (child.parentElement)
	{	return child.parentElement
	}
	else if (child.parentNode)
	{	return child.parentNode
	}
}

//Firefox fix for nextSibling
function GetNextSibling(startBrother)
{	endBrother = startBrother.nextSibling
	while(endBrother.nodeType != 1)
	{endBrother = endBrother.nextSibling}
	return endBrother
}

function RemoveSpaces(str)
{	str = str.toString()
	return str.replace(/ /g, "")
}


function ReadNewsletter(id)
{	var w = 600, h = 600

	if (document.body.clientWidth)
	{	l = (document.body.clientWidth -w) /2
		t = (document.body.clientHeight - h) /2
	}
	else if (window.innerWidth)
	{	l = (window.innerWidth -w) /2
		t = (window.innerHeight - h) /2
	}
	else
	{	t = 100; l = 100
	}
	var props = "left= " +l
	props += ", top=" +t
	props += ", width=" +w
	props += ", height=" +h
	props += ", scrollbars=yes"
	props += ", menubar=no"
	props += ", resizable=no"
	props += ", toolbar=no"
	props += ", location=no"
	props += ", status=no"
	
	if (win) {win.close()}
	win = open("reader.aspx?n=" + id, "", props)
}

function PopWin(page, w, h)
{	if (document.body.clientWidth)
	{	l = (document.body.clientWidth -w) /2
		t = (document.body.clientHeight - h) /2
	}
	else if (window.innerWidth)
	{	l = (window.innerWidth -w) /2
		t = (window.innerHeight - h) /2
	}
	else
	{	t = 100; l = 100
	}
	var props = "left= " +l
	props += ", top=" +t
	props += ", width=" +w
	props += ", height=" +h
	props += ", scrollbars=no"
	props += ", menubar=no"
	props += ", resizable=no"
	props += ", toolbar=no"
	props += ", location=no"
	props += ", status=no"
	
	if (win) {win.close()}
	win = open(page, "", props)
}

function ContactMe(to)
{	var url = "mailto:" + to + "@rocktonsoftware.com"
	location.href = url
}

function ToggleFAQ(e)
{	if (!e) e = window.event
	if (e.srcElement) //IE
	{var cont = e.srcElement.childNodes[1]}
	else
	{var cont = e.target.childNodes[1]}
	
	if (cont && cont.className)
	{	if (cont.className == "Hide")
		{cont.className = "Show"}
		else if (cont.className == "Show")
		{	HideAllChildren(cont)
			cont.className = "Hide"
		}
	}
	e.cancelBubble = true; //IE
	if (e.stopPropagation) e.stopPropagation();
}

function HideAllChildren(parent)
{	var i
	if (parent.hasChildNodes())
	{	for (i=0; i<parent.childNodes.length; i++)
		{HideAllChildren(parent.childNodes[i])}
	}
	if (parent.className == "Show") parent.className = "Hide"
}

function AddLoadEvent(func)
{	var oldonload = window.onload
	if (typeof window.onload != 'function')
	{window.onload = func}
	else
	{	window.onload = function()
		{	oldonload()
			func()
		}
	}
}

function UpdateCharCounter(box, label, maxLength)
{	if (box.value.length > maxLength)
	{box.value = box.value.substr(0, maxLength)}
	
	var chars = box.value.length
	var objLabel = document.getElementById(label)
	objLabel.innerHTML = "Characters: " + chars + " of " + maxLength
}

function CheckMouseLeave (element, evt)
{	if (element.contains && evt.toElement)
	{	if (! element.contains(evt.toElement))
		{	HideObject(element.id)
		}
	}
	else if (evt.relatedTarget)
	{	if (! ContainsDOM(element, evt.relatedTarget))
		{	HideObject(element.id)
		}
	}
}

function ContainsDOM (container, containee)
{	var isParent = false
	do
	{	if ((isParent = container == containee))
		break
		containee = containee.parentNode
	}
	while (containee != null)
	return isParent
}

function HideObject(objectID)
{	var object = document.getElementById(objectID)
	object.className = "Hide"
}

function AddItemToList(newItem, listName)
{	var i, list = document.getElementById(listName)
	var itemArray = new Array()
	
	//Add all existing items in the list to an array
	for (i=0; i<list.length; i++)
	{	itemArray[i] = list.options[i].value
	}
	
	//Add the new item to the array
	itemArray.push(newItem)
	
	//Sort the array
	itemArray.sort()
	
	//Clear the list
	list.options.length = 0
	
	//Add sorted items to the list
	for (i=0; i<itemArray.length; i++)
	{	list.options[i] = new Option(itemArray[i], itemArray[i])
		if (itemArray[i] == newItem)
		{	list.selectedIndex = i
		}
	}
}

function MoveRowDown(row)
{	var selRowIndex = row.rowIndex
	var table = GetParent(row)
	
	if (selRowIndex == (table.rows.length -1)) //Last row cannot be moved down
	{	return
	}
	
	var newRow = table.insertRow(selRowIndex +2)
	newRow.id = row.id
	for (i=0; i<row.cells.length; i++)
	{	cell = newRow.insertCell(i)
		cell.style.width = row.cells[i].style.width
		cell.innerHTML = row.cells[i].innerHTML
	}
	
	table.deleteRow(selRowIndex)
}

function MoveRowUp(row)
{	var selRowIndex = row.rowIndex
	var table = GetParent(row)
	
	if (selRowIndex == 0) //First row cannot be moved up
	{	return
	}
	
	var newRow = table.insertRow(selRowIndex -1)
	newRow.id = row.id
	for (i=0; i<row.cells.length; i++)
	{	cell = newRow.insertCell(i)
		cell.style.width = row.cells[i].style.width
		cell.innerHTML = row.cells[i].innerHTML
	}
	
	table.deleteRow(selRowIndex +1)
}

function GetListValue(list)
{	if (typeof list == "string")
	{	list = document.getElementById(list)
	}
	return list.options[list.selectedIndex].value
}

//mode 1: $4,500
//mode 2: $4,500.00
function FormatCurrency(mode, amount)
{	switch (mode)
	{	case 1:
		s = new String(amount)
		break
	
		case 2:
		var i = parseFloat(amount)
		if (isNaN(i)) {i = 0.00}
	
		var minus = ''
		if (i < 0)
		{	minus = '-'
		}
		i = Math.abs(i);
		i = parseInt((i + .005) * 100)
		i = i / 100;
		s = new String(i)
	
		if (s.indexOf('.') < 0)
		{	s += '.00'
		}
		if (s.indexOf('.') == (s.length - 2))
		{	s += '0'
		}
		s = minus + s
		break
	}
	return "$" + AddCommas(s)
}

function AddCommas(s)
{	x = s.split('.')
	x1 = x[0]
	x2 = x.length > 1 ? '.' + x[1] : ''
	var rgx = /(\d+)(\d{3})/
	while (rgx.test(x1))
	{	x1 = x1.replace(rgx, '$1' + ',' + '$2')
	}
	return (x1 + x2)
}

// Start AJAX helper functions
function CreateXmlHttpRequestObject()
{	var xmlHttpObject
	try
	{	xmlHttpObject = new XMLHttpRequest()
	}
	catch (e)
	{	try
		{	xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e)
		{	try
			{	xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e)
			{	xmlHttpObject = null
			}
		}
	}
	return xmlHttpObject
}



