var loaded = false;
var xmlhttp = false;
var bc_bgcolor = '';
var doNotChangeBG = false;

function readCookie(cookieName)
{
 var theCookie= '' + document.cookie;
 var ind = theCookie.indexOf(cookieName);

   if (ind==-1 || cookieName=='') return ''; 
   var ind1=theCookie.indexOf(';',ind);
   if (ind1==-1) ind1=theCookie.length; 
   return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function leadingZeroes(i, zeros)
{
 var str = '';
 
   str += i;
   while(str.length < zeros) {
      str = '0' + str;
   }
   
   return str;
}

function checkLogin()
{
 var bc;

   if ( loaded ) {
      return;
   }
   loaded = true;

   try { // Moz supports XMLHttpRequest. IE uses ActiveX.
         // browser detction is bad. object detection works for any browser
      xmlhttp = window.XMLHttpRequest ?
         new XMLHttpRequest():
         new ActiveXObject('Microsoft.XMLHTTP');
   }
   catch (e) { // browser does not support ajax. handle however you want
      alert('[1] Your browser does not support XmlHttpRequest!');
   }
   // the xmlhttp object triggers an event everytime the status changes
   //
   // triggered() function handles the events
   xmlhttp.onreadystatechange = triggered;
   // open takes in the HTTP method and url.
   try {
      xmlhttp.open('GET', '/cgi-bin/mrk/login/'+readCookie('_data'));
   }
   catch (e) {
      alert('[2]' + e);
   }
   // send the request. if this is a POST request we would have
   // sent post variables: send('name=aleem&gender=male')
   // Moz is fine with just send(); but  IE expects a value here, hence
   xmlhttp.send(null);
}

function triggered()
{
 var loginDiv;
 
   loginDiv = document.getElementById('login_dlg');
   if (undefined == loginDiv || null == loginDiv) {
      return;
   }

   // if the readyState code is 4 (Completed)
   // and http status is 200 (OK) we go ahead and get the responseText
   // other readyState codes:
   // 0=Uninitialised 1=Loading 2=Loaded
   // 3=Interactive
   if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200) && loginDiv) {
      // xmlhttp.responseText object contains the response.
      loginDiv.innerHTML = xmlhttp.responseText;
   }

   try {
    var _corpId = readCookie('_corp');
    var corpId = parseInt(_corpId, 10);

      if (corpLogo == 1 && corpId > 0) {
       var corpImg;
       var imageName;

         corpImg = document.getElementById('sponsoredlink');
         if(null != corpImg) {
            imageName = 'AaIcorp_' + leadingZeroes(corpId, 4) + '_logo.jpg';
            corpImg.src = "/images/"+imageName;
         }
      }
   }
   catch(err) {
      alert('3 ' + err.message);
   }

   if (false) { 
//   if (!doNotChangeBG) { 
      bc = document.getElementById('body_content');
      if ( bc ) {
         bc.style.background = '#F0EBD7';
      }
      bc = document.getElementById('copyright');
      if ( bc ) {
         bc.style.background = '#F0EBD7';
      }
   }
}

function param( name )
{
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]"+name+"=([^&#]*)";
 var regex = new RegExp( regexS );
 var results = regex.exec( window.location.href );

   return results == null ? "" : results[1];
}

function startup()
{
   checkLogin(); 
}
