//**********************************************************************************
//
// routines.js - JavaScript routines for AFIM Donation Web Pages
//
// History:
// --------
// 27dec07,mjb		Updated chkDate() allow max starting donation of 1 year.
//			Updated setTrialPeriod() to switch to weeks for donations
//			  starting after 90 days.
//
//**********************************************************************************

var submitClicked = false;

//
// ***** onLoad handler.
//
function onLoad() 
    { 

    // Initialze main form.
    //
    var f = window.document.xForm;
    f.amount.value  = "";
    f.months.selectedIndex = 0
    f.date.value    = today ();
    f.date.disabled = true;
    f.amount.focus ();

    // Initialze paypal form.
    //
    var p = window.document.pForm;
    p.cmd.value	    = "";
    p.a1.value	    = "";
    p.p1.value	    = "";
    p.t1.value	    = "";
    p.a3.value	    = "";
    p.p3.value	    = "";
    p.t3.value	    = "";
    p.src.value	    = "";
    p.srt.value	    = "";
    p.sra.value	    = "1";
    p.amount.value  = "";
    return (true);
    };

//
// ***** Check valid date (mm/dd/yyyy).
//
function chkDate (tb)
    {
    var patDate = new RegExp ("^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})$", "");
    var val     = tb.value;
    var OK	= 1;

    if ((val == "") || (val == "mm/dd/yyyy"))
	{
	alert ('Plase enter a valid date.   ');
	OK = 0;
	}
    else if (!patDate.test(val))
	{
	alert ('Invalid date. Must be 2-digit month, 2-digit day, and 4-digit year ' +
		'(ie. 07/01/2008 for July 1, 2008).   ');
	OK = 0;
	}
    else
        {

        // Check month/date/year values.
        //
	var month = RegExp.$1 - 0;
	var day   = RegExp.$2 - 0;
	var year  = RegExp.$3 - 0;
	if (month == 1 || month ==  3 || month ==  5  || month ==  7 || 
	    month == 8 || month == 10 || month == 12)
	    {
	    if ((day < 1) || (day > 31))
		{
		alert ('Invalid day. Must be 01 - 31 for month specified.   ');
		OK = 0;
		}
	    }
	else if (month == 2)
	    {

	    // February - Check for leap year.
	    //
	    if ((year % 4) == 0)
	        {
		if ((day < 1) || (day > 29))	// Leap year.
		    {
		    alert ('Invalid day. Must be 01 - 29 for month specified.   ');
		    OK = 0;
		    }
	        }
	    else
	        {
		if ((day < 1) || (day > 28))	// Non-leap year.
		    {
		    alert ('Invalid day. Must be 01 - 28 for month specified.   ');
		    OK = 0;
		    }
		}
	    }
	else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
	    {
	    if ((day < 1) || (day > 30))
		{
		alert ('Invalid day. Must be 01 - 30 for month specified.   ');
		OK = 0;
		}
	    }
	else
	    {
	    alert ('Invalid month. Must be 01 - 12.');
	    OK = 0;
	    }

        if (OK == 1)
            {
            if (parseDate (tb.value) < parseDate (today()))
		{
		alert ('Invalid date. Please enter a "1st Monthly Donation" ' +
		       'date of today or later.  ');
		OK = 0;
		}

	    // Make sure the donation starts within a year.
	    //
	    var secs = parseDate (tb.value) - parseDate (today());
	    var secsPerDay = (1000*24*60*60);
	    var days = (secs / secsPerDay) - 1;
	    days = days + 0.5;
	    var patDays = new RegExp ("^([0-9]+).[0-9]+$", "");
	    patDays.test (days);
	    days = RegExp.$1 - 0;
	    if (days > 365)
	        {
		alert ('Invalid date. First donation must be within 1 year.   ');
		OK = 0;
	        }
	    }
	}

    if (OK == 0)
        {
        tb.focus ();
        if (tb.value != "")
            {
            tb.select ();
            }
	return (false);
	}

    return (true);
    }

//
// ***** Check valid dollar amount.
//
function chkAmount (tb)
    {
    var patMoney1 = new RegExp ("^\\s*\\$?\\s*([0-9]+)\.([0-9]{2})\\s*$", "");
    var patMoney2 = new RegExp ("^\\s*\\$?\\s*([0-9]+)\\s*$", "");
    var OK = 1;

    if (patMoney1.test(tb.value))
        {
	var dollars = RegExp.$1 - 0;
	var cents   = RegExp.$2 - 0;
	if ((dollars == 0) && (cents == 0))
	    {
	    alert ('Invalid value. Please enter non-zero donation amount.   ');
	    OK = 0;
	    }
        }
    else if (patMoney2.test(tb.value))
        {
	var dollars = RegExp.$1 - 0;
	if (dollars == 0)
	    {
	    alert ('Invalid value. Please enter non-zero donation amount.   ');
	    OK = 0;
	    }
        }
    else
	{
	alert ('Invalid value. Must be dollars and cents (e.g., 12.34) or ' +
	       'whole dollars (e.g., 35). May include leading dollar sign, but no spaces.   ');
	OK = 0;
	}

    if (OK == 0)
        {
        tb.focus ();
        if (tb.value != "")
            {
            tb.select ();
            }
	return (false);
	}

    return (true);
    }

