var FontSizeTable;
var LTR_PRINTWIDTH;
var LTR_PRINTHEIGHT;
var LTR_SEGMENTPRICE;
var LTR_SEGMENT1PRICE;
var LTR_SEGMENT2PRICE;
var LTR_SEGMENT3PRICE;
var RootPath;
var PartNumber;

function SetFontSizeTable(SizeTable) {FontSizeTable = SizeTable;}
function SetLetterPrintWidth(Width) {LTR_PRINTWIDTH = Width;}
function SetLetterPrintHeight(Height) {LTR_PRINTHEIGHT = Height;}
function SetLetterSegmentPrice(Price) {LTR_SEGMENTPRICE = Price;}
function SetLetterSegment1Price(Price) {LTR_SEGMENT1PRICE = Price;}
function SetLetterSegment2Price(Price) {LTR_SEGMENT2PRICE = Price;}
function SetLetterSegment3Price(Price) {LTR_SEGMENT3PRICE = Price;}
function SetRootPath(Path) {RootPath = Path;}
function SetPartNumber(Part) {PartNumber = Part;}

function Trim(s)
{
var maxchars = 255; // Change number to maximum characters.
if (s.value.length > maxchars)
s.value = s.value.substring(0,maxchars);
}

var UpdateTimer;

function UpdatePriceDelay()
{
setTimeout('UpdatePrice()', 50); //need to delay trim after paste
}

function UpdatePrice()
{
// First trim to max 255 characters
Trim(document.getElementById('textarea'));
if (UpdateTimer){
clearTimeout(UpdateTimer)};
UpdateTimer = setTimeout('RefreshPrice()', 500); // delay updating price 1/2 second
}

//ANY CHANGES TO THE REFRESHPRICE, GETLETTERWIDTH OR GETLETTERHEIGHT CALCULATIONS MUST ALSO BE MADE TO THE LETTERPRICECALC PAGE
function RefreshPrice()
{
var a=0;
var width = 0;
var linewidth = 0;
var totalwidth = 0;
var height = 0;
var lineheight = 0;
var totalheight = 0;
var maxheight = 0;
var maxtop = 0;
var minbottom = 0;
var charcode = 0;
var searchstring = "";
var sectioncount = 0;
var price = 0;
var fontsize = 1;
var IsBold = document.getElementById('Bold').checked;
var IsItalic = document.getElementById('Italic').checked;

var lettertext = document.getElementById('textarea').value;

fontsize = parseFloat(document.getElementById('fontsize').value);
if (fontsize < 1){
document.getElementById('fontsize').value = 1
fontsize = 1;
};
if (fontsize > 15){
document.getElementById('fontsize').value = 15
fontsize = 15;
};

document.getElementById('price').innerHTML = "Calculating Price";
for (a=0; a < lettertext.length; a++)
{
charcode = lettertext.charCodeAt(a);

if (charcode >= 32 && charcode <= 127)
{
if (charcode != 32)
{
if (IsBold)
{
if (IsItalic)
{
width =  GetLetterWidth(charcode, "True", "True");
height = GetLetterHeight(charcode, "True", "True");
}
else
{
width = GetLetterWidth(charcode, "True", "False");
height = GetLetterHeight(charcode, "True", "False");
}
}
else
{
if (IsItalic)
{
width = GetLetterWidth(charcode, "False", "True");
height = GetLetterHeight(charcode, "False", "True");
}
else
{
width = GetLetterWidth(charcode, "False", "False");
height = GetLetterHeight(charcode, "False", "False");
}
}

if (linewidth + width * fontsize + 0.1 > LTR_PRINTWIDTH)
{
totalheight = totalheight + lineheight;
lineheight = 0 //height * fontsize + 0.1; //lineheight = height of first character
linewidth = 0 //width * fontsize + 0.1; //last letter was discarded to make first letter on next line
}

linewidth = linewidth + width * fontsize + 0.1;
lineheight = Math.max(lineheight, height * fontsize + 0.1);

//account for descenders for g (103), p (112), q (113), y (121) and j (106)
if (charcode == 103 || charcode == 112 || charcode == 113 || charcode == 121 || charcode == 106)
{
if (charcode == 106)
{
maxtop = Math.max(fontsize, maxtop); //align top of 'j' with top of font
minbottom = Math.min(fontsize * (1 - height), minbottom);//calculate bottom of descender
}
else
{
maxtop = Math.max(height * fontsize * 0.75, maxtop);
minbottom = Math.min(-height * fontsize * 0.25, minbottom);
}
}
else
{
if (height > 1)
{//center larger fonts on line
maxtop = Math.max(fontsize + fontsize * (height - 1) / 2, maxtop);
minbottom = Math.min( -fontsize * (height - 1) / 2, minbottom);
}
else
{
maxtop = Math.max(height * fontsize, maxtop);
//minbottom does not change
}
}

totalwidth = totalwidth + width * fontsize + fontsize * 0.11; //add 11% for space between letters
}
else
{
totalwidth = totalwidth + fontsize * 0.4; //add 40% for space
}
}
else
{
if (charcode != 13 && charcode != 10){
alert("There is an invalid character in the text: " + charcode)};
}
}//for loop
maxheight = maxtop - minbottom;
if (totalwidth > fontsize * 0.11){
totalwidth = totalwidth - fontsize * 0.11;} //remove last 11% added at the end

//make sure we add in the last few characters
totalheight = totalheight + lineheight;

//caclulate number of elements and then the price
if (totalheight > 0)
{
sectioncount = parseInt((totalheight - 0.01) / LTR_PRINTHEIGHT) + 1; //subtract 0.01 so that sizes are exactly on don't cause a whole extra section

price = LTR_SEGMENT1PRICE;
if (sectioncount > 1){
price = price + LTR_SEGMENT2PRICE};
if (sectioncount > 2){
price = price + LTR_SEGMENT3PRICE};
if (sectioncount > 3){
price = price + LTR_SEGMENTPRICE * (sectioncount - 3)};
}
document.getElementById('price').innerHTML = "Price $" + price + ".00"; //Letter stickers are always even dollars
document.getElementById('textspace').innerHTML = "Estimated Text Space: Width = " + parseInt(totalwidth * 100)/100 + " inches, Height = " + parseInt(maxheight*100)/100 + " inches"
//document.getElementById('textspace').innerHTML = document.getElementById('textspace').innerHTML + " MaxTop = " + maxtop + ", MinBottom = " + minbottom
}

