function ltrim(argvalue)
{
   while (1) {
      if (argvalue.substring(0, 1) != " ")
        break;
      argvalue = argvalue.substring(1, argvalue.length);
   }
   return argvalue;
}

function rtrim(argvalue)
{
   while (1) {
      if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
        break;
      argvalue = argvalue.substring(0, argvalue.length - 1);
   }
   return argvalue;
}

function trim(argvalue)
{
   var tmpstr = ltrim(argvalue);
   return rtrim(tmpstr);
}


function confirm_form()
{
   if (checkform())
      document.frmSurveyInquiry.submit();
}

function checkform()
{
    var typemailing = valButton(document.frmSurveyInquiry.m_typemailing);
    if (typemailing == null){
        alert('Please indicate what type of mailing you received.');
        return false;
    }

    var mmreceived = document.frmSurveyInquiry.m_mmreceived.value;
    var ddreceived = document.frmSurveyInquiry.m_ddreceived.value;
    var yyreceived = document.frmSurveyInquiry.m_yyyyreceived.value;

    if (mmreceived.length < 1 || ddreceived.length < 1 || yyreceived.length < 1){
        alert('Please indicate the receipt date of the email invitation or survey.');
        return false;
    }

    if (!isNumber(mmreceived)){
        alert('Please enter a numeric value for the month you received the email invitation or survey.');
        return false;
    }

    if (!isNumber(ddreceived)){
        alert('Please enter a numeric value for the day you received the email invitation or survey.');
        return false;
    }

    if (!isNumber(yyreceived)){
        alert('Please enter a numeric value for the year you received the email invitation or survey.');
        return false;
    }

    if (yyreceived.length < 4) {
        alert('Please enter a 4-digit year.');
        return false;
    }
/*
    var websiteaddress = document.frmSurveyInquiry.m_websiteaddress.value;
    if (websiteaddress.length < 1 ) {
        alert('Please indicate the website address that you are trying to access.');
        return false;
    }
*/
    var signature = document.frmSurveyInquiry.m_signature.value;
    if (signature.length < 1 ) {
        alert('Please indicate whose signature was on the survey, postcard, or email invitation.');
        return false;
    }

    var issue = valButton(document.frmSurveyInquiry.m_issue);
    if (issue == null){
        alert('Please indicate what type of issue you are having with our survey.');
        return false;
    }

    var firstname = document.frmSurveyInquiry.m_firstname.value;
    if (firstname.length < 1 ) {
        alert('Please indicate your first name.');
        return false;
    }

    var lastname = document.frmSurveyInquiry.m_lastname.value;
    if (lastname.length < 1 ) {
        alert('Please indicate your last name.');
        return false;
    }

    var prefcontact = valButton(document.frmSurveyInquiry.m_prefcontact);
    if (prefcontact == null){
        alert('Please indicate your preferred method of contact.');
        return false;
    }
/*
    if (issue == 'Remove me from your mailing list'){

        var address = document.frmSurveyInquiry.m_address.value;
        var city = document.frmSurveyInquiry.m_city.value;
        var state = document.frmSurveyInquiry.m_state.value;
        var zip = document.frmSurveyInquiry.m_zip.value;
        var country = document.frmSurveyInquiry.m_country.value;

        if (address.length < 1 || city.length < 1 || state.length < 1 || zip.length < 1 || country.length < 1 ) {
            alert('Please complete all of the address information (address, city, state/province, postal code and country) so that we can remove you from our mailing list.');
            return false;
        }
    }
*/

    if (prefcontact == 'Email') {

        var emailaddr = document.frmSurveyInquiry.m_emailaddr.value;
        if (emailaddr.length < 1) {
            alert('Please indicate your email address.');
            return false;
        }

        if (!email_ok(emailaddr)) { return false; }
    }

    if (prefcontact == 'Phone') {

        var phone = document.frmSurveyInquiry.m_area.value + document.frmSurveyInquiry.m_exch.value + document.frmSurveyInquiry.m_phone.value;
        // alert(phone);
        if (phone.length != 10) {alert('Please indicate a valid phone number.'); return false; }
        if (!isNumber(phone)) {alert('Phone number must consist of numbers only.'); return false; }
    }

    return true;
}

function email_ok(str)
{
   var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
   var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
   if (!reg1.test(str) && reg2.test(str))
   {
         return true;
   }
   alert("\"" + str + "\" is an invalid email address."); // this is also optional
   return false;
}
// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
// you may copy this function but please keep the copyright notice with it
function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
function isNumber(entry){
    validChar='0123456789';// characters allowed
    strlen=entry.length;         // how long is test string
    // Now scan string for illegal characters
    for (i=0;i<strlen;i++){
        if(validChar.indexOf(entry.charAt(i))<0){   return false;}
    } // end scanning loop
    return true;
}
