DEBUG = false;
AJAX_DEBUG=DEBUG;

var win;
var req;
var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var req;
var supportsXmlHttpReq = true;

//''*******************************************************************
//''  onErrorHandler
//''*******************************************************************
//''* 
window.onerror = function(msg, url, linenumber)
{
   alert("Error: " + msg + "\nfor: " + url + "\nat line " + linenumber);
}



////''*******************************************************************
////''  createTimeline(histories)
////''*******************************************************************
////''* 
//function createTimeline(histories)
//{
//  var div = document.getElementById("historyTimeline");
//  //''alert("div == "+div+"\n");
//
//  if (div && histories && histories.length > 0)
//  {
//    for (var i in histories)
//    {
//      var aHistory = histories[i];
//
//      if (aHistory.spacer)
//      {
//        var img = document.createElement("img");
//        img.setAttribute("src", aHistory.spacer.src);
//        img.style.float = "left";
//        div.appendChild(img); // spacer img to div
//      }
//
//      if (aHistory.intervalImage && aHistory.intervalImageOver)
//      {
//        // create image
//        var img = document.createElement("img");
//        img.setAttribute("src", aHistory.intervalImage.src);
//        img.setAttribute("id", "intervalImage"+i);
//        img.setAttribute("onmouseover", "showInformation("+ i +")");
//        img.style.float = "left";
//        div.appendChild(img); // append img to anchor
//      }
//    }
//  }
//}



//''*******************************************************************
//''  showInformation(chosenHistoryIndex)
//''*******************************************************************
//''* 
function showInformation(chosenHistoryIndex)
{
  if (chosenHistoryIndex > -1 && chosenHistoryIndex != currentHistoryIndex)
  {
    var currentHistory = histories[currentHistoryIndex];
    var chosenHistory = histories[chosenHistoryIndex];
    var historyNameDiv = document.getElementById("historyName");

    if (historyNameDiv && historyNameDiv)
      historyNameDiv.innerHTML = chosenHistory.intervalName;

    var historyInformationDiv = document.getElementById("historyInformation");

    if (historyInformationDiv && historyInformationDiv)
    {
      var info = ritec_decodeCustomEntities(chosenHistory.information);
      //alert(info);
      historyInformationDiv.innerHTML = info;
    }

    showInformationImage(chosenHistoryIndex, 1);
    showInformationImage(chosenHistoryIndex, 2);
    showInformationImage(chosenHistoryIndex, 3);

    var currentIntervalImage = document.getElementById("intervalImage"+currentHistoryIndex);

    // change current history interval image non-over image
    if (currentIntervalImage)
      currentIntervalImage.src = currentHistory.intervalImage.src;
    
    var chosenIntervalImage = document.getElementById("intervalImage"+chosenHistoryIndex);

    // change chosen history interval image over image
    if (chosenIntervalImage)
      chosenIntervalImage.src = chosenHistory.intervalImageOver.src;
    
    //'' set current histories index to passed in chosen histories index
    currentHistoryIndex = chosenHistoryIndex;
  }
}



////''*******************************************************************
////''  showInformationImage(historiesIndex, imageNum)
////''*******************************************************************
////''* 
//function showInformationImage(chosenHistoryIndex, anInformationImageNum)
//{
//  var chosenHistoryIndex = chosenHistoryIndex >= 0?chosenHistoryIndex:-1;
//
//  if (chosenHistoryIndex > -1 && chosenHistoryIndex != currentHistoryIndex)
//  {
//    var historyImagesDiv = document.getElementById("historyImages");
//
//    var chosenHistory = histories[chosenHistoryIndex];
//
//    var chosenHistoryInformationImage;
//
//    if (anInformationImageNum == 1)
//      chosenHistoryInformationImage = chosenHistory.informationImage1;
//
//    else if (anInformationImageNum == 2)
//      chosenHistoryInformationImage = chosenHistory.informationImage2;
//
//    else if (anInformationImageNum == 3)
//      chosenHistoryInformationImage = chosenHistory.informationImage3;
//
//    //''alert("chosenHistoryInformationImage == "+chosenHistoryInformationImage+"\n");
//
//    if (historyImagesDiv)
//    {
//      if (chosenHistoryInformationImage)
//      {
//        var currentInformationImageSpan = document.getElementById("informationImageSpan"+anInformationImageNum);
//
//        if (!currentInformationImageSpan)
//        {
//          /*
//          currentInformationImageSpan = document.createElement("span");
//          currentInformationImageSpan.setAttribute("id", "informationImageSpan"+anInformationImageNum);
//          historyImagesDiv.appendChild(currentInformationImageSpan); // append img to div 
//          */
//          historyImagesDiv.innerHTML = "<span id='informationImageSpan"+anInformationImageNum+"'><span/>";
//        }
//
//        /*
//        currentInformationImageSpan.innerHTML = "";
//
//        currentInformationImageSpan.appendChild(chosenHistoryInformationImage); // append img to span
//        */
//
//        currentInformationImageSpan.innerHTML = chosenHistoryInformationImage != null?"<img src='"+chosenHistoryInformationImage.src+"' />":"";
//      }
//    }
//  }
//}



