
////////////////////////////////////////////////////////////////////////
// these methods expand and fold up the Advanced sections on various
// pages
////////////////////////////////////////////////////////////////////////
//changed to display for IE fix
function showAdvancedOptions()
{
  $('showAdvanced').style.display = '';
  $('hideAdvanced').style.display = 'none';
}
  
function hideAdvancedOptions()
{
  $('hideAdvanced').style.display = '';
  $('showAdvanced').style.display = 'none';
}

function showAdvancedOptions2()
{
  $('showAdvanced2').style.display = '';
  $('hideAdvanced2').style.display = 'none';
}

function hideAdvancedOptions2()
{
  $('hideAdvanced2').style.display = '';
  $('showAdvanced2').style.display = 'none';
}

///////////////////////////////////////////////////////////////////////
// Suckerfish menu for top nav
// This code is purely to workaround IE
///////////////////////////////////////////////////////////////////////
startlist = function() {
    if(document.getElementById("nav")) {
        var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
                this.className+=" over";
                if(document.getElementById("menu_iframe")) {
                    var iframe = document.getElementById("menu_iframe");

                    var submenu = this.getElementsByTagName("UL");
                    if(submenu.length) {
                        iframe.style.top = submenu[0].offsetTop + this.offsetTop;
                        iframe.style.left = submenu[0].offsetLeft + this.offsetLeft;
                        iframe.style.width = submenu[0].scrollWidth;
                        iframe.style.height = submenu[0].clientHeight;
                        iframe.style.display = "inline";
                    }
                }
            }
            sfEls[i].onmouseout=function() {
                this.className=this.className.replace(new RegExp(" over\\b"), "");
                if(document.getElementById("menu_iframe")) {
                    var iframe = document.getElementById("menu_iframe");
                    iframe.style.display = "none";
                }
            }
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", startlist);

////////////////////////////////////////////////////////////////////////
// popup function for directory and switchboard links
////////////////////////////////////////////////////////////////////////

function directoryPopUp(url)
{
	return popUp(url,'directory',350,350);
}

function helpPopUp(url,customWidth,customHeight)
{
	if(!customWidth)
	{
		customWidth = 450;
	}
	if(!customHeight)
	{
		customHeight = 450;
	}
	popUp(url,'help',customWidth,customHeight);
}

function popUp(url,inName,width,height)
{
	inName = inName.replace(/ /g, "_"); /* For stupid pos IE */
	var popup = window.open('',inName,'width='+width+',height='+height+',toolbars=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,left=200,top=200');

	// only reload the page if it contains a new url
	if (popup.closed || !popup.document.URL || (-1 == popup.document.URL.indexOf(url))) 
	{
		popup.location = url;
	}	
	popup.focus();
	return popup;
}

function swapImage(from,to)
{
	if(document.images) 
	{
		document.images[from].src = pics[to].src;
	}
}

/**********************************************************************
* Help function for converting XML data to a javascript array
***********************************************************************/
function xmlData2Array(xmlNode,dataArray)
{
    if(!xmlNode[0])
    {
        return;
    }
    if(xmlNode.length != 0)
    {
        for(var i = 0; i < xmlNode.length; i++)
        {
			printfire(xmlNode);
			if(xmlNode[i].firstChild)
			{
            	dataArray.push(xmlNode[i].firstChild.nodeValue);
			}
        }
    }
    else
    {
        dataArray.push(xmlNode[0].firstChild.nodeValue);
    }
    return;
}

/**********************************************************************
* Help function for converting XML data to a javascript array
***********************************************************************/
function xmlTag2Array(xml,tagname)
{
	var xmlNode = xml.getElementsByTagName(tagname);

	var dataArray = [];
    if(!xmlNode[0])
    {
        return [];
    }
    if(xmlNode.length != 0)
    {
        for(var i = 0; i < xmlNode.length; i++)
        {
            printfire(xmlNode);
            if(xmlNode[i].firstChild)
            {
                dataArray.push(xmlNode[i].firstChild.nodeValue);
            }
        }
    }
    else
    {
        dataArray.push(xmlNode[0].firstChild.nodeValue);
    }
    return dataArray;
}


/**********************************************************************
* Generic toggle function to disable/enable a group of elements
***********************************************************************/
function toggleElements(params)
{
  var radio = $(params.radioName);
  var disableOnCheck = params.disableOnCheck;
  var enableOnCheck = params.enableOnCheck;
  var oldClassNames = params.oldClassNames;

  if(radio.checked)
  {
	disableOnCheck.map(function(n){
		var e = $(n);
		if($(n+'disabled'))
		{
			e.style.display = 'none';
			$(n+'disabled').style.display = '';
		}
		else
		{
			e.className = oldClassNames[n] + 'disabled';
			e.disabled  = true;
		}
	});
	enableOnCheck.map(function(n){
		var e = $(n);
		e.className = oldClassNames[n];
		e.disabled  = false;
	});
  }
  else
  {
	disableOnCheck.map(function(n){
		var e = $(n);
		if($(n+'disabled'))
		{
			e.style.display = '';
			$(n+'disabled').style.display = 'none';
		}
		else
		{
			e.className = oldClassNames[n];
			e.disabled  = false;
		}
	});
	enableOnCheck.map(function(n){
        var e = $(n);
		e.className = oldClassNames[n] + 'disabled';
		e.disabled  = true;
	});
  }
  return;
}

function cacheOldClassNames(ids)
{
  var oldClassNames = [];
  ids.each(function(e){
	oldClassNames[e] = $(e).className;
  });
  return oldClassNames;
}

function setupToggle(params)
{
  var tglArrays = params.toggleElements;
  var oldClassNames = [];

  for (i=0;i<tglArrays.length;i++)
  {
	tglArrays[i].map(function(n) { printfire(n); oldClassNames[n] = $(n).className; });
  }
  return oldClassNames;
}

function getNodeValue(xml,tag)
{
  if(!xml)
    return null;
  var filename_node = xml.getElementsByTagName(tag)[0];
  if(!filename_node || !filename_node.firstChild)
	return null;
  return filename_node.firstChild.nodeValue;
}

//debug printer for firefox
function printfire(msg)
{
  debug(msg);
}

//new debuggin methods for firebug
//simple log
function debug(msg)
{
  if(typeof console == 'undefined')
    return;
  console.log(msg);
}
//dump flat object
function dump(e)
{
  if(typeof console == 'undefined')
    return;
  console.log(Object.inspect(e));
}
//dump interactive object
function dumpObj(e)
{
  if(typeof console == 'undefined')
    return;
  console.log('%1.o',e);
}


/* moved function into common because of IE bug not accessing
   select boxes across windows */
function addToSelect(selectId,groupName,groupID,makeSelected)
{
  var selectBox = $(selectId);
  if(!selectBox)
  {
    alert("Error adding your new group to your selection. Perhaps you closed the page?");
    return 1;
  }

  var empty = 0;
  for (i=0;i<selectBox.options.length;i++)
  {
    if(selectBox.options[i].value == -1)
    {
      empty = 1;
    }
  }

  var arrayPosition = selectBox.length;
  if(empty == 1)
  {
    arrayPosition = 0;
  }

  var newoption = new Option(groupName,groupID);


  //trying putting these on different pages don't select the box from the openeer
  //firefox,IE fix
  try {
  	selectBox.options[arrayPosition] = newoption;
  }
  catch(e)
  {
	alert(e);
  }

  if(makeSelected == 1 )
  {
    selectBox.selectedIndex = arrayPosition;
  }
  selectBox.disabled = false;
  return;
}

function getDisplayDate()
{
  var now = new Date();
  return now.getMonth() + "/" + now.getDate() + "/" + now.getFullYear();
}

function getDisplayTime()
{
  var now = new Date();
  var hour = now.getHours();
  var minute = now.getMinutes();
  var second = now.getSeconds();

  var ap = "AM";
  if (hour > 11) { ap = "PM"; }
  if (hour > 12) { hour = hour - 12; }
  else if (hour == 0) { hour = 12; }
  if (minute < 10) { minute = "0" + minute; }
  if (second < 10) { second = "0" + second; }
  return hour + ':' + minute + ':' + second + ' ' + ap;
}

function getWindowWidth()
{
    var winWidth = 0;
    if( typeof( window.innerWidth ) == 'number' )
    {
        winWidth = window.innerWidth;
    }
    //IE 6+
    else if(document.documentElement &&
           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
        winWidth = document.documentElement.clientWidth;
    }
    //IE 5
    else if ( document.body &&
            ( document.body.clientWidth || document.body.clientHeight ) )
    {
        winWidth = document.body.clientWidth;
    }
  return winWidth;
}


function getWindowHeight()
{
    var winHeight = 0;
    if( typeof( window.innerHeight ) == 'number' )
    {
        winHeight = window.innerHeight;
    }
    //IE 6+
    else if(document.documentElement &&
           ( document.documentElement.clientHeight || document.documentElement.clientHeight ) )
    {
        winHeight = document.documentElement.clientHeight;
    }
    //IE 4
    else if ( document.body &&
            ( document.body.clientHeight || document.body.clientHeight ) )
    {
        winHeight = document.body.clientHeight;
    }
  return winHeight;
}

function getWindowDimensions()
{
    var w = getWindowWidth();
    var h = getWindowHeight();
    return { width : w, height : h};
}

function $HOV(element) 
{
  return Element.addClassName(element,$A(Element.classNames(element)).first() + '_hover');
}

function $UHOV(element) 
{
  return Element.removeClassName(element,$A(Element.classNames(element)).first() + '_hover');
}

function durationize(val)
{
  var sec = val % 60;
  var min = parseInt(val / 60);
  hr  = parseInt(min / 60);
  min = min % 60;
  return ((hr) ? hr + ' hours, ' : '') + ((min) ? min + ' mins, ' : '') + sec + ' secs';
}

function epoch2time(val)
{
	var theDate = new Date(val*1000);
	var hours =  theDate.getHours();
	var ampm = (hours >= 12) ? 'pm' : 'am';
	var minutes = theDate.getMinutes();
	if (minutes < 10)
		minutes = "0" + minutes;
	if (hours > 12)
		hours = hours - 12;
	return hours + ":" + minutes + " " + ampm;
}


/**********************************************
* A new createElement function that takes a 2nd
* arg as the parent to append too
***********************************************/
function createElement (type, class_name, parent)
{
    var el = document.createElement(type);
    if(typeof class_name != 'undefined')
    {
        el.className = class_name;
        el.origClassName = class_name;
    }
    if (typeof parent != 'undefined')
        parent.appendChild(el);
    return el;
};


function createInputElement(type,name,value,class_name,parent)
{
  //IE fix
  try {
    var el = createElement('<input type="'+type+'"  name="'+name+'"  />',class_name,parent);
  }
  catch(e) //ff only
  {
    var el    = createElement('input',class_name,parent);
    el.setAttribute('type',type);
  }
  el.value = value;
  el.name = name;
  return el;
}

/**********************************************************************
* Create mouseover popups
***********************************************************************/
function SVMAIN_create_tooltip(element,title,txt,delay,size)
{
    if(!size)
        size = 200;
    if(!delay)
        delay = 0;

    //--add the mouseover for away note
    element.T_TITLE = title;
    element.T_SHADOWWIDTH = 2;
    element.T_WIDTH = size;

    var uid = Math.random()*4000;
    var htm = tt_Htm(element,"SVMAIN_tool_tip_"+uid,txt,uid);

    var overDiv = createElement('div','',document.getElementsByTagName("body")[0]);
    overDiv.innerHTML = htm;
    var toolTipFunc = new Function('e','tt_Show(e,'+'"SVMAIN_tool_tip_'+uid+'"'+',0,'+delay+',"",0,'+ttOffsetX+','+ttOffsetY+',0,0,'+ttTemp+');');

    //T_ABOVE,T_DELAY,T_FIX,T_LEFT,T_OFFSETX,T_OFFSETY,T_STATIC,T_STICKY,T_TEMP
    Event.observe(element,'mouseover',toolTipFunc);
    Event.observe(element,'mouseout',tt_Hide);

    return { element: element, mouseover:toolTipFunc, mouseout:tt_Hide, overDiv:overDiv, uid : uid};
}

function SVMAIN_create_DOM_tooltip(element,title,dom_element,delay,size)
{
    if(!size)
        size = 200;
    if(!delay)
        delay = 0;

    //--add the mouseover for away note
    element.T_TITLE = title;
    element.T_SHADOWWIDTH = 2;
    element.T_WIDTH = size;

    var uid = Math.random()*4000;
    var htm = tt_Htm(element,"SVMAIN_tool_tip_"+uid,'',uid);

    var overDiv = createElement('div','',document.getElementsByTagName("body")[0]);
    overDiv.innerHTML = htm;

	$("SVMAIN_tooltiptd_" + uid).appendChild(dom_element);
    var toolTipFunc = new Function('e','tt_Show(e,'+'"SVMAIN_tool_tip_'+uid+'"'+',0,'+delay+',"",0,'+ttOffsetX+','+ttOffsetY+',0,0,'+ttTemp+');');

    //T_ABOVE,T_DELAY,T_FIX,T_LEFT,T_OFFSETX,T_OFFSETY,T_STATIC,T_STICKY,T_TEMP
    Event.observe(element,'mouseover',toolTipFunc);
    Event.observe(element,'mouseout',tt_Hide);

    return { element: element, mouseover:toolTipFunc, mouseout:tt_Hide, overDiv:overDiv, uid : uid};
}

var toggleStore = [];
function genericToggleDisplay( params )
{
    var type 	= params.type;
	var key		= ( params.key ) ? params.key : params.type;
	var disable = params.disable;
    var val  	= $F(params.select_box);

	if(typeof params.bad != 'undefined')
	{
		if(val == params.bad)
		{
			dump("Converting val from " + params.bad + " to " + params.good);
			val = params.good;
		}
	}

    if(toggleStore[key])
        toggleStore[key].style.display = 'none';

    dump("TYPE:" + type + " vs VAL:" + val);
    if( !$(type+val) )
        return 1;

    $(type+val).style.display = '';
    toggleStore[key] = $(type+val);


	//disables
	if(toggleStore['disable_'+key])
	{
		toggleStore['disable_'+key].disabled = true;
	}
	if(! $(disable+val) )
		return 1;

	$(disable+val).disabled = false;
	toggleStore['disable_' + key] = $(disable+val);

    return;
}

//Not used yet....
function addWindowOnLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
	{
      if (oldonload) {
        oldonload();
	  }
      func();
    }
  }
}

