// return the first actual element (skipping all the weird text nodes)
function getElement( node ) {
	while( node && node.nodeType != 1 ) {
		node = node.nextSibling;
	}
	
	return node;
}


// return a HTTPRequest object for Ajax
function getHTTPObject() {
	var xhr = false;
	
	if( window.XMLHttpRequest ) {
		xhr = new XMLHttpRequest();
	} else if( window.ActiveXObject ) {
		try {
			xhr = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch( e ) { 
			try {
				xhr = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch( e ) { 
				xhr = false;
			}
		}
	}
	
	return xhr;
}

// uses HTTPRequest object to get specified url and pass results to parseResponse function
function getURL( url, area, callback ) {
	var xhr = getHTTPObject();
	
	if( xhr ) {
		xhr.onreadystatechange = function() {
			callback( xhr, area );
		};
		
		xhr.open( 'GET', url, true );
		xhr.send( null );
		
		return true;
	} else {
		return false;
	}
}

function sendData( url, data, area, callback ) {
	var xhr = getHTTPObject();
	
	if( xhr ) {
		xhr.onreadystatechange = function() {
			callback( xhr, area );
		};
		
		xhr.open( 'POST', url, true );
		xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
		xhr.send( data );
		
		return true;
	} else {
		return false;
	}
}

// displays loading graphic inside of element
function displayLoading( element ) {
	while( element.hasChildNodes() ) {
		element.removeChild( element.lastChild );
	}
	
	var center = document.createElement( 'center' );
	
	var image = document.createElement( 'img' );
	
	image.setAttribute( 'src', '/images/loading.gif' );
	image.setAttribute( 'alt', 'loading...' );
	
	center.appendChild( image );
	element.appendChild( center );
}


// allows you to add function to document onload event
function addLoadEvent( func ) {
	var oldonload = window.onload;
	
	if( typeof window.onload != "function" ) {
		window.onload = func;
	} else {
		window.onload = function() {
			if( oldonload ) {
				oldonload();
			}
			
			func();
		}
	}
}

// sorts an html table ( minus head (first) row )
function sortTable( table, sortcol ) {
    var table_array = [];
   
    // build array of table rows
    var rows = table.lastChild.childNodes;
    
    for( var i = 1; i < rows.length; i++ ) {
        if( rows[i].nodeType == 1 ) {
            var cc = 0;
            
            for( var c = 0; c < rows[i].childNodes.length; c++ ) {
                if( rows[i].childNodes[c].nodeType == 1 ) {
                    cc++;
                    
                    if( cc == sortcol ) {
                        table_array[rows[i].childNodes[c].firstChild.nodeValue] = rows[i];
                    }
                }
            }
        }
    }
    
    // sort array
    var array_keys = [];
    
    for( var i in table_array ) {
        array_keys[ array_keys.length ] = i;
    }
    
    array_keys.sort();
    
    // remove old rows
    while( table.lastChild.childNodes.length > 1 ) {
        table.lastChild.removeChild( table.lastChild.lastChild );
    }
    
    // apply new row order
    for( var c = 0; c < array_keys.length; c++ ) {
        table.lastChild.appendChild( table_array[ array_keys[c] ] );
    }
    
    return false;
}