var url = "http://www.ejai.com/";

var cSubmenu   = null;
var cJourney   = null;
var cJourneyId = 1;

var allJourneys = new Array("fun", "leadership", "balance", "happiness", "flash", "destination_now", "destination_x");
var allAbout    = new Array("ejai", "team", "mission", "inspiration", "contact");
var allFAQ      = new Array("faq_whatisajourney.txt", "faq_howtobuy.txt", "faq_whileonajourney.txt", "faq_contact.txt", "faq_sharingexperiences.txt");

var actionCodes = new Array();
var discounts = new Array()


function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

/********************
GLOBALLY USED METHODS
Author: A.G. Gideonse
Website: http://www.xirtcms.com
********************/
function setOpacity(obj, v)
{
   if (!v) v = 1; 

   var cNode = null;
   var pNode = obj.parentNode;
   for (var i = 0; i < pNode.childNodes.length; i++)
   {
      cNode = pNode.childNodes[i];

      if (cNode.className && cNode.className == 'bg')
      {
         if (navigator.appName == "Microsoft Internet Explorer")
            cNode.style.filter = "alpha(opacity = " + (v * 100) + ")";
         else
            cNode.style.opacity = v;
      }
   }

}

function restoreOpacity(obj, v)
{
   var pNode = obj.parentNode;

   var cNode = null;
   for (var i = 0; i < pNode.childNodes.length; i++)
   {
      cNode = pNode.childNodes[i];

      if (cNode.className && cNode.className == 'bg')
      {
         if (navigator.appName == "Microsoft Internet Explorer")
            cNode.style.filter = "alpha(opacity = " + (v * 100) + ")";
         else
            cNode.style.opacity = v;
      }
   }
}

function setBackground(obj, v)
{
   if (!v) return;

   var cNode = null;
   var pNode = obj.parentNode;
   for (var i = 0; i < pNode.childNodes.length; i++)
   {
      cNode = pNode.childNodes[i];

      if (cNode.className && cNode.className == 'bg')
      {
         cNode.style.background = v;
      }
   }
}

function restoreBackground(obj, v)
{
   var pNode = obj.parentNode;
   
   var cNode = null;
   for (var i = 0; i < pNode.childNodes.length; i++)
   {
      cNode = pNode.childNodes[i];

      if (cNode.className && cNode.className == 'bg')
         cNode.style.background = v;
   }
}

function goLanguage(dir)
{

   cLocation = new String(document.location.href);
   locArr = cLocation.split('/');

   if (dir)
      document.location.href = url + dir + "/" + locArr[locArr.length - 1];
   else
      document.location.href = url + locArr[locArr.length - 1];
}

/********************
PAGE SPECIFIC METHODS
Author: A.G. Gideonse
Website: http://www.xirtcms.com
********************/
function swapImage(id)
{
   document.getElementById("lImg").src = "images/journeys/" + cJourney + "_"  + cJourneyId + "." + id + ".jpg";
}

function swapTargetImage(img, target)
{
   document.getElementById(target).src = img;
}

function swapContent(txt, xml)
{
   document.getElementById("content").innerHTML = txt;
}

function swapReview(txt, xml)
{
   document.getElementById("review").innerHTML = txt;
}

function showSubmenu(id)
{
   return loadJourney(allJourneys[id], 1, document.getElementById(allJourneys[id] + "_1"));

   for (var i = 0; document.getElementById("div_right_submenu_" + i); i++)
      document.getElementById("div_right_submenu_" + i).style.display = "none";

   if (id != cSubmenu)
   {
      loadJourney(allJourneys[id], 1, document.getElementById(allJourneys[id] + "_1"));
      document.getElementById("div_right_submenu_" + id).style.display = "block";
      cSubmenu = id;
   }
   else
   {
      cSubmenu = null;
   }
}

function setMenuItem(obj)
{
   // Restyle all menuitems to normal
   aTags = document.getElementsByTagName("a");
   for (var i = 0; i < aTags.length; i++)
   {
      if (aTags[i].className && aTags[i].className == "jitem")
         aTags[i].style.fontStyle = "normal";
   }

   // Restyle current menuitem
   obj.style.fontStyle = "italic";
}

