// JavaScript functions for Norwalk Camera Club website

/* control parameters */

function siteIsAvailable() {
   return true;  // if ready
   //return false; // if not ready
}

/* function navBar is used in index.html and other html files that need the same navigation bar */

function navBar(arg){
	var emp = '';
	var up  = '../'; 
	var Act = 'Active_pages/';
	var MO = 'MO/';
	var prefix1 = emp;  var prefix2 = Act; var prefix3 = MO; // setting for the 'Home' page
	if (arg != 'Home') { 
	   if (arg == undefined) { // setting for all other pages, except for 12 'Calendar' pages and the 'MO' page  
	      prefix1 = up;  prefix2 = emp; prefix3 = up + MO;
	   }
	   else { // setting for 12 'Calendar' pages and for the 'MO' page
	      prefix1 = up;  prefix2 = up + Act; prefix3 = up + MO; 
	   }
	}
	document.write('<body text="#000000" bgcolor="#CCCC99" link="#003300" vlink="#003300" alink="#FF0000">');
	document.write(     '<table id="outer" align="center" width="770" cellspacing="0" bgcolor="#FFFFCC">');  
	document.write(     '<tr><td>'); 
	document.write('<table align="center" width="766" cellspacing="0" cellpadding="4" bgcolor="#003300">');
	
	document.write('<tr>');
	var style1 = 'style="text-align: center; color: #FFFFCC; font: normal      xx-large Georgia, serif;"';
	var text = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Norwalk Camera Club';
	document.write('<td colspan="7"' + ' ' + style1 + '>' + text + '</td>');
	document.write('<td align="center" rowspan="2">');
	document.write('<img src="' + prefix1 + 'images/icon.jpg" width="84" height="90"></td>');
	document.write('</tr>');
	      
	var style2a = 'style="width: 84; text-align: center;"'; 
	var style2b = 'style="text-align: center;"'; 
	var style3  = 'style="text-decoration: none; color: #FFFFCC; font: bold x-small Georgia, serif;"';
	document.write('<tr>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix1 + 'index.html" border="0"' + 
	                   style3 +'>' + '&nbsp;&nbsp;Home</a></td>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix2 + 'about.html" border="0"' +
	                   style3 +'>' + '&nbsp;About&nbsp;&nbsp;Us</a></td>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix2 + 'events.html" border="0"' +
	                   style3 +'>' + '&nbsp;Events</a></td>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix2 + '27.html" border="0"' +
	                   style3 +'>' + '27&nbsp;&nbsp;Club</a></td>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix2 + 'gallery.html" border="0"' +
	                   style3 +'>' + '&nbsp;Gallery</a></td>');
	document.write('<td ' + style2a + '>' + '<a href="' + prefix2 + 'links.html" border="0"' +
	                   style3 +'>' + 'Links&nbsp;&nbsp;</a></td>');
	document.write('<td ' + style2b + '>' + '<a href="' + prefix3 + 'members_only.html" border="0"' +
	                   style3 +'>' + '&nbsp;Members&nbsp;&nbsp;Only</a></td>');
	document.write('</tr>');
	
	document.write('</table>');
	return;
}  

/* *** function to jump to the top of the current page (linking to the current page) by clicking on TEXT *** */

 function top(TEXT) {
    var str = '<p style="text-align: center;">';
    str += '<a href="' + location.href + '">';
    str += '<span ';
    str += 'style="color: #CC0000; font: bold small Georgia, sans-serif; text-decoration: none;">';
    str += TEXT; 
    str += '</span>'; 
    str += '</a>';  
	str += '</p>';
    document.write(str);
    return;   
 }	   

/* ********** function Title is used in about.html, events.html and in some others ********** */

function Title(title) {
	document.write('<br />');
    document.write('<h2 style="text-align: center; color: #CC0000; font-family: Georgia, sans-serif;">');
	document.write(title);
	document.write('</h2>'); 
	return;
}

