var $jq = jQuery.noConflict();
$jq(document).ready(function(){

/** Datepicker **/
	get_datepicker();
	get_datepicker11();
	tinymce_editor(); 
		
	$jq('#dapshortcodebtn').on('click',function() {

		var isloggedin = $jq('#isloggedin').val();
		var hasaccessto = $jq('#hasaccessto').val();
		var hasaccesstoand = $jq('#hasaccesstoand').val();
		var hasnoaccessto = $jq('#hasnoaccessto').val();
		var hasexpiredaccessto = $jq('#hasexpiredaccessto').val();
		var neverhadaccessto = $jq('#neverhadaccessto').val();
		var errmsgtemplate = $jq('#errmsgtemplate').val();
		var userid = $jq('#userid').val();
		var startday = $jq('#startday').val();
		var endday = $jq('#endday').val();  
		var startdate = $jq('#startdate').val();  
		var enddate = $jq('#enddate').val();  
		var publicuntil = $jq('#publicuntil').val();  
		var publicafter = $jq('#publicafter').val();  
		var add_html0 = $jq('.div_html_cls').html(); 
		var add_html1 = add_html0.replace('<br>', ''); 
		var add_html = add_html1.replace('&nbsp;', ''); 
		var add_htmltext = $jq('.div_html_cls').text();  
		var gdprstatus = $jq('#gdprstatus').val();  
		
		if(isloggedin != 0){
			var setisloggedin = ' isloggedin="'+isloggedin+'"';
		}else{
			var setisloggedin = "" ;
		}
		
		if(hasaccessto != 0){
			hasaccessto = hasaccessto.toString();
			hasaccessto = hasaccessto.replace('0,', '');
			hasaccessto = hasaccessto.replace('0,', '');
			var sethasaccessto = ' hasaccessto="'+hasaccessto+'"';
			
		}else{
			var sethasaccessto = "" ;
		}
		
		if(hasaccesstoand != 0){
			hasaccesstoand = hasaccesstoand.toString();
			hasaccesstoand = hasaccesstoand.replace('0,', '');
			hasaccesstoand = hasaccesstoand.replace('0,', '');
			var sethasaccesstoand = ' hasaccesstoand="'+hasaccesstoand+'"';
			
		}else{
			var sethasaccesstoand = "" ;
		}
		
		if(hasnoaccessto != 0){
			hasnoaccessto = hasnoaccessto.toString();
			hasnoaccessto = hasnoaccessto.replace('0,', '');
			hasnoaccessto = hasnoaccessto.replace('0,', '');
			var sethasnoaccessto = ' hasnoaccessto="'+hasnoaccessto+'"';
		}else{
			var sethasnoaccessto = "" ;
		}
		
		if(hasexpiredaccessto != 0){
			hasexpiredaccessto = hasexpiredaccessto.toString();
			hasexpiredaccessto = hasexpiredaccessto.replace('0,', '');
			hasexpiredaccessto = hasexpiredaccessto.replace('0,', '');
			var sethasexpiredaccessto = ' hasexpiredaccessto="'+hasexpiredaccessto+'"';
		}else{
			var sethasexpiredaccessto = "" ;
		}
		
		if(neverhadaccessto != 0){
			neverhadaccessto = neverhadaccessto.toString();
			neverhadaccessto = neverhadaccessto.replace('0,', '');
			neverhadaccessto = neverhadaccessto.replace('0,', '');
			var setneverhadaccessto = ' neverhadaccessto="'+neverhadaccessto+'"';
		}else{
			var setneverhadaccessto = "" ;
		}
		
		
		if( (hasaccesstoand != 0) && ((hasaccessto != "ANY") &&(hasaccessto != 0))){ 
			alert("Cannot use both 'Has Access To (OR)' and 'Has Accesss To (AND)' Rules" );
			return false;
		}
		
		if(errmsgtemplate != 0){
			var seterrmsgtemplate = ' errmsgtemplate="'+errmsgtemplate+'"';
		}else{
			var seterrmsgtemplate = "" ;
		}

		if(userid > 0){
			var setuserid = ' userid="'+userid+'"';
		}else{
			var setuserid = "" ;
		}

		if((startday != 0 || endday != 0) && (startdate != 0 || enddate != 0) ){ 
			alert("Cannot use both day and date-based dripping." );
			return false;
		}
					
		if(startday != 0){
			if(hasaccessto != 0){
				var setstartday = ' startday="'+startday+'"';
			}else{
				alert("HasAccessTo is a required field for 'Start Day'." );
				return false;
			}
		
			var setstartday = ' startday="'+startday+'" ';
		}else{
			var setstartday = "" ;
		}
		
		if(endday != 0){
			if(hasaccessto != 0 ){
				var setendday = ' endday="'+endday+'"';
			}else{
				alert("HasAccessTo is a required field for 'End Day'." );
				return false;
			}
			var setendday = 'endday="'+endday+'"';
		}else{
			var setendday = "" ;
		} 
		
		
		if(startdate != 0){
			if(hasaccessto != 0){
				var setstartdate = ' startdate="'+startdate+'"';
			}else{
				alert("HasAccessTo is a required field for 'Start Day'." );
				return false;
			}
			if(enddate != 0){
				var setstartdate = ' startdate="'+startdate+'"';
			}else{
				alert("Please select End Date." );
				return false;
			}
		}else{
			var setstartdate = "" ;
		}
		
		if(enddate != 0){
			if(hasaccessto != 0){
				var setenddate = ' enddate="'+enddate+'"';
			}else{
				alert("HasAccessTo is a required field for 'End Day'. " );
				return false;
			}
			if(startdate != 0){
				var setenddate = ' enddate="'+enddate+'"';
			}else{
				alert("Please select Start Date." );
				return false;
			}
		}else{
			var setenddate = "" ;
		}
		
		
		if(publicuntil != 0){
			var setpublicuntil = ' publicuntil="'+publicuntil+'"';
		}else{
			var setpublicuntil = "" ;
		}
		
		if(publicafter != 0){
			var setpublicafter = ' publicafter="'+publicafter+'"';
		}else{
			var setpublicafter = "" ;
		}

		if(add_htmltext != 0 ){
			var add_html = add_html;
		}else{
			var add_html = "" ;
		}

		if(gdprstatus != 0 ){
			var setGDPRStatus = ' GDPRStatus="'+gdprstatus+'"';
		}else{
			var setGDPRStatus = "" ;
		}
		

		var shortcode = '[DAP'+setisloggedin + sethasaccessto + sethasnoaccessto +  sethasaccesstoand + sethasexpiredaccessto + setneverhadaccessto + seterrmsgtemplate + setuserid + setstartday + setendday + setstartdate + setenddate + setpublicuntil + setpublicafter + setGDPRStatus + ']'+add_html+'[/DAP]';

		jQuery('.shortcode_container').show();
		jQuery('.shortcode_container textarea').html(shortcode);
	}); 
	
});	
	
