
    
var altCommentCounter = 0;
var removedCommentAlt = new Array();

function validateCommentForm() {
   var validator = jQuery("#new_comment_form").validate({
    
    
       rules : {comment_you_tub : {you_tube_link : true}},
       messages :{comment_you_tub: {you_tube_link: 'Invalid you tube link'}},
       errorPlacement: function(error, element) {
                jQuery("#comment_error").html('');
                error.appendTo ("#comment_error");
        },
       submitHandler: function(data) {
             if(altCommentCounter >0 || jQuery('#comment_box').val() != '' || jQuery('#comment_you_tub').val() != '' ||CKEDITOR.instances.comment_box.getData()!= ''){
            var queryString = jQuery('#new_comment_form').formSerialize()+'&comment_box='+encodeURIComponent(CKEDITOR.instances.comment_box.getData());
                  
            var options = {
                            url :  baseUrlForJs+'/index/handlecomment',
                            target : '#response',
                            type    : 'POST',
                            data :  queryString,
                            beforeSend :function (XMLHttpRequest){
                                       jQuery("#comment_error").html('');
                                       jQuery("#loading_comment").show();
                                       jQuery("#loading_txt").show();
                                        
                                       jQuery("#submit_comment").hide(); 
                                        },
                            success: function (msg) {
								
                                jQuery("#loading_comment").hide();
                                 jQuery("#loading_txt").hide();
                                 
                                 jQuery("#submit_comment").show(); 
                                 if(jQuery.trim(msg) != 'False'){
                                    jQuery('#response').prepend('<a name="response_msg"></a>'+jQuery.trim(msg));
                                    jQuery('#comment_box').val("");
                                    jQuery('#no_comments').hide();
                                    //updatecount
                                    jQuery('#comment_error').show();
                                    jQuery('#comment_error').html('<div class="sucess_msg">Commented Posted sucessully</div>');
                                    updatePostCount(jQuery('#item_id').val(),jQuery('#module_name').val());
									
									//vibhor - cleanup process
									for(var i=1; i <= 10; i++) {
										if(jQuery('#li_'+i)) {
											jQuery('#li_'+i).remove();
											jQuery('#uploaded_alt_'+i).val('');
										}
									}
									//mahendra - send email to user 
									
									if(jQuery('#module_name').val() == 'members'){
										
										
									var qString = 'id='+jQuery('#item_id').val()+'&moduleName='+jQuery('#module_name').val();
									
										jQuery.ajax({  	url :  baseUrlForJs+'/index/sendemailtouser',
														type : 'POST',
														data : qString,
														success: function (msg) {
															return true;
															}
													});
										//alert('here');
										}
							    //alert(CKEDITOR.instances.comment_box.getData());			
								CKEDITOR.instances.comment_box.setData( '' );
								location.replace('#response_msg');
								/*jQuery('html, body').animate({ 
										scrollTop: jQuery("#response").offset().top}, 2000);*/
									
                                }
                                else{
                                jQuery.blockUI({ 
                                        message: jQuery('#login_div'), 
                                        css: { width: '375px' }
                                        
                                    });
                                    }
                               }
                            
                            };
            jQuery.ajax(options);
             }else{
             
                jQuery('#comment_error').show();
                jQuery('#comment_error').html('<div class="error">You must either comment, put youtube link or upload a file </div>');
               
             }
             
        }, 
        success: function(label) {             
            label.html(" ").addClass("checked"); 
        }
    });
}

function updatePostCount(item_id,module_name) {
    //console.log (module_name);
    jQuery.ajax({
                                    url :  baseUrlForJs+'/index/getblogcount',
                                    type : 'POST',
                                    data : {id : item_id,moduleName :module_name },
                                    success : function (msg) {
                                        var chunks = msg.split('|');
                                        if(jQuery('#has_comment').val() == 'no'){
					jQuery('#response').attr('style','margin-bottom:30px;');
                                          jQuery('#comment_png').show();
                                        }
                                        jQuery('#comment_count').hide();
                                        jQuery('#comment_count').html('');
                                        if(chunks[1] == 1){
                                          jQuery('#comment_count').html(chunks[1]+' comment');
                                        }else{
                                           jQuery('#comment_count').html(chunks[1]+' comment(s)');
                                        }
                                        jQuery('#comment_count').show();

										jQuery('#comment_counter').hide();
                                        jQuery('#comment_counter').html('');
                                        jQuery('#comment_counter').html(chunks[1]+' ');
                                        jQuery('#comment_counter').show();
                                        jQuery('#comment_you_tub').val('');
                                        
										jQuery("#comment_boxIFrame").contents().find("body").empty();

                                        altCommentCounter = 0;
                                        for(var blogCounter = 1; blogCounter <= 10; blogCounter++)
                                        {
                                            jQuery('#li_'+blogCounter).remove();
                                            jQuery('#uploaded_alt_'+blogCounter).val('');

                                        }

                                        for(var i=0 ; i < removedCommentAlt.length ; i++ ){
                                            removedCommentAlt.splice(i, 1);
                                        }
                                       jQuery('#upload_pic').show();
                                    }
                                });
}



