// main.js includes code that is specific to individual pages or actions
startList = function() {
    /* IE just DON'T want to display menus in the right place...
     * This adjusts them.
     * Iterate over all of the elements in 'tabNav' and find all of the 
     * list elements.  For each list element, mark what the offset is,
     * then find the unordered list (UL) in its children. Set the left style
     * to match the offset of the tab */
    if (oldIEBrowser) {
	navRoot = $("tabNav");
	for (i=0; i<navRoot.childNodes.length; i++) {
	    node = navRoot.childNodes[i];
	    if (node.nodeName=="LI") {
		childoffset=node.offsetLeft;

		for (j=0; j<node.childNodes.length; j++) {
		    childnode = node.childNodes[j];
		    if (childnode.nodeName=="UL") {
			childnode.style.left=childoffset-39;
		    }
		}
	    }
	}
    }
}
addEvent(window, 'load', startList);
var menuStates = new Object();
function showMenu(elm){
    if( menuStates['openMenuTabId']==elm.id ){
	return;
    }
    menu = $(elm.id+'-menu');
    if( menu ){
	menu.style.display="block";
	hideMenu(true);

	menuStates['openMenuTabId']=elm.id;
	menuStates['openMenu'] = menu;
	menuStates['timeoutID'] = setTimeout('hideMenu(false)', 3000);
    }
}
function hideMenu(clearTimeout){
    if(menuStates['openMenuTabId']){
	if(clearTimeout){
	    window.clearTimeout(menuStates['timeoutID']);
	}
	new Effect.BlindUp(menuStates['openMenu'],
			   {duration: 0.15});

	menuStates['openMenu']='';
	menuStates['openMenuTabId']='';
    }
}
function lockMenu(menuID){
    if(menuStates['openMenu'].id == menuID){
	window.clearTimeout(menuStates['timeoutID']);
    }
}

function unlockMenu(menuID){
    if(menuStates['openMenu'].id == menuID){
	menuStates['timeoutID'] = setTimeout('hideMenu(false)', 1250);
    }
}

// is called from autocode
var persEngineSelect;
var persUpdating = false;
function show_pers_engine(){
    current = ($("pers-engine").style.display == 'block') ? 'none' : 'block';
    $("pers-engine").style.display = current;
}

function persEngineSetSelect(){
 persEngineSelect = $("pers-list");
 if( persEngineSelect.value == '' ){
  return;
 }
 finalHolder = document.createElement('div');
 finalHolder.setAttribute('id','responseHolder');

 instructions1 = document.createElement('p');
 instructions1.appendChild(
    document.createTextNode(
     'Copy this code into the area to be personalized.'
    )
   );
 finalHolder.appendChild( instructions1);
 answerHolder= document.createElement('div');
 answerHolder.setAttribute('id','pers-code');
 finalHolder.appendChild(answerHolder);

 if(persEngineSelect.value != 'EMAIL'){
  instructions2 = document.createElement('p');
  instructions2.appendChild(
    document.createTextNode(
     'Provide a default in the field below.'
    )
   );
  finalHolder.appendChild( instructions2);

  defaultVal = document.createElement('input');
  defaultVal.setAttribute('type','text');
  defaultVal.setAttribute('name','pers-default');
  defaultVal.setAttribute('id',  'pers-default');
  defaultVal.setAttribute('autocomplete',  'off');
  defaultVal.onfocus = function() { togglePersUpdateMonitor(); }
  defaultVal.onblur = function() { togglePersUpdateMonitor(); }

  finalHolder.appendChild( defaultVal );
 }

 $("pers-engine").replaceChild(finalHolder,persEngineSelect);
 pers_engine_update();
}
function persEngineReset(){
 if(persEngineSelect && $('responseHolder')){
  $("pers-engine").replaceChild(persEngineSelect,$('responseHolder'));
  persEngineSelect.value='';
 }
}

