//UTF8
$(document).ready(function() {
	
	var formSubmitted = false;
	

	$( window ).load(function() {
		$("#response_msg").hide();	
		var ls_host = location.hostname + (location.port ? ':'+location.port: '');
		var ls_port = location.port;
		var ls_protocol = location.protocol + "//";
		/*
		if (ls_port == "86" || ls_port == "85") {
			ls_protocol = "http://";
		} else if (ls_host=="10.87.121.216" || ls_host=="10.36.250.86") {
			ls_protocol = "";
			ls_host = "";
		}*/
      	/*
		if (ls_host=="hkt.com" || ls_host=="www.hkt.com") {
			//do nothing
		} else {
			ls_protocol = "";
			ls_host = "";
		}
        */
		
		ls_protocol = "https://";
		if (ls_port == "85") {
			ls_protocol = "http://";
        }
		
		console.log("ls_host:" + ls_host);
		console.log("ls_port:" + ls_port);
		console.log("ls_protocol:" + ls_protocol);
		
		console.log("domain:" +ls_protocol+ls_host);		
		if($("#frame-emailalert").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/emailAlert.html";
			//var ls_addr = "/assets/HKTCorpsite/eform/emailAlert.html";
			console.log("frame-emailalert addr:" + ls_addr);
			$("#frame-emailalert").attr("src",ls_addr);
		}
		if($("#frame-emailalert-c").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/emailAlert_c.html";
			console.log("frame-emailalert-c addr:" + ls_addr);
			$("#frame-emailalert-c").attr("src",ls_addr);
		}
		if($("#frame-feedback").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/feedback.html";
			//var ls_addr = "/assets/HKTCorpsite/eform/feedback.html";
			console.log("frame-feedback addr:" + ls_addr);
			$("#frame-feedback").attr("src",ls_addr);
		}
		if($("#frame-feedback-c").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/feedback_c.html";
			console.log("frame-feedback-c addr:" + ls_addr);
			$("#frame-feedback-c").attr("src",ls_addr);
		}
		if($("#frame-servicerelocation").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/serviceRelocation.html";
			//var ls_addr = "/assets/HKTCorpsite/eform/serviceRelocation.html";
			console.log("frame-servicerelocation addr:" + ls_addr);
			$("#frame-servicerelocation").attr("src",ls_addr);
		}
		if($("#frame-servicerelocation-c").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/serviceRelocation_c.html";
			console.log("frame-servicerelocation-c addr:" + ls_addr);
			$("#frame-servicerelocation-c").attr("src",ls_addr);
		}
		if($("#frame-mediasubscribe").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/mediaSubscribe.html";
			//var ls_addr = "/assets/HKTCorpsite/eform/mediaSubscribe.html";
			console.log("frame-mediasubscribe addr:" + ls_addr);
			$("#frame-mediasubscribe").attr("src",ls_addr);
		}
		if($("#frame-mediasubscribe-c").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/mediaSubscribe_c.html";
			console.log("frame-mediasubscribe-c addr:" + ls_addr);
			$("#frame-mediasubscribe-c").attr("src",ls_addr);
		}
		if($("#frame-webcastregistration").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/webcastRegistration.html";
          	var qString = window.location.search;
			console.log("frame-webcastregistration addr:" + ls_addr);
			console.log("frame-webcastregistration-c window.location.search:" + qString);
			$("#frame-webcastregistration").attr("src",ls_addr + qString);
		}
		if($("#frame-webcastregistration-c").length > 0) {
			var ls_addr = ls_protocol+ls_host+"/assets/HKTCorpsite/eform/webcastregistration_c.html";
          	var qString = window.location.search;
			console.log("frame-webcastregistration-c addr:" + ls_addr);
			console.log("frame-webcastregistration-c window.location.search:" + qString);
			$("#frame-webcastregistration-c").attr("src",ls_addr + qString);
		}

 });	
	
	<!-- Real-time Validation -->
		<!--Input can't be blank-->
		$('#hkt_given_name').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_first_name').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_surname').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_new_installation_address').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_organization').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_company').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		$('#hkt_message').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});		
		/*
		$('#position').on('input', function() {
			var input=$(this);
			var is_name=input.val();
			if(is_name){unhighlightInput(input);}
			else{highlightInput(input);}
		});
		*/
		<!--Email must be an email -->
		$('#hkt_email').on('input', function() {
			var input=$(this);
			//var re = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
			//console.log("check email: " + input.val());
			//var is_email=re.test(input.val());
			if(isEmail(input.val())){
				unhighlightInput(input);
			}else{highlightInput(input);}
		});

		$('#hkt_email').on('input', function() {
			var input=$(this);
			//var re = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
			//console.log("check email: " + input.val());
			//var is_email=re.test(input.val());
			if(isEmail(input.val())){
				unhighlightInput(input);
			}else{highlightInput(input);}
		});
		
		<!--Phone Number must be an phone number -->
		$('#hkt_contact_number_day').on('input', function() {
			var input=$(this);
			var type = $("#Type").val();
			if (type == "HKTServiceRelocation") {
				if(isContactNumber(input.val())){
					unhighlightInput(input);
				}else{
					if(!(input.val())){
						unhighlightInput(input);
					} else {
						highlightInput(input);
					}
				}
			} else {
				if(isContactNumber2(input.val())){
					unhighlightInput(input);
				}else{
					if(!(input.val())){
						unhighlightInput(input);
					} else {
						highlightInput(input);
					}
				}
			}
		});
		$('#hkt_contact_number_night').on('input', function() {
			var input=$(this);
			var type = $("#Type").val();
			if (type == "HKTServiceRelocation") {
				if(isContactNumber(input.val())){
					unhighlightInput(input);
				}else{
					if(!(input.val())){
						unhighlightInput(input);
					} else {
						highlightInput(input);
					}
				}
			} else {
				if(isContactNumber2(input.val())){
					unhighlightInput(input);
				}else{
					if(!(input.val())){
						unhighlightInput(input);
					} else {
						highlightInput(input);
					}
				}
			}
		});
		
		/*
		<!--Contact Telephone must be digit and space -->
		$('#contact_number').on('input', function() {
			var input=$(this);
			if(isContactNumber2(input.val())){
				unhighlightInput(input);
			}else{highlightInput(input);}
		});
		*/
  
		<!--Account Number must be Account No -->
		$('#hkt_eye_service_no').on('input', function() {
			var input=$(this);
			if(isAccountNumber(input.val())){
				unhighlightInput(input);
			}else{highlightInput(input);}
		});		
  
		<!--Web URL must be URL -->
		/*
		$('#web_url').on('input', function() {
			var input=$(this);
			if(isWebsiteURL(input.val())){
				unhighlightInput(input);
			}else{highlightInput(input);}
		});		
		*/		

	<!-- End Real-time Validation -->		
	
	
	var highlightInput = function(input) {
			input.css("border", "2px solid red");
	};

	var unhighlightInput = function(input) {
			input.css("border", "");
	};
	

	$("#EmailAlertForm").submit(function(e)
	{
		console.log("start submit");
		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");
		var langu = $("#Language").val();
		console.log("data:" + JSON.stringify(postData));
		console.log("type:" + $("#Type").val());
		console.log("langu:" + langu);
		
		$.ajax(
		{
			url : formURL,
			type: "Post",
			//data : postData,
      data: {
      	"data": JSON.stringify(postData),
      	"type": $("#Type").val(),
      	"language": langu
      },
			//dataType:'json',
      //contentType: "application/json; charset=utf-8",
			success:function(data, textStatus, jqXHR) 
			{
				console.log("langu:" + langu);
				if (data.res_code == "0000") {
					if ($("#status").val() == "Y") {
						showResponseMsg2(langu, "Thank you for your subscription. Your email address has been added to HKT Investor Relations online mailing list.", "多謝您選擇收取資料，您的電郵地址已加入香港電訊的網上投資者名錄。");
					} else {
						if (data.res_msg != ""){
							showResponseMsg2(langu, "You will not be receiving latest Company " + data.res_msg + " alerts from now on.", "由現在開始你將不會收到最新的 " + data.res_msg + " 通知。");
						} else {
							showResponseMsg2(langu, "Your email address has been taken out from the mailing list.", "您在名單上的電郵地址已被刪除。");
						}
					}
					$("#EmailAlertForm").trigger("reset");
					$("#email-registration-form").trigger("reset");
				} else if (data.res_code == "0100") {
						showResponseMsg2(langu, "Your email address is not on our subscriber's list. Please double check.", "我們的名單並沒有您的電郵地址，請核實一下。");
				} else if (data.res_code == "0101") {
						showResponseMsg2(langu, "Thank you for your interest. You are already our Email Alerts subscriber.", "謝謝。您已經是我們的電子郵件讀者。");
				} else {
					showResponseMsg(data.res_msg + "(" + data.res_code + ")");
				}
			},
			error: function(jqXHR, textStatus, errorThrown) 
			{
				alert("submit failed");

			}
		});
	    formSubmitted=true;
	    e.preventDefault();	//STOP default action
	});
	
	var hideResponseMsg = function() {
			$("#response_msg").hide();
	};
	
	var showResponseMsg = function(s) {
			$("#response_msg").html(s);
			$("#response_msg").show();
	};

	var showResponseMsg2 = function(l, e, c) {
			if (l == "2") {
				showResponseMsg(c);
			} else {
				showResponseMsg(e);
			}
	};
	
	var showModalMsg = function (s){
		var modal = document.getElementById('mediaSubscribeModal');
		modal.style.display = "block";
		$("#modal_msg").html(s);
		//window.parent.parent.scrollTo(0,0);
		parent.document.body.style.backgroundColor = 'rgba(0,0,0,0.4)';

	};
	$("#modal-close").click(function() {
		var modal = document.getElementById('mediaSubscribeModal');
		modal.style.display = "none";
		window.parent.document.body.removeAttribute("style");
	});
	window.onclick = function(event) {
		var modal = document.getElementById('mediaSubscribeModal');
		if (event.target == modal) {
			modal.style.display = "none";
			window.parent.document.body.removeAttribute("style");
		}
	}
	var showResponseMsg3 = function(l, e, c) {
		if (l == "2") {
			showModalMsg(c);
		} else {
			showModalMsg(e);
		}
	};
	
	
	$( "#subscribeEmailAlert" ).click(function() {
						
		console.log("subscribe clicked, " + formSubmitted);		
		$('#status').val('Y');
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#email-registration-form :input").each(function(){
			unhighlightInput($(this));
		});			
		
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}
		if($("#hkt_announcements").prop('checked') == false && $("#hkt_press_releases").prop('checked') == false && $("#hkt_shareholder_circulars").prop('checked') == false && 
			$("#hkt_financial_results").prop('checked') == false && $("#hkt_presentations").prop('checked') == false){
			valid=false;			
			showResponseMsg2(langu, "At least one information must be selected.", "請選擇其中一項資訊。");
			return false;
		}
		if (valid) {
			updateEmailForm();
			$("#EmailAlertForm").submit();
		}
				
	});
	
	