function uploadCommentFile(type,moduleName) {
new Ajax_upload('#comment_'+type, {
                        //action: 'upload.php',
                        action: baseUrlForJs+'/index/handleblogfile/type/'+type+'/moduleName/'+moduleName,
                        name: 'file',
                        onSubmit: function(file, extension) {
                                    jQuery("#comment_"+type+"_error").html("");
                                    jQuery("#loading_comment").show();
                                    jQuery('#comment_'+type).hide(); 
                                    jQuery('#submit_comment').hide(); 
                                    jQuery('#comment_error').html('')
                                    
                                },

                        onComplete : function(data,response){
                       
                            jQuery("#comment_"+type+"_error").html("");
                            jQuery("#loading_comment").hide();
                            jQuery('#comment_'+type).show();
                            jQuery('#submit_comment').show();  
                            //split the string
                              var result = response.split('|');
                           
                            
                            var otherInc=parseInt(result[3])+1;
                            
                             if(jQuery.trim(result[0]) == "FAILED") {
                                jQuery("#comment_"+type+"_error").show();
                                
                                jQuery("#comment_"+type+"_error").html(result[1]);
                                  
                                
                             }
                             else {
                             
                             altCommentCounter=altCommentCounter+1;
                             
                             var ext = result[4];
                             
                             
                             if(removedCommentAlt.length > 0) { //user removed some pics
                                
                                removedCommentAlt.sort();
                                
                                var emptyAltCounter = removedCommentAlt.pop();
                                
                                 jQuery('#uploaded_alt_'+emptyAltCounter).val(jQuery.trim(result[1]));
                                var str = formatImageStr(emptyAltCounter,result[1], result[4]);
                             }
                             else {
                                 jQuery('#uploaded_alt_'+altCommentCounter).val(jQuery.trim(result[1]));
                                var str = formatImageStr(altCommentCounter,result[1], result[4]);
                               
                             
                             }
                             
                            jQuery('#upload_pic').before(str);
                            if(altCommentCounter == 10) {
                               jQuery('#upload_pic').hide(); 
                            }
                            
                             }
                              
                        }    
}); 

}

function formatImageStr(counter, result, ext) {
    if(ext != "null") {
        return '<li class="uploaded_img_preview" id="li_'+counter+'"><img id="'+counter+'_preview" src="'+baseUrlForJs+'/images/icons/'+ext+'.gif" /><div class="file_name">'+result+'</div><div class="change"><a href="javascript:void(0)" onclick="removeAltImage(\''+counter+'\')">(Remove)</a></div></li>';
    }
    else {
        return '<li class="uploaded_img_preview" id="li_'+counter+'"><img id="'+counter+'_preview" width="50" height="50" src="'+baseUrlForJs+'/temp/thumb_image/'+result+'" /><div class="change"><a href="javascript:void(0)" onclick="removeAltImage(\''+counter+'\')">(Remove)</a></div></li>';
    }
    
}

function removeAltImage(counter)
{
                                removedCommentAlt.push(counter);

                                jQuery('#li_'+counter).remove();
                                jQuery('#uploaded_alt_'+counter).val('');
                                
                                altCommentCounter=altCommentCounter-1;
                               
                                if(altCommentCounter < 10) {
                                  jQuery('#upload_pic').show();   
                                }                                   
                              
                          
}

jQuery(document).ready(function(){
  
    jQuery.validator.addMethod("you_tube_link", function (value, element) {
		return this.optional(element) || /^(ht|f)tp(s?)\:\/\/www\.youtube\.com(\/?)([a-zA-Z0-9\-\=\.\?\,\'\/\\\+&amp;%\$#_]*)?$/.test(value);
            },
            "Enter valid Youtube link "
    );

    //validateNewDesignForm();
});