/*
    
  
This lib contains the "plugin" functions required to 
price up various product options such as the cost of 
adding a name and number to a football shirt.

To include an option in here, you must give the controlling
form field a unique name!

*/ 
    
// This is the master function - call this to enable evaluation of plug-in modifiers

function checkOptions() {

	modValue = parseFloat('0');

	if (document.form.options) {
	optionList = document.form.options.value.split(" ");

	for (i=0;i < optionList.length; i++) {
		if (optionList[i]) {

				thisVal = eval('document.form.' + optionList[i] + '.value');
				if (thisVal) {

					// Options pricing rules in here:
					
					(thisVal == "perletter" ? modValue += perletter() : modValue = modValue);
					(thisVal == "fixedprice" ? modValue += parseFloat(fixedprice()) : modValue = modValue);

				}
				
		}
	}
	
	}
	return modValue;
}

// All functions below here are the individual functions responsible for evaluating the various options


function perletter () {
	
	numLetters = 0

	if (document.form.nameOnShirt) {
		numLetters = document.form.nameOnShirt.value.length;
		if (document.form.nameOnShirt.value.indexOf(" ") > -1) {
			numLetters = numChars(document.form.nameOnShirt.value);
		}
		numLetters *= 1;
		costPerLetter = perlettersplit("letters");
		}

	if (document.form.numbersOnShirt) {
		numNumbers = document.form.numbersOnShirt.value.length;
		costPerNumber = perlettersplit("numbers")
	}


	returnVal = (numLetters * costPerLetter) + (numNumbers * costPerNumber);		

	return returnVal;
}

function perlettersplit(whichVal) {

	var returnVal = whichVal;
	
	myPrice = document.form.nameOnShirtPrice.value.indexOf(":");

	if (myPrice > -1) {
	
		priceOptions = document.form.nameOnShirtPrice.value.split(":");
	
		myLetterPrice = priceOptions[0];
		myNumberPrice = priceOptions[1];

	} else {
	
		myLetterPrice = document.form.nameOnShirtPrice.value;
		myNumberPrice = myLetterPrice;
	
	}
		

	if (returnVal == "letters") {
		return myLetterPrice;
	} else {
		return myNumberPrice;
	}


}
function fixedprice () {

	if (document.form.nameOnShirtPrice) {
		if (document.form.nameOnShirt.value != "" || document.form.numbersOnShirt.value != "") {
			return document.form.nameOnShirtPrice.value;
		}
	} 
	
return 0;

}

function nocharge () {
}


function numChars(thisText) {

	var checkText = thisText;
	var checkStr = "";
	var numSpaces = 0;

	for (i=0; i < checkText.length; i++) {	

		if (checkText.charAt(i) != " ") {
			checkStr += checkText.charAt(i);
		}			

	}

	return checkStr.length
}