var xmlhttp

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	 try {
	   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch(e) {
	   try {
	     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	   } catch(e) {
	     xmlhttp = false;
	   }
	 }
  @else
     xmlhttp = false;
  @end @*/
  
if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined') {
  try {
	xmlhttp = new XMLHttpRequest();
  } catch (e) {
	xmlhttp = false;
  }
}

function myXMLHttpRequest() {
  var xmlhttplocal;
  
  try {
	xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
  } catch (e) {
    try {
	  xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	} catch (E) {
	  xmlhttplocal=false;
	}
  }

  if (!xmlhttplocal && typeof(XMLHttpRequest) != 'undefined') {
	try {
	  var xmlhttplocal = new XMLHttpRequest();
    } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
    }
  }
  return(xmlhttplocal);
}

function sendRequestCB(id,action) {
  if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
	str = 'clipboard_process.php?action='+action+'&cb_id='+id;
	xmlhttp.open("GET",str,true);
	xmlhttp.onreadystatechange = handleResponseCB; 
	xmlhttp.send(null);
  }
  return false;
}

function handleResponseCB() {
  if (xmlhttp.readyState == 4) {
	input = xmlhttp.responseText.split(',');
	id = input[0];
	action = input[1];
	btn = document.getElementById('cb'+id+'_btn');
    
	if (input[2] == 1)
	  result = true;
	else
	  result = false;

	switch (action) {
	  case 'add':
	    if (result) btn.innerHTML = "<a class=\"action\" href=\"javascript:void(0);\" onClick=\"sendRequestCB('" + id + "','remove')\">- Clip</a>";
	    break;
	  
	  case 'remove':
	    if (result) btn.innerHTML = "<a class=\"action\" href=\"javascript:void(0);\" onClick=\"sendRequestCB('" + id + "','add')\">+ Clip</a>";
	    break;
	}
    document.getElementById('cb_size').innerHTML = input[3];
  }
}

function sendRequestCP(form_name,fld_name,file_name,type) {
  if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
	var set_num;

    switch (type) {
      case "Drug Discrimination":
	    set_num = 1;
	    break;

	  case "Drug Self-Administration":
	    set_num = 2;
	    break;

	  default:
	    set_num = 0;
    }

	str = 'pulldown_process.php?form_name='+form_name+'&fld_name='+fld_name+'&file_name='+file_name+'&set_num='+set_num;
	
	xmlhttp.open("GET",str,true);
	xmlhttp.onreadystatechange = handleResponseCP; 
	xmlhttp.send(null);

	document.getElementById(fld_name+"_display").innerHTML = "--- Loading ... ---";
  }
  return false;
}

function handleResponseCP() {
  if (xmlhttp.readyState == 4) {
	input = xmlhttp.responseText.split('|');
	fld_name = input[0];
    pulldown = input[1];

	if (pulldown.length > 0)
	  document.getElementById(fld_name+"_display").innerHTML = pulldown;
  }
}

// following code uses jquery functionality
$(document).ready(function() {
  $(document).ajaxError(function(e,xhr,settings,exception) { 
    alert('Error Page: ' + settings.url + '\n\n'+'Error Message:\n' + xhr.responseText ); 
  });
});

function renderDefinition(xml) {
  var id = "";
  var title = "";
  var code = "";
  var body = "";
  
  hideDefinitionPanel();

  $("content",xml).each(function() {
	obj = $(this);
	id = $("id",obj).text();
	title = $("title",obj).text();
	code = $("code",obj).text();
	body = ($("body",obj).text()).replace(/\n/g,"<br>");
  });

  if (id.length > 0)
  {
    $("#definition_title").html(title);
	$("#definition_body").html(body);
    
	displayDefinitionPanel();
  }
}

function displayDefinitionPanel() {
  // width of definition panel
  width = 300;

  xpos = Math.round((document.body.clientWidth/2)-(width/2));
  ypos = 200;

  $("#definition").css({ "left": xpos + "px", "top": ypos + "px" });
  $("#definition").show();
}

function hideDefinitionPanel() {
  $("#definition").css({ "left": "0px", "top": "0px" });
  $("#definition").hide();
}

function displayDefinition(code) {
  // dynamically load definition
  $.post("/definition.php", {code: code}, function(xml) {
	renderDefinition(xml);
  },"xml");
}

function hideDefinition() {
  hideDefinitionPanel();
}
