function findXPos( obj )
{
  var left = 0;

  if ( obj.offsetParent )
  {
    while ( obj.offsetParent )
    {
      left += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if ( obj.x )
    left += obj.x;

  return left;
}

function findYPos( obj )
{
  var top = 0;

  if ( obj.offsetParent )
  {
    while ( obj.offsetParent )
    {
      top += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }
  else if ( obj.y )
    top += obj.y;

  return top;
}

function openWindow( uri, name, width, height, resizable, scrollbars, toolbar, location, directories, status, menubar, copyhistory )
{
  attributes = 'width=' + ( width ? width : 0 ) + ',' +
               'height=' + ( height ? height : 0 ) + ',' +
               'resizable=' + ( resizable == true ? 'yes' : 'no' ) + ',' +
               'scrollbars=' + ( scrollbars == true ? 'yes' : 'no' ) + ',' +
               'toolbar=' + ( toolbar == true ? 'yes' : 'no' ) + ',' +
               'location=' + ( location == true ? 'yes' : 'no' ) + ',' +
               'directories=' + ( directories == true ? 'yes' : 'no' ) + ',' +
               'status=' + ( status == true ? 'yes' : 'no' ) + ',' +
               'menubar=' + ( menubar == true ? 'yes' : 'no' ) + ',' +
               'copyhistory=' + ( copyhistory == true ? 'yes' : 'no' );

  window.open( uri, name, attributes );
}

// Functions and variables for Edit Live for Java
var imageLibrary = '/1-877-334-9638/vo/secure/fm/editlive_images?path=images,/1/Resources/Image Library';
var fileLibrary = '/1-877-334-9638/vo/secure/fm/editlive_link?path=';
var createButton = '/1-877-334-9638/vo/secure/fm/editlive_button';

function setImage( imageUri, editor )
{
  if ( imageUri.length > 0 )
  {
    image = '<img src=\"' + imageUri + '\" border=\"0\"/>';
    editor.InsertHTMLAtCursor( escape( image ) );
    return true;
  }

  return false;
}

function setLink( linkName, target, linkUri, editor )
{
  errors = '';

  if ( linkName.length <= 0 )
    errors = errors + 'Invalid Link Name\n';
  if ( linkUri.length <= 0 )
    errors = errors + 'Invalid Link URI\n';

  if ( errors.length <= 0 )
  {
    path = '<a href="' + linkUri + '" target="' + target + '">' + linkName + '</a>';
    editor.InsertHTMLAtCursor( escape( path ) );
    return true;
  }
  else
    alert( errors );

  return false;
}

function setButton( linkName, linkURL, target, width, height, editor )
{
	button = '<table>';
	button += '<tr>';
	button += '<td>';
	button += '<table cellpadding="0" cellspacing="0">';
	button += '<tr>';
	button += '<td class="buttonLeft"></td>';
	button += '<td class="buttonCenter">';
	
	if ( target == '1' )
		button += '<a class="buttonText" href="javascript:void(0);" onclick="window.open( \'' + linkURL + '\', \'_blank\', \'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes\' );">' + linkName + '</a>';
	else
		button += '<a class="buttonText" href="javascript:void(0);" onclick="document.location.href=\'' + linkURL + '\';">' + linkName + '</a>';
		
	button += '</td>';
	button += '<td class="buttonRight"></td>';
	button += '</tr>';
	button += '</table>';
 	editor.InsertHTMLAtCursor( escape( button ) );
 	return true;
}

function createAndSubmitForm( action, target, fields )
{
  formElement = document._htmlActionForm;
	formFields = document._htmlActionForm._mvc_form_fields;
      
  formElement.action = action;
  formElement.target = target;
  formFields.value = fields;
      
  formElement.submit();
}

function addFormField( objectName, objectValue )
{
  formElement = document._htmlActionForm;
  hiddenNode = undefined;
  
  for ( var i = 0; i < formElement.elements.length; i++ )
  {
    if ( formElement.elements[i].name == objectName )
    {
      hiddenNode = formElement.elements[i];
      hiddenNode.value = objectValue;
      break;
    }
  }
  
  if ( hiddenNode == undefined )
  {
    hiddenNode = document.createElement( 'INPUT' );
    hiddenNode.type = 'hidden';
    hiddenNode.name = objectName;
    hiddenNode.value = objectValue;
    formElement.appendChild( hiddenNode );
  }
}

function showHelp( id, text )
{
  var help = document.getElementById( id );
  var helpText;
  if ( help )
  {
    help.style.visibility = 'visible';
    helpText = document.getElementById( id + "Text" );
    if ( helpText )
      helpText.innerHTML = text;
  }
}

function hideHelp( id )
{
  var help = document.getElementById( id );
  if ( help )
    help.style.visibility = 'hidden';
}

function getQueryValue( name )
{
	query = document.location.search;
	if ( query.length > 0 )
	{
		fields = query.substring( 1 ).split( "&" );
		for ( i = 0; i < fields.length; i++ )
		{
			fieldInfo = fields[i].split( "=" );
			if ( fieldInfo[0] == name )
				return fieldInfo[1];
		}
	}
	
	return null;
}

function createXMLDocument()
{
	if ( document.implementation && document.implementation.createDocument )
		return document.implementation.createDocument( "", "", null );
	else if ( window.ActiveXObject )
		return new ActiveXObject( "Microsoft.XMLDOM" );
	else
		return undefined;
}

function xmlToString( xmlDoc )
{
	if ( document.implementation && document.implementation.createDocument )
		return ( new XMLSerializer() ).serializeToString( xmlDoc );
	else if ( window.ActiveXObject )
		return xmlDoc.xml;
	else
		return "";
}

function stringToXML( string )
{
	if ( document.implementation && document.implementation.createDocument )
		return ( new DOMParser() ).parseFromString( string, "text/xml" );
	else if ( window.ActiveXObject )
	{
		var xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
		xmlDoc.async = "false";
		xmlDoc.loadXML( string )
		return xmlDoc;
	}
	else
		return null;
}

function drawPrettyData()
{
	fields = document.getElementsByTagName( "input" );
	for ( i = 0; i < fields.length; i++ )
	{
		if ( fields[i].id.match( ".+_XML" ) )
		{
			if ( fields[i].value.length > 0 )
			{
				xmlDoc = stringToXML( fields[i].value );
				configurationData = xmlDoc.firstChild;
				if ( configurationData != null && configurationData.nodeName == "configuration-data" )
				{
					table = document.createElement( "table" );
					table.setAttribute( "width", "100%" );
					tableBody = document.createElement( "tbody" );
					for ( j = 0; j < configurationData.childNodes.length; j++ )
					{
						tr = document.createElement( "tr" );
						dataBlock = configurationData.childNodes[j];
						
						for ( k = 0; k < dataBlock.childNodes.length; k++ )
						{
							td = document.createElement( "td" );
							data = dataBlock.childNodes[k];

							for ( l = 0; l < data.childNodes.length; l++ )
							{
								dataValue = data.childNodes[l];

								if ( dataValue.lastChild.firstChild )
								{
									td.appendChild( document.createTextNode( dataValue.lastChild.firstChild.nodeValue ) );
									br = document.createElement( "br" );
									td.appendChild( br );
								}
							}
							
							tr.appendChild( td );
						}
						tableBody.appendChild( tr );
					}
					table.appendChild( tableBody );
						
					prettyDiv = document.getElementById( fields[i].name + "_Pretty" );
					divDetails = prettyDiv.firstChild;
					if ( divDetails != null )
						prettyDiv.removeChild( divDetails );
					prettyDiv.appendChild( table );
				}
			}			
		}
	}
}

function checkAll( form, checkboxName )
{
  for ( i = 0; i < form[checkboxName].length; i++ )
    form[checkboxName][i].checked = true;
}

function checkNone( form, checkboxName )
{
  for ( i = 0; i < form[checkboxName].length; i++ )
    form[checkboxName][i].checked = false;
}

function checkInvert( form, checkboxName )
{
  for ( i = 0; i < form[checkboxName].length; i++ )
    form[checkboxName][i].checked = !form[checkboxName][i].checked;
}

/*
	Debugger Function
  This tag needs to exist to use this function:
  
  <textarea id="debugger" cols="<your_pref>" rows="<your_pref>"></textarea>
  
  Replace cols and rows with the sizes you prefer.
*/
function debugString( string )
{
	debugString( string, false );
}

function debugString( string, clear )
{
  debugBox = document.getElementById( "debugger" );
  
  if ( !debugBox )
  {
  	textarea = document.createElement( "textarea" );
  	textarea.setAttribute( "id", "debugger" );
  	textarea.setAttribute( "cols", "100" );
  	textarea.setAttribute( "rows", "20" );
  	textarea.setAttribute( "line", "0" );
  	document.getElementsByTagName( "body" )[0].appendChild( textarea );
	  debugBox = document.getElementById( "debugger" );
  }
  
  if ( debugBox )
  {
  	if ( clear )
  		debugBox.value = "";
  		
  	debugBox.setAttribute( "line", parseInt( debugBox.getAttribute( "line" ) ) + 1 );
    if ( debugBox.value.length == 0 )
      debugBox.value = debugBox.getAttribute( "line" ) + ". " + string + "\n";
    else
      debugBox.value = debugBox.value + debugBox.getAttribute( "line" ) + ". " + string + "\n";
  }
}

/*
	Function for making javascript http requests
*/
function makeHttpRequest( url, callback_function, return_xml )
{
  var http_request = false;
  
  if ( window.XMLHttpRequest )
  { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if ( http_request.overrideMimeType )
      http_request.overrideMimeType( "text/xml" );
  }
  else if ( window.ActiveXObject )
  { // IE
    try { http_request = new ActiveXObject( "Msxml2.XMLHTTP" ); }
    catch ( e )
    {
      try { http_request = new ActiveXObject( "Microsoft.XMLHTTP" ); }
      catch ( e ) {}
    }
  }

  if ( !http_request )
  {
    alert( "Browser doesn't support this feature." );
    return false;
  }
  
  http_request.onreadystatechange = function()
  {
    if ( http_request.readyState == 4 )
    {
      if ( http_request.status == 200 )
      {
        if ( return_xml )
          eval( callback_function + "(http_request.responseXML)" );
        else
          eval( callback_function + "(http_request.responseText)" );
      }
      else
        alert( "There was a problem with the request: " + http_request.status );
    }
  }
  
  http_request.open( "GET", url, true );
  http_request.send( null );
}

function createRequestString( theform )
{
  var reqStr = "";
  for ( i = 0; i < theform.elements.length; i++ )
  {
    isformObject = false;
    if ( theform.elements[i].tagName.toLowerCase() == "input" )
    {
      switch ( theform.elements[i].type )
      {
        case "text":
        case "hidden":
          reqStr += theform.elements[i].name + "=" + escape( theform.elements[i].value );
          isformObject = true;
          break;
        case "checkbox":
          if ( theform.elements[i].checked )
            reqStr += theform.elements[i].name + "=" + escape( theform.elements[i].value );
          else
            reqStr += theform.elements[i].name + "=";
          isformObject = true;
          break;
        case "radio":
          if ( theform.elements[i].checked )
          {
            reqStr += theform.elements[i].name + "=" + escape( theform.elements[i].value );
            isformObject = true;
          }
      }
    }
    else if ( theform.elements[i].tagName.toLowerCase() == "textarea" )
    {
      reqStr += theform.elements[i].name + "=" + escape( theform.elements[i].value );
      isformObject = true;
    }
    else if ( theform.elements[i].tagName.toLowerCase() == "select" )
    {
      var sel = theform.elements[i];
			if ( sel.selectedIndex >= 0 )
	      reqStr += sel.name + "=" + escape( sel.options[sel.selectedIndex].value );
      isformObject = true;
    }

    if ( isformObject && i + 1 != theform.elements.length )
      reqStr += "&";
  }

  return reqStr;
}

function resizeIframeToContents( iframe )
{
  var height = iframe.contentWindow.document.body.scrollHeight;
  iframe.height = height;
}
