﻿function validateComment() {

    var _name = document.getElementById('_ContactMail_Name');
    var _phone = document.getElementById('_ContactMail_Phone');
    var _email = document.getElementById('_ContactMail_MailFrom');
    var _subject = document.getElementById('_ContactMail_Subject');
    var _comments = document.getElementById('_ContactMail_Body');
    var _app = document.getElementById('_ContactMail_AppsDropDown');
    var _appOtherInput = document.getElementById('_ContactMail_AppOther');
    
    try
    {
        if(_name && Trim(_name.value) == '')
        {
            throw 'Your name is required to continue.';
        }
        if(_phone == null || (Trim(_phone.value) == '') || isNaN(_phone.value.replace(/[\(\)\.\-\ ]/g, '')))
        {
            throw 'Your phone number is required to continue.';
        } 
        if(_email && Trim(_email.value) == '')
        {
            throw 'Your Email Address is required to continue.';
        }
        if (!validateEmail(_email.value)) {
            throw 'Your Email Address is not of the proper form\nPlease enter your address in the form of:\n\n\tuser@domain.ext';
        }
        if(_comments && Trim(_comments.value) == '')
        {
            throw 'Comments are required to continue.';
        }
        if (_comments.value.length < 5) {
            throw 'Please be more descriptive with your feedback.';
        }
        if (_app.value == 'Other' && _appOtherInput.value == '') {
            throw 'You selected "Other." Please write the name of the application in the provided text box.';
        }
        if (_app.value == 'Select An Application') {
            throw 'Please choose an application.';
        }
        
        _subject.value = getSubject();
    }
    catch(s)
    {
        alert('ERROR: ' + s);
        return false;
    }
    return true;
}

function appChange() {
    var _app = document.getElementById('_ContactMail_AppsDropDown');
    var _appOtherInput = document.getElementById('_ContactMail_AppOtherContainer');
    if (_app.value == 'Other') {
      _appOtherInput.style.display = "block";
    }
    else {
      _appOtherInput.style.display = "none";
    }
}

function getSubject() {
    return "";
}

function Trim(trimstring) {
    return trimstring.replace(/^\s+|\s+$/g, "");
}

function validateEmail(emailstring) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(emailstring) == false) {
        return false;
    }
    return true;
}

$(document).ready(function () {
    $('#_ContactMail_Phone').keydown(function (e) {
        if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 45 || e.keyCode == 9) {
            // let it happen, don't do anything 
        } else {
            if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
                // let it happen, don't do anything 
            } else {
                e.preventDefault();
            }
        }
    });

    $('#_Submit_Form').click(function() {
      if(validateComment()) {
        $('#_ContactMail_Form').submit();
      }
    });

    $('#_Reset_Form').click(function () {
        $('#_ContactMail_Name').val('');
        $('#_ContactMail_Company').val('');
        $('#_ContactMail_MailFrom').val('');
        $('#_ContactMail_Phone').val('');
        $('#_ContactMail_Body').val('');
    });
});
