/*
* Holds main generic functions that are available
* throughout the website.
*
* Written by Gary Stimson 2004
*/

/* uses search.js, printstyle.css, screenstyle.css, *_docscript.js */

var sUpdatedDate = '29-04-2009';
var sYear        = '2009';

// Var to control document
var iDocument  = 0;
var sPPath     = '';
var sDocNote   = '&nbsp;';

// Below will store the browser:
var iBrowser; // 1 for IE, 2 for netscape, 3 for opera, 4 for hotjava,
var iVersion; // will store the version number.

lTimeMenu = false;

var oPopUp;
var oBigPopUp;

// Variables used on product pages:
var iProducts = 0;
var iCat      = 0;
var sCode     = '';
var sContinue = '';

// Finds what browser the user has:
function detectBrowser()
{
  var sAgt = navigator.userAgent.toLowerCase();

  var iTempVer = parseInt(navigator.appVersion);

  // Netscape
    if ((sAgt.indexOf('mozilla')!=-1) && (sAgt.indexOf('spoofer')==-1)
       && (sAgt.indexOf('compatible') == -1) && (sAgt.indexOf('opera')==-1)
       && (sAgt.indexOf('webtv')==-1) && (sAgt.indexOf('hotjava')==-1))
    {
      iBrowser = 2;
      iVersion = iTempVer;
      if (sAgt.indexOf('gecko') != -1)
      {
        iVersion = 10; // signifies mozilla
      }
    }
    else // IE
    if ((sAgt.indexOf("msie") != -1) && (sAgt.indexOf("opera") == -1))
    {
      iBrowser = 1;
      if (iTempVer < 4)
      {
        iVersion = 3;
      }
      else
      if (iTempVer == 4)
      {
        if (sAgt.indexOf("msie 4")!=-1)
        {
          iVersion = 4;
        }
        else
        if (sAgt.indexOf("msie 5")!=-1)
        {
          iVersion = 5;
        }
        else
        if (sAgt.indexOf("msie 6")!=-1)
        {
          iVersion = 6;
        }
        else
        {
          iVersion = 7; // signifies new untested version
        }
      }
      else
      {
        iVersion = 10; // signifies new untested version
      }
    }
    else
    if (sAgt.indexOf("opera") != -1)
    {
      iBrowser = 3;

      if (sAgt.indexOf("opera 2") != -1 || sAgt.indexOf("opera/2") != -1)
      {
        iVersion = 2;
      }
      else
      if (sAgt.indexOf("opera 3") != -1 || sAgt.indexOf("opera/3") != -1)
      {
        iVersion = 3;
      }
      else
      if (sAgt.indexOf("opera 4") != -1 || sAgt.indexOf("opera/4") != -1)
      {
        iVersion = 4;
      }
      else
      if (sAgt.indexOf("opera 5") != -1 || sAgt.indexOf("opera/5") != -1)
      {
        iVersion = 5;
      }
      else
      {
        iVersion = 6; // signifies new untested version
      }
    }
    else
    if (sAgt.indexOf("hotjava") != -1)
    {
      iBrowser = 4;
      iVersion = iTempVer;
    }
    else
    {
      iBrowser = 0;
      iVersion = 0;
    }
}

// Shows Sub menus:
function showSubMenu(iTheIndex, iALeft, iATop){
	if (saSubMenu != null && saSubMenu[iTheIndex] != ''){
		rightMenu(false);
		oSubMenuDiv.innerHTML     = saSubMenu[iTheIndex];

		oSubMenu.style.left    = iALeft;
		oSubMenu.style.top     = iATop;
		oSubMenu.style.display = 'block';
	}
	else{
		hideSubMenu();
	}
}

function hideSubMenu(){
	if (oSubMenu.style.display == 'block'){
		//oSubMenuDiv.innerHTML     = '';
		oSubMenu.style.display = 'none';
	}
}

function doMouseLeave(){
	hideSubMenu();
}

function doMouseOverMain(){
	hideSubMenu();
}

function doSubMouseIn(){
	event.srcElement.className = 'OverLink';
}

function doSubMouseOut(){
	event.srcElement.className = 'Norm';
}

// Function below contols the link colour:
function overLink(oObject, iImageIndex)
{
  var oElement;
  
  if (iBrowser == 1 || iBrowser == 3) // IE and opera only
  {
    oElement = event.srcElement;
    
    document.search.topImage.src = saImage[iImageIndex];
        
    if (iBrowser == 1) // IE only
    {
      if (oObject.className == 'OverLink')
      {
        oObject.className = 'Norm';
      }
      else
      {
        oObject.className = 'OverLink';
        showSubMenu(iImageIndex, oElement.offsetLeft + oLinkBar.offsetLeft + 25, oElement.offsetTop + oLinkBar.offsetTop + 20);
      }
    }
  }
}

// Function changes the search image when over it:
function overSearch(lOver)
{
  if (iBrowser == 1 || iBrowser == 3) // IE and opera only
  {
    if (lOver == true)
    {
      document.search.searchImage.src = sPPath + 'images/search_on.gif';
    }
    else
    {
      document.search.searchImage.src = sPPath + 'images/search_off.gif';
    }
  }
}

