var abc = 4;

$(document).ready(function() {

//To add new input file field dynamically, on click of "Add More Files" button below function will be executed
    $('#add_more').click(function() {
        $(this).before($("<div/>", {id: 'filediv'}).fadeIn('slow').append(
                $("<input/>", {name: 'file[]', type: 'file', id: 'file'}),        
                $("<br/><br/>")
                ));
    });

//following function will executes on change event of file input to select different file	
$('body').on('change', '#file', function(){
            if (this.files && this.files[0]) {
                 abc += 1; //increementing global variable by 1
				
				var z = abc - 1;
                var x = $(this).parent().find('#previewimg' + z).remove();
                $(this).before("<div id='abcd"+ abc +"' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
               
			    var reader = new FileReader();
                reader.onload = imageIsLoaded;
                reader.readAsDataURL(this.files[0]);
               
			    $(this).hide();
                $("#abcd"+ abc).append($("<img/>", {id: 'img', src: 'x.png', alt: 'delete'}).click(function() {
                $(this).parent().parent().remove();
                }));
            }
        });

//To preview image     
    function imageIsLoaded(e) {
        $('#previewimg' + abc).attr('src', e.target.result);
    };

    $('#upload').click(function(e) {
        var name = $(":file").val();
        if (!name)
        {
            alert("First Image Must Be Selected");
            e.preventDefault();
        }
    });
});



function deleteF(id) {

    var dataString = 'did=' +id ;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/panel_submit.php',
        success: function (data) {
            window.location.reload();
        },
        error: function (request, status, error) {
        }
    });
}

function deleteFF(id) {
    var dataString = 'uid='+id;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/access.php',
        success: function (data) {
            window.location.reload();
        },
        error: function (request, status, error) {
        }
    });
}

function accessVal()
{
    var uname=document.getElementById('uname').value;
    var uid=document.getElementById('uid').value;
    var upwd=document.getElementById('upwd').value;
    var dataString ="uname="+uname+"&uid="+uid+"&upwd="+upwd;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/access.php',
        success: function (data) {
            window.location.reload();
        },
        error: function (request, status, error) {
        }
    });
}