/* *********************** function shuffle is used in index.html *************************** */

function shuffle(array) {
	var L = array.length;
	for (var i=0; i < L; i++) {
	   var k = Math.floor(Math.random()*L); 
	   if (k != i) { // then swap i-th and k-th elements
	      var s = array[i];
	      array[i] = array[k];
		  array[k] = s;
	   }
	}	   
	return array;
}

/* *********** function webmaster is used in index.html and some other html files *********** */
 
function webmaster() {
	document.write('<p style="text-align: center;  font: bold larger Verdana, sans-serif">');
	document.write('<a href="mailto:webmaster@norwalkcameraclub.org');
	document.write('?subject=NCC Website"');
	document.write('style="text-decoration: none; color: #FF0000;">Webmaster</a>');
	document.write('</p>');
	return;
}

/* function EPcontest & functions it calls (heading, category, winner) are for EP200Xwinners.html */

function EPcontest(arg1, arg2, // either EPContest('Heading',year) of EPContest('Category',category,...)
   	member1, place1, name1, title1, photo1,
	member2, place2, name2, title2, photo2,
	member3, place3, name3, title3, photo3) {
	if (arg1 == 'Heading') { heading(arg2); } 
	else if (arg1 == 'Category') {
	   if ((arg2 == undefined) || (member1 == undefined)) return; 
	   category(arg2);
	winner(member1, place1, name1, title1, photo1);
	if (member2 == undefined) return;
	winner(member2, place2, name2, title2, photo2);
	if (member3 == undefined) return;
	winner(member3, place3, name3, title3, photo3);
	}
	return;
}

function heading(year) { // e.g. heading('2007');
   document.write('<body>');
   document.write('<div style="text-align: center; color: #003300; font: bold medium Verdana,sans-serif;">');
   document.write('<span style="font-size: large;">Earthplace/Norwalk Camera Club</span><br>');
   document.write('<span style="line-height: 1.8;">' + year + ' Open Photography Contest</span><br>');
   document.write('<span style="font-size: xx-small">&nbsp;</span><br>');
   document.write('<img src="../images/thinrainbow.gif" style="height: 1; width: 600;"><br>');
   document.write('<span style="color: #CC0000;">WINNERS PAGE</span><br>');
   document.write('<img src="../images/thinrainbow.gif" style="height: 1; width: 600;"><br>');
   document.write('</div><br>'); 
   document.write('<table width="760" align="center" cellspacing="0" cellpadding="7" ' + 
                  ' border="1" bordercolor="#CCCCCC">');
   document.write('<tr style="height: 48">');
   document.write('<td width="20%" align="center">&nbsp;</td>'); 
   document.write('<td width="20%" class="header">Place</td>');   
   document.write('<td class="header">Photographer</td>'); 
   document.write('<td class="header">Photo Title&nbsp;</td>');   
   document.write('</tr>');
return;
}

function category(contest_category) { // e.g. category('Black and White Prints/General') 
   document.write('<tr style="height: 42">');
   document.write('<td colspan="4" class="category">' + contest_category + '</td>'); // e.g. 'Digital' 
   document.write('</tr>');
   return;
}

function winner(member, place, name, title, photo) {
   var blank = '&nbsp;';
   var member_cell = blank; 
   if (member != 0) member_cell = 'NCC member';
   var place_cell; var its_class = 'place';
   if (place == 0) { place_cell = 'BEST IN SHOW'; its_class = 'best_in_show';} 
   else if (place == 1) { place_cell = 'First'; }
   else if (place == 2) { place_cell = 'Second'; } 
   else if (place == 3) { place_cell = 'Third'; }
   var name_cell = blank + name;
   var title_cell = blank + title;
   if (photo != 0) {
      var view = '&nbsp;<a href="' + photo + '"><span style="color: #FFFFFF">(view)<span></a>'; 
	  title_cell += view;
   }
   document.write('<tr>');
   document.write('<td class="member">' + member_cell + '</td>');
   document.write('<td class="' + its_class + '">' + place_cell + '</td>');
   document.write('<td class="photographer">' + name_cell + '</td>');
   document.write('<td class="title">' + title_cell + '</td>');
   document.write('</tr>');
   return;
}

