/***
 Javascript library for the ITS Core Services Display and Edit pages
***/

var training_vars = {
  json_url: "https://www.case.edu/cgi-bin/its/training/training.pl",
  json_url_s: "https://www.case.edu/cgi-bin/its/training/training_s.pl",

//  image_base: "/its/images/training/",
  image_base: "/its/training/images/",
  service_home: "/its/training/",
  service_page: "/its/training/service.html",
  service_page_help: "/its/help/service_training.html",
  
  plus_img: "category_plus.png",
  minus_img: "category_minus.png"
};

/******
	Functions for the Core Services Editor
*******/

function init_edit_page() {
	set_form_defaults();
	get_services();
}

function set_form_defaults() {
  
  $(':input').each( function() {
    var type = $(this).attr('type');
  
    if (type == 'text' || type == 'textarea') {
      $(this).val("");
    }
    
    if (type == 'select' || type == 'select-one') {
      if ($(this).attr('name') != "svc_name_id") {
        $(this).selectedIndex = 0;
      }
    }
  
    if (type == 'checkbox') {
      $(this).attr('checked', false);
    }
    
  });
}

function disable_form(disabled) {
  var disabled_str = "";
  if (disabled) { disabled_str = 'disabled'; }
  
  $(':input').each( function() {
    $(this).attr('disabled', disabled_str);
  });
}

/*
	Get the list of catagories and services from the backend 
	and perform initial population of the form.
*/
function get_services() {
	disable_form(true);
	get_data(true, "get_services", training_vars.json_url_s, "display_services");
}

function display_services(json) {	

	var service_select = document.coreservices_form.svc_name_id;
	var category_select = document.coreservices_form.category_id;
	
	// clear both select lists before we populate them
	clean_select(service_select);
	clean_select(category_select);
	
	// add "default" values to the selects
	service_select[service_select.length] = new Option("-- Create a new class --", "0");
	category_select[category_select.length] = new Option("Select a training program", "-1");
	
	for (var i=0; i<json.categories.length; i++) {
		// add category to the category list
		category_select[category_select.length] = new Option(json.categories[i].name, json.categories[i].id);
		
		// add category to the service list (as a header for organization)
		//service_select[service_select.length] = new Option("--"+json.category[i].name+"--", "0");
	
		// add services withing this catagory to the service select list
		if (json.categories[i].services != null) {
			var services = json.categories[i].services;
			for (var j=0; j<services.length; j++) {
				service_select[service_select.length] = new Option(services[j].name, services[j].id);
			}
		}
	}

  $('#svc_name_id').change( function() {
      if ($('#svc_name_id').val() != "0") {
        get_service(this);
      } else {
        set_form_defaults();
      }
    });    

  disable_form(false);
}

/*
	Populate the form with a selected service
*/

function get_service(select) {
	set_form_defaults();

	// get the value selected option
	var selected_id = $(select).val();
	
	// ignore id=0...it's a header value
	if (selected_id == "0") {
	//	set_form_defaults();
		return true;
	}
	
  $('#savestatus').html("");

	get_data(true, "get_service_full", training_vars.json_url_s, "display_service", "svc_name_id="+selected_id);
}

