var maxNumPics = 0;
var root = "SAwards";
var glbSubPath = "";
var glbFileName = "";
var glbCategory = "";
var glbID = 0;
var orderWin;
var yearChoice = 1999;
var placeChoice = "PL";
var compChoice = "01";
var thumbPath = "thumbs\\";
var picExists;
var pageType = "comp";
var longDate = "01 January 2005";

function newPlace ( placeName )
{
    placeChoice = placeName;
    compChoice = "01";
    reInitComp();
}

function updateYear ( yearChoice )
{
    document.images['year'].src = "images\/" + String( yearChoice ) + "_up.png";
}

function yearUp ()
{
    if (yearChoice < 2006) { yearChoice++; }
    document.images['year'].src = "images\/" + String( yearChoice ) + "_up.png";
    compChoice = "01";
    reInitComp();
}

function yearDown ()
{
    if (yearChoice > 1999) { yearChoice--; }
    document.images['year'].src = "images\/" + String( yearChoice ) + "_up.png";
    compChoice = "01";
    reInitComp();
}

function yearUpBest ()
{
    if (yearChoice < 2004) { yearChoice++; }
    document.images['year'].src = "images\/" + String( yearChoice ) + "_up.png";
    startPic ( 'bestpic', "Best" + String( yearChoice ).substring(2), "Best", NumInYear[String( yearChoice )]);
    loadThumbs ( 'bestpic', String( yearChoice ) );
}

function yearDownBest ()
{
    if (yearChoice > 1999) { yearChoice--; }
    document.images['year'].src = "images\/" + String( yearChoice ) + "_up.png";
    startPic ( 'bestpic', "Best" + String( yearChoice ).substring(2), "Best", NumInYear[String( yearChoice )]);
    loadThumbs ( 'bestpic', String( yearChoice ) );
}

function newComp ( compName )
{
    compChoice = compName;
    reInitComp();
}

function reInitComp ()
{
    var compName = getCompetitorName( String(placeChoice) + String(yearChoice).substring(2) + String(compChoice));
    var rootName = String( placeChoice ) + String( yearChoice ).substring(2) + compChoice;
    var rootPath = String (placeChoice);
    reInit ( compName, rootName, rootPath );
    updateResultTable();
}

function reInit ( title, rootName, rootPath )
{
    var elemPara;
    startPic ( 'bestpic', rootName, rootPath, 99);
    insertText ( 'competitorName', title );
    elemPara = document.getElementById('competitorName');
    elemPara.style.fontWeight = 'bold';
    loadThumbs ( 'bestpic', title );
}

function updateResultTable ()
{
    var posName;
    var compName;
    var rootName;
    var i;
    
    if ( placeChoice == "PL")
    {
        insertText ( 'tableHead', "British Firework Championships " + String ( yearChoice ));
        elemPara = document.getElementById('tableHead');
//        elemPara.style.fontWeight = 'bold';
    }
    if ( placeChoice == "SP")
    {
        insertText ( 'tableHead', "Musical Firework Championships " + String ( yearChoice ));
        elemPara = document.getElementById('tableHead');
//        elemPara.style.fontWeight = 'bold';
    }
    var resultLimit = 6;
    if (pageType == "show") { resultLimit = 5; };
    for (i=1; i<=resultLimit; i++)
    {
        posName = "pos" + String(i);
        rootName = String(placeChoice) + String(yearChoice).substring(2) + "0" + String(i);
        compName = getCompetitorName( rootName );
//alert("*" + compName + "*");
        if (compName == undefined)
        {
            insertText ( posName, " " );
            elemPara = document.getElementById("dnp" + String(i));
            elemPara.height = 0;
            elemPara = document.getElementById(posName);
        }
        else
        {
            elemPara = document.getElementById("dnp" + String(i));
//            alert(i + " " + elemPara);
            elemPara.height = 18;
            insertText ( posName, compName + " (" + setNumPics(rootName) + ")" );
            elemPara = document.getElementById(posName);
            if ((i<4) && ( pageType == "comp" ))
            {
                elemPara.style.color = '#000000';
            }
            elemPara.style.fontWeight = "bold";
            elemPara.style.marginLeft = '10';
            elemPara.style.fontSize = '12';
        }
    }
}

