// *** parse out the current page or pic file name ***
function GetFile(path)
{
   var answer = "";
   path = String(path);
   var SlashPos = path.lastIndexOf("/");
   
   answer = path.substring(SlashPos+1,path.length);   
   
   // remove hash name in url if necessary
   var HashPos = answer.indexOf("#");
   if (HashPos != -1) answer = answer.substring(0,HashPos);

   return answer;
}


// *** parse out the current page or pic name only ***
function GetName(page)
{
   var answer = "";
   page = String(page);
   var DotPos = page.indexOf(".");
   var FileName = page.substring(0,DotPos);

   for (var i = FileName.length-1; i >= 0; i--)
   {     
      if (isNaN(parseInt(FileName.charAt(i)))) {
         answer = FileName.substring(0,i+1);
         break;
      }
   }
   return answer;
}


// *** parse out the picture index in the document array ***
function GetPicIndex(pic)
{
   var answer = 999;
   for (var i = 0; i <= document.images.length; i++)
   {     
      if (document.images[i].name == pic.name) {
         answer = i;
         break;
      }
   }
   return answer;
}


// Set the new source for pic on the page
function SetPic(pic,NewPic,NewWidth,NewHeight,Msg) 
{
   var CurrPath = String(pic.src);
   var CurrWidth = pic.width;
   var CurrHeight = pic.height;

   CurrPath = CurrPath.substring(0,CurrPath.lastIndexOf("/")+1);
   pic.src = CurrPath + NewPic;
   // adjust witdth/height only if specified
   if (NewWidth != 0) {
      CurrWidth = NewWidth;
   }
   if (NewHeight != 0) {
      CurrHeight = NewHeight;
   }
   pic.width=CurrWidth;
   pic.height=CurrHeight;
   
   window.status = Msg;
}


// this function will alternate between 2 identical sized images
function AltPic(pic,image1,image2)
{
   var browserName = navigator.appName;
   var browserVer = parseInt(navigator.appVersion);
   var version = "";
   var CurrSrc,NewSrc = "";
   if (browserName == "Netscape"  && browserVer >= 3) version = "n3";
   else if (browserName == "Microsoft Internet Explorer" && browserVer >= 4.0) version = "n3";
   else version = "n2";

   if (version == "n3") {
      CurrSrc = GetFile(pic.src);   
      if (CurrSrc == image1) SetPic(pic,image2,pic.width,pic.height);
      else SetPic(pic,image1,pic.width,pic.height);
   }
}

// this function will expand/shrink pictures on the page
function ChangePic(pic,NewPic,NewWidth,NewHeight,MaxPage,PicType)
{
   var browserName = navigator.appName;
   var browserVer = parseInt(navigator.appVersion);
   var version = "";
   var CurrName = pic.name;
   var CurrPic = GetFile(pic.src);
   var CurrNum = GetNum(CurrPic);
   var CurrIndex = GetPicIndex(pic);

   if (browserName == "Netscape"  && browserVer >= 3) version = "n3";
   else if (browserName == "Microsoft Internet Explorer" && browserVer >= 4.0) version = "n3";
   else version = "n2";

   if (version == "n3") {        
      if (NewPic != "next" && NewPic != "prev") {
         if (CurrPic == "expand.gif") {
            SetPic(document.images[CurrIndex+1],NewPic + "." + PicType,NewWidth,NewHeight);
            SetPic(pic,"shrink.gif",pic.width,pic.height);
         }
	 if (CurrPic == "shrink.gif") {          
            SetPic(document.images[CurrIndex+1],"nothing.gif",1,0); 
            SetPic(pic,"expand.gif",pic.width,pic.height);
         }
      }
      if (NewPic == "next") {
         if (CurrNum == MaxPage) {
            SetPic(document.images[CurrIndex-1],"expand.gif",document.images[CurrIndex-1].width,document.images[CurrIndex-1].height);
            SetPic(pic,"nothing.gif",1,0);
         }
         if (CurrNum < MaxPage) SetPic(pic,CurrName + String(CurrNum + 1) + "." + PicType,pic.width,pic.height);
      }
      if (NewPic == "prev") {
         if (CurrNum == 1) {
            SetPic(document.images[CurrIndex-1],"expand.gif",document.images[CurrIndex-1].width,document.images[CurrIndex-1].height);
            SetPic(pic,"nothing.gif",1,0);
         }
         if (CurrNum > 1) SetPic(pic,CurrName + String(CurrNum - 1) + "." + PicType,pic.width,pic.height);
      }       

   if (typeof(parent.mainframe) != "undefined") parent.mainframe.focus();      
   }         
}

// *** count occurences of a string in a string ***
function CountHits(instring,srchitem)
{
   var count = 0;  
   var pos = instring.indexOf(srchitem);
   while (pos != -1) {
      count++;
      pos = instring.indexOf(srchitem,pos+1);
   }
   return count;
}

// load new page (translated if needed)
function LoadPage(pagename, dotrans)
{
   if (dotrans == "yes") {
      window.location="http://www.systranbox.com/systran/box";
   }
   else {
      window.location=pagename;
   }
}
