function print_errors(errors, app)
{
    var errOut = '<strong>Errors</strong><ul>';
    var i =0;
    var key;

    for (var formInput in errors) {
        if (formInput != 'photo') {
            highlight_failure(app[formInput]);
        }
        errOut = errOut+'<li>'+errors[formInput]+'</li>';

        if (0 == i) {
            key = formInput;
        }
        i++;
    }

    errOut = errOut+'</ul>';
    document.getElementById('error_cell').innerHTML = errOut;
    window.location = '#error_cell';
    if (key != 'Date_Day'    && key != 'Date_Month'  &&
        key != 'Date_Year'   && key != 'state'       &&
        key != 'country'     && key != 'height_feet' &&
        key != 'height_inch' && key != 'weight'      &&
        key != 'photo') {
            app[key].select();
    }
}

function highlight_failure(formInput)
{
    formInput.style.borderColor = '#FF0000';
}

function validate_app(page, app)
{
    return true;
//    switch (page)
//    {
//        case 1:
//          return validate_page1(app);
//          break;
//
//        case 2:
//          return validate_page2(app);
//          break;
//
//        case 3:
//          return validate_page3(app);
//          break;
//
//        case 4:
//          return validate_page4(app);
//          break;
//
//        default:
//          return false;
//    }
}

function validate_page1(app)
{
    var first_name      = trim(app.first_name.value);
    var last_name       = trim(app.last_name.value);
    var stage_name      = trim(app.stage_name.value);

    var month_index     = app.Date_Month.selectedIndex;
    var day_index       = app.Date_Day.selectedIndex;
    var year_index      = app.Date_Year.selectedIndex;
    var date_month      = trim(app.Date_Month.options[month_index].value);
    var date_day        = trim(app.Date_Day.options[day_index].value);
    var date_year       = trim(app.Date_Year.options[year_index].value)

    var street_address  = trim(app.street_address.value);
    var street_address2 = trim(app.street_address2.value);
    var city            = trim(app.city.value);

    var state_index     = app.state.selectedIndex;
    var country_index   = app.country.selectedIndex;
    var state           = trim(app.state.options[state_index].value);
    var country         = trim(app.country.options[country_index].value);

    var zip_code        = trim(app.zip_code.value);
    var phone           = trim(app.phone.value);
    var email           = trim(app.email.value);
    var email2          = trim(app.email2.value);
    var site            = trim(app.site.value);
    var username        = trim(app.username.value);
    var pass_word       = trim(app.pass_word.value);
    var pass_word2      = trim(app.pass_word2.value);
    var application_id  = trim(app.application_id.value);

    var failures = new Object();
    var num_fail = 0;

    if (0 == first_name.length) {
        failures['first_name'] = 'First Name cannot be blank';
        num_fail++;
    } else {
        var inv_char = /[^A-Za-z\s\.-]/;
        if (first_name.match(inv_char)) {
            failures['first_name'] = 'First Name can only contain letters, spaces, periods, and dashes.';
            num_fail++;
        }
    }

    if (0 == last_name.length) {
        failures['last_name'] = 'Last Name cannot be blank';
        num_fail++;
    } else {
        var inv_char = /[^A-Za-z\s\.-]/;
        if (last_name.match(inv_char)) {
            failures['last_name'] = 'Last Name can only contain letters, spaces, periods, and dashes.';
            num_fail++;
        }
    }

    if (0 == stage_name.length) {
        failures['stage_name'] = 'Model Name cannot be blank';
        num_fail++;
    } else {
        var inv_char = /[^A-Za-z\s\.-]/;
        if (stage_name.match(inv_char)) {
            failures['stage_name'] = 'Model Name can only contain letters, spaces, periods, and dashes.';
            num_fail++;
        }
    }

    var d       = new Date();
    var c_year  = d.getFullYear();
    var cut_off = c_year-18;

    if (12      < date_month || 0    == date_month ||
        31      < date_day   || 0    == date_day   ||
        cut_off < date_year  || 1910 >  date_year) {
        failures['Date_Month'] = 'Invalid Birth Month';
        failures['Date_Day'] = 'Invalid Birth Day';
        failures['Date_Year'] = 'Invalid Birth Year';
        num_fail++;
    }

    if (0 == street_address.length) {
        failures['street_address'] = 'Please enter your home street address';
        num_fail++;
    }

    if (0 == city.length) {
        failures['city'] = 'Please enter the name of your city';
        num_fail++;
    }

    if (2 != state.length) {
        failures['state'] = 'Please select a State/Province or choose "Outside US/Canada"';
        num_fail++;
    }

    if ('--' == country) {
        failures['country'] = 'Please select a Country';
        num_fail++;
    }

   /* var zip_test = validate_zip(zip_code);
    if (0 != zip_test.length) {
        failures['zip_code'] = zip_test[0];
        num_fail++;
    }*/

    //var delim = /\.|\(|\)|\+|\-/g;
    //phone = phone.replace(delim, '');
    //phone = phone.match(/\d*/);
    //phone = phone.toString();
    //if (phone.length != 11 && phone.length != 10) {
    //    failures['phone'] = 'Invalid Phone Number';
    //    num_fail++;
    //}

    var email_reg = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
    if (!email.match(email_reg)) {
        failures['email'] = 'Please enter a valid email address';
        num_fail++;
    }

    if (email != email2) {
        failures['email2'] = 'Email addresses do not match';
        num_fail++;
    }

    var cred_reg = /[A-Za-z0-9]/;
    if (!username.match(cred_reg)) {
        failures['username'] = 'Username can only contain alphanumeric charaters';
        num_fail++;
    }

    if (6 > username.length) {
        failures['username'] = 'Username must be a minimum of 6 charaters';
        num_fail++;
    }

    if (application_id == 0) {
        var cred_reg = /[A-Za-z0-9]/;
        if (!pass_word.match(cred_reg)) {
            failures['pass_word'] = 'Password can only contain alphanumeric charaters';
            num_fail++;
        }

        if (6 > pass_word.length) {
            failures['pass_word'] = 'Password must be a minimum of 6 charaters';
            num_fail++;
        }

        if (pass_word != pass_word2) {
            failures['pass_word2'] = 'Passwords do not match';
            num_fail++;
        }
    }

    if (site != 'http://' && 0 != site.length) {
        var site_reg = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;

        if (!site_reg.test(site)) {
            failures['site'] = 'Please enter a valid website address';
            num_fail++;
        }

    }

    if (num_fail == 0) {
        return true;
    }

    print_errors(failures, app);
    return false;
}

