

// Constants


// Validation Functions
function isEmpty(s)
{
	return( ( s == null ) || ( s.length == 0 ) );
}

function isInt(s)
{
	var intExp = /^\d+$/;
	
	if( isEmpty(s) )
		return false;
	
	return( intExp.test( s ) );
}

function isZipCode(s)
{
	var zipExp = /^\d{5}$/;
	
	if( isEmpty(s) )
		return false;
		
	return( zipExp.test( s ) );
}

// Redirection Scripts
function redirectCostSavings(frm, url)
{
	if( !isZipCode(frm.costZipCode.value) )
		return;
	
	var newUrl = "";
	
	if( frm.rbCost.options[frm.rbCost.selectedIndex].value == "D" )
	{
		newUrl = "/cost-compare.aspx";
	}
	else if( frm.rbCost.options[frm.rbCost.selectedIndex].value == "V" )
	{
		newUrl = "/cost-comparev.aspx";
	}
	
	if( newUrl == "" )
	{
		return;
	}
	
	var toUrl =  newUrl + "?zipCode=" + frm.costZipCode.value;

	top.location = toUrl;
}
/*
function redirectCostSavings(frm, url)
{
	if( !isZipCode(frm.costZipCode.value) )
		return;
	
	var insType = frm.rbCost.options[frm.rbCost.selectedIndex].value;
	var toUrl =  url + "?insType=" + insType + "&zipCode=" + frm.costZipCode.value;

	top.location = toUrl;
}
*/

function OpenNewWindow(url, name, options)
{
	var startWindow;
		 var windowOptions;
		 var windowName;
		 
		 windowName = name;
		 windowOptions = options;
		 
		 startWindow = window.open(url, windowName, windowOptions);
}

function DoGoogleSearch(frm, url)
{
	var pair1 = "?q=";
	var pair2 = "&btnG=Search&site=compbenefitsdirect&client=compbenefitsdirect&proxystylesheet=compbenefitsdirect&output=xml_no_dtd";
	
	
	if( isEmpty(frm.q.value) )
		return false;
	
	pair1 += URLEncode(frm.q.value);
	
	var toURL = url + pair1 + pair2;
	
	top.location = toURL;
}


function URLEncode( sStr )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = sStr;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {			    
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	
	return encoded;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
		
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
		
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
		
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
