
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)");
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.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
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.");
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!");
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.");
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.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}


function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
function isInteger(s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }

    // All characters are numbers.
    return true;
}
function   allTrim(ui){    
	  var   notValid=/\s/;    
	  while(notValid.test(ui)){    
	  ui=ui.replace(notValid,"");  
	  }    
	  return   ui;
}   
function isAlphabetic(val)
{
	val=allTrim(val);
	if (val.match(/^[a-zA-Z]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function checkMail(email){
	if ( email.value.indexOf('@') <0 )
	{
		return false;
	}
	return true;
}
//J's Mods 2011-05-17
function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}
// Checks that an input string is a decimal number, with an optional +/- sign character.
function isDecimal(s) {
	var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
	return String(s).search (isDecimal_re) != -1;
}
function checkEngOtherAmt(){
	var amountArray=MM_findObj("amount");
	if(amountArray[4].checked==true){
		//J;s Mods 2011-05-17
		//var otherAmt=MM_findObj("otherAmt");
		//if(!isInteger(otherAmt.value) || otherAmt.value==''){
		var otherAmt=document.getElementById('otherAmt').value;
		otherAmt=otherAmt.replace(/,/g,"");
		if(isDecimal(otherAmt) == false || otherAmt ==''){
			alert('Amount must be a numeric and must have only one decimal point.');
			return false;
		}else if(otherAmt < 50 || otherAmt > 10000){
			alert('Amount must between HKD50 to HKD10000.');
			return false;
		}
		otherAmt = roundNumber(otherAmt,2);
		document.getElementById('otherAmt').value = otherAmt;
		//End J's Mods 2011-05-17
	}
	return true;
}
//End J's Mods 2011-05-17
function checkEngFields(){
	if(!checkEngOtherAmt()){
		return false;
	}
	var eSurName=MM_findObj("eSurName");
	var eGivenName=MM_findObj("eGivenName");
	var phone=MM_findObj("phone");
	var age=MM_findObj("age");
	var hkid1=MM_findObj("hkid1");
	var hkid2=MM_findObj("hkid2");
	var address1=MM_findObj("address1");
	var address2=MM_findObj("address2");
	var address3=MM_findObj("address3");
	var email=MM_findObj("email");
	if(eSurName.value==''){
		alert('Please input your surname.');
		return false;
	}else if(!isAlphabetic(eSurName.value)){
		alert('Please input the correct alphabetical format for your surname.');
		return false;
	}
	if(eGivenName.value==''){
		alert('Please input your given name.');
		return false;
	}else if(!isAlphabetic(eGivenName.value)){
		alert('Please input the correct alphabetical format for your given name.');
		return false;
	}
	if(phone.value==''){
		alert('Please input your phone number.');
		return false;
	}
	/**
	if(age.value!=''){
		if(!isInteger(age.value)){
		alert('please input the right age.');
		return false;
		}
	}
	
	if(hkid1.value!=''){
       if(hkid2.value==''){
			alert('hkid must be input,please.');
			return false;
		}
	}
	*/
	if(address1.value==''){
		alert('Please input your address.');
		return false;
	}
	if(email.value=='' || emailCheck(email.value) == false){
		alert('Please input your email address. Use the correct format for email addresses.');
		return false;
	}
	if(!checkEngReceipt()){
		return false;
	}
	return true;
}
function checkEngReceipt(){
	var receiptArray=MM_findObj("receipt");
	if(receiptArray[0].checked==true){
		var recipient=MM_findObj("recipient");
		if(recipient.value==''){
			//J's Mods 2011-05-17
			//alert('recipient must be input,please.');
			//return false;
			document.getElementById('recipient').value = document.getElementById('eGivenName').value + ' ' + document.getElementById('eSurName').value;
			//End J's Mods 2011-05-17
		}
	}
	return true;
}
function formEngSubmit(){
	if(checkEngFields()){
		var amount = '';
		var amountArray=MM_findObj("amount");
		if(amountArray[4].checked==true){
			amount = document.getElementById('otherAmt').value;	
		}else{
			if (amountArray[0].checked){
				amount = amountArray[0].value;
			}
			if (amountArray[1].checked){
				amount = amountArray[1].value;
			}
			if (amountArray[2].checked){
				amount = amountArray[2].value;
			}
			if (amountArray[3].checked){
				amount = amountArray[3].value;
			}			
		}
		var churchDestination = '';
		var churchDestinationArray = MM_findObj("churchDestination");
		if (churchDestinationArray[0].checked){
			churchDestination = churchDestinationArray[0].value;
		}
		if (churchDestinationArray[1].checked){
			churchDestination = churchDestinationArray[1].value;
		}
		if (churchDestinationArray[2].checked){
			churchDestination = churchDestinationArray[2].value;
		}
		if (churchDestinationArray[3].checked){
			churchDestination = churchDestinationArray[3].value;
		}	
		var title = document.getElementById('title').options[document.getElementById('title').selectedIndex].value;
		var language = '';
		var languageArray = MM_findObj("language");
		if (languageArray[0].checked){
			language = languageArray[0].value;
		}
		if (languageArray[1].checked){
			language = languageArray[1].value;
		}
		var receipt = '';
		var receiptArray = MM_findObj("receipt");
		var recipient = '';
		if (receiptArray[0].checked){
			receipt = 'Yes';
			recipient = MM_findObj("recipient").value;
		}else{
			receipt = 'No';
			recipient = 'none';
		}
		var answer = 
			confirm(
				"Amount: " + amount + "\n" +
				"Church Destination: " + churchDestination + "\n" +
				"Title: " + title + "\n" +
				"Chinese Name: " + MM_findObj("cName").value + "\n" +
				"English Given Name: " + MM_findObj("eGivenName").value + "\n" +
				"English Surname: " + MM_findObj("eSurName").value + "\n" +
				"Phone No.: " + MM_findObj("phone").value + "\n" +
				"Address 1: " + MM_findObj("address1").value + "\n" +
				"Address 2: " + MM_findObj("address2").value + "\n" +
				"Address 3: " + MM_findObj("address3").value + "\n" +
				"Country: " + MM_findObj("country").value + "\n" +
				"Donor No.: " + MM_findObj("donorNo").value + "\n" +
				"E-mail Address: " + MM_findObj("email").value + "\n" +
				"Preferred Language of Communication: " + language + "\n" +
				"Receipt: " + receipt + "\n" +
				"Recipient (if Receipt is to be issued): " + recipient + "\n"
					);
		if (answer){
			MM_findObj('payInfo').submit();
		}	
	}
}
function checkCNOtherAmt(){
	var amountArray=MM_findObj("amount");
	if(amountArray[4].checked==true){
		//J's Mods 2011-05-17
		//var otherAmt=MM_findObj("otherAmt");
		var otherAmt=document.getElementById('otherAmt').value;
		otherAmt=otherAmt.replace(/,/g,"");
		//if(!isInteger(otherAmt.value) || otherAmt.value==''){
		if(isDecimal(otherAmt) == false || otherAmt ==''){
			alert('請您正確填上捐贈金額.');
			return false;
		}else if(otherAmt<50 || otherAmt>10000){
			alert('網上捐款額由港幣 $50 至 $10,000.');
			return false;
		}
		otherAmt = roundNumber(otherAmt,2);
		document.getElementById('otherAmt').value = otherAmt;
		//End J's Mods 2011-05-17
	}
	return true;
}
function checkCNFields(){
	if(!checkCNOtherAmt()){
		return false;
	}
	var eSurName=MM_findObj("eSurName");
	var eGivenName=MM_findObj("eGivenName");
	var phone=MM_findObj("phone");
	var age=MM_findObj("age");
	var hkid1=MM_findObj("hkid1");
	var hkid2=MM_findObj("hkid2");
	var address1=MM_findObj("address1");
	var address2=MM_findObj("address2");
	var address3=MM_findObj("address3");
	var email=MM_findObj("email");
	if(eSurName.value==''){
		alert('請您填上英文姓.');
		return false;
	}else if(!isAlphabetic(eSurName.value)){
		alert('請您填上正確的英文姓.');
		return false;
	}
	if(eGivenName.value==''){
		alert('請您填上英文名.');
		return false;
	}else if(!isAlphabetic(eGivenName.value)){
		alert('請您填上正確的英文名.');
		return false;
	}
	if(phone.value==''){
		alert('請您填上日間聯絡電話.');
		return false;
	}
	/**
	if(age.value!=''){
		if(!isInteger(age.value)){
		alert('請您正確填寫年齡.');
		return false;
		}
	}
	
	if(hkid1.value!=''){
		if(hkid2.value==''){
			alert('請您填完整香港身份證號碼.');
			return false;
		}
	}
	*/
	if(address1.value==''){
		alert('請您填上地址.');
		return false;
	}
	//J's Mods 2011-05-27
	if(email.value=='' || emailCheck(email.value) == false){
		alert('請您正確填寫電郵地址.');
		return false;
	}
	//End J's Mods 2011-05-27
	if(!checkCNReceipt()){
		return false;
	}
	return true;
}
function checkCNReceipt(){
	var receiptArray=MM_findObj("receipt");
	if(receiptArray[0].checked==true){
		var recipient=MM_findObj("recipient");
		if(recipient.value==''){
			//J's Mods 2011-05-17
			//alert('請您填寫抬頭人之英文名稱.');
			//return false;
			document.getElementById('recipient').value = document.getElementById('eGivenName').value + ' ' + document.getElementById('eSurName').value;
			//End J's Mods 2011-05-17
		}
	}
	return true;
}
function formCNSubmit(){
	if(checkCNFields()){
		var amount = '';
		var amountArray=MM_findObj("amount");
		if(amountArray[4].checked==true){
			amount = document.getElementById('otherAmt').value;	
		}else{
			if (amountArray[0].checked){
				amount = amountArray[0].value;
			}
			if (amountArray[1].checked){
				amount = amountArray[1].value;
			}
			if (amountArray[2].checked){
				amount = amountArray[2].value;
			}
			if (amountArray[3].checked){
				amount = amountArray[3].value;
			}			
		}
		var churchDestination = '';
		var churchDestinationArray = MM_findObj("churchDestination");
		if (churchDestinationArray[0].checked){
			churchDestination = churchDestinationArray[0].value;
		}
		if (churchDestinationArray[1].checked){
			churchDestination = churchDestinationArray[1].value;
		}
		if (churchDestinationArray[2].checked){
			churchDestination = churchDestinationArray[2].value;
		}
		if (churchDestinationArray[3].checked){
			churchDestination = churchDestinationArray[3].value;
		}	
		var title = document.getElementById('title').options[document.getElementById('title').selectedIndex].value;
		var language = '';
		var languageArray = MM_findObj("language");
		if (languageArray[0].checked){
			language = languageArray[0].value;
		}
		if (languageArray[1].checked){
			language = languageArray[1].value;
		}
		var receipt = '';
		var receiptArray = MM_findObj("receipt");
		var recipient = '';
		if (receiptArray[0].checked){
			receipt = '是';
			recipient = MM_findObj("recipient").value;
		}else{
			receipt = '不必';
			recipient = '無';
		}
		var answer = 
			confirm(
				"港幣 $: " + amount + "\n" +
				"捐赠: " + churchDestination + "\n" +
				"稱謂: " + title + "\n" +
				"中文姓名: " + MM_findObj("cName").value + "\n" +
				"英文姓: " + MM_findObj("eGivenName").value + "\n" +
				"英文名: " + MM_findObj("eSurName").value + "\n" +
				"日間聯絡電話: " + MM_findObj("phone").value + "\n" +
				"地址 1: " + MM_findObj("address1").value + "\n" +
				"地址 2: " + MM_findObj("address2").value + "\n" +
				"地址 3: " + MM_findObj("address3").value + "\n" +
				"國家: " + MM_findObj("country").value + "\n" +
				"捐款者號碼: " + MM_findObj("donorNo").value + "\n" +
				"電郵地址: " + MM_findObj("email").value + "\n" +
				"通訊語言選擇: " + language + "\n" +
				"收據: " + receipt + "\n" +
				"抬頭人之英文名稱: " + recipient + "\n"
					);
		if (answer){
			MM_findObj('payInfo').submit();
		}	
	}
}
