$(document).ready(function () {
	$(':input').focus(function () {
		if (this.defaultValue == $(this).val())
			$(this).val('');
			
	}).blur(function () {
		if ('' == $(this).val())
			$(this).val(this.defaultValue);
	});

	// d: only once
	$('.hLogin').click(function() {$('#TB_ajaxContent').html(gLd); cd_login();});
});

function printSetup(x, y) {
	tb_remove();
	window.print();
}

function cd_login() {
	var p = 'c=l';
	$.ajax({type:'POST', url:'/login.php', data:p, success:function (t) {$("#TB_ajaxContent").html(t);}});
}

function cd_hlp() {
	p = window.location.pathname.substring(1);
    window.location='/help.php?c=t&x='+((p.length != 0)?((p == 'recipe.php')?'recipe.php&id='+$('#recipeId').val():p):'index.php');
}

function lb_close(num) {
	var url = window.location;
	var sch =url.search;
	url = url.pathname + sch;
	if (num) {
		url += (sch) ? '&l=1' : '?l=1';
		if ($('#r_vi').length > 0)
			url += '&vi=' + $('#r_vi').val();
	} else {
		if ($('#r_vi').length > 0)
			url += (sch ? '&' : '?') + 'vi=' + $('#r_vi').val();;
	}	
	window.location.href = url;
}

function ti_url(url) {
	$.ajax({type:'GET',url:url,dataType:'html', success:function(data, statusType) {$("#TB_ajaxContent").html(data);}});
}

function tiLogin(frmNm) {
	var f = $('#' + frmNm).get(0);
	if (f.u.value == 'Email') f.u.value = '';
	if (f.p.value == 'Password' || f.p.value == 'Email Address or Username') f.p.value = '';
		
	var p = 'c=l&u='+f.u.value+'&p='+f.p.value;

	if (frmNm == 'lb_login_form') {
		$('#lb_login_box').hide();
		$('#lb_login_load').show();
	} else if (frmNm == 'lb_li_form') {
		$('#lb_li_box').hide();
		$('#lb_li_load').show();
	} else {
		if (f.u.value == '') f.u.value = 'Email';
	}

	$.ajax({type:'POST', url:'/login.php', data:p, 
		success:function(t) {
			if (t == 'OK') {
				document.location.href = '/index.php?l=1';
				return ;
			}
			$("#TB_ajaxContent").html(t);
		}
	});
}

function tiReg(frmNm) {
	var f = $('#' + frmNm).get(0);

	if (f.u.value == 'Username' || f.u.value == 'Email') f.u.value = '';
	if (f.e.value == 'Email') f.e.value = '';
	if (f.p.value == 'Password') f.p.value = '';

	var p = 'c=c&u=' + f.u.value + '&p=' + f.p.value + '&e=' + f.e.value;
	
	if (frmNm == 'lb_signup_form') {
		$('#lb_signup_box').hide();
		$('#lb_signup_load').show();
	} else if (frmNm == 'lb_li_form') {
		$('#lb_li_box').hide();
		$('#lb_li_load').show();
		p += '&i=1';		// from initial RHS page
	} else {
		p += '&i=1';		// from initial RHS page
	}

	$.ajax({type:'POST', url:'/login.php', data:p, success:function (t) {$("#TB_ajaxContent").html(t);}});
}

function doSMS(ctrl, id) {
	var f = $('#lb_sms_form').get(0);
	var p = 'c=sms&s=1&num=' + f.n.value + '&id=' + id;
	
	$('#lb_sms_box').hide();
	$('#lb_sms_load').show();

	$.ajax({type:'post', url:ctrl, data:p, 
		success:function (t) {
			if (t == 'OK') {
				$('#lb_sms_load').hide();
				$('#lb_sms_done').show();
			} else {
				$('#lb_sms_err').html("<b>Error:</b> " + t);
				$('#lb_sms_load').hide();
				$('#lb_sms_box').show();
			}
		}
	});
}

function doAmnesia() {
	$('#lb_resend_box').hide();
	$('#lb_resend_load').show();
	var p = 'c=r&g=1&u=' + $('#recoveru').val();
	$.ajax({type:'POST', url:'/login.php', data:p, success:function (t) {$("#TB_ajaxContent").html(t);}});
}

function isint(str) {
	var v = "0123456789";
   	for (i=0; i<str.length; i++) { 
      	if (v.indexOf(str.charAt(i),0) == -1) 
			return false;
	}
   return true;
}

function di2n(i) {
	switch (i) {
		case 0: case '0': return 'Sunday';
		case 1: case '1': return 'Monday';
		case 2: case '2': return 'Tuesday';
		case 3: case '3': return 'Wednesday';
		case 4: case '4': return 'Thursday';
		case 5: case '5': return 'Friday';
		case 6: case '6': return 'Saturday';
	}
	return '';
}
