// JavaScript Document
function reqType(){
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	return req;
}


function sendVote(poststr){
	var httpRequest = reqType();
	httpRequest.onreadystatechange = processRequest;
	httpRequest.open('POST', 'vote.jsp', false);
	httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	httpRequest.send(poststr);
	function processRequest() {
	if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				alert("您神聖的一票已送出\n感謝您~");
				//var dvalue = httpRequest.responseText;
				return true;
			} else {
				alert("failed");
				return false
			}
		}
	}
}

function getVote(poststr){
	var httpRequest = reqType();
	httpRequest.onreadystatechange = processRequest;
	httpRequest.open('POST', 'result.jsp', false);
	httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	httpRequest.send(poststr);
	function processRequest() {
	if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				var dvalue = httpRequest.responseText;
			} else {
				return false
			}
		}
	}
	var dvalue = httpRequest.responseText;
	return dvalue;
}

function voteThis(st_id){
	var opt = document.getElementsByName("vote_option");
	if(GetCookie("ov_"+st_id) != 0){
		alert("您已經投過了~");
	}else{
		for(var i = 0 ; i < opt.length ; i ++){
			if(opt[i].checked){
				sendVote("st="+st_id+"&ov="+opt[i].value);
				document.cookie = "ov_"+st_id+"=yes";
			}
			document.getElementById("ov_"+opt[i].value).innerHTML = getVote("st="+st_id+"&ov="+opt[i].value) + "人";
		}
	}
	
}

function GetCookieVal(offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return 0;
}

// 金牌猜猜樂

function guessThis(st_id){
	var opt = document.getElementsByName("vote_option");

		var guessDiv = document.getElementById("guess");
		var optval = "";
		var check = 0;
		for(var i = 0 ; i < opt.length ; i ++){
			if(opt[i].checked){
				optval = opt[i].value;
				check++;
			}
		}
		if ( check == 0 ) {
				alert("請選擇項目!!");
				return;
		}
		if ( GetCookie("guess") != 0 ){
			checkGuess(st_id, optval, GetCookie("guess"));
			return;
		}
		//guessDiv.innerHTML = "";
		var _table = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" "
							 + "style=\"background-image: url(images/frame_sports_05.gif); background-repeat: repeat-x; background-position: center top;\" "
							 + "valign=\"top\">\n"
							 + "<tbody>\n"
							 + "<tr>\n"
							 + "<td align=\"left\">請輸入下列資料,以參加猜猜樂抽獎活動</td>\n"
							 + "</tr>\n"
							 + "<tr>\n"
							 + "<td align=\"left\" class=\"titleLink\">您的身份證字號\n"
							 + "<input type=\"text\" name=\"pid\" id=\"pid\" /></td>\n"
							 + "</tr>\n"
							 + "<tr>\n"
							 + "<td align=\"left\" class=\"titleLink\">您的聯絡E-mail\n"
							 + "<input type=\"text\" name=\"email\" id=\"email\" /></td>\n"
							 + "</tr>\n"
							 + "<tr>\n"
							 + "<td align=\"right\" height=\"25\"><input type=\"image\" onclick=\"checkGuess("+st_id+", "+optval+" , '0');\" src=\"images/btn_send.gif\" /></td>\n"
							 + "</tr>\n"
							 + "</tbody>\n";
		guessDiv.innerHTML = _table;

}

function reguess() {
		var guessDiv = document.getElementById("guess");
		var _table = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" "
							 + "style=\"background-image: url(images/frame_sports_05.gif); background-repeat: repeat-x; background-position: center top;\" "
							 + "valign=\"top\">\n"
							 + "<tbody>\n"
							 + "<tr>\n"
							 + "<td height=\"50\" class=\"titleLink\" align=\"left\"><strong>本項目您已經投過囉，歡迎至其它項目投票</strong></td>\n"
							 + "</tr>\n"
							 + "</tbody>\n"
							 + "</table>\n"
		guessDiv.innerHTML = _table;
}

function checkGuess(st_id, optval, id) {
		if ( id != 0 ) {
			sendGuess("st="+st_id+"&sn="+optval+"&id="+id);
			return;
		}
		var pid = document.getElementById("pid").value;
		var email = document.getElementById("email").value;
		if ( !pid ) {
				alert("請填寫身份證字號!!")
				return;
		} else if ( !IDcheck(pid) ) {
				alert("請填寫正確的身分證字號格式!!");
				return;
		} else if ( !email || !checkMailAddr(email) ) {
				alert("請填寫正確的E-mail格式!!");
				return;
		} else {
			sendGuess("st="+st_id+"&sn="+optval+"&pid="+pid.toUpperCase()+"&email="+email);
		}
}

function sendGuess(poststr){
		var httpRequest = reqType();
		httpRequest.onreadystatechange = processRequest;
		httpRequest.open('POST', 'guessAdd.jsp', 'false');
		httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		httpRequest.send(poststr);
		function processRequest() { 
			if (httpRequest.readyState == 4) {
					if (httpRequest.status == 200) {
						var id = trim(httpRequest.responseText);
						if ( id >= 1 ) {
								if ( GetCookie("guess") == 0 && id != 404 ) {
									document.cookie = "guess="+id;
								}
								alert("你的資料已成功送出\n還可以投其它項目噢~\n投越多得獎機會越高喲!");
								document.location = 'guess.jsp';
						} else if ( id == 0 ) {
								reguess();
						} else {
								alert("failed");
								return false;
						}
						return true;
					} else {
						alert("failed");
						return false;
					}
			}
		}
}

//身分證號檢查
function IDcheck(pid){
  if (pid.length == 10) {
	var compstr,pos,checksum;
	checksum = 0;
	cmpstr="ABCDEFGHJKLMNPQRSTUVXYWZIO";
	pos=cmpstr.indexOf((pid.charAt(0)).toUpperCase());
	if(pos == -1 || isNaN(pid.substring(1,9)))
	   return false;
	else   {
		pos = pos + 10;
		checksum = Math.floor(pos/10) + (pos - (Math.floor(pos/10)*10) )*9;
		var checksum = checksum+8*parseInt(pid.charAt(1))+7*parseInt(pid.charAt(2))+6*parseInt(pid.charAt(3))+5*parseInt(pid.charAt(4))+4*parseInt(pid.charAt(5))+3*parseInt(pid.charAt(6))+2*parseInt(pid.charAt(7))+1*parseInt(pid.charAt(8))+1*parseInt(pid.charAt(9));
		var check1 = parseInt(checksum/10);
		var check2 = checksum/10;
		var check3 = (check2-check1)*10;
		if (checksum == check1*10) { return true; }
		else {
		  if (parseInt(pid.charAt(9)) == (10-check3)) { return true; }
		  else { return false; }
		}
	}
  }
  else
	return false;
}

function checkMailAddr(dstText)
{
	data = dstText.match(/^\S+@\S+\.\S+$/);
	//data = dstText.match(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9])+$/)
	if (!data || !dstText) return false;
	return true;
}

function trim(strvalue) {
        ptntrim = /(^\s*)|(\s*$)/g;
        var val = strvalue.replace(ptntrim,"");
        return val;
}