// **********************************************************************
// * magicsoc.js contains declarations for the magicsoc object and
// * declares an instance of the magicsoc object.
// ***********************************************************************/ 

 //alert ('magicsoc');   

// create a new magicsoc entry
function entry (theId, theRegion,theUrl,theImage,theName, theContact, 
                theAddr1, theAddr2, theAddr3, theEmail, 
                thePos1, thePos2, thePos3, theJnr, theMeetDay, theMeet, theMeetMap, theDate) {
//alert ('magicsoc ENTRY')

   this.id = theId;
   this.region = theRegion;
   this.url	= theUrl;
   this.image = theImage;
   this.name = theName;
   this.contact = theContact;
   this.addr1 = theAddr1;
   this.addr2 = theAddr2;
   this.addr3 = theAddr3;
   this.email = theEmail;
   this.pos1 = thePos1;
   this.pos2 = thePos2;
   this.pos3 = thePos3;
   this.meet = theMeet;
   this.meetmap = theMeetMap;
   this.date = theDate;
   this.jnr = theJnr;
   this.meetday = theMeetDay;
}


// *********************************************
// * magicsoc constructor creates a new magicsoc
// *********************************************
function magicsoc () {
//alert ('magicsoc main')

    this.numberEntries = 0;      
    this.entries = new Array();  
    this.addEntry = magicsoc_addEntry; 
    this.displayregion = magicsoc_displayregion;  
    this.displayregion2 = magicsoc_displayregion2;
    this.dispheader = magicsoc_dispheader;   
    this.displaysocimagemap = magicsoc_displaysocimagemap;
    this.showsoc = magicsoc_showsoc; 
    this.menuindex = magicsoc_menuindex; 

}

// **********************************************************
// magicsoc_addEntry adds an item to the magicsoc table
// **********************************************************
function magicsoc_addEntry (entryId, entryRegion, entryUrl, entryImage, entryName, entryContact, 
		entryAddr1, entryAddr2, entryAddr3, entryEmail, 
		entryPos1, entryPos2, entryPos3, entryJnr, entryMeetDay, entryMeet, entryMeetMap, entryDate) {
  //alert ('add entry ' + entryName);
   this.numberEntries += 1;
   this.entries[this.numberEntries-1] = new entry(entryId, entryRegion, entryUrl, entryImage, entryName, entryContact, entryAddr1, entryAddr2, entryAddr3,entryEmail, entryPos1, entryPos2, entryPos3, entryJnr, entryMeetDay, entryMeet, entryMeetMap, entryDate);    

}

// **************************************************************
// * Internal function to create Table Row details for a society
// **************************************************************
function displaysocietyrow (theEntry) {

   var textstring = "";
       textstring +=  '<TR>';

		//*********************************
		// Col 1: Image and Link 
		//*********************************
	    textstring +=  '   <TD Width="100" Height="90" valign="middle">' ;
	    textstring +='<div id="' + theEntry.id + '" name="' + theEntry.id + '">';

		if (theEntry.url != "") {
			textstring +=  '<a href="' + theEntry.url + '" target="_blank">';
		} // end if
		textstring +=  '<img src="images/logos/' + theEntry.image + '" border="0">';
		if (theEntry.url != "") {
			textstring +=  '</a>' ;
		} // end if

        textstring +='</div>';	        
		textstring +=  '</TD>';
	    
	    //*********************************
		// Col 2: Details 
		//*********************************
		textstring +=  '<TD valign="top"><b>' + theEntry.name + '</b><br>';
		if (theEntry.contact != "") {
			textstring += theEntry.contact + '<br>' ;
		}
		if (theEntry.addr1 != "") {
			textstring +=  theEntry.addr1 + '<br>';
		}
		if (theEntry.addr2 != "") {
			textstring +=  theEntry.addr2 + '<br>';
		}
		if (theEntry.addr3 != "") {
			textstring +=  theEntry.addr3 + '<br>';
		}
		if (theEntry.email != "") {
			textstring +=  'Email: ' + theEntry.email  + '<br>';
		}
		if (theEntry.jnr != "") {
			textstring +=  theEntry.jnr  + '<br>';
		}
		if (theEntry.meetday != "") {
			textstring +=  'Meetings: ' + theEntry.meetday  + '<br>';
		}

		if (theEntry.meet != "") {
			textstring +=  'HQ: ' + theEntry.meet  + '<br>';
		}
	   
	    //*********************************
		// Last Updated 
		//*********************************
		textstring +=  'Last Updated: <i>' + theEntry.date + '</i><br><br>';
	    textstring +=  '</TD>';
	        
	    //*********************************
		// Col 3: Mail 
		//*********************************
		textstring +=  '<TD align=left valign="middle">';
		if (theEntry.email != "") {
			textstring +=  '<a href="mailto:' + theEntry.email + '">';
			textstring +=  '<img src="images/email.gif" border="0" ';
			textstring +=  ' alt="Email: ' + theEntry.name + '"></a><br>';
		}
	
		if (theEntry.url != "") {
			textstring +=  '<a href="' + theEntry.url + '" target="_blank">';
			textstring +=  '<img src="images/iconweb.gif" border="0" ';
			textstring +=  ' alt="Web: ' + theEntry.url + '"></a><br>';
		}
		if (theEntry.meetmap != "") {
			textstring +=  '<a href="http://www.streetmap.co.uk/streetmap.dll?Postcode2Map?code=';
			textstring +=  theEntry.meetmap + '&title=' + theEntry.name + '&nolocal=X" target="_blank">';
			textstring +=  '<img src="images/iconmap.gif" border="0" ';
			textstring +=  ' alt="Map of Meeting Point"></a>';
		}

	    textstring +=  '</TD>';  
		textstring +=  '</TR>';
        
        return textstring;
}

