var file_hash = '';
var status_ajax = '';
var status_intr = '';
var response = '';

$(document).ready(function() 
{
	$("#stop").click(function()
	{
		stop_request();
	});
	
	$("#upload").click(function()
	{
		upload_file();
	});
	
	$(".top").click(function()
	{
		window.scroll(0,0);
	});
	
	$(".print_report").click(function()
	{
		window.print();
	});
	
	$(".download_report").click(function()
	{
		//window.print();
	});	
	
});

function stop_request()
{
	clearInterval(status_intr);
}

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(obj, msg, opt, msg_id)
{
	$("#"+ msg_id).html(msg);
	if(opt == true)
	{
		$("#" + obj).show("fast");
	}
	else if(opt == false)
	{
		$("#"+ msg_id).html("");
		$("#" + obj).hide("fast");
	}
}

function show_hide_infmsg(msg, opt)
{
	show_hide_msg("inf_msg_wrap", msg, opt, "inf_msg");	
}

function show_hide_errormsg(error_msg, opt)
{
	show_hide_msg("error_msg_wrap", error_msg, opt, "error_msg");
}

function show_hide_statusmsg(status_msg, opt)
{	
	show_hide_msg("status_msg_wrap", status_msg, opt, "status_msg");
}

function persistent_check_status()
{
	status_intr = setInterval('check_status()', 10000);
}

function check_status()
{
	show_hide_statusmsg("Checking file status...", true);
	if(response == '' && file_hash != '')
	{
		response = $.ajax(
		{
			type: "get",
	        url: "file_report.php",
	        data: "h=" + file_hash,
	        dataType: "json",
	        error: function(request, msg)
			{
				alert("Server timeout: " + msg);
	        },
	        success: function(data)
	       	{
	       		show_hide_infmsg("", false);
	       		if(data.error_msg != "")
	       		{
	       			stop_request();
	       			show_hide_statusmsg("", false);
	       			show_hide_infmsg("", false);
	        		show_hide_errormsg(data.error_msg, true);
	        	}
	        	else
	        	{
	        		show_hide_statusmsg(data.status_msg, true);
	        	}
			}
		});
		response = '';
	}
}

function upload_file()
{
	var msg = "";
	
	if($("#file").val() == "")
	{
		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_errormsg('<div style="color:red;font-weight:bold">Please correct the following:</div>' + msg,true);
		return false;
	}
	else
	{
		show_hide_errormsg("",false);
	}

	/*$("#upload_progress_wrap")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});*/	
	/*$("#upload_progress_wrap").show();
	$.ajaxFileUpload
	(
		{
			url:'file_report.php',
			secureuri:false,
			fileElementId:'file',
			dataType: 'json',
			beforeSend:function()
			{
				//$("#upload_progress_wrap").show();
			},
			complete:function()
			{
				//$("#upload_progress_wrap").hide();
			},				
			success: function (data, status)
			{
				$("#file").val("");
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error_msg);
					}
					else
					{
						show_hide_infmsg(data.msg, true);						
						if(data.file_hash != '')
						{
							file_hash = data.file_hash;
							persistent_check_status();
						}
						
						if(data.status_msg != '')
						{
							show_hide_statusmsg(data.status_msg +"-" +data.file_name, true);
						}
					}
				}
				$("#upload_progress_wrap").hide();
			},
			error: function (data, status, e)
			{
				alert("Server Error!");
				$("#upload_progress_wrap").hide();
			}
		}
	)*/
	
	$("#form").submit();
	return false;
}
