register("SYM.util.dom");
SYM.util.dom.hide = function(idname)
{
	return SYM.util.dom.show( idname,1 );
}
 
SYM.util.dom.show = function(obj, type)
{
	if ( SYM.util.isString( obj) )
	{ obj = SYM.util.dom.getObject(obj ); }
	if (obj != null)
	{
		if (type == null || type == 0)
		{		
			obj.style.display = "";
		}else if (type == 1)
		{
			obj.style.display = "none";
		}else
		{
			if (obj.style.display == "")
			{obj.style.display = "none";}else{obj.style.display = "";}
		}
	}
	return obj;
}
/*
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
SYM.util.dom.addEvent = function(obj, evType, fn)
{
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	// IF OBJECT IS NULL, DO NOTHING.
	if (obj == null)
	{ 
		return false;
	}
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
    		return false;
	}
}
 
SYM.util.dom.addLoadedEvent = function( obj , fn)
{
/*
	"obj" : obj,
	"fn":fn,
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	// IF OBJECT IS NULL, DO NOTHING.
	if (obj == null)
	{ 
		return false;
	}
	loaded:function{ 
		if (document.all.DocumentDelete.readyState == 'complete') {
			createSiteDocument2()												
		};}
*/
}
SYM.util.dom.createIERadio = function( obj )
{
	wrapper = document.createElement( "span"  );
	wrapper.className = "radioTxt";
	sz = "<input type='radio' name='" + obj.name + "' id='" + obj.id + "'"	
	for (i = 0 ; i < obj.attributes.length ; i++)
	{
		if (  obj.attributes[i].name == "value" )
		{
			szValue = obj.attributes[i].value;
			szText = obj.attributes[i].value;
			if (szValue.indexOf("|") != -1)
			{
				szValue = szValue.substring(szValue.indexOf("|") +1, szValue.length);
				szText = szText.substring( 0 , szText.indexOf("|") );
			}	
			sz += " " + obj.attributes[i].name + "='" + szValue + "'"
			if (  obj.attributes[i].name == "value" )
			{
				textNode = document.createTextNode(  szText  );			
			}		
		}else
		{
			sz += " " + obj.attributes[i].name + "='" + obj.attributes[i].value + "'"
		}
	}	
	sz += ">"
	var item = document.createElement(sz);
	wrapper.appendChild(item);
	wrapper.appendChild( textNode );
	if ( obj.linebreak!= null && obj.linebreak)
	{
		wrapper.appendChild(document.createElement("br"));
	}
	
	return wrapper;
}
/* CREATES A NEW DOM OBJECT, READY TO INSERT */ 
SYM.util.dom.create = function( obj )
{
	if (obj == null)
	{return null;}
	var newObject = null;
	if (obj.type == "obj")
	{		
		// this is just an object, so its easier to nest.
		newObject = obj.value;
	}else
	{		
		var wrapper = null;
		if (obj.type == "radio" || obj.type == "text" || obj.type == "checkbox" || obj.type == "hidden" || obj.type == "password")
		{	
			if ( obj.first == null &&  (obj.type == "radio" || obj.type == "checkbox")  )
			{
				/* SPECIAL SHOUD CREATE ONE ITEM AT THE TIME */			
				obj.first = false;							
				wrapper = document.createElement("div")
				for ( var i = 0 ; i < obj.attributes.length ; i++)
				{		
					if ( obj.attributes[i].name == "value" )
					{
						var tmp = SYM.util.clone(obj)		
						tmp.attributes = new Array();
						tmp.attributes.push(obj.attributes[i]);		
						var item = null;
						if ( SYM.util.ie  && obj.type == "radio")
						{			
							tmp.id = tmp.name + i
							item = SYM.util.dom.createIERadio(tmp)										
						}else
						{	
							item = SYM.util.dom.create( tmp );		
						}
						wrapper.appendChild(item);									
					}		
				}
				return wrapper;
			}else	
			{
				if ( (obj.type == "checkbox" || obj.type == "radio"))
				{wrapper = document.createElement( "span" );	wrapper.className = "radioTxt";}
	
				newObject = document.createElement( "input" );
				newObject.type = obj.type
				if (obj.value != null){ newObject.value = obj.value; }		
			}	
		}else
		{
			newObject = document.createElement( obj.type );
			if (obj.type == "select" && obj.multi)
			{
				newObject.setAttribute("multiple" , true);
			} 
		}
		if (obj.name != null)
		{
			newObject.setAttribute( "name" ,obj.name);
			if (obj.id != null)
			{
				newObject.setAttribute(  "id" ,obj.id);
			}else
			{
				newObject.setAttribute(  "id" ,obj.name);
			}
		}else if (obj.id != null)
		{
			newObject.setAttribute(  "id" ,obj.id);
		}
		if (obj.szclass!= null)
		{newObject.className = obj.szclass}
		if(obj.events != null)
		{
			for (i = 0 ; i < obj.events.length ; i++)
			{
				if (obj.events[i] != null && obj.events[i] != "")
				{
					SYM.util.dom.addEvent( newObject ,  obj.events[i].type , obj.events[i].action);
				}
			}
		}
		var textNode = null;
		if(obj.attributes != null)
		{
			for (i = 0 ; i < obj.attributes.length ; i++)
			{
				if (obj.attributes[i] != null && obj.attributes[i] != "")
				{
					if (SYM.util.ie)
					{	
						
						if (obj.attributes[i].name == "cellpadding")
						{obj.attributes[i].name = "cellPadding"}			
	
						if (obj.attributes[i].name == "cellspacing")
						{obj.attributes[i].name = "cellSpacing"}	
	
						if (obj.attributes[i].name == "valign")
						{obj.attributes[i].name = "vAlign"}	
							
					}
					if ( SYM.util.ie && ( obj.attributes[i].name == "style" || obj.attributes[i].name == "class" ))
					{	
						if (obj.attributes[i].name == "class")
						{
							newObject.className = obj.attributes[i].value;
						}else
						{
							newObject.style.cssText = obj.attributes[i].value;
						}
					}else
				{	
					if ( (obj.type == "checkbox" || obj.type == "radio") && obj.attributes[i].name == "value" )
					{
						szValue = obj.attributes[i].value;
						szText = obj.attributes[i].value;
						if (szValue.indexOf("|") != -1)
						{
							szValue = szValue.substring(szValue.indexOf("|") +1, szValue.length);
							szText = szText.substring( 0 , szText.indexOf("|") );
						}	
						textNode = document.createTextNode(  szText   );
						newObject.setAttribute( obj.attributes[i].name, szValue );	
					}else if( obj.type == "select" &&  obj.attributes[i].name == "option")
					{
						optionNode = document.createElement('option'); 
							if (obj.attributes[i].value.indexOf("|") != -1)
							{
								vv = obj.attributes[i].value.split("|")
								optionNode.value = vv[1]
								optionNode .appendChild(document.createTextNode( vv[ 0 ] )); 
							}else
							{
								optionNode .appendChild(document.createTextNode( obj.attributes[i].value )); 
							}
							newObject.appendChild(optionNode); 
						}else if(  obj.type == "textarea" && obj.attributes[i].name == "value" ) 
						{
							newObject.value = obj.attributes[i].value;
						}else if ( obj.attributes[i].name == "onclick" && SYM.util.ie)
						{
							newObject.onclick = new Function (obj.attributes[i].value );						
						}else
						{
							newObject.setAttribute( obj.attributes[i].name, obj.attributes[i].value );		
						}
					}
				}
			}
		}
		if (obj.type == "img" && obj.src != null)
		{
			newObject.src = obj.src.replace("§dbfilepath" , SYM.util.path );
		}
	
		if (obj.szhtml != null  && obj.szhtml.length > 0)
		{
			newObject.innerHTML = obj.szhtml;
		}
		if (wrapper != null)
		{
			wrapper.appendChild( newObject );
			wrapper.appendChild( textNode );
			if ( obj.linebreak!= null && obj.linebreak)
			{
				wrapper.appendChild(document.createElement("br"));
			}
			newObject = wrapper;
		}
	}
	// if we have any items neasted print them.
	if (obj.items != null)
	{	
		for( i = 0 ; i < obj.items.length ; i++)
		{
				tmpitem = SYM.util.dom.create( obj.items[i] );
				if (tmpitem != null)
				{	
					newObject.appendChild(tmpitem)
				}
		}		
	}
	return newObject;
}
/* REMOVES A DOM OBJECT, */
SYM.util.dom.remove = function(obj)
{
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	if ( obj != null )
	{
		obj.parentNode.removeChild(obj)
	}		
}
SYM.util.dom.removeEvent = function (obj, evType, fn, useCapture)
{
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	// IF OBJECT IS NULL, DO NOTHING.
	if (obj == null)
	{ 
		return false;
	}else if (obj.removeEventListener)
	{
		obj.removeEventListener(evType, fn, useCapture);
 		return true;
	 } else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} 
}
SYM.util.dom.replace = function( oldobj , obj , mimic)
{
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( oldobj ) )
	{ oldobj = SYM.util.dom.getObject(oldobj); }
	if (mimic)
	{
		if ( oldobj.className != null ){obj.className = oldobj.className}
		if (SYM.util.ie)
		{
			if  ( oldobj.currentStyle ){obj.style.cssText = oldobj.style.cssText}
		}else
		{
			if ( oldobj.style ){ obj.setAttribute("style" , oldobj.style.cssText ) }		
		}
	}
	
	oldobj .parentNode.replaceChild(obj,oldobj ); 
}
SYM.util.dom.insert = function( obj ,  objinsert )
{
	if (objinsert == null)
	{ 
		return false;
	}
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	// IF OBJECT IS NULL, DO NOTHING.
	if (obj == null)
	{ 
		return false;
	}
	
	obj.appendChild(objinsert);	
}
SYM.util.dom.move = function( idmove, idto)
{
	var move = SYM.util.dom.getObject(idmove);
	var to = SYM.util.dom.getObject(idto);
	if (move != null && to != null)
	{
		move.style.display = "";
		to.appendChild(move);
	}
}
 