$( "#unsubscribeEmailAlert" ).click(function() {
						
		console.log("unsubscribe clicked, " + formSubmitted);		
		$('#status').val('N');
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#email-registration-form :input").each(function(){
			unhighlightInput($(this));
		});
				
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}
		if($("#hkt_announcements").prop('checked') == false && $("#hkt_press_releases").prop('checked') == false && $("#hkt_shareholder_circulars").prop('checked') == false && 
			$("#hkt_financial_results").prop('checked') == false && $("#hkt_presentations").prop('checked') == false){
			valid=false;			
			showResponseMsg2(langu, "At least one information must be selected.", "請選擇其中一項資訊。");
			return false;
		}
		if (valid) {
			updateEmailForm();
			$("#EmailAlertForm").submit();
			event.preventDefault();
		}
				
	});
	
	$("#FeedbackForm").submit(function(e)
	{
		console.log("start submit");
		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");
		var langu = $("#Language").val();
		console.log("data:" + JSON.stringify(postData));
		console.log("type:" + $("#Type").val());
		
		$.ajax(
		{
			url : formURL,
			type: "Post",
      data: {
      	"data": JSON.stringify(postData),
      	"type": $("#Type").val(),
      	"language": langu
      },      
			success:function(data, textStatus, jqXHR) 
			{
				if (data.res_code == "0000") {
					showResponseMsg2(langu, "Thank you for your feedback. We will study this thoroughly to improve our services or products", "謝謝閣下提出寶貴意見，我們會加以充份考慮，務求服務精益求精。");
				} else {
					showResponseMsg(data.res_msg + "(" + data.res_code + ")");
				}
				$("#FeedbackForm").trigger("reset");
				$("#feedback-form").trigger("reset");				
			},
			error: function(jqXHR, textStatus, errorThrown) 
			{
				alert("submit failed");

			}
		});
	    formSubmitted=true;
	    e.preventDefault();	//STOP default action
	});	
	
	$( "#FeedbackSubmit" ).click(function() {
		
		console.log("FeedbackSubmit clicked, " + formSubmitted);		
		
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#feedback-form :input").each(function(){
			unhighlightInput($(this));
		});		
				
		if (!$("#hkt_given_name").val()) {
			valid=false;			
			highlightInput($("#hkt_given_name"));
			showResponseMsg2(langu, "First name is required", "請填上名字。");
			return false;
		}
		if (!$("#hkt_surname").val()) {
			valid=false;			
			highlightInput($("#hkt_surname"));
			showResponseMsg2(langu, "Last name is required", "請填上姓氏。");
			return false;
		}		
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		if (!$("#hkt_contact_number_day").val() && !$("#hkt_contact_number_night").val()) {
			valid=false;			
			highlightInput($("#hkt_contact_number_day"));
			showResponseMsg2(langu, "contact number is required", "請填上聯絡電話。");
			return false;
		}
		
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}
		
		if ($("#hkt_contact_number_day").val() && !isContactNumber2($("#hkt_contact_number_day").val())) {
			valid=false;			
			highlightInput($("#hkt_contact_number_day"));
			showResponseMsg2(langu, "invalid contact number for day time", "您輸入的聯絡電話格式有誤。");
			return false;
		}

		if ($("#hkt_contact_number_night").val() && !isContactNumber2($("#hkt_contact_number_night").val())) {
			valid=false;			
			highlightInput($("#hkt_contact_number_night"));
			showResponseMsg2(langu, "invalid contact number for evening time", "您輸入的聯絡電話格式有誤。");
			return false;
		}
		
		unhighlightInput($("#hkt_contact_number_day"));

		if (!$("#hkt_message").val()) {
			valid=false;			
			highlightInput($("#hkt_message"));
			showResponseMsg2(langu, "Message is required", "請填上意見。");
			return false;
		}			
		
		if (valid) {
			updateFeedbackForm();
			$("#FeedbackForm").submit();
		}
	});
	
	$( "#FeedbackClear" ).click(function() {
		console.log("FeedbackCancel clicked, " + formSubmitted);		
		hideResponseMsg();
		$("#feedback-form").trigger("reset");
		$("form#feedback-form :input").each(function(){
			unhighlightInput($(this));
		});				
	});	
	
	
	$("#ServiceRelocationForm").submit(function(e)
	{
		console.log("start submit");
		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");
		var langu = $("#Language").val();
		console.log("data:" + JSON.stringify(postData));
		console.log("type:" + $("#Type").val());

		$.ajax(
		{
			url : formURL,
			type: "Post",
      data: {
      	"data": JSON.stringify(postData),
      	"type": $("#Type").val(),
      	"language": langu
      },      
			success:function(data, textStatus, jqXHR) 
			{
				if (data.res_code == "0000") {
					showResponseMsg2(langu, "Thank you for informing us. Our Service representative will contact you shortly.", "謝謝！我們的客戶服務代表將儘快聯絡閣下跟進有關事宜。");
				} else {
					showResponseMsg(data.res_msg + "(" + data.res_code + ")");
				}
			  $("#ServiceRelocationForm").trigger("reset");
			  $("#service-reloc-form").trigger("reset");
			},
			error: function(jqXHR, textStatus, errorThrown) 
			{
				alert("submit failed");

			}
		});
	    formSubmitted=true;
	    e.preventDefault();	//STOP default action
	});		
	
	$( "#ServiceRelocationSubmit" ).click(function() {
		
		console.log("ServiceRelocationSubmit clicked, " + formSubmitted);		
		
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#service-reloc-form :input").each(function(){
			unhighlightInput($(this));
		});				
		
		if (!$("#hkt_given_name").val()) {
			valid=false;			
			highlightInput($("#hkt_given_name"));
			showResponseMsg2(langu, "First name is required", "請填上名字。");
			return false;
		}
		if (!$("#hkt_surname").val()) {
			valid=false;			
			highlightInput($("#hkt_surname"));
			showResponseMsg2(langu, "Last name is required", "請填上姓氏。");
			return false;
		}		
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		if (!$("#hkt_contact_number_day").val() && !$("#hkt_contact_number_night").val()) {
			valid=false;			
			highlightInput($("#contact_number_day"));
			showResponseMsg2(langu, "contact number is required", "請填上聯絡電話。");
			return false;
		}
		
		if (!$("#hkt_fixed_line_service_no").val() && !$("#hkt_eye_service_no").val() && !$("#hkt_netvigator_acc_no").val() && !$("#hkt_nowtv_acc_no").val()) {
			valid=false;			
			//highlightInput($("#fixed_line_service_no"));
			showResponseMsg2(langu, "Please enter your Services / products", "請填上服務/產品資料。");
			return false;
		}
		
		if (!$("#hkt_new_installation_address").val()) {
			valid=false;			
			highlightInput($("#hkt_new_installation_address"));
			showResponseMsg2(langu, "New Installation Address is required", "請填上新安裝地址。");
			return false;
		}

		if (!$("#hkt_additional_information").val()) {
			valid=false;			
			highlightInput($("#hkt_additional_information"));
			showResponseMsg2(langu, "Additional information", "請填上其他資料。");
			return false;
		}
		

		
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}
		
		if ($("#hkt_contact_number_day").val() && !isContactNumber($("#hkt_contact_number_day").val())) {
			valid=false;			
			highlightInput($("#contact_number_day"));
			showResponseMsg2(langu, "invalid contact number (day)", "您輸入的聯絡電話格式有誤。");
			return false;
		}
		
		if ($("#hkt_contact_number_night").val() && !isContactNumber($("#hkt_contact_number_night").val())) {
			valid=false;			
			highlightInput($("#hkt_contact_number_night"));
			showResponseMsg2(langu, "invalid contact number (night)", "您輸入的聯絡電話格式有誤。");
			return false;
		}
		
		if ($("#hkt_eye_service_no").val() && !isAccountNumber($("#hkt_eye_service_no").val())) {
			valid=false;			
			highlightInput($("#hkt_eye_service_no"));
			showResponseMsg2(langu, "invalid eye service no", "您輸入的eye 多媒睇號碼格式有誤。");
			return false;
		}
		
				
		if (valid) {
			updateServiceRelocationForm();
			$("#ServiceRelocationForm").submit();
		}
	});
	
	$( "#ServiceRelocationClear" ).click(function() {
		console.log("Service Relocation clear clicked, " + formSubmitted);		
		hideResponseMsg();
		$("#service-reloc-form").trigger("reset");
		$("form#service-reloc-form :input").each(function(){
			unhighlightInput($(this));
		});		
	});
	
	
	
	$("#MediaSubscribeForm").submit(function(e)
	{
		console.log("start submit");
		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");
		var langu = $("#Language").val();
		console.log("data:" + JSON.stringify(postData));
		console.log("type:" + $("#Type").val());

		$.ajax(
		{
			url : formURL,
			type: "Post",
      data: {
      	"data": JSON.stringify(postData),
      	"type": $("#Type").val(),
      	"language": langu
      },      
			success:function(data, textStatus, jqXHR) 
			{
				
				console.log("langu:" + langu);
				if (data.res_code == "0000") {
					if ($("#status").val() == "Y") {
						showResponseMsg3(langu, "Your subscription has been accepted. Thank you.", "您的訂閱已被接受。 謝謝。");
					} else {
						showResponseMsg3(langu, "Your subscription has been cancelled.", "您的訂閱已被取消。");
					}
				  $("#MediaSubscribeForm").trigger("reset");
				  $("#email-registration-form").trigger("reset");
				} else if (data.res_code == "0100") {
						showResponseMsg3(langu, "Your email address is not on our subscribers list. Please check.", "我們的名單並沒有您的電郵地址，請核實一下。");
				} else if (data.res_code == "0101") {
						showResponseMsg3(langu, "Your subscription has been accepted. Thank you.", "您的訂閱已被接受。 謝謝。");
						$("#MediaSubscribeForm").trigger("reset");
				  	$("#email-registration-form").trigger("reset");
				} else {
					showResponseMsg(data.res_msg + "(" + data.res_code + ")");
				}
				
			},
			error: function(jqXHR, textStatus, errorThrown) 
			{
				alert("submit failed");

			}
		});
	    formSubmitted=true;
	    e.preventDefault();	//STOP default action
	});	
	
	
		
	$( "#MediaSubscribe" ).click(function() {
		
		console.log("MediaSubscribe clicked, " + formSubmitted);		
		$("#status").val("Y");		
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#email-registration-form :input").each(function(){
			unhighlightInput($(this));
		});				
		
		
		if (!$("#hkt_given_name").val()) {
			valid=false;			
			highlightInput($("#hkt_given_name"));
			showResponseMsg2(langu, "First name is required", "請填上名字。");
			return false;
		}
		if (!$("#hkt_surname").val()) {
			valid=false;			
			highlightInput($("#hkt_surname"));
			showResponseMsg2(langu, "Last name is required", "請填上姓氏。");
			return false;
		}		
		if (!$("#hkt_organization").val()) {
			valid=false;			
			highlightInput($("#hkt_organization"));
			showResponseMsg2(langu, "Name of organization is required", "請填上機構名稱。");
			return false;
		}
		if (!$("#hkt_position").val()) {
			valid=false;			
			highlightInput($("#hkt_position"));
			showResponseMsg2(langu, "Job title/position is required", "請填上職位。");
			return false;
		}
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}

		//if ($("#hkt_contact_number").val() && !isContactNumber3($("#contact_number").val())) {
			if ($("#hkt_contact_number").val() && !isContactNumber3($("#hkt_contact_number").val())) {
			valid=false;			
			highlightInput($("#hkt_contact_number"));
			showResponseMsg2(langu, "The Contact telephone you entered is not in a valid format.", "您輸入的聯絡電話格式有誤。");
			return false;
		}
		if ($("#hkt_web_url").val() && !isWebsiteURL($("#hkt_web_url").val())) {
			valid=false;			
			highlightInput($("#hkt_web_url"));
			showResponseMsg2(langu, "The Organization website URL you entered is not in a valid format.", "您輸入的機構網址格式有誤。");
			return false;
		}	
		
		if (valid) {
			updateMediaForm();
			$("#MediaSubscribeForm").submit();
		}
	});
	
	$( "#MediaUnsubscribe" ).click(function() {
		
		console.log("MediaUnsubscribe clicked, " + formSubmitted);		
		$("#status").val("N");		
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		$("form#email-registration-form :input").each(function(){
			unhighlightInput($(this));
		});				
		
		
		if (!$("#hkt_given_name").val()) {
			valid=false;			
			highlightInput($("#hkt_given_name"));
			showResponseMsg2(langu, "First name is required", "請填上名字。");
			return false;
		}
		if (!$("#hkt_surname").val()) {
			valid=false;			
			highlightInput($("#hkt_surname"));
			showResponseMsg2(langu, "Last name is required", "請填上姓氏。");
			return false;
		}		
		if (!$("#hkt_organization").val()) {
			valid=false;			
			highlightInput($("#hkt_organization"));
			showResponseMsg2(langu, "Name of organization is required", "請填上機構名稱。");
			return false;
		}
		if (!$("#hkt_position").val()) {
			valid=false;			
			highlightInput($("#hkt_position"));
			showResponseMsg2(langu, "Job title/position is required", "請填上職位。");
			return false;
		}
		if (!$("#hkt_email").val()) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}

		
		if (!isEmail($("#hkt_email").val())) {
			valid=false;			
			highlightInput($("#hkt_email"));
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}

		
		if ($("#hkt_contact_number").val() && !isContactNumber3($("#hkt_contact_number").val())) {
			valid=false;			
			highlightInput($("#hkt_contact_number"));
			showResponseMsg2(langu, "The Contact telephone you entered is not in a valid format.", "您輸入的聯絡電話格式有誤。");
			return false;
		}
		
		if ($("#hkt_web_url").val() && !isWebsiteURL($("#hkt_web_url").val())) {
			valid=false;			
			highlightInput($("#hkt_web_url"));
			showResponseMsg2(langu, "The Organization website URL you entered is not in a valid format.", "您輸入的機構網址格式有誤。");
			return false;
		}	
		
		
		if (valid) {
			updateMediaForm();
			$("#MediaSubscribeForm").submit();
		}
	});	
	
	$( "#MediaSubscribeClear" ).click(function() {
		console.log("MediaSubscribeClear clicked, " + formSubmitted);		
		hideResponseMsg();
		$("#MediaSubscribeForm").trigger("reset");
		$("#email-registration-form").trigger("reset");
	});				
	
	$( "#PopupPrivacyInfo" ).click(function() {
		var langu = $("#Language").val();
		if (langu == "2"){
			window.open("/assets/HKTCorpsite/files/terms/privacy_information_collection_statement_cn.html", "_blank");
		}
		else {
			window.open("/assets/HKTCorpsite/files/terms/privacy_information_collection_statement.html", "_blank");
		}
	});
	
	$( "#WebcastRegistrationSubmit" ).click(function() {
		
		console.log("WebcastRegistrationSubmit clicked, " + formSubmitted);		
		$("#status").val("Y");		
		var valid=true;
		var langu = $("#Language").val();
		hideResponseMsg();
		
		if (!$("#first_name").val()) {
			valid=false;			
			highlightInput($("#first_name"));
			showResponseMsg2(langu, "First name is required", "請填上名字。");
			return false;
		}
		if (!$("#surname").val()) {
			valid=false;			
			highlightInput($("#surname"));
			showResponseMsg2(langu, "Last name is required", "請填上姓氏。");
			return false;
		}		
		if (!$("#company").val()) {
			valid=false;			
			highlightInput($("#company"));
			showResponseMsg2(langu, "Company is required", "請填上公司。");
			return false;
		}
		if (!$("#email").val()) {
			valid=false;			
			highlightInput($("#email"));
			showResponseMsg2(langu, "The Email field must not be left blank.", "請填上電郵地址。");
			return false;
		}
		
		if (!isEmail($("#email").val())) {
			valid=false;			
			highlightInput($("#email"));
			showResponseMsg2(langu, "The Email you entered is not in a valid format.", "您輸入的電郵地址格式有誤。");
			return false;
		}

    	if ($("#disclaimer").length > 0) {
			if($("#disclaimer").prop('checked') == false){
				valid=false;			
				showResponseMsg2(langu, "Please accept the above Disclaimer by checking the checkbox", "請選擇同意以上的免責聲明。");
				return false;
			}
        }
		if($("#pic").prop('checked') == false){
			valid=false;			
			showResponseMsg2(langu, "Please accept the above Personal Information Collection Statement.", "請選擇同意個人資料的收集及使用。");
			return false;
		}

		if (valid) {
			$("#WebcastRegistrationForm").submit();
		}
	});
	
	$("#WebcastRegistrationForm").submit(function(e)
	{
		console.log("start submit");
		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");
		var langu = $("#Language").val();
      	var webcasturl = $("#webcastloc").val();
		console.log("data:" + JSON.stringify(postData));
		console.log("type:" + $("#Type").val());

		$.ajax(
		{
			url : formURL,
			type: "Post",
          	data: {
              "data": JSON.stringify(postData),
              "type": $("#Type").val(),
              "language": langu, 
              "webcastloc": webcasturl  
            },
			success:function(data, textStatus, jqXHR) 
			{
              	var legalchoice = Cookies.get('legalchoice');
              	if (legalchoice !== 'Y') {
					Cookies.set('legalchoice', 'Y', {
                      expires: 1/12
                    });
                }
				
				console.log("langu:" + langu);
              	if (langu === '1') {
					window.open(webcasturl, 'livepop', 'menubar=0,toolbar=no,location=no,status=no,scrollbars=no,resizable=no,height=850,width=1200,top=0,left=0');
                  	parent.location.href= '/investors/financial-results/index.page?locale=en';
                } else {
					window.open(webcasturl, 'livepop', 'menubar=0,toolbar=no,location=no,status=no,scrollbars=no,resizable=no,height=850,width=1200,top=0,left=0');
                  	parent.location.href= '/investors/financial-results/index.page?locale=zh';
                }
				
			},
			error: function(jqXHR, textStatus, errorThrown) 
			{
				alert("submit failed");

			}
		});
	    formSubmitted=true;
	    e.preventDefault();	//STOP default action
	});	
	
		
});
		
