

function closeIbox()
{
    if(window.closeHomeIbox)
    {
        closeHomeIbox();
    }
    else
    {
        hideIbox();
    }
}

function AddEvent(object, eventType, eventHandler)
{
	if (object.addEventListener)
	{
		object.addEventListener(eventType, eventHandler, false); 
		return true; 
	} 
	else if (object.attachEvent)
	{
		var r = object.attachEvent("on" + eventType, eventHandler);
		return r;
	}
	else 
	{
		return false;
	} 
}


function AddStamp(controlName, text, stampAtBottom)
{
	var control = document.getElementById(controlName);
	if(stampAtBottom)
	{
	    if (control.value != "")
		    control.value += "\n";
    		
	    control.value += text + "\n";
    	
	    scrollToBottom(control);
	    setCaretToEnd(control);
	}
	else
	{
	    if(control.value != "")
	    {
	        text += "\n\n";
	    }
	    control.value = text = control.value;
	    
	    scrollToTop(control);
	    setCaretToStart(control);
	}
	control.focus();
}

function StampHtml(clientId, text, stampAtBottom)
{
    try
    {
	    //var ftb = FTB_API[clientId];
	    var radE = GetRadEditor(clientId);
	    if (radE != null)
	    {
		    var html = radE.GetHtml();
		    if(stampAtBottom)
		    {
		        if (html.length > 0)
		        {
				    html += "<br/><br/>";
		        }
		        html += text + "<br/>";
		    }
		    else
		    {
		        if(html.length > 0)
		        {
		            text += "<br /><br />";
		        }
		        html = text + html;
		    }

		    radE.SetFocus();
		    radE.SetHtml(html);
		    if(stampAtBottom)
		    {
		        if (radE.Document.selection)//IE
                {
                    var oRange = radE.Document.selection.createRange();
                    oRange.scrollIntoView();
                }
                else //Moz
                {   
                    //We assume that the added text is at the bottom of the editor and this is the zone that we want to show
                    var oBody = radE.Document.body;
                    //Set cursor at the end of the text
                    radE.Fire("SelectAll");
                    var oSel = radE.ContentWindow.getSelection();
                    oSel.collapseToEnd();
                    //Scroll the body to make the text visible
                    var top = oBody.offsetHeight;
                    oBody.scrollTop = top;
                }
            }
	    }
	}
	catch(e)
	{
	    AddStamp(clientId, text, stampAtBottom);
	}
}

/////////////////////////
// Functions for scrolling and setting the input for a TextArea HTML control
// code copied from: http://www.faqts.com/knowledge_base/view.phtml/aid/7746/fid/130
/////////////////////////
function scrollToTop (element) {
    element.scrollTop = 0;
}
function scrollToBottom (element) {
    element.scrollTop = element.scrollHeight;
}
function setCaretToStart (input) {
  if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.select();
  }
}
function setCaretToEnd (input) {
  if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(false);
    range.select();
  }
}
/////////////////////
// end code copied from: http://www.faqts.com/knowledge_base/view.phtml/aid/7746/fid/130
/////////////////////

function showHideDiv(elemId)
{
	if (document.getElementById)
	{
		var div = document.getElementById(elemId);
		if (div)
		{
			if (div.style.display == "block")
			{
				div.style.display = "none";
			}
			else
			{
				div.style.display = "block";
			}
		}
	}
}

function showHideSpan(elemId)
{
	if (document.getElementById)
	{
		var div = document.getElementById(elemId);
		if (div)
		{
			if (div.style.display == "inline")
			{
				div.style.display = "none";
			}
			else
			{
				div.style.display = "inline";
			}
		}
	}
}

function showSpan(elemId)
{
	if (document.getElementById)
	{
		var span = document.getElementById(elemId);
		if (span)
		{
			span.style.display = "inline";
		}
	}
}

function showDiv(elemId)
{
	if (document.getElementById)
	{
		var div = document.getElementById(elemId);
		if (div)
		{
			div.style.display = "block";
		}
	}
}

function hideDiv(elemId)
{
	if (document.getElementById)
	{
		var div = document.getElementById(elemId);
		if (div)
		{
			div.style.display = "none";
		}
	}
}

function closeWindow(redirectUrl, inWindow)
{
    redirectUrl = unescape(redirectUrl);
    if(!inWindow)
    {
        window.location = redirectUrl;
    }
    else
    {
        if(self.opener)
        {
            if(self.opener.doHomeRefresh)
            {
                self.opener.doHomeRefresh();
            }
            else
            {
                self.opener.doRefresh();
            }
        }
        self.close();
    }
}

function getBrowserInformation()
{
	try
	{
		if (typeof window.parent.BrowserInformation != 'undefined')
			return window.parent.BrowserInformation;
	}
	catch(ex)
	{
	}
	
	return getThisWindowBrowserInformation();
}

function getThisWindowBrowserInformation()
{
	try
	{
		if (typeof window.BrowserInformation != 'undefined')
			return window.BrowserInformation;
	}
	catch(ex)
	{
	}
	
	return null;
}

function clearList(selectList)
{
	// clear list
	while (selectList.options.length > 0)
	{
		selectList.options[0] = null;
	}
}
		
function parentPickListValueChanged(sender, senderId, itemType)
{
	var val = sender.value;
	var result = ParentPicklistValueChanged(senderId, val, itemType);
	if(!result)
		return;
		
	var foo = "var vals = " + result;
	eval(foo);
	var i =0;
	for(i = 0;i< vals.length;i++)
	{
		if(!vals[i])
			break;
		var childList = document.getElementById(vals[i].ChildPickListId);
		if(!childList)
			continue;
							
		clearList(childList);
		var j = 0;
		for (j=0; j < vals[i].PickListValues.length; j++)
		{
			var opt = document.createElement("option");
			childList.options.add(opt);
			opt.text = vals[i].PickListValues[j].Text;
			opt.value  = vals[i].PickListValues[j].Value;
		}
	}
}