var defaultShortUrlMsg="Paste short URL here",defaultFullUrlMsg="Expanded URL will appear here",defaultNameMsg="Your Name",defaultEmailMsg="Your Email",isExpanding=false,isRequestingApiKey=false,lastUrl="",shortUrlRegExp=/http\:\/\/[a-z0-9\.]+\/[a-z0-9]+/i;$(document).ready(function(){var a=$("<img />").attr("src","/img/ajax_load.gif"),b=$("<img />").attr("src","/img/arrow_right_green.png");$("#shorturl").val(defaultShortUrlMsg);$("#fullurl").val(defaultFullUrlMsg);$("#api_email").val(defaultEmailMsg);$("#api_name").val(defaultNameMsg);$("#shorturl").bind("keydown",function(c){if(c.keyCode==10||c.keyCode==13){if(validShortUrl($(this).val())){urlExpand($(this).val())}else{$(this).addClass("error")}}else{if($(this).val()==defaultShortUrlMsg){$("#shorturl").val("");$("#fullurl").val("");$("#gotherelink").html("")}}});$("#shorturl").bind("focus",function(){if($(this).val()==defaultShortUrlMsg||$(this).val()==lastUrl){$("#shorturl").val("");$("#fullurl").val("");$("#gotherelink").html("")}return true});$("#shorturl").bind("blur",function(){var c=$.trim($(this).val());if(c==""){$("#shorturl").val(defaultShortUrlMsg);$("#fullurl").val(defaultFullUrlMsg)}else{if(c==defaultShortUrlMsg){return true}else{if(c!=lastUrl&&validShortUrl(c)){urlExpand(c)}else{$(this).addClass("error")}}}});setInterval(function(){var c=$("#shorturl").val();if(validShortUrl(c)&&c!=lastUrl){lastUrl=c;urlExpand(c)}},1000);$("#api_btn").bind("click",requestApiKey);$("#api_name").bind("blur",function(){var c=$.trim($(this).val());if(c==""){$("#api_name").val(defaultNameMsg)}else{if(c==defaultNameMsg){return true}}});$("#api_email").bind("blur",function(){var c=$.trim($(this).val());if(c==""){$("#api_email").val(defaultEmailMsg)}else{if(c==defaultEmailMsg){return true}}});$("#api_name").bind("focus",function(){if($(this).val()==defaultNameMsg){$("#api_name").val("")}return true});$("#api_email").bind("focus",function(){if($(this).val()==defaultEmailMsg){$("#api_email").val("")}return true});showTopUrls()});function showTopUrls(){$.ajax({data:{topurls:1,k:"149e17eef02e8ec877c9aa5420d1becc"},url:"/json",dataType:"json",success:function(b){if(b.ERR){$("#topurls").html("<i>Could not retrieve the list</i>")}else{var a="<table><th>#</th><th>Short URL</th><th>Expanded</th>";$.each(b,function(d,c){a+="<tr><td>"+(d+1)+'</td><td><a title="Click to urlexpand it" href="javascript:expandTopUrl(\''+c.short_url+"');\">"+c.short_url+"</a></td><td>"+c.hit_count+" times</td></tr>"});a+="</table>";$("#topurls").html(a)}},error:function(){$("#topurls").html("<i>Could not retrieve the list</i>")}})}function expandTopUrl(a){$("#fullurl").val("");$("#gotherelink").html("");$("#shorturl").val(a)}function validShortUrl(a){return shortUrlRegExp.test($.trim(a))}function urlExpand(a){if(isExpanding){return}isExpanding=true;showLoader();$.ajax({data:{u:a,k:"149e17eef02e8ec877c9aa5420d1becc"},url:"/json",dataType:"json",success:function(b){$("#shorturl").removeClass("error");if(b.e){lastUrl=b.u;$("#fullurl").val(b.e);$("#gotherelink").html('<a target="_blank" href="'+b.e+'">go there &raquo;</a>');showVisualLink();isExpanding=false}else{showDisabledLink();isExpanding=false;Boxy.alert("Could not expand the URL",null,{title:"Data Format Error"})}},error:function(){showDisabledLink();isExpanding=false;Boxy.alert("Could not expand the URL",null,{title:"Transport Error"})}})}function showDisabledLink(){$("#expandbtn").attr("src","/img/arrow_right_gray.png")}function showVisualLink(){$("#expandbtn").attr("src","/img/arrow_right_green.png")}function showLoader(){$("#expandbtn").attr("src","/img/ajax_load.gif")}function requestApiKey(){if(isRequestingApiKey){return}var d=false,c=$.trim($("#api_name").val()),a=$.trim($("#api_email").val()),b=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;$("#apikeyform input").removeClass("error");if(c==""||c==defaultNameMsg){$("#api_name").addClass("error");d=true}if(a==""||a==defaultEmailMsg){$("#api_email").addClass("error");d=true}else{if(!b.test(a)){$("#api_email").addClass("error");d=true}}if(!d){isRequestingApiKey=true;$.ajax({data:{get_key:1,email:a,name:c},url:"/json",dataType:"json",success:function(e){isRequestingApiKey=false;if(e.ERR){Boxy.alert(e.ERR,null,{title:"Error"})}else{if(e.OK){Boxy.alert("API key was sent to "+e.email,null,{title:"Mail Sent"});$("#api_email").val(defaultEmailMsg);$("#api_name").val(defaultNameMsg)}else{Boxy.alert("An error occured. Please try again later.",null,{title:"Data Error"})}}},error:function(){isRequestingApiKey=false;Boxy.alert("An error occured. Please try again later.",null,{title:"Transport Error"})}})}}function showSampleCode(){popup=Boxy.load("/page/sample_code",{title:"Sample Code"})};