function previewPrayers()
{
	window.open('previewPrayers.php', 'previewPR', 'scrollbars=auto,toolbar=no,menubar=no,addressbar=no,resizable=yes');
}

function editPrayers()
{
	document.frmPrayers.action = "editPrayers.php";
	document.frmPrayers.submit();
}

function sendPrayers()
{
	document.frmPrayers.action = "sendPrayers.php";
	document.frmPrayers.submit();
}

function submitForm()
{
	theForm = document.frmsignup;
	if (validateForm())
	{
		theForm.submit();
	}
}

function validateForm()
{
	theForm = document.frmsignup;
	
	if (isEmpty(theForm.fname.value))
	{
		alert('Please enter the first name');
		theForm.fname.focus();
		return false;
	}

	if (isEmpty(theForm.lname.value))
	{
		alert('Please enter the last name');
		theForm.lname.focus();
		return false;
	}

	if (isEmpty(theForm.email.value))
	{
		alert('Please enter the email address');
		theForm.email.focus();
		return false;
	} else {

		if (!isValidEmailAddress(theForm.email.value))
		{
			alert('Please enter a valid email address');
			theForm.email.focus();
			return false;
		}
	}

	if (isEmpty(theForm.reconfEmail.value))
	{
		alert('Please enter the reconfirm email address');
		theForm.reconfEmail.focus();
		return false;
	} else {

		if (!isValidEmailAddress(theForm.reconfEmail.value))
		{
			alert('Please enter a valid reconfirm email address');
			theForm.reconfEmail.focus();
			return false;
		}
	}

	if (theForm.email.value != theForm.reconfEmail.value)
	{
		alert('Email address and the reconfirm email address should be the same. Please enter the email addresses again.');
		theForm.email.focus();
		return false;
	}

	return true;

}

function submitPrayer()
{
	theForm = document.frmprayerline;
	if (validatePrayer())
	{
		theForm.submit();
	}
}

function validatePrayer()
{
	theForm = document.frmprayerline;
	
	if (isEmpty(theForm.fullname.value))
	{
		alert('Please enter the full name');
		theForm.fullname.focus();
		return false;
	}

	if (isEmpty(theForm.email.value))
	{
		alert('Please enter the email address');
		theForm.email.focus();
		return false;
	} else {

		if (!isValidEmailAddress(theForm.email.value))
		{
			alert('Please enter a valid email address');
			theForm.email.focus();
			return false;
		}
	}

	if (isEmpty(theForm.phone.value))
	{
		alert('Please enter the phone number');
		theForm.phone.focus();
		return false;
	}

	if (isEmpty(theForm.prayer.value))
	{
		alert('Please enter the prayer');
		theForm.prayer.focus();
		return false;
	} 

	return true;

}

/**
 * Checks whether e-mail address provided is valid.
 * Validations for e-mail address are:
 * 1. Character '@' must exist and only once
 * 2. Character '.' must exist after '@'
 * 3. 2 to 4 characters must exist after last '.'
 * 4. Valid characters before '@' are - 
 *			'a' to 'z', 'A' to 'Z', '0' to '9', '.', '_'
 *
 * @param emailAddress e-mail address to be checked
 * @return true if e-mail address is valid, false otherwise
 */
function isValidEmailAddress(emailAddress)
{
    emailAddress = trimValue(emailAddress);
    atExists = emailAddress.indexOf("@");
    if (atExists != -1 && atExists != 0){
        afterAt = emailAddress.substring(atExists + 1);
        dotExists = afterAt.indexOf(".");
        if ((dotExists != -1) && (dotExists > 1)){
            afterDot = afterAt.substring(afterAt.lastIndexOf(".") + 1);
            afterDotLength = afterDot.length;
            if (afterDotLength >= 2 && afterDotLength <= 4){
                atExistsMoreThanOnce = emailAddress.lastIndexOf("@");
                if (atExistsMoreThanOnce == atExists){
                    emailAddressLength = emailAddress.length;
                    for (i=0; i < emailAddressLength; i++){
                        letter = emailAddress.charAt(i); 
                        if ((letter >= "a" && letter <= "z") ||
                            (letter >= "A" && letter <= "Z")  || 
                            (letter >= "0" && letter <= "9")  ||
                            (letter == ".") || 
                            (letter == "@") || 
                            (letter == "_")){
                            continue;
                        } else {
                            return false;
                        }
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * Checks whether value provided is empty.
 * First it trims the value and checks for the empty
 *
 * @param value any text value
 * @return true if the text value is empty, false otherwise
 */
function isEmpty(value)
{
    var re = /.+/;
    value = trimValue(value);
    if (value.match(re)) {
        return false;
    } else {
        return true;
    }
}

/**
 * Trims the text value that is supplied as parameter.
 *
 * @param value any text value
 * @return trimmed value
 */
function trimValue(s)
{
    var temp=''; 
    var temp1=''; 
    var ss=s;
    for(var i=0;i<=ss.length;++i) { 
        if(ss.charAt(i)!=' ') {
            temp=ss.substring(i);	
            break;
        }
    }
    if(temp=='') { 
        return '';
    } 
    var m=temp.length; m--;
    for(var j=m;j>=0;j--) {
        if(temp.charAt(j)!=' ') {
            j++; 
            temp1=temp.substring(0,j); 
            return temp1;
        }
    }
}
