$(document).ready(function() { 
	WebHosting = {
	
		results: {
			available: false,
			domain: "",
			response: ""
		},
		
		request: function(){
			this.preload(true);
			$.ajax({
				type: "POST",
				url: DOC_URL + "ajax_whois_lookup.php",
				data: {
					domain_name: $("#domain_name").val(),
					domain_ext: $('#domain_ext').val()
				},
				cache: false,
				dataType: "text",
				success: function(res){
					WebHosting.preload(false);
					
					var xml = new XMLParser(res);
					
					if(xml.getTag("message") == "available"){
						WebHosting.results = {
							available: true,
							domain: xml.getTag("domain"),
							response: xml.getTag("response_message")
						}
						WebHosting.show_result("available");
					} else {
						WebHosting.results = {
							available: true,
							domain: xml.getTag("domain"),
							response: xml.getTag("response_message")
						}
						WebHosting.show_result("not_available");
					}
				}
			});
		},
		
		preload: function(show){
			if(show == true){
				$("#domain_name").css("background-image", "url(img/ajax-loader_small_orange.gif)");
			} else {
				$("#domain_name").css("background-image", "none");
			}
		},
		
		show_result: function(action){
			switch (action){
				case "available":
					var text = $("#domain_result_message .available").html();
					$("#domain_result_message .available").html(text.replace(/{domain}/, "<span>" + WebHosting.results.domain + "</span>"));
					$("#domain_result_message .available span").text(WebHosting.results.domain);
					$("#domain_result_message .not_available").fadeOut("fast", function(){
						$("#domain_result_message .available").fadeIn("slow");
					});
					 
					$("#hosting_reg_action").each(function(){
						$("#hosting_reg_action option").removeAttr("selected"); 
					});
					$("#hosting_reg_action option[value='1']").attr('selected', 'selected');
					//$("#hosting_reg_action").attr("disabled", "disabled");
					break;
				
				case "not_available":
					var text = $("#domain_result_message .not_available").html();
					$("#domain_result_message .not_available").html(text.replace(/{domain}/, "<span>" + WebHosting.results.domain + "</span>"));
					$("#domain_result_message .not_available span").text(WebHosting.results.domain);
					$("#domain_result_message .available").fadeOut("fast", function(){
						$("#domain_result_message .not_available").fadeIn("slow");
					});
					
					$("#hosting_reg_action").each(function(){
						$("#hosting_reg_action option").removeAttr("selected"); 
					});
					$("#hosting_reg_action option[value='2']").attr('selected', 'selected');
					//$("#hosting_reg_action").attr("disabled", "disabled");
					break;
					
				default:
					$("#domain_result_message .available").slideUp("slow");
					$("#domain_result_message .not_available").slideUp("slow");
					
					//$("#hosting_reg_action").removeAttr("disabled"); 
					$("#hosting_reg_action").each(function(){
            			$("#hosting_reg_action option").removeAttr("selected"); 
					});
					break;						
			}
		},
		
		show_info: function(){
			Shadowbox.open({
				player:     'html',
				title: WebHosting.results.domain,
				content:    '<div class="shadowbox_message" align="left"><pre>' + WebHosting.results.response + '</pre></div>',
				options:	{
					modal: false,
					displayNav: true,
					enableKeys: true
				},
				width:      650,
				height:     600
			});
		}
	}			
 
	$("#domain_name").typeWatch({
		callback: function(){ WebHosting.request() },
		wait: 750,
		highlight: true,
		captureLength: 2
	});
	
	$("#domain_ext").change(function(){
		WebHosting.show_result(false);
		if($("#domain_name").val().length > 2){
			WebHosting.request();
		}
	});
	
	$("#domain_name").keypress(function(){  
		WebHosting.show_result(false);
		WebHosting.preload(false);
	});
	
	$('#domain_name').keypress(function (event){ return (event.keyCode == 13)?false:true; });
	$('#domain_ext').keypress(function (event){ return (event.keyCode == 13)?false:true; });
});