/** 
 * Alvaro's Javascript Library
 */

function stopEventPropagation (e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function var_dump ( obj ) {
	var a = '';
	var type = typeof obj;
	a += "Type: " + type + "\n";	
	if (type == 'array' || type == 'object') {
		for ( i in obj ) {
			a += i + ": "+ obj[i] + "\n";
		}
	}
	//alert(a);
	return a;
}


function getJsQueryVars () {
	var string = self.location.href.split('#');
	var vars = new Array();
	if (string[1]) {
		var var_array = string[1].split('&');

		$H(var_array).each(function (obj) { 
			var_tmp = obj[1].split('=');
			vars[var_tmp[0]] = var_tmp[1];
		});

	}
	return vars;
}

function getJsQueryVar ( query_var ) {
	var query_vars = getJsQueryVars();
	if (query_vars[query_var] === 'undefined') return false;
	else return query_vars[query_var];
}

function getScroll () {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
}


function createXMLDoc (str) {
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = (new DOMParser).parseFromString( str, 'text/xml' );
	} else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
		xmlDoc.loadXML( str );
 	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	return xmlDoc;
}


/** 
 * IE Compatibility functions
 */
function createEventListener ( obj, handler, func_ref ) {
	if (obj.attachEvent) {
		obj.attachEvent('on'+handler, func_ref);
	} else {
		obj.addEventListener( handler, func_ref, false );
	}
}


/**
 * DIV POPUP
 */
var t_popup_div = document.createElement('div');
t_popup_div.setAttribute('style','display:none; z-index:500');
t_popup_div.setAttribute('onmousedown', 'stopEventPropagation(event)');

function DivPopupClass () {
	if (t_popup_div.attachEvent) {
		t_popup_div.attachEvent('onmousedown', stopEventPropagation);
	} else {
		t_popup_div.addEventListener('mousedown', stopEventPropagation, false);
	}

	// append a child as a placeholder
	t_popup_div.appendChild(document.createElement('div'));
	if (document.all) {
		t_popup_div.appendChild(document.createElement('iframe'));
	}


	this.setContent = function ( content ) {
		// content div
		var c = t_popup_div.firstChild;
		
		// content 
		c.innerHTML = ''; //clear it first
		c.style.position = 'absolute';
		c.style.left = '0px';
		c.style.top = '0px';
		c.style.zIndex = '1000';
	
		c.style.backgroundColor = '#ffffff';


		/** mozilla hacks
		// add it to the document to figure out the size
		var test = document.createElement('table');
		test.innerHTML = '<tr></td>content</td></tr>';
		document.body.appendChild( test );
		alert(test.offsetWidth);
		*/
	
		c.innerHTML = "<div>" + content + "</div>";

		/*
		alert(c.firstChild.offsetWidth);	
		*/

		// iframe hack for 'select over everything' ie bug
		if (document.all) {
			var iframe_hack = c.nextSibling;
			iframe_hack.src = '/shared/blank';
			iframe_hack.style.position = 'absolute';
			iframe_hack.style.top = '0px';
			iframe_hack.style.left = '0px';
			iframe_hack.style.zIndex = '900';
		}	
	}

	this.show = function (obj) {
		// get the current width of the body
		var body_width = document.body.offsetWidth;

		// ad the div to the body
		document.body.appendChild( t_popup_div );
	
		t_popup_div.style.width = t_popup_div.firstChild.offsetWidth;

		// position it to the right of the object
		t_popup_div.style.position = 'absolute';
		t_popup_div.style.left = findPosX(obj) + obj.offsetWidth + 20 + 'px';
		t_popup_div.style.top = findPosY(obj) + 'px';

		// display the div
		t_popup_div.style.display = 'block';
		
		// size the iframe correctly
		if (document.all) {
			var iframe_hack = t_popup_div.firstChild.nextSibling;
			iframe_hack.style.width = t_popup_div.firstChild.offsetWidth;
			iframe_hack.style.height = t_popup_div.firstChild.offsetHeight;
		}
	
		// if off the page, fix it
		if ((findPosX(obj) + 220) > body_width) {
			t_popup_div.style.left = parseInt( t_popup_div.style.left ) - 220 + 'px';
		}

		if (document.addEventListener) {
			document.addEventListener('mousedown', this.hide, false);
		} else if ( document.attachEvent ) {
			document.attachEvent('onmousedown', this.hide);
		}
	}

	this.hide = function () {
		t_popup_div.style.display = 'none';
	}

}
var DivPopup = new DivPopupClass();



