function checkFrame(tv_path) {
	//alert (parent.name);

	if (!parent.spot) {
	//if ( (parent.name != 'Window123') || (parent.name != 'spot') ) {
		//alert ("should redirect");
		//alert(tv_path + '?URL=' + document.location.href);
		document.location.href = tv_path + '?URL=' + document.location.href;

	}
}

//-------------------------------------------------------------------------------------------------------

function blanks_only(pString) {
	return ( chk_Match(pString,/^[\s]*$/,false) );
}

function trim(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}

function TrimAllTextFields(ThisForm) {
//alert ("in TrimAllTextFields");
//alert ("in TrimAllTextFields.   ThisForm.elements.length is " + ThisForm.elements.length);
	for (i = 0; i < ThisForm.elements.length; i++) {
		if (ThisForm.elements[i].type == "text" || ThisForm.elements[i].type == "file") {
//alert ("ThisForm.elements[i].value " + ThisForm.elements[i].value);
			ThisForm.elements[i].value = trim(ThisForm.elements[i].value);
		}
	}
}

function chk_Match(pObj, pRE_AcptPatn, pAllowBlank) {
	if ( ! ( pAllowBlank  &&  pObj.length == 0 ) ) {
		if ( ! pRE_AcptPatn.test(pObj) ) {
//			pObj.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------------------------------------------
// For news frame scrolling

var scrollSpeed = 5;
var scrollTimer = null;

function scrollNs( value ) {
	scrollHeight = -1 * (document.layers["realtimeRankingLayerNS"].document.layers["mainDiv"].clip.height - 150);
	if ( value < 0 && document.layers["realtimeRankingLayerNS"].document.layers["mainDiv"].top > scrollHeight ) {
		document.layers["realtimeRankingLayerNS"].document.layers["mainDiv"].moveBy(0,value);
	}
	else if ( value > 0 && document.layers["realtimeRankingLayerNS"].document.layers["mainDiv"].top < 0 ) {
		document.layers["realtimeRankingLayerNS"].document.layers["mainDiv"].moveBy(0,value);
	}
	else clearInterval(scrollTimer);
}
if (document.layers) {
	funcScrollUp = 'scrollNs(scrollSpeed)';
	funcScrollDown = 'scrollNs(-scrollSpeed);';
}
else if (document.getElementById || document.all) {
	funcScrollUp = 'window.frames["realtimeRankingLayer"].scrollBy(0,-scrollSpeed)';
	funcScrollDown = 'window.frames["realtimeRankingLayer"].scrollBy(0,scrollSpeed)';
}

function rankingScrollUp() {
	scrollTimer = setInterval(funcScrollUp,10);
}
function rankingScrollDown() {
	scrollTimer = setInterval(funcScrollDown,10);
}
function rankingScrollStop() {
	clearInterval(scrollTimer);
}

// End - For news frame scrolling
//-------------------------------------------------------------------------------------------------------

function doPoll(poll_action, total_ans) {
var i = 1;
var checkok = false;

	for (i = 0; i < total_ans; i++) {
		if (document.poll_form.answer[i].checked) {
			checkok = true;
		}
	}

	if ( (checkok) || (poll_action == 'view') ) {
		window.name = 'BaseWin';
		win = window.open('','poll','resizable=1,scrollbars=1,width=450,height=350'); win.focus();
		document.poll_form.poll_action.value = poll_action;
		document.poll_form.submit();
	} else {
		alert("Please select one");
	}

}

//-------------------------------------------------------------------------------------------------------
// Email Address Checking

/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address.
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */

//	alert("Email address seems incorrect (check @ and .'s)");
	alert("請輸入完整的電郵地址.");
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
//		alert("Ths username contains invalid characters.");
		alert("請輸入完整的電郵地址.");
		return false;
	}
}
for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
//		alert("Ths domain name contains invalid characters.");
		alert("請輸入完整的電郵地址.");
		return false;
	}
}

// See if "user" is valid

if (user.match(userPat)==null) {

	// user is not valid

//	alert("The username doesn't seem to be valid.");
	alert("請輸入完整的電郵地址.");
	return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

	// this is an IP address

	for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
//			alert("Destination IP address is invalid!");
			alert("請輸入正確電郵地址.");
			return false;
		}
	}
	return true;
}

// Domain is symbolic name.  Check if it's valid.

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
//		alert("The domain name does not seem to be valid.");
		alert("請輸入正確電郵地址.");
		return false;
	}
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
//	alert("The address must end in a well-known domain or two letter " + "country.");
	alert("請輸入正確電郵地址.");
	return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//	alert("This address is missing a hostname!");
	alert("請輸入正確電郵地址.");
	return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//-------------------------------------------------------------------------------------------------------

function doTellFriend() {
	if (!document.tell_friend.recommand.checked && !document.tell_friend.newsletter.checked) {
		alert("請選擇 '推介此頁' 或 '訂閱電子刊物'");
		document.tell_friend.recommand.focus();
		return false;
	}
	if (emailCheck(document.tell_friend.email)) {
//	if (checkEmail(document.tell_friend.email)) {
		document.tell_friend.submit();
		document.tell_friend.recommand.checked = false;
		document.tell_friend.newsletter.checked = false;
		document.tell_friend.email.value = '';
	}
}

