<?php require_once(dirname(__FILE__).'/../../../../config.php'); ?>
<?php header("Content-type: text/javascript"); ?>
$ = jQuery;

//*** SELECT TEXT ***//
    function selectText(containerid) {
        if (document.selection) {
            var range = document.body.createTextRange();
            range.moveToElementText(document.getElementById(containerid));
            range.select();
        } else if (window.getSelection) {
            var range = document.createRange();
            range.selectNode(document.getElementById(containerid));
            window.getSelection().addRange(range);
        }
    }


$('.number').keypress(function (event) {
    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }

    var text = $(this).val();

    if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.'), text.indexOf('.').length).length > 2)) {
        event.preventDefault();
    }
});

//*** COLOR PICKER ***//

$('.color-picker').ColorPicker({
	onSubmit: function(hsb, hex, rgb, el) {
		$(el).val( "#"+hex );
		$(el).ColorPickerHide();
		$(el).ColorPickerSetColor(hex);

	//**UPDATE AD PREVIEW COLOR**//
	//		alert(el.id+'/'+hex);
	if ( el.id == 'title-color' ) {
		$( "#"+el.id+"-preview" ).css( "color", "#"+hex );
	}

	if ( el.id == 'link-color' ) {
		$( "#"+el.id+"-preview" ).css( "color", "#"+hex );
		$( "#button-color-preview" ).css( "background", "#"+hex );
	}

	if ( el.id == 'text-color' ) {
		$( "#"+el.id+"-preview" ).css( "color", "#"+hex );
	}

	if ( el.id == 'background-color' ) {
		$( "#ad-design-div" ).css( "background", "#"+hex );
		$( "#button-color-preview" ).css( "color", "#"+hex );
	}

	if ( el.id == 'border-color' ) {
		$( "#ad-design-div" ).css( "border", "1px solid #"+hex );
	}


	},
	onBeforeShow: function () {
		$(this).ColorPickerSetColor( this.value );
	}
})
.bind('keyup', function(){
	$(this).ColorPickerSetColor( this.value );
});
//*** END COLOR PICKER ***//

function jhzEv(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

function url ( href ) {
	var url =	href;
	if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-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  return 'invalid';
	}
}


(function($) {
    $.fn.extend( {
        limiter: function(limit, elem) {
            $(this).on("keyup focus", function() {
                setCount(this, elem);
            });
            function setCount(src, elem) {
			
                var chars = src.value.length;
				
                if (chars > limit) {
                    src.value = src.value.substr(0, limit);
                    chars = limit;
                }
                elem.html( limit - chars );
            }
            setCount($(this)[0], elem);
        }
    });
})(jQuery);



jQuery(document).ready(function (e) {

	// ADMIN LOGIN
	$("#admin-login").on('click', function () {

		var	t	=	0;
		var	email		=	$("#email").val();
		var	password	=	$("#password").val();

		if ( password == "" ) {
			$("#password").css("border","1px solid #b94a48");t++;
				} else { 
			$("#password").css("border",""); }

		if ( email == "" ) {
			$("#email").css("border","1px solid #b94a48");t++;
				} else { 
			$("#email").css("border",""); }

			if( !jhzEv( email ) && email != '' ){
				$("#email").css("border","1px solid #b94a48");
				t = t + 1;
			}


if ( t == 0 ) {
				$("#admin-login").attr('disabled', true);
				$("#admin-loader").html('<img src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/load.gif"/>');


	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.login.php?t=auth', { email:email, password:password },
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').html();
			var msj		= $r.filter('#message').html();
			if ( mode == 1 ) {
					//** PAGE REDIRECT **//
				window.location = '<?php echo HTTP_SERVER_ADMIN;?>index.php';
			}
			if ( mode == 2 ) { 
				$("#admin-loader").html('<div class="alert alert-danger">'+msj+'</div>');
				$("#admin-login").removeAttr('disabled');
			}
		});	
	//END POST FUNCTION

			} else {
				$("#admin-login").removeAttr('disabled');
				$("#admin-loader").html('<div class="alert alert-danger"><?php echo $LANG["WBS_ADMIN_COMPLETE_ALL_MANDATORY_FIELDS"];?></div>');
			}


	});



	// LOG OUT
	$("#log-out").on('click', function () {
		$("#logged-in").html('<img class="logged-in-hd" src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/loader.gif"/>');

	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.login.php?t=out',
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').html();
			var msj		= $r.filter('#message').html();
			if ( mode == 1 ) { window.location = '<?php echo HTTP_SERVER_ADMIN;?>login.php'; }
			if ( mode == 2 ) { 
				$("#logged-in").html('<div class="alert alert-danger">'+msj+'</div>');
			}
		});	
	//END POST FUNCTION
	});



//*** MARK AS PAID ***//
	$("#mark-paid").on('click', function () {
	$("#mark-paid").attr('disabled', true);
	$("#mark-rejected").attr('disabled', true);
	$("#mark-loader").html('<img class="logged-in-hd" src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/loader.gif"/>');

		id	=	$(this).data("id");

	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.payments.php?t=approve', { id:id },
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').text();
			var msj		= $r.filter('#message').text();
			if ( mode == 1 ) {
				$("#mark-loader").html('<div class="alert alert-success">'+msj+'</div>');
			}
			if ( mode == 2 ) { 
				$("#mark-loader").html('<div class="alert alert-danger">'+msj+'</div>');
			}
		});	
	//END POST FUNCTION

	});
//*** END MARK AS PAID ***//


