<div class="panelHeader">
	<h3>YouTube Delivery Setting Defaults</h3>
</div>
<div class="panelContent">
	<form action="/relationships/youtubedeliverysettingdefaults" method="post">
		<div id="channel_message" class="alertError" style="display: none"></div>
		<div id="channel_error_message" class="alertError" style="display: none"></div>
		<input type="hidden" name="vendor_id" value="<?=$this->vendor_id?>">
		<table class="form full">
		<tr><th>YouTube Channel:</th>
			<td><input type="text" class="textField small" name="video_upload_channel" id="video_upload_channel" value="<?=$this->video_upload_channel?>">
			<div id='channelVerified' class="textField small" style="display: none"></div>
			  <input type="hidden" name="youtube_channel_name" id="youtube_channel_name" value=""></td>
			<td><input type = "checkbox" class="small" name="music_video_vendor" id="music_video_vendor" value="vevo"<?=($this->music_video_vendor=='vevo'?'checked="checked"':'')?>>Vevo Controlled</input>
			</td>
		</tr>
		<tr><th>Audio Match Policy:</th>
			<td><select class="small" name="audio_match_policy" id="audio_match_policy">
				<option value="monetize"<?=($this->audio_match_policy=='monetize'?' selected="selected"':'')?>>Monetize</option>
				<option value="block"<?=($this->audio_match_policy=='block'?' selected="selected"':'')?>>Block</option>
				</select></td>
		</tr>
		<tr><th>Video Match Policy:</th>
			<td><select class="small" name="video_match_policy" id="video_match_policy">
				<option value="monetize"<?=($this->video_match_policy=='monetize'?' selected="selected"':'')?>>Monetize</option>
				<option value="block"<?=($this->video_match_policy=='block'?' selected="selected"':'')?>>Block</option>
				<option value="dont_match"<?=($this->video_match_policy=='dont_match'?' selected="selected"':'')?>>Don't Match</option>
				</select></td>
		</tr>
		<tr><th style="display: none">Video Upload Policy:</th>
			<td><select class="small" name="video_upload_policy" id="video_upload_policy"style="display: none">
				<option value="public"<?=($this->video_upload_policy=='public'?' selected="selected"':'')?>>Public</option>
				<option value="fingerprint_only"<?=($this->video_upload_policy=='fingerprint_only'?' selected="selected"':'')?>>Fingerprint Only</option>
				</select></td>
		</tr>
		</table>
		<div class="panelButtons">
			<input type="submit" id="submit_button" class="btn btn-sm btn-primary" name="submit_button" value="Update" onclick="return validateChannelName();">
		</div>
	</form>
</div>
<script>
		/**
		 * This function validates the other channel name to be a valid youtube channel name.
		 * 2-20 chars and no special chars allowed
		 */
	function validateChannelName(){
		//trim the values before validating
		$("#video_upload_channel").val($.trim($("#video_upload_channel").val()));

		if($("#music_video_vendor").is(':checked') && !$("#video_upload_channel").val().match(/^[A-Za-z0-9]{2,20}$/)){
			$("#channel_error_message").html("Invalid Youtube channel name, it should not contain special characters. Length should be 2-20 characters.").show();
			$("#alertConfirm").hide();
			return false;
		}
		if($("#video_upload_channel").val().length != 0 && !$("#music_video_vendor").is(':checked') && !$("input:hidden[name=youtube_channel_name]").val()){
			$("#channel_error_message").html("Channel name should be verified.").show();
			return false;
		}

		$("#channel_error_message").hide();
		return true;
	}
	$(document).ready(function() {
		$("#channel_message").html("Changing these settings will not impact this vendor's artist-level YouTube Delivery Settings.").show();

		$("#video_upload_channel").bind("blur",function(){
			if($("#video_upload_channel").val()!= "" && !$("#music_video_vendor").is(':checked'))
					checkYoutubeChannel($(this).val());
		});
		$("#music_video_vendor").bind("click",function(){
			if($("#music_video_vendor").is(':checked')){
				$("#channelVerified").hide();
				$("input:hidden[name=youtube_channel_name]").val('');
			}
		});

	});

	//Get youtubeChannelId from channel address
	function checkYoutubeChannel(){
		if(!$("#music_video_vendor").is(':checked')){
			var channelName = $("#video_upload_channel").val();
				if(!$("#video_upload_channel").val().match(/^[A-Za-z0-9]{2,20}$/)){
					$("#channelVerified").hide();
					$("input:hidden[name=youtube_channel_name]").val("");
					$("#channel_error_message").html("Invalid Youtube channel name, it should not contain special characters. Length should be 2-20 characters.").show();
					return false;
				} else {
					$("#channel_error_message").fadeOut();
				}
				$.ajax({
					type: 'POST',
					url: '/deliverysettings/verifypartneredchannel',
					data: 'format=json&ajax=1&channelName=' + channelName + '&channelType=name',
					dataType: 'json',
					async: true,
					beforeSend: function( xhr ) {
					     $("#channelVerified").html("Verifying...");
					     $("#channelVerified").show();
					     $("#submit_button").attr("disabled","disabled");
					},
					success: function( data, textStatus, XMLHttpRequest ){
						$('#channel_error_message').fadeOut();
						// Verified message
						$("#channelVerified").html('Channel name Verified.').show();
						$("input:hidden[name=youtube_channel_name]").val(data.response.youtubeChannelId);
						return true;
					},
					error: function( xhr, textStatus, errorThrown ){
						var obj = jQuery.parseJSON(xhr.responseText);
						$("#channelVerified").hide();
						 $("input:hidden[name=youtube_channel_name]").val("");
						//Error message
						$("#channel_error_message").html(obj.err_msg).show();
						return false;
					},
					complete: function() {
						 $("#submit_button").removeAttr("disabled");
					}
				});
	}
		return false;
}

</script>