// global.js
// Copyright © by Thomas A. Osthege. All Rights Reserved.
// 16.8.2007

// window.onError= null;

// Get param from browsers infoline (behind '?')
function getPar(paramname) {
  var searchval= window.location.search;

  searchval= unescape(searchval);
  searchval= '&' + searchval.substr(1, searchval.length - 1);

  var valpos= searchval.toLowerCase().indexOf('&' + paramname.toLowerCase() + '=');
  var valposstring= '';

  if (valpos > -1) 
  	valposstring= searchval.substring(valpos + paramname.length + 2, searchval.length);

  var endpos= valposstring.indexOf('&');
  var parval= '';
  
  if (endpos < 0) 
  	endpos= valposstring.length;

  if (endpos > 0) 
  	parval= valposstring.substring(0, endpos);

  return parval;
 }

// toggle display status of a div
function doToggle(theItem) {
  if (document.getElementById(theItem).style.display == '')
    document.getElementById(theItem).style.display = 'none';
  else
    document.getElementById(theItem).style.display = '';
}

function displayTheLastModifiedDate() {
  var LastModifiedDateAndTime = new Date(document.lastModified)
  var LastModifiedDay = LastModifiedDateAndTime.getDate()
  var LastModifiedMonth = (LastModifiedDateAndTime.getMonth() + 1) // You must add one because it returns the previous month.
  var LastModifiedYear = LastModifiedDateAndTime.getYear()
  var LastModifiedDate = ''
  if (LastModifiedDay < 10)
    LastModifiedDay = '0' + LastModifiedDateAndTime.getDate()
  if (LastModifiedMonth < 10)
    LastModifiedMonth = '0' + (LastModifiedDateAndTime.getMonth() + 1)
  if (LastModifiedYear < 100)
    LastModifiedYear = '20' + LastModifiedDateAndTime.getYear()
  LastModifiedDate = LastModifiedDay + '/' + LastModifiedMonth + '/' + LastModifiedYear
  document.write(LastModifiedDate)
 }

// Displays the URL of the current page (not frame page)
function displayTheDocumentNameAndPath() {
  var DocumentNameAndPath = new String(document.location)
  document.write(DocumentNameAndPath)
}


// Replaces the active document with the specified URL
function goToURL(URL) {
  if (URL == '[object]')
    document.location.href= URL.options[URL.selectedIndex].value;
  else
    document.location.href= FileLocation = URL;
}


// Navigates back one throught the History list
function goBack() {
  parent.history.back()
}

function openPopupWindow(URL) {
  window.open(URL,'PopupWindow','LEFT=10,Top=10,HEIGHT=380,WIDTH=610,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,directories=no,menubar=no,location=no');
}

function openHelpWindow(URL) {
  window.open(URL,'HelpWindow','LEFT=110,Top=110,HEIGHT=380,WIDTH=280,resizable=no,scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no,location=no');
}

function changeBackColor(Element, Color) {
  Element.style.background = Color;
}


// Test theField for theSize Characters
function testField(theField,theSize,theName,theLang) {
  with(theField) {
    if (value == '') {
      if (theLang == 'de')
        alert('Bitte geben Sie einen Wert für "'+theName+'" an.');
      else
        alert('Please enter a value for "'+theName+'".');

      focus();
      return false;
    }

    if (value.length < theSize) {
      if (theLang == 'de')
        alert('"'+theName+'" muss mindestens '+theSize+' Zeichen lang sein.');
      else
        alert('"'+theName+'" must have at least '+theSize+' Characters.');

      focus();
      return false;
    }
  }
  return true;
}


// Test theField for a valid email address
function testEmail(theField,theField2,theLang) {
  if (theField.value == theField2.value) {
//  var isEmail = /^([a-zA-Z0-9-+&_\/]+)(\.[a-zA-Z0-9-+&_\/]+)*@([a-zA-Z0-9-_]+\.)+([a-zA-Z]{2,6})$/;
//  if (isEmail.test(theField.value))
    if (isRFC822ValidEmail(theField.value))
      return true;
    else {
      if (theLang == 'de')
        alert('Die angegebene Emailadresse ist nicht gültig.');
      else
        alert('The email address you entered is not valid.');
    }
  } else {
    if (theLang == 'de')
      alert('Die beiden Emailadressen sind nicht gleich. Bitte überprüfen Sie Ihre Eingabe!');
    else
      alert('The two email addresses are not equal. Please examine your input!');
  }
  focus();
  return false;
}


// Test Email field and submit
function testEmailAndSubmit(theLang) {
  with(document.Form1) {
    if (testEmail(email,emailconfirm,theLang)) {
      submit();
      return true;
    }
  }

  focus();
  return false;
}


// from http://rosskendall.com/blog/web/javascript-function-to-check-an-email-address-conforms-to-rfc822
function isRFC822ValidEmail(sEmail) {
  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
  
  var reValidEmail = new RegExp(sValidEmail);
  
  if (reValidEmail.test(sEmail)) return true;
  
  return false;
}


// parse a URL to form an object of properties (from http://brothercake.com/)
function parseURL(url)
{
    //save the unmodified url to href property
    //so that the object we get back contains
    //all the same properties as the built-in location object
    var loc = { 'href' : url };

    //split the URL by single-slashes to get the component parts
    var parts = url.replace('//', '/').split('/');

    //store the protocol and host
    loc.protocol = parts[0];
    loc.host = parts[1];

    //extract any port number from the host
    //from which we derive the port and hostname
    parts[1] = parts[1].split(':');
    loc.hostname = parts[1][0];
    loc.port = parts[1].length > 1 ? parts[1][1] : '';

    //splice and join the remainder to get the pathname
    parts.splice(0, 2);
    loc.pathname = '/' + parts.join('/');

    //extract any hash and remove from the pathname
    loc.pathname = loc.pathname.split('#');
    loc.hash = loc.pathname.length > 1 ? '#' + loc.pathname[1] : '';
    loc.pathname = loc.pathname[0];

    //extract any search query and remove from the pathname
    loc.pathname = loc.pathname.split('?');
    loc.search = loc.pathname.length > 1 ? '?' + loc.pathname[1] : '';
    loc.pathname = loc.pathname[0];

    //return the final object
    return loc;
}
