jQuery(document).ready(function($){
    var hash = document.location.hash;
    if ( hash )
        setTimeout(function(){ $('[href^='+hash+']').click() }, 500);

    $('#commentform').submit(function(){
    	$('[type=submit]').attr('disabled', 'disabled');
        var error = [];
        var tab = $('#sales_review').css('display') == 'block' ? $('#sales_review') : $('#service_review');
        if ( tab.find('.starrating-field').css('display') != 'none' && tab.find('.gdsr-mur-cls-rt').val().match(/0/) )
        	error.push("Please select all rating");
        if ( tab.find('.survey').css('display') != 'none' )
        {
        	if ( $(this).find('[name=survey_type]').val() == 1 )
        	{
        		if ( tab.find('[name="survey[0]"]:checked').size() < 1 || 
        			tab.find('[name="survey[1]"]:checked').size() < 1 || 
        			tab.find('[name="survey[2]"]').val() == '' || 
        			tab.find('[name="survey[3]"]:checked').size() < 1 || 
        			tab.find('[name="survey[4]"]:checked').size() < 1
        		)
        			error.push("Please fill all fields");
        	}
        	else if ( $(this).find('[name=survey_type]').val() == 2 )
        	{
        		if ( 
        			tab.find('[name="survey[0]"]:checked').size() < 1 || 
        			tab.find('[name="survey[1]"]').val() == '' || 
        			tab.find('[name="survey[2]"]').val() == '' || 
        			tab.find('[name="survey[3]"]').val() == '' || 
        			tab.find('[name="survey[4]"]:checked').size() < 1 || 
        			tab.find('[name="survey[5]"]:checked').size() < 1
        		)
        			error.push("Please fill all fields");
        	}
        }
        if ( $('#comment-user-details').css('display') == 'block' )
        {
	        if ( $(this).find('[name=author]').size() > 0 && $('#author').val() == '' )
	        	error.push("Please enter your name");
	        if ( $(this).find('[name=email]').size() > 0 && $('#email').val() == '' )
	        	error.push("Please enter your email");
    	}
        if ( $(this).find('[name=comment]').val() == '' )
        	error.push("Please enter your comment");
        if ( error.length > 0 )
        {
        	alert(error.join("\n"));
    		$('[type=submit]').removeAttr('disabled');
        	return false;
        }
    });
    
    $('#service_review').find('input, select').attr('disabled', 'disabled');
    
    $('#review-tab .review-tab-nav li a').click(function(){
    	var href = $(this).attr('href');
    	$('.review-tab-content').not(href).css('visibility', 'hidden');
    	$('.review-tab-nav li.active').removeClass('active');
    	$(this).parent().addClass('active');
    	$(href).show();
    	$('.review-tab-content').not(href).css('visibility', 'visible').hide();
    	$('.review-tab-content').not(href).find('input, select').attr('disabled', 'disabled');
    	$(href).find('input, select').removeAttr('disabled');
    	$('[name=survey_type]').val( href == '#sales_review' ? 1 : 2 );
    	return false;
    });
});
