// Version       : 1.0
// Last modified : 18.08.2003
// Author        : BBDO InterOne Hamburg (W. Rauch)
// Copyright     : BMW Group 2002-2003

// Unique scripting solutions that are provided for  BMW Group sites are for use exclusively
// within  BMW Group projects.  No other use of these solutions is permitted.

//  definition of the page to be displayed by the framepage, if no parameter is passed in and
//  no filename is given in the variable "contentFrameSource" in index.html.
var defaultPage       = "content.asp"; //Default Name for the default content-Page
var framesetPage      = "default.asp";   //Name of the Frameset-Page(s)
var checkIntervall    = 100; //Check for top- an bottom-frame Intervall in ms
var initContentURL 	  = "";
var framesetStatus    = false;
var stdTimer;

function splitSearchstring() {
	parameterArray = unescape(self.location.search).substring(1).split("&");
	if (unescape(self.location.search).indexOf("=") == -1) {return;}
	for (var i=0;i<parameterArray.length;i++) {
		parameterArray[i] = parameterArray[i].split("=");
		eval("query." + parameterArray[i][0] + " = \"" + parameterArray[i][1] + "\"");
	}
}
//  Use the function execFrameAction to execute different modifications in the frames, i.e.
//  set styles for text and/or change images. Refer to the variables defined in the framepage
//  index.html. The two lines are only an example.
function execFrameAction(frameActionParameter) {
  if(parent.framesetParameter_1 != ""){
    // Execute an action using framesetParameter_1
   }
  if(parent.framesetParameter_2 != ""){
    // Execute an action using framesetParameter_2
   }
  if(parent.framesetParameter_3 != ""){
    // Execute an action using framesetParameter_3
   }
  if(parent.framesetParameter_4 != ""){
    // Execute an action using framesetParameter_4
   }
}

//  The function checkForFrameset() only sets up an interval running the function checkFrameLoadStatus()
function checkForFrameset(indexDistanceIndicator) {
  var myName = "";
  var myPath = "";
  var newLocation ="";
  if((top.frames.length == 0)||(top.frames.length == 1)){
    splitSearchstring();
    if(query.contentPath){  // if query.contentPath is available, use it as a frameset path 
      newLocation = genericpath + query.contentPath.substr(0,query.contentPath.lastIndexOf("/")+1) + framesetPage;
    } else {
      if(!isNaN(parseInt(indexDistanceIndicator))){   // if indexDistanceIndicator is available, use it to locate the nearest higher-level frameset
        myName = self.location.href.substr(0,(self.location.search?self.location.href.indexOf(self.location.search):self.location.href.length));
        myPath = myName;
        for (i=0; i <= parseInt(indexDistanceIndicator); i++) {
          myPath = myPath.substr(0,myPath.lastIndexOf("/"));
        }
        myPath += "/";
        myName = myName.substring(myPath.length, myName.length)
        newLocation = myPath + framesetPage + "?content=" + myName;
      } else {
        if(self.location.href.indexOf("include/overview")!=-1){ // if the calling page is an overview page, open the hompage instead
          myPath = self.location.href.substr(0,self.location.href.lastIndexOf("/"));
          myPath = myPath.substr(0,myPath.lastIndexOf("/")+1);
          newLocation = myPath + framesetPage;
        } else {  // in all othe cases, extract the filename and append it to the frameset page in that folder
          myPath = self.location.href.substr(0,self.location.href.lastIndexOf("/")+1);
          myName = self.location.href.substr(self.location.href.lastIndexOf("/")+1,self.location.href.length);
          newLocation = myPath + framesetPage + "?content=" + myName;
        }
      }
    }
    self.location.href = newLocation;
  }
  else {
    stdTimer     = window.setInterval("checkFrameLoadStatus()",checkIntervall);
  }
}

//  The functions setTopFrame & setTopFromJumppage 
//  are used to access the topframe via executing a function 
//  that manipulates the top frame.
//  Both are triggered at every checkIntervall ms by the setInterval() call in
//  checkForFrameset() and jumpCheck() respectively UNTIL the condition
//  in the if() statement is met and the variables holding the 
//  timers are cleared within.
function checkFrameLoadStatus() {
  if((framesetStatus)&&(top.frames.length != 0)) {
    if(topFrame.preLoadIsComplete && bottomFrame.preLoadIsComplete) {
      window.clearInterval(stdTimer);
      execFrameAction('checkFrameLoadStat');
    }
   }  
}

//  The function pageHandler() checks which page to display in the contentframe in this order:
//  1: Querystring or hard coded page name in "contentFrameSource" if it contains "://"?
//  2: hard coded page name in "contentFrameSource"?
//  If no file specified, it displays the page defined in variable "defaultPage" in the contentframe.
//
//  In case 1, if the string contains "://", indicating a disallowed external URL, the querystring 
//  will be ignored and the hardcoded target or default page are displayed instead, depending on 
//  what is defined in the index.html.
//
//  If the hardcoded target is an external page, indicated by "http://", a jumppage is used 
//  to execute the various highlighting within the topframe. Internal pages execute the
//  needed functions for topframe manipulation in the onLoad="" Event of their body-tag by calling
//  the function checkForFrameset() to eventually trigger the changes.

function pageHandler() {
  splitSearchstring();
  if(query.content){
    if(query.content.indexOf("://") != -1) {
      if(query.content.indexOf(yourDomain) != -1) {
        initContentURL = query.content;
      } else {
        if(top.contentFrameSource != defaultPage) {
          initContentURL = top.contentFrameSource;
          stdTimer       = window.setInterval("checkFrameLoadStatus()",checkIntervall);
        }
        else {
          initContentURL = defaultPage;
        }
      }
    }
    else {
      initContentURL = query.content;
    }
  } else {
    if(top.contentFrameSource != defaultPage) {
      initContentURL = top.contentFrameSource;
      stdTimer       = window.setInterval("checkFrameLoadStatus()",checkIntervall);
    }
    else {
      initContentURL = defaultPage;
    }
  }
}
