﻿// JScript File

function showsegment()
{ //Show and Segment fields
    var pre = "ctl00_CPH_Main_mbrSignup_";
    var segment = document.getElementById(pre + "selSegment");
    var segment_index = segment.selectedIndex;
    var segmentValue = segment.options[segment_index].value;
    
    var lc = document.getElementById("LC");
    var cu = document.getElementById("CU");
    var k12 = document.getElementById("K12");
    var hc = document.getElementById("HC");
    var bi = document.getElementById("BI");
    var cr = document.getElementById("CR");
    var ve = document.getElementById("VE");
    /*var cds = document.getElementById("CDS");
    var mi = document.getElementById("MI");*/
       
    if (segmentValue == "K12"){
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'block';
        k12.style.visibility = 'visible';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "LC"){
        lc.style.display = 'block';
        lc.style.visibility = 'visible';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "CU"){
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'block';
        cu.style.visibility = 'visible';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "HC"){
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'block';
        hc.style.visibility = 'visible';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "BI"){
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';       
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'block';
        bi.style.visibility = 'visible';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "CR"){
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'block';
        cr.style.visibility = 'visible';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
     if (segmentValue == "VE")
    {
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'block';
        ve.style.visibility = 'visible';
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "CDS")
    {
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'block';
        cds.style.visibility = 'visible';        
        mi.style.display = 'none';
        mi.style.visibility = 'hidden';*/
    }
    if (segmentValue == "MI")
    {
        lc.style.display = 'none';
        lc.style.visibility = 'hidden';        
        cu.style.display = 'none';
        cu.style.visibility = 'hidden';        
        k12.style.display = 'none';
        k12.style.visibility = 'hidden';        
        hc.style.display = 'none';
        hc.style.visibility = 'hidden';        
        bi.style.display = 'none';
        bi.style.visibility = 'hidden';        
        cr.style.display = 'none';
        cr.style.visibility = 'hidden';        
        ve.style.display = 'none';
        ve.style.visibility = 'hidden';        
        /*cds.style.display = 'none';
        cds.style.visibility = 'hidden';        
        mi.style.display = 'block';
        mi.style.visibility = 'visible';*/
    }    
}

/* Sep 2009   O.N.                                                        */
/* this function is used in the FAFH signup page to toggle a hidden field */
/* takes a dropdownlist ID and an html element IDs and toggles visibility */
/* the dropdownList ID can also be a radio button list or any other       */
/* controlling element to be used to act as a toggle switch.  the first 2 */
/* parameters are required, but the third parameter is optional           */
/* this method was designed to display 2 hiddent td's as part of a tr row */
function showSelfOperated(ddListID, htmlElementID, optionalHtmlElementID)
{
    if (ddListID != null && ddListID != undefined && htmlElementID != null && htmlElementID != undefined)
    {
        var ddList = document.getElementById(ddListID);
        if (ddList != undefined && ddList != null)
        {
            var hiddenTag = document.getElementById(htmlElementID);            
            if (hiddenTag != null && hiddenTag != undefined)
            {
                if (ddList.options[ddList.selectedIndex].value.toUpperCase() == "NO")
                {
                    hiddenTag.style.display = 'block';
                    hiddenTag.style.visibility = 'visible';
                    if (typeof optionalHtmlElementID != "undefined")
                    {
                        var optionalHiddenTag = document.getElementById(optionalHtmlElementID);            
                        optionalHiddenTag.style.display = 'block';
                        optionalHiddenTag.style.visibility = 'visible';
                    }                
                }
                else /* YES */
                {
                    hiddenTag.style.display = 'none';
                    hiddenTag.style.visibility = 'hidden';
                    if (typeof optionalHtmlElementID != "undefined")
                    {
                        var optionalHiddenTag = document.getElementById(optionalHtmlElementID);            
                        optionalHiddenTag.style.display = 'none';
                        optionalHiddenTag.style.visibility = 'hidden';
                    }                
                }
            } 
        }
    }
}

/* Oct 2009   O.N.                                                        */
/* this function is used in the FAFH signup page to toggle a hidden field */
/* it takes the id of the radio button to toggle, and 1 or 2 IDs of div   */
/* tags which should be hidden or displayed                               */
function toggleHiddenDivwithRB(radioButtonID, requiredDivElementID, optionalDivElementID)
{
    var rbOwnershipClassChain = document.getElementById(radioButtonID);    
    var chainBrandName1 = document.getElementById(requiredDivElementID);
    // toggle visibility of div containing textbox
    if (rbOwnershipClassChain.checked && rbOwnershipClassChain.value.indexOf("rbLcChainOwned") >= 0)
    {
        chainBrandName1.style.display = 'block';
        chainBrandName1.style.visibility = 'visible';
        if (typeof optionalDivElementID != "undefined")
        {
            var chainBrandName2 = document.getElementById(optionalDivElementID);
            chainBrandName2.style.display = 'block';
            chainBrandName2.style.visibility = 'visible';
        }
    }
    else
    {
        chainBrandName1.style.display = 'none';
        chainBrandName1.style.visibility = 'hidden';
        if (typeof optionalDivElementID != "undefined")
        {
            var chainBrandName2 = document.getElementById(optionalDivElementID);
            chainBrandName2.style.display = 'none';
            chainBrandName2.style.visibility = 'hidden';
        }
    }
}

function validateform()
{ //Validate form fields
    //alert('inside validatteform');
    var pre = "ctl00_CPH_Main_mbrSignup_";
    var errmsg = "";
    var i;   
    var segment = document.getElementById(pre + "selSegment");
    var segment_index = segment.selectedIndex;
    var segmentValue = segment.options[segment_index].value;
    
    /* Member Basic Section Validation */
    
    var arrReqFields = new Array("txtTitle","txtFirst","txtLast","txtAddress","txtCity","txtState","txtZip","txtPhone","txtFax","txtEmail","txtUsername","txtPassword","selSegment","txtPassword2");
    
    //Loop through fields and validate
    for (i=0; i<arrReqFields.length; i++){
       var rField = pre + arrReqFields[i];
       if (rField == pre + "selSegment"){
            var rValue = segmentValue;
       }else{
            var rValue = document.getElementById(rField).value;
       }
       
       //Check all required fields
       if (rValue.length == 0){
            errmsg += "You must enter a " + rField.substring(pre.length+3,rField.length) + " \r\n";
       }
       
       //Check Zipcode is 5 digits
       if (rField == pre + "txtZip"){
            //alert("rField - " + rValue.length + "\r\n");
            //check if zip is a number
            if (IsNumeric(rValue) == false || rValue.length != 5){
                errmsg += "The " + rField.substring(pre.length+3,rField.length) + " Code field must be all numbers \r\n";
            }
       }
       
       //check email is valid format
       if (rField == pre + "txtEmail"){
            if (emailCheck(rValue) == false){
                errmsg += rField.substring(pre.length+3,rField.length) + " Address is not valid  \r\n";
            }
        }     
    }     
    
    //Check if passwords match
    var pwd1 = document.getElementById(pre + "txtPassword").value;
    var pwd2 = document.getElementById(pre + "txtPassword2").value;

    if (pwd1 != pwd2){
        errmsg += "Your passwords must match \r\n";
    }   
    
       
	//Check if username exists
	//checkUser();
	
	//Check if user email exists
	//checkUserEmail();
	
    
	//K12
	 if (segmentValue == "K12")
	 {
	 // unsure if ESROC knows what they want to make required so some code is commented out  K-12: E-mail, School Name, District Name, Contract Management Company, and Co-Op Membership
	 // txtSchoolName 
        var arrReqFieldsK12 = new Array("txtSchoolName","txtK12SchoolDistrict","txtK12NumSchools","txtK12BADP","txtK12DistrictEnrollment","txtK12LADP","txtK12SalesRep");
        var ddlK12SelfOp = document.getElementById(pre + "ddlK12SelfOperated");
        if (ddlK12SelfOp.options[ddlK12SelfOp.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsK12.push("txtk12ContractCo");
        }
        var ddlK12CoOp = document.getElementById(pre + "ddlK12CoOp");
        if (ddlK12CoOp.options[ddlK12CoOp.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsK12.push("txtk12CoOp");
        }
        var counter =0;
        for (i=0; i<arrReqFieldsK12.length; i++)
        {
           var rField = pre + arrReqFieldsK12[i];   
           var rValue = document.getElementById(rField).value;
           if (rField == pre + "txtSchoolName")
           {
                if (rValue.length == 0)
                {
                    errmsg += "K-12 School Name is a required field \r\n";
                }
           }
           if (rField == pre + "txtK12NumSchools"){
                //check if value is a number
                if (IsNumeric(rValue) == false && rValue.length != 0){
                    errmsg += "K-12 Number of School must be a number \r\n";
                }
           }
           if (rField == pre + "txtk12ContractCo")
           {
                if (rValue.length == 0)
                {
                    errmsg += "K-12 Contract Management Company is a required field \r\n";
                }
           }
           if (rField == pre + "txtK12SchoolDistrict")
           {
                if (rValue.length == 0)
                {
                    errmsg += "K-12 School District is a required field \r\n";
                }
           }                      
           if (rField == pre + "txtk12CoOp")
           {
                if (rValue.length == 0)
                {
                    errmsg += "K-12 Co-Op Membership is a required field \r\n";
                }
           }
           //Check all required fields
           if (rValue.length == 0){
                counter += 1;
           }
        }
        if (counter > 0){
            errmsg += "Complete all K-12 Schools Fields \r\n";   
            counter = 0;  
        }
        
    }
	
	//College & University
	if (segmentValue == "CU")
	{
	    var pre = "ctl00_CPH_Main_mbrSignup_";   
        var arrReqFieldsCU = new Array("txtCUname","txtCUstudentEnrollment","ddlDistributorNameCU","ddlDistributorStateCU","txtCUSalesRep");
        var ddlCUSelfOp = document.getElementById(pre + "ddlCUSelfOperated");
        if (ddlCUSelfOp.options[ddlCUSelfOp.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsCU.push("txtCUcmc");
        }

        var counter =0;
        for (i=0; i<arrReqFieldsCU.length; i++){ 
            var rField = pre + arrReqFieldsCU[i];               
            var rValue = document.getElementById(rField).value;            
                 
            //Check all required fields
            if (arrReqFieldsCU[i] == "ddlDistributorNameCU"){                
                var distName = document.getElementById(rField);
                var distName_index = distName.selectedIndex; 
                if (distName_index == 0){
                    counter += 1;
                }
            } 
            if (arrReqFieldsCU[i] == "ddlDistributorStateCU"){
                var distState = document.getElementById(rField);
                var distState_index = distState.selectedIndex; 
                if (distState_index == 0){
                    counter += 1;
                }
            } 
           if (arrReqFieldsCU[i] == "txtCUstudentEnrollment")
           {
                if (IsNumeric(rValue) == false)
                {
                    counter += 1;
                    errmsg += "Student Enrollment must be a numeric value \r\n";
                }
                if (rValue.length == 0)
                {
                    counter += 1;
                    errmsg += "Student Enrollment is a required field \r\n";
                }
           }
           if (arrReqFieldsCU[i] == "txtCUname")
           {
                if (rValue.length == 0)
                {
                    counter += 1;
                    errmsg += "College/University Name is a required field \r\n";
                }
           }
           if (arrReqFieldsCU[i] == "txtCUSalesRep")
           {
                if (rValue.length == 0)
                {
                    counter += 1;
                    errmsg += "Kellogg Sales Representative is a required field \r\n";
                }
           }           
           if (arrReqFieldsCU[i] == "txtCUcmc")
           {
                if (rValue.length == 0)
                {
                    counter += 1;
                    errmsg += "Contract Management Company is a required field \r\n";
                }
           }  
        }
        
        // Validate Check Boxes
        var chkCash = document.getElementById(pre + "chkCUopCash");
        var chkDining = document.getElementById(pre + "chkCUopDining");
        var chkCater = document.getElementById(pre + "chkCUopCater");
        
        if (chkCater.checked==false && chkDining.checked==false && chkCash.checked==false)
        {
            counter += 1;
        }
        if (counter > 0)
        {
            errmsg += "Complete all College & University Fields \r\n";   
            counter = 0;  
        }
    }        
    //Lodging & Catering
	if (segmentValue == "LC")
	{
        var arrReqFieldsLC = new Array("txtLCproperty","ddlDistributorNameLC","txtDistributorCityLC");
        arrReqFieldsLC.push("ddlLCdistributorState");
        arrReqFieldsLC.push("txtLCnumRooms");
        
        // check status of chain franchise owned drop down and add to array if necessary
        var ddlChainFranchise = document.getElementById(pre + "ddlLCChainFranchise");
        if (ddlChainFranchise.options[ddlChainFranchise.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsLC.push("txtLCChainFranchise");
        }

        // check status of management group drop down and add to array if necessary
        var ddlMgmtGroup = document.getElementById(pre + "ddlLCMgmtGrp");
        if (ddlMgmtGroup.options[ddlMgmtGroup.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsLC.push("txtLCMgmtGrp");
        }
        
        var counter =0;
        var rbOwnershipClassInd = document.getElementById(pre + "rbLcIndependent");
        var rbOwnershipClassChain = document.getElementById(pre + "rbLcChainOwned");
        var divChainBrandName = document.getElementById(pre + "divLcBrandName1");

        // ensure that one of the ownership classification radio buttons have been selected
        if (rbOwnershipClassInd.checked == false && rbOwnershipClassChain.checked == false)
        {
            counter += 1;
            errmsg += "Ownership Classifications is a required field \r\n";
        }
        // display chain brand name textbox 
        if (rbOwnershipClassChain.checked == true && divChainBrandName.style.visibility == 'visible')
        {
            arrReqFieldsLC.push("txtLCbrand");
        }

        for (i=0; i<arrReqFieldsLC.length; i++)
        {
            var rField = pre + arrReqFieldsLC[i];            
            var rValue = document.getElementById(rField).value;
                           
            //Check all required fields
            if (rValue.length == 0)
            {
                counter += 1;
            }
           // required field LC Chain Brand Name
           if (arrReqFieldsLC[i] == "txtLCbrand")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Chain Brand Name is a required field \r\n";
                }
           }           
           // required field LC Property Name
           if (arrReqFieldsLC[i] == "txtLCproperty")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Property Name is a required field \r\n";
                }
           }
           // check radio button group 
           if (arrReqFieldsLC[i] == "LcOwnershipClass")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Property Name is a required field \r\n";
                }
           }                                   
           //set state dropdown
           if (arrReqFieldsLC[i] == "ddlDistributorNameLC")
           {
                var distName = document.getElementById(rField);
                var distName_index = distName.selectedIndex; 
                if (distName_index == 0)
                {
                    errmsg += "Distributor Name is a required field \r\n";
                    counter += 1;
                }
           }
           //set state dropdown
           if (arrReqFieldsLC[i] == "ddlLCdistributorState")
           {
                var state = document.getElementById(rField);
                var state_index = state.selectedIndex; 
                if (state_index == 0)
                {
                    errmsg += "Distributor State is a required field \r\n";
                    counter += 1;
                }
           }
           //check if Number of Rooms value is a number
           if (arrReqFieldsLC[i] == "txtLCnumRooms")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Number of Rooms is a required field \r\n";
                }
                if (IsNumeric(rValue) == false)
                {
                    errmsg += "Number of Rooms must be a number \r\n";
                }                
           }
                        
           //check if Distributor City was specified
           if (arrReqFieldsLC[i] == "txtDistributorCityLC")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Distributor City is a required field \r\n";
                }
           }

           //check if chain franchise name was specified
           if (arrReqFieldsLC[i] == "txtLCChainFranchise")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Chain Franchise name is a required field \r\n";
                }
           }
           
           //check if management group name was specified
           if (arrReqFieldsLC[i] == "txtLCMgmtGrp")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Management Group name is a required field \r\n";
                }
           }          
        }            
        //check Full Service Kitchen
        var rLCkitchenYes = document.getElementById(pre + "rLCkitchenYes");
        var rLCkitchenNo = document.getElementById(pre + "rLCkitchenNo");            
        if (rLCkitchenYes.checked==false && rLCkitchenNo.checked==false)
        {
            counter += 1;
        }
        //check OnSite Catering
        var LCcateringYes = document.getElementById(pre + "LCcateringYes");
        var LCcateringNo = document.getElementById(pre + "LCcateringNo");            
        if (LCcateringYes.checked==false && LCcateringNo.checked==false)
        {
            counter += 1;
        }
        // check counter        
        if (counter > 0)
        {
            errmsg += "Complete all Lodging & Catering Fields \r\n";               
            counter = 0;  
        }
    }
    
	//Healthcare
    if (segmentValue == "HC"){
        var arrReqFieldsHC = new Array("txtFacilityName","txtPrimaryGPO","txtLicensedBedNum","txtEmployeeNum","ddlDistributorNameHC","txtDistributorCityHC","ddlDistributorStateHC");
        var ddlHCSelfOp = document.getElementById(pre + "ddlHCSelfOperated");
        var rbAcuteCare = document.getElementById(pre + "rAcute");
        var rbOutpatientCare = document.getElementById(pre + "rOutpatient");
        var rbLongTermCare = document.getElementById(pre + "rLongTerm");
        var rbOtherFacilityType = document.getElementById(pre + "rFTOther");
        var counter =0;

        // ensure that at least one of the facility type radio buttons are selected
        if (rbAcuteCare.checked == false && rbOutpatientCare.checked == false && rbLongTermCare.checked == false && rbOtherFacilityType.checked == false)
        {                
                counter += 1;
                errmsg += "Facility Type is a required field \r\n";
        }
        
        // if other radio button is checked, add text box other to array of required fileds
        if (rbOtherFacilityType.checked)
        {
            arrReqFieldsHC.push("txtOtherFacilityType");
        }
        
        if (ddlHCSelfOp.options[ddlHCSelfOp.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsHC.push("txtContractMgmt");
        }
        
        for (i=0; i<arrReqFieldsHC.length; i++){        
            var rField = pre + arrReqFieldsHC[i];            
            var rValue = document.getElementById(rField).value;
            
            //Check all required fields         
            if (rValue.length == 0){
                counter += 1;
            }

           // required field Other Facility Type (when facility type radio button is selected)
           if (arrReqFieldsHC[i] == "txtOtherFacilityType")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Please specify the other facility type \r\n";
                }
           }

           // required field Contract Management Company (when Self Operated is set to no)
           if (arrReqFieldsHC[i] == "txtContractMgmt")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Contract Management Company is a required field \r\n";
                }
           }
                       
           // required field LC Property Name
           if (arrReqFieldsHC[i] == "txtFacilityName")
           {
                if (rValue.length == 0)
                {
                    errmsg += "Facility Name is a required field \r\n";
                }
           }            
            
             //check if Number of Beds value is a number
            if (arrReqFieldsHC[i] == "txtLicensedBedNum" && rValue.length != 0){               
                if (IsNumeric(rValue) == false)
                {
                    errmsg += "Number of Beds must be a number \r\n";
                }
            } 
             //check if Number of Employees value is a number
            if (rField == pre + "txtEmployeeNum" && rValue.length != 0){               
                if (IsNumeric(rValue) == false){
                    errmsg += "Number of Employees must be a number \r\n";
                }
            }
            //set DistributorName dropdown
            if (arrReqFieldsHC[i] == "ddlDistributorNameHC"){
                var distName = document.getElementById(rField);
                var distName_index = distName.selectedIndex; 
                if (distName_index == 0){
                    counter += 1;
                }
            }
            //set state dropdown
            if (arrReqFieldsHC[i] == "ddlDistributorStateHC"){
                var state = document.getElementById(rField);
                var state_index = state.selectedIndex; 
                if (state_index == 0){
                    counter += 1;
                }
            }                        
        }
        //check Facility Type
        var chkRAcute = document.getElementById(pre + "rAcute");
        var chkROutp = document.getElementById(pre + "rOutpatient");
        var chkRLongt = document.getElementById(pre + "rLongTerm");
        var chkROther = document.getElementById(pre + "rFTOther");            
        var chkROtherDesc = document.getElementById(pre + "txtOtherFacilityType");
        if (chkRAcute.checked==false && chkROutp.checked==false && chkRLongt.checked==false && chkROther.checked==false){
            counter += 1;
        }
        if (chkROther.checked==true){
            if (chkROtherDesc.value == ""){
                counter += 1;
            }
        }
        // check counter
        if (counter > 0){            
            errmsg += "Complete all Healthcare Fields \r\n";   
            counter = 0;  
        }      
    }
    
    //Business
    if (segmentValue == "BI")
    {
        //alert('inside segment check on BI');
        /* the Self Operated dropdown value will drive what fields are required and which are not */
        var arrReqFieldsBI = new Array("txtEstablishmentNameBI","ddlDistributorNameBI","txtDistributorCityBI","ddlDistributorStateBI","txtBIBADP","txtBILADP");
        /* if BI self operated drop downlist has a value of no then we have to modify the required array */
        var ddlBISelfOp = document.getElementById(pre + "ddlBISelfOperated");
        if (ddlBISelfOp.options[ddlBISelfOp.selectedIndex].value.toUpperCase() == "NO")
        {
            arrReqFieldsBI.push("txtBIContractMgmt");
        }
        
        var counter =0;
        for (i=0; i<arrReqFieldsBI.length; i++)
        {        
            var rField = pre + arrReqFieldsBI[i];            
            var rValue = document.getElementById(rField).value;
            //Check all required fields         
            if (rValue.length == 0)
            {
                //set DistributorName dropdown
                if (arrReqFieldsBI[i] == "txtEstablishmentNameBI")
                {
                    counter += 1;
                    errmsg += "Establishment Name is a required field \r\n";
                }                
                //set DistributorName dropdown
                if (arrReqFieldsBI[i] == "txtBIContractMgmt")
                {
                    counter += 1;
                    errmsg += "Contract Management Company is a required field \r\n";
                }                      
            }             

            //set DistributorName dropdown
            if (arrReqFieldsBI[i] == "ddlDistributorNameBI")
            {
                var distName = document.getElementById(rField);
                var distName_index = distName.selectedIndex; 
                if (distName_index == 0)
                {
                    counter += 1;
                }
            }
            //set state dropdown
            if (arrReqFieldsBI[i] == "ddlDistributorStateBI")
            {
                var state = document.getElementById(rField);
                var state_index = state.selectedIndex; 
                if (state_index == 0)
                {
                    counter += 1;
                }
            }                        
        }        
        // check counter
        if (counter > 0){            
            errmsg += "Complete all Business & Industry Fields \r\n";   
            counter = 0;  
        }      
    }
    //Commercial Restaurants
    if (segmentValue == "CR")
    {
        var arrReqFieldsCR = new Array("txtEstablishmentNameCR", "txtFranchiseNameCR","txtFranchisesOwnedCR","ddlDistributorNameCR","txtDistributorCityCR","ddlDistributorStateCR","txtCRSalesRep");    
        var counter = 0;
        for (i=0; i<arrReqFieldsCR.length; i++)
        {        
            var rField = pre + arrReqFieldsCR[i];            
            var rValue = document.getElementById(rField).value;
            //Check all required fields         
            if (rValue.length == 0)
            {
                counter += 1;               
                 //check if Establishment value is filled out
                if (arrReqFieldsCR[i] == "txtEstablishmentNameCR")
                {            
                    errmsg += "Establishment Name is a required field \r\n";
                }
                if (arrReqFieldsCR[i] == "txtFranchisesOwnedCR")
                {
                    errmsg += "Number of Franchises Owned must be a number \r\n";
                }                            
            }            
            
             //check if Number of Franchises value is a number
            if (arrReqFieldsCR[i] == "txtFranchisesOwnedCR")
            {             
                if (IsNumeric(rValue) == false)
                {
                    errmsg += "Number of Franchises Owned must be a number \r\n";
                    counter += 1;
                }
            }
            
            //set DistributorName dropdown
            if (arrReqFieldsCR[i] == "ddlDistributorNameCR")
            {
                var distName = document.getElementById(rField);
                var distName_index = distName.selectedIndex; 
                if (distName_index == 0)
                {
                    counter += 1;   
                    errmsg += "Select a distributor \r\n";                 
                }
            }
            //set state dropdown
            if (arrReqFieldsCR[i] == "ddlDistributorStateCR")
            {
                var state = document.getElementById(rField);
                var state_index = state.selectedIndex; 
                if (state_index == 0)
                {
                    counter += 1;     
                    errmsg += "Select a distributor state \r\n";               
                }
            }
                                 
        }
        // check counter
        if (counter > 0)
        {            
            errmsg += "Complete all Commercial Restaurants Fields \r\n";   
            counter = 0;  
        }
    }

    //Vending
    if (segmentValue == "VE")
    {
        
        var arrReqFieldsVE = new Array("txtVendCompanyName","txtVendDistributors","txtVendDistributorsCity", "ddlVendDistributorsState","txtVendGlassFronts","txtVendHonorBoxes","txtCounterTops");
        var counter = 0;
        for (i=0; i< arrReqFieldsVE.length; i++)
        {
            var rField = pre + arrReqFieldsVE[i]; 
            
            var rValue = document.getElementById(rField).value;                    
            //Check all required fields         
            if (rValue.length == 0)
            {
                counter += 1;  
                if (arrReqFieldsVE[i] == "txtVendCompanyName")
                {
                    errmsg += "Company Name is a required field \r\n";                 
                }                             
                if (arrReqFieldsVE[i] == "txtVendGlassFronts")
                {
                    errmsg += "Enter a number for Number of Glass Fronts\r\n";                 
                }
                if (arrReqFieldsVE[i] == "txtVendHonorBoxes")
                {
                    errmsg += "Enter a number for Number of Honor Boxes\r\n";                 
                }
                if (arrReqFieldsVE[i] == "txtCounterTops")
                {
                    errmsg += "Enter a number for Number of Counter Tops\r\n";                 
                }                
            }            
            //set state dropdown
            if (arrReqFieldsVE[i] == "ddlVendDistributorsState")
            {
                var state = document.getElementById(rField);
                var state_index = state.selectedIndex; 
                if (state_index == 0)
                {
                    counter += 1;     
                    errmsg += "Select a distributor state \r\n";               
                }
            }                             
        }
        // check counter
        if (counter > 0)
        {            
            errmsg += "Complete all required Vending Fields \r\n";   
            counter = 0;  
        }    
    }

    //Convenience & Drug Store
    if (segmentValue == "CDS")
    {
    }
    
    //Military
    if (segmentValue == "MIL")
    {
    }
    
           
    if (errmsg != ""){
        Modalbox.show('<div align=\'center\' class=\'warning\'><input type=\'button\' value=\'' + errmsg + '\' onclick=\'Modalbox.hide()\' /></div>', {width: 400, title: 'Oops! There were Sign Up Errors'}); 
        return false;
    }else{
        return true;   
    }
    
    /* End Member Basic */ 
           
    
}    

