﻿String.prototype.trim = function() {
    //return this.replace(/^\s+|\s+$/g, "");
    return trim17(this);
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}


var _paba_mail_host = "pabafoundation.org";

function donateToProject() 
{
    var objAmount = document.getElementById("txtDonateAmount");
    var sValue = objAmount.value;
    var objSource;
    var bValidAmount = isValidDonationAmount(sValue);

    if (bValidAmount) 
    {
        if (Number(sValue) < 1.00) 
        {
            bValidAmount = false;
            alert("Please specify a donation amount of at least $1.00.");
        }
        else 
        {
            objSource = document.getElementById("_post_source");
            objSource.value = "DonateToProject";
            document.forms[0].submit();
        }
    }
    else {
        alertInvalidDonation();
    }

    if (!bValidAmount) {
        objAmount.select();
    }
}


function alertInvalidDonation() 
{
    alert("Please enter a valid donation amount.");
}

function isValidDonationAmount(sValue) 
{
    var bResult = false;

    sValue = sValue.trim().replace("$","");

    if (sValue.length == 0 || isNaN(sValue)) 
    {
        //do nothing
    }
    else 
    {
        if (sValue.indexOf(".") < 0) 
        {
            bResult = true;
        }
        else if (sValue.search(/,/) < 0)
	    {
	        if (sValue.search(/^\s*\d+(\.\d\d?)?$/) == 0) 
	        {
	            bResult = true; 
	        }
		}
		else
	    {
	        if (sValue.search(/^\s*\d{1,3}(,\d{3})+(\.\d\d?)?$/) == 0)
		    {
		        bResult = true;
		    }
    		
	    }
	}

	return bResult;
}

function createEmailLink(sEmail) 
{
    document.write("<a href=" + "mail" + "to:" + sEmail + "@" + _paba_mail_host + ">" +  sEmail + "@" + _paba_mail_host + "</a>")
}


function trim17(str){
    //source: http://yesudeep.wordpress.com/2009/07/31/even-faster-string-prototype-trim-implementation-in-javascript/
    var len = str.length;
    if (len){
        var whiteSpace = String.whiteSpace, i = 0;
        while (whiteSpace[str.charCodeAt(--len)]);
        if (++len){
            while (whiteSpace[str.charCodeAt(i)]){ ++i; }
        }
        str = str.substring(i, len);
    }
    return str;
}




function validateFieldLength(source, nMinLength) 
{
    var bResult = false;
    var sValue;

    if (source != null) 
    {
        bResult = 
        sValue = source.value;
        if (sValue.trim().length >= nMinLength) 
        {
            bResult = true; 
        }
    }


    return bResult;

}
