//dk. to aid user interface and prevent accidental duplicate http:// at beginning of pasted urls
function inputFill(id, v) {
  $(id).css({ color: "#ccc" }).attr({ value: v }).focus(function() {
    if($(this).val()==v) {
      $(this).val("").css({ color: "#333" });
    }
  }).blur(function() {
      if($(this).val()==""){
        $(this).css({ color: "#b2adad" }).val(v);
      }
  });
}
//dk. process long url
function processURL(longurl) {
    	$("#msgbox").removeClass().addClass('msgbox').html('<img src="images/loading.gif" alt="Processing..." />&nbsp;Processing...').fadeIn(1000);
		$("#shorturl").fadeOut(1000);

        $.getJSON("add/",{ long_url:longurl,lang:lang,add:Math.random(),output:'json' } ,function(json) {
              renderShortInfo(json);
        });

}
//dk. create short url detail display
function renderShortInfo(data) {
	if (data && data.status == 'OK') {
                 //dk. show success msg and short url info
                 $("#su_title").html(data.data.title);
                 $("#short_url").val(data.data.url);
	   				var url = '<a href="' + data.data.url + '" alt="' + data.data.title + '" title="' + data.data.title + '">' + data.data.url + '</a>';
                 $("#su_link").html(url);
                 $("#shorturl").fadeIn(1000);

                 //dk. empty longurl field with default
                 inputFill($("#longurl"), "http://");

				//dk. show success msg
                 $("#msgbox").addClass('success_icon').text('Short URL created!').fadeIn(1000);

				//dk. update zeroclip
				updateZC();

    } else if (data && data.status == 'ERROR') {
               //dk. show error msg
               renderError(data.data.msg);

    } else {
               //dk. show error msg when no json data object exists
               renderError('Something is really wrong.');
    }
	return false;

}

//dk. display error message
function renderError(msg) {
    $("#msgbox").addClass('error_icon').text(msg).fadeIn(1000);
}

function checkHash() {
	//dk. check if there is a base64 encoded json string in url
  	hash = self.document.location.hash.substring(1);
  	if (hash != '') {
		  basejson = decode64(hash);

		  if (basejson) {
		  	jsonobj = (basejson.substr(0,15) == 'renderShortInfo' || basejson.substr(0,11) == 'renderError') ? eval(basejson) : '';
		  } else {
            renderError('Invalid hash.');
		  }
  	}
}
function decode64(input) {
	var keyStr = "ABCDEFGHIJKLMNOP" +
	              "QRSTUVWXYZabcdef" +
	              "ghijklmnopqrstuv" +
	              "wxyz0123456789+/" +
	              "=";
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	if (base64test.exec(input)) {
	    return false;
	//alert("There were invalid base64 characters in the input text.\n" + "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + "Expect errors in decoding.");
	}
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do {
	    enc1 = keyStr.indexOf(input.charAt(i++));
	    enc2 = keyStr.indexOf(input.charAt(i++));
	    enc3 = keyStr.indexOf(input.charAt(i++));
	    enc4 = keyStr.indexOf(input.charAt(i++));

	    chr1 = (enc1 << 2) | (enc2 >> 4);
	    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	    chr3 = ((enc3 & 3) << 6) | enc4;

	    output = output + String.fromCharCode(chr1);

	    if (enc3 != 64) {
	       output = output + String.fromCharCode(chr2);
	    }
	    if (enc4 != 64) {
	       output = output + String.fromCharCode(chr3);
	    }

	    chr1 = chr2 = chr3 = "";
	    enc1 = enc2 = enc3 = enc4 = "";

	 } while (i < input.length);

	 return unescape(output);
}

