function setdate(date) {
	window.location = "reserve.php?date="+date;
}
//below is ajax
var xmlhttp;
if(window.ActiveXObject) {
	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} else {
	xmlhttp = new XMLHttpRequest;
}
function changeCap(what) {
	var params = "changecap=yes";
	xmlhttp.open("POST", "ajax.php", true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) {
			what.innerHTML = "處理中...";
		} else {
			what.innerHTML = "改變驗證碼";
			document.getElementById('cap').src = "verify.php?" + new Date().getTime();
		}
	}
	xmlhttp.send(params);
}
function killbox() {
	var blackout = document.getElementById('killall');
	var loginbox = document.getElementById('login_box_here');
	var float = document.getElementById('float');
	blackout.style.position = "absolute";
	blackout.style.opacity = "1";
	blackout.style.width = "0px";
	blackout.style.height = "0px";
	blackout.style.zIndex = "-1";
	blackout.style.background = "transparent";
	blackout.style.visibility = "hidden";
	
	loginbox.style.visibility = "hidden";
	loginbox.style.zIndex = "-2";
	float.style.visibility = "hidden";
	float.style.zIndex = "-3";
	//document.getElementById('num_4').focus();
}
function phone() {
	var num_1 = document.getElementById('num_1').value;
	var num_2 = document.getElementById('num_2').value;
	var num_3 = document.getElementById('num_3').value;
	var num_4 = document.getElementById('num_4').value;
	var pnum = num_1 + num_2 + num_3 + num_4;
	var params = "phone="+pnum;
	xmlhttp.open("POST", "ajax.php", true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) {
		} else {
			if(xmlhttp.responseText != "") {
				var thisobj = document.getElementById('killall');
				var loginbox = document.getElementById('login_box_here');
				var float = document.getElementById('float');
				if(window.innerWidth && window.innerHeight) { //ff
					var x = parseInt((window.innerWidth/2)/3);
					//var y = parseInt((window.innerHeight/2));
					var winX = "100%";
					var winY = parseInt(window.innerHeight)+(window.scrollMaxY);
				} else { //ie
					var x = parseInt((document.documentElement.clientWidth/2)/3);
					//var y = parseInt((document.documentElement.clientHeight/2));
					var winX = parseInt(document.documentElement.clientWidth)+"px";
					var winY = document.body.parentNode.scrollHeight;
				}
				thisobj.style.visibility = "visible";
				thisobj.style.position = "absolute";
				thisobj.style.top = "0";
				thisobj.style.left = "0";
				thisobj.style.opacity = ".7";
				thisobj.style.filter = "alpha(opacity=70)";
				thisobj.style.background = "black";
				thisobj.style.width = winX;
				thisobj.style.height = winY + "px";
				thisobj.style.zIndex = "100";
				loginbox.innerHTML = xmlhttp.responseText;
				document.getElementById('password').focus();
				loginbox.style.visibility = "visible";
				loginbox.style.position = "relative";
				loginbox.style.top = x+"px";
				loginbox.style.zIndex = "101";
				float.style.zIndex = "102";
				float.style.visibility = "visible";
				float.style.position = "relative";
			}
		}
	}
	xmlhttp.send(params);
}
var shit;
function checkpass() {
	var obj = document.getElementById('password');
	var num_1 = document.getElementById('num_1').value;
	var num_2 = document.getElementById('num_2').value;
	var num_3 = document.getElementById('num_3').value;
	var num_4 = document.getElementById('num_4').value;
	var pnum = num_1 + num_2 + num_3 + num_4;
	var params = "password="+obj.value+"&phone="+pnum;
	xmlhttp.open("POST", "ajax.php", true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) {
			obj.disabled = true;
		} else {
			obj.disabled = false;
			if(xmlhttp.responseText != "ng") {
				killbox();
				document.getElementById('p_add').innerHTML = "<input type=\"hidden\" name=\"password\" value=\""+document.getElementById('password').value+"\" />";
				shit = "yes";
				document.getElementById('add').innerHTML = "<p>登入成功!</p>";
				var i;
				for(i=1;i<=4;i++) {
					document.getElementById('num_'+i).removeAttribute("onblur");
				}
				document.getElementById('delete_name').style.display = "none";
				document.getElementById('amnt_people').focus();
				return true;
			} else {
				obj.select();
				obj.style.background = "red";
				obj.onkeydown = function() {
					obj.style.background = "";
				}
				shit = "no";
				return false;
			}
		}
	}
	xmlhttp.send(params);
}
function checkform(what) {
	var name = document.getElementById('name');
	var num_1 = document.getElementById('num_1') != null ? document.getElementById('num_1').value : "";
	var num_2 = document.getElementById('num_2') != null ? document.getElementById('num_2').value : "";
	var num_3 = document.getElementById('num_3') != null ? document.getElementById('num_3').value : "";
	var num_4 = document.getElementById('num_4') != null ? document.getElementById('num_4').value : "";
	var pnum = num_1 + num_2 + num_3 + num_4;
	var amnt_people = document.getElementById('amnt_people');
	if(((name != null && (name.value != "" || shit == "yes" && document.getElementById('delete_name').style.display == "none")) || name == null) && ((pnum != null && pnum != "09") || pnum == null) && amnt_people.value != "" && document.getElementById('error').innerHTML == "") {
		if(document.getElementById('password') != null) {
			checkpass();
			if(shit == "no") {
				alert("請輸入正確的密碼!");
				return false;
			}
		}
		document.getElementById('reserve_form').submit();
	} else {
		alert("請正確的填完表單!");
	}
}
function cancel(date,uid,ordernum,kill,what) {
	var a = confirm("確定取消 " + date + " 的訂位?");
	if(a == false) {
		return false;
	}
	var params = "uid="+uid+"&ordernum="+ordernum+"&date="+date;
	xmlhttp.open("POST", "ajax.php", true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) {
			
		} else {
			document.getElementById(kill).innerHTML = "<strong>已取消</strong>訂位";
		}
	}
	xmlhttp.send(params);
}
function clickad() {
	var params = "killad=true";
	xmlhttp.open("POST", "ajax.php", true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) {
			return false;
		} else {
			return true;
		}
	}
	xmlhttp.send(params);
}