// source --> https://atifonline.org/wp-content/plugins/presspoint/paumail/assets/j/paumail.js?ver=6.9.4 
jQuery.noConflict();


// AJAX TEST FOR ERRORS
function paumail_js_errors() {

	// RESET THE ERRORS
	jQuery('*').removeClass('halt');
	jQuery('.errors').remove();
	
	// RUN A FRESH CHECK
	jQuery('#paumail_campaign_form .pau-required').siblings().find(':input').each(function(){
	
		if ( jQuery(this).closest('li.meta-item').is(':visible') ) {
			
			var jval = jQuery(this).val().trim();
	    	if ( 0 === jval.length || '0' == jval || 'false' == jval ) {
	    		// SPECIAL EXEMPTION FOR CHOSEN
	    		if ( jQuery(this).closest('li.meta-item').hasClass('chzn-req') ) {
	    		} else {
	    			jQuery(this).closest('li.meta-item').addClass('halt');
	    		}
	    	}
	    	
	    	if ( jQuery(this).attr('id') == '_pp_mail_segment' && 'text' == jQuery(this).attr('type') ) {
	    		var etest = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	    		if ( false == etest.test(jval) ) {
	    			jQuery('#_pp_mail_segment').closest('li.meta-item').addClass('halt');
	    		}
	    	}
	    	
	    	// SPECIAL CASE FOR CHOSEN
	    	if ( jQuery(this).hasClass('chzn-select') ) {
	    		
	    		// RESET THE CONTEXT
	    		jQuery(this).closest('li.meta-item').removeClass('halt').addClass('chzn-req');
	    		//mychzn = jQuery(this).attr('id');
	    		//jQuery(this).closest('li.meta-item').addClass(mychzn);
	    		jQuery(this).siblings().find('.chzn-results').each(function(){
	    			if ( jQuery('li',this).hasClass('result-selected') ) {
	    				jQuery(this).closest('li.meta-item').removeClass('halt');
	    				return false;
	    			}
	    			jQuery(this).closest('li.meta-item').addClass('halt');
	    		});
	    		
	    	}
	    	
	    }
		
	});
	
	jQuery('#paumail_campaign_form .pau-required').siblings().find(':checkbox').each(function(){
		if ( jQuery(this).is(':checked') ) {
			jQuery(this).closest('li.meta-item').removeClass('halt');
			return false;
		}
		jQuery(this).closest('li.meta-item').addClass('halt');
	});
	
	if ( jQuery('.halt').length != 0 ) {
		// TAKE ME HOME, SCOTTY!
		if ( !jQuery('#errors').length ) {
			jQuery('#paumail_campaign_form').prepend('<div class="pp-error">'+paumailAjax.errMsg+'</div>');
		}
		jQuery('html, body').animate({ scrollTop: jQuery('#paupress').offset().top }, 'slow');
		return true;
	}
	
	return false;

}

function paumail_toggle_status() {
	var val = jQuery('#_pp_mail_destination').val();
	
	if ( '' == val || 'draft' == val ) {
		jQuery('#paumail_submission').attr('disabled', 'disabled');
	} else {
		jQuery('#paumail_submission').removeAttr('disabled');
	}
	 
}


function paumail_template_preview_close() {
	jQuery("#paupress-overlay").fadeOut('slow').remove();
}

function paumail_template_preview() {
	var val = jQuery('#paumail-template-selector').val();
	var pid = jQuery('#paumail_post_id').val();
	if ( typeof tinyMCE !== 'undefined' ) {
		if ( tinyMCE.get('content') != null ) {
			var tmcecontent = tinyMCE.get('content').getContent({format : 'raw'});
		} else if ( tinyMCE.get('post_content') != null ) {
			var tmcecontent = tinyMCE.get('post_content').getContent({format : 'raw'});
		} else {
			var tmcecontent = tinyMCE.activeEditor.getContent({format : 'raw'});
		}
	} else {
		var tmcecontent = jQuery('#post_content').val();
	}
	
	if ( jQuery(this).data('builder') ) {
		var tmcecontent = '';
	}
	
	// EMPTY OUT THE MAIN HOLDER'S ELEMENTS AND PLAY THE LOADER
	//jQuery('#paumail-template-previewer').empty();
	jQuery('body').prepend('<div id="paupress-overlay"></div>');
	jQuery('#paupress-overlay').prepend('<div id="paumail-template-previewer">'+paumailAjax.ajaxload+'</div>');
	 
	jQuery.post( 
    	paumailAjax.ajaxurl, { 
    	action : 'paumail_template_action', 
    	data : val, 
    	dhtm : tmcecontent, 
    	dpid : pid, 
    	paumail_nonce : paumailAjax.paumail_nonce 
    	},
        function( response ) {
            // DISPLAY THE RESPONSE
            jQuery('#paumail-template-previewer').html(response);
            
            //var tmcecontent = tinyMCE.activeEditor.getContent({format : 'raw'});
            //jQuery('div[mc\\:edit="main"]').html(tmcecontent);
            
            //tb_show(null,'#TB_inline?height=500&width=630&inlineId=paumail-template-previewer',null);
            /*jQuery('#paumail-template-previewer').find('script').each(function(i) {
            	eval(jQuery(this).text());
            });*/
         }
    );
 
}


