var fired=true;var suggestions;var message_box=function(){var button='<button type="button" onclick="message_box.close_message();" id="message_box_close" name="exitResult">Close</button>';var message;return{show_message:function(title,body){if($('#message_box').html()===null){message='<div id="message_box">'+title+'<div class="message_box_text">'+body+'</div>'+button+'</div>';$(document.body).append(message);$(document.body).append('<div id="darkbg"></div>');$('#darkbg').show();$('#darkbg').css('height',$('html, body').height());$('#message_box').fadeIn('slow')}else{message='<div id="message_box_title">'+title+'</div><div class="message_box_text">'+body+'</div>'+button;$('#darkbg').show();$('#darkbg').css('height',$('html, body').height());if($('#message_box').css('height')!='150px'){$('#message_box').animate({height:'150px'},function(){$('#message_box_close').focus()})}$('#message_box').fadeIn('slow');$('#message_box').html(message)}},close_message:function(){$('#message_box').fadeOut('fast');$('#darkbg').hide();document.main.gamertag.focus()},show_suggestions:function(){var MAX_SUGGESTIONS=Math.min(5,suggestions.length);var formatted_message='<div class="message_box_text">Click on one of the suggestions below to see if it\'s available or try a different gamertag.</div><div id="message_box_list"><br />\n';for(var i=0;i<MAX_SUGGESTIONS;i++){var display=suggestions[i];while(display.indexOf(' ')!=-1){display=display.replace(' ','&nbsp;')}formatted_message+='<a class="message_box_list" onclick="checkTag(\''+suggestions[i]+'\');">'+display+'</a>\n'}formatted_message+='</div>';$('#message_box_suggestions').hide();$('#message_box').animate({height:'250px'});$('#message_box_close').animate({top:'220px'},function(){$('#message_box_close').focus()});$('#message_box').append(formatted_message)}}}();function checkTag(tag){if(fired){fired=false;message_box.show_message('','<br /><br /><br /><div id="loadImg"></div>');var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest()}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4&&xmlhttp.status==200){fired=true;var title='<div id="message_box_title_unavailable">woops!</div>';var result;if(xmlhttp.responseText=='blank'){result='You must enter a gamertag to check its availability.  Please try again.'}else if(xmlhttp.responseText=='length'){result='Gamertags cannot be longer than 15 characters.  Please try again.'}else if(xmlhttp.responseText=='illegal characters'){result='Gamertags may only contain alpha-numeric characters and spaces.  Please try again.'}else if(xmlhttp.responseText=='profanity'){result='This gamertag contains profanity not allowed by Microsoft.  Please try again.'}else if(xmlhttp.responseText=='first char'){result='The first character of a gamertag must be a letter.  Please try again.'}else if(xmlhttp.responseText=='taken'||xmlhttp.responseText=='xbox'){suggestions=generateSuggestions(tag);title='<div id="message_box_title_unavailable">unavailable</div>';result='The gamertag <strong>'+tag+'</strong> is already taken. <br /> Please try again.';if(suggestions.length>0){result+='<button type="button" onclick="message_box.show_suggestions();" id="message_box_suggestions">Suggestions</button>'}}else if(xmlhttp.responseText=='available'){title='<div id="message_box_title_available">available</div>';result='The gamertag <strong>'+tag+'</strong> is available!'}else{result='There was an error processing your request.  Please try again.'}message_box.show_message(title,result);$('#message_box_close').focus()}};xmlhttp.open("POST","CheckGamertag.php",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("tag="+tag+"&t="+Math.random())}}function generateSuggestions(tag){String.prototype.reverse=function(){return this.split('').reverse().join('')};var i=0;var n;var rnd;var len=tag.length;var suggestions=new Array();var alphaSwap={'e':'3','o':'0','l':'1','t':'7','s':'5','a':'4','g':'9'};var append13=['xO','o0'];var append14=['x','z','i'];if(len<=13){suggestions[i++]=tag+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);for(n in append13){rnd=Math.random();if(rnd>0.5){suggestions[i++]=append13[n]+tag}else{suggestions[i++]=tag+append13[n].reverse()}}for(n in append14){rnd=Math.random();if(rnd>0.5){suggestions[i++]=append14[n]+tag}else{suggestions[i++]=tag+append14[n]}}}else if(len==14){suggestions[i++]=tag+Math.floor(Math.random()*10);for(n in append14){rnd=Math.random();if(rnd>0.5){suggestions[i++]=append14[n]+tag}else{suggestions[i++]=tag+append14[n]}}}if(len<=11){for(n in append13){suggestions[i++]=append13[n]+tag+append13[n].reverse()}for(n in append14){suggestions[i++]=append14[n]+tag+append14[n]}}var first=tag.substring(0,1);tag=tag.substring(1,tag.length).toLowerCase();for(n in alphaSwap){if(tag.indexOf(n)!=-1){suggestions[i++]=first+tag.replace(n,alphaSwap[n])}}tag=first+tag;suggestions.sort(function(){return 0.5-Math.random()});return suggestions}
