$(document).ready(function(){
    $('#img_upload, #img_remove').css({
        'display': 'none'
    });
    if ($('#image1').val().length > 0) {
        $('#img_upload').attr('src', $('#image1').val());
        $('#img_upload, #img_remove').css({
            'display': 'block'
        });
    }
    
    new AjaxUpload('btn_upload', {
        action: actionUrl,
        data: {
            'function': '1',
            'language': $('#txt_language').val()
        },
        autoSubmit: true,
        onComplete: function(file, response){
            if ($(response).find('responseXML header success').text() == '1') {
            
                $(response).find('responseXML body items').each(function(){
                    $('#img_upload').attr('src', $(this).find('item').text());
                    $('#image1').val($(this).find('item').text());
                });
                $('#img_upload, #img_remove').css({
                    'display': 'block'
                });
            }
            else {
            
                $(response).find('responseXML body items').each(function(){
                    alert($(this).find('item').attr('key'));
                });
            }
        }
    });
    // 900 characters is approx 150 words.
    function limitWordCount(element, limit){
        //$(element).val($(element).val().slice(0, limit));
        checkWordCount(element)
        $(element).bind("keyup", function(){
            checkWordCount(element)
        });
        $('#form1').submit(function(){
            if ($(element).val().length > limit) {
                $(element).addClass('errormsg_field');
                $('p.error').slideDown();
                return false;
            }
        })
		
		if ($('#txterrormessage').length > 0 && $(element).val().length < 1) {
			$(element).addClass('errormsg_field');
		}
        
        /*
        function checkWordCount(element){
            if ($(element).attr('class') != 'errormsg_field' && $(element).val().split(' ').length > limit) {
                $(element).attr('class', 'errormsg_field');
                $('p.error').slideDown();
            }
            else 
                if ($(element).val().split(' ').length < limit) {
                    $(element).removeClass('errormsg_field');
                    $('p.error').slideUp();
                }
        }
        */
        function checkWordCount(element){
            if ($(element).attr('class') != 'errormsg_field' && $(element).val().length > limit) {
                $(element).attr('class', 'errormsg_field');
                $('p.error').slideDown();
            }
            else 
                if ($(element).val().length <= limit) {
                    $(element).removeClass('errormsg_field');
                    $('p.error').slideUp();
                }
        }
    };
    
    limitWordCount('#survey106', 1100)
});

