$(document).ready(function() 
{
	$("#add_more").click(function()
	{
		add_more_files();	
	});
	
	$("#upload").click(function()
	{
		upload_file();
	});
	
	// Selects one or more elements to assign a simpletip to 
	$("#malware_name_area a").simpletip({ content: '<img src="style/images/AV_alert.jpg" />',fixed:true, position: 'bottom' });
	
	$("#false_positive").click(function()
	{
		if($(this).attr("checked") == true)
		{
			$("#malware_name").val("").attr("disabled","");
			$("#malware_name_area").show();
		}
	});
	
	$("#suspicious_malware").click(function()
	{
		if($(this).attr("checked") == true)
		{
			$("#malware_name_area").hide();
			$("#malware_name").val("").attr("disabled","disabled");
		}
	});
	
	if($("#false_positive").attr("selected") == true)
	{
		$("#false_positive").trigger("click");
	}
});

function validate_email(email_id)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test($("#" + email_id).val()))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function show_hide_msg(msg, opt, msg_type)
{
	if(opt == true)
	{
		$("#msg_wrap").fadeOut("fast");
		$("#msg").html(msg);
		$("#msg_icon").removeClass();		
		if(msg_type == "info")
		{
			$("#msg_icon").addClass("correct_msg");
		}
		else if(msg_type == "error")
		{
			$("#msg_icon").addClass("error_msg");
		}
		else if(msg_type == "loading")
		{
			$("#msg_icon").addClass("loading");
		}
		$("#msg_wrap").fadeIn("fast");
	}
	else
	{
		$("#msg_wrap").fadeOut("fast");
		$("#msg").html("");
		$("#msg_icon").removeClass();
	}
}

function add_more_files()
{
	var files = $("#file_upload input[type='file']").length;
	if(files < 5)
	{
		var rand = (Math.floor(Math.random() * 100 + 1)) + (new Date().getTime());
		$('<span id="file_area' + rand +'"><br clear="all" /><input type="file" name="file[]" id="file' + rand + '" size="50" /><a id="' + rand + '" href="javascript:remove_files(\'file_area' + rand + '\');" class="remove_files" title="Remove File">X</a>&nbsp;</span>').insertBefore("#add_more");
	}
	else
	{
		alert("File(s) limit reached for this session. Please complete this upload(s) to continue");	
	}
}

function remove_files(id)
{
	if($("#file_upload input[type='file']").length > 1)
	{
		$("#" + id).remove();
	}
}

function upload_again()
{
	show_hide_msg("", false);
	$("#uploaded_msg").hide().html("");
	$("#top_content").show();
	clear_form($("#file_upload"));
	$("#form_area").show();
}

function clear_form(form)
{
	$(':input', form).each(function()
	{
		var type = this.type;
		var tag = this.tagName.toLowerCase();
		if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'file') this.value = "";
		else if (type == 'checkbox' || type == 'radio') this.checked = false;
		else if (tag == 'select') this.selectedIndex = -1;
	});

	$("#malware_name_area").hide();
	$("#malware_name").val("").attr("disabled","disabled");
}

function upload_file()
{
	var msg = "";
	var file_error_msg = true;
	
	if(($("#suspicious_malware").attr("checked")) == false && ($("#false_positive").attr("checked")) == false)
	{
		temp_msg = "Please select file type";
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;
	}
	
	if($("#false_positive").attr("checked") == true && $.trim($("#malware_name").val()) == "")
	{
		temp_msg = "Please enter Malware Name";
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;
	}
	
	if($("input[type='file']").length <= 0)
	{
		temp_msg = "Please select a file to upload";
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;		
	}
	else
	{
		$("input[type='file']").each(function()
		{
			if($(this).val() != '')
			{
				file_error_msg = false;
			}
		});
		
		if(file_error_msg == true)
		{
			temp_msg = "Please select a file to upload";
			msg += (msg == "") ? temp_msg : "<br>" + temp_msg;			
		}		
	}
	
	if($("#email").val() == "")
	{
		temp_msg = "Please enter Email ID";
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;
	}
	else if(!(validate_email("email")))
	{
		temp_msg = "Please enter valid Email ID"
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;
	}
	
	if(($("#agree").attr("checked")) == false)
	{
		temp_msg = "Please read and agree the Terms and Conditions";
		msg += (msg == "") ? temp_msg : "<br>" + temp_msg;
	}
	
	if(msg != "")
	{
		show_hide_msg('<div class="text_red text_bold">Please correct the following:</div><br />' + msg, true, "error");
		return false;
	}
	else
	{
		show_hide_msg("",false);
	}

	show_hide_msg('File(s) upload is in progress.<br />This will take a while depending on the file size' + msg, true, "loading");
	$("#form_area").hide();
	$("#top_content").hide();
	$.ajaxFileUpload
	(
		{
			url:'file_upload.php?rnd=' + new Date().getTime(),
			secureuri:false,
			fileElementId:'file',
			dataType: 'json',
			beforeSend:function()
			{
				//$("#upload_progress_wrap").show();
			},
			complete:function()
			{
				//$("#upload_progress_wrap").hide();
			},				
			success: function(data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error == 'true')
					{
						alert(data.error_msg);
						show_hide_msg("", false);
						upload_again();
					}
					else
					{
						if(data.msg == '')
						{
							$("#top_content").hide();
							$("#uploaded_msg").html('<p class="text_bold">Thank-you!</p><p>Your File was uploaded successfully and will shortly undergo analysis by Comodo technicians.  Your valuable contribution will help improve the effectiveness of Comodo products and so help us in our mission to provide the very highest levels of security to users worldwide.</p><p>&nbsp;</p><p>If you would like to submit more files then <a href="javascript:upload_again();">click here</a> to return to the submission form.</p>').show();
							show_hide_msg("", false);							
						}
					}
				}
			},
			error: function (data, status, e)
			{
				alert("Server Error! Please try again");
				show_hide_msg("", false);
				$("#form_area").show();
				$("#top_content").show();
			}
		}
	)
	
	return false;
}
