var ua = navigator.userAgent;

var aBrowser = new Array();
aBrowser['MSIE'] = (navigator.appName == "Microsoft Internet Explorer");
aBrowser['MSIE5'] = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
aBrowser['MSIE5_0'] = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
aBrowser['Gecko'] = ua.indexOf('Gecko') != -1;
aBrowser['Gecko18'] = ua.indexOf('Gecko') != -1 && ua.indexOf('rv:1.8') != -1;
aBrowser['Safari'] = ua.indexOf('Safari') != -1;
aBrowser['Opera'] = ua.indexOf('Opera') != -1;
aBrowser['Mac'] = ua.indexOf('Mac') != -1;
aBrowser['NS7'] = ua.indexOf('Netscape/7') != -1;
aBrowser['NS71'] = ua.indexOf('Netscape/7.1') != -1;

// Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
if (aBrowser['Opera']) {
	aBrowser['MSIE'] = true;
	aBrowser['Gecko'] = false;
	aBrowser['Safari'] =  false;
}

function createDomElement( tag, attributes )
{
	attribs = attributes || {};
	if( aBrowser['MSIE'] == true )
	{
		var attr = "";
		if( attribs['name'] )
			attr += " name=\""+attribs['name']+"\" ";
		if( attribs['checked'] )
			attr += " checked=\"checked\" ";
		if( attr != '' )
			var oObj = document.createElement( "<"+tag+attr+"></"+tag+">" );
	}
	if( ! oObj )
		var oObj = document.createElement( tag );
	
	for( key in attribs )
	{
		oObj.setAttribute( key, attribs[ key ] );
	}
	return oObj;
}

var URL = {
	location : function( url, newpage )
	{
		if (newpage==1) {
			window.open(url, "_new");
		} else {
			document.location=url;
		}
	},
	
	getParam : function( strParamName )
	{
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > -1 )
		{
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
			{
				if ( aQueryString[iParam].indexOf(strParamName + "=") > -1 )
				{
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
				}
			}
		}
		return strReturn;
	}
}
var Style = {
	setAttribute : function( obj, attrib, value )
		{
			if( obj.style.setAttribute )
			{
				obj.style.setAttribute( attrib, value );
			} else
			{
				obj.style[ attrib ] = value;
			}
		},
	
	removeAttribute : function( obj, attrib )
		{
			if( obj.style.removeAttribute )
			{
				obj.style.removeAttribute( attrib );
			} else                                                            
			{
				obj.style[ attrib ] = null;
			}
		}
}