/**
 * RsViewer
 */
function RsViewer_class ( file ) {
	this.records_per_page = 20;
	this.file = file;
	this.page = getJsQueryVar('page');
	this.params = new Array();
	this.total_records = 0;
	this.total_pages = 0;
	this.order_field = '';
	this.order_direction = 'desc';

	if (!this.page) this.page = 1;

	this.setRecordsPerPage = function ( num ) {
		this.records_per_page = num;
		this.setParam('limit', ((this.page*this.records_per_page)-this.records_per_page) + ',' + this.records_per_page);
	}
	
	this.changePage = function ( page ) {
		if ((page > 0 && page <= this.total_pages) || this.total_pages == 0) {
			this.page = parseInt(page);
			this.setParam('limit', ((page*this.records_per_page)-this.records_per_page) + ',' + this.records_per_page);
			this.setParam('page', page);
			this.updateResults();
		}
	}
	
	this.toggleOrder = function ( order, obj ) {
		if (this.order == order) {
			this.direction = (this.direction == 'asc' ? 'desc': 'asc');
		} else {
			this.order = order;
			this.direction = 'desc';
		}
		this.setParam('order', this.order);
		this.setParam('direction', this.direction);
		this.updateResults();
	}
	
	this.updateResults = function () {
		this.updateQuery();
		document.getElementById('RsViewer_matches').innerHTML = document.getElementById('RsViewer_matches').innerHTML + "<div class='searching'></div><div class='searching2'>Searching ...</div>\n";
		setTimeout( this.updateResults2, 500 );
	}
	
	this.updateResults2 = function () {
		document.getElementById('RsViewer_matches').innerHTML = HTTPCallback( file + '?' + RsViewer.buildQuery()  );
		RsViewer.total_records = document.getElementById('RsViewer_total').value;
		document.getElementById('RsViewer_display_total').innerHTML = RsViewer.total_records;
		RsViewer.total_pages = Math.ceil(RsViewer.total_records / RsViewer.records_per_page);
		document.getElementById('RsViewer_total_pages').innerHTML = RsViewer.total_pages;
		
		var field_headers = document.getElementsByTagName('th');
		for (i=0; i<field_headers.length; i++) {
			if (field_headers[i].getAttribute('field')) {
				var th = field_headers[i];
				var field = th.getAttribute('field');
				if (th.attachEvent) {
					eval("th.attachEvent('onclick', function () { RsViewer.toggleOrder('" + field + "'); } ); ");
				}
				th.setAttribute('onclick', 'RsViewer.toggleOrder("' + field + '")');
				if (RsViewer.order == field) {
					th.className = RsViewer.direction;
				}
			}
		}
		document.getElementById('RsViewer_current_page').value = RsViewer.page;
	}
	
	this.setParam = function ( param, value ) {
		this.params[param] = value;
	}
	
	this.updateQuery = function () {
		self.location.href = '#' + this.buildQuery();
	}
	
	this.buildQuery = function () {
		var values = new Array();	
		$H(this.params).each(function(index) {
			values[values.length] = index[0] + '=' + index[1];
		});
		var q_string = '';
		for (var i=0; i < values.length; i++) {
			q_string += key + '=' 	
		}
		return values.join('&');
	}
	this.params = getJsQueryVars();
	this.setParam('limit','0,' + this.records_per_page);
	//this.setParam('Status', 'open');
}












/**
 * DATE PICKER
 */