function GetLetterWidth(Index, Bold, Italic)
{
var SearchString;
var StartPos;
var EndPos;
var i;

if (Bold=="True")
{
if (Italic=="True")
{
SearchString = Index + "BI_w";
}
else
{
SearchString = Index + "B_w";
}
}
else
{
if (Italic=="True")
{
SearchString = Index + "I_w";
}
else
{
SearchString = Index + "_w";
}
}

StartPos = FontSizeTable.search(SearchString);
if (StartPos != 0)
{
for (i=StartPos; i < FontSizeTable.length; i++)
{
if (FontSizeTable.substring(i, i + 1) == "=")
{
StartPos = i + 1;
for (i=StartPos; i < FontSizeTable.length; i++)
{
if (FontSizeTable.substring(i, i + 1)== " ")
{

return parseFloat(FontSizeTable.substring(StartPos, i));
}
}
}
}
}
}

function GetLetterHeight(Index, Bold, Italic)
{
var SearchString;
var StartPos;
var EndPos;
var i;

if (Bold=="True")
{
if (Italic=="True")
{
SearchString = Index + "BI_h";
}
else
{
SearchString = Index + "B_h";
}
}
else
{
if (Italic=="True")
{
SearchString = Index + "I_h";
}
else
{
SearchString = Index + "_h";
}
}

StartPos = FontSizeTable.search(SearchString);
if (StartPos != 0)
{
for (i=StartPos; i < FontSizeTable.length; i++)
{
if (FontSizeTable.substring(i, i + 1) == "=")
{
StartPos = i + 1;
for (i=StartPos; i < FontSizeTable.length; i++)
{
if (FontSizeTable.substring(i, i + 1)== " ")
{

return parseFloat(FontSizeTable.substring(StartPos, i));

}
}
}
}
}


}

function UpdateFontImage(form)
{
if (form.Bold.checked)
{
if (form.Italic.checked)
{
document.getElementById('fontimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "BIfull.gif";
document.getElementById('zoomimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "BIzoom.gif";
//document.getElementById('zoomimage2').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "BIzoom.gif";
}
else
{
document.getElementById('fontimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Bfull.gif";
document.getElementById('zoomimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Bzoom.gif";
//document.getElementById('zoomimage2').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Bzoom.gif";
}
}
else
{
if (form.Italic.checked)
{
document.getElementById('fontimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Ifull.gif";
document.getElementById('zoomimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Izoom.gif";
//document.getElementById('zoomimage2').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "Izoom.gif";
}
else
{
document.getElementById('fontimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "full.gif";
document.getElementById('zoomimage').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "zoom.gif";
//document.getElementById('zoomimage2').src = RootPath + "Images/Products/Letters/" + PartNumber + "/" + PartNumber + "zoom.gif";
}
}

}
