function PrettyMoney(e)
{
	var val = Trim(e.value)
	val = KeepChars("1234567890", val)
	if (val == "")
	{
		e.value = ""
		return
	}
	var out = ""
	var group_count = 0
	for (var i = (val.length - 1); i >= 0; i--)
	{
		if (group_count == 3)
		{
			out = "," + out
			group_count = 0 
		}
		out = val.charAt(i) + out
		group_count++
	}
	e.value = "$" + out
}

function FormatMoney(s)
{
	var val = Trim(String(s))
	val = KeepChars("1234567890", val)
	if (val == "")
	{
		return s
	}
	var out = ""
	var group_count = 0
	for (var i = (val.length - 1); i >= 0; i--)
	{
		if (group_count == 3)
		{
			out = "," + out
			group_count = 0 
		}
		out = val.charAt(i) + out
		group_count++
	}
	return out
}


function ValidateIndexNewsletter(f)
{
	var msg = ""
	var x = Trim(f.email.value)
	if (x == "" || x == "e-mail")
	{
		msg += "Please enter your email address"
	}
	else if (!IsEmail(Trim(f.email.value)))
	{
		msg += "Please enter a valid email address"
	}
	
	if (msg != "")
	{
		alert(msg)
		return 
	}
	
	var newsletter = 0
	var affiliates = 0
	if (f.newsletter.checked)
		newsletter = 1
	if (f.affiliates.checked)
		affiliates = 1
	

	
	var win = window.open(
							"do_newsletter.asp?first_name=" + escape("Lenders Block Customer") +
								"&last_name=" +  
								"&email=" + escape(x) +
								"&newsletter=" + newsletter +
								"&affiliates=" + affiliates,
							"",
							"height=300,width=420" 
							)
				
	f.email.value = "e-mail"
	win.focus()
}


function ValidateNewsletter(f)
{
	var msg = ""
	var x = Trim(f.first_name.value)
	if (x == "" || x == "First Name")
	{
		msg += "Please enter your first name\n"
	}
	x = Trim(f.last_name.value)
	if (x == "" || x == "Last Name")
	{
		msg += "Please enter your last name\n"
	}
	x = Trim(f.email.value)
	if (x == "" || x == "Email")
	{
		msg += "Please enter your email address"
	}
	else if (!IsEmail(Trim(f.email.value)))
	{
		msg += "Please enter a valid email address"
	}
	
	if (msg != "")
	{
		alert(msg)
		return
	}
	
	var win = window.open(
							"do_newsletter.asp?first_name=" + escape(f.first_name.value) +
								"&last_name=" + escape(f.last_name.value) + 
								"&email=" + escape(f.email.value) +
								"&newsletter=1&affiliates=1",
							"",
							"height=300,width=420" 
							)

	f.first_name.value = "First Name"
	f.last_name.value = "Last Name"
	f.email.value = "Email"
	win.focus()
					
}


function OpenWindow(url, height, width)
{
	var h = 480
	var w = 640
	if (height) h = height
	if (width) w = width
	
	window.open(url, "", "height=" + h + ",width=" + w + ",top=100,left=200,scrollbars=yes,resizable=yes")
	
}


// these vars are used during form validation
var r, b
var focusElem = null


function ListReq(e, desc)
{
	if (!e || e.selectedValue == "" || e.selectedIndex == 0)
	{
		r.Add(desc)
		if (!focusElem) focusElem = e
	}
}
function Req(e, desc)
{
	if (!e || Trim(e.value).length == 0) 
	{
		r.Add(desc)
		if (!focusElem) focusElem = e
	}
}
function RadReq(e, desc)
{	
	var buttons = e
	var one_checked = false
	
	if(buttons)
	{
		for (var i = 0; i < buttons.length; i++)
		{
			if (buttons[i].checked)
			{
				one_checked = true
				break
			}
		}
	}
	
	if (!e || !one_checked) 
	{
		r.Add(desc)
		if (!focusElem) focusElem = buttons[0]
	}
}
function IsInterestRate(strIn)
{
	var s = Trim(strIn)
	if (s.length == 0) return true
	if (s.length != KeepChars("1234567890%.", s).length)
		return false		
	var value = parseFloat(KeepChars("1234567890.", s))
	if (isNaN(value) || value > 50)
		return false
	return true
}
function IsMoney(strIn)
{
	var s = Trim(strIn)
	if (s.length == 0) return true
	if (s.length != KeepChars("1234567890$,", s).length)
		return false		
	var value = parseInt(KeepChars("1234567890", s))
	if (isNaN(value))
		return false
	return true
}
function Check(e, test, msg)
{
	if (test == false)
	{
		b.Add(msg)
		if (!focusElem)
			focusElem = e
	}
}
function KeepChars(charsToKeep, strIn)
{
	var strOut = ""
	for (var i = 0; i < strIn.length; i++)
	{
		var thisChar = strIn.charAt(i)
		if (charsToKeep.indexOf(thisChar) != -1)
		{
			strOut += thisChar
		}
	}
	
	return strOut
}

function IsEmail(strIn)
{
	var s = Trim(String(strIn))
	if (s.length == 0) return true
	var pos = s.indexOf("@")
	if (pos > 0 && pos < (s.length - 1))
	{
		return true
	}
	return false
}
function IsNum(strIn, len)
{
	var s = KeepChars("1234567890", Trim(String(strIn)))
	if (len)	
	{
		if (s.length != len)
			return false
	}
	var str = Trim(String(strIn))
	if (str.length != KeepChars("1234567890", str).length)
		return false
	return true
}
function Add(s)
{
	this[this.length] = s
}
function Trim(s)
{
	var strIn = String(s)
	var c
	var curChar = 0
	while (c = strIn.charAt(curChar) == " " && curChar < strIn.length){curChar++}
	var firstNonWhite = curChar

	if (curChar == strIn.length) return ""

	curChar = strIn.length - 1
	while (c = strIn.charAt(curChar) == " " && curChar >= 0){curChar--}
	var lastNonWhite = curChar
	
	return strIn.substring(firstNonWhite, lastNonWhite + 1)
}
