$(document).ready(function() {

	$('img.warning').hide();
	$('div#feedback').hide();
	
	$('textarea#message,input.basicfield').focus(function(){
		$(this).removeClass('error');
	});

	$('form#commentform').submit(function() {
		var output = '';
		var error;
		if (!$('input#name').val()) {
			output += error_no_name;
			$('img#warning_name').fadeIn('fast');
			$('input#name').addClass('error');
			error = 1;
		}

		if (!$('input#email').val() || !valid_email($('input#email').val())) {
			output += error_no_email;
			$('input#email').addClass('error');
			error = 1;
		}

		if (!$('textarea#message').val()) {
			output += error_no_message;
			$('textarea#message').addClass('error');
			error = 1;
		}
		
		if ($('input#url').val() && !valid_url($('input#url').val())) {
			output += error_invalid_url;
			$('input#url').addClass('error');
			error = 1;
		}
		
		var data = {
			'name':$('input#name').val(),
			'email':$('input#email').val(),
			'url':$('input#url').val(),
			'message':$('textarea#message').val(),
			'parent_id':$('input[name=parent_id]').val()
		};
		if (!error) {
			$.post('/ajax/comment.php', data, function(json) {
				if (!json.success) {
					$('#feedback').hide();
					$('div#feedback').html(json.html).fadeIn('fast');
				}
				else if (json.authorised == 1) $('ul#comments').append(json.html).fadeIn('fast');
				else {
					$('#feedback').hide();
					$('div#feedback').html(json.html).fadeIn('fast');
					$('textarea, input').removeClass('error');
				}
			}, "json");
			$('textarea#message').val('');
		} else $('div#feedback').html(output).fadeIn('fast');
		
		return false;
	});
});


function docomment() {

}

function writeIn(target,source,text) {
	var t = document.getElementById(target);
	if (source) var i = document.getElementById('warning_'+source.name);
	if (source) source.style.border = '1px solid black';
	if (i) i.style.visibility = 'hidden';
	t.style.visibility = 'visible';
	t.style.border = '2px dotted green';
	t.innerHTML = text;
}

function valid_email(email) {
	re = /[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,6}/i;
	if (re.exec(email)) return true;
	else return false;
}

function valid_url(url) {
	re = /https?:\/\/(\w*:\w*@)?[-\w.]+(:\d+)?(\/([\w\/_.]*(\?\S+)?)?)?/i;	
	if (re.exec(url)) return true;
	else return false;
}

function clr(target) {
	var t = document.getElementById(target);
	t.style.visibility = 'hidden';
}

function urldecode( str ) {
    var histogram = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u00DC'] = '%DC';
    histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4';
    histogram['\u00E4'] = '%E4';
    histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6';
    histogram['\u00DF'] = '%DF'; 
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
 
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}