//to overcome the timing issues with Bitly, all variables are global. All but the bit.ly URL (bURL) are set in googleQR()
//but then are available to the call back function.

var bURL=''
var encodedTextToQR=''
var gWidth=''
var gHeight=''
var gImg=''
var qrImg=''
var qrSelectableURL=''
var qrText=''
var qr=''
var qrTextDisplay=''
var qrShowHTML=''

//http://www.codehouse.com/javascript/articles/external/
function dhtmlLoadScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

function fbs_click(u,t) {
d="I created this QR Code on the great website of qrmonkey.com."
openURL="http://www.facebook.com/sharer.php?u=http://fb-share-control.com/?d=" + encodeURIComponent(d) + "%26u="+encodeURIComponent(u)+"%26t="+encodeURIComponent(t)+"%26i="+encodeURIComponent(u)
window.open(openURL,' sharer', 'toolbar=0, status=0, width=626, height=436');
return false; } 

BitlyCB.myShortenCallback = function(data) {
	// this is how to get a result of shortening a single url
	var result;
	for (var r in data.results) {
		result = data.results[r];
		result['longUrl'] = r;
		break;
	}
	bURL=result['shortUrl'];
	var qrImg="<img src='" + gImg + "' width='" + gWidth + "' height='" + gHeight + "' style='display: block;border: 1px solid #ccc;'>"
	var qrSelectableURL="<div id='qrInfo'><div id='qrSocial'>"
	qrSelectableURL=qrSelectableURL + "<a class='socialLinkSmall fbs' onclick='return fbs_click(\"" + bURL + "\",\"" + qrTextDisplay + "\")'>FB</a>"
	qrSelectableURL=qrSelectableURL + "<a href='http://twitter.com/?status=I created a QR Code on qrmonkey.com: " + bURL + "' class='socialLinkSmall tws'>TW</a>"
	qrSelectableURL=qrSelectableURL + "<a href='mailto:?subject=I created a QR Code on qrmonkey.com&body=Check out the QR code I created on qrmonkey.com: " + bURL + "' class='socialLinkSmall ems'>EM</a></div>"
	qrSelectableURL=qrSelectableURL + "<input type='text' value='" + bURL + "' class='qrBitlyURL' onfocus='this.select();'>"
	var qrText="<p class='qrText'>" + qrTextDisplay + "</p></div>"
	var qr=qrImg + qrSelectableURL + qrText
	document.getElementById("custom_box").innerHTML= qr
	//dhtmlLoadScript("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
}

function googleQR(textToQR) {
	qrShowHTML=document.getElementById("custom_box").innerHTML
	qrTextDisplay=textToQR
	encodedTextToQR=Url.encode(textToQR)
	gWidth="410"
	gHeight="267"
	gImg="http://chart.apis.google.com/chart?chs=" + gWidth + "x" + gHeight + "&cht=qr&chl=" + encodedTextToQR + "&choe=UTF-8"
	BitlyClient.shorten(gImg, 'BitlyCB.myShortenCallback');
}

function googleQRZazzle(textToQR) {
	qrShowHTML=document.getElementById("custom_box").innerHTML
	qrTextDisplay=textToQR
	encodedTextToQR=Url.encode(textToQR)
	gWidth="540"
	gHeight="540"
	gImg="http://chart.apis.google.com/chart?chs=" + gWidth + "x" + gHeight + "&cht=qr&chl=" + encodedTextToQR + "&choe=UTF-8"
	z="http://www.zazzle.com/api/create/at-238992953928243505?rf=238992953928243505&ax=Linkover&pd=235373133136816554&fwd=ProductPage&ed=false&c=";
	zImg="http://chart.apis.google.com/chart?chs=" + gWidth + "x" + gHeight + "%26cht=qr%26chl=" + encodedTextToQR + "%26choe=UTF-8"
	zLink="<a target='_blank' href=" + z + zImg + ">Get Your T-shirt!</a>"
	document.getElementById("zLinkBox").innerHTML= zLink
	var qrImg="<img src='" + gImg + "' width='" + gWidth + "' height='" + gHeight + "' style='display: block;border: 1px solid #ccc;'>"
	var qr=qrImg + qrText
	document.getElementById("custom_box").innerHTML= qr
	//235484903355307277 shirt
}

//http://www.zazzle.com/api/create/at-238992953928243505?rf=238992953928243505&ax=Linkover&pd=235373133136816554&fwd=ProductPage&ed=false&c=
//http://www.htmlcodetutorial.com/forms/index_famsupp_157.html
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   googleQR(document.qrForm.qrMe.value);
   return false;
   }
else
   return true;
}

//http://www.htmlcodetutorial.com/forms/index_famsupp_157.html
function submitenterZ(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   googleQRZazzle(document.qrForm.qrMe.value);
   return false;
   }
else
   return true;
}

/**
*  http://www.webtoolkit.info/javascript-url-decode-encode.html
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
