/* 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, and alignment */
	this.changeFont("twopeasblessings", "2 Peas Blessings");
	this.changeColor("Black", "0x000000");
	this.setAlignment("center");
	
	with (document.forms[0])
	{
		//text1.value = "Enter Your Custom Phrase Here\nUse As Many Lines As You Need (up to 100 characters)";
		text1.value = "";
		text1.focus();
		txtHeight.value = "3";
	}
	
	this.text1_onKeyUp();
}

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;
}

/* Called when appropriate to determine banner's price */
function calculatePrice()
{
	var letterHeight, price_per_letter, 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;
	}
	
	
	switch (parseInt(letterHeight))
	{
		case 1:
			price_per_letter = 0.25;
		break;
		case 2:
			price_per_letter = 0.50;
		break;
		case 3:
			price_per_letter = 1.00;
		break;
		case 4:
			price_per_letter = 2.00;
		break;
		case 5:
			price_per_letter = 3.00;
		break;
		case 6:
			price_per_letter = 3.50;
		break;
		case 7:
			price_per_letter = 4.00;
		break;
		case 8:
			price_per_letter = 4.50;
		break;
		case 9:
			price_per_letter = 5.00;
		break;
		case 10:
			price_per_letter = 5.50;
		break;
		case 11:
			price_per_letter = 6.00;
		break;
		case 12:
			price_per_letter = 7.00;
		break;
		case 13:
			price_per_letter = 8.00;
		break;
		case 14:
			price_per_letter = 9.00;
		break;
		case 15:
			price_per_letter = 10.00;
		break;
		case 16:
			price_per_letter = 11.00;
		break;
		case 17:
			price_per_letter = 12.00;
		break;
		case 18:
			price_per_letter = 14.00;
		break;
		case 19:
			price_per_letter = 16.00;
		break;
		case 20:
			price_per_letter = 18.00;
		break;
		case 21:
			price_per_letter = 19.00;
		break;
		case 22:
			price_per_letter = 20.00;
		break;
		default:
			price_per_letter = 0;
	}
	
	finalPrice = priceWord.length * price_per_letter;
	
	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 > 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 = "";
	// New rounding feature (9/27/10)
	/*
	textHeight = document.getElementById("txtHeight").value;
	ceilingHeight = Math.ceil(textHeight);
	decimalPart = 1 - (ceilingHeight - textHeight);
	wholePart = parseInt(textHeight);

	if (decimalPart >= 0 && decimalPart < 0.25) {
		document.getElementById("txtHeight").value = wholePart;
	}
	else if (decimalPart >= 0.25 && decimalPart < 0.75) {
		document.getElementById("txtHeight").value = wholePart + 0.50;
	}
	else {
		document.getElementById("txtHeight").value = wholePart + 1;
	}
	*/
	// End rounding feature
}

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()
{	
	/* Provide paypal item description with non-blank Italic and Reverse properties */
	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.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) > 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;
		this.calculatePrice();
	}		

	if (command == "ChangeTotalWidth")
	{
		document.getElementById("overallWidth").value = args;
		this.calculatePrice();
	}	
}	

