function checkIt( item )
{
  var i = 0;
}

/**
 *	this variable is a global  name space resolver.
 *	You will need this if you want to make a search call
 *	like bb.evaluate and have an element in your search pad
 *	that lives outside of the default namespace.
 **/
namespaceResolver = function( prefix )
{
	var ns = { 'aj':'http://www.aboutjohnrock.com/2009/aj',
		'b':'http://www.backbase.com/2006/btl',
		'e':'http://www.backbase.com/2006/xel' };
		 
	return ns[prefix] || null;
}

/**
	One difference between IE and FF is that in the event
	object IE will pass you a "srcElement" for the element
	that sent the event, FF will pass "target".  BB wraps
	this and always give you a "target" but sometimes you 
	have to deal with DOM events and you'll enjoy this
	simply switch function.
*/
function getSourceFromEvent( event )
{
	if ( event.target )
	{
		return event.target;
	}
	else if ( event.srcElement )
	{
		return event.srcElement;
	}
	
	return event;
}


/**
 *	This function attempts to return the text
 *	value from an XML node.  IE and FF do things
 *	differently here by tucking the data into either the
 *	"data" attribute of the "value" attribute.
 *	
 *	Use this function to take the browser out of the
 *	equation.
 **/
function getTextFromData( oNode )
{
	if ( oNode.data )
	{
		return oNode.data;
	}
	else if ( oNode.value )
	{
		return oNode.value;
	}
	else if ( oNode.textContent )
	{
		return oNode.textContent;
	}
	else if ( oNode.innerHTML )
	{
		return oNode.innerHTML;
	}
	else if ( oNode.text )
	{
		return oNode.text;
	}

	return oNode;
}


/**
 * 
 * Helper function to set the value of an input element
 **/
function setInputText( input, str )
{
	if ( input.data )
	{
		input.data = str;
	}
	else if ( input.value )
	{
		input.value = str;
	}
	else if ( input.textContent )
	{
		input.textContent = str;
	}
	else if ( input.innerHTML )
	{
		input.innerHTML = str;
	}
	else if ( input.text )
	{
		input.text = str;
	}
}

/**	
 *	Completely remove all children from the given node.
 **/
function removeChildren( node )
{
	if ( node.hasChildNodes() )
	{
		if (node.viewNode && node.modelNode)
		{
			bb.controller.destructChildren(node);
		}
		else
		{
			while( node.childNodes.length >= 1 )
			{
				try
				{
					var c = bb.getControllerFromModel( node.firstChild );
					bb.command.destroy( c );
				}
				catch( error )
				{
					bb.command.destroy( node.firstChild );
					//node.removeChild( node.firstChild );
				}
			}
		}
	}
}

/**
 *	Will replace any character in a string with a
 *	replacement string.
 **/
function replaceAll( string, char, replacement )
{
	var tokens = string.split( char );
	string = '';
	
	for( var i = 0; i < tokens.length; i++ )
	{
		string += tokens[i] + replacement; 
	}
	
	string = string.substring( 0, string.lastIndexOf( replacement ) );
	return string;
}


/**
 *	This function will take a controller and generate a CGI script
 *	from all the form input widgets that it finds - very similar to
 *	a form.submit call.
 **/
function generateCGIFormData( controller )
{
	if ( !controller )
		return;

	var inputs = bb.evaluate( "descendant::input", controller.modelNode );
	var options = bb.evaluate( "descendant::option", controller.modelNode );
	var cgi = '';
	
	for( var i = 0; i < inputs.length; i++ )
	{
		var input = inputs[i];
		var iC = bb.getControllerFromModel( input );
		
		if ( input.getAttribute( 'type' ) == 'checkbox' )
		{
			cgi += input.getAttribute( 'name' ) + "=" + iC.viewNode.checked;
		}
		else if ( input.getAttribute( 'type' ) == 'radio' )
		{
			cgi += input.getAttribute( 'value' ) + "=" + iC.viewNode.checked;
		}
		else
		{
			cgi += input.getAttribute( 'name' ) + "=";
		
			var k = input.value;
			
			if ( k=="" || k==null)
			{
				if( iC.getAttribute('value') )
					cgi += iC.getAttribute('value');
				else
					cgi += iC.getProperty('value')
			} else
				cgi += input.value;
		}
		
		if ( (i+1) != inputs.length )
		{
			cgi += '&';
		}
	}
	
	if ( cgi != '' && options.length > 0 )
	{
		cgi += '&';
	}
	
	for( var i = 0; i < options.length; i++ )
	{
		var option = options[i];
		var sC = bb.getControllerFromModel( option );
	
		cgi += sC.getAttribute( 'value' ) + '=';
		
		if ( sC.getAttribute( 'selected' ) )
		{
			cgi += 'true';
		}
		else
		{
			cgi += 'false';
		}
		
		if ( (i+1) != options.length )
		{
			cgi += '&';
		}
	}

	return cgi;
}


