﻿var contactReasonElem = null;
var commentsElem = null;
var contactBackElem = null;
var nameElem = null;
var emailElem = null;
function ValidateAndSubmit()
{
    contactReasonElem = document.form1.ddlContactReason;
    commentsElem = document.form1.tbComments;
    contactBackElem = document.form1.rbContactBack;
    nameElem = document.form1.tbName;
    emailElem = document.form1.tbEmail;
    if (contactReasonElem.selectedIndex <= 0)
    {
        alert("Please tell us why you are contacting us today");
        contactReasonElem.focus();
        return false;
    }
    commentsElem.value = trim(commentsElem.value);
    if (commentsElem.value == "")
    {
        alert("Please provide your comments/question/feedback");
        commentsElem.focus();
        return false;
    }
    contactBackValue = "";
    var boolContactBack = false;
    for (var i=0; i<contactBackElem.length; i++)
    {
        if (contactBackElem[i].checked == true)
        {
            contactBackValue = contactBackElem[i].value;
            break;
        }
    }
    if (contactBackValue == "")
    {
        alert("Please specify if we may contact you back");
        return false;
    }
    nameElem.value = trim(nameElem.value);
    emailElem.value = trim(emailElem.value);
    if (contactBackValue == "1")
    {
        boolContactBack = true;
        if (nameElem.value == "")
        {
            alert("Please provide your name");
            nameElem.focus();
            return false;
        }
        if (emailElem.value == "")
        {
            alert("Please provide your email address so that we may contact you back");
            emailElem.focus();
            return false;
        }
    }
    if (emailElem.value != "" && ! isValidEmail(emailElem.value))
    {
        alert("Please provide a valid email address.");
        emailElem.select();
        return false;
    }
    document.form1.btnSubmit.disabled = true;
    document.form1.btnCancel.disabled = true;
    CommonPages_Contact.SaveComments(document.form1.uid.value, parseInt(contactReasonElem.value, 10), commentsElem.value, boolContactBack, nameElem.value, emailElem.value, SaveComments_CallBack)
    alert("Thank you for contacting us. Somebody from our team will get back to you shortly.");
    ResetForm();
    return false;
}

function SaveComments_CallBack(resp)
{
    respVal = resp.value;
    return false;
}

function ResetForm()
{
    document.form1.reset();
    document.form1.btnSubmit.disabled = false;
    document.form1.btnCancel.disabled = false;
    return false;
}