function validate_page2(app)
{

    var height_feet = trim(app.height_feet.value);
    var height_inch = trim(app.height_inch.value);
    var weight      = trim(app.weight.value);
    var are_a       = trim(app.are_a.value);
    var body_fur    = trim(app.body_fur.value);
    var fur_color   = trim(app.fur_color.value);
    var facial_hair = trim(app.facial_hair.value);
    var ethnicity   = trim(app.ethnicity.value);
    var hair_color  = trim(app.hair_color.value);
    var eye_color   = trim(app.eye_color.value);
    var body_type   = trim(app.body_type.value);

    var photo       = document.forms[1]['photo[]'];

    var failures = new Object();
    var num_fail = 0;

    if (height_feet == 'Select' || (height_feet > 7 || height_feet < 0)) {
        failures['height_feet'] = 'Please select the number of feet in your height.';
        num_fail++;
    }

    if (height_inch == 'Select' || (height_inch > 11.5 || height_inch < 0)) {
        failures['height_inch'] = 'Please select the number of inches in your height.';
        num_fail++;
    }

    if (weight == 'Select')
    {
        failures['weight'] = 'Please select a value for your weight.';
        num_fail++;
    }
    else if (weight != 'Under 100' && weight != 'Over 300')
    {
        if (weight < 100 || weight > 300)
        {
            failures['weight'] = 'Please select a value for your weight.';
            num_fail++;
        }
    }

    if (are_a == 'Select') {
        failures['are_a'] = 'Please tell us if you are a Bear, Cub, etc.';
        num_fail++;
    }

    if (body_fur == 'Select') {
        failures['body_fur'] = 'Please tell us how much body hair you have.';
        num_fail++;
    }

    if (fur_color == 'Select') {
        failures['fur_color'] = 'Please tell us what color your body hair is.';
        num_fail++;
    }

    if (facial_hair == 'Select') {
        failures['facial_hair'] = 'Please tell us what kind of facial hair you have.';
        num_fail++;
    }

    if (ethnicity == 'Select') {
        failures['ethnicity'] = 'Please select your ethnicity- or choose Not Given- from the list.';
        num_fail++;
    }

    if (body_type == 'Select') {
        failures['body_type'] = 'Please tell us about your body type.';
        num_fail++;
    }

    if (hair_color == 'Select') {
        failures['hair_color'] = 'Please tell us what color your hair is.';
        num_fail++;
    }

    if (eye_color == 'Select') {
        failures['eye_color'] = 'Please tell us what color your eyes are.';
        num_fail++;
    }


    var length = photo.length;
    var path   = null;
    var empty  = 0;
    for(var i=0; i<length; i++) {
        path = trim(photo[i].value);
        if (path.length == 0) {
            empty++;
        }
    }
    if (empty > 0) {
        failures['photo'] = 'You must upload all three pictures requested.';
        num_fail++;
    }
    alert('Please make sure that you have submitted the following photos:\n\tA clear face shot\n\tA full, nude body shot\n\tA hard cock shot\nYour application will not be considered if you do not supply these photos.');

    if (num_fail == 0) {
        return true;
    }
    print_errors(failures, app);
    return false;
}

