var ref_count = 0;
var cur_emp_sz = 0;
var prev_emp_sz = 0;
function InitCheckBoxExplain(){	
	$('.explain SPAN.holderWrap').click(function(){
    	if ($(this).parent().parent().hasClass('show-expl') && $('.crime-explain-row').css('display') == 'none') {
			$('.crime-explain-row').slideDown();
		}
		if ($(this).parent().parent().hasClass('hide-expl') && $('.crime-explain-row').css('display') == 'block') {
			$('.crime-explain-row').slideUp();
		}
		
	});
    $('.explain label.prettyCheckbox').click(function(){
    	if ($(this).parent().hasClass('show-expl') && $('.crime-explain-row').css('display') == 'none') {
			$('.crime-explain-row').slideDown();
		}
		if ($(this).parent().hasClass('hide-expl') && $('.crime-explain-row').css('display') == 'block') {
			$('.crime-explain-row').slideUp();
		}
		
	});
};

function InitAllEvents(){	      
   autoHint();
   showInfoBox();
   InitCheckBoxExplain();	
   bindAddReference();
   bindDelReference();
   bindAddCurrEmployer();
   bindDelCurrEmployer();
   bindDelPrevEmployer();
   bindAddPrevEmployer();
   
}
function back(){
    $('button.back').unbind('click');
    $('button.back').bind('click', function(){
        var step = parseInt($('input[name="step"]').val());
        $('#info-box').load('application-form.html?step='+ step,function(){
                hideBox();
                InitAllEvents();                
                back();
        });
        
        return false;
    });
}
	
function bindAddReference(){    
    ref_count = parseInt($('#ref_sz').val()) + 1;
    
    $('button.add-reference').click(function(){
    var innerHtml = '<h4>Personal Reference '+ ref_count +'</h4>' +
                    '<div class="form-row"><label for="name'+ ref_count + '">Name:</label>' +
                    '<input type="text" id="name'+ ref_count +'" name="r_name'+ ref_count +'" value="" /></div>' +
                    '<div class="form-row">' +
                        '<label for="relationship'+ ref_count +'">Relationship:</label>'+
                        '<input type="text" id="relationship'+ ref_count +'" class="address" name="r_relationship'+ ref_count +'" value="" />' +
                        '<label for="years-known'+ ref_count +'" class="for-radio">Years Known:</label>' +
                        '<input type="text" id="years-known'+ ref_count +'" class="years-known" name="r_year'+ ref_count +'" value="" /></div>' +
                    '<div class="form-row">'+
                        '<label for="day-phone'+ ref_count +'">Day Phone:</label>' + 
                        '<input type="text" id="day-phone'+ ref_count +'" class="year-compl first" name="d_phone'+ ref_count +'" value="" />' +
                        '<label for="evening-phone'+ ref_count +'">Evening Phone:</label>' + 
                        '<input type="text" id="evening-phone'+ ref_count +'" class="evening-phone" name="e_phone'+ ref_count +'" value="" /></div>' +
                    '<div class="form-row">' +
                        '<label for="email-address'+ ref_count +'">E-mail Address:</label>' +
                        '<input type="text" id="email-address'+ ref_count +'" name="r_email'+ ref_count +'" value="" /></div>' +
                        '<input type="hidden" class="ref-id" name="ref_id_'+ ref_count +'" value="0" />' +
                    '<div class="form-row">'+
                        '<button class="del-reference" type="submit">Del Another Reference</button></div>';
        $('#ref-end').before("<div id='ref_"+ref_count+"'>"+innerHtml+"</div>");
		$('#ref_sz').val(ref_count);		       
		ref_count = ref_count+1;
        $('button.del-reference').hide();
        $('button.del-reference:last').show();        
		bindDelReference();
        
        return false;
	});

    
}

function bindDelReference(){    
    $('button.del-reference').unbind('click');
    $('button.del-reference').bind('click', function(){
        $(this).parent().parent().remove();
        ref_count = ref_count-1;
        $('#ref_sz').val(ref_count - 1);        
        var ref = $(this).parent().parent().find('.ref-id');
        if ( ref.val() > 0 ){
            $.post('application-form.html',{S2XEvent:'DeleteReference', id: ref.val()}, function(){
                $('button.del-reference').hide();
                $('button.del-reference:last').show();
            });
        }
        $('button.del-reference').hide();
        $('button.del-reference:last').show();
    });   
    
}