//-------------------------------------------------------------------------------------------------------

function clearValue(obj) {
	//alert (obj.name);
	obj.value = '';
}

//-------------------------------------------------------------------------------------------------------

function newin2 (url)
	{
		db123Window = window.open(url,"db123Window","toolbar=no,width=550,height=600,directories=no,status=no,resizable=no,menubar=no,scrollbars=yes");
		db123Window = db123Window.focus();
	}

//-------------------------------------------------------------------------------------------------------

function SetFocus() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i=0; i<field.length; i++) {
      if ( (field.elements[i].type != "image") &&
           (field.elements[i].type != "hidden") &&
           (field.elements[i].type != "reset") &&
           (field.elements[i].type != "submit") ) {

        document.forms[0].elements[i].focus();

        if ( (field.elements[i].type == "text") ||
             (field.elements[i].type == "password") )
          document.forms[0].elements[i].select();

        break;
      }
    }
  }
}

//-------------------------------------------------------------------------------------------------------

// Checking login email and password
function login_check() {

	//the following code is for trial accounts, gnci1001 - gnci9999
//	if ( chk_Match(document.login.input_Email.value,/^GNCI1[0-3][0-9][1-9]$/,false) )  {
	if ( document.login.input_Email.value >= 'GNCI1001' || document.login.input_Email.value <= 'GNCI1300' )  {
//	if (!(chk_Match(document.login.input_Email.value,/^GNCI1[0-2][0-9][0-9]$/,false) &&
//			document.login.input_Email.value <> 'GNCI1300'))  {
		if ( chk_Match(document.login.input_Password.value,/^[\s]*$/,false) || document.login.input_Password.length == 0)  {
			alert("請輸入密碼.");
			document.login.input_Password.select();
			return false;
		}
		return true;
	}

	if (emailCheck(document.login.input_Email.value)) {
//		if (document.login.input_Password.value.length == 0) {
//			alert("請輸入密碼.");
//			document.login.input_Password.select();
//			return false;
//		}
		if ( chk_Match(document.login.input_Password.value,/^[\s]*$/,false) )  {
			alert("請輸入密碼.");
			document.login.input_Password.select();
			return false;
		}
	} else {
		document.login.input_Email.select();
		return false;
	}

}

//-------------------------------------------------------------------------------------------------------

//function isEmailAddress (string) {
//var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
//	return addressPattern.test(string);
//}

//function checkEmail (field) {
//	if (!isEmailAddress(field.value)) {
//		alert('請輸入正確的電郵地址');
//		field.focus();
//		field.select();
//		return false;
//	}
//	return true;
//}

//-------------------------------------------------------------------------------------------------------


function isDate (year, month, day)
{
	// Original post: Dan Osborne <dano@specialist.co.uk>
	//                4/26/1999
	//                JavaScript <javascript@LaTech.edu>
	// Modified by:   Walter Torres <walter@torres.ws>
	//                4/29/2001

	// This is an interesting way to do validation of dates!

	// First, decrement the given month;
	// since humans think in base ONE and
	// JavaScript dates for months think in base ZERO.
	//      Human month range : 1 - 12
	// JavaScript month range : 0 - 11
	month--;

	// Then, create a new JS Date Object based upon given values.
	// NOTICE: This will convert 2/32/2000 to 3/3/2000
	//         JavaScript 'helps' you like this.
	var objTempDate = new Date(year,month,day);

	// Now, tear that new Data Object apart and see if the
	// values it gives back matches what we gave it.
	return	( ( objTempDate.getFullYear() == year  ) &&
			  ( objTempDate.getMonth()    == month ) &&
			  ( objTempDate.getDate()     == day)  )  ? true : false

	// Very nice method Dan!
}

function isTime (intHour, intMinute, intSecond)
{
	// Baased upon:   Dan Osborne <dano@specialist.co.uk>
	//                4/26/1999
	//                JavaScript <javascript@LaTech.edu>
	// Modified by:   Walter Torres <walter@torres.ws>
	//                4/29/2001

	// This is an interesting way to do validation of Time!

	// Create a new JS Date Object based upon given values.
	var objTempTime = new Date( 0, 0, 1, intHour, intMinute, intSecond );

	// Now, tear that new Data Object apart and see if the
	// values it gives back matches what we gave it.
	return	( ( objTempTime.getHours()   == intHour   )  &&
			  ( objTempTime.getMinutes() == intMinute ) &&
			  ( objTempTime.getSeconds() == intSecond ) ) ? true : false
}

//-------------------------------------------------------------------------------------------------------

function LoginMessage(msg_code) {

	switch (msg_code){
		case "1" :
			alert ("對不起，您沒有登入。請登入或登記成為新會員。");
			document.login.input_Email.select();
			break;
		case "2" :
			alert ("對不起，您登入失敗。請再嘗試登入 或 登記成為新會員。");
			document.login.input_Email.select();
			break;
		case "3" :
//			alert ("對不起，您登記的教會指示碼錯誤, 請再向 貴教會查詢");
			alert ("對不起，您的個人戶口並未輸入 或 輸入有誤的教會指示碼，請向 貴教會查詢。");
			break;
	}

}

//-------------------------------------------------------------------------------------------------------

function SortThis(col) {
	document.sorting.new_sort_col.value = col;
	document.sorting.submit();
}