var n;
var p;
var p1;
    
function ValidatePhone()
{
    var pre = "ctl00_CPH_Main_mbrSignup_";
    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+")";
	    document.getElementById(pre + "txtPhone").value="";
	    document.getElementById(pre + "txtPhone").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);
		    document.getElementById(pre + "txtPhone").value="";
		    document.getElementById(pre + "txtPhone").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);
	        document.getElementById(pre + "txtPhone").value="";
	        pp="("+p13+")"+p14+p15;
	        document.getElementById(pre + "txtPhone").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;
            document.getElementById(pre + "txtPhone").value="";
            document.getElementById(pre + "txtPhone").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()
}


//Fax Format
var nf;
var pf;
var p1f;

function ValidateFax()
{
    var pre = "ctl00_CPH_Main_mbrSignup_";    
    pf=p1f.value
    if(pf.length==3){
	    //d10=p.indexOf('(')
	    pp=pf;
	    d4=pf.indexOf('(')
	    d5=pf.indexOf(')')
	    if(d4==-1){
		    pp="("+pp;
	    }
	    if(d5==-1){
		    pp=pp+")";
	    }
	    //pp="("+pp+")";
	    document.getElementById(pre + "txtFax").value="";
	    document.getElementById(pre + "txtFax").value=pp;
    }
    if(pf.length>3){
	    d1=pf.indexOf('(')
	    d2=pf.indexOf(')')
	    if (d2==-1){
		    l30=pf.length;
		    p30=pf.substring(0,4);
		    //alert(p30);
		    p30=p30+")"
		    p31=pf.substring(4,l30);
		    pp=p30+p31;
		    //alert(p31);
		    document.getElementById(pre + "txtFax").value="";
		    document.getElementById(pre + "txtFax").value=pp;
	    }
	}
    if(pf.length>5){
	    p11=pf.substring(d1+1,d2);
	    if(p11.length>3){
	        p12=p11;
	        l12=p12.length;
	        l15=pf.length
	        //l12=l12-3
	        p13=p11.substring(0,3);
	        p14=p11.substring(3,l12);
	        p15=pf.substring(d2+1,l15);
	        document.getElementById(pre + "txtFax").value="";
	        pp="("+p13+")"+p14+p15;
	        document.getElementById(pre + "txtFax").value=pp;
	        //obj1.value="";
	        //obj1.value=pp;
	    }
        l16=pf.length;
        p16=pf.substring(d2+1,l16);
        l17=p16.length;
        if(l17>3&&p16.indexOf('-')==-1){
	        p17=pf.substring(d2+1,d2+4);
	        p18=pf.substring(d2+4,l16);
	        p19=pf.substring(0,d2+1);
	        //alert(p19);
            pp=p19+p17+"-"+p18;
            document.getElementById(pre + "txtFax").value="";
            document.getElementById(pre + "txtFax").value=pp;
            //obj1.value="";
            //obj1.value=pp;
        }
    }
    //}
    setTimeout(ValidateFax,100)
}
    
    function getItFax(m){
        nf=m.name;
        //p1=document.forms[0].elements[n]
        p1f=m
        ValidateFax()
    }
    
    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 IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


