$(document).ready(function()
	{	
		var orgLogin = $('#txtLogin').val();
		var orgPassword = $('#txtPassword').val();
		
		
		
		
		$('#contentlink').mouseover(function()
				{
					$(this).css({cursor: 'pointer'});	
				}
		);
	
		
		
		
		$('#btnSearch').mouseover(function()
			{
				$(this).css({cursor: 'pointer'});	
				$(this).attr("src", "../images/searchGlass_1.gif");
			}
		);
		
		$('#btnSearch').mouseout(function()
			{				
				$(this).attr("src", "../images/searchGlass_0.gif");
			}
		);
		
		$('#btnSearch').click(function()
			{
				/*
				var opt = $('#searchDropDown').val();
				var val = $.trim($('#mainSearchText').val());
				
				if (val == "") {
					alert("請輸入搜尋的關鍵字!");
					return;
				} else if (!isSpecialChar(val)){
					alert("搜尋的關鍵字,不接受標點符號!");
					return;
				} else {
					mainSearch(opt, encodeURI(val));
				}
				*/
				mainSearch();
			}
		);	
		
		
		$('#btnLogin').click(function()
			{				
				var img_src = $(this).attr("src");
				if (img_src.indexOf("_logout") >= 0) {
					$('#loginBox').hide();
					//var servername = $('#servername').val();					
					//window.location = "/template/registration/jsp/_logout_prg.jsp";
					//window.location = "/template/dnews/jsp/toc_main.jsp";					
					$('#logoutForm').submit();
					
				} else {
					$('#loginBox').show();
					dropdowncontent.init("contentlink", "right-bottom", 1, "click");
					
					$('#loginErrMsg').text("");
					
					var loginCookie = $.trim($('#txtLoginCookie').val());					
					if (loginCookie == "") {
						$('#txtLogin').val("");
						$('#txtPassword').val("");
						$('#lblCleanCookie').text("");
						$('#storeDiv').html("<input type='checkbox' name='store' id='store' value='1' /><label id='storeText'>在此電腦記住我的電郵及密碼。</label>");
					} else {
						$('#txtLogin').val(orgLogin);
						$('#txtPassword').val(orgPassword);
						$('#storeDiv').html("");
						$('#lblCleanCookie').text("使用不同電郵登入。");						
					}
				}				
			}
		);
		
		$('#btnLoginSubmit').click(function()
			{
				mainLogin();
			}
		);
		
		$('#lblCleanCookie').mouseover(function()
			{
				$(this).css({color: '#000000',cursor: 'pointer'});				
			}
		);
		
		$('#lblCleanCookie').mouseout(function()
			{
				$(this).css({color: '#0071bc'});				
			}
		);
		
		$('#lblCleanCookie').click(function()
			{
				var servername = $('#servername').val();
				$.post(servername + "/template/common/jsp/_clean_cookie.jsp",
					function(data) {
						$('#txtLogin').val("");
						$('#txtPassword').val("");
						$('#txtLoginCookie').val("");
						
						$('#lblCleanCookie').text("");
						$('#storeDiv').html("<input type='checkbox' name='store' id='store' value='1' /><label id='storeText'>在此電腦記住我的常用電郵。</label>");	
					}					
				);				
			}
		);
		
		$('#mainSearchText').keydown(function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				
				if (key == 13) {
					//Enter Key Code
					mainSearch();
				}				
			}
		);
		
		$('#txtPassword').keydown(function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				
				if (key == 13) {
					//Enter Key Code
					mainLogin();
				}				
			}
		);
		
	}
);

function isSpecialChar(data){
	var iChars = "!@#$%^&*()+=[]\\\';,/{}|\":<>?~"; 
	for (var i = 0; i < data.length; i++) {
		if (iChars.indexOf(data.charAt(i)) != -1) {
			return false;
		}
	}
	return true;
}


function mainSearch() {	
	var opt = $('#searchDropDown').val();
	var val = $.trim($('#mainSearchText').val());
	
	if (val == "") {
		alert("請輸入搜尋的關鍵字!");
		return;
	} else if (!isSpecialChar(val)){
		alert("搜尋的關鍵字,不接受標點符號!")
		return;
	}
	
	var category = "";
		category = opt;
	var value = "";
		value = encodeURI(val);
	
	if (category == "dnews") {
		window.location = "/template/dnews/jsp/dnews_search_results.jsp?txtSearch=" + value;
	} else if (category == "forum") {
		window.location = "/template/forum/php/search.php?txtSearch=" + value;
	} else if (category == "chosir") {
		window.location = "/template/cho/jsp/cho_search_results.jsp?txtSearch=" + value;
	} else if (category.indexOf("archive_") >= 0) {
		window.location = "/template/archive/common/jsp/archive_search_results.jsp?txtSearch=" + value + "&archive_id=" + category.substring(8, category.length);
	}
}


function readCookie(cookieValue) {
	var name = "testHKEJCookie";
	
	document.cookie = name + '=' + cookieValue + '; path=/';	
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function mainLogin() {
	var login = $('#txtLogin').val();
	var password = $.trim($('#txtPassword').val());
	
	if (login == "" || password == "") {
		alert("請輸入用戶/訂戶電郵或密碼。");
		return;
	}
	
	var cookieValue = "hkej_cookie_test";
	
	var test_cookie = readCookie(cookieValue);	
	if (test_cookie != cookieValue) {
		alert("請設定閣下的瀏覽器允許寫入 Cookie!");
		return;
	}
	
	$.post("/template/landing/jsp/_main_login_prg.jsp",
		{login: login, password: password},
		function(data)
		{
			var rtn = $.trim(data);
		
			if (rtn == "success") {
				$('#loginForm').submit();
			} else {				
				$('#txtLogin').val("");
				$('#txtPassword').val("");
				$('#loginErrMsg').text("輸入的用戶/訂戶電郵或密碼錯誤，請重新輸入");
			}
		}
	);
}
