// JavaScript Document
var boatname = "";
var ttfFontName = "fonts/arial.ttf";
var colorOfSurface = "ffffff";
var FXColor = "000000";
var strokeColor = "000000";
var fontColor = "0000ff";
var fontColorName = "BRIGHT BLUE";
var FXColorName = "BLACK";
var strokeColorName = "BLACK";
var symbolFlag=0;
var symbolType="";
var lightDirectionX=1;
var lightDirectionY=-1;
var lightDirectionDisplayed="Top Right";
var outlineSet = 0;
var dropShadowSet = 0;
var gapShadowSet = 0;
var D3DShadowSet = 0;
var stencilFlag = 0;
var italicFlag = 0;

if (readCookie("TEXT")!="") {
	boatname = readCookie("TEXT");
	ttfFontName = readCookie("FONT");
	colorOfSurface = readCookie("BG");
	FXColor = readCookie("EC");
	strokeColor = readCookie("OC");
	fontColor = readCookie("FG");
	outlineSet = readCookie("OUTLINE");
	D3DShadowSet = readCookie("BSHADOW");
	dropShadowSet = readCookie("DSHADOW");	
	gapShadowSet = readCookie("GSHADOW");		
	fontColorName = readCookie("TEXT_COLOR");
	FXColorName = readCookie("EFFECT_COLOR");
	strokeColorName = readCookie("OUTLINE_COLOR");	
	lightDirectionDisplayed = readCookie("LIGHT_DIRECTION");
	
}

var letters=0;
var boatHullColorName="WHITE";

if (stencilFlag==1) {
	fontColorName = "STENCIL";
	fontColor = "FFFFFF";
	colorOfSurface = "000099";
	FXColor = "000000";	
	FXColorName = "NONE";
	strokeColor = "000000";	
	strokeColorName = "NONE";
} else {
	fontColorName = "BRIGHT BLUE";
	fontColor = "0000ff";
	colorOfSurface = "FFFFFF";
	FXColor = "000000";
	FXColorName = "BLACK";	
	strokeColor = "000000";	
	strokeColorName = "BLACK";
}

/*
Single Dimensioned Array for calculating prices and effects

A simple example of a single-dimensioned array from http://www.devarticles.com/c/a/JavaScript/JavaScript-Arrays/
*/

unitCostPricing=new Array(0.25,0.30,0.50,0.60,0.75,1.00,1.50,2.00,2.50,3.00,3.50,4.00,4.50);
FXUnitCostPricing=new Array(0.50,0.60,1.00,1.20,1.50,2.00,3.00,4.00,5.00,6.00,7.00,8.00,9.0);
fontSizeMM=new Array(25,30,50,60,75,100,150,200,250,300,350,400,450);