function bindAddCurrEmployer(){    
    cur_emp_sz = parseInt($('#cur_emp_sz').val()) + 1;
    $('button.add-cur-employer').unbind('click');
    $('button.add-cur-employer').click(function(){
        var innerHtml = '';
        $.ajax({
                type: 'GET', url: 'application-form.html?S2XEvent=Step3Ajax&count='+cur_emp_sz+'&type=current', 
                success: function ( responseData ){
                    innerHtml = responseData;
                    $('#current_emploer_end').before(innerHtml);
                    $('#cur_emp_sz').val(cur_emp_sz);	                                        
                    $('#current-emp-' + cur_emp_sz + ' .yesno input[type=radio]').each(function(){
                        $(this).prettyCheckboxes({
                            checkboxWidth: 13, 
                            checkboxHeight: 13
                        });                    
                    });
                    cur_emp_sz = cur_emp_sz+1;
                    $('button.del-current-employer').hide();
                    $('button.del-current-employer:last').show();      
                    
                    bindDelCurrEmployer();
                    autoHint();                    
                }
        });		
        
        return false;
	});    
}
function bindDelCurrEmployer(){    
    $('button.del-current-employer').unbind('click');
    $('button.del-current-employer').bind('click', function(){
        $(this).parent().parent().remove();
        cur_emp_sz = cur_emp_sz-1;
        $('#cur_emp_sz').val(cur_emp_sz - 1);        
        var emp = $(this).parent().parent().find('.emp-id');
        if ( emp.val() > 0 ){
            $.post('application-form.html',{S2XEvent:'DeleteEmploer', id: emp.val()}, function(){
                $('button.del-current-employer').hide();
                $('button.del-current-employer:last').show();
            });
        }
        $('button.del-current-employer').hide();
        $('button.del-current-employer:last').show();
    });   
    
}
function bindAddPrevEmployer(){    
    prev_emp_sz = parseInt($('#prev_emp_sz').val()) + 1;
    $('button.add-prev-employer').unbind('click');
    $('button.add-prev-employer').click(function(){
        var innerHtml = '';
        $.ajax({
                type: 'GET', url: 'application-form.html?S2XEvent=Step3Ajax&count='+prev_emp_sz+'&type=previous', 
                success: function ( responseData ){
                    innerHtml = responseData;
                    $('#previous_emploer_end').before(innerHtml);
                    $('#prev_emp_sz').val(prev_emp_sz);		
                    $('#previous-emp-' + prev_emp_sz + ' .yesno input[type=radio]').each(function(){
                        $(this).prettyCheckboxes({
                            checkboxWidth: 13, 
                            checkboxHeight: 13
                        });                    
                    });                    
                    prev_emp_sz = prev_emp_sz+1;
                    $('button.del-previous-employer').hide();
                    $('button.del-previous-employer:last').show();        
                    bindDelPrevEmployer();
                    autoHint();                    
                }
        });		
        
        
        return false;
	});    
}
function bindDelPrevEmployer(){    
    $('button.del-previous-employer').unbind('click');
    $('button.del-previous-employer').bind('click', function(){
        $(this).parent().parent().remove();
        prev_emp_sz = prev_emp_sz-1;
        $('#prev_emp_sz').val(prev_emp_sz - 1);        
        var emp = $(this).parent().parent().find('.emp-id');
        if ( emp.val() > 0 ){
            $.post('application-form.html',{S2XEvent:'DeleteEmploer', id: emp.val()});
        }
        $('button.del-previous-employer').hide();
        $('button.del-previous-employer:last').show();
    });   
    
}

function viewError(str){
    list = str.split(':');
    if (list[2] != undefined){
        var error = list[2].split(';');
        var sz = error.length;
        for ( var i = 0; i < sz; i++ ){
            var input = $('input[name="' + error[i] + '"]');
            input.addClass('red-error');
            //$('label[for="' + input.attr('id') + '"]').addClass('red');
        }
    }   
}
function hideError(){
    $('input').removeClass('red-error');;
}