$.fn.clearForm = function() {
	// iterate each matching form
	return this.each(function() {
		// iterate the elements within the form
		$(':input', this).each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	});
};
var options, a;
jQuery(function(){
     
	$('#loginform').clearForm();
    
    $('.tata').each(function(){
        var $input = $(this);
        var maxcount = $input.attr('maxlength')*1;
        var $counter = $('#'+$(this).attr('name')+'_ajaxerror');
        
        var update = function(){
            var now = maxcount - $input.val().length;
            if($counter.text() == ""){
                $counter.text("Characters remaining: " + now);
            }
            var before = $counter.text().substr(22,3)*1;
            
            if(now < 0){
                var str = $input.val();
                $input.val(str.substr(0,maxcount));
                $now = 0;
            }
            
            if(before != now){
                $counter.text("Characters remaining: " + now);
            }
        }
        $input.focusin(function(){
            $input.css({'background-position': '0  -90px'});
            $counter.removeClass('error').addClass('countpad');
            $(this).bind('input keyup paste', function(){
				$count = $(this).val().length;
				if($count > 1){
					$(this).css({'background-position': '0  0'});
				}
				setTimeout(update,0);
			});
            update();
        }).focusout(function() {
            $input.css({'background-position': '0  0'});
            $counter.val('');
            $counter.addClass('error').removeClass('countpad');
        });
    });
    
    $('input:text').focusin(function(){
        $(this).css({'background-position': '0  -30px'});
        $(this).bind('input keyup paste', function(){
            $count = $(this).val().length;
            if($count > 1){
                $(this).css({'background-position': '0  0'});
            }
        });
    }).focusout(function(){
        $(this).css({'background-position': '0  0'});
    });

	$('#ajaxform .ajaxval').focusin(function(){
		$(this).val(jQuery.trim($(this).val()));
	}).focusout(function(){
        var item = $(this);
        item.classes = item.attr('class');
        item.id = item.attr('id');
        $('#'+item.id+'_ajaxerror').text('').siblings('p.error').text('');
        var validate = function(){
            item.classes = replace_chars(item.classes.replace("ajaxval ", "")," ",":");
            item.label  = replace_chars(item.id,"_"," ");
            var p = {};
            p['message'] = item.value + "~" + item.label + "~" + item.classes;
            $('#'+item.id+'_ajaxerror').load("/index.php/validation_check/response/help",p);
        }
        if(item.classes.indexOf("ajaxval") != -1){
            item.value = jQuery.trim(item.val());
            if(item.classes.indexOf("required") != -1){
                validate();
            }else{
                if(item.value.length > 0){
                    validate(); 
                }
            }
        }else{
            console.log("no ajax");
        }
	});
	
	$('#ajaxform').submit(function(){
		$('#ajaxform .ajaxval').each(function(){
			var item = $(this);
			var item_name = item.attr('name');
			$('#'+item_name+'_ajaxerror').text('');
		});
	});

    $('#city').autocomplete({
        minLength: 3,
        source: function(req, add){
            $.ajax({
                url: '/index.php/validation_check/check_city/help',
                dataType: 'json',
                type: 'POST',
                data: req,
                success: function(data){
                    if(data.response =='true'){
                       add(data.message);
                    }
                }
            });
        },
        select: function(event, ui){
            $('#postcode').val(ui.item.postcode);
            $('#state').val(ui.item.state);
            $('#postcode_ajaxerror').text('').siblings('p.error').text('');
            $('#state_ajaxerror').text('').siblings('p.error').text('');
            $('#comments').focus();
        }
    });
    
    var availableTags = [
			"NSW",
			"QLD",
			"VIC",
			"ACT",
			"TAS",
			"WA",
			"SA",
			"NT"
		];
		$( "#state" ).autocomplete({
			source: availableTags
		});
    
    $(":input:visible:first").focus();

});
function replace_chars(value,replacethis,withthis){
	while(value.indexOf(replacethis) != -1){
		value = value.replace(replacethis,withthis);
	}
	return value;
}
function isIE7(){
   return (/MSIE (7)/.test(navigator.userAgent) && navigator.platform == "Win32");
}



