var file_elements = 1;

$(document).ready(function() 
{
	$("#upload").hover(function() { $(this).attr("src", "style/images/submit_on.gif");  },function() { $(this).attr("src", "style/images/submit_default.gif"); });
	
	$("#upload").click(function()
	{
		upload_file();
	});
	
	$("#add_more").click(function()
	{
		add_more_files();
	});
	
});

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()
{
	if(file_elements < 5)
	{
		file_elements++;
		$('<br clear="all" /><input type="file" name="file[]" id="file' + file_elements + '" size="50" />').insertBefore("#add_more");
	}
	else
	{
		alert("File(s) limit reached for this session. Please complete this upload(s) to continue");	
	}
}

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;
	});
}

function upload_file()
{
	var msg = "";
	var file_error_msg = true;
	
	$("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>' + 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 CCSS Forum.</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);							
						}
					}
				}
				//clear_form($("#file_upload"));
				//$("#form_area").show();
			},
			error: function (data, status, e)
			{
				alert("Server Error! Please try again");
				show_hide_msg("", false);
				$("#form_area").show();
				$("#top_content").show();
			}
		}
	);
	
	return false;
}
