var tsd = 0;

function view_small_details(obj,st){
  var cd = "";
  if (st == 1){
    obj.style.borderTop = '1px solid blue';
    obj.style.borderBottom = '1px solid blue';    
  }else if (st == 2){
    cd = obj.id.split("/");
    if (cd[1] == '0'){
      location.href="../courses/index.jsp?coursecode="+trim(cd[0]);
    }else{
      location.href="../courses/index.jsp?coursecode="+trim(cd[0])+"&eventcode="+trim(cd[1]);
    }
  }else{
    obj.style.border = 'none';
  }
}

var handleSuccess = function(o){
  
  
  //0:Course Code
  //1:Event Code
  //2:Country
  //3:Location
  //4:Start Date
  //5:Course Title
  
  document.getElementById("quick_bd").innerHTML = "";
  
  var countrylist = trim(o.responseText,"");
  var clist_temp1 = countrylist.split("[");
  var course_multi = "";
  var course_multi = new Array (7);
  var clist_temp2 = "";
  var i = 0;
  var p = 0;
  var current_country = "";
  var last_country = "";
  var item_color = "";
  
  for(i=0;i<clist_temp1.length;i++){
    course_multi[i] = ['','','','','','',''];
  }

  for(i=0;i<clist_temp1.length - 1;i++){
    if (item_color == "#F1F0FF"){
      item_color = "#FFFFDD";        
    }else{
      item_color = "#F1F0FF";        
    }
    clist_temp2 = clist_temp1[i].split(":"); // See list above for definitions
    current_country = clist_temp2[2];
    
    if (current_country != last_country){
      document.getElementById("quick_bd").innerHTML = document.getElementById("quick_bd").innerHTML + "<div class='area'>" + current_country + "</div>";
      document.getElementById("quick_bd").innerHTML = document.getElementById("quick_bd").innerHTML + "<div id='"+clist_temp2[0]+"/"+clist_temp2[1]+"' class='item' style='background-color:"+item_color+"' onmouseover='view_small_details(this,1)' onmouseout='view_small_details(this,0)'  onclick='view_small_details(this,2)'><div style='float:left;width:204px;'>" +  clist_temp2[5] + "</div><div style='float:left;width:123px;'>" + clist_temp2[4] + "</div></div>";
    }else{
      document.getElementById("quick_bd").innerHTML = document.getElementById("quick_bd").innerHTML + "<div id='"+clist_temp2[0]+"/"+clist_temp2[1]+"' class='item' style='background-color:"+item_color+"' onmouseover='view_small_details(this,1)' onmouseout='view_small_details(this,0)'  onclick='view_small_details(this,2)'><div style='float:left;width:204px;'>" +  clist_temp2[5] + "</div><div style='float:left;width:123px;'>" + clist_temp2[4] + "</div></div>";
    }
    last_country = current_country;
  }
};

var handleFailure = function(o){
	alert("Problem with online connection");
};

var callback =
{
  success:handleSuccess,
  failure:handleFailure,
  argument:['foo','bar']
};

function makeRequest(){
  var courseIndex = document.getElementById('qc_courses').selectedIndex;  
  var courseValue = document.getElementById('qc_courses')[courseIndex].value;  
  if (!courseIndex == 0){
    var coursecode = courseValue; //document.getElementById("qc_course").selectedIndex; // GET VALUE BIT
    var sUrl = "get_event_list.jsp";
    var postData = "coursecode="+coursecode;
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
  }
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

