// LIMIT CHARS
function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		cntfield.value = maxlimit - field.value.length;
	}
}

function onPressEnter(evt) {
	var keyCode = null;
	if (evt.which) {
		keyCode = evt.which;
	} else if(evt.keyCode) {
		keyCode = evt.keyCode;
	}
	if (13 == keyCode) {
		checkID();
		return false;
	}
	return true;
}

// LOADER ON
function loaderOn() {
	$('#output').hide()
	$('#ads_avatar').hide();
	$('#statusbox').fadeIn();
	$('#avatar').attr('src', 'imgs/pix.gif');
	$('#statusbox').animate({height:"35px"},400);
	$('#loader').show();
}

// LOADER OFF
function loaderOff() {
	$('#loader').hide();
}

// SET DEFAULT INPUT TEXT
function defaultInputText(value) {
	if (value == "show") {
		if ($("#yid").val() == "") {
			$("#yid").val("Enter the Yahoo ID you want to check");
		}
	}
	if (value == "clear") {
		if ($("#yid").val() == "Enter the Yahoo ID you want to check") {
			$("#yid").val("");
		}
	}
}

// CHECK ID
function checkID() {
	if ($('#yid').val() != 'Enter the Yahoo ID you want to check' && $('#yid').val() != '') {
		loaderOn();
		$.ajax({
		    url: 'ajax.php?function=checkid',
		    type: 'POST',
		    cache: false,
		    dataType: 'text',
		    data: 'yid=' + $.URLEncode($('#yid').val()),
		    error: function(){
		        alert('Network error. Try refreshing the page.');
		    },
		    success: function(response){
		    	loaderOff();
		    	if (response != 'error') {
					var content = ''+$('#yid').val()+'<span>is</span>'+response;
					var avatar = 'http://img.msg.yahoo.com/avatar.php?format=jpg&yids='+$('#yid').val();
					$('#output').hide().html(content).fadeIn("slow");
					$('#avatar').attr('src', avatar);
					$('#statusbox').animate({height:"155px"},400);
					$('#ads_avatar').fadeIn("slow");
		    	} else {
		    		var content = '<span class="username" id="username">Server</span> is <span class="status_user">busy</span>.';
		    		$('#output').hide().html(content).fadeIn("slow");
		    		$('#statusbox').animate({height:"35px"},400);
		    	}
				
		    }
		});
	}
}
