
//var CT_TF_ENV_ = "";
var APP_VERSION = "";
var RATE_MSG_MAX = 300;
var LINE_URL_QRCODE = "https://lin.ee/fEn93mB";
var LINE_AT_ID = "@Linebroadcast  (มี @ นำหน้า)";
var TF_WEB_STORE_URL = "https://chrome.google.com/webstore/detail/cactusent-tf-smart-fb-fan/pmimhkkkopgnkalpdehhihddnpbeeobi?hl=en";
var TL_WEB_STORE_URL = "https://chrome.google.com/webstore/detail/cactusent-tl-line-oa-smar/bnfenogpggpfjgledfkiadijlianjbpp/related?hl=en";
var OL_WEB_STORE_URL = "https://chrome.google.com/webstore/detail/smart-lineoa-messaging-v1/naoapjikdgnoepcaphmlfdjcbmbflfii?hl=th";
var MAIN_WEB_PAGE_URL = "https://linebroadcast.com";
var MANUAL_DOC_LINK_URL = "https://docs.google.com/presentation/d/1Q2eDcqsL7s2YdvpI_TSLB-gPBZFxiOOB7or_DN_HtPs/edit#slide=id.p1";
var MANUAL_VIDEO_LINK_URL = "https://www.youtube.com/watch?v=AD1BKbYJ-sA";
var ENABLED_FB_PAGE_DONE_OLD_ARCHIVED = "";
//var ENABLED_FB_PAGE_DONE_OLD_ARCHIVED = "tags=action:archived|เรียบร้อยแล้วเก่ากว่า 3 ปี";
var ENABLED_FB_ALL_PAGES = "folder=all|ทุกโฟล์เดอร์"

//var CT_TF_REQ_AUTH_URL = MAIN_WEB_PAGE_URL + "/tlmember/v4"+"{env}"+"/";
var CT_TF_REQ_AUTH_URL = MAIN_WEB_PAGE_URL  + "/tlmember/v7"+"{env}"+"/";
var CT_TF_REQ_UNLOCK_URL = MAIN_WEB_PAGE_URL + "/deviceunlockreq/";

var IS_SHOW_MOVE_TO_DONE_FOLDER_DIV = true;
var IS_SHOW_GET_FB_FANPAGE_BTN = true;
var IS_SHOW_SPEED_LIMIT_OPTION = false;
var IS_SHOW_TF_HEADER_VERSION = true;
var IS_SHOW_TF_RND_MSGSEND_HEADER = false;
var IS_SHOW_TF_RND_MSGSEND_FOOTER = false;
var IS_SHOW_TF_LOGIN_PWD_AUTH_MGMT = true;
var CT_TF_FORGOT_PASSWORD_PATH =MAIN_WEB_PAGE_URL+"/userauth?tab=forgetpwd";
var CT_TF_CHANGE_PASSWORD_PATH =MAIN_WEB_PAGE_URL+"/userauth?tab=changepwd";

var CT_TF_MY_POLICY = MAIN_WEB_PAGE_URL+"/openline/tools/policy/fbpolicy.html";
var CT_TL_MY_POLICY = MAIN_WEB_PAGE_URL+"/openline/tools/policy/oapolicy.html";

var LOGIN_EMAIL = "";