function DatePicker () {

	this.obj;
	this.months = new Array('January','February','March','April','May','June','July','August','September', 'October','November','December');

	this.getDaysInMonth = function (iMonth, iYear) {
		var dPrevDate = new Date(iYear, (parseInt(iMonth)+1), 0);
		//alert("new Date("+iYear+", "+(iMonth+1)+", 0); getDate("+dPrevDate.getMonth()+")");
		return dPrevDate.getDate();
	}

	this.buildCal = function (iYear, iMonth, iDayStyle) {
		var aMonth = new Array();
		aMonth[0] = new Array(7);
		aMonth[1] = new Array(7);
		aMonth[2] = new Array(7);
		aMonth[3] = new Array(7);
		aMonth[4] = new Array(7);
		aMonth[5] = new Array(7);
		aMonth[6] = new Array(7);
		var dCalDate = new Date(iYear, iMonth, 1);
		var iDayOfFirst = dCalDate.getDay();
		var iDaysInMonth = this.getDaysInMonth(iMonth, iYear);
		var iVarDate = 1;
		var i, d, w;
		if (iDayStyle == 2) {
			aMonth[0][0] = "Sunday";
			aMonth[0][1] = "Monday";
			aMonth[0][2] = "Tuesday";
			aMonth[0][3] = "Wednesday";
			aMonth[0][4] = "Thursday";
			aMonth[0][5] = "Friday";
			aMonth[0][6] = "Saturday";
		} else if (iDayStyle == 1) {
			aMonth[0][0] = "Sun";
			aMonth[0][1] = "Mon";
			aMonth[0][2] = "Tue";
			aMonth[0][3] = "Wed";
			aMonth[0][4] = "Thu";
			aMonth[0][5] = "Fri";
			aMonth[0][6] = "Sat";
		} else {
			aMonth[0][0] = "Su";
			aMonth[0][1] = "Mo";
			aMonth[0][2] = "Tu";
			aMonth[0][3] = "We";
			aMonth[0][4] = "Th";
			aMonth[0][5] = "Fr";
			aMonth[0][6] = "Sa";
		}
		for (d = iDayOfFirst; d < 7; d++) {
			aMonth[1][d] = iVarDate;
			iVarDate++;
		}
		for (w = 2; w < 7; w++) {
			for (d = 0; d < 7; d++) {
				if (iVarDate <= iDaysInMonth) {
					aMonth[w][d] = iVarDate;
					iVarDate++;
			  	}
		   	}
		}
		return aMonth;
	}


	this.drawCal = function (iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight, iDayStyle) {
		var myMonth;
		myMonth = this.buildCal(iYear, iMonth, iDayStyle);

		var cur_date = new Date();

		var c_html = '';


		c_html += "<table class='calendar' border='0' cellspacing='1' style='border: solid 1px gray'>";
		c_html += "  <tr>";
		c_html += "    <th colspan='7' nowrap>";

		c_html += "      <span><a href='##' onClick='date_picker.updateCalendar("+((iMonth==0)?(iYear-1):iYear)+", "+((iMonth==0)?11:(iMonth-1))+")'>&lt;</a></span>";
		c_html += "      <select name='month' onchange='date_picker.updateCalendar(this.nextSibling.nextSibling.value, this.value)'>";
		for ( var i=0; i<12; i++ ) {
			c_html += "     <option value='" + (i) + "'"; 
			if (iMonth == i) c_html += " selected='selected'";
			c_html += ">" + this.months[i] + "</option>";
		}
		c_html += "      </select>";

		c_html += "      <select name='year' onchange='date_picker.updateCalendar(this.value, this.previousSibling.previousSibling.value)'>";
		c_html += "        <option value='"+(iYear-1)+"'>" + (iYear-1) + "</option>";
		c_html += "        <option value='"+(iYear)+"' selected='selected'>" + iYear+ "</option>";
		c_html += "        <option value='"+(parseInt(iYear)+1)+"'>" + (parseInt(iYear)+1) + "</option>";
		c_html += "      </select>";
		c_html += "      <span><a href='##' onClick='date_picker.updateCalendar("+((iMonth==11)?(iYear+1):iYear)+", "+((iMonth==11)?0:(iMonth+1))+")'>&gt;</a></span>";

		c_html += "    </th>\n";
		c_html += "  </tr>";
		c_html += "  <tr>";
		c_html += "    <th>" + myMonth[0][0] + "</th>";
		c_html += "    <th>" + myMonth[0][1] + "</th>";
		c_html += "    <th>" + myMonth[0][2] + "</th>";
		c_html += "    <th>" + myMonth[0][3] + "</th>";
		c_html += "    <th>" + myMonth[0][4] + "</th>";
		c_html += "    <th>" + myMonth[0][5] + "</th>";
		c_html += "    <th>" + myMonth[0][6] + "</th>";
		c_html += "  </tr>\n";
		for (w = 1; w < 7; w++) {
			c_html += "  <tr>";
			for (d = 0; d < 7; d++) {
				if (!isNaN(myMonth[w][d])) {
					c_html += "    <td onmouseover='date_picker.toggleColor(this)' \n";
					c_html += "      onmouseout='date_picker.toggleColor(this)'";
					if (iYear == cur_date.getFullYear() && iMonth == cur_date.getMonth() && myMonth[w][d] == cur_date.getDate()) c_html += " style='background: #cdf' ";
					c_html += "      onclick='date_picker.toggleColor(this); date_picker.setSelectedDay("+iYear+","+iMonth+","+myMonth[w][d]+")'>\n";
					c_html += myMonth[w][d];
					c_html += "    </td>";
				} else {
					c_html += "<td></td>";
				}
			}
			c_html += "  </tr>\n";
		}
		c_html += "</table>";

		return c_html;
	}


	this.prompt = function( obj ) {
		this.obj = obj;
		cur_date = new Date();
		DivPopup.setContent( this.drawCal(cur_date.getFullYear(),cur_date.getMonth()) );
		DivPopup.show(obj);
	}


	this.updateCalendar = function ( iYear, iMonth ) {
		DivPopup.setContent( this.drawCal( iYear, iMonth ) );
	}


	this.setSelectedDay = function (iYear,iMonth,iDay) {
		this.obj.value = (iMonth + 1) + '/' + iDay + '/' + iYear;
		
		// if we can run the onchange on it
		if (this.obj.onchange) {
			this.obj.onchange();
		}
		DivPopup.hide();
	}

	this.toggleColor = function (myTd) {
		if (myTd.style.color == "red") {
			myTd.style.color = "black";
		} else {
			myTd.style.color = "red";
		}
	}

	this.addStyle = function () {
		var myStyle = document.createElement('style');
		myStyle.innerHTML = c_html;

		document.appendChild( myStyle );
	}
}

