function isNumeric(string){
    if (!string) return false;
    var chars = "0123456789.";

    for (var i = 0; i < string.length; i++) {
       if (chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}


function submitReg(){
	if($('#screen_name').val().length < 6 || $('#screen_name').val().length > 12){
		alert("Your screen name must be between 6 and 12 characters in length.");
		return false;
	}
	else if(!$('#agree').is(':checked')){
		alert('You must agree to the DietTV TERMS OF USE before you can proceed.');
		return false;
	}else{
		document.regForm.submit();
	}
}

function activateRealScreenName(){
	if($('#real_sn_chbx').attr('checked')){
		$('#screen_name').val($('#first_name').val() + ' ' + $('#last_name').val());		
	}else{
		$('#screen_name').val('');
	}
        checkScreenNameAvailability();
}

function updateScreenName(){
	if($('#real_sn_chbx').attr('checked')){
		$('#screen_name').val($('#first_name').val() + ' ' + $('#last_name').val());
		checkScreenNameAvailability();
	}
}

function checkScreenNameAvailability(userFieldId) {
	if(!userFieldId)
		userFieldId = 'screen_name'
	var usr = $("#" + userFieldId).val();
	if(usr == ''){           
		$("#" + userFieldId + "_status").removeAttr('style').html('');
	}else if(usr.length >= 6 && usr.length <= 12){
		$.ajax({
			type: "POST",
			url: "/ajax/ajax.register.php",
			data: "action=validate_username&username="+ usr,
			success: function(msg){
				if(msg == 'OK'){
					$("#" + userFieldId + "_status").removeAttr('style').attr('style','color:#7ea831;').html('... available ');
				}else{
					$("#" + userFieldId + "_status").removeAttr('style').attr('style','color:red;').html('... unavailable');
				}
			}
		});
	}
	else if(usr.length < 6){
		$("#" + userFieldId + "_status").removeAttr('style').attr('style','color:red;').html('... too short');
	}
	else if(usr.length > 12){
		$("#" + userFieldId + "_status").removeAttr('style').attr('style','color:red;').html('... too long');
	}
}

function toggleSystemOfMeasure(){
	if($('#system_m').attr('checked')){
    	$('#wt_label').html('kg');
	    $('#metricHeight').toggle();
        $('#englishHeight').toggle();
       	$('#fip_v').attr("disabled", true);
	  	$('#mheight_select').removeAttr("disabled");
	}else{
 		$('#wt_label').html('lbs.');
	    $('#metricHeight').toggle();
        $('#englishHeight').toggle();
      	$('#fip_v').removeAttr("disabled");
       	$('#mheight_select').attr("disabled", true);
    }
}


function tooltip(){
	/* CONFIG */
		xOffset = 10;
		yOffset = 20;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$("a.tooltip").live('mouseover',function(e){
		this.t = this.title;
		this.title = "";
		$("body").prepend("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")                        
			.fadeIn("fast");
    });
    $("a.tooltip").live('mouseout',function(){
            this.title = this.t;
            $("#tooltip").remove();
    });
    $("a.tooltip").live('mousemove',function(e){
            $("#tooltip")
                    .css("top",(e.pageY - xOffset) + "px")
                    .css("left",(e.pageX + yOffset) + "px");
    });
};



// starting the script on page load
$(document).ready(function(){
	tooltip();
});


var confirmation_dialog;

$(document).ready(function(){

    confirmation_dialog = $('#global-confirmation-div').dialog({
            autoOpen: false,
            modal: true,
            title: 'Confirm Action',
            width: 500
//            buttons: {
//                    "Yes": function(){
//                            this.successFunction();
//                            $(this).dialog("close");
//
//                     },
//                    "No": function() {
//                        $(this).dialog("close");
//                    }
//            }
        });
})