//-- Used in IVR actions like (new_actions/play_digits.html, etc)
function disableSelectOnKeyPress(textbox_id,selectbox_id)
{
  //-selectbox not around
  if(!$(selectbox_id))
	return;

  //-textbox empty filled then disable the selectbox
  if($F(textbox_id) != '')
	$(selectbox_id).disabled = true;
  else
 	$(selectbox_id).disabled = false;
}


function show_result(string,item_id,success)
{
	var item = $('result_popup');
	$('new_result_message').innerHTML = string;

	var temp = $(item_id);
	var yoffset = 0;
	while ( temp != null)     
	{
		yoffset += temp.offsetTop;
		temp = temp.offsetParent;
	}

	var winWidth = getWindowWidth();

	item.style.display = '';
	item.style.top = (yoffset - item.offsetHeight - 10 ) + 'px';
	item.style.left = ( (winWidth/2) - parseInt(parseInt(item.style.width)/2) ) + 'px';

	// make the iframe visible and move it under the div
	var fif = $('iframe_result_popup');
	fif.style.top = item.style.top;
	fif.style.left = item.style.left;
	fif.style.width = item.style.width;
	fif.style.height = item.offsetHeight + "px";
	fif.style.visibility = "visible";
	fif.style.display = '';

//	$('cd_circle').src = countdown_circle.src;
	$('close_href').style.display = '';
	if(Math.random && Math.round)
	{
		runID = Math.round(Math.random()*4000);
	}
	if(success)
	{
		$('close_href').innerHTML = 'Closing...';
		temp = setTimeout('fadeResultWindow(' +  runID + ')',2000);
	}
	else
	{
		$('close').style.display = '';
		$('close_href').innerHTML = 'Close';
	}
	return;
}

function isDefined(obj, variable)
{
    if(variable)
    {
        return (obj != null) ? (typeof(eval(obj)[variable]) != 'undefined') : false;
    }
    else
    {
        return (typeof(obj) != 'undefined');
    }
}
