﻿var win

function AddLoadEvent(func)
{	
    var oldonload = window.onload
	
	if (typeof window.onload != 'function')
	{
	    window.onload = func
	}
	else
	{	
	    window.onload = function()
		{	
		    oldonload()
			func()
		}
	}
}

function ToggleFAQ(link)
{	
    var answer = GetNextSibling(GetParent(link))
	
	if (answer.className == "Hidden")
	{	
	    Show(answer)
	}
	else
	{	Hide(answer)
	}
}

function Hide(what)
{
    what.className = "Hidden"
}

function Show(what)
{
    what.className = "Visible"
}

//Firefox fix for nextSibling
function GetNextSibling(startBrother)
{	
    endBrother = startBrother.nextSibling
	
	while(endBrother.nodeType != 1)
	{
	    endBrother = endBrother.nextSibling
    }
	
	return endBrother
}

function GetParent(child)
{	
    if (child.parentElement)
	{	
	    return child.parentElement
	}
	else if (child.parentNode)
	{	
	    return child.parentNode
	}
}

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 GetListValue(list)
{	
    if (typeof list == "string")
	{	list = document.getElementById(list)
	}
	
	return list.options[list.selectedIndex].value
}

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 RemoveSpaces(str)
{	str = str.toString()
	return str.replace(/ /g, "")
}

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)
}

//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)
}

function MaskNumbers(evt)
{	
    evt = (evt) ? evt : event
    var charCode = GetCharCode(evt)
    
    if (charCode > 31 && (charCode < 48 || charCode > 57)) //Not a number
    {	
        return false
    }
    
    return true
}

function GetCharCode(evt)
{	
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0))
	return charCode
}

// 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
}