function buildEventLists ()
{
   var listFrame = window.frames['listFrame'];
   listFrame.document.open ();

   var out_text;
   out_text = "<html><head><link rel=\"stylesheet\" href=\"css\/firework.css\">";
   out_text += "<style>A.piclink {color: #FFCC00 }</style>";
   out_text += "<style fprolloverstyle>A.piclink:hover {color: #FFFF00; font-style: italic }</style></head>";
   out_text += "<body leftmargin=8 topmargin=4>";
//alert(out_text);
   listFrame.document.write (out_text);

   var i;
   var inTop5;
   
   for (i=1; i<=maxNumEvents; i++)
   {
     inTop5 = false;
     for (j=1; j<=5; j++)
     {
        if (Top5[j] == i) { inTop5 = true; }
     }
     if (!inTop5)
     {
        listFrame.document.write ("<A class=piclink ");
        listFrame.document.write ("onmouseover=\"javascript: self.status=\'" + getEventName (i) + "\'; return true;\" ");
        listFrame.document.write ("onmouseout=\"javascript: self.status=''; return true;\" ");
        listFrame.document.write ("href=\"javascript: parent.doNewCo(\'" + String(i) + "\');\">");
        listFrame.document.write ( getEventName(i) + "<BR>");
     }
   }
   out_text = "</p></body></html>";
   listFrame.document.write (out_text);
   listFrame.document.close ();

}

function doNewCo( id )
{
    newCompany( id );
    document.images['logo'].src = "images/coollogo_com_fwkphoto.gif";
    
}

function buildEventTop5 ()
{
    var i;
    for (i=1; i<=5; i++)
    {
        document.write ("<TR><TD width=234 height=18");
        if (i % 2 == 1) { document.write(" bgcolor=#606060");}
        document.write (">&nbsp;&nbsp;<A class=piclink ");
        document.write ("onmouseover=\"javascript: self.status=\'" + getEventName ( Top5[i]) + "\'; return true;\" ");
        document.write ("onmouseout=\"javascript: self.status=''; return true;\" ");
        document.write ("href=\"javascript: doNewCo(\'" + String(Top5[i]) + "\');\">");
        
        if ( Top5[i] != 0)
        {
            document.write ( getEventName ( Top5[i]) );
        }
        else
        {
        }
        document.write ("</A></TD></TR>");
     }
     
}

function newCompany ( idStr )
{
    var compName = getEventName( idStr ) + ". " + getCompanyName( idStr );
    var rootName = getEventRoot( idStr );
    var rootPath = "Events"; // \/" + getEventRoot( idStr );
//    alert ( compName +"**"+ rootName +"**"+ rootPath );
    glbID = Number(idStr);
    longDate = getLongDate ( glbID );
    reInit ( compName, rootName, rootPath );
    return true;
}


function startPic ( picID, rootName, subPath, endNum )
{
    root = rootName;
    glbSubPath = subPath + "\/";
    var index = 99;
    var textNum3 = String(index);
    if (index < 10) { textNum3 = "0" + textNum3; }
    var URL = "pics\/" + glbSubPath + rootName + textNum3 + ".jpg";
    picExists = 1;
//    var test = testImage(URL);
//    while ((picExists == 0) & (index > 1))
//    {
//        index--;
//        textNum3 = String(index);
//        if (index < 10) { textNum3 = "0" + textNum3; }
//        URL = "pics\/" + glbSubPath + rootName + textNum3 + ".jpg";
//        test = testImage(URL);
//    }
    maxNumPics = endNum;
    maxNumPics = setNumPics (root);
//    alert(maxNumPics);
    if (maxNumPics > 99)
	{ rootName = rootName + "0" };
	if (maxNumPics == 0)
	{
	    document.images[picID].src = "images\/sorry.png";
	}
	else
	{
	    document.images[picID].src = "pics\/" + glbSubPath + rootName + "01.jpg";
	}

    return true;     					 
};

