﻿function VerifyForm() {
    if (formVerify()) {
        document.searchform.submit();
    }
    
}

function formVerify() {
    if (document.searchform.theme.selectedIndex == 0) {
        alert("Please select a theme.");
        return false;
    }
    else if (document.searchform.recipient.value == '') {
        alert("Please specify a recipient.");
        return false;
    }
    else if (document.searchform.gifter.value == '') {
        alert("Please enter your name or who the gift is from.");
        return false;
    }
    else if (isValidEmail(document.searchform.recemail.value) == false) {
        alert("Please specify a valid recipient e-mail address.");
        return false;
    }
    else if (document.searchform.amount.value == '') {
        alert("Please enter a gift amount.");
        return false;
    }
    else if (isInt(document.searchform.amount.value) == false && isDecimal(document.searchform.amount.value) == false) {
        alert("Please enter a valid gift amount.");
        return false;
    }
    else if (isInAdvance() == false) {
        alert("The delivery date must be in advance.");
        return false;
    }
    else {
        return true;
    }
}

function isValidEmail(str) {
    return (str.indexOf(".") >= 1) && (str.indexOf("@") > 0);
}
function isInt(myNum) {
    // get the modulus: if it's 0, then it's an integer
    var myMod = myNum % 1;

    if (myMod == 0) {
        return true;
    } else {
        return false;
    }
}
        

function isDecimal(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function ShowContent(d) {

    if (d.length < 1) { return; }
    document.getElementById(d).style.display = "block";
}

function RemoveContent(d) {
    if (d.length < 1) { return; }
    document.getElementById(d).style.display = "none";
}

function setMonthDayOptions() {
    switch (document.searchform.check_in_month.selectedIndex) {
        //Handles February 
        case 1:
            if ((document.searchform.check_in_year.options[document.searchform.check_in_year.selectedIndex].value - 2000) % 4 == 0) {
                populateDayOptions(29);
            }
            else {
                populateDayOptions(28);
            }

            break;
        //Handles April 
        case 3:
            populateDayOptions(30);
            break;
        //Handles June 
        case 5:
            populateDayOptions(30);
            break;

        //Handles September 
        case 8:
            populateDayOptions(30);
            break;
        //Handles November 
        case 10:
            populateDayOptions(30);
            break;
        default:
            populateDayOptions(31);
    }
}
function populateDayOptions(monthIndex) {

    document.getElementById('check_in_day').length = 0;
    for (var y = 1; y <= monthIndex; y++) {
        newOpt = new Option(y);
        document.searchform.check_in_day.options[document.searchform.check_in_day.length] = newOpt;
    }
    document.searchform.check_in_day.selectedIndex = document.searchform.today.value - 1;
}
function setToday() {
    document.searchform.today.value = document.searchform.check_in_day.selectedIndex + 1;
}


function loadTheme() {
    try {
        if (document.searchform.theme.selectedIndex == 0) {
            RemoveContent('picdiv');
        }
        else {
            changePicture(document.searchform.theme.options[document.searchform.theme.selectedIndex].value);
            ShowContent('picdiv');
        }
    }
    catch (err) {
        alert(err.description);
    }
    
}

function changePicture(pictureName) {
    elem1 = document.getElementById('themepreview');
    elem1.src = '/gifts/images/' + pictureName + '_sm.jpg';
}
    