// **********************************************************
// magicsoc_displaysocimagemap
// Creates a TableRow
// **********************************************************
function magicsoc_displaysocimagemap () {

	var args = getArgs();
	var theRegion = "east";
	if (args.region) theRegion = args.region;

	//alert(theRegion);

	// ***************************************
	//  Set Imagenames
	// ***************************************
    var imagename = "construction.gif";
    var imagedetail = ' width="242" height="82" ';
    
    if (theRegion.toLowerCase() == "south east") {
	    imagename = 'region_southeast.gif';
	    imagedetail = ' width="440" height="340"';
    }
    if (theRegion.toLowerCase() == "south west") {
	    imagename = 'region_southwest.gif';
	    imagedetail = ' width="434" height="340"';
    }
    if (theRegion.toLowerCase() == "east") {
	    imagename = 'region_east.gif';
	    imagedetail = ' width="323" height="340"';
    }
    if (theRegion.toLowerCase() == "east midlands") {
	    imagename = 'region_eastmid.gif';
	    imagedetail = ' width="309" height="340"';
    }
    if (theRegion.toLowerCase() == "west midlands") {
	    imagename = 'region_westmid.gif';
	    imagedetail = ' width="325" height="340"';
    }
    if (theRegion.toLowerCase() == "north west") {
	    imagename = 'region_northwest.gif';
	    imagedetail = ' width="308" height="400"';
    }
    if (theRegion.toLowerCase() == "north east") {
	    imagename = 'region_northeast.gif';
	    imagedetail = ' width="214" height="340"';
    }
    if (theRegion.toLowerCase() == "yorkshire") {
	    imagename = 'region_yorkshire.gif';
	    imagedetail = ' width="393" height="340"';
    }
    if (theRegion.toLowerCase() == "wales") {
	    imagename = 'region_wales.gif';
	    imagedetail = ' width="267" height="340"';
    }
    if (theRegion.toLowerCase() == "scotland") {
	    imagename = 'region_scotland.gif';
	    imagedetail = ' width="285" height="400"';
    }
    if (theRegion.toLowerCase() == "ireland") {
	    imagename = 'region_ireland.gif';
	    imagedetail = ' width="279" height="340"';
    }
    if (theRegion.toLowerCase() == "london") {
	    imagename = 'region_london.gif';
	    imagedetail = ' width="404" height="340"';
    }
    if (theRegion.toLowerCase() == "national") {
	    imagename = 'region_national.gif';
	    imagedetail = ' width="121" height="150"';
    }

    //alert(imagename + ' ' + imagedetail);

	// ***************************************
	//  Title Row
	// ***************************************
	document.write('<TR><TD><IMG SRC="images/spacer.gif" HEIGHT="10"></TD></TR>' );
	document.write('<TR align=left><TD><div class="titletext">');
	document.write('Magic Societies: [' + theRegion + ']</div></TD></TR>' );
	document.write('<tr><td><img src="images/purpledot.gif" height=1 width="100%"></td></tr>' );
	document.write('<TR><TD><IMG SRC="images/spacer.gif" HEIGHT="10"></TD></TR>' );

	// ***************************************
	//  Table Row and Column
	// ***************************************
	document.write ( '<tr>' );
	
	// ***************************************
	//  Map Column
	// ***************************************
	document.write ( '<td valign="top" align="center">' );
	document.write ( '<map name="FPMap0">');
	
	// ***************************************
	//  Process each entry in the collection
	// ***************************************
   for (var i = 0; i <= this.numberEntries -1; i++) {

        var my = this.entries[i];
        if (my.region.toLowerCase() == theRegion.toLowerCase()) {
			document.write ( '<area alt="' + my.name + '" ');
			document.write ( '     shape="circle" href="#' + my.id + '" ');
			document.write ( '     coords="' + my.pos1 + ', ' + my.pos2 + ', ' + my.pos3 + '">' );
        }
    }
    
    document.write ( '</map>' );
    document.write ( '<img src="images/maps/' + imagename + '" ');
    document.write ( '     border="0" usemap="#FPMap0" ');
    document.write ( imagedetail + '>');
    document.write ( '</td>');

    document.write ( '</tr>');

}

