var base = '/living/';

function checkfield(type, form, field) {
	$.ajax({
		url: base + 'check.php?type='+type+'&name=' + field.value,
		type: "GET",
		success: function(rc) {
			if (rc == 'OK') {
				$('#' + type + 'checkitvalue').html('<span style="color:black">"' + field.value + '": ' + rc + '</span>');
			} else {
				$('#' + type + 'checkitvalue').html('<span style="color:red">"' + field.value + '": ' + rc + '</span>');
			}
		}
	});
}

function sendRecommend() {
	var emails = $('#sendemail').val();
	if (!emails) {
		alert('Please enter valid email address');
		return;
	}
	var id = $('#sendlinkid').val();
	
	$.ajax({
		url: base + "recommend.php",
		data: "linkid=" + id + "&emailaddress=" + emails,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
			}
			$('#emailto-'+id).html(rc);
		} 
	});
}

function showRecommend(id) {
	$('#emailto-' + id).toggle().html("Loading...");
	if ($('#emailto-' + id).css("display") == 'none') {
		return;
	}
	$.ajax({
		url: base + "recommend.php",
		data: "open=1&linkid=" + id,
		type: "POST",
		success: function(rc) {
			$('#emailto-' + id).html(rc);
		}
	});	
}

function enablebutton (button, button2, target) {
	var string = target.value;
	button2.disabled = false;
	if (string.length > 0) {
		button.disabled = false;
	} else {
		button.disabled = true;
	}
}

function toggleMenu(selection) {
	for (var i=0; i<menuItems.length; i++) {
		$('#' + menuItems[i]).hide();
  		$('#m_' + menuItems[i]).removeClass('on');
	}
	$('#' + selection).show();
	$('#m_' + selection).addClass('on');
}

function showLive(type) {
	$('#live-status').html('reloading ' + type + ' page');
	$.ajax({
		url: base + "live.php",
		data: "dataonly=1&Type=" + type,
		type: "POST",
		success: function(rc) {
			$('#live-' + type).html(rc);
			$('#live-status').html('The page will refresh every 5 seconds');
		}
	});		
}

function closeAllSub(level) {
	var matchThis = new RegExp('Sub_Level' + level);
	$('div#CategorySelection').find("div").each ( 
		function() { 
			if (this.id.toString().match(matchThis))
				$('#'+this.id).hide();
		}
	);
		
	// close all the subs
	$('div#CategorySelection').find("div")
	.filter("#Sub_Level" + level + "\\S*")
	.each(
		function() {
			alert( 'here' );
			this.hide();
		}
	);
}

function submitComment(dataForm, statusDiv) {
	var params = {};
	if (dataForm != '') { 
		$('#'+dataForm)
		.find("input[checked], input[type='text'], input[type='hidden'], input[type='submit'], option[selected], textarea").each(
			function() {
				params[ this.name || this.parentNode.name ] = this.value;
			}
		);
	}
	$('#'+statusDiv).html('Submitting...');
	$.ajax({
		url: base+'story.php?AddComment=1',
		data: $.param(params),
		type: "POST",
		success: function(rc) {
			jsonResp = eval('(' + rc + ')');
			if (jsonResp.code == 0) {
				$('#'+statusDiv).html('<span class="error">'+jsonResp.message+'</span>');
			} else {
				document.location = jsonResp.redirect;
			}
		}
	});
}