var cPREFIX= 'bsubs_', cTABLE_SECTIONS= new Array('thead', 'tbody', 'tfoot'),
  cRT_RECAPTCHA_KEY= '--- need to get this from the RECAPTCHA site ---',
  cRECAPTCHA_THEME= 'red';

function getElementsByClass( pClass, pNode ) {
 if ( !pNode ) pNode= document.getElementsByTagName('body')[0];
 var r= [], aElt= pNode.getElementsByTagName('*'), i, Li= aElt.length,
   re= new RegExp('\\b'+pClass+'\\b');
 for ( i= 0; i< Li; i++ ) if ( re.test(aElt[i].className) ) r.push(aElt[i]);
 return r;
}

function centerElement( pElt ) { var x, y;
  x= ( screen.availWidth?screen.availWidth:window.innerWidth)/2;
  y= ( screen.availHeight?screen.availHeight:window.innerHeight)/2;
  with ( pElt ) { style.display= 'block'; style.left= (x-offsetWidth/2)+'px'; style.top= (y-offsetHeight/2)+'px'; }
}

function myGetClass( pElt ) { return pElt.className; }

function mySetClass( pElt, pClass ) { pElt.className= pClass; }

function formValidate() {
  var i, msg = '', r;
  for ( i=0; i < document.KIFORM.elements.length; i++)  {
    if ( document.KIFORM.elements[i].getAttribute('KIV') )  {
       r= eval(document.KIFORM.elements[i].getAttribute('KIV'));
       if ( r ) { msg+= r + '\n'; }
    }
  }
  if ( msg!= '' ) { alert(msg); }
  return (msg== '');
}

function DoSubmit() { var parmAction= arguments[0], parmValidate;
  parmValidate= ( arguments.length== 2?arguments[1]:'Y' );
  if ( (parmValidate== 'N') || formValidate() ) {
    //document.getElementById(cPREFIX+'captchaContainer').style.display= 'none';
    centerElement(document.getElementById(cPREFIX+'pageLoading'));
    document.KIFORM.action.value= parmAction;
    document.KIFORM.submit();
  }
}

function myCancelEvent( pEvt ) { 
 pEvt.cancel= true; pEvt.cancelBubble= true; pEvt.returnValue= false;
 if ( pEvt.stopPropagation ) pEvt.stopPropagation();
 if ( pEvt.preventDefault ) pEvt.preventDefault();
 return false;
}

function enterSubmit( e ) { var a, c, myAction, myID, prefix= cPREFIX+'action_', r= false;
  if ( e && e.which ) { c= e.which; }
  else if ( typeof(event)!= 'undefined' ) { e= event; c= e.keyCode; }
  if ( c== 13 ) { myCancelEvent(e);
    if ( myFormFocus ) { myID= myFormFocus.id;
      if ( myID.substr(0, prefix.length)== prefix ) {
        a= myID.split('_'); myAction= a.slice(-1);
        DoSubmit(myAction, 'N');
      }
    }
  }
}

function setBrowserClass()
{
  var 
    a0= navigator.userAgent.toLowerCase(),
    is= function(t) { return a0.indexOf(t) != -1; },
    h= document.getElementsByTagName('html')[0],
    a1= (!(/opera|webtv/i.test(a0))&&/msie (\d)/.test(a0))?('ie ie'+RegExp.$1)
      :is('gecko/')? 'gecko'
      :is('opera/9')?'opera opera9'
      :/opera (\d)/.test(a0)?'opera opera'+RegExp.$1
      :is('konqueror')?'konqueror'
      :is('applewebkit/')?'webkit safari'
      :is('mozilla/')?'gecko'
      :'',
    e= (is('x11')||is('linux'))?' linux'
      :is('mac')?' mac'
      :is('win')?' win':'',
    c= a1+e+' js',
    oldClass= myGetClass(h);
  mySetClass(h, ( oldClass.length== 0?c:oldClass+' '+c ));
  return c;
}

function buildSurvey() { 
  var eltSurvey= document.getElementById(cPREFIX+'survey'), i;
  if ( eltSurvey ) { 
    aEltInput= eltSurvey.getElementsByTagName('input');
    for ( i= 0; i< aEltInput.length; i++ ) { elt= aEltInput[i];
      if ( (t= elt.getAttribute('type')) && (t== 'radio') ) {
        elt.onchange= function(){ DoSubmit('pollVoted', false); };
      }
    }
  }
}

