
var Utils = {};
Utils.Path = {};


function id(obj)
{
	if (typeof obj == "string")
	{
	    //alert(obj + " is instance of string");

		obj = document.getElementById(obj);
	} else {
	    //alert("not instance of string");	
	}
	return obj;
}

function writeln(s)
{
	document.write(s);
	document.write("<br>");
}

Utils.ShowHideElement = function (el, show)
{
    var el = id(el);
    if (el) {
    
    	if (arguments.length == 1)
    	{
    		// not passing show means to toggle;
    		if (el.style.display == 'none')
    			show = true;
    		else
    			show = false;
    	}
	   // alert("style.display=" + el.style.display);
	    
	    if (show)
	    {
	    //	alert("making visible");
	    	el.style.display = '';
	    } else {
	    //	alert("making hidden");
		    el.style.display = 'none';

	    }
	} else {
		Debug.Alert(null, " el == null");
	}

}

Utils.PointInRect = function(pt, rect)
{
	if (pt.x >= rect.x && pt.x <= rect.x + rect.width &&
		pt.y >= rect.y && pt.y <= rect.y + rect.height)
	{
		return true;
	}
	return false;
}

Utils.IsVisible = function( el, partialOk)
{
	if (arguments.length == 1)
	{
		partialok = false;
	}
	
	var ptEl = Utils.GetElementPosition(el);
	var ptEl2 = {};
	ptEl2.x = ptEl.x + el.offsetWidth;
	ptEl2.y = ptEl.y + el.offsetHeight;
	
	var rect = Utils.GetVisibleRect();
	if (rect)
	{
		if (partialOk)
		{
			if (Utils.PointInRect(ptEl, rect) ||
				Utils.PointInRect(ptEl2, rect))
			{
				return true;
			}
			return false;
		}
		else
		{
			if (Utils.PointInRect(ptEl, rect) &&
				Utils.PointInRect(ptEl2, rect))
			{
				return true;
			}
			return false;
		}
	}
	return false;
}

Utils.FindParentOfName = function( n )
{
	var w = window;
	
	do
	{
		if (w.name == n)
			return w;
		w = w.parent;
	} while (w.parent != w);
	
	if (w.name == n)
		return w;
	
	return null;
}


// returns a function that, given an object, returns the string of the property prop
function GetPropertyFunc(prop)
{
   var f = new Function("obj", "return obj."+prop +";");   
   return f;
}


function GetProperty(obj, prop)
{
   var f = GetPropertyFunc(prop);
   //AlertObj(f);
   return prop + " : " + f(obj);
}

Utils.RunOnLoad = function (f)
{
	if (Utils.RunOnLoad.loaded) 
		f();
	else
		Utils.RunOnLoad.funcs.push(f);
}
Utils.RunOnLoad.funcs = [];
Utils.RunOnLoad.loaded = false;
Utils.RunOnLoad.Run = function ()
{
	with (Utils) {
	with (RunOnLoad)
	{
		if (loaded) return;
		
		for (var i = i; i < funcs.length; i++)
		{
			try { 
				funcs[i](); 
			}
			catch(e) {
			}
		}
		
		RunOnLoad.loaded = true;
		delete funcs;
		delete Run;
	}
	}
}


if (window.addEventListener)
	window.addEventListener("load", Utils.RunOnLoad.Run, false);
else if (window.attachEvent) window.attachEvent("onload", Utils.RunOnLoad.Run);
else window.onload = Utils.RunOnLoad.Run;




Utils.Path.GetFileName = function(szPath)
{
	var szComponents = szPath.split("/");
	if (szComponents.length > 0)
	{
		var strFilename = szComponents[szComponents.length - 1];
		return strFilename;
	}
	
	return null;
}

Utils.Path.Parent = function( strPath )
{
	var iStartSearch = strPath.length - 1;
	
	// if the last char is a /, skip it
	// a neg number means from the end of the string
	if (strPath.substr(-1, 1) == "/")
	{
		iStartSearch--;
	}
	
	var iStringEnd = strPath.lastIndexOf("/",  iStartSearch);
	if (iStringEnd != -1)
	{
		
		var str = strPath.substr(0, iStringEnd);
		return str;
	}
}

Utils.Path.RemoveExtension = function(strFile)
{
	var strName = strFile.split(".");
	return strName[0];
}

Utils.Path.Combine = function(strPath)
{
	str = strPath;
	
	for (var i = 1; i < arguments.length; i++)
	{

		if (str[str.length - 1] != '/')
		{
			str += "/";
		}
		str += arguments[i];
	 		
	}
	
	
	return str;
}