var Elem = {
	setAttribute : function( obj, attrib, value )
		{
			if( aBrowser['MSIE'] == true )
			{
				switch( attrib.toLowerCase() )
				{
					case "name":
					case "selected":
						alert( "Utólag nem módosítható a(z) " + attrib + " attribútum" );
						return false;
						break;
				}
			}
			if( attrib.toLowerCase() == "class" ||
				attrib.toLowerCase() == "classname"
			)
			{
				obj.setAttribute( "class", value );
				obj.setAttribute( "className", value );
			} else
			{
				obj.setAttribute( attrib, value );
			}
			if( value == "" )
			{
				obj.removeAttribute( attrib );
			}
		},
	
	getAttribute : function( obj, attrib, s )
		{
			var style = s || attrib;
			
			if( attrib.toLowerCase() == "class" ||
				attrib.toLowerCase() == "classname"
			)
			{
				var val = obj[ "class" ] || obj[ "className" ];
				return val;
			} else
			{
				var val = "";
				 if( obj[ attrib ] )
					 val = obj[ attrib ];
				 else
					 val = eval( "obj.style." + style );
				 return typeof( val ) != "undefined" ? val : "";
			}
		},

	removeAttribute : function( obj, attrib )
		{
			if( obj.removeAttribute )
			{
				obj.removeAttribute( attrib );
			} else                                                            
			{
				obj[ attrib ] = null;
			}
		},
	
	cloneNode : function( obj, deep )
		{
			var clonedeep = deep || true;
			var clone = null;
			
			if( aBrowser['MSIE'] == true )
			{
				clone = obj.cloneNode( clonedeep );
			} else if( aBrowser['Gecko'] == true )
			{
				// create the serializer object
				var stringMaker	= new XMLSerializer();
				
				// convert the xmlDocument object to a string
				var xmlString	= stringMaker.serializeToString( obj );
				
				// display the string
	//			var oParser = new DOMParser;
	//			clone = oParser.parseFromString( xmlString, "text/xml" );
	
				oParent = document.createElement( 'div' );
				oParent.innerHTML = xmlString;
				clone = oParent.firstChild;
			}
			return clone;
		},
	
	findParent : function( obj, attrib, value )
		{
			while( obj && obj.parentNode && obj.getAttribute( attrib ) != value )
			{
				obj = obj.parentNode;
			}
			if( ! obj || ! obj.parentNode  ) return false;
			return obj;
		},
	
	getParent : function( node )
		{
			if (node.nodeType == 1)
				return node;
	
			// Find parent node that is a element
			while ((node = node.parentNode) != null && node.nodeType != 1) ;
	
			return node;
		},
	fromString : function( string )
		{
			if( string == "" ) return null;
			var placeholder = createDomElement( 'div' );
			placeholder.innerHTML = string;
			var fragment = placeholder.firstChild;
			return fragment;
		},
	toString : function( obj )
		{
			if( typeof( obj ) != "object" ) return "";
			
			var placeholder = createDomElement( 'div' );
			var str = "";
			if( aBrowser['MSIE'] == true )
			{
				str = obj.outerHTML;
			} else
			{
				placeholder.appendChild( obj );
				str = placeholder.innerHTML;
			}
			return str;
		},
	innerText : function( obj )
		{
			if( aBrowser['MSIE'] == true )
			{
				str = obj.innerText;
			} else
			{
				str = obj.textContent;
			}
			return str;
		}
};
/*
var CDocument = {
	getSelection : function()
		{
			if( aBrowser['MSIE'] == true && aBrowser['Opera'] == false )
				return document.selection;
	
			return window.getSelection();
		},
		
	getRange : function()
		{
	
			var sel = this.getSelection();
			if (sel == null)
				return null;
	
			if( aBrowser['MSIE'] == true && aBrowser['Opera'] == false )
				return sel.createRange();
	
			sel = this.getSel();
			var rng = sel.getRangeAt(0);
			return rng;
		},
	
	getFocusElement : function( r ) 
		{
			if( aBrowser['MSIE'] == true && aBrowser['Opera'] == false )
			{
				var doc = document;
				var rng = r || doc.selection.createRange();
		
				var elm = rng.item ? rng.item(0) : rng.parentElement();
			} else {
				var rng = r || this.getRange();
		
				var elm = rng.commonAncestorContainer;
				//var elm = (sel && sel.anchorNode) ? sel.anchorNode : null;
		
				// Handle selection a image or other control like element such as anchors
				if (!rng.collapsed) 
				{
					// Is selection small
					if (rng.startContainer == rng.endContainer) 
					{
						if (rng.startOffset - rng.endOffset < 2) 
						{
							if (rng.startContainer.hasChildNodes())
								elm = rng.startContainer.childNodes[rng.startOffset];
						}
					}
				}
		
				// Get the element parent of the node
				elm = Elem.getParent(elm);
		
				//if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "img")
				//	elm = tinyMCE.selectedElement;
			}
		
			return elm;
		},
		
	getSelectedHTML : function( range )
		{
			if( aBrowser['MSIE'] == true && aBrowser['Safari'] == false )
			{
				// Not realy perfect!!
		
				return range.toString();
			}
		
			var elm = document.createElement("body");
		
			if( aBrowser['Gecko'] == true )
				elm.appendChild( range.cloneContents());
			else
				elm.innerHTML = range.htmlText;
		
			return elm;
		}
};
*/