function togglePersUpdateMonitor() {
 persUpdating = persUpdating ? false : true; 
 if(persUpdating){
   setTimeout('persUpdateMonitor()', 1500);
 }
}
function persUpdateMonitor() {
 if( persUpdating ) {
  pers_engine_update();
  setTimeout('persUpdateMonitor()', 800);
 }
}
function pers_engine_update(){
  if( persEngineSelect &&
      persEngineSelect.value ) {
    def_val = $("pers-default");
    code_span = $("pers-code");

    new_val = "%%" + persEngineSelect.value;
    if( def_val && def_val.value && (persEngineSelect.value != 'EMAIL') ) {
      if( def_val.value.match(/%/) != null ) {
	def_val.value = def_val.value.replace(/%/g, '');
        alert("The percent sign ( % ) is not allowed.");
      }
      new_val = new_val + ":" + def_val.value;
    }
    new_val = new_val + "%%";
    code_span.innerHTML = new_val;
  } else {
    code_span.innerHTML = "&nbsp;";
  }
  return false;
}

var attachEngineSelect;
var attachUpdating = false;
function show_attach_engine(){
    current = ($("attach-engine").style.display == 'block') ? 'none' : 'block';
    $("attach-engine").style.display = current;
}
function insertAttachEngineLink(){
  var linkTarget = $('attach-link-target').value;
  var linkType = 'text';

  if( $('attach-link-text') ){  linkType='html';  }

  var attachEngineSelect = $("attach-list");
  if( attachEngineSelect.value == '' ){
      return;
  }
  var finalLink = '%%FILE_LINK:' + attachEngineSelect.value + '%%';

  if( linkType == 'html' ){
      var linkTextElm = $('attach-link-text');
      if(linkTextElm.value ){
	  finalLink = '<a href="'+finalLink+'" title="Download File">'+linkTextElm.value+'</a>';
      } else {
	  return;
      }
  }
  insertAtCursor(linkTarget,finalLink);
}
// listbuilder/profile/index.php
function lb_profile_uncheck(s){
  var display = "D" + s;
  var require = "R" + s;
  if (!(document.survey.elements[display].checked)){	
    display = document.survey.elements[require].checked = 0;
  }
}
function lb_profile_auto_check(s){
  var display = "D" + s;
  var require = "R" + s;
  if (document.survey.elements[require].checked){	
    display = document.survey.elements[display].checked = 1;
  }
}
function lb_profile_show_table(s,obj){
    $("section-"+s).style.display = ($("section-"+s).style.display == 'block') ? 'none' : 'block';

    if( $("section-"+s).style.display == 'none' ){
	obj.src='/images/plus.gif';
    } else {
	obj.src='/images/minus.gif';
    }
}


// join.php
var monitorStates = new Object();
function monitorController(controllerName,currentEvent) {
 if( document.getElementById ){
  if (currentEvent == 'focus'){
    monitorStates[controllerName]=true;
    setTimeout(controllerName+"();", 50);
  } else {
    monitorStates[controllerName]=false;
  }
 }
}

function monitorCompanyName(){
  createCompanyId();
  if( monitorStates['monitorCompanyName'] ){
   setTimeout("monitorCompanyName();", 250);
  }
}

function createCompanyId(){
  var cName = $('companyName').value;
  cName=cName.replace(/ /g, '_');
  cName=cName.replace(/[^\w-]/g, '');
  $('companyId').value = cName;
  toggleElement('companyIdError', 'off');
}

function monitorCompanyId(){
  var cId = $('companyId').value;
  var invalidPosition=cId.search(/[^\w-]/g);
  if( invalidPosition == -1 ){
    toggleElement('companyIdError', 'off');
  } else {
    toggleElement('companyIdError', 'on');
  }
  if( monitorStates['monitorCompanyId'] ){
   setTimeout("monitorCompanyId();", 100);
  }
}

function clickForInfo(field,initialLink,postLink) {
  infoNode=field.nextSibling.nextSibling;
  if(infoNode.clickForInfo)
      return;
  
  infoNode.clickForInfo=true;

  if( infoNode.style.display=='none' ){
    new Effect.Parallel(
	[new Effect.BlindDown(infoNode,{sync: true}),
	 new Effect.Appear(infoNode,{sync: true})],
	{fps: 40, duration: 0.6, afterFinish: function(){
		field.firstChild.data = postLink? postLink : 'Close Info';
		infoNode.clickForInfo=false;
	    } 
	});
  } else {
    new Effect.Parallel(
	[new Effect.BlindUp(infoNode,{sync: true}),
	 new Effect.Fade(infoNode,{sync: true})],
	{fps: 40, duration: 0.6, afterFinish: function(){
		field.firstChild.data = initialLink ? initialLink : 'More Info';
		infoNode.clickForInfo=false;
	    } 
	});
  }
}
function effectComplete(el){
    el.effectActive=false;
}
