/**
 * CSOS Popup function.
 *
 * popupCenterGrow( filename, windowname, width=300, height=300,
 * 				 pop_dest_x=center-(width/2), pop_dest_y=center-(height/2),
 * 				 do_not_grow=false, interval=25, other_args='' )
 *
 * This will grow a popup from the smallest size to the popup size, increasing
 * by 15px at a time until properly sized.  Any of the last four arguments can
 * be left off to set them to the defaults, listed above.  It will return the
 * popup window on success, boolean false on failure.
 *
 * If you set do_not_grow equal to boolean true, it will popup a window with
 * the coords & properties specified without funky growing pains.
 *
 * Interval is an int representing the number of steps that it will take the
 * window to reach maximum size.
 */
function popupCenterGrow(	file_name,window_name,pop_width,pop_height,
							pop_dest_x,pop_dest_y,do_not_grow,interval,
							other_args ) {
	/*  if we can't / shouldn't popup, don't try */
	if(do_not_grow)  {
		var args = 'resizable=1,'
				 + 'scrollbars=1,'
				 + 'width='+pop_width+','
				 + 'height='+pop_height+','
				 + 'left='+pop_dest_x+','
				 + 'top='+pop_dest_y
				 + ((other_args && other_args!='')
				 	? ((other_args.charAt(0)==',')?'':',')+other_args
				 	: '');
		return window.open( file_name, window_name, args );
	}
	if(!window.open){ alert('Popped windows not possible.'); return false; }
	
	/*  handling empty function arguments, */
	/*  make sure they're all the type they should be, damnit */
	if(pop_width   == null || pop_width   == '' || pop_width  < 200 ){pop_width   = 200;}
	pop_width   	= parseInt(pop_width  );
	
	if(pop_height  == null || pop_height  == '' || pop_height < 150 ){pop_height  = 150;}
	pop_height  	= parseInt(pop_height );
	
	if(pop_dest_x  == null || pop_dest_x  == ''){pop_dest_x  = Math.round((parseInt(screen.width)/2)-(pop_width/2));}
	pop_dest_x  	= parseInt(pop_dest_x );
	
	if(pop_dest_y  == null || pop_dest_y  == ''){pop_dest_y  = Math.round((parseInt(screen.height)/2)-(pop_height/2));}
	pop_dest_y  	= parseInt(pop_dest_y );
	
	if(interval    == null || interval    == ''){interval    = 15;}
	interval    	= parseInt(interval   ); 
	
	if(other_args  == null || other_args  == ''){other_args  = '';}
	if(file_name   == null || file_name   == ''){file_name   = '';}
	if(window_name == null || window_name == ''){window_name = 'pcgwin';}
	if(do_not_grow == null || do_not_grow == ''){do_not_grow = false;}
	
	/*  Cross browser ? */
	var width_var = 'width';
	var height_var = 'height';
							
	/*  opening the popup */
	/*  Getting the click event if we can.							 */							
	var e = window.event;
	if(e){
	    /*  if something was clicked, grow from there */
		var center_x = e.screenX;
		var center_y = e.screenY;
	} else {
		/*  center coords for the screen */
		var center_x = (screen.width/2);
		var center_y = (screen.height/2);
	}
	
	/*  center coords for the destination */
	var dest_center_x = Math.round(0 + pop_dest_x + (pop_width/2));
	var dest_center_y = Math.round(0 + pop_dest_y + (pop_height/2));
	
	/*  initial height and width */
	var pwidth = 200;
	var pheight = 150;

	/*  setting the initial variables */
	var x_interval   = Math.round(((pop_width > pwidth)  ?((pop_width-pwidth)/interval)  :((pwidth-pop_width)/interval)));
	var y_interval   = Math.round(((pop_height > pheight)?((pop_height-pheight)/interval):((pheight-pop_height)/interval)));
	var c_x_interval = Math.round(((dest_center_x > center_x)?((dest_center_x-center_x)/interval):((center_x-dest_center_x)/interval)));
	var c_y_interval = Math.round(((dest_center_y > center_y)?((dest_center_y-center_y)/interval):((center_y-dest_center_y)/interval)));
		
	/*  popup arguments */
	var args = 'resizable=1,'
			 + 'scrollbars=1,'
			 + width_var+'='+pwidth+','
			 + height_var+'='+pheight+','
			 + 'left='+(Math.round((center_x)-(pwidth/2)))+','
			 + 'top='+(Math.round((center_y)-(pheight/2)))
			 + ((other_args!='')
			 	? ((other_args.charAt(0)==',')?'':',')+other_args
			 	: '');

	/*  pop it up, baby. */
	var popupWindow = window.open( file_name, window_name, args );
	
	/*  growing it to the destination */
	while(pop_width != pwidth || pop_height != pheight){
		/*  WIDTH */
		if(pop_width > pwidth){
			pwidth += x_interval;   	 }
		else if(pwidth >= pop_width){
			pwidth = pop_width;          }
			
		/*  HEIGHT */
		if(pop_height > pheight){
			pheight += y_interval; 		 }
		else if(pheight >= pop_height){
			pheight = pop_height;        }
			
		/*  CENTER X */
		if(center_x > dest_center_x){
			if(c_x_interval >= (center_x - dest_center_x)){
				center_x = dest_center_x;	}
			else {
				center_x -= c_x_interval;	}	}
		else {
			if(c_x_interval >= (dest_center_x - center_x)){
				center_x = dest_center_x;	}
			else {
				center_x += c_x_interval;	}	}
				
		/*  CENTER Y */
		if(center_y > dest_center_y){
			if(c_y_interval >= (center_y - dest_center_y)){
				center_y = dest_center_y;	}
			else {
				center_y -= c_y_interval;	}	}
		else {
			if(c_y_interval >= (dest_center_y - center_y)){
				center_y = dest_center_y;	}
			else {
				center_y += c_y_interval;	}	}

		popupWindow.resizeTo(pwidth, pheight);
		popupWindow.moveTo((center_x)-(pwidth/2), (center_y)-(pheight/2));
	}
	
	/*  returning a reference to the window */
	return popupWindow;
}

