Location: PHPKode > projects > Course-manager > courseman-0.1.1/global-javascript.php
<?php
  header('Content-Type: application/x-javascript');
  require('global-php.php');
  $javascript = 'function introspect(object) {';
  				/* gibt Informationen ueber object in
					Textform zurueck */
  $javascript .= $debug_mark[7] . '
  return typeof(object);
}
function valid_number(string) {'; /* prueft einen String, ob er eine gueltige
					Zahl darstellt */
  $javascript .= '
  return (-1 == string.search(/[^0-9]/));
}
function ordinal_number(cardinal) {'; /* creates an ordinal number
					representation from a cardinal number */
  $ordinal_generator = array(
	'en' => 'var suffix = "th";
  if ((1 == cardinal % 10) && !(11 == cardinal % 100)) {
    suffix = "st";
  }
  if ((2 == cardinal % 10) && !(12 == cardinal % 100)) {
    suffix = "nd";
  }
  if ((3 == cardinal % 10) && !(13 == cardinal % 100)) {
    suffix = "rd";
  }
  return cardinal + suffix;',
	'de' => 'return cardinal + ".";');
  $javascript .= '
  ' . $ordinal_generator[$language] . '
}
function get_xmlHTTP() {
  var xmlHTTP = false;
  // Mozilla, Opera, Safari sowie Internet Explorer 7
  if (typeof(XMLHttpRequest) != "undefined") {
    xmlHTTP = new XMLHttpRequest();
  }
  if (!xmlHTTP) {
  // Internet Explorer 6 und älter
    try {
      xmlHTTP  = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHTTP  = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xmlHTTP  = false;
      }
    }
  }
  return xmlHTTP;
}
function entferne_kindknoten(knoten) {'; /* entfernt alle Kindknoten des
						übergebenen Elternknotens */
  $javascript .= '
  if (knoten) {
    while (knoten.childNodes.length > 0) {
      knoten.removeChild(knoten.firstChild);
    }
  }
}
function do_nothing(ignored) {'; /* tut nichts. Benoetigt als funktionaler
					Aufrufparameter */
  $javascript .= '
}
function Prefixed_Number_Set(prefix) {';
			/* unendliche Menge aller Zeichenketten, die mit prefix
				gefolgt von einer Ziffer beginnen */
  $javascript .= '
  this.regexp = new RegExp("^" + prefix + "[0-9]");
  this.contains = function (element) {
    return element.match(this.regexp);
  }
}
function Set(elements) {'; /* endliche Menge der Werte im array elements */
  $javascript .= '
  if ((elements == 0) || (typeof(elements) == "undefined")) {
    elements = new Array();
  }
  this.elements = elements;
  this.add_hook = do_nothing;'; /* is called on added elements */
  $javascript .= '
  this.remove_hook = do_nothing;'; /* is called on removed elements */
  $javascript .= $debug_mark[8] . '
  this.element_index = function (contained) {
    for (var i = 0; i < this.elements.length; i++) {
      if (this.elements[i] == contained) {
        return i;
      }
    }
    return null;
  }
  this.contains = function (contained) {
    return this.element_index(contained) != null;
  }
  this.add_element = function (added) {
    if (!this.contains(added)) {
      ' . js_array_push('this.elements','added') . '
      this.add_hook(added);
    }
  }' .  $debug_mark[10] . '
  this.remove_element = function (removed) {
    var index = this.element_index(removed);
    if (index != null) {
      for (var i = index; i > 0; i--) {
        this.elements[i] = this.elements[i - 1];
      }
      this.elements.shift();
      this.remove_hook(removed);
    }
  }
  this.remove_from = function (removed) {
    var remaining = 0;
    var tested = 0;
    while (tested < this.elements.length) {
      if (!removed.contains(this.elements[tested])) {
        remaining++;
      } else {
        this.remove_hook(this.elements[tested]);
      }
      tested++;
      if (tested > remaining) {
        this.elements[remaining] = this.elements[tested];
      }
    }' .  $debug_mark[11] . '
    while (tested > remaining) {
      tested--;
      this.elements.pop();
    }
  }
}
var disabled_menu_options = new Set();' . ($old_internet_explorer ? '
var menu_selections = new Array();' : '') . '
var menu_onchange = new Array();
disabled_menu_options.add_hook = function (added) {' . ($debug["old-debug"] ? '
  trace("disabling " + added);' : '') . '
  var existing = document.getElementById(added);
  if (existing != null) {' . ($old_internet_explorer ? '
     existing.style.color = "graytext";' : '') . '
     existing.disabled = true;
  }
}' . $debug_mark[12] . '
disabled_menu_options.remove_hook = function (removed) {'
	. ($debug["old-debug"] ? '
  trace("enabling " + removed);' : '') . '
  var existing = document.getElementById(removed);
  if (existing != null) {' . ($old_internet_explorer ? '
     existing.style.color = "menutext";' : '') . '
     existing.disabled = false;
  }
}
function hook_onchange(menu_id,handler) {'; /* haengt einen neuen
			onChange-Handler handler fuer das Menue mit der ID
			menu_id ein */
  $javascript .= '
  if (typeof(menu_onchange[menu_id]) == "undefined") {
    menu_onchange[menu_id] = new Array();
  }
  ' . js_array_push("menu_onchange[menu_id]","handler") . '
}
function execute_onchange(menu) {'; /* fuehrt die eingehaengten onChange-Handler
					des Menues menu aus */
  $javascript .= '
  var id = menu.id;
  var handlers = menu_onchange[id];
  for (var i = 0; i < handlers.length; i++) {
    handlers[i](menu);
  }
}
function execute_onchange_by_event(event) {'; /* fuehrt die eingehaengten
				onChange-Handler des von event betroffenen
				Menues aus */
  $javascript .= '
  if (window.event) {
    event = window.event;
  }
  var target = event.srcElement;
  if (!target) {
    target = event.target;
  }
  execute_onchange(target);
}
function menu_option(shortcut,label) {'; /* erzeugt einen Menüpunkt mit dem
						Wert shortcut und der
						Beschreibung label */
  $javascript .= $debug_mark[13] . '
  var eintrag = document.createElement("option");'
  	. $debug_mark[16]
	. set_attribute('eintrag','value','shortcut','
  ') . $debug_mark[16] . '
  var text = document.createTextNode(label);
  eintrag.appendChild(text);
  return eintrag;
}' . $debug_mark[14] . '
function referencable_menu_option(shortcut,label,prefix) {';
				/* erzeugt einen Menüpunkt mit dem
					Wert shortcut und der
					Beschreibung label,
					referenzierbar durch
						prefix##shortcut*/
  $javascript .= $debug_mark[9] . '
  var id = prefix + shortcut;
  var eintrag = document.createElement("option");'
	. set_attribute('eintrag','value','shortcut','
  ') . set_attribute('eintrag','id','id','
  ') . '
  var text = document.createTextNode(label);
  eintrag.appendChild(text);
  if (disabled_menu_options.contains(id)) {' . ($old_internet_explorer ? '
    eintrag.style.color = "graytext";' : '') . '
    eintrag.disabled = true;
  }
  return eintrag;
}
function initialize_menu(menu,entries,names,initialize) {';
					/* initialisiert Menue menu mit den
						Moeglichkeiten aus dem Array
						entries, benannt mit Namen aus
						names. Trifft automatische
						Auswahl und initialisiert
						mit initialize, falls keine
						Mehrdeutigkeit besteht */
  $javascript .= '
  for (var entry = 0; entry < entries.length; entry++) {
    menu.appendChild(menu_option(entries[entry],names[entries[entry]]));
  }' . ($debug['javascript'] ? '
  alert(introspect(menu.options[1]))' : '') . '
  if ((entries.length == 1)
  	&& (menu.options[1] != null)
	&& (!menu.options[1].disabled)) {
    menu.selectedIndex = 1;
    initialize(menu);
  }
}
function initialize_referencable_menu(menu,
                                      entries,
                                      names,
                                      initialize,
                                      prefix) {';
					/* initialisiert Menue menu mit den
						Moeglichkeiten aus dem Array
						entries, benannt mit Namen aus
						names. Trifft automatische
						Auswahl und initialisiert
						mit initialize, falls keine
						Mehrdeutigkeit besteht.
						Die Menuepunkte erhalten
						Ids mit dem Praefix prefix
						zur spaeteren
						Referenzierung */
  $javascript .= '
  for (var entry = 0; entry < entries.length; entry++) {
    menu.appendChild(referencable_menu_option(entries[entry],
                                              names[entries[entry]],
                                              prefix));
  }' . ($old_internet_explorer ? '
  hook_onchange(menu.id,function (menu) {
    if (menu.options[menu.selectedIndex].disabled) {
      menu.selectedIndex = menu_selections[menu.id];
    }
  });
  menu.onchange = execute_onchange_by_event;
  menu.onfocus = function () {
    menu_selections[this.id] = this.selectedIndex;
  }' : '') . '
  if ((entries.length == 1) && (!menu.options[1].disabled)) {
    menu.selectedIndex = 1;
    initialize(menu);
  }
}' . (($debug["old-debug"] || $debug["ajax"]) ? '
function trace(message) {' . ($old_internet_explorer ? '
  alert(message);' : '
  var debug_paragraph = document.getElementById("debug_output");
  if (debug_paragraph == null) {
    debug_paragraph = document.createElement("p");
    debug_paragraph.style.class = "hinweis";
    document.getElementsByTagName("body")[0].appendChild(debug_paragraph);
  }
  debug_paragraph.appendChild(document.createTextNode(message));
  debug_paragraph.appendChild(document.createElement("br"));') . '
}' : '') . '
function complementary_entry(entry,base) {';
				/* erzeugt den Id des entry entsprechenden
					Menuepunkts im dazugehoerigen
					Alternativmenue. Benoetigt den Basename
					fuer das Menue, der vor dem
					Alternativ-Suffix steht, uebergeben als
					base */
  $javascript .= '
  if (entry.match("_alternative")) {
    return entry.replace("_alternative","");
  } else {
    return entry.replace(base,base + "_alternative");
  }
}
function disable_menu_alternative(menu) {'; /* passt die
  						Auswahlmöglichkeiten
						für die Wahl der Alternative
						an den im Menue menu
						gewählten Kurs an.*/
  $javascript .= '
  var entry = menu.options[menu.selectedIndex];' . ($debug["old-debug"] ? '
  trace("entry type: " + introspect(entry));
  var attribute_list = "";
  var attributes = entry.attributes;
  for (var i = 0;i < attributes.length;i++) {
    attribute_list = attribute_list
    		   + attributes[i].name
		   + "="
		   + attributes[i].value
		   + " ";
  }
  trace("entry attributes: " + attribute_list);' : '') . '
  var entry_id = entry.getAttribute("id");
  var complementary_id = complementary_entry(entry_id,
                                             "termin_kurs");'
	. ($debug["old-debug"] ? '
  trace("entry id: " + entry_id);
  trace("complementary id: " + complementary_id);' : '') . '
  var prefix = complementary_id.replace(/[0-9]*$/,"");'
  	. ($debug["old-debug"] ? '
  trace("prefix: " + prefix);' : '') . '
  disabled_menu_options.remove_from(new Prefixed_Number_Set(prefix));
  disabled_menu_options.add_element(complementary_id);
}';
?>
<?=$javascript?>
Return current item: Course-manager