SYM.util.dom.onLoaded = function(func) {
	if (document.addEventListener)
	{
		document.addEventListener("DOMContentLoaded", func, false);
	}else
	{
		var oldonload = document.onreadystatechange;
		if (typeof document.onreadystatechange  != 'function') {
			document.onreadystatechange  = func;
		} else {
			document.onreadystatechange  = function() 
			{
				if (oldonload) {
	                			oldonload();
		            		}
            				func();
        			}
    		}
	}
}
SYM.util.dom.onLoad = function(func) {
	var oldonload = window.onload;	
	if (typeof window.onload != 'function') {
		window.onload = func;
 	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
      			}
      				func();
    			}
  	}
}
/*
function(func)
{	
	if (document.onreadystatechange == null )
	{
		document.onreadystatechange = func;
	}else
	{
		document.onreadystatechange = document.onreadystatechange + ; + func;
		alert(document.onreadystatechange);
	}
}
*/
SYM.util.dom.getWindowWidth = function()
{
	var myWidth = 0;
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
    		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}

	wWindow = 
	{
		wWidth: document.body.clientWidth,
		wHeight: document.body.clientHeight,
		dWidth:myWidth,
		dHeight:myHeight
	}
	return wWindow;
}
SYM.util.dom.getObject = function(szobj )
{
	try
	{
		if (document.all)
		{
			return eval( "document.all." +  szobj );
		}else
		{
			return eval( document.getElementById( szobj ) );
		}	
	}catch(oopsy){ }
}
SYM.util.dom.pos = function( obj )
{
	return ({width:obj.offsetWidth, height:obj.offsetHeight })
}
// set the position of one object based on the other.
SYM.util.dom.setPos = function( obj , obj2 , extra)
{
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	var pos = null;
	if ( SYM.util.isArray( obj2 ) )
	{
		pos = obj2;	
	}else
	{
		if ( SYM.util.isString( obj2 ) )
		{ obj2 = SYM.util.dom.getObject(obj2); }	
		pos = SYM.util.dom.findPos(obj2);
	}
	obj.style.top = (pos[1] + ( extra != null && extra.top != null?extra.top:0 ) ) + "px";
	obj.style.left = (pos[0] + ( extra != null && extra.left != null?extra.left:0 ) ) + "px";
	return obj;
}
// set the innerhtml of an object
SYM.util.dom.set = function ( obj ,  szhtml , reset )
{
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	if (obj == null){return null;}
	if (reset)
	{
		obj.innerHTML = szhtml;		
	}else
	{
		obj.innerHTML = obj.innerHTML+ szhtml;		
	}
	return obj;
}
SYM.util.dom.findPos = function(obj) 
{
	// IF THIS IS TEXT, THEN GET THE OBJECT 
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	var curleft = window.scrollLeft;
	var curtop = window.scrollTop;
	if (!obj){return [curleft ,curtop ];}

	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft - obj.scrollLeft
		 	curtop += obj.offsetTop - obj.scrollTop
		 }
	}	
	return [curleft ,curtop ];
}
SYM.util.dom.changeClass = function( obj , classname )
{
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	if (obj == null){return false;}	
	obj.className = classname;
}
SYM.util.dom.clonePos = function(objA, objB , obj)
{
	if (SYM.util.isString(objA)){ objA=SYM.util.dom.getObject(objA) }
	if (SYM.util.isString(objB)){ objB=SYM.util.dom.getObject(objB) }
	if (objA == null || objB == null){return;}
	var A = SYM.util.dom.findPos( objA );
	if (obj == null)
	{
		objB.style.top = A[1] + "px";		
		objB.style.left = A[0]+ "px";
		objB.style.width = objA.offsetWidth+ "px";		
		objB.style.height = objA.offsetHeight+ "px";		
	}else
	{
		objB.style.top = (A[1] + (obj.top!=null?obj.top:0)  ) + "px";		
		objB.style.left = (A[0] + (obj.left!=null?obj.left:0) )+ "px";
		objB.style.width = (objA.offsetWidth + (obj.width!=null?obj.width:0) )+ "px";		
		objB.style.height = (objA.offsetHeight + (obj.height!=null?obj.height:0) )+ "px";	
	}
}
SYM.util.dom.hover = function( obj , on , off )
{
	if ( SYM.util.isString( obj ) )
	{ obj = SYM.util.dom.getObject(obj); }
	
	SYM.util.dom.addEvent(
		obj , 
		"mouseover",
		function(){SYM.util.dom.changeClass( obj , on )}
	);
	SYM.util.dom.addEvent(
		obj , 
		"mouseout",
		function(){SYM.util.dom.changeClass( obj , off )}
	);
}

