// Globals
var remainingSeats;
var r1, r2, r3, r4;

function $(something) {
    if (typeof(something) == 'string') {
        var elm = document.getElementById(something);
    } else {
        var elm = something;
    }

    if (!elm) return false;

    return elm;
}

function trim(str) {
    if (str) {
        return str.replace(/^\s*|\s*$/g, '');
    } else {
        return '';
    }
}

function getEventSource(evt) {
    return evt.target ? evt.target : (evt.srcElement ? evt.srcElement : null);
}

function initPage(remainingSeats) {
    onChangeRegistrantType();

    r1 = $('registrant1');
    r2 = $('registrant2');
    r3 = $('registrant3');
    r4 = $('registrant4');
}

function onChangeRegistrantType() {
    var professionalRegistrant = $('registrant_type_professional').checked;
    var studentRegistrant = $('registrant_type_student').checked;
    var seniorRegistrant = $('registrant_type_seniors_freelancers').checked;

    if (professionalRegistrant) {
        $('number_of_registrants_field').style.display = '';
        $('registrant1_header_text').innerHTML = 'Registrant No.1';
        $('registrant1_title_field').style.display = '';
        $('registrant1_student_fields').style.display = 'none';
        $('company_field').style.display = '';

        onSelectNumRegistrants();
    } else if (studentRegistrant) {
        $('number_of_registrants_field').style.display = 'none';
        $('registrant1_header_text').innerHTML = 'Registrant';
        $('registrant1_title_field').style.display = 'none';
        $('registrant1_student_fields').style.display = '';
        $('company_field').style.display = 'none';

        $('registrant2').style.display = 'none';
        $('registrant3').style.display = 'none';
        $('registrant4').style.display = 'none';
    } else if (seniorRegistrant) {
        $('number_of_registrants_field').style.display = 'none';
        $('registrant1_header_text').innerHTML = 'Registrant';
        $('registrant1_title_field').style.display = 'none';
        $('registrant1_student_fields').style.display = 'none';
        $('company_field').style.display = 'none';

        $('registrant2').style.display = 'none';
        $('registrant3').style.display = 'none';
        $('registrant4').style.display = 'none';
    }
}

function onSelectNumRegistrants() {
    var r1 = $('registrant1');
    var r2 = $('registrant2');
    var r3 = $('registrant3');
    var r4 = $('registrant4');

    var numRegistrants = $('number_of_registrants').value;

    switch (numRegistrants) {
        case "1":
            r1.style.display = '';
            r2.style.display = 'none';
            r3.style.display = 'none';
            r4.style.display = 'none';
            break;
        case "2":
            r1.style.display = '';
            r2.style.display = '';
            r3.style.display = 'none';
            r4.style.display = 'none';
            break;
        case "3":
            r1.style.display = '';
            r2.style.display = '';
            r3.style.display = '';
            r4.style.display = 'none';
            break;
        case "4":
            r1.style.display = '';
            r2.style.display = '';
            r3.style.display = '';
            r4.style.display = '';
        break;
    }
}

function isValidEmailAddress(str) {
    return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);
}

function isValidPhoneNumber(str) {
    var regEx = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
    if (!regEx.test(str)) {
        return false;
    }

    return true;
}

function isValidZipCode(str) {
    var regEx = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    if (!regEx.test(str)) {
        return false;
    }

    return true;
}

function checkForm() {

    var professionalRegistrant = $('registrant_type_professional').checked;
    var studentRegistrant = $('registrant_type_student').checked;
    var numRegistrants = document.getElementById('number_of_registrants').value;

    var registrant1FirstName = trim($('registrant1_first_name').value);
    if (registrant1FirstName.length < 2) {
        alert("Please enter the first name of registrant 1.");
        $('registrant1_first_name').focus();
        return false;
    }
    var registrant1LastName = trim($('registrant1_last_name').value);
    if (registrant1LastName.length < 2) {
        alert("Please enter the last name of registrant 1.");
        $('registrant1_last_name').focus();
        return false;
    }

    if (professionalRegistrant) {

        if (numRegistrants >= 2) {
            var registrant2FirstName = trim($('registrant2_first_name').value);
            if (registrant2FirstName.length < 2) {
                alert("Please enter the first name of registrant 2.");
                $('registrant2_first_name').focus();
                return false;
            }
            var registrant2LastName = trim($('registrant2_last_name').value);
            if (registrant2LastName.length < 2) {
                alert("Please enter the last name of registrant 2.");
                $('registrant2_last_name').focus();
                return false;
            }
        }

        if (numRegistrants >= 3) {
            var registrant3FirstName = trim($('registrant3_first_name').value);
            if (registrant3FirstName.length < 2) {
                alert("Please enter the first name of registrant 3.");
                $('registrant3_first_name').focus();
                return false;
            }
            var registrant3LastName = trim($('registrant3_last_name').value);
            if (registrant3LastName.length < 2) {
                alert("Please enter the last name of registrant 3.");
                $('registrant3_last_name').focus();
                return false;
            }
        }

        if (numRegistrants >= 4) {
            var registrant4FirstName = trim($('registrant4_first_name').value);
            if (registrant4FirstName.length < 2) {
                alert("Please enter the first name of registrant 4.");
                $('registrant4_first_name').focus();
                return false;
            }
            var registrant4LastName = trim($('registrant4_last_name').value);
            if (registrant4LastName.length < 2) {
                alert("Please enter the last name of registrant 4.");
                $('registrant4_last_name').focus();
                return false;
            }
        }

        var company = trim($('company').value);
        if (company.length < 2) {
            alert("Please enter the company.");
            $('company').focus();
            return false;
        }
    } else if (studentRegistrant) {

        var registrant1University = trim($('registrant1_university').value);
        if (registrant1University.length < 2) {
            alert("Please enter a university for registrant 1.");
            $('registrant1_university').focus();
            return false;
        }
        var registrant1Major = trim($('registrant1_major').value);
        if (registrant1Major.length < 2) {
            alert("Please enter a major for registrant 1.");
            $('registrant1_major').focus();
            return false;
        }
        var registrant1GraduationYear = trim($('registrant1_graduation_year').value);
        if (registrant1GraduationYear.length < 2) {
            alert("Please enter a graduation year for registrant 1.");
            $('registrant1_graduation_year').focus();
            return false;
        }
    }

    var address1 = trim($('address1').value);
    if (address1.length < 2) {
        alert("Please enter the address.");
        $('address1').focus();
        return false;
    }

    var city = trim($('city').value);
    if (city.length < 2) {
        alert("Please enter the city.");
        $('company').focus();
        return false;
    }

    var stateSelectBox = $('state');
    var state = stateSelectBox[stateSelectBox.selectedIndex].value;

    if (state.length < 2) {
        alert("Please select the state.");
        $('state').focus();
        return false;
    }

    var zip = trim($('zip').value);
    if (!isValidZipCode(zip)) {
        alert("Please enter a valid zip code.");
        $('zip').focus();
        return false;
    }

    var telephone = trim($('telephone').value);
    if (telephone.length < 7) {
        alert("Please enter a valid telephone number.");
        $('telephone').focus();
        return false;
    }

    var emailAddress = trim($('email_address').value);
    if (!isValidEmailAddress(emailAddress)) {
        alert("Please enter a valid email address.");
        $('email_address').focus();
        return false;
    }

    return true;
}