/**
 * Trims any whitespace from the beginning or end of a string.
 */
function trim(s) { return String(s).replace(/^\s+|\s+$/g, ''); }

/**
 * Finds the X value of obj from the top of the document.
 *
 * Courtesy of PP]{ at Quirksmode.org
 */
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;
}

/**
 * Finds the Y value of obj from the top of the document.
 *
 * Courtesy of PP]{ at Quirksmode.org
 */
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;
}

/**
 * Will hopefully attach a function to the document.body's onload.  It is a 
 * convenience function that calls addEvent for the current window's 
 * document.body ;
 */
function onBodyLoaded(func){ 
	if(eventAttachingSupported){
		/*  add the onload event to the window */
		if (window.addEventListener){
			window.addEventListener("load",func,false);
			return true;
		} else {
			var r = window.attachEvent("onload",func); 
			return r;
		}
	} else {
		/* We don't want to screw up anything that's there already. */
		if((typeof document.body.onload) != 'function'){
		    document.body.onload = func;
		}
	}
}

/**
 * Will attach an event to an object.  Hopefully.
 */ 
function addEvent(object, event_name, funct){
	if (object.addEventListener) {
		object.addEventListener(event_name, funct, true);
		return true;
	} else if (object.attachEvent) {
		var r = object.attachEvent("on"+event_name, funct);
		return r;
	} else {
		return eval("object.on"+event_name+" = funct;");
	}
}
/**
 * Will remove an event from an object.  Hopefully.
 */ 
function removeEvent(object, event_name, funct){
	if (object.removeEventListener){
		object.removeEventListener(event_name, funct, true);
		return true;
	} else if (object.detachEvent){
		var r = object.detachEvent("on"+event_name, funct);
		return r;
	} else {
		return eval("object.on"+event_name+" = '';");
	}
}

/**
 * Will return an xmlhttprequest object if possible, false otherwise.
 */ 
function getHttpRequestObj(){
	var rspobj = false;
		if (window.XMLHttpRequest) { /*  code for Mozilla, etc. */
		rspobj=new XMLHttpRequest(); }
		else if (window.ActiveXObject) { /*  code for IE */
		rspobj=new ActiveXObject("Microsoft.XMLHTTP"); }
	return rspobj;
}

/**
 * Returns true if node has the class class_name in its attributes.
 */
