function encodeMyHtml(str) {
	//from: http://www.yuki-onna.co.uk/html/encode.html
	encodedHtml = escape(str);
	encodedHtml = encodedHtml.replace(/\//g,"%2F");
	encodedHtml = encodedHtml.replace(/\?/g,"%3F");
	encodedHtml = encodedHtml.replace(/=/g,"%3D");
	encodedHtml = encodedHtml.replace(/&/g,"%26");
	encodedHtml = encodedHtml.replace(/@/g,"%40");
	encodedHtml = encodedHtml.replace(/\+/g,"%2B");
	//encodedHtml = encodedHtml.replace(/\+/g,"%20");
	return encodedHtml;
}

function decodeMyHtml(str) {
	encodedHtml = unescape(str);
	encodedHtml = encodedHtml.replace(/\+/g," ");
	encodedHtml = encodedHtml.replace(/%2F/g,"\\");
	encodedHtml = encodedHtml.replace(/%3F/g,"?");
	encodedHtml = encodedHtml.replace(/%3D/g,"=");
	encodedHtml = encodedHtml.replace(/%26/g,"&");
	encodedHtml = encodedHtml.replace(/%40/g,"@");
	encodedHtml = encodedHtml.replace(/%2B/g,"+");
	return encodedHtml;
}
var timeint = 500;
var timeout;
function reloadimg() {
	clearTimeout(timeout);
	timeout = setTimeout('newimg()', timeint);
}

// Updates the image
function newimg() {
	var div = document.getElementById('pic');
	removeAllChildren(div);
	var txt = document.getElementById('txt').value;
	var img = document.createElement('img');
	img.src = "modules/image_generator.php?str=" + encodeMyHtml(txt);
	img.alt = "I Heart "  + encodeMyHtml(txt) + " Mug";
	div.appendChild(img);
	//document.getElementById('input').getElementsByTagName('a')[0].href = "#" + encodeMyHtml(txt.replace(/ /g,"+"));
	if( (T = document.getElementById('google_notif') ) ) 
		document.getElementById('google_notif').style.display = "none";
	if (document.getElementById('instructions') !== null) {
		document.getElementById('instructions').style.display = "block";
	}
	
	document.getElementById('linkto').value = 'http://www.iheartmugs.com/i-heart/' + encodeMyHtml(txt).replace(/ /g,"+");
	document.getElementById('purchase').href = "actions/basket.php?str=" + encodeMyHtml(txt).replace(/ /g,"+") + "&add_to_basket=1";
	if (!document.location.toString().match(/basket$/g)) {
		document.location = '#' + encodeMyHtml(txt).replace(/ /g, "+");
	}
	
	if (_gaq != undefined) {
		_gaq.push(['_trackPageview', '/reload-image'], ['_trackPageview', '/reload-image?str=' + txt]);
	}
}

// Removes all children from an element
function removeAllChildren(elem) {
	if (elem && elem.hasChildNodes()) {
		while (elem.childNodes.length >= 1) {
			elem.removeChild(elem.firstChild);
		}
	}
}

// Updates picture to reflect any anchor links
function checkURL() {
	var url = String(document.location);
	if (url.indexOf('#') > 0) {
		msg = url.substr(url.indexOf('#')+1);
		document.getElementById('input').getElementsByTagName('input')[0].value = decodeMyHtml(msg);
		document.getElementById('pic').getElementsByTagName('img')[0].src = "modules/image_generator.php?str=" + msg;
		document.getElementById('linkto').value = 'http://www.iheartmugs.com/i-heart/' + encodeMyHtml(msg).replace(/ /g,"+");
		//document.getElementById('input').getElementsByTagName('a')[0].href = "#" + msg;
		document.getElementById('purchase').href = "actions/basket.php?str=" + msg + "&add_to_basket=1";
	}
}

// Updates postage
function updatePostage() {
	var nextday = document.getElementById('nextday').checked.toString();
	document.location = 'actions/basket.php?postage=GB&nextday=' + nextday;
}

function twoDigits(p) {
	p = p.toString();
	var index = p.indexOf(".");
	if (p.substr(index) == ".5") {
		return p + "0";
	} else if (index < 0) {
		return p + ".00";
	} else {
		return p;
	}	
}

function SelectAll(id){
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

checkURL();
if (document.getElementById('txt')!=null && !document.location.toString().match(/basket$/g)) {
	document.getElementById('txt').focus();
}

