var arr;
var option;
var employees;
var listings;
var n;
var p;
var p1;
function ValidatePhone() {
    p = p1.value
    if (p.length == 3) {
        //d10=p.indexOf('(')
        pp = p;
        d4 = p.indexOf('(')
        d5 = p.indexOf(')')
        if (d4 == -1) {
            pp = "(" + pp;
        }
        if (d5 == -1) {
            pp = pp + ")";
        }
        //pp="("+pp+")";
        p1.value = "";
        p1.value = pp;
    }
    if (p.length > 3) {
        d1 = p.indexOf('(')
        d2 = p.indexOf(')')
        if (d2 == -1) {
            l30 = p.length;
            p30 = p.substring(0, 4);
            //alert(p30);
            p30 = p30 + ")"
            p31 = p.substring(4, l30);
            pp = p30 + p31;
            //alert(p31);
            p1.value = "";
            p1.value = pp;
        }
    }
    if (p.length > 5) {
        p11 = p.substring(d1 + 1, d2);
        if (p11.length > 3) {
            p12 = p11;
            l12 = p12.length;
            l15 = p.length
            //l12=l12-3
            p13 = p11.substring(0, 3);
            p14 = p11.substring(3, l12);
            p15 = p.substring(d2 + 1, l15);
            p1.value = "";
            pp = "(" + p13 + ")" + p14 + p15;
            p1.value = pp;
            //obj1.value="";
            //obj1.value=pp;
        }
        l16 = p.length;
        p16 = p.substring(d2 + 1, l16);
        l17 = p16.length;
        if (l17 > 3 && p16.indexOf('-') == -1) {
            p17 = p.substring(d2 + 1, d2 + 4);
            p18 = p.substring(d2 + 4, l16);
            p19 = p.substring(0, d2 + 1);
            //alert(p19);
            pp = p19 + p17 + "-" + p18;
            p1.value = "";
            p1.value = pp;
            //obj1.value="";
            //obj1.value=pp;
        }
    }
    //}
    setTimeout(ValidatePhone, 100)
}
function getIt(m) {
    n = m.name;
    //p1=document.forms[0].elements[n]
    p1 = m
    ValidatePhone()
}
function testphone(obj1) {
    p = obj1.value
    //alert(p)
    p = p.replace("(", "")
    p = p.replace(")", "")
    p = p.replace("-", "")
    p = p.replace("-", "")
    //alert(isNaN(p))
    if (isNaN(p) == true) {
        alert("Check phone");
        return false;
    }
}
function ShowMenu(num, menu, max) {
    //starting at one, loop through until the number chosen by the user
    for (i = 1; i <= num; i++) {
        //add number onto end of menu
        var menu2 = menu + i;
        //change visibility to block, or 'visible'
        document.getElementById(menu2).style.display = 'block';
    }
    //make a number one more than the number inputed
    var num2 = num;
    num2++;
    //hide it if the viewer selects a number lower
    //this will hide every number between the selected number and the maximum
    //ex.  if 3 is selected, hide the <div> cells for 4, 5, and 6
    //loop until max is reached
    while (num2 <= max) {
        var menu3 = menu + num2;
        //hide 
        document.getElementById(menu3).style.display = 'none';
        //add one to loop
        num2 = num2 + 1;
    }
    calc_listings()
}
function calc_employees() {
    employees = Math.round(parseInt(document.AppForm.ftemploy.value) + (parseInt(document.AppForm.ptemploy.value)/2));
    switch (document.AppForm.busncat.selectedIndex){
        case 1:
           if (employees >= 1 && employees <= 5) {
               document.AppForm.invest.value = 125;
               break;
            }
       case 38:
            if (employees >= 1 && employees <= 5) {
                document.AppForm.invest.value = 635;
                break;
            }
            else {
                if (employees >= 6 && employees <= 15) {
                    document.AppForm.invest.value = 775;
                    break;
                }
                else {
                    if (employees >= 16 && employees <= 50) {
                        document.AppForm.invest.value = 820;
                        break;
                    }
                    else {
                        if (employees >= 51 && employees <= 100) {
                            document.AppForm.invest.value = 1025;
                            break;
                        }
                        else {
                            alert("Please contact our office at 412-856-0622 to submit your application");
                            break;
                        }
                    } 
                } 
            }
        case 89: case 90: case 91: case 92: case 93: case 94: case 95:
            document.AppForm.invest.value = 310;
            break;
        case 114: case 115: case 116: case 117:
            if (employees >= 1 && employees <= 5) {
                document.AppForm.invest.value = 425;
                break;
            }
            else {
                if (employees >= 6 && employees <= 15) {
                    document.AppForm.invest.value = 495;
                    break;
                }
                else {
                    if (employees >= 16 && employees <= 30) {
                        document.AppForm.invest.value = 635;
                        break;
                    }
                }
            }
        default:
            if (employees == 1 || employees == 2) {
                document.AppForm.invest.value = 225;
            }
            else {
                if (employees >= 3 && employees <= 5) {
                    document.AppForm.invest.value = 255;
                }
                else {
                    if (employees >= 6 && employees <= 10) {
                        document.AppForm.invest.value = 310;
                    }
                    else {
                        if (employees >= 11 && employees <= 18) {
                            document.AppForm.invest.value = 380;
                        }
                        else {
                            if (employees >= 19 && employees <= 30) {
                                document.AppForm.invest.value = 455;
                            }
                            else {
                                if (employees >= 31 && employees <= 50) {
                                    document.AppForm.invest.value = 515;
                                }
                                else {
                                    if (employees >= 51 && employees <= 75) {
                                        document.AppForm.invest.value = 625;
                                    }
                                    else {
                                        alert("Please contact our office at 412-856-0622 to submit your application");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            break;
    }
    calc_total();    
}

function calc_listings() {
    listings = parseInt(document.AppForm.catlist.value) * 25;
    document.AppForm.listings.value=listings;
    calc_total();
}
function calc_total() {
    document.AppForm.tot.value = parseInt(document.AppForm.listings.value) + parseInt(document.AppForm.invest.value)+25;
}
function setName() {
    document.AppForm.cardname.value = document.AppForm.contact.value;
}
function check_form() {
    if (val_bus() && val_emp() && val_con() && val_tle() && val_adr() && val_cty() && val_zip() && val_phone() && val_eml() && val_bct() && val_bdr() && val_other()) {
        document.AppForm.submit();
    }
}
function val_bus(){
    if (document.AppForm.business.value == "") {
        alert("Please enter a value for the \"Business Name\" field.");
        document.AppForm.business.focus();
        return (false);
    }
    return (true);
}
function val_emp() {
    if (document.AppForm.business.value != "") {
         if ((document.AppForm.ftemploy.value == 0) && (document.AppForm.ptemploy.value == 0)) {
            alert("The number of employee fields cannot total zero.");
            document.AppForm.ftemploy.focus();
            return (false);
         }
         return (true);
     }
}
function val_con() {
    if ((document.AppForm.ftemploy.value != 0) || (document.AppForm.ptemploy.value != 0)){
       if (document.AppForm.contact.value == "") {
            alert("Please enter at value for the \"Contact Name\" field.");
            document.AppForm.contact.focus();
            return (false);
        }
        return (true);
    }
}
function val_tle() {
    if ((document.AppForm.business.value != "") && (document.AppForm.contact.value != "")) {
        if (document.AppForm.title.value == "") {
            alert("Please enter a value for the \"Title/Position\" field.");
            document.AppForm.title.focus();
            return (false);
        }
        return (true);
    }
}
function val_adr() {
    if ((document.AppForm.business.value != "") && (document.AppForm.contact.value != "") && (document.AppForm.title.value != "")) {
        if (document.AppForm.addr.value == "") {
            alert("Please enter a value for the \"Business Address\" field.");
            document.AppForm.addr.focus();
            return (false);
        }
        return (true);
    }
}
function val_cty() {
    if ((document.AppForm.business.value != "") && (document.AppForm.contact.value != "") && (document.AppForm.title.value != "") && (document.AppForm.addr.value != "")) {
        if (document.AppForm.city.value == "") {
            alert("Please enter a value for the \"City\" field.");
            document.AppForm.city.focus();
            return (false);
        }
        return (true);
    }
}
function val_zip() {
    if ((document.AppForm.business.value != "") && (document.AppForm.contact.value != "") && (document.AppForm.title.value != "") && (document.AppForm.addr.value != "") && (document.AppForm.city.value != "")) {
        if (document.AppForm.zip.value == "") {
            alert("Please enter a value for the \"Zip \" field.");
            document.AppForm.zip.focus();
            return (false);
        }
        if (document.AppForm.zip.value.length != 5) {
            alert("Please enter at valid Zip Code.");
            document.AppForm.zip.focus();
            return (false);
        }

        return (true);
    }
}
function val_phone() {

    if ((document.AppForm.business.value != "") && (document.AppForm.contact.value != "") && (document.AppForm.title.value != "") && (document.AppForm.addr.value != "") && (document.AppForm.city.value != "") && (document.AppForm.zip.value != "")) {
        
        if (document.AppForm.phone1.value == "") {
            alert("Please enter a value for the \"Phone (1)\" field.");
            document.AppForm.phone1.focus();
            return (false);
        }

        if (!isPhone(document.AppForm.phone1.value)) {
            document.AppForm.phone1.focus();
            return (false);
        }
        
        return (true);
    }
}
function val_eml() {
    if (document.AppForm.email.value == "") {
        alert("Please enter a value for the \"Email\" field.");
        document.AppForm.email.focus();
        return (false);
    }
    if (document.AppForm.email.value != "") {
        if (!(isEmail(document.AppForm.email.value))) {
            alert("Please enter a valid Email address.");
            document.AppForm.email.focus();
            return (false);
        }
    }
    return (true);
}
function val_bct() {
    if (document.AppForm.busncat.selectedIndex < 0) {
        alert("Please select one of the \"Business Category\" options.");
        document.AppForm.busncat.focus();
        return (false);
    }
    if (document.AppForm.busncat.selectedIndex == 0) {
        alert("The first \"Business Category\" option is not a valid selection.  Please choose one of the other options.");
        document.AppForm.busncat.focus();
        return (false);
    }
    return (true);
}
function val_bdr() {
    if (document.AppForm.busncat.selectedIndex > 0) {
        if (document.AppForm.descr.value == "") {
            alert("Please enter a value for the \"Brief description of business and services\" field.");
            document.AppForm.descr.focus();
            return (false);
        }
    }
    return (true);
}

function val_other() {
    if ((document.AppForm.phone2.value != "")) {
            if (!(isPhone(AppForm.phone2.value))) {
                alert("Please enter a valid Phone Number")
                document.AppForm.phone2.focus()
                return false
            }
        }
        if ((document.AppForm.fax.value != "")) {
            if (!(isPhone(AppForm.fax.value))) {
                alert("Please enter a valid Fax Number")
                document.AppForm.fax.focus()
                return false
            }
        }
        setinterests();
        if (document.AppForm.catlist.value > 0) {
          for (i = 1; i <= parseInt(document.AppForm.catlist.value); i++) {
              var fld1 = 'bcat' + i;
              if (document.getElementById(fld1).value == " ") {
                  alert("Please enter a value for the \"Additional Category\" field.");
                  document.getElementById(fld1).focus();
                  return (false);
              }
           }
        }
        payOption = -1;
        for (i = document.AppForm.pay.length - 1; i > -1; i--) {
            if (document.AppForm.pay[i].checked) {
                payOption = i;
            }
        }
        if (payOption == -1) {
            alert("Please select a Payment Method");
            return (false);
        }
        if (payOption > 1) {
            if (document.AppForm.ccnumber.value.length < 1) {
                alert("Please enter a valid Credit Card Number.");
                document.AppForm.ccnumber.focus();
                return (false);
            }
            if (document.AppForm.Exp_month.value == "") {
                alert("Please enter the Credit Card Expiration Date Month.");
                document.AppForm.Exp_month.focus();
                return (false);
            }
            if (document.AppForm.Exp_year.value == "") {
                alert("Please enter the Credit Card Expiration Date Year.");
                document.AppForm.Exp_year.focus();
                return (false);
            }
            if (document.AppForm.bill_address.value == "") {
                alert("Please enter a valid billing address.");
                document.AppForm.bill_address.focus();
                return (false);
            }
            if (document.AppForm.bill_city.value == "") {
                alert("Please enter the city for billing address.");
                document.AppForm.bill_city.focus();
                return (false);
            }
            if (document.AppForm.bill_zip.value == "") {
                alert("Please enter the zip code for billling address.");
                document.AppForm.bill_zip.focus();
                return (false);
            }
        }
        switch (payOption) {
            case 0:
                document.AppForm.pay_meth.value = "Check";
                break;
            case 1:
                document.AppForm.pay_meth.value = "Cash";
                break;
            case 2:
                document.AppForm.pay_meth.value = "VISA";
                break;
            case 3:
                document.AppForm.pay_meth.value = "MasterCard";
                break;
            case 4:
                document.AppForm.pay_meth.value = "Amex";
                break;
            case 5:
                document.AppForm.pay_meth.value = "Discover";
                break;
        }
        document.AppForm.autor.value = "No";
        if (document.AppForm.autorenew.checked) {
            document.AppForm.autor.value = "Yes";
        }
        return (true);
    }
    function setinterests() {
        document.AppForm.printr.value = "";
        if (document.AppForm.interests_0.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + "Network Opportunities ";
        }
        if (document.AppForm.interests_1.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Education Opportunities ";
        }
        if (document.AppForm.interests_2.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Committee Involvement ";
        }
        if (document.AppForm.interests_3.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Advertising Opportunities ";
        }
        if (document.AppForm.interests_4.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Member-2-Member Discounts ";
        }
        if (document.AppForm.interests_5.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Chamber Choice Insurance ";
        }
        if (document.AppForm.interests_6.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Advocacy ";
        }
        if (document.AppForm.interests_7.checked) {
            document.AppForm.printr.value = document.AppForm.printr.value + " Other - " + document.AppForm.other_desc.value;
        }
        
    }
    function isEmail(inputVal) {
        var inputStr = inputVal.toString();
        var foundat = false;
        var founddot = false;
        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar == "@") {
                foundat = true;
            }
            if (oneChar == ".") {
                founddot = true;
            }
        }
        if (foundat && founddot) {
            return true;
        }
        return false;
    }
    function isPhone(inputval) {

    var inputStr = inputval.toString();
    var foundat = false;

    if (inputStr.length != 13) {
        return false;
    }

    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (i == 0) {
            if (oneChar != "(") {
                return false;
            }
        }
        else {
            if (i == 4) {
                if (oneChar != ")") {
                    return false;
                }
            }
            else {
                if (i == 8) {
                    if (oneChar != "-") {
                        return false;
                    }
                }
                else {
                    if (oneChar < "0" || oneChar > "9") {
                        return false;
                    }
                }
            }
        }
    }

    return true;
}