function display_service(json) {
	
	var svc = json.services[0];
	var form = document.coreservices_form;
	
	// fill the form fields
	
	// class information
	set_input(form.svc_name, svc.svc_name);
	
	if (svc.hot_topic == 'on') { 
	$('#hot_topic').attr('checked', true);
	} else {
  	$('#hot_topic').attr('checked', false);
	}
	set_input(form.svc_description, svc.svc_description);
	/* set_input(form.special_instructions, svc.special_instructions); */
	set_input(form.contact, svc.contact);
	/* set_input(form.policy, svc.policy); */
	set_input(form.image, svc.image);
	set_urltitle(svc.classroom_materials, "coreservices_form", "classroom_materials");
	set_input(form.classroom_cal, svc.classroom_cal);
	// program information
	set_select(form.category_id, svc.category_id);

  // courses
	enable_training("online", svc.has_online);
	enable_training("classroom", svc.has_classroom);
	enable_training("video", svc.has_video);
	enable_training("inperson", svc.has_inperson);
	//info for online
	set_input(form.online_description, svc.online_description);
/* 	set_input(form.online_url, svc.online_url); */
	set_urltitle(svc.online_url, "coreservices_form", "online_url");
	set_input(form.online_prereq, svc.online_prereq);
	set_urltitle(svc.online_prereq_url, "coreservices_form", "online_prereq_url")
	set_input(form.online_time, svc.online_time);
	set_input(form.online_special_instructions, svc.online_special_instructions);
	set_input(form.online_policy, svc.online_policy);
	//info for classroom		
	set_input(form.classroom_description, svc.classroom_description);
	set_urltitle(svc.classroom_reg_url, "coreservices_form", "classroom_reg_url")
	set_input(form.classroom_prereq, svc.classroom_prereq);
	set_urltitle(svc.classroom_prereq_url, "coreservices_form", "classroom_prereq_url")
	set_input(form.classroom_time, svc.classroom_time);
	set_input(form.classroom_special_instructions, svc.classroom_special_instructions);
	set_input(form.classroom_policy, svc.classroom_policy);
	//info for video	
	set_input(form.video_description, svc.video_description);
	set_urltitle(svc.video_url, "coreservices_form", "video_url")
	set_input(form.video_time, svc.video_time);
	set_input(form.video_special_instructions, svc.video_special_instructions);
	set_input(form.video_policy, svc.video_policy);
	//info for inperson	
	set_input(form.inperson_description, svc.inperson_description);
	set_urltitle(svc.inperson_url, "coreservices_form", "inperson_url")
	set_input(form.inperson_time, svc.inperson_time);
	set_input(form.inperson_special_instructions, svc.inperson_special_instructions);
	set_input(form.inperson_policy, svc.inperson_policy);

  // set the last mod
  $('#last_modified').html("last modified " + svc.update_date + " by " + svc.mod_by);
}

function do_type_checkbox(el) {
  if ( $(el).is(':checked') ) {
    $('#'+$(el).attr("id")+'_div').show();
  } else {
    $('#'+$(el).attr("id")+'_div').hide();
  }
}

function enable_training(name, enabled) {
  if (enabled == 'on') {
    $('#has_'+name).attr('checked', true);
    $('#has_'+name+'_div').show();
  } else {
    $('#has_'+name).attr('checked', false);
    $('#has_'+name+'_div').hide();
  }
}

function save_service() {
	//disable_form(true);
	
  $('#savestatus').css('color', '#aaaaaa');
  $('#savestatus').html('Saving...');
 
  var sb = [];

	
	// build the multi-value title url values
	sb[sb.length] = get_urltitle("online_url");
	sb[sb.length] = get_urltitle("online_prereq_url");
	sb[sb.length] = get_urltitle("classroom_reg_url");
	sb[sb.length] = get_urltitle("classroom_prereq_url");
	sb[sb.length] = get_urltitle("classroom_materials");
	sb[sb.length] = get_urltitle("classroom_reference");
	sb[sb.length] = get_urltitle("video_url");
	sb[sb.length] = get_urltitle("inperson_url");

	
	// add the mode
	sb[sb.length] = "mode=put_service";
	
	sb[sb.length] = $('#coreservices_form').serialize();
				
	$.ajax({
		type: 'POST',
		url: training_vars.json_url_s,
		data: sb.join('&'),
		success: show_save_state,
		dataType: "json"
	});
}

function show_save_state(data) {
	if (data.result == "success") {
	  $('#savestatus').css('color', 'green');
	  $('#savestatus').html('Save Complete');
		init_edit_page();
	} else {
	  $('#savestatus').css('color', 'red');
	  $('#savestatus').html('Save Failed');
	}

	disable_form(false);
}

function delete_service() {
  var answer = confirm("This will delete this service and make it no longer visible in the Our Services catalog.  Are you sure you want to continue?");
  if (answer) {
    delete_service2();
  } else {
  }
}

function delete_service2() {
	disable_form(true);
	
	$('#deletestatus').css('color', '#aaaaaa');
  $('#deletestatus').html('Deleting...');
  
	var data = new Object();
	
	// fill in required fields
  data.category_id = $('#category_id').val();
	data.svc_name = $('#svc_name').val();

	
	// send the data to the server
	data.mode = "delete_service";
	data.svc_name_id = $('#svc_name_id').val();

  $.ajax({
		type: 'POST',
		url: training_vars.json_url_s,
		data: data,
		success: show_delete_state,
		dataType: "json"
	});
	
}