/* ******************** function monthlyWinnersBar is for all 1stYYMM.html pages ******************* */

function monthlyWinnersBar(interval) { // interval of months starting always with September, e.g. 'Sep-Jan'
   months = new Array(undefined,'Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug');
   fullMonths = new Array(undefined,'September','October','November','December','January','February',
                'March','April','May','June','July','August');
   mon_char = new Array(undefined,'09','10','11','12','01','02','03','04','05','06','07','08');
   var begin = interval.substring(0,3);
   var end = interval.substring(4); 
   if (begin != 'Sep') { 
   //document.write('Wrong months specified!'); 
   alert('Wrong months specified in monthlyWinnersBar(months)!'); 
   return; }
   var YYMM = getYYMM();
   var YY = YYMM.substring(0,2); var MM = YYMM.substring(2);
   var num_currMonth = parseInt('1' + MM) - 100; // to get around interpreting '0N's as octal numbers
   var ordered_currMonth; // 1 - for Sep, 2 - for Oct etc.
   if (num_currMonth >= 9) ordered_currMonth = num_currMonth - 8;
   else ordered_currMonth = num_currMonth + 4;
   var ordered_endMonth = undefined;
   for (i=1; i<=12; i++) { 
       if (months[i] == end) { ordered_endMonth = i; break; }	  
   }
   if (ordered_endMonth == undefined) { document.write('Wrong months specified!'); return; }
   if (ordered_currMonth > ordered_endMonth ) { document.write('Wrong months specified!'); return; }
   var char_currYear = '20' + YY; 
   var num_currYear = parseInt(char_currYear);
   var num_prevYear = num_currYear - 1;
   var num_nextYear = num_currYear + 1;
   if (num_currMonth >= 9) clubYear = char_currYear + '-' + num_nextYear; // from Sep to Dec
   else                    clubYear = num_prevYear + '-' + char_currYear; // Jan and after Jan
   var short_currMonth = months[ordered_currMonth];
   var full_currMonth = fullMonths[ordered_currMonth];     
   var Y1 = clubYear.substr(2,2); var Y2 = clubYear.substr(7,2);
   var bar_style = 'text-align: center; font: bold small Georgia, serif;'; // color: #003300;
   var h3 = '<h3 style="' + bar_style + '">Competition Year ' +clubYear+ ':&nbsp;  ';
   for (i=1; i<=ordered_endMonth; i++) {
	  if (i != ordered_currMonth) {
	     var html_file = '1st';
         if (i > 4) { html_file += Y2 + mon_char[i] + '.html'; }
	     else { html_file += Y1 + mon_char[i] + '.html'; }
	     h3 += '<a href="' + html_file +'">' + months[i] + '</a>';
      }
      else h3 += short_currMonth;
      h3 += '&nbsp;'
   }
   document.write(h3+'</h3>'); 
   var heading_style = 'text-align: center; color: #CC0000; font: bold large Georgia, sans-serif;'; // 
   var h2 = '<h2 style="' + heading_style + '">' + full_currMonth + ' ' + char_currYear;
   // h2 += '<span style="color: #FF0000;"> 1st </span>Place Winners';
   h2 += ' 1st place Winners';
   document.write(h2+'</h2>');
   return;
}

/* ******************** function categoryHeading is for all 1stYYMM.html pages ******************* */

function categoryHeading(arg) { 
   var style1 = 'text-align: center; color: #CC0000; font-family: Verdana, sans-serif;';
   // var style2 = 'color: #FF0000; font-style: italic;';
   var style2 = 'color: #FF0000; font-style: normal;';
   if (arg.substr(0,18).toUpperCase() != 'ASSIGNED SUBJECT: ') {
      document.write('<h4 style="' + style1 + '">' + arg.toUpperCase() + '</h4>');
   }
   else {
      assigned_subject = arg.substring(18);
	  document.write('<h4 style="' + style1 + '">ASSIGNED SUBJECT:&nbsp; ' + 
	  '<span style="' + style2 + '">' + assigned_subject + '</span></h4>');
   }
   return;
}

