//
// 	Project:	PJM Pacific Journal of Mathematics Design
//
// 	Author:		Ev Shafrir
// 	Date:		March 21, 2005
//	Modified:	April 14, 2005	
//
// (c) Copyright 2005 Lingua Franca Design
//

var firstVolume = 1;
var lastVolume = 176;		// this needs to be computationally constructed
var issueLinks = ['N1', 'N2', 'N3', 'N4'];
var windowName = "PJMARCHIVE";		

function highLightId (anyThing) {

	highLight (document.getElementById(anyThing));
}

function dimLightId (anyThing) {		// convert ID to OBJECT

	dimLight (document.getElementById(anyThing));
}

function highLight (anObject) {			// by naming convention, the last character of class name 
										// denote states of classes: '-d' dim; '-h' highlighted;
	var currentClass;
	
	if (anObject) {
		currentClass = anObject.className;
		if (currentClass) {					// ergo a class has been defined 
			anObject.className = currentClass.replace("-d","-h");	// all but last two chars
		}
	}
}

function dimLight (anObject) {			// by naming convention, the last character of class name 
										// denote states of classes: '-d' dim; '-h' highlighted;
	var currentClass;
	
	if (anObject) {
		currentClass = anObject.className;
		if (currentClass) {					// ergo a class has been defined 
			anObject.className = currentClass.replace("-h","-d");	// all but last three chars
		}
	}
}

function validateField (volumeField) {
		// arrives here with each keystroke into the field
		// object is expected parameter 'this'
		// alert ( entry is not a valid number); 
				// crop junk off the number and put it back in the cage;
		// alert ( when number is not within valid range)
		// then foreach ( valid issue for this volume) 
				// activate the appropriate buttons by swapping classes
				
	var requestedVolume = volumeField.value;
	var intVol;
	var aLink;		// id string of a link wrapped by <div>
	
	if (requestedVolume == "") {		// field is empty	
		for (i = 0; i < 4; i++)		// hide all available issue links
			dimLightId (issueLinks[i]);
	}
	else { 											// field is not empty
		intVol = parseInt (requestedVolume);		// convert string to integer
		
		if (isNaN (intVol)) {	// if it's NOT a number...
			volumeField.value = "";
			volumeField.focus();
			for (i = 0; i < 4; i++)		// hide all available issue links
				dimLightId (issueLinks[i]);
			return (true);
		}
								
		if (intVol < firstVolume || intVol > lastVolume) { // so it's an integer, but out of valid range
			for (i = 0; i < 4; i++)		// hide all available issue links
				dimLightId (issueLinks[i]);
			alert ("Please select a volume number between "+firstVolume+" and "+lastVolume+".");
			volumeField.focus();
			return (true);
		}
		
		// here we have a valid volume number
		// enable/disable issue buttons for each non-zero key in that volume row
		
		for (i = 0; i < 4; i++)		
			(doesIssueExist(intVol,i+1) ? highLightId (issueLinks[i]) : dimLightId (issueLinks[i]));
		volumeField.value = intVol.toString();	// put back a 'cleaned' string with digits only
		return (true);
	}
}

function viewIssue (theVolumeId, issueNumber) {
		// verify volumn number
		// then concatenate the number provided
		// finally look up actual URL from table
		
		var theURL;
		var volumeNumber = parseInt (document.getElementById(theVolumeId).value);
		// serious limitations with window names: no spaces, no funny characters, only alphanumerics
		// the SAME window is resued for all archive issues display
		
		theURL = lookupURL (volumeNumber, issueNumber);
		window.open(theURL, windowName);
		return (true);
}

// End