/* --------------------------------------------------------------------------------------------------------
AUTHOR:			Jeremy Burgeson
DATE CREATED:	2009.07.30
Client:			Bicycle Theory
File Purpose:	Common Utilities Javascript File
REVISIONS:
-------------------------------------------------------------------------------------------------------- */


// toggles an element's display property between none and block
function toggleElement(strID, strDisplayType) {
	strDisplayType = strDisplayType || 'block';
	var objSection = document.getElementById(strID);
	if (objSection.style.display != 'none') {
		objSection.style.display = 'none';
	} else {
		objSection.style.display = strDisplayType;
	}
}


// sets cursor focus on the field given by param (formID.fieldID)
function formFocus(strFormAndFieldIDs) {
	if (isPageLoaded) {
		eval('document.forms.' + strFormAndFieldIDs + '.focus();');
	} else {
		setTimeout('formFocus("' + strFormAndFieldIDs + '")', 100);
	}
}


// sets cursor focus on the field given by param (formID.fieldID) and selects its contents
function formSelect(strFormAndFieldIDs) {
	if (isPageLoaded) {
		eval('document.forms.' + strFormAndFieldIDs + '.select();');
	} else {
		setTimeout('formSelect("' + strFormAndFieldIDs + '")', 100);
	}
}


// returns a number formatted for dollars and cents with commas as needed
function numberFormatDollars(strNumber) {
	// make sure number string is a string
	strNumber += '';
	// split by the decimal point
	var x = strNumber.split('.');
	var x1 = x[0];
	var x2 = '';
	//alert('strNumber = ' + strNumber + '\nx = ' + x + '\nx1 = ' + x1 + '\nx2 = ' + x2 + '\n');
	// handle numbers after decimal place
	if (x.length > 1) {
		x2 = x[1];
		if (x2.length == 1) {
			x2 = '.' + x2 + '0';
		} else if (x2.length >= 2) {
			/* DOES NOT YET HANDLE ROUNDING! */
			x2 = '.' + x2.substr(0, 2);
		}
	} else {
		x2 = '.00';
	}
	// add commas to every third digit
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	// return formatted number
	return x1 + x2;
}

/*
function numberFormat(strNumber, intDecimalPlaces) {
	// set default decimal places if needed
	var intDecimalPlaces = intDecimalPlaces || 2;
	// make sure number string is a string
	strNumber += '';
	// split by the decimal point
	var x = strNumber.split('.');
	var x1 = x[0];
	var x2 = x[1];
	// handle numbers after decimal place
	if (x2.length > 1) {
		if (x2.length > intDecimalPlaces) {
			// round and lop off
		}
	}
	//x2 =  ? '.' + x[1] : '';
	
	// add commas to every third digit
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	// return formatted number
	return + x2;
}
*/
