// JavaScript Document
var PdsRequestUI = {
	_id : 0,
	_currentMember: null,
	_currentPdsId: 0,
	isDownload: false,
	memberUrl: '',
	trackUrl: '',
	linksUrl: '',
	defaultType: '',
	orderListUrl: '',
	addToListUrl: '',
	removeFromListUrl: '',
	downloadUrl: '',
	addressUrl: '',
	sendMailUrl: '',
	sendMail: function(id)
	{
		$U.showProgress();
		
		$.post(PdsRequestUI.sendMailUrl, {'id': PdsRequestUI._currentPdsId},
			   function(response)
			   {
				   $U.hideProgress();
					$('#pds_mail_confirm').slideUp('fast', 
						function()
						{
							$('#result_mail_send').slideDown('fast');
						});
			   });
	},
	confirmMailPds: function(id)
	{	
		$U.showProgress();
		PdsRequestUI._currentPdsId = id;
		$.post(PdsRequestUI.addressUrl, {},
			   function(record)
			   {
				   $U.hideProgress();
				   
				   $('#spnAddress').html(record.street);
				   $('#spnSuburb').html(record.suburb);
				   $('#spnState').html(record.state);
				   $('#spnPostCode').html(record.postCode);
				   
				   $('#pds_download').slideUp('fast',
											  function()
											  {
												  $('#pds_mail_confirm').slideDown('fast');
											  });
				   
			   },'json');
	},
	downloadPDS: function(id)
	{
		$U.showProgress();
		
		$.post(PdsRequestUI.downloadUrl, {'id': id},
			   function(html)
			   {
				   $U.hideProgress();
				   $('#list_pds').html(html);
				   
				   $('#pds_download').slideUp('fast',
								   function()
								   {
									   $('#pds_download_confirm').slideDown('fast');
								   });
			   });
	},
	cancelDownload: function()
	{
		$('#pds_mail_confirm').slideUp('fast',
			function()
			{
				$('#pds_download_confirm').slideUp('fast',
								   function()
								   {
									   $('#pds_download').slideDown('fast');
								   });
			});
		
	},
	addToList: function(id)
	{
		$U.showProgress();
		$.post(PdsRequestUI.addToListUrl, {'id' : id},
			   function(response)
			   {
				   if(response.succeed)
				   {
					   $('#result').hide();
					   $('#result').slideDown('fast',
						function()
						{
							$.scrollTo({ top:400, left:0}, 500);
						});
				   }
				   else
				   {
					   alert(response.message);
				   }
				   $U.hideProgress();
			   },'json');
	},
	removeFromList: function(id)
	{
		$U.showProgress();
		$.post(PdsRequestUI.removeFromListUrl, {'id' : id},
			   function(response)
			   {
				   if(response.succeed)
				   {
					   PdsRequestUI.loadOrderList();
				   }
				   else
				   {
					   alert(response.message);
				   }
				   $U.hideProgress();
			   },'json');
	},
	loadOrderList: function()
	{
		$.post(PdsRequestUI.orderListUrl, {},
			   function(html)
			   {
				   $('#pds_download').html(html);
				   $U.hideProgress();
			   })
	},
	onPdsDownloadClick: function(isDownload, id)
	{
		PdsRequestUI.isDownload = isDownload;
		PdsRequestUI._id = id;
		$('#step_two,#step_final, #request_message, #step_links').hide();
		$('#step_one').show();
		
		$U.showProgress();
		
		$.post(PdsRequestUI.memberUrl, {}, function(record)
		{
			PdsRequestUI._currentMember = record;
			$U.checked('chkAccept', false);
			$('#txtToEmail, #txtAddress').val('');
			if(record.id > 0)
			{
				$('#txtName').val(record.fullName).attr('readonly', true);
				$('#txtEmail').val(record.email).attr('readonly', true);
			}
			else
			{
				$('#txtName').val('').attr('readonly', false);
				$('#txtEmail').val('').attr('readonly', false);
			}
			
			$U.hideProgress();
			
			$U.showModal('pds_request', {'width':520});
		},'json');
		
	},
	onContinueClick: function()
	{
		var val = new Validator();
		
		val.checkData('txtName', 'Name is required. Please enter your name.');
		
		val.checkData('txtEmail', 'Email is required. Please enter an email address.');
		
		val.checkEmail('txtEmail', 'Email is invalid. Please enter a valid email address.');
		
		if(!$U.checked('chkAccept'))
		{
			val.addError('chkAccept', 'Please tick to confirm acceptance of the iRefund Financial Services Guide and Terms.');
		}
		
		if(!val.isValid())
		{
			val.report('request_message', 'error');
			val.putFocus();
			return false;
		}
			
		
		$('#request_message').fadeOut('fast');
		
		if(!PdsRequestUI.isDownload)
		{
			$U.checked('optEmail', true);
			
			$U.text('txtToEmail', $U.text('txtEmail'));
			
			$('#step_one').fadeOut('fast', function()
			{
				$('#step_two').fadeIn('fast');
			});
		}
		else
		{
			$U.showProgress();
			
			$U.disabled('btnDownload', true);
			$U.disabled('btnCancel', true);
			
			
			// save data
			$.post(PdsRequestUI.trackUrl, 
		   {'name' : $('#txtName').val(), 
		   'email' : $('#txtEmail').val(), 
		   'toEmail' : $('#txtToEmail').val(),
		   'address': $('#txtAddress').val(),
		   'type': PdsRequestUI.defaultType,
		   'pdsId': PdsRequestUI._id}, 
		   function(response)
			{
				if(response.succeed)
				{
					$.post(PdsRequestUI.linksUrl, {'pdsId' : PdsRequestUI._id}, function(response)
					{
						$U.hideProgress();
						
						$U.disabled('btnDownload', false);
						$U.disabled('btnCancel', false);
						
						$('#download_links').html(response);
						
						$('#step_one').fadeOut('fast', function()
						{
							$('#step_links').fadeIn('fast');
						});
					});	
				}
				else
				{
					$U.hideProgress();
						
					$U.disabled('btnDownload', false);
					$U.disabled('btnCancel', false);
						
					$('#request_message').html(response.message).addClass('error').fadeIn('fast');
				}
			},'json');
			
		}
	},
	onSubmitClick: function()
	{
		var val = new Validator();
		
		if($U.checked('optEmail'))
		{
			val.checkData('txtToEmail', 'Email is required. Please enter an email address.');
		}
		
		if($U.checked('optPost'))
		{
			val.checkData('txtAddress', 'Postal Address is required. Please enter your postal address.');
		}
		
		if(!val.isValid())
		{
			val.report('request_message', 'error');
			val.putFocus();
			return;
		}
		
		$('#request_message').fadeOut('fast');
		
		$U.showProgress();
		$U.disabled('btnRequest', true);
		$U.disabled('btnCancelRequest', true);
		
		var typeVal = $('#optEmail').val();
		if($U.checked('optPost'))
		{
			typeVal = $('#optPost').val();
		}
		
		// save data
		$.post(PdsRequestUI.trackUrl, 
	   {'name' : $('#txtName').val(), 
	   'email' : $('#txtEmail').val(), 
	   'toEmail' : $('#txtToEmail').val(),
	   'address': $('#txtAddress').val(),
	   'type': typeVal,
	   'pdsId': PdsRequestUI._id}, 
	   function(response)
		{
			if(response.succeed)
			{
				$U.hideProgress();
				$U.disabled('btnRequest', false);
				$U.disabled('btnCancelRequest', false);
				
				$('#step_two').fadeOut('fast', function()
				{
					$('#step_final').fadeIn('fast');
				});	
			}
			else
			{
				$U.disabled('btnRequest', false);
				$U.disabled('btnCancelRequest', false);
					
				$('#request_message').html(response.message).addClass('error').fadeIn('fast');
			}
		},'json');
	},
	onCancel: function()
	{
		$U.hideModal('pds_request');
	},
	init: function()
	{
		$('#step_one, #step_two, #step_final, #step_links').css('background-color','#FFF');
	}
}