function loadFAQ(file)
{
   if (navigator.appName == "Microsoft Internet Explorer")
      XAjax('texts/ie_only/' + file, '', swapContent);
   else
      XAjax('texts/' + file, '', swapContent);
}
function loadJourney(name, id, obj)
{
   cJourney   = name;
   cJourneyId = id;

   // Set menuitem
   setMenuItem(obj);

   // Replace small images
   for (var i = 0; i < 6; i++)
      swapTargetImage("images/journeys/" + name + "_"  + id + "." + i + "_s.jpg", "sImg_" + i);

   // Replace big image
   swapImage(0);

   document.getElementById("content_bg").style.height = '300px';
   document.getElementById("content").style.height = '280px';
   XAjax("texts/journeys/" + name + "_" + id + ".txt", "", swapContent);
   XAjax("texts/journeys/reviews/" + name + "_" + id + ".txt", "", swapReview);
}

function expandJourney(button, h)
{
   button.style.display = "none";

   document.getElementById("div_readmore").style.display = "block";

   document.getElementById("content_bg").style.height = h + 'px';
   document.getElementById("content").style.height = (h - 20) + 'px';
}

function contractJourney()
{
   document.getElementById("morebutton").style.display = "block";

   document.getElementById("div_readmore").style.display = "none";

   document.getElementById("content_bg").style.height = '300px';
   document.getElementById("content").style.height = '280px';
}

function loadFormContent(txt, xml)
{
   document.getElementById("div_form").innerHTML = txt;
}

function buyJourney()
{
   document.getElementById("div_formbg").style.display = "block";
   document.getElementById("div_formbgholder").style.display = "block";
   document.getElementById('div_buyform').src = "order/index.php?lang=en_GB";
}

function hideBuyJourney()
{
   document.getElementById("div_formbg").style.display = "none";
   document.getElementById("div_formbgholder").style.display = "none";
   document.getElementById('div_buyform').src = "about:blank";
}


function openFAQ(obj, id)
{
   obj.style.fontStyle = "italic";
   document.getElementById("faq_answer_" + id).style.display = "block";
}

function closeFAQ(obj, id)
{
   document.getElementById("faq_question_" + id).style.fontStyle = "normal";
   obj.parentNode.style.display = "none";
}


/****************
PAGE INIT METHODS
Author: A.G. Gideonse
Website: http://www.xirtcms.com
****************/
function initHome()
{
   XAjax("texts/home.txt", "", swapContent);
}

function initJourney()
{
   var cInit = 0;
   var regex = new RegExp("[\\?&]jid=([^&#]*)");

   var res = regex.exec(window.location.href);
   if (regex.exec(window.location.href) != null)
      cInit = res[1];

   loadJourney(allJourneys[cInit], 1, document.getElementById(allJourneys[cInit] + "_1"));
   showSubmenu(cInit);
}


function initJourneyInformation()
{
   setMenuItem(document.getElementById("journey_0"));
   XAjax("texts/journeyinfo_what.txt", "", swapContent);
}

function initFAQs()
{
   var cInit = 0;
   var regex = new RegExp("[\\?&]jid=([^&#]*)");

   var res = regex.exec(window.location.href);
   if (regex.exec(window.location.href) != null)
      cInit = res[1];

   setMenuItem(document.getElementById("faq_" + cInit));
   loadFAQ(allFAQ[cInit]);
}

function initGifts()
{
   setMenuItem(document.getElementById("gifts_0"));
   XAjax("texts/gifts_gift.txt", "", swapContent);
}

function initPress()
{
   setMenuItem(document.getElementById("press_0"));
   XAjax("texts/press_press.txt", "", swapContent);
}

function initBusiness()
{
   setMenuItem(document.getElementById("business_0"));
   XAjax("texts/business_business.txt", "", swapContent);
}

