/* Courtesy of Michael J. Petruniak [mike.petruniak@gmail.com] */	

/* Public variables */
/* Regular expression for a number with x number of digits and one decimal point */
var const_numeric_reg_exp = /^\d*(\.\d{0,})?$/;
	
/* Settings functions */
function setDefaults()
{
	/* Default font, color, alignment, and color type */
	this.changeFont("1smoothy", "Smoothy");
	this.changeColor("Black", "0x000000");
	this.setAlignment("center");
	this.toggleColorType("G");	
	
	with (document.forms[0])
	{
		text1.value = "Enter your own text here";
		text1.focus();
		txtHeight.value = "3";
	}
}

function changeFont(s, title)
{	
	document.getElementById("fontAlias").value = s;
	
	if (document.getElementById("chkItalic").checked == true)
		document.Format.SetVariable("txtFontChange", s.concat("I"));
	else
		document.Format.SetVariable("txtFontChange", s);
	
	
	document.getElementById("theFont").value = title;
	document.getElementById("font_title").innerHTML = title;
}

function changeColor(colorName, colorString)
{
	document.Format.SetVariable("txtColor", colorString);
	document.getElementById("theColor").value = colorName;
	document.getElementById("color_text").innerHTML = colorName;
}

function setAlignment(s)
{
	document.Format.SetVariable("txtAlign", s);
	
	switch (s)
	{
		case "left"	:
			document.getElementById("align_left").style.border = "2px solid #0000FF";
			document.getElementById("align_center").style.border = "none";
			document.getElementById("align_right").style.border = "none";
		break;
		
		case "center":
			document.getElementById("align_left").style.border = "none";
			document.getElementById("align_center").style.border = "2px solid #0000FF";
			document.getElementById("align_right").style.border = "none";
		break;
		
		case "right":
			document.getElementById("align_left").style.border = "none";
			document.getElementById("align_center").style.border = "none";
			document.getElementById("align_right").style.border = "2px solid #0000FF";
	}
	
	document.getElementById("alignment").value = s;
}

function toggleColorType(type)
{
	if (type == "G")
	{
		document.getElementById("matte").style.display = "none";
		document.getElementById("gloss").style.display = "block";
	}
	else
	{
		document.getElementById("matte").style.display = "block";
		document.getElementById("gloss").style.display = "none";
	}

}

/* Called when appropriate to determine banner's price */	
function calculatePrice()
{
	var letterHeight = 0, multiPrice = 0, priceWord = document.getElementById("text1").value.replace(new RegExp("\\n\|\\r", "g"), ""), finalPrice = 0;	
	
	if (document.getElementById("txtHeight").value.length == 0)
		letterHeight = 0;
	else
	{
		if (const_numeric_reg_exp.exec(document.getElementById("txtHeight").value))
			letterHeight = document.getElementById("txtHeight").value;
	}
	
	     if (letterHeight == 0.5)	multiPrice = 0.60;
	else if (letterHeight == 1)	multiPrice = 0.56;
	else if (letterHeight == 1.5)	multiPrice = 0.58;	
	else if (letterHeight == 2)	multiPrice = 0.60;	
	else if (letterHeight == 2.5)	multiPrice = 0.68;	
	else if (letterHeight == 3) 	multiPrice = 0.80;	
	else if (letterHeight == 3.5)	multiPrice = 0.87;	
	else if (letterHeight == 4)	multiPrice = 1.07;	
	else if (letterHeight == 5)	multiPrice = 1.28;	
	else if (letterHeight == 6)	multiPrice = 1.52;	
	else if (letterHeight == 7)	multiPrice = 1.92;	
	else if (letterHeight == 8)	multiPrice = 2.41;
	else if (letterHeight == 9)	multiPrice = 2.76;
	else if (letterHeight == 10)	multiPrice = 3.19;	
	else if (letterHeight == 11)	multiPrice = 3.80;	
	else if (letterHeight == 12)	multiPrice = 4.20;	
	else if (letterHeight == 13) 	multiPrice = 5.20;	
	else if (letterHeight == 14)	multiPrice = 7.25;	
	else if (letterHeight == 15)	multiPrice = 8.00;	
	else if (letterHeight == 16)	multiPrice = 9.00;	
	else if (letterHeight == 17)	multiPrice = 11.00;	
	else if (letterHeight == 18)	multiPrice = 13.00;	
	else if (letterHeight == 19)	multiPrice = 14.00;
	else if (letterHeight == 20) 	multiPrice = 16.00;	
	else if (letterHeight == 21)	multiPrice = 18.00;	
	else if (letterHeight == 22)	multiPrice = 20.00;	
	else if (letterHeight == 23)	multiPrice = 22.00;			
	finalPrice = priceWord.length * multiPrice;
	
	document.getElementById("totalprice").innerHTML = "$" + finalPrice.toFixed(2);
	document.getElementById("amount").value = "$" + finalPrice.toFixed(2);
}