/************************** saved pair27 for 27.html ****************************************/
function convertYYMM(s) {
   return s;
}
function pair27(date1, name1, title1, filename1, width1, height1, 
                date2, name2, title2, filename2, width2, height2) {

var W1, H1, W2, H2, P1, P2, T1, T2;
var max = 300;
var DATE1, DATE2;
var FILENAME1, FILENAME2;

var just_one = false;
if (date2 == undefined) just_one = true;

if (date1 != '') DATE1 = date1; 
else DATE1 = '&nbsp;';
if (just_one == false) { 
   if (date2 != '') DATE2 = date2;
   else DATE2 = '&nbsp;'; 
}

if ((filename1 == '') || (filename1 == ' ')) { FILENAME1 = '../27_Photos/dummy.jpg'; W1 = 100; H1 = 100; }
else FILENAME1 = '../27_Photos/' + filename1;
if (width1 <= max && height1 <= max) { W1 = width1; H1 = height1; }
else if (width1 >= height1) { W1 = max; H1 = Math.round(max*height1/width1); }
else { H1 = max; W1 = Math.round(max*width1/height1); }
 
if (just_one == false) { 
   if ((filename2 == '') || (filename2 == ' ')) { FILENAME2 = '../27_Photos/dummy.jpg'; W2 = 100; H2 = 100; } 
   else FILENAME2 = '../27_Photos/' + filename2; 
   if (width2 <= max && height2 <= max) { W2 = width2; H2 = height2;}
   else if (width2 >= height2) { W2 = max; H2 = Math.round(max*height2/width2); }
   else { H2 = max; W2 = Math.round(max*width2/height2); }
}

var date_style = 'style="height: 30; text-align: center; color: #000099;'; 
date_style += ' font: bold medium Times New Roman, serif;"'; 
var td1_style = 'style="height: 362; text-align: center;"';
var td2_style = 'style="height: 28; text-align: center; color: #000099; font: bold medium Times New Roman, serif;"';
var span_style = 'style="font-weight: normal; font-style: italic;"';

if (just_one) document.write('<table width="752" height="422"  align="center" border="0" cols="1">');
else          document.write('<table width="752" height="422"  align="center" border="0" cols="2">');
document.write('<tr valign="middle">'); // the only row in this table
document.write('<td width="376">'); // cell for the left photo
if (just_one) document.write('<table width="374" height="392" cols="1" border="1" align="center">');
else          document.write('<table width="374" height="392" cols="1" border="1" align="left">');

document.write('<tr valign="middle"> <td ' + date_style + '>' + DATE1 + '</td> </tr>'); // left photo's date

document.write('<tr valign="middle">'); // left photo
document.write('<td ' + td1_style + '>');
P1 = '<img src="' + FILENAME1 + '" style="width: ' + W1 + '; height: ' + H1 + ';">';
document.write(P1 + '</td>');
document.write('</tr>');

document.write('<tr valign="middle">'); // left photo's title and name
document.write('<td ' + td2_style + '>');
T1 = '';
if (title1 != '') T1 = '<span ' + span_style + '>' + title1 + '</span> - '; 
T1 += name1;
document.write(T1 + '</td>');
document.write('</tr>');

document.write('</table>');
document.write('</td>'); // end of the cell for the left photo
if (just_one) {
   document.write('</tr>');
   document.write('</table>');
   document.write('<br>');
   return;
}

document.write('<td width="376">'); // cell for the right photo
document.write('<table width="374" height="392" cols="1" align="right" border="1">');

document.write('<tr valign="middle"> <td ' + date_style + '>' + DATE2 + '</td> </tr>');

document.write('<tr valign="middle">'); // right photo
document.write('<td ' + td1_style + '>');
P2 = '<img src="' + FILENAME2 + '" style="width: ' + W2 + '; height: ' + H2 + ';">';
document.write(P2 + '</td>');
document.write('</tr>');
document.write('<tr valign="middle">'); // right photo's title and name
document.write('<td ' + td2_style + '>');
T2 = '';
if (title2 != '') T2 = '<span ' + span_style + '>' + title2 + '</span> - '; 
T2 += name2;
document.write(T2 + '</td>');
document.write('</tr>');
document.write('</table>');
document.write('</td>'); // end of the cell for right photo

document.write('</tr>');
document.write('</table>');
document.write('<br>');
return;
}
/* ******************** function pair1st is for all 1stYYMM.html pages ******************* */

