function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}



var previewAtts = new Array()
previewAtts['font'] = 'ttf/Fun-Kids/Kids.ttf'
previewAtts['color'] = 'black'
previewAtts['wording'] = 'See your Custom Phrase'
previewAtts['size'] = 'small'
previewAtts['hex'] = 'FFFFFF'
previewAtts['justify'] = 'Centre'
function promptForWording(text) {
updatePreview(null,null,null,null,null,null)
}

function getCharacterCount() {
	var text = document.getElementById('wording').value.trim()		
	var ws = 0
	var i
	for(i=0; i<text.length; i++) {
		var c = text.charAt(i)
		if(c.isWhiteSpace())
			ws++
	}
	return i-ws
}

var previewWait = null

function selectFont(font) {
	updatePreview(font, null, null, null, null, null)
}

function selectColor(color) {
	updatePreview(null, color, null, null ,null, null)
}

function selectSize(size) {
	updatePreview(null, null, size, null ,null, null)
}

function selectBG(hex) {
	updatePreview(null, null, null, hex ,null, null)
}

function selectJustify(justify) {
	updatePreview(null, null, null, null, justify, null)
}

function enterLetter() {
	previewWait = clearTimeout(previewWait)
	previewWait = setTimeout('updatePreview(null, null, null ,null, null, \'' + document.getElementById('wording').value.escape() + '\')',700)
}

function updatePreview(font, color, size, hex, justify, wording) {
	if(font != null) {
		previewAtts['font'] = font
	}
	if(color != null) {
		previewAtts['color'] = color
	}
	if(wording != null) {
		previewAtts['wording'] = wording.trim()
	}
	if(previewAtts['wording'] == '') {
		return
	}
	if(size != null) {
		previewAtts['size'] = size
	}
	if(hex != null) {
		previewAtts['hex'] = hex
	}
	if(justify != null) {
		previewAtts['justify'] = justify
	}

//	var url = 'preview.php?f=' + previewAtts['font'] + '&c=' + previewAtts['color'] + '&w=' + previewAtts['wording']
	var url = 'text2PNGalign.php?text_input=' + previewAtts['wording'] + '&font_input=' + previewAtts['font'] + '&size_input=' + previewAtts['size'] + '&colour_input=' + previewAtts['color'] + '&hex=' + previewAtts['hex'] + '&justify_input=' + previewAtts['justify']

	
	document.getElementById('preview').src = url
	document.getElementById('preview').onload = updatePrices
}

function updatePrices() {

	var width = document.getElementById('preview').scrollWidth
	var height = document.getElementById('preview').scrollHeight
	var ratio = parseFloat(width, 10) / parseFloat(height, 10)
	
	document.getElementById('n1').innerHTML = Math.round(1*ratio)
//	document.getElementById('n1_5').innerHTML = Math.round(1.5*ratio)
	document.getElementById('n2').innerHTML = Math.round(2*ratio)
//	document.getElementById('n2_5').innerHTML = Math.round(2.5*ratio)
	document.getElementById('n3').innerHTML = Math.round(3*ratio)
	document.getElementById('n4').innerHTML = Math.round(4*ratio)
	document.getElementById('n5').innerHTML = Math.round(5*ratio)
	document.getElementById('n6').innerHTML = Math.round(6*ratio)
	document.getElementById('n7').innerHTML = Math.round(7*ratio)
	document.getElementById('n8').innerHTML = Math.round(8*ratio)
	document.getElementById('n9').innerHTML = Math.round(9*ratio)
	document.getElementById('n10').innerHTML = Math.round(10*ratio)
	document.getElementById('n11').innerHTML = Math.round(11*ratio)
	document.getElementById('n12').innerHTML = Math.round(12*ratio)
	
	updatePrice()
}

function updatePrice() {

	var inputs = document.getElementsByTagName('input')
	var count = getCharacterCount()
	var price = 0
	for(var i=0; i<inputs.length; i++) {
		if(inputs[i].type == 'radio' && inputs[i].name == 'product' && inputs[i].checked) {
			price = inputs[i].value.split('|')[0]
			break
		}
	}
	document.getElementById('letterCount').innerHTML = count
	var quantity = document.getElementById('quantity').value
	if (quantity == '') {
		return
	}
	var subTotal = price*count + ''
	var total = subTotal*quantity + ''
	
	var stDollars = subTotal.substring(0, subTotal.length-2)
	if(stDollars.length == 0) {
		stDollars = '0'
	}
	var stCents = subTotal.substring(subTotal.length-2)
	if(stCents.length == 0) {
		stCents = '00'
	} else if(stCents.length == 1) {
		stCents = '0'
	}
	
	var tDollars = total.substring(0, total.length-2)
	if(tDollars.length == 0) {
		tDollars = '0'
	}
	var tCents = total.substring(total.length-2)
	if(tCents.length == 0) {
		tCents = '00'
	} else if(tCents.length == 1) {
		tCents = '0'
	}
	
	document.getElementById('subTotal').innerHTML = stDollars + '.' + stCents
	document.getElementById('totalPrice').innerHTML = tDollars + '.' + tCents
}


function updateQuantity(elem, e) {
	var key
	var keychar
	var good = false
	if (window.event)
		key = window.event.keyCode
	else if (e)
		key = e.which
	else
		good = true
	keychar = String.fromCharCode(key)
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		good = true
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		good = true
	return good
}
