// JavaScript Document to load up the mini search results on the left of the details page.
var eventHolder = new Object;
eventHolder['run'] = null;

function addLoader( func )
{

    if ( eventHolder['run'] && eventHolder['run'] != null )
	{
	    var oldRun = eventHolder['run'];
	    eventHolder['run'] = function(e) { oldRun(e); func(e) }
	}
    else
	{
	    eventHolder['run'] = function(e) { func(e) }
	}

}

function addEvent(oTarget, sType, fpDest) {
  sType = 'on'+sType;
  var oOldEvent = oTarget[sType];
  if (typeof oOldEvent != "function") {
    oTarget[sType] = fpDest;
  } else {
    oTarget[sType] = function(e) {
      oOldEvent(e);
      fpDest(e);
    }
  }
}

function getSearchResultsOnDetailsPage ()
{


    if ( window.ActiveXObject ) // if it's ie
    {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // xmlhttp is global
       xmlhttp.onreadystatechange = function ()
       {
          if(xmlhttp.readyState == 4) writercontent(xmlhttp.responseText);
       };
    }
    else
    {
       if (window.XMLHttpRequest) // if it's firefox etc.
       {
         xmlhttp        = new XMLHttpRequest() // xmlhttp is global
         xmlhttp.onload = function () { writercontent(xmlhttp.responseText) };
       }
       else return; //
    }

	var courseid = document.mainform.course_id.value;
	var searchparams = document.mainform.searchparams.value;
	if(searchparams != "")
	{
      if(searchparams.match(/course_search.cgi?/))
	  {
	   	   var url =  'http://' + document.location.host + '/' + searchparams + "&mini=1" + "&course_id=" + courseid;

	  }
	  else
	  {
	   var url =  'http://' + document.location.host + '/course_search.cgi?' + searchparams + "&mini=1" + "&course_id=" + courseid;

	   }


         xmlhttp.open("GET", url , true);
         xmlhttp.send(null);
	}
}

function writercontent (content)
{
		 document.getElementById('searchResultsOnDetailsPage').innerHTML = content;
		 write_next_page_link ();
		 write_prev_page_link ();
}

function transfercookieNext()
{

  delCookie('searchLearnPrev');
  delCookie('searchLearn');
  delCookie('thiscourseseqno');

  var value = document.minimainform.next_course_count.value;
  var ExpireDate = new Date ();
   var tmp = new Date();
   ExpireDate.setTime(ExpireDate.getTime() + 600000);
   var curCookie = "thiscourseseqno=" + value + "; expires=" + ExpireDate.toGMTString() + "; path=/course/";
   document.cookie = curCookie;
}

function transfercookiePrev()
{
  delCookie('searchLearn');
  delCookie('searchLearnNext');
  delCookie('thiscourseseqno');

  var value = document.minimainform.prev_course_count.value;
  var ExpireDate = new Date ();
  var tmp = new Date();
  ExpireDate.setTime(ExpireDate.getTime() + 600000);
  var curCookie = "thiscourseseqno=" + value + "; expires=" + ExpireDate.toGMTString() + "; path=/course/";
  document.cookie = curCookie;
}


function write_next_page_link ()
{
  var shownexturl;
  var nexturl;

  if(document.minimainform && document.minimainform.next_course_url)
  {
    if(document.minimainform.next_course_url)
	{
     nexturl = document.minimainform.next_course_url.value;
    }
  }

  if(document.minimainform && document.minimainform.show_next_details_url);
  {
   if(document.minimainform.show_next_details_url)
   {
     shownexturl = document.minimainform.show_next_details_url.value;
   }
  }

  if(shownexturl == 1)
  {
   var imgs = getElementsByClass('nextimg');
   for(i=0; i < imgs.length; i++)
   {
    imgs[i].style.display =  'inline';
   }

   var targs = getElementsByClass('next_course');
   for(i=0; i < targs.length; i++)
   {
     targs[i].style.visibility =  'visible';
     var link = document.createElement( 'A' );
     targs[i].appendChild( link );
     link.href = nexturl;
     link.appendChild( document.createTextNode("next") );
     addEvent( link, 'click', transfercookieNext );
   }


  }
}

function write_prev_page_link ()
{
  var prevurl;
  var showprevurl;

  if(document.minimainform && document.minimainform.prev_course_url)
  {
    prevurl = document.minimainform.prev_course_url.value;
  }

  if(document.minimainform && document.minimainform.show_prev_details_url)
  {
    showprevurl = document.minimainform.show_prev_details_url.value;
  }

  if(showprevurl == 1)
  {
   var imgs = getElementsByClass('previmg');
   for(i=0; i < imgs.length; i++)
   {
    imgs[i].style.display =  'inline';
   }

   var targs = getElementsByClass('prev_course');
   for(i=0; i < targs.length; i++)
   {
    targs[i].style.visibility =  'visible';
	var link = document.createElement( 'A' );
    targs[i].appendChild( link );
    link.href = prevurl;
    link.appendChild( document.createTextNode("previous") );
    addEvent( link, 'click', transfercookiePrev );
   }


  }
}

function loadfunctions()
{
  getSearchResultsOnDetailsPage();
  setMyDetails();
}

addLoader( loadfunctions  );
addEvent( window, 'load', loadfunctions  );