function magicsoc_menuindex () {

  	// ***************************************
	//  Index Column
	// ***************************************
    document.write ( '<tr>');
    document.write ( '<td>');
    document.write ( '<Table class="footer" width="450">');
    document.write ( '<tr align="left">');
    document.write ( '<td><a href="linsocregion.htm?region=South West">South West</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=North West">North West</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=West Midlands">West Mid</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=London">London</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=Yorkshire">Yorkshire</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=Scotland">Scotland</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=Offshore">Offshore</a></td>');
	document.write ( '</tr>');
    document.write ( '<tr align="left">');
	document.write ( '<td><a href="linsocregion.htm?region=South East">South East</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=North East">North East</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=East Midlands">East Mid</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=East">East</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=Wales">Wales</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=Ireland">Ireland</a></td>');
	document.write ( '<td><a href="linsocregion.htm?region=National">National</a></td>');
	document.write ( '<td></td>');
    document.write ( '</tr>');
    document.write ( '</table>');
	document.write ( '</td>' );
    document.write ( '</tr>');

}

// *****************************************************
// * magicsoc_display outputs the magic society table
// * built from the contents of magicsocitems.js
// *****************************************************
function magicsoc_showsoc (theId) {

	// ***************************************
	//  Process each entry in the collection
	// ***************************************
   var textstring = "";
   
   for (var i = 0; i <= this.numberEntries -1; i++) {

        var my = this.entries[i];
        if (my.id == theId) {
           //alert (my.name + ' ' + theId);

        	textstring = '<table class="tbltext" cellspacing="0" cellpadding="0" ';
        	textstring +=  '      border="0" width="450" >';

            textstring += displaysocietyrow (my)


			textstring += '</Table>';
			 
			return textstring;
           
        }
   }     

}

// *****************************************************
// * magicsoc_display outputs the magic society table
// * built from the contents of magicsocitems.js
// *****************************************************
function magicsoc_displayregion (theRegion) {
   
   	var iRegion=0;

	// ***************************************
	// Table Header Row Details
	// ***************************************
	document.write ( '<DIV class="ListNuggetBody" id="Link' + theRegion + 'Body" name="Link' + theRegion + 'Body">' );
	
	document.write ( '<table class="tbltext" cellspacing="0" cellpadding="0" ' );
	document.write ( '      border="0" width="450" id="tablesoc' + theRegion + '">' ); 

	// ***************************************
	//  Process blank line if no details
	// ***************************************
	if (this.numberEntries == 0) {
		document.write ( '<TR><TD>&nbsp;</TD><TD&nbsp;</TD></TD><TD&nbsp;</TD></TR>' );
	}

	// ***************************************
	//  Process each entry in the collection
	// ***************************************
   for (var i = 0; i <= this.numberEntries -1; i++) {

        var my = this.entries[i];
        if (my.region.toLowerCase() == theRegion.toLowerCase()) {
            
//alert ('magicsoc display B ' + my.name);                 
	        iRegion += 1; 

            document.write (displaysocietyrow(my))

		   //*********************************
		   // Break Line
		   //*********************************
	        document.write( '<TR><TD colspan="3">' );
	        document.write( '<img src="images/purpledot.gif" height="1" width="100%">' );
	        document.write( '</TR></TD>' );

		} // End If Region Matches    
     }  // End for

//alert ('magicsoc display C ' + this.numberEntries);                 

     // ****************************************************************
     // * Last Rows for links to other sites with soc list (remove?)
     // ****************************************************************
	 document.write ( '<tr><td colspan="3">&nbsp;</td></tr>' );
	 document.write ( '<tr><td></td><td colspan="2">' );
	 document.write ( 'Total Societies = ' + iRegion + ' of ' + this.numberEntries + '</td></tr>' );
	 document.write ( '<tr><td colspan="3">&nbsp;</td></tr>' );


   // ***************************************
   //  End the Table
   // ***************************************
     document.write ( '</TABLE>' );
	 document.write ( '</DIV' );

} // End Function