Utils.QuoteString = function( str )
{
	return '\"' + str + '\"';
}
var Q = Utils.QuoteString;
	
Utils.GetWindowSize = function()
{
	var sz = {};
	
	if (window.innerHeight != null)
	{
		sz.width = window.innerWidth;
		sz.height = window.innerHeight;
	} else if (document.body.clientWidth != null)
	{
		sz.width = document.body.clientWidth;
		sz.height = document.body.clientHeight;		
	} else if (document.documentElement.clientWidth != null)
	{
		sz.width = document.documentElement.clientWidth;
		sz.height = document.documentElement.clientHeight;		
	} else
	{
		//alert("can't get the window size");
		return null;
	}
	return sz;
}

Utils.GetVisibleRect = function()
{
	with (Utils)
	{
	
	var rect = {};
	
	var pt = GetScrollPosition();
	if (pt)
	{
		rect.x = pt.x;
		rect.y = pt.y;
		
		var sz = GetWindowSize();
		rect.width = sz.width;
		rect.height = sz.height;
		rect.top = pt.y;
		rect.left = pt.x;
		rect.right = rect.left + rect.width;
		rect.bottom = rect.top + rect.height;
	
		return rect;
	}
	return null;
	}
}

Utils.GetScrollPosition = function()
{
	var pt = {};
	
	if (document.body.scrollLeft != null)
	{
		pt.x = document.body.scrollLeft;
		pt.y = document.body.scrollTop;
	} 
	else if (document.documentElement.scrollLeft != null)
	{
		pt.x = document.documentElement.scrollLeft;
		pt.y = document.documentElement.scrollTop;
		
	} else if (window.pageXOffset != null)
	{
		pt.x = window.pageXOffset;
		pt.y = window.pageYOffset;
	} else
	{
		//alert("can't get the window size");
		return null;
	}
	return pt;
}

function GetXY(obj)
{
	return Utils.GetElementPosition(obj);
}
Utils.GetElementPosition = function( el )
{
	var x = 0;
	var y = 0;
	
	var width = el.offsetWidth;
	var height = el.offsetHeight;
	
	while (el) {
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}
	
	return { x:x, y:y,
		width: width,
		height: height,
		bottom : y+height,
		right : x+width,
		top :y,
		left: x
	};
	
}


Utils.LoadScript = function(path, file)
{
   var scriptline =  '<script src=\"' + Utils.Path.Combine(path, file) + '\"><\/script>';
   document.write(scriptline);

}

Utils.scrollIntoView = function (el)
{
	var rect = Utils.GetElementPosition(el);
	var rectVisible = Utils.GetVisibleRect();
	var pt = Utils.GetScrollPosition();
	
	if (rect.top <= rectVisible.top)
	{
		pt.y = rect.top;
	}
	
	if (rect.left <= rectVisible.left)
	{
		pt.x = rect.left;
	}
	
	if (rect.bottom >= rectVisible.bottom)
	{
		pt.y = rect.bottom - rectVisible.height;
	}
	
	if (rect.right >= rectVisible.right)
	{
		pt.x = rect.right - rectVisible.width;
	}
	
	window.scrollTo(pt.x, pt.y);
}

Utils.scrollToTop = function (el)
{
	var pt = Utils.GetElementPosition(el);
	window.scrollTo(pt.x, pt.y);
}


Utils.EnsureVisible = function (el, partialOk)
{
	if (arguments.length == 1)
	{
		partialok = false;
	}

	if (!Utils.IsVisible(el, partialOk))
	{
		Utils.scrollIntoView(el);
	}
}

/* loads an external js or css file */
/* loadjscssfile("filename", "js"); */
/* loadjscssfile("filename", "css"); */

function loadjscssfile(filename, filetype){
    if (filetype=="js"){ //if filename is a external JavaScript file
	var fileref=document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
    }
    else if (filetype=="css"){ //if filename is an external CSS file
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", filename);
    }
    if (typeof fileref!="undefined")
	document.getElementsByTagName("head")[0].appendChild(fileref);

    /* the other way is to do it using a writeln of script.  this will force a load immediately */
    /* document.writeln('<script src="' + filename + '"></script>');*/

}

String.prototype.folderOf=function(no_slash){ 
    return this.slice(0,this.lastIndexOf('/',this.length-(/\/$/.test(this)?2:0))+(no_slash?0:1)) 
}; 