function paumail_reload_templates() {
	
	//jQuery('#paumail-template-selector option[value*="mc_"]').remove();
	
	// EMPTY OUT THE HOLDER
	//jQuery('#paumail-template-preholder').empty();
	jQuery('#paumail-template-preholder').html('<div id="paumail-template-previewer">'+paumailAjax.ajaxload+'</div>');
	 
	jQuery.post( 
    	paumailAjax.ajaxurl, { 
    	action : 'paumail_reset_templates', 
    	data : true, 
    	paumail_nonce : paumailAjax.paumail_nonce 
    	},
        function( response ) {
            // DISPLAY THE RESPONSE
            jQuery('#paumail-template-preholder').html(response);
            //jQuery('#paumail-template-preholder option').appendTo('#paumail-template-selector');
            
         }
    );
 
}

function paumail_disable() {
	jQuery(this).closest('ul.paupress-list').children('input').each(function(){ 
		jQuery(this).attr("disabled","disabled"); 
		alert('doin it!');
	});
}

function paumail_confirm_submission() {
	
	var errors = paumail_js_errors();
	if ( errors ) {
		return false;
	}
	
	var answer = confirm( paumailAjax.confirmSubmit );
	if (answer) {
		jQuery('#paumail_submission').click();
	}
	
	return false;
}

/*
function paumail_template_insert() {
	var tid = jQuery(this).attr('id');
	
	// EMPTY OUT THE MAIN HOLDER'S ELEMENTS AND PLAY THE LOADER
	jQuery('#content').empty();
	 
	jQuery.post( 
    	paumailAjax.ajaxurl, { 
    	action : 'paumail_template_action', 
    	data : tid, 
    	mode : 'insert',
    	paumail_nonce : paumailAjax.paumail_nonce 
    	},
        function( response ) {
            // DISPLAY THE RESPONSE
            jQuery('#post_content').html(response);
         }
    );
 
}
*/

function paumail_post_box_send() {

	if ( jQuery(this).attr('disabled') ) {
		return false;
	}

	var errors = paumail_js_errors();
	if ( errors ) {
		return false;
	}
	
	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paupress').find(':input, select').serialize();
	// ]]>
		
	// EMPTY OUT THE MAIN HOLDER'S ELEMENTS AND PLAY THE LOADER
	jQuery('.paumail-receipt-items').empty().html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
		
	// SUBMIT THE FORM DATA
	jQuery.post( 
		
		paumailAjax.ajaxurl, { 
			action : 'paumail_ajax_mail_by_post', 
			data : str, 
			paumail_nonce : paumailAjax.paumail_nonce 
		},
	    function( response ) {
	        // DISPLAY THE RESPONSE
	        jQuery('.paumail-receipt-items').html(response);
			paupress_bind_events();
			paumail_check_receipts();
	    }
	);

}


function paumail_check_receipts() {
	jQuery('#send-toggle').closest('ul.paupress').find('input, select, textarea').attr('disabled', 'disabled');
	jQuery('#paumail_meta_submit').attr('disabled', 'disabled');
}

function paumail_send_toggle() {
	if ( jQuery(this).hasClass('off') ) {
		jQuery(this).closest('ul.paupress').find('input, select, textarea').removeAttr('disabled');
		jQuery('#paumail_meta_submit').removeAttr('disabled', 'disabled');
	} else {
		jQuery(this).closest('ul.paupress').find('input, select, textarea').attr('disabled', 'disabled');
		jQuery('#paumail_meta_submit').attr('disabled', 'disabled');
	}
	jQuery(this).toggleClass('on off');
}