function getmail(id,status) {
    var dataString='mail='+id+'&mstatus='+status;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/show.php',
        success: function (data) {
            alert('Please check your mail!!')
            window.location.reload();
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}

function submitVal() {

    var name=document.getElementById('pname').value;
    var hd=document.getElementById('hd').value;
    var fd=document.getElementById('fd').value;
    var dt=document.getElementById('dt').value;
    var dataString = 'pname=' +name+'&fd='+fd+'&hd='+hd+'&dt='+dt ;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/panel_submit.php',
        success: function (data) {

            window.location.reload();
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}

function editF(id) {
    var headerval="";
    var footerval="";
    var panelval="";
    var domainval="";
    headerval=document.getElementById('head'+id).value;
    footerval=document.getElementById('foot'+id).value;
    panelval=document.getElementById('pninp'+id).value;
    domainval=document.getElementById('dtinp'+id).value;
    var dataString = 'uid=up'+id+'&hd='+headerval+'&fd='+footerval+'&pn='+panelval+'&dn='+domainval;
    $.ajax({
        type: 'POST',
        data: dataString,
        cache: false,
        url: '/panel_submit.php',
        success: function (data) {

            window.location.reload();
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });

}




function revenueType() {
	if(document.getElementById("OfferRevenueType").value=='cpa_flat')
	{
		document.getElementById("rev1").style.display = "block";
		document.getElementById("prsale").disabled = true;
		document.getElementById("rpclk").disabled = true;
		document.getElementById("rpthousand").disabled = true;
		document.getElementById("rpconver").disabled = false;
		document.getElementById("rev2").style.display = "none";
		document.getElementById("rev3").style.display = "none";
		document.getElementById("rev4").style.display = "none";
	}
	else if(document.getElementById("OfferRevenueType").value=='cpa_percentage')
	{
		document.getElementById("rev2").style.display = "block";
		document.getElementById("rpconver").disabled = true;
		document.getElementById("rpclk").disabled = true;
		document.getElementById("rpthousand").disabled = true;
		document.getElementById("prsale").disabled = false;
		document.getElementById("rev1").style.display = "none";
		document.getElementById("rev3").style.display = "none";
		document.getElementById("rev4").style.display = "none";
	}
	else if(document.getElementById("OfferRevenueType").value=='cpa_both')
	{
		document.getElementById("rev1").style.display = "block";
		document.getElementById("rev2").style.display = "block";
		document.getElementById("prsale").disabled = false;
		document.getElementById("rpconver").disabled = false;
		document.getElementById("rpclk").disabled = true;
		document.getElementById("rpthousand").disabled = true;
		document.getElementById("rev3").style.display = "none";
		document.getElementById("rev4").style.display = "none";
	}
	else if(document.getElementById("OfferRevenueType").value=='cpc')
	{
		document.getElementById("rev3").style.display = "block";
		document.getElementById("rpclk").disabled = false;
		document.getElementById("prsale").disabled = true;
		document.getElementById("rpconver").disabled = true;
		document.getElementById("rpthousand").disabled = true;
		document.getElementById("rev2").style.display = "none";
		document.getElementById("rev4").style.display = "none";
		document.getElementById("rev1").style.display = "none";
	}
	else if(document.getElementById("OfferRevenueType").value=='cpm')
	{
		document.getElementById("rev4").style.display = "block";
		document.getElementById("prsale").disabled = true;
		document.getElementById("rpconver").disabled = true;
		document.getElementById("rpclk").disabled = true;
		document.getElementById("rpthousand").disabled = false;
		document.getElementById("rev2").style.display = "none";
		document.getElementById("rev3").style.display = "none";
		document.getElementById("rev1").style.display = "none";
	}
}
	function payoutType() 
	{
		
	if(document.getElementById("OfferPayoutType").value=='cpa_flat')
	{
		document.getElementById("pay1").style.display = "block";
		document.getElementById("pay2").style.display = "none";
		document.getElementById("pay3").style.display = "none";
		document.getElementById("pay4").style.display = "none";
		document.getElementById("pay-pcs").disabled = true;
		document.getElementById("pay-click").disabled = true;
		document.getElementById("pay-thousand").disabled = true;
		document.getElementById("pay-con").disabled = false;
	}
	else if(document.getElementById("OfferPayoutType").value=='cpa_percentage')
	{
		document.getElementById("pay2").style.display = "block";
		document.getElementById("pay1").style.display = "none";
		document.getElementById("pay3").style.display = "none";
		document.getElementById("pay4").style.display = "none";
		document.getElementById("pay-pcs").disabled = false;
		document.getElementById("pay-click").disabled = true;
		document.getElementById("pay-thousand").disabled = true;
		document.getElementById("pay-con").disabled = true;
	}
	else if(document.getElementById("OfferPayoutType").value=='cpa_both')
	{
		document.getElementById("pay1").style.display = "block";
		document.getElementById("pay2").style.display = "block";
		document.getElementById("pay3").style.display = "none";
		document.getElementById("pay4").style.display = "none";
		document.getElementById("pay-pcs").disabled = false;
		document.getElementById("pay-click").disabled = true;
		document.getElementById("pay-thousand").disabled = true;
		document.getElementById("pay-con").disabled = false;
	}
	else if(document.getElementById("OfferPayoutType").value=='cpc')
	{
		document.getElementById("pay3").style.display = "block";
		document.getElementById("pay2").style.display = "none";
		document.getElementById("pay4").style.display = "none";
		document.getElementById("pay1").style.display = "none";
		document.getElementById("pay-pcs").disabled = true;
		document.getElementById("pay-click").disabled = false;
		document.getElementById("pay-thousand").disabled = true;
		document.getElementById("pay-con").disabled = true;
	}
	else if(document.getElementById("OfferPayoutType").value=='cpm')
	{
		document.getElementById("pay4").style.display = "block";
		document.getElementById("pay2").style.display = "none";
		document.getElementById("pay3").style.display = "none";
		document.getElementById("pay1").style.display = "none";
		document.getElementById("pay-pcs").disabled = true;
		document.getElementById("pay-click").disabled = true;
		document.getElementById("pay-thousand").disabled = false;
		document.getElementById("pay-con").disabled = true;
	}
}
$(document).ready(function(){
	$('#OfferStatus').click(function(){
		if(typeof window['Intercom'] !== "undefined"){
			Intercom('trackEvent','offer-status-made-active');
		}
	});

	 var select2Inputs = $('input.s2');

    // Set up select2 inputs for Offer and Affiliate
    select2Inputs.each(function() {
        var select2Input = $(this);
        // Grab the filter name (Offers, Affiliates) that this input collects
        var filterName = select2Input.data('filtername');

        // select2 formatter for results and selections.
        var filterOptionsFormatter = function(optionRow) {
            return _.escape(optionRow.text) + ' (ID: ' + optionRow.id + ')';
        };

        var searchString = '';
        var filterField = 'status';
        var filterValue = 'active';

        // Initialize the select2 multiselect for these inputs
        select2Input.select2({
            closeOnSelect: false,
            minimumInputLength: 0,
            multiple: true,
            formatResult: filterOptionsFormatter,
            formatSelection: filterOptionsFormatter,
            ajax: {
                url: '/offers/ajax_get_filter_options/' + filterName,
                dataType: 'json',
                data: function (term, page) {
                    searchString = term;
                    return {
                        filterField: filterField,
                        filterValue: filterValue,
                        searchString: term,
                        page: page
                    };
                },
                quietMillis: 400,
                results: function (data, page) {
                    return {
                        results: data,
                        more: data.length >= 50
                    };
                }
            }
        });

        // For edit page, fill dropdown with existing categories
        var postedFilterValues = select2Input[0].value.split(',');

        if (postedFilterValues.length > 0 && postedFilterValues[0] != "" ) {
            $.ajax({
                url: '/offers/ajax_get_filter_options/' + filterName,
                data: {
                    filterField : 'id',
                    filterLimit: postedFilterValues.length,
                    filterValue : postedFilterValues
                },
                dataType: "json"
            }).done(function(response) {
                // Update default selection to include previously posted filter values
                select2Input.select2('data', response);
            });
        }
    });
});