function contact_detail(title){
	let caption = "ติดต่อแอดมิน";
	let cap2 = caption;
	if(typeof title!="undefined" && title!=null){
		caption = title;
	}
	let linelink='<a href="' + LINE_URL_QRCODE + '" target="_blank" style="color: rgb(176, 176, 176);"><i class="far fa-comment-dots">  ' + LINE_URL_QRCODE + '</i></a>'
	Swal.fire({
		title: caption,
		//type: 'info',
		html: '<h4>'+cap2+'หรือเพิ่มเพื่อนได้ที่ ' + LINE_AT_ID + '</h4>'+linelink+'<p class="mdQrCode"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6AQAAAACgl2eQAAABsElEQVR4Xu2Y0W3DMAxEBWgAj+TVNZIHMMDy3blpEhTpbw8woRo29X4OPFJKR32ONd4zb3EDjhtw3IBDwBod2zq3Osd+jjGPcZLyXgzQf+e26tgbm1WGnQ8CWlQDfgLopbFAoF8Q2EDrTQYWeVsuEChbrkpbLbZ+9+T/BlraoFmeFinvpQCO9dDorSubA8hyDDHnJJyZHAXwWa2RJIs6bTVfLBcAeLcYYteaxz6lOghgAh+jsaHZxa7gOODRMlOTudRHWYDd5U4paofxni0XAaDRE8CkJHsa5ACqEdcSHYvqfS3gIODUae720UDb7bcsQPMKpxWSGcs8fywXAfCiawnNQr00vjycgwC5iyFg47VIjWWwJKBDt0TLZOsY/BJJA3Adq/PmyWcBJYNxoOC3Re1ei5UCuDTemmSERQGMYhWoSzYklqciB/h+bUACTwQ+y4wAFqp0oF+VkvFeRnECUBwoaFTJ1mU51SsIOHVXHIyywdO7T1f3IEBPOmjqiHxp/yBAh4iuKEtNRAQBJcvxJbFTl0b6KArAZ/3JP9Wv87F5jvsk4FPcgOMGHDfg+Bv4An8Upb44G4WCAAAAAElFTkSuQmCC" alt="@Line"></p>',
	});
}
function manual_detail(){
	Swal.fire({
		title: 'วีดีโอ/คู่มือ การใช้งาน',
		//type: 'info',
		html: `<ul>
				<li><a href="${MANUAL_VIDEO_LINK_URL}" target="_blank" style="color: rgb(176, 176, 176);">วีดีโอการใช้งาน</a></li>
				<li><a href="${MANUAL_DOC_LINK_URL}" target="_blank" style="color: rgb(176, 176, 176);">คู่มือการใช้งาน</a></li>
			  </ul>`,
	});
}
function moreinfo_detail(){
	Swal.fire({
		title: 'รายละเอียดเพิ่มเติม',
		//type: 'info',
		html: `<ul>
				<li><a href="${MAIN_WEB_PAGE_URL}" target="_blank" style="color: rgb(176, 176, 176);">ข้อมูลผลิตภัณฑ์/โปรโมชั่นเพิ่มเติม</a></li>
			  </ul>`,
	});
}
function download_software(){
	Swal.fire({
		title: 'ซอฟแวร์ดาวโหลด/อัพเกรด',
		//type: 'info',
		html: `<ul>
				<li><a href="${TF_WEB_STORE_URL}" target="_blank" style="color: #ffff00;background-color:#000;"><div><h4 style="padding:10px;color: #ffff00;background-color:#000;">คลิ๊กที่นี่เพื่ออัพเกรด</h4></div></a></li>
			  </ul>`,
		showCancelButton: true,
		confirmButtonColor: '#3085d6',
		cancelButtonColor: '#d33',
		confirmButtonText: 'Yes'
	}).then((result) => {
		if (result.value) {
			var mywinUrl = window.open(TL_WEB_STORE_URL, '_blank');
			mywinUrl.focus();
		}
	});
}

function header_detail(){
	
}

function header_top_tf_main_menu(){
	//tf_promotion.js
	let menu_promotion="";
	let currentDate = new Date();
	let day = currentDate.getDate();
	let month = currentDate.getMonth() + 1;
	let year = currentDate.getFullYear();
	if(year>2560){year-=543;}
	let curdate = ymdToDate(year+"/"+month+"/"+day);
	
	//console.log("curdate "+curdate);
	
	if(PROMO_START.trim()!=""){	//promotion.js
		//console.log(PROMO_START);
		let promote_start = ymdToDate(PROMO_START);
		//console.log("promote_start "+promote_start);
		if(promote_start.getTime()<=curdate.getTime()){
			menu_promotion=`<button class="btn-menu" id="btn_menu_promotion_fb_fp" style="margin-right:0px;background-color:#33ff00;"><img src="app-assets/img/menu-promo.png" id="menu_promotion_img_fb_fp" width="48" height="32" alt="promotion"><i class="button-promotion"></i>โปรโมชั่น</button>`;
		}
	}
	if(PROMO_END.trim()!=""){	//promotion.js
		//console.log(PROMO_END);
		let promote_end = ymdToDate(PROMO_END);
		//console.log("promote_end "+promote_end);
		if(curdate.getTime()>promote_end.getTime()){
			menu_promotion="";
		}
	}
	
	let detail=`<i id="device_id_tag_no" style="width:fit-content;margin-left:0px;margin-right:15px;color:white;text-shadow: 0.1em 0.1em 0.2em yellow;display:none;"></i>
				<button class="btn-menu" id="btn_menu_manual_fb_fp" style="margin-top:-30px;margin-bottom:10px;"><img src="app-assets/img/menu-manual.png" id="menu_manual_img_fb_fp" width="48" height="32" alt="manual"><i class="button-manual"></i> วิธีใช้งาน</button>
				<button class="btn-menu" id="btn_menu_contact_fb_fp" style="margin-right:0px;"><img src="app-assets/img/menu-contact.gif" id="menu_contact_img_fb_fp" width="32" height="32" alt="contact"><i class="button-contact"></i> ติดต่อแอดมิน</button>
				<button class="btn-menu" id="btn_menu_moreinfo_fb_fp" style="margin-right:0px;"><img src="app-assets/img/menu-moreinfo.png" id="menu_moreinfo_img_fb_fp" width="32" height="32" alt="more info"><i class="button-moreinfo"></i> ข้อมูลเพิ่มเติม</button>
				<button class="btn-menu" id="btn_menu_downloadapp_fb_fp" style="margin-right:0px;display:none;"><img src="app-assets/img/menu-download-app.png" id="menu_downloadapp_img_fb_fp" width="32" height="32" alt="download app"><i class="button-download-app"></i> ดาวน์โหลด</button>
				<button class="btn-menu" id="btn_menu_config_fb_fp" style="margin-right:0px;display:none;"><img src="app-assets/img/menu-config.png" id="menu_config_img_fb_fp" width="32" height="32" alt="config"><i class="button-config"></i> ตั้งค่า</button>
				${menu_promotion}
				`;
	let myAnchor = document.getElementById("top_tf_main_menu");
	if(typeof myAnchor!=="undefined" && myAnchor!=null){
		let mySpan = document.createElement("div");
		mySpan.setAttribute('id', "top_tf_main_menu");
		mySpan.className = "col-12 right";
		mySpan.setAttribute('style', "text-align:right;margin-top:-80px;margin-right:20px;background-color:#0563af;z-index:0;");
		mySpan.innerHTML = detail;
		myAnchor.parentNode.replaceChild(mySpan, myAnchor);
	}
}