//''*******************************************************************
//''  showInformationImage(historiesIndex, imageNum)
//''*******************************************************************
//''* 
function showInformationImage(chosenHistoryIndex, anInformationImageNum)
{
  var chosenHistoryIndex = chosenHistoryIndex >= 0?chosenHistoryIndex:-1;

  if (chosenHistoryIndex > -1 && chosenHistoryIndex != currentHistoryIndex)
  {
    var historyImagesDiv = document.getElementById("historyImages");

    var chosenHistory = histories[chosenHistoryIndex];

    var chosenHistoryInformationImage;

    if (anInformationImageNum == 1)
      chosenHistoryInformationImage = chosenHistory.informationImage1;

    else if (anInformationImageNum == 2)
      chosenHistoryInformationImage = chosenHistory.informationImage2;

    else if (anInformationImageNum == 3)
      chosenHistoryInformationImage = chosenHistory.informationImage3;

    //''alert("chosenHistoryInformationImage == "+chosenHistoryInformationImage+"\n");

    if (historyImagesDiv)
    {
      var currentInformationImageSpan = document.getElementById("informationImageSpan"+anInformationImageNum);

      if (!currentInformationImageSpan)
      {
        historyImagesDiv.innerHTML = historyImagesDiv.innerHTML+"<span id='informationImageSpan"+anInformationImageNum+"'><span/>";
        currentInformationImageSpan = document.getElementById("informationImageSpan"+anInformationImageNum);
      }

      if (chosenHistoryInformationImage)
        currentInformationImageSpan.innerHTML = "<img style='margin:10px 10px 10px 10px;' src='"+chosenHistoryInformationImage.src+"' />";
      else
        currentInformationImageSpan.innerHTML = "";
    }
  }
}



//''*******************************************************************
//''*******************************************************************
//''******* ERRORHANDLER *********************************************
//''*******************************************************************
//''*******************************************************************
//''
var errorHandler = function(msg, url, linenumber)
{
   alert("Error: " + msg + "\nfor: " + url + "\nat line " + linenumber);
}



//''*******************************************************************
//''*******************************************************************
//''******* handleErrFullPage ********************************************
//''*******************************************************************
//''*******************************************************************
//''
function handleErrFullPage(strIn)
{

  var errorWin;

  // Create new window and display error
  try
  {
    errorWin = window.open('', 'errorWin');
    errorWin.document.body.innerHTML = strIn;
  }
  catch(e) // If pop-up gets blocked, inform user
  {
    alert('An error occurred, but the error message cannot be' +
            ' displayed because of your browser\'s pop-up blocker.\n' +
            'Please allow pop-ups from this Web site.');
  }
}



//''*******************************************************************
//''*******************************************************************
//''******* getBrowserInfo ***********************************************
//''*******************************************************************
//''*******************************************************************
//''
function getBrowserInfo()
{
  var rVal = "";
  
  rVal += "browser appName == "+navigator.appName+"\n";
  rVal += "browser codename == "+navigator.codename+"\n";
  rVal += "browser version == "+navigator.version+"\n";
  rVal += "browser platform == "+navigator.platform+"\n";
  rVal += "browser javaEnabled == "+navigator.javaEnabled+"\n";
  rVal += "browser screenWidth == "+navigator.screenWidth+"\n";
  rVal += "browser screenHeight == "+navigator.screenHeight+"\n";

  return rVal;
}



//''*******************************************************************
//''*******************************************************************
//''******* CHECKEMAIL ************************************************
//''*******************************************************************
//''*******************************************************************
//''
function checkEmail(input, result)
{
  //'' Result mode
  if (result > "")
  {
    if (result == "true")
      return true;
    else
      return false;
  }

  //'' Input mode
  else
  {
    url  = "http://admin-2-2-9.plugandplaycms.com/content/ajax?1=1&ClientID=6024&processor=checkEmail&input="+input;
    loadXMLDoc(url);
  }
}