/*
function urlencode() developed from
http://issues.apache.org/jira/browse/TAPESTRY-977

*/
function urlencode(textString) {
return escape(textString).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function changeFontFileName(form, flag) {
ttfFontName = flag;
symbolFlag=0;
symbolType="";

refreshLetteringName(form);
return false;
}

function changeSymbolName(form, flag, type) {
ttfFontName = flag;
symbolFlag=1;
symbolType=type;

refreshLetteringName(form);
return false;
}

function replaceStrokeSet(form, flag) {
outlineSet = flag;
refreshLetteringName(form);
}

function replace3D3ShadowSet(form, flag) {
D3DShadowSet = flag;
if (dropShadowSet==1 && D3DShadowSet==1) dropShadowSet=0;
if (dropShadowSet==1 && gapShadowSet==1) gapShadowSet=0;
refreshLetteringName(form);
}

function replaceDropShadowSet(form, flag) {
dropShadowSet = flag;
if (dropShadowSet==1 && D3DShadowSet==1) D3DShadowSet=0;
if (dropShadowSet==1 && gapShadowSet==1) gapShadowSet=0;
refreshLetteringName(form);
}

function replaceGapShadowSet(form, flag) {
gapShadowSet = flag;
if (gapShadowSet==1 && D3DShadowSet==1) D3DShadowSet=0;
if (gapShadowSet==1 && dropShadowSet==1) dropShadowSet=0;
refreshLetteringName(form);
}


function changeToStenciSet(form, flag) {
stencilFlag = flag;
if (stencilFlag==1) {
	fontColorName = "STENCIL";
	fontColor = "FFFFFF";
	colorOfSurface = "000099";
	FXColor = "000000";	
	FXColorName = "NONE";
} else {
	fontColorName = "BRIGHT BLUE";
	colorOfSurface = "FFFFFF";
	FXColor = "000000";
	fontColor = "0000ff";
	FXColorName = "BLACK";	
}
refreshLetteringName(form);
}

function changeItalicFlag(form, flag) {
italicFlag = flag;
refreshLetteringName(form);
}

function resetLightDirection(form, cell, lightx, lighty) {
lightDirectionDisplayed=cell;
lightDirectionX=lightx;
lightDirectionY=lighty;
refreshLetteringName(form);
}

function resetShadowColor(form, color,colname) {
FXColor = color;
FXColorName = colname;

refreshLetteringName(form);
}


function changeStrokeColor(form, color,colname) {
strokeColor = color;
strokeColorName = colname;

refreshLetteringName(form);
}

function changeTextColor(form, color, colname) {
if (stencilFlag==0) {
	fontColor = color;
	fontColorName = colname;
}

refreshLetteringName(form);
}

function changeboatHullColor(form, color, colname) {
if (stencilFlag==0) {	
	colorOfSurface= color; 
	boatHullColorName=colname;
}
refreshLetteringName(form);
}

function refreshLetteringName(form) {
	
document.getElementById('ShadowExmple_fx').innerHTML=("<span class='pricing_box'>Select Shadow Colour</span> (if required) : "+FXColorName.toUpperCase());	
document.getElementById('StrokeExmple_fx').innerHTML=("<span class='pricing_box'>Select Outline Colour</span> (if required) : "+strokeColorName.toUpperCase());	
document.getElementById('FontColorExmple_fx').innerHTML=("<span class='pricing_box'>Selected Font Colour : </span>"+fontColorName.toUpperCase());
document.getElementById('BackGroundColorExmple_fx').innerHTML=("<span class='pricing_box'>Select Background Colour</span> <span class='small'>(for illustration purposes only)</span> : "+ boatHullColorName.toUpperCase());

if (document.getElementById('BsStroke').disabled==true) { outlineSet=0; }
if (document.getElementById('Bs3DShadow').disabled==true)  { D3DShadowSet=0;  }
if (document.getElementById('BsDropShadow').disabled==true)  { dropShadowSet=0;  }
if (document.getElementById('BsGapShadow').disabled==true)  { gapShadowSet=0;  }

if (outlineSet==1) {
	document.getElementById('BsStroke').checked=true;
} else {
	document.getElementById('BsStroke').checked=false;
}	

if (D3DShadowSet==1) {
	document.getElementById('Bs3DShadow').checked=true;
	document.getElementById('BsDropShadow').checked=false;	
	document.getElementById('BsGapShadow').checked=false;		
	gapShadowSet=0;
	dropShadowSet=0;
} else {
	document.getElementById('Bs3DShadow').checked=false;
}	

if (dropShadowSet==1) {
	document.getElementById('BsDropShadow').checked=true;
	D3DShadowSet=0;
	gapShadowSet=0;
	document.getElementById('Bs3DShadow').checked=false;	
	document.getElementById('BsGapShadow').checked=false;			
} else {
	document.getElementById('BsDropShadow').checked=false;
}	

if (gapShadowSet==1) {
	document.getElementById('BsGapShadow').checked=true;
	D3DShadowSet=0;
	dropShadowSet=0;
	outlineSet=0;
	document.getElementById('Bs3DShadow').checked=false;	
	document.getElementById('BsDropShadow').checked=false;
	document.getElementById('BsStroke').checked=false;
} else {
	document.getElementById('BsGapShadow').checked=false;
}	
if (ttfFontName == null)
ttfFontName = "fonts/arial.ttf";

if (symbolFlag && ttfFontName!="spacer.gif") {
	document.getElementById('impressionOfDesign').src="images/symbols/"+ttfFontName;
	document.getElementById('impressionOfDesign').align="center";
	document.getElementById('currentfont').innerHTML=("<strong>Select Graphic using the buttons below: "+ttfFontName+"</strong>");
} else {	
	
	document.getElementById('currentfont').innerHTML=("<strong>Select Font Style using the buttons above: "+ (ttfFontName.replace("fonts/", "")).replace(".TTF", "").replace(".ttf", "").replace("_", "")+"</strong>");
	document.getElementById('FontColorExmple_fx').innerHTML=("<span class='pricing_box'>Selected Font Colour : </span>"+fontColorName.toUpperCase());
	var boatNameVariable = urlencode(form.textFieldLetteringName.value);
	var url = 'name2.php?fontsize=100&';
	var params = 'ttfFontName=' + urlencode(ttfFontName) + '&colorOfSurface=' + urlencode(colorOfSurface) + '&string=' + boatNameVariable + '&fontColor=' + urlencode(fontColor) + '&shadowColor=' + urlencode(FXColor) + '&strokeColor=' + urlencode(strokeColor) + '&outlineSet=' + urlencode(outlineSet)  + '&D3DShadowSet=' + urlencode(D3DShadowSet)  + '&gapShadowSet=' + urlencode(gapShadowSet) + '&dropShadowSet=' + urlencode(dropShadowSet) + '&lightx=' + urlencode(lightDirectionX) + '&lighty=' + urlencode(lightDirectionY);
	
	document.getElementById('impressionOfDesign').src = url + params;
}
calc_price(form);

}

/*
calc_price() function
Ref: https://www.supernovagroup.net/registration/reg02 where base code developed from
*/

function calc_price(form) {
if (symbolFlag==1) {
	document.getElementById('textFieldLetteringName').value=symbolType;
}

var letters=document.getElementById('textFieldLetteringName').value.length;


var postage=3.99;
var quantity1=document.getElementById('tfQuantity1').value;
var size1=document.getElementById('tfSize1').value;
var orderdetails;


if (D3DShadowSet==1 || dropShadowSet==1 || outlineSet==1 || symbolFlag==1 || gapShadowSet==1) {
	if ((outlineSet==1) && (D3DShadowSet==1 || dropShadowSet==1 || gapShadowSet==1) && (strokeColorName.toUpperCase()!=FXColorName.toUpperCase())) {
		document.getElementById('ConfirmOrder').tfPrice1.value = round_decimals(((letters*(unitCostPricing[size1]+FXUnitCostPricing[size1]))*quantity1), 2);	
	} else {
		document.getElementById('ConfirmOrder').tfPrice1.value = round_decimals(((letters*FXUnitCostPricing[size1])*quantity1), 2);
	}
} else {
	document.getElementById('ConfirmOrder').tfPrice1.value = round_decimals(((letters*unitCostPricing[size1])*quantity1), 2);
}

if(document.getElementById('ConfirmOrder').tfPrice1.value < 10)
		{
		document.getElementById('ConfirmOrder').tfTotalPrice.value = 9.89;
		}
else
{
	document.getElementById('ConfirmOrder').tfTotalPrice.value = ConfirmOrder.tfPrice1.value;
}
writeCookie("TEXT", form.textFieldLetteringName.value, 1);
writeCookie("FONT", ttfFontName, 1);
writeCookie("FG", fontColor, 1);
writeCookie("EC", FXColor, 1);
writeCookie("TEXT_COLOR", fontColorName, 1);
writeCookie("EFFECT_COLOR", FXColorName, 1);
writeCookie("OUTLINE_COLOR", strokeColorName, 1);
writeCookie("OC", strokeColor, 1);
writeCookie("BSHADOW", D3DShadowSet, 1);
writeCookie("DSHADOW", dropShadowSet, 1);
writeCookie("GSHADOW", gapShadowSet, 1);
writeCookie("STENCIL", stencilFlag, 1);
writeCookie("OUTLINE", outlineSet, 1);
writeCookie("SIZE", fontSizeMM[document.getElementById('ConfirmOrder').tfSize1.value], 1);
writeCookie("QUANTITY", document.getElementById('ConfirmOrder').tfQuantity1.value, 1);
writeCookie("BG",colorOfSurface, 1);
writeCookie("LIGHT_DIRECTION",lightDirectionDisplayed, 1);
writeCookie("PRICE", document.getElementById('ConfirmOrder').tfTotalPrice.value, 1);
writeCookie("COMMENTS", document.getElementById('ConfirmOrder').OrderComments.value, 1);
writeCookie("LDY", lightDirectionY, 1);
writeCookie("LDX", lightDirectionX, 1);
writeCookie("EMAIL", document.getElementById('ConfirmOrder').Email2.value, 1);
}


/* 
function writeCookie() from
http://www.roscripts.com/snippets/show/47
*/

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}


/* 

function readCookie() from
http://www.roscripts.com/snippets/show/46 */

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}


/* 
Bof Round Decimal
This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.
function round_decimals(original_number, decimals)
and
function pad_with_zeros(rounded_value, decimal_places)
below
*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
   if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

/* Eof Round Decimal */
/*
function setupfields() {
}
*/
