/**
 * Email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function echeck(str)
{
    var at   = "@"
    var dot  = "."
    var lat  = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)

    if (lstr == 0)
    {
        return true;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
    {
        alert("Invalid E-mail address")
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
    {
        alert("Invalid E-mail address")
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1)
    {
        alert("Invalid E-mail address")
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2 ) == dot)
    {
        alert("Invalid E-mail address")
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1)
    {
        alert("Invalid E-mail address")
        return false
    }

    if (str.indexOf(" ") != -1)
    {
        alert("Invalid E-mail address")
        return false
    }

    return true
}

var daysInMonth = new Array();
daysInMonth["Jan"] = 31;
daysInMonth["Feb"] = 28;
daysInMonth["Mar"] = 31;
daysInMonth["Apr"] = 30;
daysInMonth["May"] = 31;
daysInMonth["Jun"] = 30;
daysInMonth["Jul"] = 31;
daysInMonth["Aug"] = 31;
daysInMonth["Sep"] = 30;
daysInMonth["Oct"] = 31;
daysInMonth["Nov"] = 30;
daysInMonth["Dec"] = 31;

function checkDates()
{
    var startday   = document.uplform.tripstartday.value
    var startmonth = document.uplform.tripstartmonth.value
    var startyear  = document.uplform.tripstartyear.value
    var endday     = document.uplform.tripendday.value
    var endmonth   = document.uplform.tripendmonth.value
    var endyear    = document.uplform.tripendyear.value

    var dayInStartMonth = daysInMonth[startmonth]
    var dayInEndMonth   = daysInMonth[endmonth]

    if ((startmonth == "Feb") && ((startyear == 2008) || (startyear == 2012) || (startyear == 2016)))
    {
        dayInStartMonth = 29;
    }

    if ((endmonth == "Feb") && ((endyear == 2008) || (endyear == 2012) || (endyear == 2016)))
    {
        dayInEndMonth = 29;
    }

    var startdate  = startmonth + '/' + startday + '/' + startyear;
    var enddate    = endmonth   + '/' +   endday + '/' +   endyear;

    if (startday > dayInStartMonth)
    {
        alert('I\'m sorry, but "' + startdate + '" is not a valid date.');
        document.uplform.tripstartday.focus()
        return false
    }

    if (endday > dayInEndMonth)
    {
        alert('I\'m sorry, but "' + enddate + '" is not a valid date.');
        document.uplform.tripendday.focus()
        return false
    }

    var start = new Date(startdate);
    var end   = new Date(  enddate);

    if (start.getTime() > end.getTime())
    {
        alert('I\'m sorry, but the trip must not end before it begins');
        document.uplform.tripendday.focus()
        return false
    }
}

function ValidateForm()
{
    // First, lets get the selected countries from the applet
    var mapApplet = document.applets["worldmap"];

    if (mapApplet != null)
    {
        var countryNames = mapApplet.getSelectedNames();
        document.uplform.destinationNames.value = countryNames;

        var countryCodes = mapApplet.getSelectedCodes();
        document.uplform.destinationCodes.value = countryCodes;
    }

    // Now we can start validating form input
    var emailID = document.uplform.email

    if (echeck(emailID.value) == false)
    {
        emailID.value = ""
        emailID.focus()
        return false
    }

    if (checkDates() == false)
    {
        return false
    }

    return true
}

function emailAlert()
{
    alert("The generated packing list will be sent to the email address you specify.\nThe address will then be thrown away. It is not saved in any way.");
}

function multiSelectAlert()
{
    alert("Hold down the Ctrl key to select more than one option.");
}

function multiSelectKidsAlert()
{
    alert("Hold down the Ctrl key to select more than one option.\nOver 8 years pack like adults.");
}