function isEmail(s) {
			//var re = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
      var re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
			var is_email=re.test(s);
			return is_email;
}

function isContactNumber (s) {
			var re = /^(?!999)[1-9]([0-9]{3})[- ]?([0-9]{4})$/;
			var is_cn=re.test(s);
			return is_cn;
	
}

function isContactNumber2 (s) {
			var re = /^[\w\-\s\(\)\+]+$/;
			var is_cn=re.test(s);
			return is_cn;
	
}

function isContactNumber3 (s) {
			var re = /^(?!999)(\+?\d{1,}[- ]?)((\d*[- ]*)*)(\d)$/;
			var is_cn=re.test(s);
			return is_cn;
}

function isAccountNumber (s) {
			var re = /^[0-9]+$/;
			var is_an=re.test(s);
			return is_an;
}

function isWebsiteURL (s) {
			var re = /^(http\:\/\/|https\:\/\/)?([a-z0-9][a-z0-9\-]*\.)+[a-z0-9][a-z0-9\-]*$/;
			var is_url=re.test(s);
			return is_url;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars(s) {
    var vars = [], hash;
    var hashes = s.slice(s.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function updateEmailForm() {
		var investor_val = $("input[name='investor']:checked").val();
		$('#investor_type').val(investor_val);
		var shareholder_val = $("input[name='shareholder']:checked").val();
		$('#pccw_shareholder').val(shareholder_val);
		var customer_val = $("input[name='customer']:checked").val();
		$('#pccw_customer').val(customer_val);
		if ($('#hkt_financial_results').is(':checked')) {
			$('#financial_results').val('Y');
		} else {
			$('#financial_results').val('N');
		}
		if ($('#hkt_financial_results').is(':checked')) {
			$('#financial_results').val('Y');
		} else {
			$('#financial_results').val('N');
		}
		if ($('#hkt_announcements').is(':checked')) {
			$('#announcements').val('Y');
		} else {
			$('#announcements').val('N');
		}
		if ($('#hkt_presentations').is(':checked')) {
			$('#presentations').val('Y');
		} else {
			$('#presentations').val('N');
		}
		if ($('#hkt_shareholder_circulars').is(':checked')) {
			$('#shareholder_circulars').val('Y');
		} else {
			$('#shareholder_circulars').val('N');
		}
		if ($('#hkt_press_releases').is(':checked')) {
			$('#press_releases').val('Y');
		} else {
			$('#press_releases').val('N');
		}
		$('#email').val($('#hkt_email').val());
		
}

function updateMediaForm() {
			
		var title_val = $("input[name='hkt_title']:checked").val();
		$('#title').val(title_val);
		
		$('#given_name').val($('#hkt_given_name').val());
		$('#surname').val($('#hkt_surname').val());
		$('#organization').val($('#hkt_organization').val());
		$('#position').val($('#hkt_position').val());
		$('#email').val($('#hkt_email').val());
		$('#country').val($('#hkt_country').val());
		$('#web_url').val($('#hkt_web_url').val());
		$('#contact_number').val($('#hkt_contact_number').val());
		
}


function updateServiceRelocationForm() {
			
		var title_val = $("input[name='hkt_title']:checked").val();
		$('#title').val(title_val);
		
		$('#given_name').val($('#hkt_given_name').val());
		$('#surname').val($('#hkt_surname').val());
		$('#contact_number_day').val($('#hkt_contact_number_day').val());
		$('#contact_number_night').val($('#hkt_contact_number_night').val());
		$('#email').val($('#hkt_email').val());
		$('#fixed_line_service_no').val($('#hkt_fixed_line_service_no').val());
		$('#eye_service_no').val($('#hkt_eye_service_no').val());
		$('#netvigator_acc_no').val($('#hkt_netvigator_acc_no').val());
		$('#nowtv_acc_no').val($('#hkt_nowtv_acc_no').val());
		$('#new_installation_address').val($('#hkt_new_installation_address').val());
		$('#new_bill_address').val($('#hkt_new_bill_address').val());
		$('#new_correspondence_address').val($('#hkt_new_correspondence_address').val());
		$('#preferred_effective_date').val($('#hkt_preferred_effective_date').val());
		$('#additional_information').val($('#hkt_additional_information').val());

		if($('#hkt_contact_time_day').is(':checked')) {
			$('#contact_time_day').val("day");
			$('#contact_time_night').val("");			
		}
		if($('#hkt_contact_time_night').is(':checked')) {
			$('#contact_time_day').val("");
			$('#contact_time_night').val("evening");			
		}
		
}

function updateFeedbackForm() {
			
		var title_val = $("input[name='hkt_title']:checked").val();
		$('#title').val(title_val);
		
		$('#given_name').val($('#hkt_given_name').val());
		$('#surname').val($('#hkt_surname').val());
		$('#contact_number_day').val($('#hkt_contact_number_day').val());
		$('#contact_number_night').val($('#hkt_contact_number_night').val());
		$('#email').val($('#hkt_email').val());
		$('#message').val($('#hkt_message').val());
		
}