function footer_detail(){
	//APP_VERSION in pop.js
	let detail=`<small id="app_footer_caption" style="color: #b0b0b0;">CACTUS TF - Fanpage ${APP_VERSION} ©Linebroadcast</small>&nbsp;
				<a id="ref_web" href="https://linebroadcast.com/" target="_blank" style="color: rgb(176, 176, 176);">
					<i class="fas fa-globe"></i>
				</a>
				<a id="ref_face" href="https://www.facebook.com/linebroadcast.page/" target="_blank" style="color: rgb(176, 176, 176);">
					<i class="fab fa-facebook"></i>
				</a>
				<a id="ref_line" href="${LINE_URL_QRCODE}" target="_blank" style="color: rgb(176, 176, 176);">
					<i class="far fa-comment-dots"></i>
				</a>
				<small id="app_login_id" style="color: #b0b0b0;">${LOGIN_EMAIL}</small>&nbsp;`;
	let myAnchor = document.getElementById("footer_detail");
	if(typeof myAnchor!=="undefined" && myAnchor!=null){
		let mySpan = document.createElement("div");
		mySpan.setAttribute('id', "footer_detail");
		mySpan.className = "pl-0 col-11";
		mySpan.setAttribute('style', "height:25px;text-align:center;");
		mySpan.innerHTML = detail;
		myAnchor.parentNode.replaceChild(mySpan, myAnchor);
	}
}
function footer_remark(){
	//<p id="fremark1" style="color:#b90020">*** หมายเหตุ *** ข้อความส่งเฉลี่ยได้ไม่เกิน 22 bubbles ต่อนาที<br />เพื่อให้เป็นไปตามนโยบายของ Line ที่ได้กำหนดไว้ </p>
	let detail=`*** ในหนึ่งชั่วโมงไม่ควรเปิด-ปิดโปรแกรมและดึงลูกเพจถี่เกินไป<br />เป็นเหตุให้โดนล็อกแอคเค้าเฟสบุ๊กได้ `;
	let myAnchor = document.getElementById("fremark1");
	if(typeof myAnchor!=="undefined" && myAnchor!=null){
		let mySpan = document.createElement("p");
		mySpan.setAttribute('id', "fremark1");
		//mySpan.className = "pl-0 col-11";
		mySpan.setAttribute('style', "color:#b90020");
		mySpan.innerHTML = detail;
		myAnchor.parentNode.replaceChild(mySpan, myAnchor);
	}
}

