function changeImageOnSelect(select,image) 
{
	var imageControl  = document.getElementById(image);
	if ( imageControl == null )
		return;
		
	var selectControl = document.getElementById(select);
	if ( selectControl == null )
		return;
	
	var selIndex = selectControl.options.selectedIndex
	var selValue = selectControl.options[selIndex].value
	imageControl.src = selValue;
}

function computeCustomPrice(textID, labelID, basePrice, freeChars, addPrice)
{
	var textControl = document.getElementById(textID);
	if ( textControl == null )
		return;
		
	var labelControl = document.getElementById(labelID);
	if ( labelControl == null )
		return;
	
	var wording = textControl.value;
	wording = wording.replace(/\s/g, "");
	
	var price = computePrice(wording, basePrice, freeChars, addPrice)
	
	labelControl.innerHTML = "$" + price + ".00";
}

function computePrice(wording, basePrice, baseChars, addPrice)
{
	if ( addPrice == 0 )
	{
		return basePrice;
	}
	
	var numChars = wording.length;
	if ( numChars <= baseChars )
	{
		return basePrice;
	}
	
	return parseInt(basePrice) + (addPrice * (numChars - baseChars));
}

function SetPaypalOptions(pname, pnum, basePrice, baseChars, addPrice, fabID, prodID, sizeID, crystalID, crystalOutID, fontID, textID, reqID)
{
	var options = "";
	
	if ( fabID != null )
	{
		var fabControl = document.getElementById(fabID);
		if ( fabControl != null ) {
		    if ( fabControl.selectedIndex >= 0 ) {
			    options = fabControl[fabControl.selectedIndex].text + " ";
		    }
		}
	}
		
	if ( prodID != null )
	{
		var prodControl = document.getElementById(prodID);
		if ( prodControl != null )
		{
			var prodName = prodControl[prodControl.selectedIndex].text;
			prodName = AbbreviateProductName(prodName);
			options = trimString(options) + " " + prodName;
		}
	}
		
	if ( sizeID != null )
	{
		var sizeControl = document.getElementById(sizeID);
		if (sizeControl != null ) {
		    if (sizeControl.selectedIndex >= 0) {
		        options = trimString(options) + ". Size " + sizeControl[sizeControl.selectedIndex].text;
		    }
		}
	}
	options = trimString(options) + ". ";
	
	var custom = "";
	if ( textID != null )
	{
		var textControl = document.getElementById(textID);
		var theText = ReplaceControlCharacters(textControl.value);
		custom = "Say: '" + theText + "'.";
	}
	
	if ( crystalID != null )
	{
		var crystalControl = document.getElementById(crystalID);
		var crystalName = AbbreviateCrystalName(crystalControl[crystalControl.selectedIndex].text);
		if ( custom.length > 0 ) custom = custom + " ";
		custom = custom + crystalName;
	}
	
	if ( crystalOutID != null )
	{
		var crystalControlOuter = document.getElementById(crystalOutID);
		var crystalName = AbbreviateCrystalName(crystalControlOuter[crystalControlOuter.selectedIndex].text);
		custom = custom + " (" + crystalName + " outer).";
	}
	else if ( crystalID != null )
	{
		custom = custom + ".";
	}
	
	if ( fontID != null )
	{
		var fontControl = document.getElementById(fontID);
		var fontName = AbbreviateFontName(fontControl[fontControl.selectedIndex].text);
		if ( custom.length > 0 ) custom = custom + " ";
		custom = custom + fontName + " font.";
	}
	
	var price;
	
	if ( textID == null )
	{
		price = basePrice;
	}
	else
	{
		var textControl = document.getElementById(textID);
		price = computePrice(textControl.value.replace(/\s/g, ""), basePrice, baseChars, addPrice);
	}

	var sURI = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&add=1&business=threegirlygirls@hotmail.com" 
				+ "&item_name=" + pname
				+ "&item_number=TGG-" + pnum
				+ "&os0=" + options
				+ "&on1=" + custom 
				+ "&amount=" + price
				+ "&handling=0&quantity=1&no_shipping=2&no_note=1&cs=0&currency_code=USD&on0=Options";
	sURI = encodeURI(sURI);

	var link = document.getElementById('AddToCartButtonLink');
	link.href = sURI;
}

function ReplaceControlCharacters(chars)
{
	chars = chars.replace(/\?/g, "(questionmark)")
	chars = chars.replace(/\&/g, "(ampersand)")
	chars = chars.replace(/\%/g, "(percent)")
	chars = chars.replace(/\#/g, "(pound)")
	return chars;
}

function AbbreviateProductName(prod)
{
	prod = prod.replace("Short Sleeve", "SS");
	prod = prod.replace("Long Sleeve", "LS");
	prod = prod.replace("Maternity", "Matern.");
	prod = prod.replace(" Size", "");
	prod = prod.replace(/\(Add \$\d*\)/g, "");
	return prod;
}

function AbbreviateFontName(font)
{
	font = font.replace(" Script", "");
	return font;
}

function AbbreviateCrystalName(crystal)
{
	crystal = crystal.replace(/^[^\n]*\(/g, "");
	crystal = crystal.replace(/\)/g, "");
	return crystal;
}

function clickToEnlarge(folder,base)
{
	var newwindow;
	newwindow=window.open("products/" + folder + "/" + base + "_xl.jpg", "zoomPicture");
	if (window.focus) {newwindow.focus()}
}

function trimString(str) 
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