var date_picker = new DatePicker;






/**
 * DIR BROWSER
 */
function drawDirTree ( xml_doc ) {

	var objDom = createXMLDoc( xml_doc );
	var directory = objDom.documentElement;
	var matches = document.getElementById('file_structure');
	
	var dir_path = '';

	var folders = new Array();
	for ( i in directory.childNodes ) {
		if (directory.childNodes[i].tagName == 'directory') folders[folders.length] = directory.childNodes[i];
	}
	
	var files = new Array();
	for ( i in directory.childNodes ) {
		if (directory.childNodes[i].tagName == 'file') files[files.length] = directory.childNodes[i];
	}

	var blah = '';
	blah += "<div style='background-image:url(/images/closedfolder.gif); background-repeat:no-repeat; padding:1px;padding-left:20px;cursor:default' \n";
	blah += "  onclick='toggleContents(this, event)'>\n";
	blah += directory.getAttribute('name');

	blah += "  <div style='display: none; margin-left: 0px'>\n";
	for (var i=0; i<folders.length; i++) {
		blah += drawFileNode( folders[i], dir_path );
	}
	for (i in files) {
		blah += drawFileNode( files[i], dir_path );
	}
	blah += "  </div>\n";
	blah += "</div>\n";

	return blah;
}


function drawFileNode ( file_node, dir_path ) {
	var blah = '';
	if ( file_node.tagName == 'directory' ) {

		dir_path = dir_path + file_node.getAttribute('name') + '/';

		var elements = new Array();
		for (i in file_node.childNodes) {
			if (file_node.childNodes[i].nodeType == 1) elements[elements.length] = file_node.childNodes[i];
		} 

		blah += "<div style='background-image:url(/images/closedfolder.gif); background-repeat:no-repeat; padding:1px;padding-left:20px;cursor:default; font-weight: normal' \n";
		blah += "  onclick='toggleContents(this, event)'>\n";
		blah += file_node.getAttribute('name');

		blah += "  <div style='display: none; margin-left: 0px'>\n";
		for (var i=0; i<elements.length; i++) {
			blah += drawFileNode( elements[i], dir_path );
		}
		blah += "  </div>\n";
		blah += "</div>\n";
	} else {
		dir_path = dir_path + file_node.getAttribute('name');

		blah += "<div style='background-image: url(/images/docicon.gif); background-repeat: no-repeat; padding: 1px; padding-left: 18px; font-weight: normal' \n";
		blah += "  onclick='stopEventPropagation(event)'>\n";
		blah += "  <span onclick=\"dir_browser.setPath('" + dir_path + "')\">" + file_node.getAttribute('name') + "</span>\n";
		blah += "</div>\n";
	}
	return blah;
}



