
//
// ---------- FUNCTIONS ----------
//

/*
addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events
*/
function addEvent(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent) {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
}

function initGeneral(){
  var i,k;

/*  
  // --- Find all SlideMenu container divs and switch them on (display: block) ---
  var headerInner = document.getElementById('divheaderinner');
  if(headerInner!=undefined){
    var divs = headerInner.getElementsByTagName('div');
    // find the mmContainer divs and set their display style from 'none' (as defined in ypMenu.css) to 'block'
    for (i = 0; i < divs.length; i++) {
      if (/\bmmContainer\b/.test(divs[i].className))  divs[i].style.display = 'block';
    }
  }
*/
  
  // ---
}


// Clear the default input of an input text field when it receives focus. Save the original input in this.orgValue.
function defaultInputFocus(me){
  if(me.orgValue==undefined)  me.orgValue=me.value;
  if(me.value==me.orgValue)  me.value='';
}
// Restore the original value of an input text field when it looses focus and nothing has been entered.
function defaultInputBlur(me){
  if(trim(me.value)=='')  me.value=me.orgValue;
}


function trim(st){
  return st.replace(/^\s*(.*\S)?\s*$/, '$1');
}


function InsertFlash(flashURL, w, h, version){
  if(version=='')  version='6,0,0,0';
  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+w+'" height="'+h+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'">');
  document.write('<param name="allowScriptAccess" value="sameDomain" />');
  document.write('<param name=movie value="'+flashURL+'" />');
  document.write('<param name="quality" value="high" />');
  document.write('<param name="wmode" value="opaque" />');  // this allows DHTML elements in front of Flash and prevents screenreaders (like JAWS) from getting stuck of Flash
  document.write('<embed src="'+flashURL+'" type="application/x-shockwave-flash" quality="high" width="'+w+'" height="'+h+'" wmode="opaque" allowScriptAccess="sameDomain" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
  document.write('</object>');
}


function linkTileTo(url, target){
  // so far we do not obey the target. 
  // it can have one of these patterns:
  //   ''                no target - solved!
  //   '_self'           same as above of self.location.href
  //   '_parent'         parent.location.href
  //   '_top'            top.location.href
  //   '_blank'          JS popup target - window.open(url, '_blank')
  //   '300x600'         JS popup target - window.open(url, '_blank', 'width=300,height=600') - gleiche Lösung/Parameter wie Typo3-Routinen wählen!!!
  
  // ie does not honor the <base href=...> tag in JS, so we have to add the base ourselves
  var b = document.getElementsByTagName('base');
  if (b && b[0] && b[0].href){
    if (b[0].href.substr(b[0].href.length-1) == '/' && url.charAt(0) == '/')  url=url.substr(1);
    url = b[0].href + url;
  }
  
  location.href = url;
  return true;
}


// Special for GSHC card order forms
// calculates the total price and stores it in a hidden field, a hidden autoresponder field, and a visible span.
function mailFormChanged(obj){
  if(document.mailform.mailformAnzahl && document.mailform.Versandart){
    var cardcnt   = parseInt(document.mailform.mailformAnzahl.options[document.mailform.mailformAnzahl.selectedIndex].value);
    var sendcards = (document.mailform.Versandart[1].checked?1:0);
    var price = Math.round((cardcnt*gCardPrice + sendcards*gSendPrice)*100).toString();
    var pricelen = price.length;
    price = price.substr(0,pricelen-2) + ',' + price.substr(pricelen-2)
    price = price.replace(/,00/g, '');
    
    document.mailform.gesamtkosten.value = price + 'EUR';
    document.mailform.auto_respond_msg.value = 'Bestaetigung Ihrer Kartenbestellung //Vielen Dank! /Wir haben Ihre Kartenbestellung erhalten. / / /Bitte ueberweisen Sie den Gesamtbetrag von '+price+' EUR per Vorauskasse auf folgendes Konto: / /    Stephan Wolsdorfer /    Sparda Bank Suedwest /    BLZ:  550 905 00 /    Konto-Nr.:  6 099 335 / /Wenn Sie als Versandart "Hinterlegen" gewaehlt haben, werden die Karten auf Ihren Namen an der Abendkasse hinterlegt. /Der Kartenversand oder die Kartenhinterlegung erfolgt ausschliesslich nach Eingang des Gesamtbetrages! / / /Sehr wichtig: / /Bitte vermerken Sie unbedingt Ihren Namen und das Stichwort "GSHC-Onlinebestellung" auf Ihrer Ueberweisung! / / /Der Gemischte Saarbruecker Herrenchor. /www.gemischter-herrenchor.de';
    document.getElementById('totalcost').innerHTML = price;
  }
  
  return true;
}


//
// ---------- MAIN ----------
//

// Set the following global variables:
// gBrowser, gVersion, gSystem, gNS4

function NextChar(st,findst){
  //Returns 0 if findst is not found, else return charpos following substring.
  //Result is also stored in variable NextCharPos (which is local to outer function block).
  var p=st.indexOf(findst);
  return NextCharPos=(p<0?0:p+findst.length);
}

var NextCharPos;
var UA = navigator.userAgent.toLowerCase();

if (NextChar(UA,'konqueror')){
  gBrowser="konqueror"; gSystem="linux";
}else if(NextChar(UA,'opera')) 
  gBrowser="opera"
else if(NextChar(UA,'webtv'))
  gBrowser="webtv";
else if(NextChar(UA,'icab'))
  gBrowser="icab"
else if(NextChar(UA,'msie'))
  gBrowser="ie"
else if(!NextChar(UA,'compatible')){
  gBrowser="ns"
  gVersion=UA.charAt(8);
}else 
  gBrowser="unknown";

if(!window.gVersion)
  gVersion=UA.charAt(NextCharPos+1);
if(!window.gSystem){
  if(NextChar(UA,'linux'))
    gSystem="linux";
  else if(NextChar(UA,'x11'))
    gSystem="unix";
  else if(NextChar(UA,'mac'))
    gSystem="mac"
  else if(NextChar(UA,'win'))
    gSystem="win"
  else gSystem="unknown";
}

gNS4=(gBrowser=="ns" && gVersion=="4");

//alert(gBrowser + gVersion)

gURL = self.location.toString();
gURLnoquery = gURL.slice(0, gURL.indexOf('?')<0 ? gURL.length+1 : gURL.indexOf('?') );
gURLfile = gURLnoquery.slice(gURLnoquery.lastIndexOf('/')+1);
gURLpath = gURLnoquery.slice(0,gURLnoquery.lastIndexOf('/'));

if(document.getElementById && document.createTextNode)
{
  addEvent(window, 'load', initGeneral);
}