function show_delete_state(data) {
	if (data.result == "success") {
	  $('#deletestatus').css('color', 'green');
	  $('#deletestatus').html('Delete Complete');
		init_edit_page();
	} else {
	  $('#deletestatus').css('color', 'red');
	  $('#deletestatus').html('Delete Failed');
	}

	disable_form(false);
}

/****
 Functions for the Program Editor
*****/

var category_json;

function init_category_edit_page() {
	set_form_defaults();
	get_categories();
}

function get_categories() {
	disable_form(true);
	get_data(true, "get_services", training_vars.json_url_s, "display_categories");
}

function display_categories(json) {
	//disable_form(true);
	
	category_json = json; // store it for later
	
	var category_select = document.coreservices_form.category_id;
	clean_select(category_select);

	category_select[category_select.length] = new Option("-- Create a New Program --", "0");

  for (var i=0; i<json.categories.length; i++) {
		// add category to the category list
		category_select[category_select.length] = new Option(json.categories[i].name, json.categories[i].id);
	}
	
  $('#category_id').change( function() {
      if ($('#category_id').val() != "0") {
        display_category(this);
      } else {
        set_form_defaults();
      }
    });
    
  disable_form(false);
}

function display_category(select) {
  set_form_defaults();
  
  $('#savestatus').html("");
  
  // get the value selected option
	var selected_id = $(select).val();
	
	if (selected_id == 0) {
	  // it's a header....ignore
	  return true;
	}

  var categories = category_json.categories;
  var form = document.coreservices_form;
  
  for (var i=0; i<categories.length; i++) {
    if (categories[i].id == selected_id) {
      // this is the one we want
      
      set_input(form.category_name, categories[i].name);
      set_input(form.category_description, categories[i].description);
      set_input(form.category_image, categories[i].image);
      set_input(form.category_url, categories[i].url);
      
      $('#last_modified').html("last modified " + categories[i].update_date + " by " + categories[i].mod_by);
      
      break;
    }
  }
}

function save_category() {
  $('#savestatus').css('color', '#aaaaaa');
  $('#savestatus').html('Saving...');
  
  var sb = [];
  
  // add the mode
	sb[sb.length] = "mode=put_category";
	
	sb[sb.length] = $('#coreservices_form').serialize();
  
  $.ajax({
		type: 'POST',
		url: training_vars.json_url_s,
		data: sb.join('&'),
		success: show_category_save_state,
		dataType: "json"
	});
}

function show_category_save_state(data) {
	if (data.result == "success") {
	  $('#savestatus').css('color', 'green');
	  $('#savestatus').html('Save Complete');
		init_category_edit_page();
	} else {
	  $('#savestatus').css('color', 'red');
	  $('#savestatus').html('Save Failed');
	}

	disable_form(false);
}

function delete_category() {
  var answer = confirm("This will delete this category and make it no longer visible in the training catalog.  Are you sure you want to continue?");
  if (answer) {
    delete_category2();
  } else {
  }
}
    
function delete_category2() {
	disable_form(true);
	
	$('#deletestatus').css('color', '#aaaaaareen');
  $('#deletestatus').html('Deleting...');
  
	var data = new Object();
	
	// send the data to the server
	data.mode = "delete_category";
  data.category_id = $('#category_id').val();
  
  $.ajax({
		type: 'POST',
		url: training_vars.json_url_s,
		data: data,
		success: show_category_delete_state,
		dataType: "json"
	});
	
}

function show_category_delete_state(data) {
	if (data.result == "success") {
	  $('#deletestatus').css('color', 'green');
	  $('#deletestatus').html('Delete Complete');
		init_category_edit_page();
	} else {
	  $('#deletestatus').css('color', 'red');
	  $('#deletestatus').html('Delete Failed');
	}

	disable_form(false);
}
    
/****
	Functions for the Core Services Catalog
*****/

function init_catalog_page() {  

  // set alternate service display page based on URL
  if ( location.href.match('/its/help') ) {
    training_vars.service_page = training_vars.service_page_help;
  }
  
  get_data(false, "get_services", training_vars.json_url, "display_catalog_services");
	
	//catalog_mode("category");
	
}