//************************************************
//* Show all Societies for Region 
//* Region Passed in by argument set from previous page
//************************************************
function magicsoc_displayregion2 () {
   
   	var iRegion=0;
	var args = getArgs();
	var theRegion = "East";
	if (args.region) theRegion = args.region;
	

	// ***************************************
	// Table Header Row Details
	// ***************************************
	//document.write ( '<DIV class="tableborder">' );
	//alert (my.id);
	//document.write ( '<DIV id="' + my.id + '" name="' + my.id + '">' );
	document.write ( '<DIV >' );
	document.write ( '<table class="tbltext" cellspacing="0" cellpadding="0" ' );
	document.write ( '      border="0" width="450" id="tablesoc' + theRegion + '">' ); 

	// ***************************************
	//  Process blank line if no details
	// ***************************************
	if (this.numberEntries == 0) {
		document.write ( '<TR><TD>&nbsp;</TD><TD&nbsp;</TD></TD><TD&nbsp;</TD></TR>' );
	}

	// ***************************************
	//  Process each entry in the collection
	// ***************************************
   for (var i = 0; i <= this.numberEntries -1; i++) {

        var my = this.entries[i];
        if (my.region.toLowerCase() == theRegion.toLowerCase()) {
            
//alert ('magicsoc display B ' + my.name);                 
	        iRegion += 1; 


            document.write (displaysocietyrow(my))

		   //*********************************
		   // Break Line
		   //*********************************

	        document.write( '<TR><TD colspan="3">' );

	        document.write( '<img src="images/purpledot.gif" height="1" width="100%">' );
	        document.write( '</TR></TD>' );

		} // End If Region Matches    
     }  // End for

//alert ('magicsoc display C ' + this.numberEntries);                 

     // ****************************************************************
     // * Last Rows for links to other sites with soc list (remove?)
     // ****************************************************************
	 document.write ( '<tr><td colspan="3">&nbsp;</td></tr>' );
	 document.write ( '<tr><td></td><td colspan="2">' );
	 document.write ( 'Total Societies = ' + iRegion + ' of ' + this.numberEntries + '</td></tr>' );
	 document.write ( '<tr><td colspan="3">&nbsp;</td></tr>' );


   // ***************************************
   //  End the Table
   // ***************************************
     document.write ( '</TABLE>' );
	 document.write ( '</DIV' );

} // End Function

//*************************************************
//* Display Collapsable regionHeader
//*************************************************
function magicsoc_dispheader (theRegion, theDisplayRegion) {

	document.write( '<TABLE class="ListNuggetHeaderClosed" width="100%" cellSpacing="0" cellPadding="0" ' );
	document.write( '		id="Link' + theRegion + 'Header" name="Link' + theRegion + 'Header">' );
	document.write( '	<TR>' );
	
	document.write( '		<TD class="ListNuggetTitleCellBlue" ');
	document.write( '           onselectstart="window.event.cancelBubble=true; return false;" ' );

	document.write( '			onclick="PartWrapperToggle(\'Link' + theRegion + '\');">' );
	document.write( '		<A class="ListNuggetTitle" ' );
	document.write( '			onclick="return PartWrapperToggle(\'Link' + theRegion + '\');" ' );
	document.write( '			href="javascript:PartWrapperToggle(\'Link' + theRegion + '\');">' );
	document.write( theDisplayRegion);
	document.write( '		</A>');
	document.write( '		</TD>' );
	document.write( '		<TD class="ListNuggetButtonCellBlue" ' );
	document.write( '			onclick="PartWrapperToggle(\'Link' + theRegion + '\');">' );
	document.write( '			<DIV class="ListNuggetButton">' );
	document.write( '			<IMG class="ListNuggetUpButton" ' );
	document.write( '				id="Link' + theRegion + 'Up" ' );
	document.write( '				height="17" alt="Hide" src="images/gold-chevron_up.gif" ' );
	document.write( '				width="17" align="right" border="0" ' );
	document.write( '				name="Link' + theRegion + 'Up">' );
	document.write( '			<IMG class="ListNuggetDownButton" ' );
	document.write( '				id="Link' + theRegion + 'Down" ' );
	document.write( '			    height="17" alt="Show" src="images/gray-chevron_down.gif" ' );
	document.write( '				width="17" align="right" border="0" ' );
	document.write( '				name="Link' + theRegion + 'Down">' );
	document.write( '			</DIV>' );
	document.write( '		</TD>' );
	document.write( '	</TR>' );
	document.write( '</TABLE>' );
}

// get querystring arguments
function getArgs() {

	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
   }

// *****************************************
// * Create an instance of the Magicsoc object
// *****************************************
var Magicsoc = new magicsoc();