function initAmstelwijs()
{
   setMenuItem(document.getElementById("business_0"));
   XAjax("texts/business_amstelwijs.txt", "", swapContent);
}

function initNovagraaf()
{
   setMenuItem(document.getElementById("business_0"));
   XAjax("texts/business_novagraaf.txt", "", swapContent);
}

function initDutch()
{
   setMenuItem(document.getElementById("business_0"));
   XAjax("texts/business_dutch.txt", "", swapContent);
}

function initSuperserver()
{
   setMenuItem(document.getElementById("business_0"));
   XAjax("texts/business_superserver.txt", "", swapContent);
}

function initAbout()
{
   var cInit = 0;
   var regex = new RegExp("[\\?&]jid=([^&#]*)");

   var res = regex.exec(window.location.href);
   if (regex.exec(window.location.href) != null)
      cInit = res[1];

   setMenuItem(document.getElementById("about_" + cInit));
   XAjax("texts/about_" + allAbout[cInit] + ".txt", "", swapContent);
}

function initEmailFriends()
{
   XAjax("texts/email_friends.txt", "", swapContent);
}

function initTryout()
{
   XAjax("texts/tryout_start.txt", "", swapContent);
}

function getTryoutCode() {
   XAjax("tryoutCode.php", "", placeTryout);
}

function placeTryout(code) {
   document.getElementById("code").value = code;
   document.forms['tryoutForm'].submit();
}

/*************
XAjax - A XirtCMS production
Function: addListener
Author: A.G. Gideonse
Website: http://www.xirtcms.com
*************/
var addListener = function()
{
   if (window.addEventListener)
   {
      return function(obj, type, func)
      {
         obj.addEventListener(type, func, false);
      };
   }
   else if (window.attachEvent)
   {
      return function(obj, type, func)
      {
         var f = function()
         {
            func.call(obj, window.event);
         };

         obj.attachEvent('on' + type, f);
      };
   }
   else
   {
      return function(obj, type, func)
      {
         element['on' + type] = func;
      }
   }
}();

/*************
XAjax - A XirtCMS production
Function: XAjax
Author: A.G. Gideonse
Modifier: A.G. Gideonse
Website: http://www.xirtcms.com
*************/
var XAjax = function()
{
   // Returns an XMLHttpRequest Object (or error)
   var getHTTPHandler = function()
   {
      var xmlHttp;
      try
      {
         xmlHttp = new XMLHttpRequest;
         getHTTPHandler = function()
         {
           return new XMLHttpRequest;
         };
      }
      catch(e)
      {
         var msxml = ["MSXML2.XMLHTTP.3.0",
                      "MSXML2.XMLHTTP",
                      "Microsoft.XMLHTTP"];

         for (var i=0, len = msxml.length; i < len; ++i)
         {
            try
            {
               xmlHttp = new ActiveXObject(msxml[i]);
               getHTTPHandler = function()
               {
                  return new ActiveXObject(msxml[i]);
               };

               break;
            }
            catch(e)
            {
               alert('ERROR: AJAX not supported...');
            }
         }
      }
      return xmlHttp;
   };

   // Start communication using AJAX
   return function(file, params, cFunction)
   {
      var onReadyStateChange = function(xmlHttp)
      {
         if (xmlHttp.readyState == 4)
         {
            if(xmlHttp.status == 200 && xmlHttp.statusText == "OK")
               return onComplete(xmlHttp.responseText, xmlHttp.responseXML);
            
            if (xmlHttp.status == 0)
               return;

            return alert("XAjax Warning: xmlHttp status is '" + xmlHttp.status + "'");
         }
         
         return true;
      };

      var xmlHttp = getHTTPHandler();
      if (xmlHttp)
      {
         var onComplete = cFunction;

         // Check if we are ready to do a new request
         if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
         {
            xmlHttp.open('POST', file, true);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            xmlHttp.onreadystatechange = function()
            {
               onReadyStateChange(xmlHttp);
            };

            xmlHttp.send(params);
         }
      }
   };
}();