function display_catalog_services(json) {

	/* populate the service catalog tab */
	
	var catalog_div = document.getElementById("coreservices_catalog");

	var sb = [];
	
	json.categories.sort(function (obj1, obj2) {
                        return obj1.sort_order < obj2.sort_order ? -1 :
                        (obj1.sort_order > obj2.sort_order ? 1 : 0);
                    });
	
	for (var i=0; i<json.categories.length; i++) {
		// Service Header
		var panel_num = i+1;

		var items = [];
	
		if (json.categories[i].services != null) {
			var services = json.categories[i].services;

			for (var j=0; j<services.length; j++) {
				if (services[j].description == "") {	
					items[items.length] = showhide_additem('<span class="item_dot">&#149;</span>'+services[j].name);
				} else {
					items[items.length] = showhide_additem('<a href="'+training_vars.service_page+'?' + services[j].id + '"><span class="item_dot">&#149;</span>' +services[j].name+'</a>');
				}
			}
		}

//		sb[sb.length] = showhide_addmenu("catalog"+i, '<div style="float: left; display: inline; width: 25px; border: solid 1 black;">'+panel_num+".</div>  "+json.category[i].name, items);
		sb[sb.length] = showhide_addmenu("catalog"+i, json.categories[i].name, json.categories[i].description, json.categories[i].image, json.categories[i].id, items, json.categories[i].url);
	}

	$('#coreservices_catalog').html( sb.join("\n") );
		
	/* populate the a-z tab */
/*	
	var az_div = document.getElementById("coreservices_catalog_az");
	sb = [];
		
	for (var i=0; i<json.az.length; i++) {
    sb[sb.length] = '<div class="showhide">';
  
		sb[sb.length] = '<div class="az_header"><div class="category_title" style="margin-top: 0px;><a name="'+json.az[i].letter+'">'+json.az[i].letter+'</a></div></div>';
		
		var services = json.az[i].service;

		sb[sb.length] = '  <div class="subsection">';

		for (var j=0; j<services.length; j++) {
      if (services[j].description == "") {	
        sb[sb.length] = showhide_additem('<span class="item_dot">&#149;</span>'+services[j].name);
      } else {
        sb[sb.length] = showhide_additem('<a href="service.html?' + services[j].id + '"><span class="item_dot">&#149;</span>' +services[j].name+'</a>');
      }
    }
    
    sb[sb.length] = '</div></div>';
	}
	
	az_div.innerHTML = sb.join("\n");
*/
}

function catalog_mode(_mode) {
	var mode_switch_div = document.getElementById("mode_switch_div");
	var category_div = document.getElementById("coreservices_catalog");
	var az_div = document.getElementById("coreservices_catalog_az");

  if (_mode == 'az') {
    mode_switch_div.innerHTML = "<a href=\"#\" onclick=\"javascript: catalog_mode('category');\">Display Services by Category</a>";
    category_div.style.display="none";
    az_div.style.display="block";
  } else {
    mode_switch_div.innerHTML = "<a href=\"#\" onclick=\"javascript: catalog_mode('az');\">Display Services Alphabetically</a>";
   az_div.style.display="none";
   category_div.style.display="block";
  }
}

/******
	Functions for individual service display
*******/

function init_service_page() {
	var service_id = window.location.search.substring(1);
	get_data(false, "get_service", training_vars.json_url, "display_service_service", "svc_name_id="+service_id);
}

