<div id="primary_header">
	<h1><?php echo $this->translate('Webphone Account Settings')?></h1>
	<div id="back_button">
		<a href="#">
			<span class="stronglastword"><?php echo $this->translate('Save Settings') ?></span>
		</a>
	</div>
</div>

<div id="primary_content">
	<div id="primary_content_inner">
		<div id="Flash" class="flash_notice"><?php echo $this->translate('The settings have been updated') ?></div>

		<div id="webphone_settings">
			<h2>Webphone Settings</h2><br>
			<p>Use the following button to adjust webphone settings like microphone, volume etc.</p>
		</div>

		<br>
		<div style="display:none;" class="bottomFormMainButtonArea">
			<div id="back_button" ><a href="#" onclick="$phone.showSettingsUICallback(); return false;"><span class="stronglastword">Adjust<strong> Microphone</strong></span></a></div>
		</div>
		<br>
		<br>

		<div class="divider"/>

		<form id="SIPsettingsForm" method="post" action="<?php echo $this->moduleBaseUrl ?>/settings/">
			<input type="hidden" name="save" value="1" />
			<table id="user_sip_account_info" class="settings">
				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('User ID')?></label></td>
					<td>
						<input type="text" class="medium" value="<?php echo $this->selectedSIPAccount['UserId'] ?>" name="fields[UserId]" id="UserId" />
						<small><?php echo $this->translate('Webphone User ID (usually the same as your Account Name).')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('Domain')?></label></td>
					<td>
						<input type="text" class="medium" value="<?php echo $this->selectedSIPAccount['Domain'] ?>" name="fields[Domain]" id="Domain" />
						<small><?php echo $this->translate('Webphone domain name.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('Authorization Name')?></label></td>
					<td>
						<input type="text" class="medium" value="<?php echo $this->selectedSIPAccount['AuthorizationName'] ?>" name="fields[AuthorizationName]" id="AuthorizationName" />
						<small><?php echo $this->translate('The Authorization Name is usually the same as your Account Name, may vary.')?></small>
					</td>
				</tr>
				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('Password')?></label></td>
					<td>
						<input type="password" class="medium" value="<?php echo $this->selectedSIPAccount['Password'] ?>" name="fields[Password]" id="SIPPassword" />
					</td>
				</tr>
				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('Caller ID Display Name')?></label></td>
					<td>
						<input type="text" class="medium" value="<?php echo $this->selectedSIPAccount['DisplayName'] ?>" name="fields[DisplayName]" id="DisplayName" />
						<small><?php echo $this->translate('This name will display as your Caller ID for outgoing calls.')?></small>
					</td>
				</tr>

				<tr>
					<td class="label"><label for="realName"><?php echo $this->translate('SIP Server Address')?></label></td>
					<td>
						<input type="text" class="medium" value="<?php echo $this->selectedSIPAccount['SIPServer'] ?>" name="fields[SIPServer]" id="SIPServer" />
						<small><?php echo $this->translate('The address of your SIP server.')?></small>
					</td>
				</tr>
			</table>

			<div class="divider"/>

			<table class="settings">

				<tr>
					<td class="label"><?php print $this->translate('Enable SIP')?></td>
					<td>
						<input name="fields[SIP]" type='hidden' value="0"/>
						<input name="fields[SIP]" class="iphoneStyle enableSIPToggle" type="checkbox" value="1"<?php echo ($this->selectedSIPAccount['SIP']=='1') ? ' checked="checked"' : '' ?> />
						<small><?php print $this->translate('Enable this to use SIP feature')?></small>
					</td>
				</tr>

			</table>

		</div>
	</form>

	<br>
			
	<div class="bottomFormMainButtonArea">
		<div id="back_button" ><a href="#" onclick="webphone_settings_save();"><span class="stronglastword">Save<strong> Settings</strong></span></a></div>
	</div>

</div>

<script type="text/javascript">

	var SIPEnabled = <?php echo $this->selectedSIPAccount['SIP']?'true':'false' ?>

	function webphone_settings_save()
	{

		var o = {};
		var params = $("#SIPsettingsForm").serializeArray();
		$.each(params, function() {
			if (o[this.name] !== undefined) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});

		var url = $("#SIPsettingsForm").attr("action") + 'sipaccountpresave'
		var postData = o;

		$.ajax({
			type:"POST",
			url:url,
			dataType:"json",
			data:postData,
			success:function(res) {

				if(res.needverify == 1) {
					VerifySipAccount(res);					
				}
				else {
					location.reload(true);
				}

			},
			error:function() {
			}
		});

	}

	function VerifySipAccount(params)
	{

		$phone.SIPLogOff();
		$phone.testingNewSIPAccount = true;
		$phone.params.authToken = params.token;
		$phone.SIPLogin();

	}

	function VerifySipAccountCallback(result)
	{
		if( SIPEnabled )
			$phone.testingNewSIPAccount = null;

		if( !result ) 
		{

			$('input[name=save]', '#SIPsettingsForm').val('-1');
			$.php($("#SIPsettingsForm").attr("action") + 'sipaccountsettingssave', $("#SIPsettingsForm").serializeArray());
			alert('New credential verification failed! Please check again');
			return;

		}
		else 
		{

			$('input[name=save]', '#SIPsettingsForm').val('1');
			$.php($("#SIPsettingsForm").attr("action") + 'sipaccountsettingssave', $("#SIPsettingsForm").serializeArray());

		}

	}

	$(function()
	{
		<?php if(!$this->userSIPGwEnabled): ?>
			$('tr', '#user_sip_account_info').css('opacity','0.5');
			$('input', '#user_sip_account_info').attr('disabled','true');
		<?php endif; ?>

		if( SIPEnabled != 1 )
			$('input.medium', '#SIPsettingsForm').attr('disabled', 'true');
		
		$('.enableSIPToggle').bind("change", function() 
		{
			
			if( $(this).attr("checked") )
			{
				
				$('input.medium', '#SIPsettingsForm').removeAttr('disabled');
				$('tr', '#user_sip_account_info').css('opacity','1');
				$('input', '#user_sip_account_info').removeAttr('disabled');
			}
			
		})

		// bind to the form's submit event and cancel legacy submit
		$('#SIPsettingsForm', '#global').submit(function()
		{
			$("#Flash", "#global").hide();

			// Grab the HTML content, put into the textarea to save

			$('.rte-zone', '#global').contents().find('SCRIPT').remove();
			var html = $('.rte-zone', '#global').rte_html();
			$("#Signature", "#global").attr('value', html);

			$.php($("#settingsForm", '#global').attr("action"),$("#settingsForm", '#global').serializeArray());
			return false;
		});
		$('#back_button a', '#global').bind("click", function() { return $('#settingsForm', '#global').submit()});

		$("#ViewThreads", "#global").change( function() { toggleThreads() });

		updateWebmailSettings();
		toggleThreads();

		try {
			$('#Settings :checkbox').iphoneStyle();
		}
		catch(e) {
			//console.log(e);
		}

		if($.browser.msie && $.browser.version < 9)
		{
			$('#faviconSetting').disable();
			$('#faviconSetting input').attr('checked','').disable();
			$('#autoReloadRow small').append("<?php echo $this->translate(' This feature not fully supported by your current browser.') ?>");
		}

		$('#AutoReload_Bitfield', '#global').trigger('change');

		$('.stronglastword').each( function() { $(this).stronglastword() });

	});

</script>