//''*******************************************************************
//''*******************************************************************
//''******* DISPLAYAJAXPROCESSING ************************************
//''*******************************************************************
//''*******************************************************************
//''
function displayAjaxProcessing(docElemID, msgStr)
{
  document.getElementById(docElemID).outerHTML;
}



//''*******************************************************************
//''*******************************************************************
//''******* GETARG ****************************************************
//''*******************************************************************
//''*******************************************************************
//''
function getArg(name,separator)
{
	if (!separator || separator == "")
		separator = "&";
	
  var args = new Object(  );
  var query = location.search.substring(1);     
    // Get query string
  var pairs = query.split(separator);
   // Break at comma
  for(var i = 0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
        // Look for "name=value"
      if (pos == -1) continue;
        // If not found, skip
      var argname = pairs[i].substring(0,pos);
        // Extract the name
      var value = pairs[i].substring(pos+1);
        // Extract the value
      args[argname] = unescape(value);
       // Store as a property
     // In JavaScript 1.5, use decodeURIComponent(  ) 
     // instead of escape(  )
     if(argname == name)
      return unescape(value);
  }
  return null;
  //return args;     // Return the object
}



//''*******************************************************************
//''*******************************************************************
//''******* GETFROMFROMREMOTESESSION **********************************
//''*******************************************************************
//''*******************************************************************
//''
function getFromRemoteSession(aVar)
{
  var temp = null;
  setFromRemoteSession(aVar);
  var millisec = new Date().getMilliseconds();

  while(millisec < 2000)
  {
    if(eval("temp"+aVar+"!=-1;"))
    {
      eval("temp=temp"+aVar+";");
      eval(temp+"aVar=null;"); 
      return temp;
    }
  }

  eval("temp"+aVar+"==null;");
}

//''*******************************************************************
//''*******************************************************************
//''******* GETUSERDATAFROMREMOTE *************************************
//''*******************************************************************
//''*******************************************************************
//''
function getUserDataFromRemote(aVar)
{
  var temp = null;
  setUserDataFromRemote(aVar);
  var millisec = new Date().getMilliseconds();

  while(millisec < 2000)
  {
    if(eval("temp"+aVar+"!=-1;"))
    {
      eval("temp=temp"+aVar+";");
      eval(temp+"aVar=null;"); 
      return temp;
    }
  }

  eval("temp"+aVar+"==null;");
}




//''*******************************************************************
//''*******************************************************************
//''******* LOADXMLDOC ************************************************
//''*******************************************************************
//''*******************************************************************
//''
function loadXMLDoc(url)
{
  if (AJAX_DEBUG)
    displayStatus(""+url);

  //'' Native XMLHttpRequest object
  if (window.XMLHttpRequest)
    req = new XMLHttpRequest();

  //'' IE/Windows ActiveX
  else if (window.ActiveXObject)
  {
    try
    {
    	req = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does a have required component.\nSome functionality might not work as expected!");
        return false;
      }
    }
  }

  if (req)
  {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
  }
  else
  {
    alert("Your browser does not support XMLHttpRequest technology!"); 
    supportsXmlHttpReq = false;
  }
}



//''*******************************************************************
//''*******************************************************************
//''******* PROCESSREQCHANGE ****************************************
//''*******************************************************************
//''*******************************************************************
//''
function processReqChange() 
{
  // only if req shows "complete"
  if (req && req.readyState == 4)
  {
		//''if (AJAX_DEBUG)
			//''alert(req.responseText);

    // only if "OK"
    if (req.status == 200)
    {
      resp  = req.responseXML.documentElement;
			
			var method;
			var result;
			
			if (resp.getElementsByTagName("method").length > 0)
      {
        //''alert("METHOD\n\n"+resp);
        //''alert(req.responseText);
				method = resp.getElementsByTagName("method")[0].firstChild.text;
        //''alert(method);
      }
		
			if (resp.getElementsByTagName("result").length > 0)
      {
        //''alert(req.responseText);
        //''alert("RESULT\n\n"+resp);
        //''alert(resp.getElementsByTagName("result")[0].firstChild.valueOf());
        //''alert(resp.getElementsByTagName("result")[0].firstChild.xml);
        //''alert(resp.getElementsByTagName("result")[0].text);
				result = resp.getElementsByTagName("result")[0].firstChild.text;
        //''alert(result);
      }
			
      //''if (AJAX_DEBUG)
        //''alert("calling .. "+method + "(\'\', result='"+result+"')");
				
			if (method)
			{
				if (result)
					eval(method + "(\'\', result)");
				else
					eval(method + "(\'\',\'\')");					
			}
    }
    else
    {
      alert("There was a problem retrieving the XML data:\n" + req.statusText + "\n\n" + req.responseText);
    }
  }
}





