$(document).ready(function()
{
    razmessage();
	
	//Combo Sponsor : Change 
	$('#cbx_sponsor').change(function()
	{
		var	id_selected_sponsor	=	$('#cbx_sponsor').val();
		processing_show();
		if(id_selected_sponsor > 0)
		{
			 $.ajax
			 ({
					url: 'scripts.php?action=get_offers',
					type: 'get',
					data: 
					{
						id_sponsor: id_selected_sponsor
					},
					success: function(data) 
					{
						$('#cbx_offer').empty();
						$('#cbx_offer').append(data);
						processing_hide();
					},
					error: function() 
					{
						$("#message-error").slideDown("slow");
						setTimeout(function(){close_message_error();},3000);
						processing_hide();
					}
		
				}); 
		}
		else
		{
			$('#cbx_offer').empty();
			processing_hide();
		}
	});
	
	//Input File Suppression File : Change
	$('#txt_suppression_file').change(function()
	{
		if(validate_input_type_file('txt_suppression_file','txt') == false)
		{
			$("#message-custom").text("Only suppression files with '.txt' exstension are accpted !");
			$("#message-warning-custom").slideDown("slow");
			setTimeout(function(){close_message_warning_custom();},3000);
		}
	});
		
	/*
	$('#btn_upload_suppression_file').click(function()
	{
			alert('clicked');
	});
	*/
	
	
	$('#frm_upload_suppression_file').on('submit',function(e) 
	{
		e.preventDefault();
		processing_show();	
		if(check_data())
		{
			var form_data = new FormData(this);                  
			$.ajax
			({
				url: "./upload.php",
				type: "POST",             
				data: form_data,
				contentType: false,       
				cache: false,             
				processData:false,        
				success: function(data)   
				{
					processing_hide();	
					alert(data);
				},
				error: function(error)
				{
					processing_hide();	
					alert(error);
				}
			});
		}
		else
		{
			processing_hide();	
			$("#message-custom").text("You have to fill all necessary fields with correct values !");
            $("#message-warning-custom").slideDown("slow");
            setTimeout(function(){close_message_warning_custom();},3000);
		}
	});
});

function razmessage()
{
  $("#message-warning").hide();
  $("#message-warning-custom").hide();
  $("#message-error").hide();
  $("#message-success").hide();
}

function close_message_warning()
{
    $("#message-warning").slideUp("slow");
}

function close_message_warning_custom()
{
    $("#message-warning-custom").slideUp("slow");
}

function close_message_error()
{
    $("#message-error").slideUp("slow");
}

function close_message_success()
{
    $("#message-success").slideUp("slow");
}

function processing_show()
{
    $("#processing").html("<div class='progress progress-info progress-striped active'><div class='bar' style='width: 100%;'>&nbsp;&nbsp;Processing...&nbsp;&nbsp;</div></div>");
    $("#processing").show();
}

function processing_hide()
{
    $("#processing").hide();
}

function check_data()
{
   var	id_selected_sponsor					=	$('#cbx_sponsor').val();
   var	id_selected_offer					=	$('#cbx_offer').val();
   var	id_selected_suppression_file_type	=	$('#cbx_suppression_type').val();
   var	isTxtFileSelected					=	validate_input_type_file('txt_suppression_file','txt');
   
   if(id_selected_sponsor && id_selected_offer && id_selected_suppression_file_type && isTxtFileSelected)
		return true;
   else
		return false;
}


function validate_input_type_file(p_inputTypeFile,p_acceprtedExtension)
{
	var	result	=	false;
	
	var val 					= 	$('#'+p_inputTypeFile).val();
	var uploadedFileExtension	=	val.substring(val.lastIndexOf('.') + 1).toLowerCase();
	switch(uploadedFileExtension)
	{
		case p_acceprtedExtension: 
			result	=	true;
			break;
		
		default:
			$('#'+p_inputTypeFile).val('');
			result	=	false;
			break;
	}
	
	return result;
}


