function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}

function OpenPopUpWindow(URL, WIDTH, HEIGHT)
{
	var url = URL;
	var w = WIDTH;
	var h = HEIGHT;
	var day = new Date();
	var id = day.getTime();	
	var ScreenWidth=window.screen.width;
    var ScreenHeight=window.screen.height;
    var movefromedge=0;
    placementx=(ScreenWidth/2)-((w)/2);
    placementy=(ScreenHeight/2)-((h)/2);
    WinPop=window.open(url,"","width="+w+",height="+h+",toolbar=0,location=0,directories=0,status=0,scrollbars=1,menubar=0,resizable=0,left="+placementx+",top="+placementy+",screenX="+placementx+",screenY="+placementy);
}

function ChangeSwatch(ImageURL,ColorName)
{
    var imageId = "imgLargeSwatch";
    var divId = document.getElementById("LargeSwatch");
    var spanId = document.getElementById("spanColorName");
    
    
    if (!document.getElementById(imageId)) return;
    var image = document.getElementById(imageId);
    image.src = ImageURL;    
    spanId.innerHTML = ColorName;
	
	divId.style.visibility = 'visible';
	hideCollidedSelectBoxes();
}

function HideSwatch()
{
    var divId = document.getElementById("LargeSwatch");
    divId.style.visibility = 'hidden';
    showSelectBoxes();
}


function hideCollidedSelectBoxes()
{
	var selectArray = document.getElementsByTagName('select');
	var o = document.getElementById("LargeSwatch");
	
	for (var i=0; i<selectArray.length; i++)
	{
		if (detectCollision(o, selectArray[i]))
		{
			selectArray[i].style.visibility = 'hidden';
		}
	}
}

function showSelectBoxes()
{
	var selectArray = document.getElementsByTagName('select');
	var o = document.getElementById("LargeSwatch");
	for (var i=0; i<selectArray.length; i++)
	{
		selectArray[i].style.visibility = (selectArray[i].style.visibility == 'hidden') ? 'visible': '';
	}
}


// collision detection theory
// for each pair of object, if one of their axis does not collide, then they do not collide
// implementation: loop through each axis and return false if they do not collide
// 
// method: detectCollision(a:Object, b:Object):Boolean
// a: document element object
// b: document element Object
// return: Boolean

function detectCollision(a,b)
{

	// x-axis
	var xA = getAbsoluteLeft(a);
	var xB = getAbsoluteLeft(b);
	var wA = a.offsetWidth;
	var wB = b.offsetWidth;
	if (Math.max(xA+wA, xB+wB)-Math.min(xA, xB) > (wA+wB))
		return false;
		
	// y-axis
	var yA = getAbsoluteTop(a);
	var yB = getAbsoluteTop(b);
	var hA = a.offsetHeight;
	var hB = b.offsetHeight;
	if (Math.max(yA+hA, yB+hB)-Math.min(yA, yB) > (hA+hB))
		return false;

	return true;
	
}

function getAbsoluteLeft(o)
{
	var result = o.offsetLeft;
	var p = o.offsetParent;
	while (p != null)
	{
		result += p.offsetLeft;
		p = p.offsetParent;
	}
	return result;
}

function getAbsoluteTop(o)
{
	var result = o.offsetTop;
	var p = o.offsetParent;
	while (p != null)
	{
		result += p.offsetTop;
		p = p.offsetParent;
	}
	return result;
}

function MakeKeepAliveRequest()
{
	if (!KeepAliveUrl)
		return;

	if (KeepAliveTimer != null)
		window.clearTimeout(KeepAliveTimer);

	var x = null;
	if (typeof XMLHttpRequest != "undefined") 
	{
		x = new XMLHttpRequest();
	} 
	else 
	{
		try 
		{
			x = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				x = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}
	
	try
	{
		// don't do this asynchronously
		x.open("GET", KeepAliveUrl, false, "", "");
		x.send(null);
	}
	catch (e)
	{
	}
	
	KeepAliveTimer = window.setTimeout(MakeKeepAliveRequest, 599999);
}

function DetermineKeepAliveUrl()
{
	var scripts = document.getElementsByTagName("SCRIPT");
	var i;
	var url;
	
	for (i = 0; i < scripts.length; i++)
	{
		url = scripts[i].src.toLowerCase();
		if (url.indexOf('js/global.js') != -1)
			return url.replace('js/global.js', 'PersistSession.aspx');
	}
	
	return null;
}

var KeepAliveUrl = DetermineKeepAliveUrl();
var KeepAliveTimer = window.setTimeout(MakeKeepAliveRequest, 60);








