// General script code for use in the website of Fanfare Juliana Munstergeleen
// Copyright 2006-2007 Theo Nijssen.

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is.ie5up to detect IE5.
var agent = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie_5  = navigator.appVersion.indexOf("MSIE 5") >= 0;

var is_nav  = ((agent.indexOf("mozilla")!=-1) && (agent.indexOf("spoofer")==-1)
              && (agent.indexOf("compatible") == -1) && (agent.indexOf("opera")==-1)
              && (agent.indexOf("webtv")==-1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav5up = (is_nav && (is_major >= 5));

var is_ie   = (agent.indexOf("msie") != -1);
var is_ie4up= (is_ie  && (is_major >= 4));

var is_ff   = (agent.indexOf("firefox") != -1);
var is_ff2  = (is_ff  && (is_major >= 2));

//if (is_ff) alert("Sorry, Firefox is not yet completely supported. We are working on it...");

var jul_focusedInputId = "";
var jul_selectedElementId = "";

// This is a template function that can be redefined in later script code.
function onLoad() {
  return true; // Just return true.
}

// This is a template function that can be redefined in later script code.
function onPrint() {
  return true; // Just return true.
}

// This is a template function that can be redefined in later script code.
function onSubmit() {
  return true; // Just return true.
}

// This is a template function that can be redefined in later script code.
function onClose() {
  clearStatus();
  return true; // Just return true.
}

// This is a general function that shows messages. There messages should be
// stored in hidden fields called _error, _warning and/or _info.
function showMessages(){
  var vResult = false;
  var vError = document.getElementById("_error");
  var vWarning = document.getElementById("_warning");
  var vInfo = document.getElementById("_info");
  if (vError && (vError.value != "")) {
    alert(vError.value);
    vResult = false; // There's an error, don't continue
  } else if (vWarning && (vWarning.value != "")) {
    alert(vWarning.value);
    vResult = true; // There's a warning but continue
  } else if (vInfo && (vInfo.value != "")) {
    alert(vInfo.value);
    vResult = true; // There's just some information to show, so continue
  } else
    vResult = true; // There's nothing to show, so continue
  return vResult;
}

// This is a general function that is executed on page load.
function doLoad() {
  clearStatus();
  var vResult = showMessages();  // Show messages
  if (vResult==true) { // Continue?
    vResult = onLoad(); // Call the template onLoad function.
    if (vResult==true) { // Continue?
      if (jul_selectedElementId != "") {
        var element = document.getElementById(jul_selectedElementId);
        if (element) element.scrollIntoView();
      }
      if (jul_focusedInputId != "") {
        var input = document.getElementById(jul_focusedInputId);
        if (input) input.focus();
      }
      window.focus(); // Focus this window
    }
  }
  return vResult;
}

// This is a general function that is executed when the print button is being clicked..
function doPrint() {
  var vResult = onPrint(); // Call the template onPrint function.
  if (vResult==true) { // Continue?
    window.print(); // Print this window
    vResult = false; // Printed, so don't continue
  }
  return vResult;
}

// This is a general function that is called by the Submit button.
function doSubmit() {
  //TODO: Check if all required fields are filled
  vResult = onSubmit(); // Call the template onSubmit function.
  if (vResult==true) setStatus("Even geduld aub...");
  return vResult;
}

// This is a general function that is called by the Close button.
function doClose() {
  vResult = onClose(); // Call the template onClose function.
  if (vResult==true) { // Continue?
    clearStatus();
    window.close();
  }
}

var parentWindow = null;
if (window.dialogArguments)
  parentWindow = window.dialogArguments[0];
else if (window.arguments)
  parentWindow = window.arguments[0];

function doReloadParentAndClose(id) {
  vResult = onClose(); // Call the template onClose function.
  if (vResult==true) { // Continue?
    if (parentWindow && parentWindow.doReload) {
       if (id && (id!=""))
         parentWindow.setTimeout("doReload(\""+id+"\")",100);
       else
         parentWindow.setTimeout("doReload()",100);
    }
    window.close();
  }
}

function makeUrl(aUrl,aQueryName,aQueryValue) {
  var vUrlParts = aUrl.split("?");
  if (vUrlParts.length==1) {
    if (aQueryValue!="")
      vUrl = vUrlParts[0] + "?" + aQueryName + "=" + aQueryValue;
    else
      vUrl = vUrlParts[0];
  } else {
    var vQueryParts = vUrlParts[1].split("&");
    var vFound = false;
    for (var i=0; i < vQueryParts.length; i++) {
      var vValue = vQueryParts[i].split("=");
      if (vValue[0].toLowerCase()==aQueryName.toLowerCase()) {
        vQueryParts[i] = aQueryName + "=" + encodeURIComponent(aQueryValue);
        vFound = true;
      }
    }
    if (!vFound)
      vQueryParts[vQueryParts.length] = aQueryName + "=" + encodeURIComponent(aQueryValue);
    vUrl = vUrlParts[0] + "?" + vQueryParts.join("&");
  }
  return vUrl;
}

function displayWindow(aUrl, aObject, aWidth, aHeight, aCenter, aResizable, aScrollbars, aMenubar, aStatusBar) {
  if (is_ie) {
    var vFeatures = "dialogWidth:"+aWidth+"px;dialogHeight:"+aHeight+"px;center:"+aCenter+";resizable:"+aResizable+";scroll:"+aScrollbars+";status:"+aStatusBar;
    return showModalDialog(aUrl,Array(window,aObject),vFeatures);
  } else {
    var vFeatures = "modal=yes,chrome=no,width="+aWidth+",height="+aHeight+",center="+aCenter+",resizable="+aResizable+",scrollbars="+aScrollbars+",menubar="+aMenubar+",status="+aStatusBar+",toolbar=no,location=no";
    vWindow = window.openDialog(aUrl,"jul_dialog",vFeatures,window,aObject);
//    vWindow = window.open(aUrl,"jul_dialog",vFeatures);
    alert(vFeatures);
  }
}

function showModal(aUrl, aObject, aWidth, aHeight, aScroll){
  return displayWindow(aUrl,aObject,aWidth,aHeight,"1","0",aScroll,"0","0");
}

function openCalendar(aRoot,aEdit) {
  showModal(aRoot+"algemeen/calendar.php",aEdit,"300","350","0");
}

function openWindow(aUrl, aWidth, aHeight) {
  var vFeatures = "width="+aWidth+",height="+aHeight+",center=yes,resizable=no,scrollbars=yes,menubar=no,status=no,toolbar=no,location=no";
  vWindow = window.open(aUrl,"jul_dialog",vFeatures);
}

// Extend the String type with a CaseInsensitive indexOf function
String.prototype.IndexOf = function(aStr,i) {
  if (!i) var i = 0;
  return this.toLowerCase().indexOf(aStr.toLowerCase(),i);
}

// Extend the String type with a Capitalize function
String.prototype.Capitalize = function() {
  return this.slice(0,1).toUpperCase() + this.slice(1);
}

// Caseinsensitive function that returns true if the text starting on position i of aText equals aStr
function nextStrIs(aText,i,aStr) {
  return (aText.IndexOf(aStr,i)==i?true:false);
}

// Caseinsensitive function that returns true if the text ending on position i of aText equals aStr.
function prevStrIs(aText,i,aStr) {
  return (aText.IndexOf(aStr,i-aStr.length)==i-aStr.length?true:false);
}

var gComma = ", ";
var gAnd = " en "; // Dutch, specify " and " for English language

// Caseinsensitive function that returns true if aText contains aWord.
function findWordInText(aText,aWord) {
  while (true) {
    var i = aText.IndexOf(aWord,0);
    if (i<0) return false;
    var j = i + aWord.length;
    if (((j==aText.length)||nextStrIs(aText,j,gComma)||nextStrIs(aText,j,gAnd))&&
        ((i==0)||prevStrIs(aText,i,gComma)||prevStrIs(aText,i,gAnd))) return true;
  }
}

// Caseinsensitive function that explodes a string like "a, b and c" into an
// array of strings.
function textToArray(aText) {
  var vResult = new Array();
  var vWord = "";
  var i = 0;
  while (i<aText.length) {
    var j = aText.IndexOf(gComma,i);
    if (j!=0) {
      vWord = aText.slice(i,j);
      i += gComma.length;
    } else {
      j = IndexOf(aText,gAnd,i);
      if (j!=0) {
        vWord = aText.slice(i,j);
        i += gAnd.length;
      } else {
        vWord = aText.slice(i);
      }
    }
    vWord = vWord.Capitalize();
    vResult[vResult.length] = vWord;
    i += aText.length;
  }
  return vResult;
}

// function that explodes a string like "a;b;c" into an
// array of strings.
function stringToArray(aText,aSep) {
  var vResult = new Array();
  var vWord = "";
  var i = 0;
  while (i<aText.length) {
    var j = aText.indexOf(aSep,i);
    if (j>=0) {
      vWord = aText.slice(i,j);
      i = j + aSep.length;
    } else {
      vWord = aText.slice(i);
      i = aText.length;
    }
    vResult[vResult.length] = vWord;
  }
  return vResult;
}

// Function that implodes an array of strings into a single string like
// "a;b;c".
function arrayToString(aArray,aSep) {
  var vResult = "";
  for (var i=0;i<aArray.length;i++) {
    if (i==0) {
      vResult += aArray[i];
    } else {
      vResult += aSep;
      vResult += aArray[i].toLowerCase();
    }
  }
  return vResult;
}

// Function that implodes an array of strings into a single string like
// "a, b and c".
function arrayToText(aArray) {
  var vResult = "";
  for (var i=0;i<aArray.length;i++) {
    if (i==0) {
      vResult += aArray[i];
    } else {
      if (i<aArray.length-1)
        vResult += gComma;
      else
        vResult += gAnd;
      vResult += aArray[i].toLowerCase();
    }
  }
  return vResult;
}

function jul_sendtext(e, text) {
  e.value += text;
}

function jul_popup(aURL,aWidth,aHeight) {
  var vWidth = (aWidth?aWidth:437);
  var vHeight = (aHeight?aHeight:500);
  var vHandle = window.open(aURL, null, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+vWidth+',height='+vHeight,true);
}

// Get the value of the cookie with the specified name.
function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      if (aCrumb[1]) {
        return unescape(aCrumb[1]);
      } else {
        return "";
      }
  }

  // a cookie with the requested name does not exist
  return "";
}