function firstPic ( picID, rootName, subPath, endNum )
{
    root = rootName;
    glbSubPath = subPath + "\/";
    var index = 99;
    var textNum3 = String(index);
    if (index < 10) { textNum3 = "0" + textNum3; }
//    var URL = "pics\/" + glbSubPath + rootName + textNum3 + ".jpg";
//    while ((!testImage(URL)) && (index > 1))
//    {
//        index--;
//        textNum3 = String(index);
//        if (index < 10) { textNum3 = "0" + textNum3; }
//        URL = "pics\/" + glbSubPath + rootName + textNum3 + ".jpg";
//    }
    maxNumPics = endNum;
    maxNumPics = setNumPics ( root );
//    alert(maxNumPics);
    if (maxNumPics > 99)
	{ rootName = rootName + "0" }
	if (maxNumPics == 0)
	{
	    document.images[picID].src = "images\/sorry.png";
	}
	else
	{
        document.images[picID].src = "pics\/" + glbSubPath + rootName + "01.jpg"
    }

    return true;     					 
};

function setNumPics ( inStr )
{
    switch (pageType) {
        case "show":
        case "comp":
        var competitorStr = "PLSPSH";
        var strName = String( inStr );
        if (competitorStr.indexOf(strName.substring(0,1)) != -1)
        {
            return getNumCompetitorPics (strName);
        }
        break;
        case "event":
        return getNumEventPics ( glbID );
        break;
        case "best":
            return maxNumPics;
            break;
    }
}

function getFileNum ( fileName )
{
      var textNum;
      var index = fileName.indexOf(".jpg");

        textNum = fileName.substring(index-6, index);
//	if (!isNaN(textNum.substring(3,4)))
//	{
//	   textNum = textNum.substring(3,textNum.length);
//	}
//	else
//	{
           textNum = textNum.substring(4,textNum.length);
//	}
      return textNum;

};

function nextPic ( picID )
{
	if (maxNumPics != 0)
	{
      var textNum;
      var index = document.images[picID].src.indexOf(".jpg");

      if (index == -1)
      { index = 1; 
      }
      else
      {
        if (pageType == "best") { textNum = document.images[picID].src.substring(index-2, index); }
        else {
        textNum = document.images[picID].src.substring(index-6, index);
        textNum = textNum.substring(4,textNum.length);
        }
        index = Number(textNum) + 1;
      }
      
      var textNum2 = String(index);
      if (index < 10) { textNum2 = "0" + textNum2; }
      if ((textNum.length == 3) && (index < 100)) { textNum2 = "0" + textNum2; }
      var rootName = root;

      var URL = "pics\/" + glbSubPath + rootName + textNum2 + ".jpg";
      if (index > maxNumPics)
      { 
        textNum2 = "01";
        index = 1;
        if ((textNum.length == 3) && (index < 100)) { textNum2 = "0" + textNum2; }
      };

      document.images[picID].src = "pics\/" + glbSubPath + rootName + textNum2 + ".jpg";

//    addDescText ( root + textNum2 );

    return true;
    }
};

function prevPic ( picID )
{

	if (maxNumPics != 0)
	{
      var textNum;
      var index = document.images[picID].src.indexOf(".jpg");

      if (index == -1)
      { index = 1; 
      }
      else
      {
        textNum = document.images[picID].src.substring(index-6, index);
        textNum = textNum.substring(4,textNum.length);
        index = Number(textNum) - 1;
        if (index < 1)
        { 
          index = maxNumPics;
        }
      }
      var textNum2 = String(index);
      if (index < 10) { textNum2 = "0" + textNum2; }
      if ((textNum.length == 3) && (index < 100)) { textNum2 = "0" + textNum2; }
      var rootName = root;

      document.images[picID].src = "pics\/" + glbSubPath + rootName + textNum2 + ".jpg"

//    addDescText ( root + textNum2 );

    return true;
	}
	
};