function display_service_service(json) {
	var svc = json.services[0];
  // populate the basic class information
  $('#category_name').html(svc.category_name);
  $('#category_description').html(decode_string_html(svc.category_description));
  $('#category_url').attr('href', svc.category_url);
  $('#svc_name').html(svc.svc_name);
  $('#svc_description').html(decode_string_html(svc.svc_description));
  
  do_service_block('contact', svc.contact);
/*  do_service_block('special_instructions', svc.special_instructions);
  do_service_block('policy', svc.policy);*/
   /*if (svc.classroom_cal != null) {*/
	$('#calendar_header').html ('Upcoming Training Classes');
 	do_calendar('classroom_cal', svc.classroom_cal);
   /*}*/
  set_urltitle_documents('classroom_materials', svc.classroom_materials);
  
  // populate the class types and show the appropriate sections
  //ONLINE
  if (svc.has_online != null && svc.has_online=="on") { 
	if(svc.online_description != null && svc.online_description.length >1){
	$('#onlineDescriptionTitle').html ('Description');
	$('#online_description').html(decode_string_html(svc.online_description));
	}
	if(svc.online_url != null && svc.online_url.length >1){
	$('#online_tmt').html ('Take Me There');
	set_urltitle_service('online_url', svc.online_url);
	}
    /* do_service_block('online_prereq', svc.online_prereq); */
	if(svc.online_prereq != null && svc.online_prereq.length >1){
	$('#online_prereq_title').html ('Prerequisites');
	$('#online_prereq_text').html(decode_string_html(svc.online_prereq));
	}
	if(svc.online_prereq_url != null && svc.online_prereq_url !="||||||||||||||||"){
	$('#online_prereq_links_title').html('Prerequisite Links');
    set_urltitle_service('online_prereq_url', svc.online_prereq_url);
	}
	if(svc.online_time != null && svc.online_time.length >1){
	$('#online_time').html(decode_string_html(svc.online_time));
	}
	if(svc.online_special_instructions != null && svc.online_special_instructions.length >1){
	$('#online_instructions_title').html ('Special Instructions');
	$('#online_special_instructions').html(decode_string_html(svc.online_special_instructions));
	}
	if(svc.online_policy != null && svc.online_policy.length >1){
	$('#online_policy_title').html ('Policies');
	$('#online_policy').html(decode_string_html(svc.online_policy));
	}
    $('#has_online_type').show();
    $('#c_online').show();
  }
  //CLASSROOM
  if (svc.has_classroom != null && svc.has_classroom=="on") {
    if(svc.classroom_description != null && svc.classroom_description.length >1){
	$('#classroomDescriptionTitle').html ('Description');
	$('#classroom_description').html(decode_string_html(svc.classroom_description));
	}
	if(svc.classroom_reg_url != null && svc.classroom_reg_url.length >1){
	$('#classRegTitle').html ('Registration');
	set_urltitle_service('classroom_reg_url', svc.classroom_reg_url);
	}
    /* do_service_block('classroom_prereq', svc.classroom_prereq); */
	if(svc.classroom_prereq != null && svc.classroom_prereq.length >1){
	$('#classroom_prereq_title').html ('Prerequisites');
	$('#classroom_prereq_text').html(decode_string_html(svc.classroom_prereq));
	}
	if(svc.classroom_prereq_url != null && svc.classroom_prereq_url !="||||||||||||||||"){
	$('#classroom_prereq_links_title').html('Prerequisite Links');
    set_urltitle_service('classroom_prereq_url', svc.classroom_prereq_url);
	}
	if(svc.classroom_time != null && svc.classroom_time.length >1){
	$('#classroom_time').html(decode_string_html(svc.classroom_time));
	}
	if(svc.classroom_special_instructions != null && svc.classroom_special_instructions.length >1){
	$('#classroom_instructions_title').html ('Special Instructions');
	$('#classroom_special_instructions').html(decode_string_html(svc.classroom_special_instructions));
	}
	if(svc.classroom_policy != null && svc.classroom_policy.length >1){
	$('#classroom_policy_title').html ('Policies');
	$('#classroom_policy').html(decode_string_html(svc.classroom_policy));
	}
    $('#has_classroom_type').show();
    $('#c_classroom').show();
  }
  //VIDEO
  if (svc.has_video != null && svc.has_video=="on") {
    if(svc.video_description != null && svc.video_description.length >1){
	$('#videoDescriptionTitle').html ('Description');
	$('#video_description').html(decode_string_html(svc.video_description));
	}
	if(svc.video_url != null && svc.video_url.length >1){
	$('#video_tmt').html ('Take Me There');
	set_urltitle_service('video_url', svc.video_url);
	}
	if(svc.video_time != null && svc.video_time.length >1){
	$('#video_time').html(decode_string_html(svc.video_time));
	}
	if(svc.video_special_instructions != null && svc.video_special_instructions.length >1){
	$('#video_instructions_title').html ('Special Instructions');
	$('#video_special_instructions').html(decode_string_html(svc.video_special_instructions));
	}
	if(svc.video_policy != null && svc.video_policy.length >1){
	$('#video_policy_title').html ('Policies');
	$('#video_policy').html(decode_string_html(svc.video_policy));
	}
    $('#has_video_type').show();
    $('#c_video').show();
  }
  //INPERSON
  if (svc.has_inperson != null && svc.has_inperson=="on") {
    if(svc.inperson_description != null && svc.inperson_description.length >1){
	$('#inpersonDescriptionTitle').html ('Description');
	$('#inperson_description').html(decode_string_html(svc.inperson_description));
	}
	if(svc.inperson_url != null && svc.inperson_url.length >1){
	$('#inperson_tmt').html ('Request');
	set_urltitle_service('inperson_url', svc.inperson_url);
	}
	if(svc.inperson_time != null && svc.inperson_time.length >1){
	$('#inperson_time').html(decode_string_html(svc.inperson_time));
	}
	if(svc.inperson_special_instructions != null && svc.inperson_special_instructions.length >1){
	$('#inperson_instructions_title').html ('Special Instructions');
	$('#inperson_special_instructions').html(decode_string_html(svc.inperson_special_instructions));
	}
	if(svc.inperson_policy != null && svc.inperson_policy.length >1){
	$('#inperson_policy_title').html ('Policies');
	$('#inperson_policy').html(decode_string_html(svc.inperson_policy));
	}

    $('#has_inperson_type').show();
    $('#c_inperson').show();
  }
  
}