/* user events */
function text1_onKeyUp()
{
	var formatted_text = document.getElementById("text1").value.replace(new RegExp("\\r", "g"), "");
	document.Format.SetVariable("txtInput", formatted_text);
}

function txtWidth_onKeyUp()
{
	if (document.getElementById("txtWidth").value.length !== 0)
	{
		document.Format.SetVariable("txtWidth", ((2 * document.getElementById("txtWidth").value) / 3));
		document.Format.SetVariable("txtRatio", "w");
	}
}

function txtWidth_onFocus()
{
	document.getElementById("theFocus").value = "w";
}

function txtWidth_onBlur()
{
	document.getElementById("theFocus").value = "";
}

function txtHeight_onKeyUp()
{
	if (document.getElementById("txtHeight").value.length !== 0)
	{
		document.Format.SetVariable("txtLetterHeight", document.getElementById("txtHeight").value);
		document.Format.SetVariable("txtRatio", "lh");
		this.calculatePrice();
	}
}

function txtHeight_onFocus()
{
	document.getElementById("theFocus").value = "h";
}

function txtHeight_onBlur()
{	
	document.getElementById("theFocus").value = ""; 
}

function massageHeight()
{
	var height = document.getElementById("txtHeight").value;
			
	     if (height > 0 && height <= 0.5)		height = 0.5;
	else if (height > 0.5 && height <= 1.0)	height = 1.0;
	else if (height > 1.0 && height <= 1.5)	height = 1.5;
	else if (height > 1.5 && height <= 2.0)	height = 2.0;
	else if (height > 2.0 && height <= 2.5)	height = 2.5;
	else if (height > 2.5 && height <= 3.0)	height = 3.0;
	else if (height > 3.0 && height <= 3.5)	height = 3.5;
	else if (height > 3.5 && height <= 4.0)	height = 4.0;
	else if (height > 4.0 && height <= 5.0)	height = 5.0;
	else if (height > 5.0 && height <= 6.0)	height = 6.0;
	else if (height > 6.0 && height <= 7.0)	height = 7.0;
	else if (height > 7.0 && height <= 8.0)	height = 8.0;
	else if (height > 8.0 && height <= 9.0)	height = 9.0;
	else if (height > 9.0 && height <= 10.0)	height = 10.0;
	else if (height > 10.0 && height <= 11.0)	height = 11.0;
	else if (height > 11.0 && height <= 12.0)	height = 12.0;
	else if (height > 12.0 && height <= 13.0)	height = 13.0;
	else if (height > 13.0 && height <= 14.0)	height = 14.0;
	else if (height > 14.0 && height <= 15.0)	height = 15.0;
	else if (height > 15.0 && height <= 16.0)	height = 16.0;
	else if (height > 16.0 && height <= 17.0)	height = 17.0;
	else if (height > 17.0 && height <= 18.0)	height = 18.0;
	else if (height > 18.0 && height <= 19.0)	height = 19.0;
	else if (height > 19.0 && height <= 20.0)	height = 20.0;
	else if (height > 20.0 && height <= 21.0)	height = 21.0;
	else if (height > 21.0 && height <= 22.0)	height = 22.0;
	else if (height > 22.0 && height <= 23.0)	height = 23.0;
	else if (height > 23.0)							height = 23.0;
	
	document.getElementById("txtHeight").value = height;	
	document.getElementById("txtHeight").focus();
	document.Format.SetVariable("txtLetterHeight", height);
	document.Format.SetVariable("txtRatio", "lh");
	this.calculatePrice();
}

function txtTHeight_onKeyUp()
{		
	if (document.getElementById("txtTHeight").value.length !== 0)
	{
		document.Format.SetVariable("txtHeight", document.getElementById("txtTHeight").value);
		document.Format.SetVariable("txtRatio", "h");
	}
}

function txtTHeight_onFocus()
{
	document.getElementById("theFocus").value = "t";
}

function txtTHeight_onBlur()
{
	document.getElementById("theFocus").value = "";
}