function pair1st(category1, name1, title1, filename1, width1, height1, 
                 category2, name2, title2, filename2, width2, height2) {

var W1, H1, W2, H2, P1, P2, T1, T2;
var max = 300;
var just_one = false;
if (category2 == undefined) just_one = true; 
var YYMM = getYYMM(); 
var FILENAME1, FILENAME2;

if ((filename1 == '') || (filename1 == ' ')) { FILENAME1 = '../27_Photos/dummy.jpg'; W1 = 100; H1 = 100; }
else FILENAME1 = '../1stPhotos/' + YYMM + 'firsts/' + filename1; 
if (width1 <= max && height1 <= max) { W1 = width1; H1 = height1; }
else if (width1 >= height1) { W1 = max; H1 = Math.round(max*height1/width1); }
else { H1 = max; W1 = Math.round(max*width1/height1); }

if (just_one == false) {
   if ((filename2 == '') || (filename2 == ' ')) { FILENAME2 = '../27_Photos/dummy.jpg'; W2 = 100; H2 = 100; }
   else FILENAME2 = '../1stPhotos/' + YYMM + 'firsts/' + filename2; 
   if (width2 <= max && height2 <= max) { W2 = width2; H2 = height2;}
   else if (width2 >= height2) { W2 = max; H2 = Math.round(max*height2/width2); }
   else { H2 = max; W2 = Math.round(max*width2/height2); }
}

var category_style = 'style="height: 30; text-align: center; color: #CC0000;'; 
category_style += ' font: bold x-small Verdana, sans-serif;"'; 
var td1_style = 'style="height: 362; text-align: center;"';
var td2_style = 'style="height: 28; text-align: center; color: #000099; font: bold medium;"'; 
var span_style = 'style="font-weight: normal; font-style: italic;"';
if (just_one) document.write('<table width="752" height="422"  align="center" border="0" cols="1">');
else          document.write('<table width="752" height="422"  align="center" border="0" cols="2">');
document.write('<tr valign="middle">'); // the only row in this table
document.write('<td width="376">'); // cell for the left (or central, if just one) photo
var cat = true; var height = 422;
if ((category1 == '') || (category1 == ' ') || (category2 == '') || (category2 == ' ')) {
   cat = false; height = 392;
}
if (just_one) document.write('<table width="374" height="' + height + '" cols="1" border="1" align="center">');
else          document.write('<table width="374" height="' + height + '" cols="1" border="1" align="left">');
if ((category1 != '') && (category1 != ' ')) {
   document.write('<tr valign="middle">'); // left photo' s category
   document.write('<td ' + category_style + '>' + category1.toUpperCase() + '</td>');
   document.write('</tr>');
}
document.write('<tr valign="middle">'); // left photo
document.write('<td ' + td1_style + '>');
P1 = '<img src="' + FILENAME1 + '" style="width: ' + W1 + '; height: ' + H1 + ';">';
document.write(P1 + '</td>');
document.write('</tr>');
document.write('<tr valign="middle">'); // left photo's title and name
document.write('<td ' + td2_style + '>');
T1 = '';
if (title1 != '') T1 = '<span ' + span_style + '>' + title1 + '</span> - '; 
T1 += name1;
document.write(T1 + '</td>');
document.write('</tr>');
document.write('</table>');
document.write('</td>'); // end of the cell for left photo
if (just_one) {
   document.write('</tr>');
   document.write('</table>');
   document.write('<br>');
   return;
}

document.write('<td width="376">'); // cell for the right photo
document.write('<table width="374" height="' + height + '" cols="1" border="1" align="right">');
if (cat) {
   document.write('<tr valign="middle">'); // right photo's category
   document.write('<td ' + category_style + '>' + category2.toUpperCase() + '</td>');
   document.write('</tr>');
}
document.write('<tr valign="middle">'); // right photo
document.write('<td ' + td1_style + '>');
P2 = '<img src="' + FILENAME2 + '" style="width: ' + W2 + '; height: ' + H2 + ';">';
document.write(P2 + '</td>');
document.write('</tr>');
document.write('<tr valign="middle">'); // right photo's title and name
document.write('<td ' + td2_style + '>');
T2 = '';
if (title2 != '') T2 = '<span ' + span_style + '>' + title2 + '</span> - '; 
T2 += name2;
document.write(T2 + '</td>');
document.write('</tr>');
document.write('</table>');
document.write('</td>'); // end of the cell for right photo

document.write('</tr>');
document.write('</table>');
document.write('<br>');
return;
}