/*
  do_service_block
    Handles the display of text blocks on the services page.  Hides the block if 
    there is nothing to display.
*/
function do_service_block(_el, _data) {
  if (_data != null && _data != "") {
    $('#'+_el+"_text").html( decode_string_html(_data) );
    $('#'+_el+"_div").show();
  } else {
    $('#'+_el+"_div").hide();
  }
}

function do_link(_el, _url) {
  $('#'+_el).html('<a href="'+_url+'">'+_url+'</a>');
}

function do_calendar(_el, _cal_id) {

 /* if (_cal_id != null) {*/
	  eventsDiv = document.getElementById(_el+'_iframe');
   var calColors = [
                     {'calid': _cal_id,
'color': '#009000'}
                   ];
	  
	   $('#'+_el+'_iframe').html('<script src="http://www.google.com/calendar/feeds/case.edu_55ahesf0ftrhlg8uip1itgiv9g@group.calendar.google.com/public/full?alt=json-in-script&callback=insertAgenda&orderby=starttime&singleevents=true&sortorder=ascending&futureevents=true"></script>');
	   /*alert(_cal_id);
  $('#'+_el+'_iframe').html('<iframe src="https://www.google.com/calendar/b/0/embed?showTitle=0&amp;showNav=0&amp;showDate=0&amp;showPrint=0&amp;showTabs=0&amp;showCalendars=0&amp;showTz=0&amp;mode=AGENDA&amp;height=300&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src='+_cal_id+'&amp;color=%23865A5A&amp;ctz=America%2FNew_York" width="190" height="200" frameborder="0" scrolling="no"></iframe>');*/
    $('#'+_el+'div').show();
 /* } else {
    $('#'+_el+'div').hide();
  }*/

}

function service_field(title, content) {
	if(content != null && content.length != 0) {
		return '<dt>'+title+':</dt><dd>'+decode_string_html(content)+'</d>'; 
	}

	return "";
}

function service_block(title, content) {
	if (content != null && content.length != 0) { 
		return '<div class="header">'+title+'</div><div class="content">'+decode_string_html(content)+'</div>';
	}
	
	return "";
}

function service_block_with_links(title, content, links) {
	if (content != null && content.length != 0) { 
		//return '<div class="header">'+title+'</div><div class="content" style="float: left">'+decode_string_html(content)+'</div><div class="links">foo bar baz</div>';
		
		return '<div class="header">'+title+'</div><div class="content"><div style="float: left; width: 430px; padding-right: 30px; ">'+decode_string_html(content)+'</div><div style="text-align: center; float: right; width: 140px; margin-left: 10px;"><div style="font-size: 110%; border-bottom: 1px solid #d9dcdf; width: 100px; margin-left: 20px; margin-bottom: 10px;">Links</div>'+links+'</div></div>';
	}
	
	return "";
}

function service_block_graybox(title, content, image) {
  if (content != null && content.length != 0) { 
		if (image != null && image.length != 0) {
  		return '<div class="graybox"><img class="service_icon" src="'+image+'"/><div class="header">'+title+'</div><div class="content">'+decode_string_html(content)+'</div></div>';
    } else {		  
      return '<div class="graybox"><div class="header">'+title+'</div><div class="content">'+decode_string_html(content)+'</div></div>';
  	}
	}
	return "";
}


/******
  Hot topics display
*******/
function init_hot_topics() {
  // set alternate service display page based on URL
  if ( location.href.match('/its/help') ) {
    training_vars.service_page = training_vars.service_page_help;
  }

  get_data(false, "get_hot_topics", training_vars.json_url, "display_hot_topics");
}

