/* vim: set expandtab tabstop=3 shiftwidth=3 softtabstop=3: */

/**
 * Adds errors to the formerror div.
 *
 * @param Array   errors   Array of error strings.
 * @param bool    keep     true to keep existing errors.
 *
 */
function addFormErrors(errors, keep)
{
   if (!keep)
   {
      clearFormErrors();
   }

   if (errors.length)
   {
      var div = document.getElementById('jsformerror');

      if (div)
      {
         div.style.display = 'block';
         div = document.getElementById('formerror');

         if (div)
         {
            var ul = document.createElement('ul');
            div.appendChild(ul);
            var li;

            for (var i in errors)
            {
               li = document.createElement('li');
               ul.appendChild(li);

               li.appendChild(document.createTextNode(errors[i]));
            }
         }
      }
   }
}


/**
 * Clears the current formerrors.
 */
function clearFormErrors()
{
   var div = document.getElementById('jsformerror');

   if (div)
   {
      div.style.display = 'block';
      div = document.getElementById('formerror');

      if (div)
      {
         while (div.childNodes.length)
         {
            div.removeChild(div.firstChild);
         }
      }
   }
}


/**
 * Determines if a string is ok to use (doesn't have some special characters)
 * as a filename.
 *
 * @warning This code is duplicated in MaxToolkit.php!
 *
 * @param string    $s  String to check.
 *
 * @return bool true if ok, false if not.
 */
function okAsFname(s)
{
   for (var i = 0; i < s.length; i++)
   {
      var c = s.charAt(i);
      var cc = s.charCodeAt(i);

      if ((cc < 32) || (cc > 126) || (c == '$') || (c == '*') || (c == '/'))
      {
         return (false);
      }
   }

   return (true);
}


/**
 * Opens a popup window.
 *
 * @param string  url         URL to open in new window.
 * @param string  title       title of nww window (no spaces!).
 * @param int     width       width of new window.
 * @param int     height      height of new window.
 * @param bool    scrollbars  true to have scrollbars, false to not.
 *
 * @return Window created window.
 */
function openPopup(url,
                   title,
                   width,
                   height,
                   scrollbars)
{
   var x = (640 - width) / 2;
   var y = (480 - height) / 2;

   if (screen)
   {
      y = (screen.availHeight - height) / 2;
      x = (screen.availWidth - width) / 2;
   } // if

   if (screen.availWidth > 1800)
   {
      x = ((screen.availWidth / 2) - width) / 2;
   } // if

   params = 'width='    + width  + ',' +
            'height='   + height + ',' +
            'screenX='  + x      + ',' +
            'screenY='  + y      + ',' +
            'top='      + y      + ',' +
            'left='     + x      + ',' +
            'dependent=yes,'  +
            'directories=no,' +
            'location=no,'    +
            'menubar=no,'     +
            'status=no,'      +
            'toolbar=no';

   if (scrollbars)
   {
      params += ',scrollbars=yes';
   } // if
   else
   {
      params += ',scrollbars=no';
   } // else

   newWin = window.open(url,
                        title,
                        params);

   return (newWin);
}


/**
 * Removes child nodes from an element.
 *
 * @param Element elem  Element to remove child nodes from.
 */
function removeChildren(elem)
{
   while (elem.childNodes.length)
   {
      elem.removeChild(elem.firstChild);
   }
}

function showAns(ans_id,total_ans)
	{         
		var ans;
		for(var i=1;i<=total_ans;i++)
		{
			ans = document.getElementById('ans'+i);
			if(i==ans_id)
				ans.style.display = ans.style.display=='inline'?'none':'inline';
			else
				ans.style.display = 'none';
		}
}

function editPageContent(max3_agent,id)
{
	//window.open('/content/?mod='+module+'&act='+action+'&ident='+ ident,'Content Manager','addressbar=no,menubar=no,scrollbars=yes,resizable =yes');
	window.open(max3_agent+"/m.3/module/Content/action/Edit/id/"+id+"/","ContentManager","addressbar=no,menubar=no,scrollbars=yes,resizable=yes");
}

function hideAllElements(tagName)
{
	var allObjects = document.getElementsByTagName(tagName);
	if(allObjects )
	{
		for(var i=0;i<allObjects .length;i++)
		{
			allObjects[i].style.display = 'none';
			
		}
	}

}
function showAllElements(tagName)
{
	var allObjects = document.getElementsByTagName(tagName);
	if(allObjects )
	{
		for(var i=0;i<allObjects .length;i++)
		{
			allObjects[i].style.display = '';
			
		}
	}

	
}

function setLoadingContentInFrames(frameIds)
{
	if(!frameIds.length)
		return;
	var oDoc,oIframe;
	for(var i=0;i<frameIds.length;i++)
	{
		oIframe = document.getElementById(frameIds[i]);

		oDoc = (oIframe.contentWindow || oIframe.contentDocument);

		oDoc.document.open();
		oDoc.document.write('<b><font style="font-size: 12px">Loading.....</font></b>');
		oDoc.document.close();
	}
}

function unloadContentInFrames(frameIds)
{
	if(!frameIds.length)
		return;
	var oDoc,oIframe;
	for(var i=0;i<frameIds.length;i++)
	{
		oIframe = document.getElementById(frameIds[i]);

		oDoc = (oIframe.contentWindow || oIframe.contentDocument);
		oDoc.document.open();
		oDoc.document.write('<b><font style="font-size: 12px">Content unloaded.</font></b>');
		oDoc.document.close();
	}
}


function setActiveLink(linkNo,totLinks,linkPrefix)
{
	var linkId;
	var link_no = parseInt(linkNo);
	for(var i=1;i<=totLinks;i++)
	{
		linkId = linkPrefix+i+'';
		
		if(i==link_no)
			document.getElementById(linkId).style.fontWeight = 'bold';
		else
			document.getElementById(linkId).style.fontWeight = 'normal';
	}
}