/* ****** function winnerNoImage is for all 1stYYMM.html pages, when there is no image available ***** */

function winnerNoImage(category, name, title, br) {
   var category_style = 'style="color: #CC0000; font: bold x-small Verdana, sans-serif;"'; 
   var name_style = 'style="color: #000099; font: bold medium;"'; 
   var title_style = 'style="color: #000099; font: normal italic;"';
   document.write('<table width="752"  align="center" rows="1" cols="1">');
      document.write('<tr valign="middle">'); 
         var str = '<td style="text-align: center">';
         str += '<span ' + category_style + '>' + category.toUpperCase() + '</span>';
         str += '<span style="font-size: small; color: #CC0000;">&nbsp; &#149; &nbsp;</span>'; // bullet
         str += '<span ' + name_style + '>' + name;
         if (title != '') str += '<span ' + title_style + '> - ' + title; 
         str += '</td>';
         document.write(str);
      document.write('</tr>');
   document.write('</table>');
   if (br == 1) document.write('<br />');
return;
}

/* ****** function skip(n) to skip n lines  ***** */

function skip(n) {
   var br = '';
   for (i=1; i<=n; i++) { br += '<br />' };
   document.write('<div>' + br + '</div>');
}

function message(msg,h1,h2) {
   if (h1 == undefined) { h1 = 50; h2 = 30; }
   if (h2 == undefined) h2 = 30;
   if (h1 != 0) document.write('<div height="' + h1 + '">&nbsp;</div>');
   var div = '<div style="height: ' + h2 + '; ';
   div += 'text-align: center; color: #FF0000; font: bold medium Geneva, sans-serif;">';
   document.write(div);
   document.write(msg);
   document.write('</div>');
   return;
}

function getYYMM() { // returns a 'YYMM' character string if a filename ends with 'YYMM' 
                     // as in '1stYYMM.html' or 'eventsYYMM.html'
   var filename = nameOfCurrentPage(3); 
   var xend = filename.lastIndexOf(".");
   return filename.substring(xend-4,xend); // YYMM
}

function nameOfCurrentPage(n) {

	/* This script gets the URL of the current page, then extracts the path and file name from the URL */

	var URL = unescape(location.href);	// get current URL in plain ASCII
    if (n == 1) return URL;

	var xstart = URL.lastIndexOf("/") + 1;
    var herePath = URL.substring(0,xstart);
	if (n == 2) return herePath;

	var xend = URL.length;
	var hereName = URL.substring(xstart,xend);
	if (n == 3) return hereName;
	return '';
}