function display_hot_topics(json) {
  var svc = json.services;
  
  var sb=[];
  
  for (var i=0; i<svc.length; i++) {
    sb[sb.length] = '<li><a href="'+training_vars.service_page+'?'+svc[i].id+'">'+svc[i].name+'</a></li>';
  }
  
  $('#training_hot_topics').html('<ul>'+sb.join('\n')+'</ul>');
}

/******
	Hide/Show Explorer Code
******/

function showhide_addmenu(_id, _text, _description, _img, _category_id, _items, _url) {
	var sb = [];
if(_items == ""){
	sb[sb.length] = '<div class="showhide">';

		sb[sb.length] = '  <div id="showhide_'+_id+'" class="header" onclick="open_win(\''+_url+'\')" >';
		
		sb[sb.length] = '    <div class="category_icon"><img class="category_icon" src="'+training_vars.image_base+_img+'"/></div>';

    sb[sb.length] = '    <div class="showhide_icon">	<img id="'+_id+'-img" class="showhide_icon", src="'+training_vars.image_base+training_vars.plus_img+'" height="11" width="16"/></div>';
    
    //sb[sb.length] = '    <div class="category_title">' + _text + '<br /><span class="category_description">'+_description+'</span></div>';
		sb[sb.length] = '    <div class="category_title">' + _text + '</div>';
			
//		sb[sb.length] = '    <img class="category_icon" src="images/coreservices-cat-icon.png"/><img id="'+_id+'-img" class="showhide_icon", src="'+training_vars.plus_img+'" height="12" width="16"/>'+ _text;
		//sb[sb.length] = '     '+ _text;
		sb[sb.length] = '  </div>';
		/* sb[sb.length] = '  <div class="subsection" style="display:none" id="'+_id+'-div">';
		sb[sb.length] = _items.join("\n");
		sb[sb.length] = '  </div>'; */
		sb[sb.length] = '</div>';
}else{
		sb[sb.length] = '<div class="showhide">';

		sb[sb.length] = '  <div id="showhide_'+_id+'" class="header" onclick="showhide(\''+_id+'\')" >';
		
		sb[sb.length] = '    <div class="category_icon"><img class="category_icon" src="'+training_vars.image_base+_img+'"/></div>';

    sb[sb.length] = '    <div class="showhide_icon">	<img id="'+_id+'-img" class="showhide_icon", src="'+training_vars.image_base+training_vars.plus_img+'" height="11" width="16"/></div>';
    
    //sb[sb.length] = '    <div class="category_title">' + _text + '<br /><span class="category_description">'+_description+'</span></div>';
		sb[sb.length] = '    <div class="category_title">' + _text + '</div>';
			
//		sb[sb.length] = '    <img class="category_icon" src="images/coreservices-cat-icon.png"/><img id="'+_id+'-img" class="showhide_icon", src="'+training_vars.plus_img+'" height="12" width="16"/>'+ _text;
		//sb[sb.length] = '     '+ _text;
		sb[sb.length] = '  </div>';
		sb[sb.length] = '  <div class="subsection" style="display:none" id="'+_id+'-div">';
		sb[sb.length] = _items.join("\n");
		sb[sb.length] = '  </div>';
		sb[sb.length] = '</div>';
}
	return sb.join("");
}

function showhide_additem(_text, _event) {
	var sb = [];
	
	sb[sb.length] = '<div class="item"';
	if (_event != null) {
		sb[sb.length] = ' onclick="'+_event+'"';
	}
	sb[sb.length] = '>' + _text + '</div>';
	
	return sb.join("");
}

function showhide(id) {
    the_block = document.getElementById(id+"-div");
    the_image = document.getElementById(id+"-img");

    if (the_block.style.display == "none") {
        the_block.style.display = "block";
        the_image.src = training_vars.image_base+training_vars.minus_img;
	  } else {
        the_block.style.display = "none";
        the_image.src = training_vars.image_base+training_vars.plus_img;
    }
}

/****
* Utility Functions 
*****/

function decode_string_html(_str) {
	if (_str != null && _str.length != 0) {
		return '<div class="bodyblock">' + _str.replace(/([\r\n]+)/g, '</div><div class="bodyblock">') + '</div>';
		
		
		/*
		if (str.length == 1) {
			// only a single line....don't do anything to it
			return str[0];
		} else {
			return '<div class="bodyblock">' + str.join('</div><div class="bodyblock">') + '</div>';
		}
		*/
	}
	
	return "";
}

