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,"%20");
	return encodedHtml;
}
function decodeMyHtml(str) {
	encodedHtml = unescape(str);
	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(/\+/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 = "backend/makeimage.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";
	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/add_tobasket.php?str=" + encodeMyHtml(txt.replace(/ /g,"+"));
	document.location = '#' + encodeMyHtml(txt.replace(/ /g,"+"));
}
// 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 = "backend/makeimage.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/add_tobasket.php?str=" + msg;
	}
}
// Updates postage
function updatePostage() {
	var country = document.getElementsByTagName('select')[0].value;
	var nextday = document.getElementById('nextday').checked.toString();
	document.location = 'basket.php?postage=' + country + '&nextday=' + nextday;
	/*
	var postage = 0;
	switch (country) {
		case "eur":
			document.getElementById('shipping').value = "3.00";
			document.getElementById('shipping2').value = "2.00";
			postage = 3 + (quant-1)*2;
			break;
		case "other":
			document.getElementById('shipping').value = "4.50";
			document.getElementById('shipping2').value = "3.00";
			postage = 4.5 + (quant-1)*3;
			break;
		default:
			document.getElementById('shipping').value = "0";
			document.getElementById('shipping2').value = "0";
			postage = 0;
	}
	document.getElementById('postage').innerHTML = "£" + twoDigits(postage);
	document.getElementById('total').innerHTML = "£" + twoDigits(Number(total) + postage);
	*/
}
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();
}