function hasClass(node, class_name){
	if(node && node.className){
		var classes = node.className.split(' ');
		for ( var i=0; i<classes.length; i++ ){
		    if(classes[i] == class_name){
				return true;
			}
		}
	}
	return false;
}
/**
 * Adds the class class_name to the node node.  Returns false on failure
 * to add class.
 */
function addClass( node, class_name ){
    if(node && (node.className || node.className=='')){
    	node.className += ' '+class_name;
    	node.className = trim(node.className);
    	return true;
	}
	return false;
}
/**
 * Replaces the class old_class with new_class for the node node.
 */
function replaceClass( node, old_class, new_class ){
	if(node && node.className){
		var classes = node.className.split(' ');
		for ( var i=0; i<classes.length; i++ ){
		    if(classes[i] == old_class){
		        classes[i] = new_class;
		}	}
		node.className = classes.join(' ');
		return true;
	}
	return false;
}
/**
 * Removes the class class_name from the node node.  Returns false on failure
 * to remove class.
 */
function removeClass( node, class_name ){
    if(node && node.className){
		var classes = node.className.split(' ');
		for ( var i=0; i<classes.length; i++ ){
		    if(classes[i] == class_name){
		    	classes[i] = '';
		    	node.className = classes.join(' ');
		    	return true;
			}
		}
	}
	return false;
}

/**
 * This function courtesy of Max Starkenburg,
  * http://cnx.rice.edu/.  Thanks!
 */
function getElementsByClassName(class_name){
    var rl = Array();
    var re = new RegExp('(^| )'+class_name+'( |$)');
    var ael = document.getElementsByTagName('*');
    var op = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
    if (document.all && !op) ael = document.all;
    for(var i=0, j=0 ; i<ael.length ; i++) {
        if(re.test(ael[i].className)) {
            rl[j]=ael[i];
            j++;
        }
    }
    return rl;
}

/**
 * Determines the current WINDOW width.
 * 
 * If win is not set, it will refer to the current window.    
 */ 
function winWidth(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	if( typeof( win.innerWidth ) == 'number' ) { /* Non-IE */
		return win.innerWidth;
	} else if( win.document.documentElement && ( win.document.documentElement.offsetWidth || win.document.documentElement.offsetHeight ) ) { /* IE 6+ in 'standards compliant mode' */
		return win.document.documentElement.offsetWidth;
	} else if( win.document.body && ( win.document.body.offsetWidth || win.document.body.offsetHeight ) ) { /* IE 4 compatible */
		return win.document.body.offsetWidth;
	}
}

/**
 * Determines the current WINDOW height.
 */ 
function winHeight(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	if( typeof( win.innerWidth ) == 'number' ) { /* Non-IE */
		return win.innerHeight;
	} else if( win.document.documentElement && ( win.document.documentElement.clientWidth || win.document.documentElement.clientHeight ) ) { /* IE 6+ in 'standards compliant mode' */
		return win.document.documentElement.offsetHeight;
	} else if( win.document.body && ( win.document.body.offsetWidth || win.document.body.offsetHeight ) ) { /* IE 4 compatible */
		return win.document.body.offsetHeight;
	}
}

/**
 * Determines the current PAGE height.
 */ 
function pageHeight(win){
	if(win == null || typeof win != 'object') {
		win = window;
	}
	
	var t1 = win.document.body.scrollHeight;
	var t2 = win.document.body.offsetHeight;
	
	if (t1 > t2) {
		return win.document.body.scrollHeight;
	} else {
		return win.document.body.offsetHeight;
	}
}

/**
 * Determines the current PAGE width.
 */ 
function pageWidth(win){
	if(win == null || typeof win != 'object') {
		win = window;
	}
	
	var t1 = win.document.body.scrollHeight;
	var t2 = win.document.body.offsetHeight;
	
	if (t1 > t2) {
		return win.document.body.scrollWidth;
	} else {
		return win.document.body.offsetWidth;
	}
}

/**
 * Gets the X coord of the window's left side.
 */ 
function posLeft(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	return ((win.screenLeft)
				? win.screenLeft
				: ((win.screenX)
						? win.screenX
						: 0));
}
/**
 * Gets the Y coord of the window's top side.
 */ 