function validate_user_err_info(){
	return 'user: <b>'+t+'</b>, <br />'
			+'<b style="color:red;">'+err_info+'</b><br />'
			+'<a href="' + LINE_URL_QRCODE + '" target="_blank" >ติดต่อ สอบถาม</a> <br />'
			+' หรือ •Line:' + LINE_AT_ID + ' หรือจากตัวแทนที่คุณสมัครใช้งาน<br />';
}
function validate_user_err_default(){
	return 'user: <b>'+t+'</b>, <br />' + '<b style="color:red;">รหัสผ่านหรืออีเมล์ คุณไม่ถูกต้อง หรือยังไม่ได้รับอนุญาติ! </b><br />'
			+'<a href="https://www.facebook.com/linebroadcast.page/" target="_blank" >ติดต่อ สอบถาม</a> <br />'
			+' หรือ •Line:' + LINE_AT_ID + ' หรือจากตัวแทนที่คุณสมัครใช้งาน<br />';
}

function login_footer_container(){
	let detail = "";
	let this_pwd_mgmt_style="display:inline-block;";
	if(IS_SHOW_TF_LOGIN_PWD_AUTH_MGMT==false){
		this_pwd_mgmt_style="display:none;";
	}
	detail=`<div class="row">
					<div id="logmeincancel" class="col-4">
					  <button id="logmecancel" type="button" class="cancelbtnlogin">ยกเลิก</button>
					</div>
					<div id="passwordauthen" class="col-8" style="${this_pwd_mgmt_style}">
						<i class="fas fa-undo">&nbsp;<span id="logmeforgotpsw" class="psw"><a href="${CT_TF_FORGOT_PASSWORD_PATH}" target="_blank">&nbsp;ลืมรหัสผ่าน</a></span></i>
						<i class="fas fa-edit">&nbsp;<span id="logmechangepsw" class="psw"><a href="${CT_TF_CHANGE_PASSWORD_PATH}" target="_blank">&nbsp;เปลี่ยนรหัสผ่าน</a></span></i>
					</div>
				</div>`;
	let myAnchor = document.getElementById("login_footer_container");
	let mySpan = document.createElement("div");
	mySpan.setAttribute('id', "login_footer_container");
	mySpan.className = "containerlogin";
	mySpan.setAttribute('style', "background-color:#f1f1f1;");
	mySpan.innerHTML = detail;
	myAnchor.parentNode.replaceChild(mySpan, myAnchor);
}

function checkImage(imageSrc, good, bad) {
    var img = new Image();
    img.onload = good; 
    img.onerror = bad;
    img.src = imageSrc;
}

function show_promotion(){
	let promo_img = MAIN_URL+PROMOTION+"/promotion.png?version="+ (new Date().getTime());
	
	checkImage(promo_img, function(){
		Swal.fire({
			title: MESSAGE_TITLE,	//tf_promotion.js
			html: `<ul style="list-style-type:none;">
					<li><a href="${promo_img}" target="_blank"><img src="${promo_img}" id="menu_promotion_img_fb_fp" width="250" height="250" alt="promotion"></a></li>
					<br />
					<li><div><h4 style="padding:10px;color: #ffff00;background-color:#000;">${PROMO_DETAIL}</h4></div></li>
				  </ul>`,
			confirmButtonColor: '#3085d6',
			confirmButtonText: 'OK'
		});
	}, 
	function(){
		Swal.fire({
			title: MESSAGE_TITLE,	//tf_promotion.js
			html: `<ul style="list-style-type:none;">
					<li><img src="data:," id="menu_promotion_img_fb_fp" width="0" height="0"><br /><div><h4 style="padding:10px;color: #ffff00;background-color:#000;">${PROMO_DETAIL}</h4></div></li>
				  </ul>`,
			confirmButtonColor: '#3085d6',
			confirmButtonText: 'OK'
		});
	} );
	
	
	return true;
}

function validate_date(str_date){
	//y,m,d
	let ymd = str_date.split("/");
	let y = ymd[0];
	let m = ymd[1];
	let d = ymd[2];
	let ndate = new Date(y,m-1,d); 
	let con_date = ""+ndate.getFullYear() + (ndate.getMonth()+1) + ndate.getDate(); //converting the date
	let gdate = "" + y + m + d; //given date
	return ( gdate == con_date); //return true if date is valid
}

function convert_str_to_date(str_ymd_date){
	return str_date.toDate(ymd);
}
// Convert 'YYYY/MM/DD' to Date()
ymdToDate = function(ymd) {
  var d = ymd.split(/[\/\-\.]/, 3);

  if (d.length != 3) return null;

  // Check if date is valid
  var mon = parseInt(d[1]), 
      day = parseInt(d[2]),
      year= parseInt(d[0]);
  if (d[0].length == 2) year += 2000;
  if (day <= 31 && mon <= 12 && year >= 2015){
	try{
		return new Date(year, mon - 1, day);
	}catch(e){
		return null;
	}
  }
  return null; 
}

