function searchNumber(searchval, uri, key, trial_enabled) {
	jQuery.support.cors = true;
	try {
		if(searchval == '') {
			return false;
		} else {
			jQuery('#result').html('<img src="/images/search_loader.gif">&nbsp;Checking availability');
			jQuery('#resultwrapper').show();
		}
		if($.browser.msie && window.XDomainRequest) {
			// Use Microsoft XDR
			var xdr = new XDomainRequest();
			xdr.open("get", uri + "/" + key + "/search/" + searchval + "?format=json");
			xdr.onload = function() {
				json = 'json = ' + xdr.responseText;
				// the string now looks like..  json = { ... };
				eval(json);
				// json is now a regular JSON object
				parse(json, trial_enabled);
				// parse using same function as for jQuery's success event
			}
			xdr.send();
		} else {
			jQuery.ajax(uri + "/" + key + "/search/" + searchval + "?format=json", {
				crossDomain : true,
				dataType : "json",
				async : true,
				success : function(json) {
					parse(json, trial_enabled);

				},
				error : function(xhr, ajaxOptions, thrownError) {
	
				}
			});
		}
	} catch(e) {
		
	}

	jQuery('#starstarsearch #resultwrapper').show();
}

function parse(json, trial_enabled) {
	try {
		var obj = json.response;
		var avail = parseInt(obj.is_available);

		if(trial_enabled == 'true') {
			jQuery('#starstarsearch #contbtn').hide();
			jQuery('#starstarsearch #contbtn').attr("href", '#');
			if(avail == 1) {
				jQuery('#starstarsearch #result').html(obj.search.prefix + obj.search.searched.toUpperCase() + ' (' + obj.search.number + ') is available');
				jQuery('#starstarsearch #trybtn').attr("href", obj.redirect);
				jQuery('#starstarsearch #trybtn').show();

			} else {
				jQuery('#starstarsearch #result').html(obj.search.prefix + obj.search.searched.toUpperCase() + ' (' + obj.search.number + ') is currently in use. <br><span style="font-size:11px;"><a href="/reservation?nbr=' + obj.search.number + '&lit=' + obj.search.searched.toUpperCase() + '&av=0">Contact us</a> for info on availability of this and other numbers.</span>');
				jQuery('#starstarsearch #trybtn').removeAttr('href');
				jQuery('#starstarsearch #trybtn').hide();

			}
		} else {
			jQuery('#starstarsearch #trybtn').hide();
			jQuery('#starstarsearch #trybtn').attr("href", '#');
			if(avail == 1) {
				jQuery('#starstarsearch #result').html(obj.search.prefix + obj.search.searched.toUpperCase() + ' (' + obj.search.number + ') is available');
				jQuery('#starstarsearch #contbtn').attr("href", '/reservation?nbr=' + obj.search.number + '&lit=' + obj.search.searched.toUpperCase() + '&av=1');
				jQuery('#starstarsearch #contbtn').show();

			} else {
				jQuery('#starstarsearch #result').html(obj.search.prefix + obj.search.searched.toUpperCase() + ' (' + obj.search.number + ') is currently in use. <br><span style="font-size:11px;"><a href="/reservation?nbr=' + obj.search.number + '&lit=' + obj.search.searched.toUpperCase() + '&av=0">Contact us</a> for info on availability of this and other numbers.</span>');
				jQuery('#starstarsearch #contbtn').attr("href", '/reservation?nbr=' + obj.search.number + '&lit=' + obj.search.searched.toUpperCase() + '&av=0');
				jQuery('#starstarsearch #contbtn').hide();
			}
		}

	} catch(e) {
		try {
			var err = json.error;
			jQuery('#starstarsearch #result').html(err.msg);
		} catch(e) {
			jQuery('#starstarsearch #result').html('Sorry but an error has occured.  Please try again.');
		}
	}
}

function createSearch(elem) {
	if(elem.attr("class") == "main") {
		elem.append("<div class='wrapper'><form name='starstarsearch' action=''><input type='text' id='searchstr' maxlength='12' value='Enter a word' style='color:#ccc' autocomplete='off'><input type='submit' value='' id='searchbtn'/></div><div id='resultwrapper' style='display:none;'><div id='result' style='float:left;display:inline;'></div><a href='#' id='trybtn'></a><a href='#' id='contbtn'></a><div style='clear:both;'></div></form></div>");
	} else {
		elem.append("<div class='wrapper'><h3>Get your StarStar number today.</h3><input type='text' id='searchstr' maxlength='12' value='Enter a word' style='color:#ccc' autocomplete='off'><button id='searchbtn'></button></div><div id='resultwrapper' style='display:none;'><div id='result' style='float:left;display:inline;'></div><a href='#' id='trybtn'></a><a href='#' id='contbtn'></a><div style='clear:both;'></div></div><p>Search for and secure your own StarStar number from our exclusive registry – this is the only place you can get one.<br/><br/>Currently we're offering a <b>7-day free trial</b> of basic features that supports up to 50 calls during the trial period.<br/><br/>StarStar numbers are available on a first come, first served basis so request yours today before they're gone! </p>");
	}
}


jQuery(document).ready(function() {

	jQuery('#starstarsearch').ready(function() {
		createSearch(jQuery('#starstarsearch'));
		jQuery('#starstarsearch #searchbtn').click(function() {
			searchNumber(jQuery('#starstarsearch #searchstr').val(), jQuery('#starstarsearch #uri').val(), jQuery('#starstarsearch #key').val(), jQuery('#starstarsearch #trial').val());
			return false;
		});
		jQuery('#starstarsearch #searchstr').focus(function() {
			jQuery('#starstarsearch #resultwrapper').hide();
			jQuery('#starstarsearch #searchstr').val('');
			jQuery('#starstarsearch #searchstr').removeAttr("style");
			jQuery('#starstarsearch #result').html('');
			jQuery('#starstarsearch #trybtn').attr("href", '');
			jQuery('#starstarsearch #trybtn').hide();
			jQuery('#starstarsearch #contbtn').attr("href", '');
			jQuery('#starstarsearch #contbtn').hide();
		});
		jQuery('#searchstr').bind('paste', function() {
			return false;
		});
		jQuery('#searchstr').keypress(function(event) {
			return alphanum(event);
		});
	});
});
function alphanum(e) {
	var key;
	var keychar;

	if(window.event) {
		key = window.event.keyCode;
	} else {
		if(e) {
			key = e.which;
		} else {
			return true;
		}
	}
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys
	if((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
		return true;
	} else {
		if((("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) != -1)) {
			return true;
		} else {
			return false;
		}
	}
}