function chkItalic_onClick()
{
	if (document.getElementById("chkItalic").checked == true)
	{
		document.Format.SetVariable("txtFontChange", document.getElementById("fontAlias").value.concat("I"));
		document.getElementById("isItalic").value = "Yes";
	}
	else
	{
		document.Format.SetVariable("txtFontChange", document.getElementById("fontAlias").value);
		document.getElementById("isItalic").value = "No";
	}
}

function chkReverse_onClick()
{
	if (document.getElementById("chkReverse").checked == true)
	{
		document.Format.SetVariable("txtReverse", "true");
		document.getElementById("isReverse").value = "Yes";
	}
	else
	{
		document.Format.SetVariable("txtReverse", "false");
		document.getElementById("isReverse").value = "No";
	}
}

function ordernow_onClick()
{	
	this.chkItalic_onClick();
	this.chkReverse_onClick();

	/* Prepare paypal item description- added in missing alignment information and changed the message somewhat */
	document.getElementById("os0").value = document.getElementById("text1").value.replace(new RegExp("\\n", "g"), "<nl>");
	document.getElementById("os1").value = "Width:" + document.getElementById("txtWidth").value + ",";
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Height:" + document.getElementById("txtHeight").value + ",");
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Italic:" + document.getElementById("isItalic").value + ",");
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Reverse:" + document.getElementById("isReverse").value + ",");
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Alignment:" + document.getElementById("alignment").value + ",");
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Font:" + document.getElementById("theFont").value + ",");
	document.getElementById("os1").value = document.getElementById("os1").value.concat("Color:" + document.getElementById("theColor").value);
	document.getElementById("os1").value = document.getElementById("os1").value.concat((document.getElementById("color_type").value == "G") ? " (gloss)" : " (matte)");

	document.forder.submit();
}

/* Flash / JavaScript communication */
function Format_DoFSCommand(command, args)
{			
	if (command == "MyPrice")
	{
		document.Format.SetVariable("txtPercText", 100);
		
		var g = parseInt(args.indexOf("\n"));	
		
		/* One line of text */
		if (g == -1)
		{
			document.Format.SetVariable("txtHowManyLines", "1");
			document.Format.SetVariable("txtLongInput", args);
		}
		/* More than one line of text */
		else
		{
			document.Format.SetVariable("txtHowManyLines", "2");
			document.Format.SetVariable("txtLongInput", args.replace(/\n/g, " "));
		}
	}
	
	if (command == "Timer")
	{
		if (document.getElementById("firstTime").value < 10)
		{
			document.getElementById("firstTime").value++;
			this.setDefaults();
		}
	}	
	
	if (command == "ChangeWidth" && document.getElementById("theFocus").value !== "w")
	{
		if (const_numeric_reg_exp.exec(args))
		{
			var calc = args * 1.5;
			document.getElementById("txtWidth").value = calc.toFixed(3);
			this.calculatePrice();
		}
	}		
	
	if (command == "ChangeLetterHeight")
	{
		if (const_numeric_reg_exp.exec(args))
		{			
			if (parseInt(args) > 23)
			{
				document.getElementById("txtHeight").value = 23;
				document.getElementById("txtHeight").focus();
				document.Format.SetVariable("txtLetterHeight", "23");
				document.Format.SetVariable("txtRatio", "lh");
				this.calculatePrice();
			}
		}
	}	
	
	if (const_numeric_reg_exp.exec(document.getElementById("txtHeight").value))
	{
		if (parseInt(document.getElementById("txtHeight").value > 23))
		{
			document.getElementById("txtHeight").value = 23;
			document.getElementById("txtHeight").focus();
			document.Format.SetVariable("txtLetterHeight", "23");
			document.Format.SetVariable("txtRatio", "lh");
			this.calculatePrice();
		}
	}	

	if (command == "ChangeLetterHeight" && document.getElementById("theFocus").value !== "h")
	{	
		//document.getElementById("txtHeight").value = args;	
		massageHeight();
		this.calculatePrice();
	}		
	
	if (command == "ChangeTotalHeight" && document.getElementById("theFocus").value !== "t")
	{	
		var line_return = new RegExp("\\n.+", "g");
		var success = line_return.exec(document.getElementById("text1").value);
		var num_lines = (success == null ? 1 : document.getElementById("text1").value.match(line_return).length +1);
		document.getElementById("txtTHeight").value = document.getElementById("txtHeight").value * num_lines;
		this.calculatePrice();
	}		

	if (command == "ChangeTotalWidth")
	{
		document.getElementById("overallWidth").value = args;
		this.calculatePrice();
	}	
}	