//Username Validation

//Check if username exists
    function checkUser()
    {
        var pre = "ctl00_CPH_Main_mbrSignup_";
		var username = document.getElementById(pre + "txtUsername").value;				
		var url = "checkmember.aspx?username="+username;				
		var http_request = false;

		    if (window.XMLHttpRequest) { // Mozilla, Safari,...
				    http_request = new XMLHttpRequest();
				    if (http_request.overrideMimeType) {
						    http_request.overrideMimeType('text/xml');
						    // See note below about this line
				    }
		    } else if (window.ActiveXObject) { // IE
				    try {
						    http_request = new ActiveXObject("Msxml2.XMLHTTP");
				    } catch (e) {
						    try {
								    http_request = new ActiveXObject("Microsoft.XMLHTTP");
						    } catch (e) {}
				    }
		    }

		    if (!http_request) {
				    alert('Giving up :( Cannot create an XMLHTTP instance');
				    return false;
		    }

		    http_request.onreadystatechange = function() { ajaxResponse_checkUser(http_request); };
		    http_request.open('GET', url, true);
		    http_request.send(null);		
	}
				
	function ajaxResponse_checkUser(http_request) {
			//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
			if ((http_request.readyState == 4) && (http_request.status == 200)) {					
				//document.getElementById('usercheck').innerHTML = http_request.responseText;
				var response = http_request.responseText;
				if (response == "True"){
				    alert("That Username is Already taken, Please enter a different username");
				    document.getElementById(pre + "txtUsername").focus();
				    return false;
				}else
				{				
				    return true;
				}
			}	
	}
	
	
	
	
	//Check if email address exists
    function checkUserEmail()
    {
        var pre = "ctl00_CPH_Main_mbrSignup_";   
		var email = document.getElementById(pre + "txtEmail").value;				
		var url = "checkmemberEmail.aspx?email="+email;				
		var http_request = false;

		    if (window.XMLHttpRequest) { // Mozilla, Safari,...
				    http_request = new XMLHttpRequest();
				    if (http_request.overrideMimeType) {
						    http_request.overrideMimeType('text/xml');
						    // See note below about this line
				    }
		    } else if (window.ActiveXObject) { // IE
				    try {
						    http_request = new ActiveXObject("Msxml2.XMLHTTP");
				    } catch (e) {
						    try {
								    http_request = new ActiveXObject("Microsoft.XMLHTTP");
						    } catch (e) {}
				    }
		    }

		    if (!http_request) {
				    alert('Giving up :( Cannot create an XMLHTTP instance');
				    return false;
		    }

		    http_request.onreadystatechange = function() { ajaxResponse_checkUserEmail(http_request); };
		    http_request.open('GET', url, true);
		    http_request.send(null);		
	}
				
	function ajaxResponse_checkUserEmail(http_request) 
	{
	        var pre = "ctl00_CPH_Main_mbrSignup_";
			//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
			if ((http_request.readyState == 4) && (http_request.status == 200)) {					
				//document.getElementById('usercheck').innerHTML = http_request.responseText;
				var response = http_request.responseText;
				if (response == "True"){
				    alert("That Email Address is Already taken, Please enter a different Email");
				    document.getElementById(pre + "txtEmail").focus();
				    return false;
				}else{
				
				    return true;
				}
			}	
	}




// email validation

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.toLowerCase().match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
//alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
//alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
//alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End -->