function insertText ( paraID, text )
{
    var textNode;
    var elemPara;
    var oldTextNode;

    elemPara = document.getElementById(paraID);
    textNode = document.createTextNode(text);
    oldTextNode = elemPara.firstChild;
    elemPara.replaceChild(textNode, oldTextNode );
    
};

function colourText ( paraID, text )
{
    var textNode;
    var elemPara;
    var oldTextNode;

    elemPara = document.getElementById(paraID);
    elemPara.style.color = text;

    
};

function loadThumbs ( picID, textName )
{

   glbCategory = textName;
   var thFrame = window.frames['thumbsFrame'];
   if (isNS)
   {
//     alert("Netscape");
   }

   thFrame.document.open ();


   var out_text;
   out_text = "<html><head><link rel=\"stylesheet\" href=\"css\/firework.css\"></head>";
   out_text += "<body><center><h4 style=\"margin-bottom:-24px\">" + textName + "</h4><BR>";
//alert(out_text);
   if (pageType == "event")
   { out_text += "<h5>" + longDate + "</h5>"; }

   thFrame.document.write (out_text);

   var i;
   for (i=1; i<=maxNumPics; i++)
   {
        out_text = "<P>";
	out_text += "<a href=\"javascript:parent.loadPic('" + picID+ "','" + String(i) + "');\">";
	out_text += "<img src=\"" + thumbPath;
	fileName = root;
	if ((maxNumPics > 100) && (i < 100)) { fileName += "0";}
	if (i < 10) { fileName += "0";}
	fileName += String(i)
	out_text += glbSubPath + fileName + ".jpg\"></a>";
	out_text += "<BR>" + fileName + "</p>";
    thFrame.document.write (out_text);
   }

   out_text = "</p></body></html>";
   thFrame.document.write (out_text);
   thFrame.document.close ();


};

function loadPic ( picID, index )
{
      var textNum2 = String(index);
      if (index < 10) { textNum2 = "0" + textNum2; }
      if ((maxNumPics > 99) && (index < 100)) { textNum2 = "0" + textNum2; }

	if (maxNumPics == 0)
	{
	    document.images[picID].src = "images\/sorry.png";
	}
	else
	{
        document.images[picID].src = "pics\/" + glbSubPath + root + textNum2 + ".jpg"
	}

//    addDescText ( root + textNum2 );

};


function buyPic( picName )
{
//alert(picName);

	if (maxNumPics != 0)
	{
        var textNum;
        var index = document.images[picName].src.indexOf(".jpg");
        
        textNum = document.images[picName].src.substring(index-6, index);
        textNum = textNum.substring(4,textNum.length);
        glbFileName = root + textNum;
        
        openWindow ("order.htm");
   
   }


//alert(glbFileName);
};

function openWindow ( url )
{

   if ((typeof(orderWin) == "undefined") || (orderWin.closed == true))
   {
   var w = 740;
   var h = 500;
   var x = (screen.width - w) /2;
   var y = (screen.height - h) /2;
   chrome = "width=" + w + ",height=" + h + ",left=" + x + ",top=" + y + ",scrollbars=yes";

   if (window.showModalDialog)
   {
     orderWin = open ( url, "orderWin", chrome);
   }
   else
   {
    // + ",dependent=yes"
     orderWin = open ( url, "orderWin", chrome );
   }
   }
   else
   {
     orderWin.location.href = url;
     orderWin.focus();
   }


};

function openBasketWin ()
{

  openWindow ("do_order.htm");

};

function isNS ()
{
   return (document.layers && navigator.appName == "Netscape")
};

function testImage(URL) {

function isGood() {
    picExists = 1;
//    alert('That image exists!');
//    picExists = 1;
    alert(picExists + " " + URL);
}

function isBad() {
//    alert('That image does no exist!');
    picExists = 0;
//   alert(picExists);
}



    var i = 0;
    picExists = 1;
    var tester=new Image();
    tester.onerror=isBad;
//    if (isNS) {tester.handleEvent(tester.onerror);}
    tester.src=URL;
//    if (isNS) { while (i < 100000) { i++; }; alert("here");}
//    return (picExists == 1);
}


