$(document).ready(function(){
						  
	$("input[name=Username]").click(function(){
		if($(this).val()=='Username'){
			$(this).val('');
			$(this).css('color','#000000');
		}
		$(this).blur(function(){
			if($(this).val().length<=0){
				$(this).val('Username');
				$(this).css('color','#999999');
			}					  
		});
	});
	
	$("input[name=Username]").keypress(function(e){
		if(e.which==13){
			var Username=$("input[name=Username]").val();
			var Password=$("input[name=Password]").val();
			Login(Username,Password);
		}
	});
	
	$("input[name=Password]").click(function(){
		if($(this).val()=='Password'){
			$(this).val('');
			$(this).css('color','#000000');
		}
		$(this).blur(function(){
			if($(this).val().length<=0){
				$(this).val('Password');
				$(this).css('color','#999999');
			}					  
		});
	});
	
	$("input[name=Password]").keypress(function(e){
		if(e.which==13){
			var Username=$("input[name=Username]").val();
			var Password=$("input[name=Password]").val();
			Login(Username,Password);
		}
	});
	
	$("input[name=btn-login]").click(function(){
		var Username=$("input[name=Username]").val();
		var Password=$("input[name=Password]").val();
		Login(Username,Password);						  
	});
	
	var Login=function(Username,Password){
		if(Username.length<=0 || Username.indexOf('Username')!=-1){
			alert('กรุณาระบุ Username ในรูปแบบของ Email เช่น example@hotmail.com');
			$("input[name=Username]").val('');
			$("input[name=Username]").focus();
		}else if(Password.length<=0 || Password.indexOf('Password')!=-1){
			alert('กรุณาระบุ Password 6 ตัวอักษรขึ้นไป');
			$("input[name=Password]").val('');
			$("input[name=Password]").focus();
		}else{
			$.ajax({
				url: "/admin/mains/ajax_login_act/",
				type: "POST",
				cache: false,
				dataType: "html",
				data: {
					Username: Username,
					Password: Password
				},
				beforeSend: function(){
					$("#login-result").css('color','#000000');
					$("#login-result").html('กำลังตรวจสอบข้อมูล การเข้าสู่ระบบ...');
				},
				error: function(){
					$("#login-result").css('color','#CC0000');
					$("#login-result").html('เกิดข้อผิดพลาด ไม่สามารถเชื่อมต่อกับฐานข้อมูลได้');
					alert('ERROR : /app/webroot/shopping/js/main.js : Login function');
				},
				success: function(text){
					$("#login-result").html('');
					if(text.indexOf('true')!=-1){
						var tmp=text.split('|');
						$("#login-result").css('color','#008000');
						$("#login-result").html('กำลังพาท่าน   เข้าสู่ระบบ');
						window.location.href='/admin/';
					}else{
						$("#login-result").css('color','#CC0000');
						$("#login-result").html('ไม่สามารถเข้าสู่ระบบได้ กรุณาตรวจสอบข้อมูล');
					}
				}
			});	
		}
	};

	$("#forgot-pass").click(function(e){
		e.preventDefault();
		$.openDOMWindow({
			borderSize:0,
			width:332,
			height:220,
			windowSource:'iframe',
			windowPadding:0,
			modal:0,
			overlayOpacity:60,
			loader:1,
			loaderHeight:20,
			loaderWidth:20,
			loaderImagePath:'/images/employer-loader.gif',
			windowSourceURL:'/indexs/forgetpass/'
		});							 
	});
	
	// ++++++++++++++++ Box  Search +++++++++++++++++++++++++++++
		if($.trim($("#Keyword").val())==""){
			$("#Keyword").val("คำค้นหา");
		}else{
			$("#Keyword").css("color",'#000000');
		}
		
		if($("#CatId").val()==""){
			$("#CatId").css("color",'#999999');
		}else{
			$("#CatId").css("color",'#000000');
		}
		
		
		$("#Keyword").focus(function(){
			var Keyword = $.trim($(this).val());
			if(Keyword=="คำค้นหา"){
				$(this).val("");
			}
			$(this).css("color",'#000000');
		});
		
		$("#Keyword").blur(function(){
			var Keyword = $.trim($(this).val());
			if(Keyword==""){
				$(this).val("คำค้นหา");
				$(this).css("color",'#999999');
			}
		});
		
		$("#CatId").change(function(){
			var CatId = $(this).val();
			if(CatId==""){
				$(this).css('color','#999999');
			}else{
				$(this).css('color','#000000');
			}
		});
		
		$("#frmSearch").submit(function(){
			var Keyword = $.trim($("#Keyword").val());
			if(Keyword=='คำค้นหา'){
				Keyword="";	
			}
			var CatId = $("#CatId").val();
			var url = "/search/index/PageId:1/Keyword:"+Keyword+"/CatId:"+CatId;
			window.location = url;
			return false;
		});
		
	// ++++++++++++++++END  Box  Search +++++++++++++++++++++++++++++
});