/**
 *	Convenience function to get a particular style value
 *	from the style attribute without making you do any 
 *	complicated parsing.
 **/
function getStyleValue( node, styleName )
{
	var value = node.getAttribute( 'style' );
	
	if ( bb.browser.ie )
	{
		value = value.cssText;
		styleName = styleName.toUpperCase();
	}
	
	value = value.substring( value.indexOf( styleName ), value.length );
	 
	if ( value.indexOf( ";" ) != -1 )
	{
		value = value.substring( 0, value.indexOf( ";" ) );
	}
	
	value = value.substring( value.indexOf( ":" ) +1, value.length );
	
	return trim( value );
}


/**
 *	Convenience function to edit specific style contents
 *	of a previously defined CSS class.  This will apply
 *	style changes to all elements with specified class.
 **/
function editClassStyle( className, styleName, styleValue )
{
	
	if ( typeof document.styleSheets != 'undefined')
	{
		var sheet = document.styleSheets;
		var i = sheet.length;

		while ( i-- )
		{
			var s = sheet[i];

			// Determine rule type - IE = rules, Moz = cssRules
			var rType = ( typeof s.rules != 'undefined' ) ? 'rules' : 'cssRules';
			
			if (typeof s[rType] != 'undefined')
			{
				var rs = s[rType];
				var j = rs.length;
				
				while ( j-- )
				{
					var r = rs[j];
					
					// Check to see if iterator has found specified class
					if ( r.selectorText == '.' + className )
					{
						var str = styleName.toLowerCase();
						var strExp = new RegExp( styleName, "i" );
						
						//Check to see if specified style exists within class
						if( r.style.cssText.toLowerCase().match( strExp ) ) 
						{
			             	// Requested style exists, modify the value
			             	var startIndex = r.style.cssText.toLowerCase().indexOf( str + ":" );
			             	var modString = r.style.cssText.substr( startIndex );
			             	
			             	//alert( modString );
			             	
			             	var endIndex = modString.indexOf( ';' );
			             	
			             	if( endIndex != -1 )
			             	{
			             		modString = modString.substring(0,endIndex);
			             	}
			             	
			             	var newStyle = styleName + ": " + styleValue + ";";
			             		
			             	r.style.cssText = r.style.cssText.replace( modString, newStyle );
             	
			            }
						else
			        	{
			        		// User has requested a new style, add it
			        	 	var newStyle = styleName + ": " + styleValue + "; ";
			        		r.style.cssText = newStyle + r.style.cssText;
			        	}

						return;
           			}
				}
			}
		}
	}
}


/**
 *	Return the actual x/y position of an object.  Returns pixel values.
 *	The "container" argument specifies a DOM element if you would like
 *	to find an object's position within another object.
 **/
function getPos( element, container )
{
	var curleft = curtop = 0;
	
	if (element.offsetParent) {
		do {
			curleft += element.offsetLeft;
			curtop += element.offsetTop;
			if( element.offsetParent == container)
				break;
		} while (element = element.offsetParent);
		
		return [curleft,curtop];
	}
	else
		return false;
}

/**
	calculate the cursor position for an event
**/
function getCursorPosition( event )
{
	if ( !event )
	{
		return
	}
	
    var cursor = {x:0, y:0};
    
    if ( event.pageX || event.pageY ) 
    {
        cursor.x = event.pageX;
        cursor.y = event.pageY;
    } 
    else 
    {
        var doc = document.documentElement;
        var body = document.body;
        cursor.x = event.clientX + 
            ( doc.scrollLeft || body.scrollLeft) - ( doc.clientLeft || 0);
        cursor.y = event.clientY + 
            ( doc.scrollTop || body.scrollTop) - (doc.clientTop || 0);
    }
    return cursor;

}

/**
 *	String manipulation functions
 **/
function trim(str, chars) 
{
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}