var success = 0;
var textInput = "";
var emailInput = "";
var mailBody = "";
var sm_http_req = null;

function checkMailForm(form) {
    // Check if the fields are required, if so return message if no data submitted in field
    var err = 0;
    var textInput = "";
    var memEmailInput = "";
    var otherEmailInput = "";
    var memEmailVal = "";
    var otherEmailVal = "";
    var mailBody = "";

    // Check optional input field (suggested use for First Name)
    if (document.getElementById('mfiLbl') != null) {
        textInput = document.getElementById('mfiLbl').innerHTML;
    }
    if (textInput.length > 0) {
        if (textInput.indexOf('*') != -1 &&  document.getElementById('mfiValue').value.length < 1) {
            if (textInput.indexOf('mailError') == -1) {
                document.getElementById('mfiLbl').innerHTML = '<div class="mailError">' + textInput + '</div>';
            }
            err = 1;
        }
    }

    // Check optional email fields
    if ((document.getElementById('mfeLbl') != null) && (document.getElementById('mfeValue') != null)) {
        memEmailInput = document.getElementById('mfeLbl').innerHTML;
        memEmailVal   = document.getElementById('mfeValue').value;
    }

    if ((document.getElementById('mfoeLbl') != null) && (document.getElementById('mfoeValue') != null)) {
        otherEmailInput = document.getElementById('mfoeLbl').innerHTML;
        otherEmailVal   = document.getElementById('mfoeValue').value;
    }

    if (memEmailInput.length > 0) {
        if (memEmailInput.indexOf('*') != -1 &&  memEmailVal.length < 1) {
            if (memEmailInput.indexOf('mailError') == -1) {
                document.getElementById('mfeLbl').innerHTML = '<div class="mailError">' + memEmailInput + '</div>';
   	    }
            err = 1;
        }
    }

    if (otherEmailInput.length > 0) {
        if (otherEmailInput.indexOf('*') != -1 &&  otherEmailVal.length < 1) {
            if (otherEmailInput.indexOf('mailError') == -1) {
                document.getElementById('mfoeLbl').innerHTML = '<div class="mailError">' + otherEmailInput + '</div>';
   	    }
            err = 1;
        }
    }

    // Check email address format
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if ((memEmailVal.length > 0) && (filter.test(memEmailVal) == false)) {
        memEmailInput  = memEmailInput.substring(0,memEmailInput.indexOf('value=')+6) + '"' +  memEmailVal +  '" ' + memEmailInput.substring(memEmailInput.indexOf('id='),memEmailInput.length);
        if (memEmailInput.indexOf('mailError') == -1) {
            document.getElementById('mfeLbl').innerHTML = '<div class="mailError">' + memEmailInput + '</div>';
        }
        err = 1;
    }

    if ((otherEmailVal.length > 0) && (filter.test(otherEmailVal) == false)) {
        otherEmailInput  = otherEmailInput.substring(0,otherEmailInput.indexOf('value=')+6) + '"' +  otherEmailVal +  '" ' + otherEmailInput.substring(otherEmailInput.indexOf('id='),otherEmailInput.length);
        if (otherEmailInput.indexOf('mailError') == -1) {
            document.getElementById('mfoeLbl').innerHTML = '<div class="mailError">' + otherEmailInput + '</div>';
        }
        err = 1;
    }

    // Check Email Textarea
    if ((document.getElementById('mfsValue') != null) && (document.getElementById('mfsValue').value.length < 1)) {
        document.getElementById('emailErr').innerHTML = '<div class="mailError">Please include your message below.</div>';
        err = 1;
    }

    if (err == 1)
        return false;
    return true;
}