function toggleContents ( file_node, e ) {

	stopEventPropagation(e);

	// open directory
	if ( file_node.firstChild.nextSibling.style.display == 'none') {
		file_node.firstChild.nextSibling.style.display = '';
		file_node.style.backgroundImage = 'url(/images/openfolder.gif)';
		file_node.style.fontWeight = 'bold';
	// close it
	} else {
		file_node.firstChild.nextSibling.style.display = 'none';
		file_node.style.backgroundImage = 'url(/images/closedfolder.gif)';
		file_node.style.fontWeight = 'normal';
	}
}

function DirBrowser () {

	this.obj;
	this.div_popup;

	this.show = function ( obj, xml_file ) {		
		var my_div = new DivPopup;
		var content = document.createElement('div');
		content.setAttribute('style', 'background: white; padding: 5px; border: solid 1px gray; border-width: 1px 2px 2px 1px; font-size: 10px');
		content.innerHTML = drawDirTree(HTTPCallback( xml_file ));
		content.style.background = 'white';
		content.style.width = '200px';
		content.style.overflow = 'auto';
		
		my_div.setContent( content );

		my_div.show( obj );
		this.div_popup = my_div;
		this.obj = obj;
		
		content.firstChild.style.width = content.firstChild.firstChild.offsetWidth + 'px';
		content.style.width = content.firstChild.offsetWidth + 'px';
		
	}	

	this.setPath = function ( path ) {
		this.obj.value = path;
		this.div_popup.hide();
	}

}

var dir_browser = new DirBrowser();




/**
 * HELP SYSTEM 
 */
function HelpTool_class () {
	this.obj;
	
	this.show = function ( obj, help_file ) {
		
		if (help_file.substring(0, 4) == 'url:') {
			content = 'loading ...';
			setTimeout('var tmp = document.createElement("div"); tmp.setAttribute("style", "padding: 5px; border: solid 1px gray; border-width: 1px 2px 2px 1px; font-size: 10px"); tmp.innerHTML = HTTPCallback("' + help_file.substring(4,200) + '"); help_tool.div_popup.setContent(tmp);', 1000);
		} else {
			content = '<div style="padding: 5px; border: solid 1px gray; border-width: 1px 2px 2px 1px; min-width: 120px; font-size: 10px; background: white">' + help_file + '</div>';
		}
		
		
		DivPopup.setContent( content );	
		DivPopup.show( obj );
	
		this.obj = obj;
	}
	
	this.hide = function () {
		DivPopup.hide();
	}
}
var HelpTool = new HelpTool_class();



/**
 * INPUT MODIFIER
 */
function InputModifier () {
	this.run = function () {
		//alert("running");
		// get all of the input tags
		var all_inputs = document.getElementsByTagName('input');

		for ( var i=0; i<all_inputs.length; i++ ) {
			if ( all_inputs[i].getAttribute('pick') == 'date' ) {
				this.addDatePicker( all_inputs[i] );
			}
			
			
			if ( all_inputs[i].getAttribute('browse')) {
				this.addDirBrowser( all_inputs[i], all_inputs[i].getAttribute('browse') );
			}
			
			if ( all_inputs[i].getAttribute('help')) {
				this.addHelpTool( all_inputs[i], all_inputs[i].getAttribute('help') );
			}
		}
	}
	
	this.addDatePicker = function ( obj ) {
		this.addIcon ( obj, '<img src="/js/mini_calendar.gif" onclick="date_picker.prompt(this.parentNode.previousSibling)" />' );
	}
	
	this.addDirBrowser = function ( obj, xml_file ) {
		this.addIcon (obj, '<img src="/global_js/openfolder.gif" onclick="dir_browser.show(this.parentNode.previousSibling, \'' + xml_file +'\')" />');
		//alert(obj.nextSibling.firstChild);
		obj.nextSibling.firstChild.setAttribute('onclick', 'dir_browser.show(this.parentNode.previousSibling, "'+ xml_file +'")');
	}
	
	this.addHelpTool = function ( obj, help_file ) {
		this.addIcon (obj, '<img src="/images/question.jpg" onmouseover="HelpTool.show(this, \'' + help_file + '\')" onmouseout="HelpTool.hide()" />');
	}
	
	this.addIcon = function ( obj, htmlcode ) {
		var icon_image = document.createElement('span');
		icon_image.innerHTML = htmlcode;
		
		obj.parentNode.insertBefore( icon_image, obj );
		obj.parentNode.insertBefore( obj, icon_image );
		
		icon_image.style.verticalAlign = 'bottom';
	}
	
	this.run();
}
createEventListener( window, 'load', InputModifier );