//*** MARK AS PAID REJECTED ***//
	$("#mark-rejected").on('click', function () {
	$("#mark-paid").attr('disabled', true);
	$("#mark-rejected").attr('disabled', true);

		$("#mark-loader").html('<img class="logged-in-hd" src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/loader.gif"/>');

		id	=	$(this).data("id");

	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.payments.php?t=reject', { id:id },
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').text();
			var msj		= $r.filter('#message').text();
			if ( mode == 1 ) {
				$("#mark-loader").html('<div class="alert alert-success">'+msj+'</div>');
			}
			if ( mode == 2 ) { 
				$("#mark-loader").html('<div class="alert alert-danger">'+msj+'</div>');
			}
		});	
	//END POST FUNCTION

	});
//*** END MARK AS REJECTED ***//



$(".start-ad").on('click', function () {
	m	=	this.id;
	id	=	$(this).data("id");
	var	mode	=	'start';

	$("#" + m ).attr('disabled', true);

	//** REMOVE ATTR DISABLED **//
	$("#stop-" + id ).removeAttr('disabled');

	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/advertiser.class.php?t=ad-status', { id:id,mode:mode });	
	//END POST FUNCTION

});


$(".stop-ad").on('click', function () {
	m			=	this.id;
	id			=	$(this).data("id");
	var	mode	=	'stop';
	$("#" + m ).attr('disabled', true);

	//** REMOVE ATTR DISABLED **//
	$("#start-" + id ).removeAttr('disabled');


	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/advertiser.class.php?t=ad-status', { id:id,mode:mode });	
	//END POST FUNCTION

});





$("#remove-reported-notification").on('click', function () {
	m			=	this.id;
	id			=	$(this).data("id");
	$("#" + m ).attr('disabled', true);

	//** REMOVE ATTR DISABLED **//
	$("#start-" + id ).removeAttr('disabled');

	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/advertiser.class.php?t=rmv-rpt', { id:id },
		function(data) {

			window.location = '<?php echo HTTP_SERVER_ADMIN;?>ads/details.php?ad='+id;
		});	
	//END POST FUNCTION


});






	// ADMIN UPDATE
	$("#admin-update").on('click', function () {

		var	t	=	0;
		var	name		=	$("#name").val();
		var	email		=	$("#email").val();
		var	password	=	$("#password").val();

		if ( name == "" ) {
			$("#name").css("border","1px solid #b94a48");t++;
				} else { 
			$("#name").css("border",""); }

		if ( email == "" ) {
			$("#email").css("border","1px solid #b94a48");t++;
				} else { 
			$("#email").css("border",""); }

			if( !jhzEv( email ) && email != '' ){
				$("#email").css("border","1px solid #b94a48");
				t = t + 1;
			}


if ( t == 0 ) {
				$("#admin-update").attr('disabled', true);
				$("#admin-loader").html('<img src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/load.gif"/>');


	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.class.php?t=update', { name:name, email:email, password:password },
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').html();
			var msj		= $r.filter('#message').html();
			if ( mode == 1 ) {
				$("#admin-loader").html('<div class="alert alert-success">'+msj+'</div>');
				$("#admin-update").removeAttr('disabled');
				$("#password").val("");
			}
			if ( mode == 2 ) { 
				$("#admin-loader").html('<div class="alert alert-danger">'+msj+'</div>');
				$("#admin-update").removeAttr('disabled');
			}
		});	
	//END POST FUNCTION

			} else {
				$("#admin-update").removeAttr('disabled');
				$("#admin-loader").html('<div class="alert alert-danger"><?php echo $LANG["WBS_ADMIN_COMPLETE_ALL_MANDATORY_FIELDS"];?></div>');
			}


	});



	$("#give-bonus").on('click', function () {
		var	t	=	0;
		var	details	=	$( "#bonus-description" ).val();
		var	bonus	=	$( "#bonus" ).val();
		var	user	=	$( "#users-list" ).val();	

		if ( details == "" ) {
			$("#bonus-description").css("border","1px solid #b94a48");t++;
				} else { 
			$("#bonus-description").css("border",""); }

		if ( bonus == "" ) {
			$("#bonus").css("border","1px solid #b94a48");t++;
				} else { 
			$("#bonus").css("border",""); }

		if ( user == "0" ) {
			$("#users-list").css("border","1px solid #b94a48");t++;
				} else { 
			$("#users-list").css("border",""); }

		if($('#get-money').is(':checked')) {
			bonus	=	-Math.abs( bonus );
		}


if ( t == 0 ) {
				$("#give-bonus").attr('disabled', true);
				$("#bonus-loader").html('<img src="<?php echo HTTP_SERVER_WEBSITE ?>templates/<?php echo ADMIN_TEMPLATE;?>/assets/images/load.gif"/>');


	//POST FUNCTION
	$.post('<?php echo HTTP_SERVER_SYSTEM;?>admin/admin.bonus.class.php?t=give', { bonus:bonus, user:user, details:details },
		function(data) {

			var $r=$(data);
			var mode	= $r.filter('#mode').html();
			var msj		= $r.filter('#message').html();
			if ( mode == 1 ) {
				$("#bonus-loader").html('<div class="alert alert-success">'+msj+'</div>');
				$("#give-bonus").removeAttr('disabled');
				$("#bonus-description").val("");
				$("#bonus").val("");
				$("#users-list").val("0");
			}
			if ( mode == 2 ) { 
				$("#bonus-loader").html('<div class="alert alert-danger">'+msj+'</div>');
				$("#give-bonus").removeAttr('disabled');
			}
		});	
	//END POST FUNCTION

			} else {
				$("#give-bonus").removeAttr('disabled');
				$("#bonus-loader").html('<div class="alert alert-danger"><?php echo $LANG["WBS_ADMIN_COMPLETE_ALL_MANDATORY_FIELDS"];?></div>');
			}



	});














});