// Create a cookie with the specified name and value.
// The cookie expires at the end of the 21st century.
function setCookie(sName, sValue)
{
//  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

// Deletes the cookie with the specified name.
// Note: the cookie is not deleted until the browser closes.
function deleteCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function clearStatus() {
  if (document.getElementById("statusdiv")) {  
    document.getElementById("statusdiv").style.visibility = "hidden";
    document.getElementById("statustext").innerHTML = "";
  }
}

function setStatus(sMessage) {
  if (sMessage=="")
    clearStatus();
  else if (document.getElementById("statusdiv")) {
    document.getElementById("statustext").innerHTML = sMessage;
    document.getElementById("statusdiv").style.visibility = "visible";
  }
}

function sendEmailSync(aEmail,aSubject) {
  var vUrl = getEmailSync(aEmail,aSubject);
  if (vUrl.substr(0,7)!='mailto:')
    alert(vUrl);
  else
    window.location.href = vUrl;
}

function setInputError(name,error)
{
  var span = document.getElementById('inputmessage' + name);
  if (span) {
    span.className = 'inputerror';
    span.innerHTML = error;  
  }
}

function setInputWarning(name,warning)
{
  var span = document.getElementById('inputmessage' + name);
  if (span) {
    span.className = 'inputwarning';
    span.innerHTML = warning;  
  }
}

function setInputInfo(name,info)
{
  var span = document.getElementById('inputmessage' + name);
  if (span) {
    span.className = 'inputinfo';
    span.innerHTML = info;  
  }
}

function hideInputMessage(name)
{
  var span = document.getElementById('inputmessage' + name);
  if (span) {
    span.className = 'hideinputmessage';
    span.innerHTML = '';  
  }
}

var oPopup = null;

function hideInputHint(name)
{
  var div = document.getElementById('inputhint' + name);
  if (div) {
    if (oPopup != null) oPopup.hide();
  }
}

function showInputHint(name)
{
  var div = document.getElementById('inputhint' + name);
  if (div) {
    if (oPopup == null) 
    {
      oPopup = window.createPopup();
    }
    var content = "<html><head><link type='text/css' rel='stylesheet' href='/css/juliana.css' /></head><body class='hintbody'><div id='hintdiv' class='hintdiv'";
    content += div.innerHTML;
    content += "</div></body></html>";
    var doc = oPopup.document;
    doc.open("text/html","replace");
    doc.write(content);
    doc.close();
    var iWidth = 500;
    oPopup.show(0,0,iWidth,0);
    var hintdiv = doc.all['hintdiv'];
    iWidth = hintdiv.offsetLeft + hintdiv.offsetWidth + 8;
    oPopup.hide();
    oPopup.show(0,0,iWidth,0);
    var iHeight = hintdiv.offsetTop + hintdiv.offsetHeight + 8;
    oPopup.hide();
    oPopup.show(8,window.event.srcElement.offsetHeight+4,iWidth,iHeight,window.event.srcElement);   
  }
}