function testblah () {
	alert('testblah');
}



/**
 * AJAX 
 */
function getHTTPObject () {

	if (self.XMLHttpRequest) {
		return new self.XMLHttpRequest();
	} else {
		var msAjax = false;
		try {
			msAjax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				msAjax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				msAjax = false;
			}
		}
		return msAjax;
	}	
}


function HTTPCallback (address) {
	var xmlHttp = getHTTPObject();
	xmlHttp.open("GET", address, false);
	xmlHttp.send(null);
	return xmlHttp.responseText;
}


/**
 * getParentByTagName
 *
 * Finds the first parent of obj of type tagname
 * Returns the object when found, otherwise false
 */
function getParentByTagName(obj, tagname) {
   if(obj.parentNode && obj.parentNode.tagName == tagname) {
      return obj.parentNode;
   } else if(obj.parentNode) {
      return getParentByTagName(obj.parentNode, tagname);
   } else {
      return false;
   }
}

/**
 * jumpToAnchor
 *
 * Parses current url in address bar and replaces
 * any anchor tags with anchor
 */
function jumpToAnchor(anchor) {
   tmphref = window.location.href;
   hrefparts = tmphref.split("?");
   qsparts = hrefparts[1].split("#");
   window.location.href = hrefparts[0] + "?" + qsparts[0] + "#" + anchor;
}

/**
 * phoneFormat
 *
 * Takes an object and ensures that it is a valid 10 digit phone number.
 */
function phoneFormat(obj) {
   var verifyvalue=obj.value;
   var sval=stripChars(verifyvalue, true);
   if(sval.length == 10){
      var newvalue=reformat(sval, "(", 3, ") ", 3, "-", 4);
      obj.value=newvalue;
   } else {
      if(obj.value.length > 0) {
         alert('The value you entered does not appear to be a valid phone number.');
      }
      obj.focus();
   }
}

function moneyFormat(obj) {
	var verifyvalue=obj.value;
	var sval=stripChars(verifyvalue, false);
	
	var s = sval.split('.');
	
	var newstring = '';
	while (s[0].length) {
		newstring = s[0].substr(s[0].length-3, 3) + ',' + newstring;
		s[0] = s[0].substr(0, s[0].length - 3);
	}
	if (s.length == 2) {
		obj.value = newstring.substr(0, newstring.length - 1) + '.' + s[1];
	} else {
		obj.value = newstring.substr(0, newstring.length - 1) + '.00';
	}
}

/**
 * stripChars
 *
 * Will remove all characters besides 0-9 and . from a string.
 * If exclude_decimal is passed as true only 0-9 are kept.
 */
function stripChars(value, exclude_decimal) {
   tmpvalue = value;
   newvalue = "";
   for(i=0;i<tmpvalue.length;i++) {
      tmpdigit = tmpvalue.substr(i,1);
      if(parseInt(tmpdigit) >= 0 || (tmpdigit == "." && !exclude_decimal)) {
         newvalue += tmpdigit;
      }
   }
   return newvalue;
}

/**
 * reformat
 *
 * Reformats a string given arguments.
 * Example: reformat(sval, "(", 3, ") ", 3, "-", 4) will turn 8017058991 into (801) 705-8991
 */
function reformat (s){
   var arg;
   var sPos = 0;
   var resultString = "";
   for (var i = 1; i < reformat.arguments.length; i++) {
      arg = reformat.arguments[i];
      if (i % 2 == 1){ resultString += arg;}
      else {
         resultString += s.substring(sPos, sPos + arg);
         sPos += arg;
      }
   }
   return resultString;
}