//''*******************************************************************
//''*******************************************************************
//''******* POPUPWINDOW **********************************************
//''*******************************************************************
//''*******************************************************************
//''
function getPopupWindow()
{
	return document.win;
}


function setPopupWindow(obj)
{
	document.win = obj;
}


//''*******************************************************************
//''*******************************************************************
//''******* SUBMITFORM ************************************************
//''*******************************************************************
//''*******************************************************************
//''
function submitForm(formObj)
{
//  var b = new BrowserInfo();
//  alert(b);
//  if(b.name != "Microsoft Internet Explorer")
//  {
//    formObj.submit();
//  }
//  else
//  {
//   	// make sure the form has a submit button
//    if(!String(formObj.elements["submit"]).match(/object/))
//		  formObj.innerHTML = formObj.innerHTML + "<input type='submit' name='submit' id='submit' value='' style='display:none'>";
//
//    formObj.submit.click();
//  }

    formObj.submit();
}




//''*******************************************************************
//''*******************************************************************
//''******* TEST ******************************************************
//''*******************************************************************
//''*******************************************************************
//''
function test(input, result)
{
  //'' Result mode
  if (result > "")
  {
    alert("result: " + result);
  }

  //'' Input mode
  else
  {
    url  = "http://admin-2-2-9.plugandplaycms.com/content/ajax?1=1&ClientID=6024&processor=test&input="+input;
    loadXMLDoc(url);
  }
}

//''*******************************************************************
//''*******************************************************************
//''******* DISPLAYSTATUS *********************************************
//''*******************************************************************
//''*******************************************************************
//''
function displayStatus(msg)
{
  alert(msg);
  //document.getElementById("status").innerHTML = msg;
}


//''*******************************************************************
//''*******************************************************************
//''******* DISPLAYSTATUS *********************************************
//''*******************************************************************
//''*******************************************************************
//''
function makeErrorMsg(previousMsg, appendMsg)
{
  var previousMsg = (previousMsg > "")?previousMsg:"Sorry! There seems to be a problem\nwith some functionality...\n";
  var appendMsg = (appendMsg > "")?appendMsg:"";
  return previousMsg.concat(appendMsg);
}



//''*******************************************************************
//''* pageHeight 
//''*******************************************************************
//''*
//''* return the page height
//''*
function pageHeight()
{
  var pageHeight

  if (navigator.appName == 'Netscape')
    pageHeight = window.innerHeight - 20;
  else
    pageHeight = document.body.clientHeight;

  return pageHeight;
}

//''*******************************************************************
//''* pageWidth
//''*******************************************************************
//''*
//''* return the page width
//''*
function pageWidth()
{
  var pageWidth

  if (navigator.appName == 'Netscape')
    pageWidth = window.innerWidth - 20;
  else
    pageWidth = document.body.clientWidth;

  return pageWidth;
}


//''*******************************************************************
//''* openPopup
//''*******************************************************************
//''*
function openPopup(url, width, height, top, left)
{
  //'' set dimesions of dialog window window
  var width = width > 0?parseInt(width):600;
  var height = height > 0?parseInt(height):600;

  //'' place dialog window  on screen
  var top = top >= 0?parseInt(top):parseInt((screen.availHeight-20 - height) / 2);
  var left = left >= 0?parseInt(left):parseInt((screen.availWidth-10 - width) / 2);

  win = MM_openBrWindow(url,'win','width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top);
}


//''*******************************************************************
//''  ritec_decodeCustomEntities
//''*******************************************************************
function ritec_decodeCustomEntities(str)
{
  str = str.replace(/&br;/g, "<br/>");
  str = str.replace(/&o-p;/g, "<p>");
  str = str.replace(/&c-p;/g, "</p>");
  //''str = str.replace(/ /g, "&nbsp;");
  return str;
}




