$(document).ready(function(){
	updateCount($('#textarea1').val(), 'contentLenght1');
	updateCount($('#textarea2').val(), 'contentLenght2');
	$('form img[alt=captcha],form a#captchaRefresh').click(function(){
		$.getJSON("/async_captcha.php",
			{'action': 'refresh'},
			function(data){
				$('form input#captcha_id').val(data.id);
				var img = $('form img[alt="captcha"]').attr('src', data.url);
				if (!img.is('img')) {
					var iePng = $('form span[title="captcha"]');
					if (iePng.is('span')) {
						iePng.attr('style', iePng.attr('style').replace(/progid:DXImageTransform\.Microsoft\.AlphaImageLoader\(src='.+',/i, "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + data.url +"',"));
					}
				}
			}
		)
		return false;
	})
})

var default_tips1 = 'Please do not use HTML tags (max: 150 characters)';
var default_tips2 = 'Please fill in if you have any suggestions (max: 2,000 characters)';
function clear_tips(obj)
{	
  if (obj ==1) {
  	if (document.getElementById("textarea" + obj).value == default_tips1) {
  		document.getElementById("textarea" + obj).value = '';
  	}
  }
  else {
  	if (document.getElementById("textarea" + obj).value == default_tips2) {
  		document.getElementById("textarea" + obj).value = '';
  	}
  }
}

function updateCount(text, elementId)
{
	var obj = document.getElementById(elementId);
    obj.innerHTML = "Character count: " + text.length ;
}

function link_submit()
{
    var category_name = $.trim($("#category_name").val());
    var site_name  = $.trim($("#site_name").val());
    var site_url  = $.trim($("#site_url").val());
    var links = $.trim($("#links").val());
    var textarea1 = $.trim($("#textarea1").val());
    var textarea2 = $.trim($("#textarea2").val());
    var authCode = $.trim($("#authCode").val());

    //clean default text
    if (textarea1 == default_tips1) {
    	$("#textarea1").val(textarea1 = '');
    }
    if (textarea2 == default_tips2) {
    	$("#textarea2").val(textarea2 = '');
    }

    if(category_name=='') {
        alert('Please select a category.');
    } else if(site_name=='') {
        alert("Please fill in site name.");
    } else if (site_url == '' || site_url =='http://') {
        alert("Please fill in site URL.");
    } else if(links=='' || links == 'http://') {
        alert("Please fill in reciprocal links.");
    } else if (textarea1 == '') {
        alert('Please fill in description');
    } else if(textarea1.length > 150) {
    	alert("You've inputted too much text in description! Please refine your text to meet the maximum number of characters allowed.(max: 150)");
    } else if (textarea2.length > 2000) {
        alert("You've inputted too much text in suggestion! Please refine your text to meet the maximum number of characters allowed.(max: 2000)");
    } else if(authCode=='') {
        alert("Please fill in security code.");
    } else {
        return true;
    }
    return false;
}