// Constructs document:
function buildDoc(iTheDocument, sParent, lAlert)
{
  iDocument = iTheDocument;
  sPPath    = sParent;

  detectBrowser();    // find the browser

  startUp();          // sets generic values

  if (iBrowser == 1 && iVersion > 4)  // I.E 5 & greater
  {
    setUpMenu();   // inserts menu
    setUpSubMenus(); // inserts sub menus
    window.status = saText[iDocument] + ' page'; // set the status text
  }
  else
  if (lAlert)
  {
    alert('This site is best viewed with the latest version of Internet Explorer.');
  }
  setUpLinkBar();  // inserts Link bar
}

// Below is the code used to activate the Right Menu

// Hides all menus:
function hideMenus(){
    hideSubMenu();	
    ThePopUpMenu.style.display = 'none';
}


// Function below creates menu at mouse pointer (for IE):
function rightMenu(aEvent)
{
  if (iBrowser == 1 && iVersion > 4) // i.e. IE
  {
  	if (event.type == 'click'){
  		if (event.srcElement.id == 'oPopupItem' || event.srcElement.id == 'ThePopUpMenu' ||
  		    event.srcElement.id == 'oSubMenuDiv' || event.srcElement.id == 'oSubMenu'){
  			return true;
  		}
  	}
  	
	hideSubMenu();
	
	// X Coord:
	var X = event.clientX;
	X += document.body.scrollLeft;

	// Y Coord:
	var Y = event.clientY;
	Y += document.body.scrollTop;

	// ThePopUpMenu is a style in the CSS that sets the table position for the menu:
	ThePopUpMenu.style.left=X;
	ThePopUpMenu.style.top=Y;

	if(aEvent)
	{
	   ThePopUpMenu.style.display = 'block';
	   return false; // default
	}
	else
	{
	   ThePopUpMenu.style.display = 'none';
	   return true; // default
	}	
        
  }

}

// Function below contols the colour:
function overItem(oObject)
{
  if (oObject.className == 'OverItem')
  {
    oObject.className = 'AnItem';
  }
  else
  {
    oObject.className = 'OverItem';
  }
}

// Function below activates the menu:
function activateMenu(iCommand)
{
  if (iCommand == 1)      // i.e. print
  {
    document.execCommand('print');         // call the print method as part of the window object.
  }
  else
  if (iCommand == 2)     // i.e. copy to clipboard
  {
    textbuffer.innerText = MainDoc.innerText; // the hidden textarea recieves the text (in span)
    Copied = textbuffer.createTextRange();
    Copied.execCommand('copy');
  }
  else
  if (iCommand == 3)      // i.e. refresh
  {
       document.execCommand('refresh');
  }
}

// ******

// Below are some functions used for general cosmetic features:

// This function adds anything needed at the bottom of the document:
function docBottom()
{
  
  
  sBottom = '<table width="100%">' +
            '<tr>' +
              '<td class="NB">&copy; ' + sCName + ' ' + sYear + '</td>' +
  	      '<td class="NB" style="text-align:right;">Updated ' + sUpdatedDate + '</td>' +
  	    '</tr>' +
  	    '</table>' +
  	    '<img src="' + sPathCor + 'images/paypal_logo.gif" style="display:none;"></img>' +
  	    '<center>' +
  	      '<table width="178">' +
  	        '<tr>' +
  	          '<td class="NB2">Secure payments by</td>' +
  	          '<td><a href="http://www.paypal.co.uk"><img src="' + sPathCor + 'images/paypal_logo.gif" border="0"></img></a></td>' +
  	        '</tr>' +
  	      '</table>' +
	    '</center>' +
	    getPayPalForm();
	      	
  document.write(sBottom);
}

// Function below is used to cut off areas of the website:
function underConst()
{
  alert('Sorry, this area of the website is still under construction.');
}

function divider(iType)
{
  if (iBrowser == 1){
	  if (iType == 1){
	    document.write('<center><TABLE width="85%"><TD class="BlueLine" COLSPAN="2"></TD></TABLE></center>');
	  }
	  else if (iType == 2){
	    document.write('<TABLE width="350"><TD class="BlueLine" COLSPAN="2"></TD></TABLE>');
	  }
	  else if (iType == 3){
	      document.write('<TABLE width="170"><TD class="GreyLine" COLSPAN="2"></TD></TABLE>');
	  }
  }
  else
  {
    if (iType == 3){
    	document.write('<P>&nbsp;<img width="170" height="1" class="GreyLine">&nbsp;<P>');
    }
    else{
    	document.write('<P>&nbsp;<HR>&nbsp;<P>');
    }
  }
}

// The function below pastes in an e-mail address therefore preventing e-mail grabbing:
function getEmail(iItem)
{
  document.write(findEmail(iItem));
}