function paumail_template_render() {
	var data = jQuery(this).val();
	var type = jQuery(this).data('type');
	var sgmt = jQuery(this).data('sgmt');
	var user = jQuery(this).data('user');
	var post = jQuery(this).attr('title');
	var rent = jQuery(this).data('parent');
	var view = jQuery(this).data('view');
	var body = jQuery(this).data('body');

	jQuery('#paumail-template-render').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post( 
		paumailAjax.ajaxurl, { 
			action : 'paumail_get_template_layout', 
			data : data, 
			post : post, 
			type : type, 
			sgmt : sgmt, 
			user : user, 
			rent : rent, 
			view : view, 
			body : body, 
		},
    function( response ) {
    	jQuery('#paumail-template-render').html(response);
    	var postval = jQuery('#paumail-template-render #receipt_id_post_id').val();
    	if ( postval ) {
    		jQuery('#paumail-templ-select').attr('title',postval);
    	} else {
    		jQuery('#paumail-template-render').append('<p>We have an error. Please try again.</p>');
    	}
			paupress_bind_events();
    }
	);
}
function paumail_template_render_text() {
	var data = jQuery(this).val();
	var type = jQuery(this).data('type');
	var sgmt = jQuery(this).data('sgmt');
	var user = jQuery(this).data('user');
	var post = jQuery(this).attr('title');
	var rent = jQuery(this).data('parent');
	var view = jQuery(this).data('view');
	var body = jQuery(this).data('body');
	
	jQuery('#paumail-template-render-text').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post( 
		paumailAjax.ajaxurl, { 
			action : 'paumail_get_template_layout', 
			data : data, 
			post : post, 
			type : type, 
			sgmt : sgmt, 
			user : user, 
			rent : rent, 
			view : view, 
			body : body, 
		},
    function( response ) {
    	jQuery('#paumail-template-render-text').html(response);
    	var postval = jQuery('#paumail-template-render-text #receipt_id_post_id').val();
    	if ( postval ) {
    		jQuery('#paumail-templ-select').attr('title',postval);
    	} else {
    		jQuery('#paumail-template-render-text').append('<p>We have an error. Please try again.</p>');
    	}
			paupress_bind_events();
    }
	);
}


function paumail_operation_listener(frm,src){

	jQuery.post( 
		paumailAjax.ajaxurl, { 
			action : 'paupress_process_operations', 
			data : frm, 
			meta : src, 
		}, 
		function( response ) {
			jQuery('#progress').html(response);
			if ( jQuery('#progress #continuing').length ) {
				var src = jQuery('#progress #continuing').attr('title');
				setTimeout(function(){ paumail_operation_listener(src); }, 1000);
			}
		}
	);

}


// AJAX FORM SUBMISSION FOR THE MAIN SEARCH
function paumail_operation_submit() { 
	
	var qry = jQuery('#_pp_mail_segment').val();
	
	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	// CHECK FOR POLLED PROCESSING
	if ( !qry ) {
		alert( paupressReportAjax.noselection );
		return false
	} else {
		// EMPTY OUT THE MAIN HOLDER'S ELEMENTS AND PLAY THE LOADER
		jQuery('#paumail-templ-select').hide();
		jQuery(this).closest('.paumail-receipt-items').empty().html('<div id="progress"><div class="pauloader">'+paumailAjax.ajaxload+'</div></div>');
		setTimeout(function(){ paumail_operation_listener(qry,str); }, 1000);
	}
    
}
// AJAX FORM SUBMISSION FOR THE MAIN SEARCH WITH SLIDEOUT
function paumail_operation_submit_report() { 
	
	// CHECK FOR POLLED PROCESSING
	if ( !jQuery('.gredit-user', parent.document).is(':checked') ) {
		var answer = confirm( paupressReportAjax.noselection );
		if (!answer) { return false; }
		var pquery = jQuery('#grexport-post-vars', parent.document).val();
		if ( pquery == '' || pquery == null ) {
			var uquery = jQuery('.gredit-user:checkbox', parent.document).map(function(){
			  return jQuery(this).val();
			}).get();
			var qry = '_paupress_u_'+uquery.join('_');
		} else {
			var qry = '_paupress_q_'+pquery;
		}
	} else {
		var uquery = jQuery('.gredit-user:checkbox:checked', parent.document).map(function(){
		  return jQuery(this).val();
		}).get();
		var qry = '_paupress_u_'+uquery.join('_');
	}
	jQuery('#_pp_mail_segment').val(qry);
	
	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	// EMPTY OUT THE MAIN HOLDER'S ELEMENTS AND PLAY THE LOADER
	jQuery('#paumail-templ-select').hide();
	jQuery(this).closest('.paumail-receipt-items').empty().html('<div id="progress"><div class="pauloader">'+paumailAjax.ajaxload+'</div></div>');
	setTimeout(function(){ paumail_operation_listener(qry,str); }, 1000);
	
}

