/* 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,})?$/;
	
function changeFont(s, title)
{	
	document.getElementById("fontAlias").value = s;
	
	/*
	Suppress italics; may implement in the future
	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 #c31371";
			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 #c31371";
			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 #c31371";
	}
	
	document.getElementById("alignment").value = s;
}

/* 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\|\\s", "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 && letterHeight <= 1)			multiPrice = 0.49;
	else if (letterHeight > 1 && letterHeight <= 2)		multiPrice = 0.74;
	else if (letterHeight > 2 && letterHeight <= 3)		multiPrice = 1.05;	
	else if (letterHeight > 3 && letterHeight <= 4)		multiPrice = 1.25;	
	else if (letterHeight > 4 && letterHeight <= 5)		multiPrice = 1.52;	
	else if (letterHeight > 5 && letterHeight <= 6)		multiPrice = 1.80;	
	else if (letterHeight > 6 && letterHeight <= 7)		multiPrice = 2.35;	
	else if (letterHeight > 7 && letterHeight <= 8)		multiPrice = 2.85;	
	else if (letterHeight > 8 && letterHeight <= 9)		multiPrice = 3.52;	
	else if (letterHeight > 9 && letterHeight <= 10)	multiPrice = 4.15;	
	else if (letterHeight > 10 && letterHeight <= 11)	multiPrice = 4.78;	
	else if (letterHeight > 11 && letterHeight <= 12)	multiPrice = 5.15;	
	else if (letterHeight > 12 && letterHeight <= 13)	multiPrice = 7.55;
	else if (letterHeight > 13 && letterHeight <= 14)	multiPrice = 8.75;
	else if (letterHeight > 14 && letterHeight <= 15)	multiPrice = 9.72;
	else if (letterHeight > 15 && letterHeight <= 16)	multiPrice = 10.22;
	else if (letterHeight > 16 && letterHeight <= 17)	multiPrice = 10.56;
	else if (letterHeight > 17 && letterHeight <= 18)	multiPrice = 11.50;
	else if (letterHeight > 18 && letterHeight <= 19)	multiPrice = 12.78;
	else if (letterHeight > 19 && letterHeight <= 20)	multiPrice = 14.06;
	else if (letterHeight > 20 && letterHeight <= 21)	multiPrice = 15.33;
	else if (letterHeight > 21 && letterHeight <= 22)	multiPrice = 16.61;	
	else if (letterHeight > 22)						 	multiPrice = 17.61;
	
	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", ((1.7 * 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 > 22)
		document.getElementById("txtHeight").value = 22;
		
	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 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 = "";
}

/*
Suppress italics; may implement in the future
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()
{	
	/* Provide paypal item description with non-blank Italic and Reverse properties */
	/*
	Suppress italics; may implement in the future
	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 + ",");
	/*
	Suppress italics; may implement in the future
	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("Comments:" + document.getElementById("comments").value);

	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.8;
			document.getElementById("txtWidth").value = calc.toFixed(3);
			this.calculatePrice();
		}
	}		
	
	if (command == "ChangeLetterHeight")
	{
		if (const_numeric_reg_exp.exec(args))
		{
			if (parseInt(args) > 22)
			{
				document.getElementById("txtHeight").value = 22;
				document.getElementById("txtHeight").focus();
				document.Format.SetVariable("txtLetterHeight", "22");
				document.Format.SetVariable("txtRatio", "lh");
				this.calculatePrice();
			}
		}
	}	
	
	if (const_numeric_reg_exp.exec(document.getElementById("txtHeight").value))
	{
		if (parseInt(document.getElementById("txtHeight").value > 22))
		{
			document.getElementById("txtHeight").value = 22;
			document.getElementById("txtHeight").focus();
			document.Format.SetVariable("txtLetterHeight", "22");
			document.Format.SetVariable("txtRatio", "lh");
			this.calculatePrice();
		}
	}	

	if (command == "ChangeLetterHeight" && document.getElementById("theFocus").value !== "h")
	{
		document.getElementById("txtHeight").value = args;
		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 * 1.4;
		this.calculatePrice();
	}		

	if (command == "ChangeTotalWidth")
	{
		document.getElementById("overallWidth").value = args;
		this.calculatePrice();
	}	
}	