function buildLists() { 
  var aeLIs, aeULs= document.getElementsByTagName('UL'), c= '', h, i, j, k, isNav, s;
  if ( aeULs ) {
    for ( i= 0; i< aeULs.length; i++ ) { c= myGetClass(aeULs[i]).toLowerCase();
      isNav= ( 	c.indexOf('nav')>= 0); 
      if ( !aeULs[i].className || isNav || (c.indexOf('articlelist')== -1) ) {
        s= document.getElementById(cPREFIX+'constant'+( isNav?'DelimiterNav':'BulletList' )).innerHTML;
        aeLIs= aeULs[i].getElementsByTagName('li');
        if ( aeLIs ) { k= aeLIs.length;
          for ( j= 0; j< k; j++ ) { h= aeLIs[j].innerHTML;
            if ( isNav && (h.substr(h.length-s.length)!= s) ) { if ( j< (k-1) ) h= h+s; }
            else if ( h.substr(0, s.length)!= s ) { h= s+h; }
            aeLIs[j].innerHTML= h;
          }
        }
      }
    }
  }
}

function showCaptcha( pPostAction ) { document.KIFORM.kirt_captchaAction.value= pPostAction;
  centerElement(document.getElementById(cPREFIX+'captchaContainer'));
}

function paintRating( pRating ) { var a;
  for ( var i= 1; i<= 5; i++ ) { a= document.getElementById(cPREFIX+'rating'+i).getElementsByTagName('img');
    a[0].src= '/rt_media/icons/iconRating'+( i<= pRating?'ON':'OFF' )+'.gif';
  }
}

function loadSubconsole( s ) {
  if ( document.getElementById(cPREFIX+'subconsole') ) {
    document.getElementById(cPREFIX+'subconsole').innerHTML=
      document.getElementById(cPREFIX+s).innerHTML;
    document.getElementById(cPREFIX+'subconsole').style.display= 'block';
  }
}

function showVideo ( pURL ) { window.open(pURL,'rtVideo', ''); return false; }

function addListeners() { var aElements, aNames= new Array('input', 'select'), i, j;
  for ( i= 0; i< aNames.length; i++ ) {
    aElements= document.KIFORM.getElementsByTagName(aNames[i]);
    for ( j= 0; j< aElements.length; j++ )
      aElements[j].onfocus= function () { myFormFocus= this; }
  }
}

function s2b(e){ var s, t;
  e= e || window.event; t= e.target || e.srcElement;
  var shiptoRegex=/_SHIPTO_/g; s= t.name;
  if ( shiptoRegex.test(s) ) { s= s.replace(shiptoRegex, '_BILLTO_');
    s= 'document.KIFORM.'+s+'.';
    switch (t.type) {
      case 'select-one':
        eval(s+'selectedIndex='+t.selectedIndex+';');
        break;
      case 'checkbox':
        eval(s+'checked='+t.checked+';');
        break;
      default:
        eval(s+'value=t.value;');
        break;
    }
  }
}

function postProcessLinks() { var a, h, i, j, L, p, s0, s1;  
  if ( document.getElementsByTagName ) { a= document.getElementsByTagName('a');
    p= document.forms[0].elements['basePath'].value;
    L= window.location.pathname.split('?')[0].toLowerCase();
    for ( i= 0; i< a.length; i++ )  
      with ( a[i] ) 
        if ( getAttribute('href') ) { h= getAttribute('href'); 
          switch ( getAttribute('rel') ) {
            case 'external': setAttribute('target', '_blank'); break;
            case 'internal':
              if ( p+h.toLowerCase()== L ) 
                setAttribute('class', (( getAttribute('class')
                ?getAttribute('class'):'' )+' bsubs_selected')
                .replace(/^ +/,''));
              s0= getAttribute('href');
              s1= '?C='+document.forms[0].elements['CID'].value;
              j= s0.indexOf('#');
              if ( j== -1 ) s0= s0+s1;
              else s0= s0.substr(0, j)+s1+s0.substr(j);
              setAttribute('href', s0); break;
            }
          }
  } 
} 

/*2011tbg pulled from consulting site*/
function classHilite( pElt, pState ) { 
  var c= '', g= pElt.id, i, j, rx, aTables;
  pElt.style.cursor= ( pState?'pointer':'default' ); 
  if ( g.length> 0 ) { aTables= document.getElementsByTagName('table');
    rx= /[0-9]+$/g; g= g.replace(rx, '');
    c= (pState?'darkover':'undarkover' );
    for ( i= 0; i< aTables.length; i++ ) {
      aRows= aTables[i].getElementsByTagName('tr');
      for ( j= 0; j< aRows.length; j++ ) {
        if ( (aRows[j].id.length> g.length) 
          && (aRows[j].id.substr(0, g.length)== g) )
          aRows[j].className= c+( aRows[j]== pElt?'Specific':'Default' );
      }
    }
  }
}


function myOnLoad() { 
  postProcessLinks(); processLoadDirectives(); buildLists(); addListeners(); buildSurvey();
  //document.getElementById(cPREFIX+'rhc').style.display= 'block';
  if ( self.slideshowSetup ) slideshowSetup();
  if ( self.rotatorSetup ) rotatorSetup();
}

// main
var s= setBrowserClass().indexOf('ie ')!= -1?'IE':'nonIE', myFormFocus;


