emag_course_url = '';
emag_random = (new Date()).getTime();
emag_org_error_handler = window.onerror;

function quoted(str) {
  return (str != null) ? '"' + str + '"' : '""';
}

function emag_encodeURIComponent(str) {
  if (typeof(encodeURIComponent) == 'function') {
    return encodeURIComponent(str);
  }
	else {
    return escape(str);
  }
}

function emag_append_url(param, value) {
  if (value) {
    window.emag_course_url += '&' + param + '=' + value;
  }
}

function emag_append_url_esc(param, value) {
  if (value) {
    emag_append_url(param, emag_encodeURIComponent(value));
  }
}

function emag_append_color(param, value) {
  if (value && typeof(value) == 'object') {
    value = value[window.emag_random % value.length];
  }
  emag_append_url('color_' + param, value);
}

function emag_show_course() {

  var w = window;
	w.onerror = w.emag_org_error_handler;

  w.emag_course_url = 'http://metrix.emagister.com/cs/getCourses?';
  w.emag_course_affiliate = w.emag_course_affiliate.toLowerCase();
  w.emag_course_url += 'id_affiliate=' + escape(w.emag_course_affiliate) + '&random=' + w.emag_random;

	emag_append_url('hl', w.emag_language);
	emag_append_url('gl', w.emag_country);
  emag_append_url('oe', w.emag_encoding);

  if (w.emag_course_format) {
    emag_append_url_esc('format', w.emag_course_format.toLowerCase());
  }
	
  emag_append_url('num_courses', w.emag_max_num_courses);
  emag_append_url('output', w.emag_course_output);
  emag_append_url_esc('url', w.emag_page_url);
  emag_append_color('bg', w.emag_color_bg);
  emag_append_color('text', w.emag_color_text);
  emag_append_color('link', w.emag_color_link);
  emag_append_color('url', w.emag_color_url);
  emag_append_color('border', w.emag_color_border);
  emag_append_color('line', w.emag_color_line);
	emag_append_url('tagId', w.emag_course_section);
	
  w.emag_course_url = w.emag_course_url.substring(0, 1000);
  w.emag_course_url = w.emag_course_url.replace(/%\w?$/, '');

  if (emag_course_output == 'js' && w.emag_courses_request_done) {
    document.write('<script language="JavaScript1.1"' +
                   ' src=' + quoted(emag_course_url) +
                   '></script>');
  }
	else if (emag_course_output == 'html') {
      document.write('<iframe' +
                     ' name="emag_courses_frame"' +
                     ' width=' + quoted(w.emag_course_width) +
                     ' height=' + quoted(w.emag_course_height) +
                     ' frameborder=' + quoted(w.emag_course_frameborder) +
                     ' src=' + quoted(w.emag_course_url) +
                     ' marginwidth="0"' +
                     ' marginheight="0"' +
                     ' vspace="0"' +
                     ' hspace="0"' +
                     ' allowtransparency="true"' +
                     ' scrolling="no">');
      document.write('</iframe>');
	}

  w.emag_course_frameborder = null;
  w.emag_course_format = null;
  w.emag_language = null;
  w.emag_country = null;
  w.emag_encoding = null;
  w.emag_course_output = null;
  w.emag_max_num_courses = null;
  w.emag_color_bg = null;
  w.emag_color_text = null;
  w.emag_color_link = null;
  w.emag_color_url = null;
  w.emag_color_border = null;
  w.emag_color_line = null;
	w.emag_course_section = null;
}

function emag_error_handler(message, url, line) {
  emag_show_course();
  return true;
}

window.onerror = emag_error_handler;

if (window.emag_course_frameborder == null) {
  emag_course_frameborder = 0;
}

if (window.emag_course_output == null) {
  emag_course_output = 'html';
}

if (window.emag_course_format == null && window.emag_course_output == 'html') {
  emag_course_format = emag_course_width + 'x' + emag_course_height;
}

if (window.emag_page_url == null) {
  window.emag_page_url = document.referrer;
  if (window.top.location == document.location) {
    window.emag_page_url = document.location;
    window.emag_last_modified_time = Date.parse(document.lastModified) / 1000;
  }
}
emag_show_course();

