{% load i18n l10n %}

{% get_current_language as LANGUAGE_CODE %}


<div id="mail_schedule" class="mail" style="height:80px">
	<h2>{% trans 'Schedule mail' %}</h2>
		<div id="cdr_radiom" class="radio_mail">
			<form id="cdr_radiomform" method = "POST" action="" > {% csrf_token %}	
			<span style="font-size:1em">{% trans 'Send CDRs every' %}&nbsp; </span>	
				<input type="radio" id="cdr_radiom1" name="datem_cdr_radio" value="day" {% if period == "day" %}checked="checked"{% endif %}>
				<label for="cdr_radiom1">{% trans 'Day' %}</label>
				<input type="radio" id="cdr_radiom2" name="datem_cdr_radio" value="week"  {% if period == "week" %}checked="checked"{% endif %}>
				<label for="cdr_radiom2">{% trans 'Week' %}</label>
				<input type="radio" id="cdr_radiom3" name="datem_cdr_radio" value="fifteen"  {% if period == "fifteen" %}checked="checked"{% endif %}>
				<label for="cdr_radiom3">{% trans '15 days' %}</label>			
				<input type="radio" id="cdr_radiom4" name="datem_cdr_radio" value="month"  {% if period == "month" %}checked="checked"{% endif %}>
				<label for="cdr_radiom4">{% trans 'Month' %}</label>
				<input type="radio" id="cdr_radiom5" name="datem_cdr_radio" value="never"  {% if period == "never" %}checked="checked"{% endif %}>
			<label for="cdr_radiom5">{% trans 'Do not send CDRs' %}</label>
			</form>
	</div>

	<div id="cdr_button_save" class="button_mail">
		&nbsp;{% trans 'to email address' %}&nbsp; <input id="email" type="text" size="40" value="{{email}}">
		<span id="validEmail">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
		<a id="save_mail_settings" href="#">{% trans 'Save settings' %}</a>
	</div>
	<div id="disclaimer" class='text_mail'>
		{% trans ' * The content of scheduled mails is provided for informative purposes only and cannot be used for billing' %}
	</div>
</div>



<script type="text/javascript" lang="text/javascript">


	{% block documentready %}


		$("#save_mail_settings").click(function(){saveSubctiptionSettings()});
		$("#email").keyup(function(){
			var email = $("#email").val();
			if(email != 0){
				if(isValidEmailAddress(email) == true){
					$("#validEmail").css({ "background-image": "url('{{ STATIC_URL }}img/success_small.png')" });
				} 
				else {
					$("#validEmail").css({ "background-image": "url('{{ STATIC_URL }}img/error_small.png')" });
				}
			} 
			else {
				$("#validEmail").css({ "background-image": "none" });
			}
		});
		
	{% endblock %}


		
	function saveSubctiptionSettings(){
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		var ok = false;
		$.ajax({
							  type: 'POST',
							  url: '{{URL_MAIL}}',
							  data:  { email: $("#email").val(), period: $("input[name='datem_cdr_radio']:checked").val()  },
							  success: function(data) {
							  			//showDialog('dialog-info')
										if(data.indexOf('OK') != -1) 
										{
											//showDialog('dialog-info');
											createMessagesLocal('cdr_result_messages',"{% trans 'The new settings have been succesfully applied.An email has been sent to the specified mail account, check your inbox to confirm .' %}", 'success', 3000);		
										}
										else 
											createMessagesLocal('cdr_result_messages',"{% trans 'Could not save settings' %}", 'error', 3000);	
									}
							});
	}


	// Checks if an email address is valid
	function isValidEmailAddress(emailAddress) {
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   if(reg.test(emailAddress) == false) {
			  return false;
		   }
		   return true;
	}

// Shows a jquery ui dialog
function showDialog(dtype,tit,h,w){
	id_dialog = "#" + dtype
	$( id_dialog ).dialog({
					resizable: true,
					title:tit,
					height:h,
					width:w,
					modal: true,
					show: "blind",
					hide: "fold",
					buttons: [{
						id: dtype+"-close-button",
						text: 'Close',
						click: function() {
							$( this ).dialog( "close" );
						}
					}]
				});
}

</script>