function chkSubscribe ()
    {
    var f = window.document.xForm;
    if (f.months.selectedIndex > 1)
	{
	alert ("\nNOTICE: Selecting to donate more than one month will require that \n" +
	       "                you currently have or will create a PayPal account.\n\n");
	f.date.disabled = false;
	f.date.focus ();
	if (f.date.value != "") f.date.select ();
	}
    else
	{
	f.date.disabled = false;
	var temp = f.date.value;
	f.date.value = "";
	f.date.value = temp;
	f.date.disabled = true;
	}
    return true;
    }

function chkSubmit2Paypal()
    {
    if (submitClicked == false) return false;
    var f = window.document.xForm;

    // Make sure amount value has valid data.
    //
    if (!chkAmount (f.amount)) 
        {
        return (false);
        }

    // Make sure number of months selected.
    //
    if (f.months.selectedIndex == 0)
	{
	alert (" Please select the number of months you wish to " +
	       " continue this donation.  ");
	f.months.focus ();
	return false;
	}

    // Make sure donation date set.
    //
    if (f.months.selectedIndex > 1)
        {
	if ((f.date.value == "") || (f.date.value == "mm/dd/yyyy"))
	    {
	    if (f.months.selectedIndex == 1)
		{
		alert (" Please enter the date of your donation. ");
		}
	    else
		{
		alert (" Please enter the date of your first donation. ");
		}
		
	    if (f.date.value != "") f.date.select ();
	    f.date.focus ();
	    return false;
	    }
	if (!chkDate (f.date)) return (false);
        }

    // Fill in the paypal submit form.
    //
    var p = window.document.pForm;

    // Set the trial period if the donation date is > today.
    //
    setTrialPeriod   (f, p);
    setDonationInfo  (f, p);
    setRecurringInfo (f, p);

    // Submit the paypal form.
    //
    p.submit ();
    return true;
    }

function today ()
    {
    var date = new Date();
    return ((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear());
    }

function setTrialPeriod (f, p)
    {
    if (f.date)
        {
	var secsPerDay = (1000*24*60*60);

	// Get difference (in seconds) between today and donation date.
	//
	var secs = parseDate (f.date.value) - parseDate (today());

	// Calculate number of days between today and donation date.
	//
	var days = (secs / secsPerDay) - 1;
	days = days + 0.5;
	var patDays = new RegExp ("^([0-9]+).[0-9]+$", "");
	patDays.test (days);
	days = RegExp.$1 - 0;

	// Calculate number of weeks between today and donation date.
	//
	var weeks = days / 7;
	weeks = weeks + 0.5;
	patDays.test (weeks);
	weeks = RegExp.$1 - 0;

	// If days there, then setup the "trial" period.
	//
	if (days > 0)
	    {
	    p.a1.value = "0";

	    if (days <= 90)
	        {
		p.p1.value = days;
		p.t1.value = "D";
		}
	    else if (weeks <= 52)
	        {
		p.p1.value = weeks;
		p.t1.value = "W";
	        }
	    }
	}
    }

function parseDate (date)
    {
    var patDate = new RegExp ("^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})$", "");
    patDate.test(date);
    var MM = RegExp.$1 - 0;
    var DD = RegExp.$2 - 0;
    var YY = RegExp.$3 - 0;
    var date = new Date (YY, MM - 1, DD);
    return (date.getTime());
    }

function setDonationInfo (f, p)
    {

    // Strip optional leading $-sign and spaces. It doesn't matter if it's there for
    // the amount, but does matter when we generate the item_number
    // below.
    //
    var patMoney1 = new RegExp ("^\\s*\\$?\\s*([0-9]+\.[0-9]{2})\\s*$", "");
    var patMoney2 = new RegExp ("^\\s*\\$?\\s*([0-9]+)\\s*$", "");
    if (!patMoney1.test(f.amount.value))
        {
	patMoney2.test(f.amount.value);		// must be whole dollars.
	}


    // Build the donation information.
    //
    p.a3.value = RegExp.$1;
    p.p3.value = "1";
    p.t3.value = "M";
    }

function setRecurringInfo (f,p)
    {
    p.item_number.value = "Donation-" + p.a3.value;
    p.cmd.value		= "_xclick-subscriptions";
    p.amount.value	= "";

    if (f.months.selectedIndex > 1)
        {

	// Subscription.
	//
	p.src.value	= 1;
	p.srt.value	= f.months.selectedIndex;
	p.item_number.value 
			= p.item_number.value + "x" + f.months.selectedIndex;
	}
    else
        {

        // Non-subscription.
        //
        p.cmd.value	= "_xclick";
        p.amount.value	= p.a3.value;
        p.a3.value	= "";
        }
    }