function set_input(el, value) {
	if (value != null) {
		// replace two pipes '||' with a newline
		el.value = value.replace( new RegExp(";-;", "g"), "\n");
	}
}

// get_input returns a name=value pair to use in the form submit
function get_input(el, data) {
	if (el.className == "full") {
		var value = el.value;		
		value = el.value.replace( new RegExp("\\n", "g"), ";-;");
		return value;
	}	
}

// set_urltitle sets the repeating Title/URL field groups based on the delimited string 
// we get from the backend
function set_urltitle_form(form, el_base, data) {
  if (data != null && data != "") {
    var info = data.split("||");
    var r_info = "";
    
    for (var i=0; i<info.length; i++) {
      if (info[i] == null || info[i] == "") { continue; }
      r_info = info[i].split("|");
      $('#'+form+' input[name="'+el_base+i+'_title"]').val(r_info[0]);
      $('#'+form+' input[name="'+el_base+i+'_url"]').val(r_info[1]);
    }
  }
}

function set_urltitle(data, form, el_base) {
  if (data != null && data != "") {
    var info = data.split("||");
    var r_info = "";
    
    for (var i=0; i<info.length; i++) {
      if (info[i] == null || info[i] == "") { continue; }
      r_info = info[i].split("|");
      $('#'+form+' input[name="'+el_base+i+'_title"]').val(r_info[0]);
      $('#'+form+' input[name="'+el_base+i+'_url"]').val(r_info[1]);


    }

  }
}
 
function set_urltitle_service(_el, _data) {
  var urls = [];
  if (_data != null && _data != "") {
    var info = _data.split("||");
    var r_info = "";
    
    for (var i=0; i<info.length; i++) {
      if (info[i] == null || info[i] == "") { continue; }
      r_info = info[i].split("|");
      if(r_info[0] != null && r_info[0] != ""){
      urls[urls.length] = '<a href="'+r_info[1]+'">'+r_info[0]+'</a>';
	  }
    }
    $('#'+_el+'_text').html( urls.join('<br />') );
    $('#'+_el+'_div').show();
  
  } else {
    $('#'+_el+'_div').hide();
  }
}

function set_urltitle_documents(_el, _data) {
  var urls = [];
  
  if (_data != null && _data != "") {
    var info = _data.split("||");
    var r_info = "";
    
    for (var i=0; i<info.length; i++) {
      if (info[i] == null || info[i] == "") { continue; }
      r_info = info[i].split("|");
      if(r_info[0] != null && r_info[0] != ""){
      urls[urls.length] = '<div class="boximage"><img src="'+training_vars.image_base+'pdf.png" width="20" height="20" alt="word" /></div><div class="boxcontent"><a href="'+r_info[1]+'">'+r_info[0]+'</a></div>';
      }
	}
    $('#'+_el+'_text').html( urls.join('\n') );
    $('#'+_el+'_div').show();
  
  } else {
    $('#'+_el+'_div').hide();
  }
}

function get_urltitle(el_base) {
  var sb = [];
  var index = -1;
  var namebase = "";
  $('input[name^="'+el_base+'"]').each( function() {
    var name = $(this).attr('name');
    var index = name.indexOf("_title");
    if (index != -1) {
      sb[sb.length] = $(this).val() + '|' + $('input[name="'+name.substr(0, index)+'_url"]').val();
    }
  });
  
  //$('#'+el_base).val(sb.join("||"));
  
  return(el_base + '=' + encodeURIComponent(sb.join("||")));
  
}
function open_win(_url)
{
	window.open(_url)
}

function clean_select(select) {
	for (var i=select.length; i >= 0; i--) {
		select[i] = null;
	}
}

function set_select(select, value) {
	select.selectedIndex = 0;
	
	if (value != null) {
		for (var i=0; i<select.length; i++) {
			if (select[i].value == value) {
				select.selectedIndex = i;
				break;
			}
		}
	}
}

function get_select(select, data) {
	return select[select.selectedIndex].value;
}

function get_data(_secure, _mode, _url, _callback, _params) {
	var url = _url + "?mode=" + _mode + "&callback=" + _callback;
	if (_params != null) {
		url += "&" + _params;
	}
	
	var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  if (_secure) {
	  script.src = "https://login.case.edu/cas/login?service=" + escape(url);
  } else {
  	script.src = url;
  }
  //alert(script.src);
  head.appendChild(script);
}