function dap_open_popup(){
	tinymce_editor();
	/* $jq('#dap_shortcode_popup').show(); */

}
function get_datepicker(){
	jQuery("input[name='startdate']").datepicker({
		dateFormat: "yy-mm-dd",
		 onSelect: function(selected) {
			jQuery("input[name='enddate']").datepicker("option","minDate", selected);

		} 
	}).datepicker();
	
	jQuery('.get_datepicker').click(function () {
		jQuery("input[name='startdate']").datepicker('show');
	});
	
	jQuery("input[name='enddate']").datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(selected) {
			jQuery("input[name='startdate']").datepicker("option","maxDate", selected);
		} 
	}).datepicker();
	
	jQuery('.get_datepicker1').click(function () {
		jQuery("input[name='enddate']").datepicker('show');
	});
	
	  
}
function get_datepicker11(){
	jQuery("input[name='publicuntil']").datepicker({
		dateFormat: "yy-mm-dd",
		 onSelect: function(selected) {
			jQuery("input[name='publicafter']").datepicker("option","minDate", selected);

		} 
	}).datepicker();
	
	jQuery('.get_datepicker11').click(function () {
		jQuery("input[name='publicuntil']").datepicker('show');
	});
	
	jQuery("input[name='publicafter']").datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(selected) {
			jQuery("input[name='publicuntil']").datepicker("option","maxDate", selected);
		} 
	}).datepicker();
	
	jQuery('.get_datepicker111').click(function () {
		jQuery("input[name='publicafter']").datepicker('show');
	});
	
	  
}

function tinymce_editor() {  

	tinymce.init({
		selector: '.editable_textarea',
		inline: true,
		theme: 'modern',
		force_br_newlines: true,
		force_p_newlines: false,
		resize: "both",
		object_resizing: "img",
		forced_root_block: '',
		fontsize_formats: "8pt 9pt 10pt 11pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 22pt 24pt 26pt 30pt",
		theme_advanced_fonts: "Andale Mono=andale mono,times;" + "Arial=arial,helvetica,sans-serif;" + "Arial Black=arial black,avant garde;" + "Book Antiqua=book antiqua,palatino;" + "Comic Sans MS=comic sans ms,sans-serif;" + "Courier New=courier new,courier;" + "Georgia=georgia,palatino;" + "Helvetica=helvetica;" + "Impact=impact,chicago;" + "Symbol=symbol;" + "Tahoma=tahoma,arial,helvetica,sans-serif;" + "Terminal=terminal,monaco;" + "Times New Roman=times new roman,times;" + "Trebuchet MS=trebuchet ms,geneva;" + "Verdana=verdana,geneva;" + "Webdings=webdings;" + "Wingdings=wingdings,zapf dingbats",
		plugins: ['advlist autolink lists link image charmap print preview hr anchor pagebreak', 'searchreplace wordcount visualblocks visualchars code fullscreen', 'insertdatetime media nonbreaking save table contextmenu directionality', 'emoticons template paste textcolor colorpicker textpattern imagetools'],
		toolbar1: 'insertfile undo redo | styleselect | bold italic |  fontselect | fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
		toolbar2: 'print preview media | forecolor backcolor emoticons | codesample',
		image_advtab: true,
		templates: [{
			title: 'Test template 1',
			content: 'Test 1'
		}, {
			title: 'Test template 2',
			content: 'Test 2'
		}]
	});
}