function mailReq(url, whichLayer, confirmMsg, errorMsg) {
    sm_http_req = null;
    success = 0;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        sm_http_req = new XMLHttpRequest();
        if (sm_http_req.overrideMimeType) {
            sm_http_req.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            sm_http_req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
       	    try {
                sm_http_req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!sm_http_req) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    sm_http_req.onreadystatechange = sendMailReq;
    sm_http_req.open("HEAD",url,true);
    sm_http_req.send(null);

    function sendMailReq() {
        try {
            if (sm_http_req.readyState == 4 && sm_http_req.status == 200) {
                displayConfirmation(whichLayer, 1, confirmMsg);
            } 
            else {
                if (sm_http_req.readyState >= 4) {
                    displayConfirmation(whichLayer, 1, errorMsg );
                }
            }
        } catch(e) {
            alert('Caught Exception: ' + e.message);
        }
    }

}

function displayConfirmation(whichLayer, state, msg, sn) {
    // 1 visible, 0 hidden
    document.getElementById('aConfirm').innerHTML = msg;

    if(document.layers) {
        document.layers[whichLayer].display = state ? "block" : "none";
    }
    else if(document.getElementById) {
        var obj = document.getElementById(whichLayer);
        obj.style.display = state ? "block" : "none";
    }
    else if(document.all) {
        document.all[whichLayer].style.display = state ? "block" : "none";
    }
    if (state == 0) {
        resetMailForm(msg, sn);
    }
}

function resetMailForm(msg, sn) {
   // return form to original state
   if (document.getElementById('mfeValue') != null) {
       document.getElementById('mfeValue').value = sn;
       resetLabelState('mfeLbl');
   }
   if (document.getElementById('mfiValue') != null) {
       document.getElementById('mfiValue').value = "";
       resetLabelState('mfiLbl');
   }
   if (document.getElementById('mfoeValue') != null) {
       document.getElementById('mfoeValue').value = "";
       resetLabelState('mfoeLbl');
   }
   resetLabelState('maxChar');
   document.getElementById('emailErr').innerHTML = '';
   document.getElementById('mfsValue').value = msg;
}

function resetLabelState(id) {
    var agt = navigator.userAgent.toLowerCase();
    var isIE =  (agt.indexOf("msie") != -1);

    if (document.getElementById(id) != null) {
        var label = document.getElementById(id).innerHTML;
        if ((isIE == true) && (label.indexOf("mailError") != -1)) {
            label = label.substring(21,label.indexOf("</DIV>"));
        } else if (label.indexOf("mailError") != -1) {
            label = label.substring(23,label.indexOf("</div>"));
        }
        document.getElementById(id).innerHTML = label;
    }
}

function submitMailForm(form, whichLayer, sendTo, subj, confirmMsg, errorMsg, sndMst, origMst, mailMst, sndMem, origMem, mailMem, master) {
    if (checkMailForm(form) == true) {
        var textVal = "";
        var memEmailVal = "";
        var otherEmailVal = "";
        if (document.getElementById('mfiValue') != null) {
            textVal = encodeURIComponent(document.getElementById('mfiValue').value);
        }
        if (document.getElementById('mfeValue') != null) {
            memEmailVal = encodeURIComponent(document.getElementById('mfeValue').value);
        }
        if (document.getElementById('mfoeValue') != null) {
            otherEmailVal = encodeURIComponent(document.getElementById('mfoeValue').value);
        }
        var url = '/mailform.adp?sendTo=' + sendTo + '&subj=' + encodeURIComponent(subj) + '&itxt=' + textVal + '&etxt=' + memEmailVal + '&oetxt=' + otherEmailVal + '&mtxt=' + encodeURIComponent(document.getElementById('mfsValue').value) + '&sndMst=' + encodeURIComponent(sndMst) + '&origMst=' + encodeURIComponent(origMst) + '&mailMst=' + encodeURIComponent(mailMst) + '&sndMem=' + encodeURIComponent(sndMem) + '&origMem=' + encodeURIComponent(origMem) + '&mailMem=' + encodeURIComponent(mailMem) + '&mst=' + encodeURIComponent(master);
        mailReq(url, whichLayer, confirmMsg, errorMsg);
    } else {
        return false;
    }
}

function imposeMaxLength(object, maxLen, msg) {
    // Only allows user to enter maxLen chars into textarea
    if ((object.value.length > maxLen) && (document.getElementById('maxChar') != null)) {
       document.getElementById('maxChar').innerHTML = '<div class="mailError">' + msg + '</div>';
    }
    var checkLen = object.value.length;
    if (checkLen > maxLen) {
        object.value = object.value.substring(0,maxLen);
    }
    return (checkLen <= maxLen);
}
