// display popup window
function addTrophyDetail () {
	// pull the trophy count and interatively call the field adding script
	var trophyCount = document.getElementById('awards-quantity').value;
	
	var trophyDetailsWindow = window.open('','trophydetails','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=450,height=' + (trophyCount * 150 + 300) + ',left = 412,top = 284');
	
	// render the form and inputs
	displayTrophyForm(trophyDetailsWindow, trophyCount);
	
	return false;
}

// function to display content in the popup
function displayTrophyForm(trophyDetailsWindow, trophyCount) {
	trophyDetailsWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Engraving Details</title><script type="text/javascript" src="/store/includes/javascripts/z_trophy-popup.js"></script><link rel="stylesheet" type="text/css" href="/awards_style.css"/></head><body style="padding-bottom: 100px;"><h2>Add Trophy Detail</h2><p><h4>If the wizard is blank below (PRESS F5 TO REFRESH)<br>Please enter the engraving into the boxes below, when completed press the continue button to proceed.<br>Engraving will be centered on the engraving plate.<h4></p><form>');
	for (var i = 0; i < trophyCount && i < 300; i++) {
		addTrophyLineInputFields(trophyDetailsWindow, trophyCount, i + 1);
	}
	trophyDetailsWindow.document.write('<label style="display:block;"><input type="submit" value="Close" onclick="javascript:window.close();" /> <input type="submit" value="Continue" onclick="javascript:return processTrophyContents(' + trophyCount + ');" /></label></form></body></html>');
}

// function to add the three input fields
function addTrophyLineInputFields(trophyDetailsWindow, trophyCount, trophyNumber) {
	trophyDetailsWindow.document.write('<h3>Details for Trophy ' + trophyNumber + '</h3><label style="display:block; margin: 10px 0;"><span>Line 1:</span><input id="line1-' + trophyNumber + '" type="text" value=""/><input style="margin-left: 20px;" type="button" value="Apply to remaining" onclick="javascript:return applyToRemaining(' + trophyCount + ', \'line1\', ' + trophyNumber + ')"/></label><label style="display:block; margin: 10px 0;"><span>Line 2:</span><input id="line2-' + trophyNumber + '" type="text" value=""/><input style="margin-left: 20px;" type="button" value="Apply to remaining" onclick="javascript:return applyToRemaining(' + trophyCount + ', \'line2\', ' + trophyNumber + ')"/></label><label style="display:block; margin: 10px 0;"><span>Line 3:</span><input id="line3-' + trophyNumber + '" type="text" value=""/><input style="margin-left: 20px;" type="button" value="Apply to remaining" onclick="javascript:return applyToRemaining(' + trophyCount + ', \'line3\', ' + trophyNumber + ')"/></label>');
}

// function to apply the contents of an item to the remaining boxes

function applyToRemaining(trophyCount, lineLevel, trophyNumber) {
	var applyableContents = document.getElementById(lineLevel + '-' + trophyNumber).value;
	
	for (var i = trophyNumber; i < trophyCount; i++) {
		var currentTrophyNumber = (i + 1);
		document.getElementById(lineLevel + '-' + currentTrophyNumber).value = applyableContents;
	}

	return true;
}

// return the contents of the popup to the parent
function processTrophyContents(trophyCount) {
	var parsedTrophyDetails = '';
	for (var i = 0; i < trophyCount; i++) {
		var trophyNumber = (i + 1);
		var thisTrophyDetails = '\n';
		thisTrophyDetails += document.getElementById('line1-' + trophyNumber).value + '\n';
		thisTrophyDetails += document.getElementById('line2-' + trophyNumber).value + '\n';
		thisTrophyDetails += document.getElementById('line3-' + trophyNumber).value + '\n';
		parsedTrophyDetails += thisTrophyDetails;
	}
	window.opener.document.getElementById('awards-xfield1').value = parsedTrophyDetails;
	window.close();
	
	return false;
}