var optionsSubmit = {success:   showResponseSubmit, dataType:  'json'}; 

$(document).ready(function() {
    $("#launch-form").fancybox(
    {

        onComplete	:	function() {
            $("#request-form").validate({   
                submitHandler: function(form) {jQuery(form).ajaxSubmit(optionsSubmit)},
                errorElement: 'span',
                invalidHandler: function(form) {$.fancybox.resize()},
            	rules: {
            		clientnumber:{required:  function(element) {return $('input[name=alreadycustomer]:checked').val() == 'yes';}},
                	subject:{required:true,minlength:5,maxlength:50}
            	},
            	messages:{
            		subject:{minlength:""}
            	}
            }); 
            
            $('input[name=alreadycustomer]').click(function(){
                if($('input[name=alreadycustomer]:checked').val() == 'yes'){
                    $("#client-number").show();
                }else{
                    $("#client-number").hide();
                }
                $.fancybox.resize();
            });
            
            $("#request-form > ul > li > input,textarea,select").click(function(){
            	$("#request-form > ul > li").removeClass('highlight');
            	$(this).parent().addClass('highlight');
            });     
            
         	$('input[name=subject]').keyup(function(){
         		limitChars('subject_field', 50, 'subject_field_info');
         	})            

		}
    }
    
    );
    


    $("#parent").live('change',function () {
    	
    	var id = $(this).val();
    	
		$.ajax({
			   dataType: "json",
			   type: "POST",
			   url: "index.php/customer-request/ajax",
			   data: "action=category&id="+id,
			   success: function(data){
			     if(data.status=="OK"){
			    	 $("#child").html("");
			    	 $.each(data.categories, function(key, value) { 
			    		 $("#child").append($("<option></option>").attr("value",key).text(value));	 
			    	 });
			     }
				}
			});		    	
    	
    });
    
});

function showResponseSubmit(data)  { 
	if(data.status=="OK"){
		$("#captcha_error").hide();
	    $(".intro").hide();
	    $("#form-block").hide();
	    $("#thank-you-method").show();	
	    $.fancybox.resize();	
	}else if(data.status=="CAPTCHA"){
		$("#captcha_error").show();
	}
		

}

function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('Must be between 5 and 50 characters. Currently Used: '+limit+' characters');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('Must be between 5 and 50 characters. Characters remaining: '+ (limit - textlength) +' characters.');
		return true;
	}
}



