/*---------------------------------------------------------------------------

onesandzeros.biz JavaScript Tool Box Library
(c) by Andy Frey/onesandzeros.biz

You may not copy any of this code or use it without permission of
Andy Frey, Phoenix, AZ USA
andy@onesandzeros.biz

Last revision: 2007-02-06

---------------------------------------------------------------------------*/

Number.prototype.format = function ( sep, dec, pad ) {
	// decpoint - the decimal point character. if skipped, "." is used
	// sep - the separator character. if skipped, "," is used
	// check for missing parameters and use defaults if so
	if( arguments.length == 0 ) {
		sep = ",";
		dec = ".";
		pad = 0;
	}
	if( arguments.length == 1 ) {
		dec = ".";
		pad = 0;
	}
	if( arguments.length == 2 ) {
		pad = 0;
	}
	// need a string for operations
	var num = this.toString();
	// separate the whole number and the fraction if possible
	a = num.split( dec );
	x = a[0]; // decimal
	y = a[1]; // fraction
	z = "";
	if( typeof( x ) != "undefined" ) {
		// reverse the digits. regexp works from left to right.
		for( i = x.length - 1; i >= 0; i-- )
			z += x.charAt( i );
		// add seperators. but undo the trailing one, if there
		z = z.replace( /(\d{3})/g, "$1" + sep );
		if( z.slice( -sep.length ) == sep )
		z = z.slice( 0, -sep.length );
		x = "";
		// reverse again to get back the number
		for( i = z.length - 1; i >= 0; i-- )
			x += z.charAt( i );
		// add the fraction back in, if it was there
		if( typeof( y ) != "undefined" && y.length > 0 ) {
			// if pad was in the argument list, pad with zeros or round
			if( y.length < pad )
				y += "0".repeat( pad - y.length );
			else
				y = Math.round( ( y.toString().substr( 0, pad ) + "." + y.toString().substr( pad ) ).valueOf() );
			x += dec + y;
		}
	}
	return x;
};

String.prototype.isWhiteSpaceAt = function ( n ) {
	return this.charAt( n ) == ' ' || this.charAt( n ) == '\t' || this.charAt( n ) == '\n'; 
};

String.prototype.ltrim = function () {
	var i = -1;
	while( this.isWhiteSpaceAt( ++i ) );
	return this.substr( i );
};

String.prototype.rtrim = function () {
	var i = this.length;
	while( this.isWhiteSpaceAt( --i ) );
	return this.substr( 0, i + 1 );
};

String.prototype.trim = function () {
	return this.rtrim().ltrim();
};