function validate_page3(back)
{
    if (back == true) {
        return true;
    }
    var app = document.forms[1];
    var prior_work   = trim(app.prior_work.value);
    var photo_work   = trim(app.photo_work.value);
    var dick_length  = trim(app.dick_length.value);
    var pos_pref     = trim(app.pos_pref.value);
    var skills       = trim(app.skills.value);
    var fantasies    = trim(app.fantasies.value);
    var hardon       = trim(app.hardon.value);
    var cum          = trim(app.cum.value);
    var match_type   = trim(app.match_type.value);
    var bear_event   = trim(app.bear_event.value);
    var comments     = trim(app.comments.value);
    var circum       = app.circum;

    var radios             = new Object();
    radios['kissing']      = app["enjoys[Kissing]"];
    radios['solo']         = app["enjoys[Solo]"];
    radios['one_one']      = app["enjoys[One on One]"];
    radios['three_way']    = app["enjoys[3-Way]"];
    radios['orgy']         = app["enjoys[Orgy]"];
    radios['anal_sex']     = app["enjoys[Anal Sex]"];
    radios['oral_sex']     = app["enjoys[Oral Sex]"];
    radios['nipple_pay']   = app["enjoys[Nipple Play]"];
    radios['toys']         = app["enjoys[Toys]"];
    radios['rimming']      = app["enjoys[Rimming]"];
    radios['spanking']     = app["enjoys[Spanking]"];
    radios['fisting']      = app["enjoys[Fisting]"];
    radios['smoking']      = app["enjoys[Cigars/Smoking]"];
    radios['water_sports'] = app["enjoys[Water Sports]"];
    radios['foot_fetish']  = app["enjoys[Foot Fetish]"];
    radios['uniforms']     = app["enjoys[Uniforms]"];
    radios['bondage']      = app["enjoys[Bondage / S&M]"];
    radios['leather']      = app["enjoys[Leather]"];
    radios['cock_torture'] = app["enjoys[Cock & Ball Torture]"];

    var failures = new Object();
    var num_fail = 0;

    if (0 == dick_length.length) {
        failures['dick_length'] = 'Dick size cannot be blank.';
        num_fail++;
    }

    var count = circum.length;

    if (2 != count) {
        failures['circum'] = 'Invalid Form';
        num_fail++;
    }

    var check = 0;
    for (var i=0; i<count; i++) {

        if (circum[i].checked == true) {
            check++;
        }
    }

    if (check != 1) {
        failures['circum'] = 'Choose between cut and uncut. (Dick Size question)';
        num_fail++;
    }

    for (in_put in radios) {
        var count = radios[in_put].length;
        if (3 != count) {
            failures[in_put] = 'Invalid Form';
            num_fail++;
        }

        var check = 0;
        for (var i=0; i<count; i++) {
            if (radios[in_put][i].checked == true) {
                check++;
            }
        }

        if (check != 1) {
            failures[in_put] = in_put+' cannot be blank.';
            num_fail++;
        }
    }

    if (0 == skills.length) {
        failures['skills'] = 'Please tell us what you are great at, sexually.';
        num_fail++;
    }

    if (0 == fantasies.length) {
        failures['fantasies'] = 'Please tell us about your sexual fantasies.';
        num_fail++;
    }

    if (0 == hardon.length) {
        failures['hardon'] = 'Please tell us if you have problems getting/keeping a hardon.';
        num_fail++;
    }

    if (0 == cum.length) {
        failures['cum'] = 'Please tell us if you have any difficulty cumming.';
        num_fail++;
    }

    if (0 == match_type.length) {
        failures['match_type'] = 'Please tell us what type of men turn you on.';
        num_fail++;
    }

    if (0 == bear_event.length) {
        failures['bear_event'] = 'Please tell us what bear events you may be attending.';
        num_fail++;
    }

    if (num_fail == 0) {
        app.submit();
        return;
    }

    print_errors(failures, app);
    return false;
}


function validate_page4(app)
{
    var agree    = app.agree;
    var failures = new Object();
    var num_fail = 0;

    if (agree.checked != true) {
        failures['agree'] = 'Check the box to indicate you agree to the terms and conditions.';
        num_fail++;
    }

    if (num_fail == 0) {
        return true;
    }

    print_errors(failures, app);
    return false;
}

function validate_zip(field)  {
    var valid       = "0123456789-";
    var hyphencount = 0;
    var failures    = new Array();

    if (field.length!=5 && field.length!=10) {
        failures[failures.length] = 'Please enter your 5 digit or 5 digit+4 zip code.';
    }

    for (var i=0; i < field.length; i++) {
        temp = "" + field.substring(i, i+1);
        if (temp == "-") hyphencount++;
            if (valid.indexOf(temp) == '-1') {
                failures[failures.length] = 'Invalid characters in zip code.';
            }

        if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
            failures[failures.length] = 'Improper zip code format.';
        }
    }

    return failures;
}

