// JavaScript Document
function $( name ){
	return document.getElementById( name );
}

function confirmAction(){
	if( confirm( "Are you sure you want to do this?" ) ) return true;
	else return false;
}

function delegate( that, thatMethod ){
    return function() { return thatMethod.call(that, arguments ); }
    //return function() { return thatMethod.apply(that, arguments ); }
}

function registerClearDefault( inpt, defTxt ){
	inpt.onfocus = function(){ inptClearDefault( inpt, true, defTxt ); }
	inpt.onblur = function(){ inptClearDefault( inpt, false, defTxt ); }
}

function inptClearDefault( inpt, isfocus, txt, ispassword ){
	if( isfocus ){
		if( inpt.value == txt ){
			inpt.value = "";
			if( ispassword ) swapPassField( inpt, isfocus );
		}
	}else{
		if( inpt.value == "" ){
			inpt.value = txt;
			if( ispassword ) swapPassField( inpt, isfocus );
		}
	}
	
}

function swapPassField( inpt, topass ){
	try{
		inpt.type = topass ? 'password' : 'text';
	}catch(err){
		// IE can't do it
		var parent = inpt.parentNode;
		var field = document.createElement('input');
		field.type = topass ? 'password' : 'text';
		field.id = inpt.id;
		field.className = inpt.className;
		field.name = inpt.name;
		field.value = inpt.value;
		parent.replaceChild(field,inpt);
		if( topass ){ field.focus(); field.select(); }
		field.onfocus = inpt.onfocus;
		field.onblur = inpt.onblur;
	}
}

/* Tool Tip */
function infoToolTip( e, title, content  ){
	if( !e ) e = window.event;
	
	var bubble = $( 'infoBubble' );
	var offset = getScrollOffset();
	bubble.style.visibility = "visible";
	bubble.style.left = ( e.clientX + 10 + offset.x ) + "px";//- 23 + getBody().scrollLeft ) + "px";
	bubble.style.top = ( e.clientY - 10 + offset.y )  + "px";//- 185 + getBody().scrollTop )  + "px";
	$( 'bubbleTitle' ).innerHTML = unescape( title );
	$( 'bubbleContent' ).innerHTML = unescape( content );
}

function hideInfoToolTip(){
	var bubble = $( 'infoBubble' );
	bubble.style.left = "0px";
	bubble.style.top = "0px";
	bubble.style.visibility = "hidden";
}

function getScrollOffset(){
	var userBrowser = navigator.appName;
	if( userBrowser.indexOf( "Internet Explorer" ) < 0 ){
		return { x:window.pageXOffset, y:window.pageYOffset };
	}else{
		return { x:document.documentElement.scrollLeft, y:document.documentElement.scrollTop};
	}
}

function isArray(obj) {
	if( obj == undefined ) return false;
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function stripPX( inText ){
	try{
		inText = inText.substr(0, inText.indexOf( "px" ) );
	}catch( err ){}
	return inText;
}

function embedSwf( path, width, height ){
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="' + width + '" height="' + height + '">' );
	document.write( '<param name="movie" value="' + path + '">' );
	document.write( '<param name="quality" value="high">' );
	document.write( '<embed src="' + path + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed>' );
	document.write( '</object>' );
}

function hideRunnerBoardMessage(){
	$('runnerMessage').className='hidden';
	
	// save it
	var conn = new ajaxConnection( this, "/ajax/general.php" );
	conn.initRequestObject();
	conn.addVariable( "cmd", 'hideRunner' );
	conn.execute();
	this.conn = conn;
}

function getAbsolutePosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;
	//var b = getBoundingBox(e);
	//return {x:b[0], y:b[1]};
	return {x:left, y:top};
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function dumpObject( obj ){
	var str = "===================== Object Dump =================================\n";
	for( var i in obj ) str += i + ": " + obj[i] + "\n";
	str += "===================== End Dump ====================================\n";
	alert( str );
}