String.prototype.isValidEmail = function () {
	return this.trim().match( /^[_a-z0-9-][^\(\)\<\>\@\,\;\:\\\"\[\] ]*\@([a-z0-9\-]+\.)+[a-z]{2,4}$/i );
}

String.prototype.isGoodPassword = function () {
	var minLen = 8;
	var requireNonAlphaNum = false;
	if( arguments.length > 0 ) minLen = arguments[0];
	if( arguments.length > 1 ) requireNonAlphaNum = arguments[1];
	if( !requireNonAlphaNum )
		return this.trim().match( /[a-zA-Z]+/i ) && this.trim().match( /[0-9]+/i ) && this.trim().length >= minLen;
	else
		return this.trim().match( /[a-zA-Z]+/i ) && this.trim().match( /[0-9]+/i ) && this.trim().match( /[^a-z^A-Z^0-9]+/i ) && this.trim().length >= minLen;
};

String.prototype.capitalize = function () {
	return this.charAt( 0 ).toUpperCase() + this.substr( 1 );
};

String.prototype.capitalizeAll = function () {
	var s = "";
	var wasSpace = true;
	for( var i = 0; i < this.length; i++ ) {
		if( !this.isWhiteSpaceAt( i ) && wasSpace )
			s += this.charAt( i ).toUpperCase();
		else
			s += this.charAt( i );
		wasSpace = this.isWhiteSpaceAt( i );
	}
	return s;
};

// optional 2nd arg true means print elipse
// optional 3rd arg true means word boundaries
String.prototype.truncate = function ( len ) {
	var s = this.valueOf();
	if( this.length > len ) {
		if( arguments.length == 3 && arguments[2] ) {
			var i = s.lastIndexOf( " ", len );
			while( i > 0 && !s.isWhiteSpaceAt( i ) ) i--;
			s = s.substr( 0, i );
		}
		if( arguments.length > 1 && arguments[1] )
			return s.substr( 0, len ) + "...";
		else
			return s.substr( 0, len );
	}
	return s;
};

String.prototype.isNumericAt = function ( n ) {
	return "0123456789".indexOf( this.charAt( n ) ) > -1;
};

String.prototype.isNumeric = function () {
	return !isNaN( this );
};

String.prototype.repeat = function ( n ) {
	var temp = "";
	for( var i = 0; i < n; i++ ) temp += this;
	return temp;
};

function isIE() {
	return document.all;
}

function getElement( objName ) {
	var element = new Object();
	if( document.all ) {
		try {
			element = document.all[objName];
		} catch( e ) {
			throw "getElement( '" + objName + "' ): document.all[" + objName + "] failed. (Exception: " + e + ")";
		}
	} else {
		try {
			element = document.getElementById( objName );
		} catch( e ) {
			throw "getElement( '" + objName + "' ): document.getElementById( '" + objName + "' ) failed. (Exception: " + e + ")";
		}
	}
	return element;
}

function getValue( objName ) {
	try {
		return getElement( objName ).value;
	} catch( e ) {
		throw "getValue( '" + objName + "' ): Exception: " + e;
	}
}

function setValue( objName, val ) {
	try {
		getElement( objName ).value = val;
	} catch( e ) {
		throw "setValue( '" + objName + "', '" + val + "' ): Exception: " + e;
	}
}

function isElementVisible( objName ) {
	try {
		return getElement( objName ).style.display != "none";
	} catch( e ) {
		throw "isElementVisible( '" + objName + "' ): " + e;
	}
}

function showElement() {
	var objName = arguments[0];
	try {
		if( arguments.length == 1 )
			getElement( objName ).style.display = "inline";
		else if( arguments.length == 2 )
			getElement( objName ).style.display = arguments[1];
	} catch( e ) {
		throw "showElement( '" + objName + "'" + ( arguments.length == 2 ? " '" + arguments[1] + "'" : "" ) + " ): " + e;
	}
}

function hideElement( objName ) {
	try {
		getElement( objName ).style.display = "none";
	} catch( e ) {
		throw "hideElement( '" + objName + "' ): " + e;
	}
}

function getElementX( objName ) {
	try {
		var obj = getElement( objName )
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	} catch( e ) {
		throw "getElementX( '" + objName + "' ): " + e;
	}
}

function getElementY( objName ) {
	try {
		var obj = getElement( objName )
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	} catch( e ) {
		throw "getElementY( '" + objName + "' ): " + e;
	}
}

function moveElement( e, x, y ) {
	if( y != null ) getElement( e ).style.top = y + "px";
	if( x != null ) getElement( e ).style.left = x + "px";
}

function sizeElement( e, w, h ) {
	getElement( e ).style.width = w + "px";
	getElement( e ).style.height = h + "px";
}

function setElementWidth( e, w ) {
	getElement( e ).style.width = w + "px";
}

function setElementHeight( e, h ) {
	getElement( e ).style.height = h + "px";
}

function getElementWidth( e ) {
	return getElement( e ).offsetWidth;
}

function getElementHeight( e ) {
	return getElement( e ).offsetHeight;
}

function getParentWidth( e ) {
	return getElement( e ).offsetParent.offsetWidth;
}

function getParentHeight( e ) {
	return getElement( e ).offsetParent.offsetHeight;
}

function toggleElementVisibility( e ) {
	if( getElement( e ).style.display == "none" )
		showElement( e );
	else
		hideElement( e );
}

function disableElement( objName ) {
	try { 
		getElement( objName ).disabled = true;
	} catch( e ) {
		throw "disableElement( '" + objName + "' ): " + e;
	}
}

function enableElement( objName ) {
	try {
		getElement( objName ).disabled = false;
	} catch( e ) {
		throw "enableElement( '" + objName + "' ): " + e;
	}
}

function elementClear( e ) {
	getElement( e ).innerHTML = "";
}

function elementWriteln( e, m ) {
	if( arguments.length > 2 && arguments[2] )
		elementWrite( e, m + "\n", arguments[2] );
	else
		elementWrite( e, m + "\n" );
}

function elementWritebr( e, m ) {
	if( arguments.length > 2 && arguments[2] )
		elementWrite( e, m + "<br />\n", arguments[2] );
	else
		elementWrite( e, m + "<br />\n" );
}

function elementWrite( e, m ) {
	if( arguments.length > 2 && arguments[2] )
		getElement( e ).innerHTML += escape( m );
	else
		getElement( e ).innerHTML += m;
}

function setElementHTML( e, h ) {
	getElement( e ).innerHTML = h;
}

function getElementHTML( e ) {
	return getElement( e ).innerHTML;
}

// for tables
function clearTableRows( tableName ) {
	var tbl = getElement( tableName );
	while( tbl.rows.length ) tbl.deleteRow( tbl.rows.length - 1 );
}

function focusField( name ) {
	getElement( name ).focus();
}

function isEnabled( name ) {
	return !getElement( name ).disabled;
}

function isDisabled( name ) {
	return getElement( name ).disabled;
}

function setCheckbox( name, yn ) {
	if( isEnabled( name ) )
		getElement( name ).checked = ( yn == 'y' || yn == 'Y' );
}

function isChecked( name ) {
	return getElement( name ).checked;
}

function toggleCheckbox( name ) {
	getElement( name ).checked = !isChecked( name );
}

function checkboxLabel( name ) {
	if( isEnabled( name ) )
		getElement( name ).checked = !getElement( name ).checked;
	return isEnabled( name );
}

function addOption( sel, txt, val, selected ) {
	getElement( sel ).options[ getElement( sel ).options.length ] = new Option( txt, val );
	getElement( sel ).options[ getElement( sel ).options.length - 1 ].selected = selected;
}

function clearSelect( name ) {
	getElement( name ).options.length = 0;
}

function getSelectedIndex( o ) {
	return getElement( o ).selectedIndex;
}

function getSelectedText( o ) {
	var s = getElement( o ); 
	return s.options[s.selectedIndex].text;
}

function getSelectedValue( o ) {
	var s = getElement( o ); 
	return s.options[s.selectedIndex].value;
}

function selectIndexByValue( o, v ) {
	var s = document.getElementById( o );
	for( var i = 0; i < s.length; i++ ) {
		if( s.options[i].value == v ) {
			s.selectedIndex = i;
			return;
		}
	}
}

function selectIndexByText( o, t ) {
	var s = document.getElementById( o );
	for( var i = 0; i < s.length; i++ ) {
		if( s.options[i].text == t ) {
			s.selectedIndex = i;
			return;
		}
	}
}

function getIndexByValue( o, v ) {
	var s = document.getElementById( o );
	for( var i = 0; i < s.length; i++ ) {
		if( s.options[i].value == v ) {
			return i;
		}
	}
	return -1;
}

function getIndexByText( o, t ) {
	var s = document.getElementById( o );
	for( var i = 0; i < s.length; i++ ) {
		if( s.options[i].text == t ) {
			return i;
		}
	}
	return -1;
}

function setOpacity( e, o ) {
	// example of transparency in either major browser
	// pass in a value (o) from 0 to 100
	if( document.all )
		document.getElementById( e ).style.filter = "alpha( opacity = " + o + " )";
	else
		document.getElementById( e ).style.opacity = o / 100;
}

function createCookie( name, value, days ) {
	if( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie( name ) {
	var nameEQ = name + "=";
	var ca = document.cookie.split( ';' );
	for( var i=0; i < ca.length; i++ ) {
		var c = ca[i];
		while( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
		if( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
	}
	return null;
}

function eraseCookie( name ) {
	createCookie( name, "", -1 );
}

/*
function show_screen_coord( event ) {
	x=event.screenX;
	y=event.screenY;
	alert( "Screen... X:" + x + ", Y:" + y );
}

function show_coords( event ) {
	x = event.clientX;
	y = event.clientY;
	alert( "X: " + x + ", Y: " + y );
}

<body onmousedown="show_coords( event ); show_screen_coords( event )">
*/
