var C_ADD = "a";
var C_QUERY = "q";
var C_VIEW = "v";
var C_LIST = "l";
var C_DOWNLOAD = "dl";

var C_OK = "0";
var C_INFO = "6";
var C_INVALID = "7";
var C_CONFIRM = "8";
var C_ERROR = "9";

var record_obj = null;

var selected_iv = "";
var selected_id = "";

$(document).ready(function () {
	if($('#txtCustSchedDelivery').val()!="") {
		$('#schedule_table_body tr').find("td[id='"+$('#txtCustSchedDelivery').val()+"']").css("background-color", "#66b3ff");
	}
	if(new Date() > new Date($('#txtCustSchedDeliveryCutOff').val())) {
		$('#overlay-expired').removeClass('d-none');
	}else{
		$('#overlay-expired').addClass('d-none');
	}
});

function selectTimeShedule(obj, selected_time){
	selected_iv = "";	
	$('#schedule_table_body tr td').each(function() {
		if($(this).attr('disabled')) {
		} else {
			$(this).css("background-color", "");
		}
	});
	
	if($(obj).attr('disabled')) {
	} else {		
		$(obj).css("background-color", "#66b3ff");
		selected_iv = selected_time;
		selected_id = obj.id;
	}
}

function openCommentModal() {
	$('#commentModal').show();
}

function clearEntry() {
	// $('#txtPositionTitle').val("");
	// $('#txtName').val("");
	// $('#txtMobileNo').val("");
	// $('#txtComment').val("");
	// record_obj = null;

	loadSchedule();
}
function addRecord(id) {
	saveRecord(C_ADD);
}
function saveRecord(type) {
	if (validateRecord()) {
		selected_time = selected_id.replace('td_schedule_time', '');

		var data = {
			"type"			: type,
			"id"			: $('#txtCustSchedId').val(),
			"name"			: $('#txtName').val(),
			"deliveryid"	: selected_time.split('_')[1],
			"deliverytime"	: selected_time.split('_')[0],
			"remarks"		: $('#txtComment').val(),
			"coy_id"		: $('#txtCustSchedCoyID').val(),
			"gift_id"		: $('#txtCustSchedGiftID').val(),
			"giftname"		: $('#txtGiftName').val(),
			"gifttype"		: $('input[name="gift-type"]:checked').val(),
		};
		var url = "submitschedule.pzx?c=" + add_url + "&d=" + new Date().getTime();
		callServer(url, "json", data, onResponse, record_obj);
	}
}
function checkRecord() {
	var data = {
		"type"	: C_QUERY,
		"phone"	: $('#txtMobileNo').val(),
	};
	var url = "submitschedule.pzx?c=" + add_url + "&d=" + new Date().getTime();
	callServer(url, "json", data, onResponse, record_obj);
}
function loadSchedule() {
	var data = {
		"type": C_LIST,
		"phone": $('#txtMobileNo').val(),
	};
	var url = "submitschedule.pzx?c=" + add_url + "&d=" + new Date().getTime();
	callServer(url, "json", data, onResponse, record_obj);
}
function onResponse(obj, resp) {
	if(resp.status != C_OK){
		alert(resp.mesg);
		loadSchedule();
		return;
	}

	if (resp.status == C_OK) {
		if (resp.type == C_ADD) {
			alert(resp.mesg);
			location.reload();
		} else if (resp.type == C_QUERY) {
			if(resp.mesg != "")
				alert(resp.mesg);
		} else if (resp.type == C_LIST) {
		}
	}
}
function validateRecord() {
	if ($('#txtPositionTitle').val() == "") {
		alert("Position Title cannot be blank!");
		$('#txtPositionTitle').focus();
		return false;
	}
	if ($('#txtName').blank()) {
		alert("Name cannot be blank!");
		$('#txtName').focus();
		return false;
	}
	// if ($('#txtMobileNo').val() == "") {
	// 	alert("Mobile Number cannot be blank!");
	// 	$('#txtMobileNo').focus();
	// 	return false;
	// }
	//check if mooncake type is selected
	if (!$('input[name="gift-type"]:checked').val()) {
		gift_name = $('#txtGiftName').html();
		alert("Please select " + gift_name + " Type!");
		return false;
	}
	if(selected_iv == ""){
		alert("Please select preferred Delivery Date and Time!");
		return false;
	}

	return true;
}

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
    return false;
};