function findEmail(iItem){
  var saEmail = new Array();
  saEmail[1]   = 'webmaster' + '@' + 'lightningware.co.uk';
  saEmail[2]   = 'sales' + '@' + 'lightningware.co.uk';
  saEmail[3]   = 'support' + '@' + 'lightningware.co.uk';
  saEmail[4]   = 'tech' + '@' + 'lightningware.co.uk';
  saEmail[5]   = 'custompc' + '@' + 'lightningware.co.uk';
  saEmail[6]   = 'returns' + '@' + 'lightningware.co.uk';
  saEmail[7]   = 'warranty' + '@' + 'lightningware.co.uk';

  return '<a HREF="mailto:' + saEmail[iItem] + '">' + saEmail[iItem] + '</A>'; 
}

function insertDownload(sCaption, sFile, sSize)
{
  var sName = '';
    
  if (sFile == ''){
    document.write('<img src="' + sPPath + 'images/downloads5.gif" ' +
    'alt="Download"></img><font class="Inactive" size="2">' + sCaption + '</font>');
  }
  else{
    sName = sFile.split('.')[0];

    document.write('<a href="javascript:doTheDownload(\'' + sFile + '\');"><img id="' + sName + '_image" src="' + sPPath + 'images/downloads3.gif" ' +
      'onMouseOver="overDownload(\'' + sName + '\');" onMouseOut="leaveDownload(\'' + sName + '\');" ' +
      'alt="' + sCaption + '" border=0></img></a>' +
      '<font class="AnItem" size="2"><a style="color:black;text-decoration:none;" href="javascript:doTheDownload(\'' + sFile + '\');" ' +
      'onMouseOver="overDownload(\'' + sName + '\');" onMouseOut="leaveDownload(\'' + sName + '\');">' +
      sCaption + ' (' + sSize + ')</a></font>');
  }
}

function doNavigate(oTheWindow, sTheURL){
	try{
		oTheWindow.navigate(sTheURL);
	}
	catch(e){
		oTheWindow.location = sTheURL; // firefox 
	}
}

function doTheDownload(sTheFile){
	
	if (top.oMonFrame != null){
		doNavigate(oMonFrame, sPPath + 'monframe.asp?download=' + sTheFile); 
	}
	
	doNavigate(window, sTheFile); 

		
}

function overDownload(sTheName){
	var sImg = sTheName + '_image';
	var oImg = document.getElementById(sImg);
	
	if (oImg != null){
		oImg.src = sPPath + 'images/downloads4.gif';
	}
}

function leaveDownload(sTheName){
	var sImg = sTheName + '_image';
	var oImg = document.getElementById(sImg);
	
	if (oImg != null){
		oImg.src = sPPath + 'images/downloads3.gif';
	}
}

function popUp(sPage)
{
  popUpDefined('popups/' + sPage + '.html', 300, 300);
}

function popUpDefined(sTheURL, iWidth, iHeight){
  var sScreen;
  var sWinStats
  
  if (oPopUp == null || oPopUp.closed){
    sScreen   = 'left=' + ((screen.availWidth/2) - (iWidth/2)) + ',top=' + ((screen.availHeight/2) - (iHeight/2));
    sWinStats = 'toolbar=no,location=no,directories=no,status=no,menubar=no,height=' + iHeight + ',width=' + iWidth + ',scrollbars=yes,resizable=yes,' + sScreen;
  
    oPopUp = window.open (sTheURL,'msgWindow',sWinStats);
  }
  else{
    doNavigate(oPopUp, sTheURL);
    //oPopUp.navigate(sTheURL);
    oPopUp.focus();	
  }
}


function openUp(sTheURL)
{
  var sWinStats;
  
  if (oBigPopUp == null || oBigPopUp.closed){
    sWinStats = 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
  
    oBigPopUp = window.open (sTheURL,'viewerWindow',sWinStats);
  }
  else{
    doNavigate(oBigPopUp, sTheURL);
    //oBigPopUp.navigate(sTheURL);
    oBigPopUp.focus();	
  }
  
}

function getPayPalForm(){
	sTheForm = '';
	
	sTheForm =	'<!-- PAY PAL -->' +
				'<form id="frmPayPal" name="frmPayPal" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">' +
				'<input type="hidden" name="cmd" value="_cart">' +
				'<input type="hidden" name="business" value="paypal@lightningware.co.uk">' +
				'<input type="hidden" name="display" value="1">' +
				'<input type="hidden" name="page_style" value="LightningWare">' +
				'</form>' +
                '<!-- -- -->';
	return sTheForm;
}

function doViewCart(){

	if (document.frmPayPal != null){
		document.frmPayPal.submit();
	}
}

function doSubmit(){
	if (sContinue != null && sContinue != '' && sContinue == 'y'){
		return true;
	}
	else{
		alert('This product can only be ordered with a new PC or Notebook. Please order a PC or Notebook for these items to become available.');
		return false;
	}
}

function doPCBuy(){
	if (top.oMonFrame != null){
		doNavigate(top.oMonFrame, sPPath + 'monframe.asp?session=y&sessionvar=pcbought&sessionval=y'); 
		//top.oMonFrame.navigate(sPPath + 'monframe.asp?session=y&sessionvar=pcbought&sessionval=y');
	}
}

function setDocNote(sValue){
	sDocNote = sValue;
}

function getDocNote(){
	return sDocNote;
}

// *******