﻿/*=====================================================
check 表單是否填寫完整
=====================================================*/
function checkAdLogin(fobj){
	if (LenCheck(fobj.UserName,'用戶名稱',6,20) && LenCheck(fobj.Password,'密碼',4,20))
	{
		fobj.action = "login.php";
		fobj.method = "POST";
		return true;
	}
	else 
	{
		return false;
	}
}

function checkChangeAdPwd(fobj){
	if (LenCheck(fobj.UserName,'用戶名稱',6,20) && LenCheck(fobj.Password,'密碼',4,20) && LenCheck(fobj.NewPassword,'新密碼',6,8) && LenCheck(fobj.NewPasswordRetype,'確認密碼',6,8) && PasswordCheck(fobj.NewPassword, fobj.NewPasswordRetype))
	{
		fobj.action = "admin.php";
		fobj.method = "POST";
		return true;
	}
	else 
	{
		return false;
	}
}

function checkEmail(fobj){
	if (LenCheck(fobj.Name,'聯絡人姓名',2,52) && LenCheck(fobj.Telephone,'電話',2,20) && LenCheck(fobj.Email,'電郵地址',6,64) && EmailCheck(fobj.Email)  )
	{
		fobj.action = "online_enquiry.php";
		fobj.method = "POST";
		fobj.theKEY.value = "formWEB";
		return true;
	}	
	else 
	{
		return false;
	}
}


//檢查是否為正確的email
function EmailCheck(emailobj)
{
	//var pattern = /^(\w+[\.\-]?)?\w+@([\w]+\.)+[a-zA-Z]+$/;
	var pattern = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	if (pattern.test(emailobj.value) == true)
	{
		return true;
	}
	else if(emailobj.value.length==0)
	{
		alert("電郵：請輸入有效的電郵以便回覆");
		emailobj.focus();
		return false;
	}
	else
	{
		alert("電郵：請輸入有效的電郵以便回覆");
		emailobj.focus();
		return false;
	}
}




//檢查文字長度
function LenCheck(fobj,filename,minlen,maxlen){
    str = fobj.value;
    if(str.length == 0)
    {
		alert("必須輸入：" + filename);
		fobj.focus();
		return false;
    }
    if(minlen != "")
    {    
		if(str.length < minlen)
		{
			alert(filename + " 的長度不可少於 " + minlen + " 個字元");
			fobj.focus();
			return false;
		}
	}
	if(maxlen != "")
	{
		if(str.length > maxlen)
		{
			alert(filename + " 的長度不可超過 " + maxlen + " 個字元");
			fobj.focus();
			return false;
		}
	}

	return true;
}

//測試字串是否只包含數值與英文
function AsciiCharOnly(fobj,filename)			//with symbol too
{
    var pattern = /^\w\S+$/;
    
    if (pattern.test(fobj.value) == true)
    {
		return true;
    }
    else
		alert(filename + " Only Characters, Numbers and Symbols are allowed."); 
		fobj.focus();
		return false;
}

//檢查是否為空
function BlankCheck(obj,msg)
{ 
	var pattern = /^\s+$/;
		if(obj.value == "" || pattern.test(obj.value))
	{
		alert(msg);
		obj.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function PasswordCheck(fobj1, fobj2)
{
	var pass1 = fobj1.value;
	var pass2 = fobj2.value;
	if(pass1 == pass2) 
		{ return true; }
	else {
		alert("新密碼 及 確認密碼 不一樣！");
		fobj2.focus();
		return false;
	}
}

function confirmDelete(name)
{
var agree=confirm('確定刪除產品 ['+name+']？');
if (agree)
	return true ;
else
	return false ;
}

function confirmDeletePhoto(name, itemsym, fobj)
{
var agree=confirm('確定刪除 ['+name+']圖片？');
if (agree) {
	fobj.Action.value = "DelPhoto";
	fobj.Item.value = itemsym;
	return true ;
}
else
	return false ;
}