// AJAX FORM SUBMISSION FOR THE MAIN SEARCH
function paumail_operation_draft() { 

	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	var rndr = jQuery(this).closest('#paumail-template-render');
	jQuery('#paumail-templ-select').hide();
	rndr.html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post(
		paumailAjax.ajaxurl, {
			action : 'paumail_save_draft', 
			data : str, 
		},
		function( response ) {
			rndr.html(response);
		}
	);
  
}
// AJAX FORM SUBMISSION FOR THE MAIN SEARCH WITH SLIDEOUT
function paumail_operation_draft_report() { 
	
	// CHECK FOR POLLED PROCESSING
	if ( !jQuery('.gredit-user', parent.document).is(':checked') ) {
		var answer = confirm( paupressReportAjax.noselection );
		if (!answer) { return false; }
		var pquery = jQuery('#grexport-post-vars', parent.document).val();
		if ( pquery == '' || pquery == null ) {
			var uquery = jQuery('.gredit-user:checkbox', parent.document).map(function(){
			  return jQuery(this).val();
			}).get();
			var qry = '_paupress_u_'+uquery.join('_');
		} else {
			var qry = '_paupress_q_'+pquery;
		}
	} else {
		var uquery = jQuery('.gredit-user:checkbox:checked', parent.document).map(function(){
		  return jQuery(this).val();
		}).get();
		var qry = '_paupress_u_'+uquery.join('_');
	}
	jQuery('#_pp_mail_segment').val(qry);
	
	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	jQuery('#paumail-templ-select').hide();
	jQuery('#paumail-template-render').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post(
		paumailAjax.ajaxurl, {
			action : 'paumail_save_draft', 
			data : str, 
		},
		function( response ) {
			jQuery('#paumail-template-render').html(response);
		}
	);
}
function paumail_operation_draft_text() { 

	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	var rndr = jQuery(this).closest('#paumail-template-render-text');
	jQuery('#paumail-templ-select').hide();
	rndr.html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post( 
		paumailAjax.ajaxurl, { 
			action : 'paumail_save_draft', 
			data : str, 
		},
		function( response ) {
			rndr.html(response);
		}
	);
  
}
// AJAX FORM SUBMISSION FOR THE MAIN SEARCH WITH SLIDEOUT
function paumail_operation_draft_text_report() { 
	
	// CHECK FOR POLLED PROCESSING
	if ( !jQuery('.gredit-user', parent.document).is(':checked') ) {
		var answer = confirm( paupressReportAjax.noselection );
		if (!answer) { return false; }
		var pquery = jQuery('#grexport-post-vars', parent.document).val();
		if ( pquery == '' || pquery == null ) {
			var uquery = jQuery('.gredit-user:checkbox', parent.document).map(function(){
			  return jQuery(this).val();
			}).get();
			var qry = '_paupress_u_'+uquery.join('_');
		} else {
			var qry = '_paupress_q_'+pquery;
		}
	} else {
		var uquery = jQuery('.gredit-user:checkbox:checked', parent.document).map(function(){
		  return jQuery(this).val();
		}).get();
		var qry = '_paupress_u_'+uquery.join('_');
	}
	jQuery('#_pp_mail_segment').val(qry);
	
	// CDATA TO AVOID VALIDATION ERRORS
	//<![CDATA[
	var str = jQuery(this).closest('.paumail-template-choice').find(':input, select').serialize();
	// ]]>
	
	jQuery('#paumail-templ-select').hide();
	jQuery('#paumail-template-render-text').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
	jQuery.post(
		paumailAjax.ajaxurl, {
			action : 'paumail_save_draft', 
			data : str, 
		},
		function( response ) {
			jQuery('#paumail-template-render-text').html(response);
		}
	);
}
function paumail_operation_schedule() {
	
	var call = jQuery(this).data('call');
	if ( jQuery(this).parents('#pp-slide-data').length ) {
		var mode = '#pp-slide-data';
	} else {
		var mode = 'body';
	}

	jQuery(mode).prepend('<div class="paupress-overlay paupress-overlay-absolute"></div>');
	jQuery('.paupress-overlay').prepend('<div id="paupress-modal-action">'+paumailAjax.oneMoment+'</div>');
		 
	jQuery.post( 
    	paumailAjax.ajaxurl, { 
    	action : 'paumail_scheduler', 
    	call : call, 
    	},
        function( response ) {
        	jQuery('#paupress-modal-action').html(response);
         }
    );
 
}
function paumail_operation_schedule_process() {
	//<![CDATA[
	var str = jQuery('#paumail-schedule').find('input,select').serialize();
	// ]]>
	jQuery('#receipt_id_post_id').closest('div').append('<input type="hidden" name="_pp_post[_pp_schedule]" value="'+str+'" />');
	jQuery('#paumail-schedule .pp-close-overlay').click();
	if ( jQuery('#paumail-schedule-info').length != 0 ) {
		var pid = jQuery('#paumail-schedule-info').data('post');
		jQuery('#paumail-schedule-info').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');	
		jQuery.post( 
				paumailAjax.ajaxurl, { 
				action : 'paumail_schedule_process', 
				post : pid,
				data : str, 
				},
				function( response ) {
					jQuery('#paumail-schedule-info').html(response);
				}
			);
	} else if ( jQuery('#paumail_submit_it').length != 0 ) {
		jQuery(document).find('#paumail_submit_it').click();
	} else if ( jQuery('#paumail-submit').length != 0 ) {
		jQuery('#paumail-submit').click();
	} else if ( jQuery('#paumail-submit-report').length != 0 ) {
		jQuery('#paumail-submit-report').click();
	}
	return false;
}
function paumail_operation_schedule_delete() {
	
	var pid = jQuery('#paumail-schedule-info').data('post');
	var vid = jQuery('#paumail-schedule-info').data('view');
	jQuery('#paumail-schedule-info').html('<div class="pauloader">'+paumailAjax.ajaxload+'</div>');
		 
	jQuery.post( 
    	paumailAjax.ajaxurl, { 
    	action : 'paumail_reschedule', 
    	data : pid, 
		view : vid,
    	},
      function( response ) {
      	jQuery('#paumail-schedule-info').html(response);
      }
  );
 
}