function posTop(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	return ((win.screenTop)
				? win.screenTop
				: ((win.screenY)
						? win.screenY
						: 0));
}
/**
 * Gets the X coord of the window's right side.
 */ 
function posRight(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	return posLeft(win) + winWidth(win);
}
/**
 * Gets the Y coord of the window's bottom side ((my favorite!)).
 */ 
function posBottom(win) {
	if(win == null || typeof win != 'object') {
		win = window;
	}
	return posTop(win) + winHeight(win);
}

/**
 * Should print_r like php does.
 */ 
function print_r(ainput, _indent, spaces)
{
	if((typeof(_indent)!='string') && (_indent==true)){ spaces = true; }
	if(spaces == null) { spaces = false; }

	if(spaces){
	    var indent       = (typeof(_indent)=='string')?_indent+'    ':'    ';
	    var paren_indent = (typeof(_indent)=='string')?_indent+'  ':'';
	} else {
	    var indent       = (typeof(_indent)=='string')?_indent+'&nbsp;&nbsp;&nbsp;&nbsp;':'&nbsp;&nbsp;&nbsp;&nbsp;';
	    var paren_indent = (typeof(_indent)=='string')?_indent+'&nbsp;&nbsp;':'';
	}

    if ( typeof(ainput) == 'string' ) {
        var output = "'"+ ainput +"'\n";
    } else if ( typeof(ainput) == 'boolean' ) {
        var output = (ainput?'true':'false') +"\n";
    } else if ( typeof(ainput) == 'object' ) {
    	if(ainput){
	        var output  = ((ainput.reverse)?'Array':'Object') +"\n";
	        output     += paren_indent + "(\n";
	        for ( var i in ainput ) {
	        	if(_indent.length > 24){
	            	output += indent + "["+ i +"] => RECURSIVE CUTOFF";
				} else {
		            output += indent + "["+ i +"] => "+ print_r(ainput[i],indent,spaces);
		        }
	        }
		}
        output += paren_indent + ")\n";
    }
    return output;
}

/**
 * See PHP's str_pad reference.  
  * {@link http://php.net/manual/en/function.str-pad.php}
 */
function str_pad(str_input, pad_length, pad_string, pad_type){
	if(pad_type == null || pad_type == ''){ pad_type = "STR_PAD_LEFT"; }
	
	str_input += '';
	
	var len = str_input.length;
	if(pad_length > len){
		switch(pad_type){
			case "STR_PAD_RIGHT":
				while(str_input.length < pad_length){
					str_input += pad_string;
				}
				str_input = str_input.substring(0,str_input.length-(str_input.length-pad_length));
				break;
			case "STR_PAD_BOTH":
				var l = false;
				while(str_input.length < pad_length){
					if(l){
						str_input = pad_string+str_input;
					} else {
						str_input += pad_string;
					}
					l = !l;
				}
				if(l){
					str_input = str_input.substring(str_input.length-pad_length);
				} else {
					str_input = str_input.substring(0,str_input.length-(str_input.length-pad_length));
				}
				break;
			case "STR_PAD_LEFT":
			default:
				while(str_input.length < pad_length){
					str_input = pad_string+str_input;
				}
				str_input = str_input.substring(str_input.length-pad_length);
				break;
		}
	}
	return str_input;
}

/**
 * Gives the getElementById function for those browsers that do not 
 * natively support it.  :) 
 */ 		
if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

/**
 * true if the xmlHttpRequest object is supported, false otherwise.
 */ 
var httpRequestSupported = 
	(window.XMLHttpRequest || window.ActiveXObject) 
		?true 
		:false;
/**
 * true if the event attaching is supported in one way or another
 */ 
var eventAttachingSupported = 
	(window.addEventListener || window.attachEvent)
		?true
		:false;
/**
 * true if we're running Mac IE 5 ((Thank you, ppk of quirksmode.org))
 */
var bugRiddenCrashPronePieceOfJunk = 
	( navigator.userAgent.indexOf('MSIE 5')!=-1 && navigator.userAgent.indexOf('Mac')!=-1 )
		?true
		:false;
		
/**
 * true if the browser id DOM compatible ((Thank you, ppk of quirksmode.org))
 */
var W3CDOM = 
	(!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement)
		?true
		:false;