jQuery(document).ready(function () {
	
	jQuery(document).on('click', '.paumail-template-reload', paumail_reload_templates);
	jQuery(document).on('click', '.paumail-template-thumbnail', paumail_template_preview);
	jQuery(document).on('click', '#paumail_meta_submit', paumail_post_box_send);
	jQuery(document).on('click', '#paumail-submit', paumail_operation_submit);
	jQuery(document).on('click', '.paumail-submit-draft', paumail_operation_draft);
	jQuery(document).on('click', '.paumail-submit-draft-text', paumail_operation_draft_text);
	jQuery(document).on('click', '.paumail-schedule',paumail_operation_schedule);
	jQuery(document).on('click', '#paumail-submit-report', paumail_operation_submit_report);
	jQuery(document).on('click', '.paumail-submit-draft-report', paumail_operation_draft_report);
	jQuery(document).on('click', '.paumail-submit-draft-text-report', paumail_operation_draft_text_report);
	jQuery(document).on('click', '.paumail-schedule-report',paumail_operation_schedule);
	jQuery(document).on('click', '.paumail-schedule-go', paumail_operation_schedule_process);
	jQuery(document).on('click', '.paumail-schedule-delete',paumail_operation_schedule_delete);
	//jQuery('.paupress-viewer').on('change', '#_pp_mail_destination', paumail_toggle_status);
	jQuery('.paupress-viewer').on('click', '#interim_paumail_submission', paumail_confirm_submission);
	jQuery('.paupress-viewer').on('load', '.paumail-receipt', paumail_disable);
	jQuery(document).on('click', '#pp-template-close', paumail_template_preview_close);
	jQuery(document).on('click', '#send-toggle', paumail_send_toggle);
	jQuery(document).on('change', '#paumail-templ-select', paumail_template_render);
	jQuery(document).on('change', '#paumail-templ-select-text', paumail_template_render_text);
	

});
// source --> https://atifonline.org/wp-content/plugins/presspoint/pauview/assets/j/paugeo.js?ver=6.9.4 
jQuery.noConflict();

// when the DOM is ready...
jQuery(document).ready(function () {

	// INITIALIZE THE GOOGLE MAP
	if ( jQuery('#gmap').is(':visible') ){//.length != 0
	    initialize();
	    jQuery('.gmap-view').addClass('gmap_resize');
	}

	// RESIZE GOOGLE MAP ONLOAD
	jQuery(document).on('click', '#report-navigation .gmap_resize', function(){
		resizeMap();
	});

	// INITIALIZE THE GOOGLE MAP
	jQuery(document).on('click', '#report-navigation .gmap-view', function(){
		if ( jQuery(this).hasClass('gmap_resize') ) {
			resizeMap();
		} else {
			jQuery(this).addClass('gmap_resize');
			initialize();
		}
	});
	
	
	/*
	
	// FIX THE MAP ISSUE WHEN PAGED
	jQuery('#report-navigation').on('click', '.gmap', function(){
		jQuery(this).removeClass('gmap');
		jQuery(this).addClass('gmap_resize');
		initialize();
	});	
	*/
});