
var http = getXMLHTTPRequest();

function getXMLHTTPRequest() {
  try {
    req = new XMLHttpRequest();
  } catch(err1) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err2) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (err3) {
        alert("Your browser does not support AJAX!");
        req = false;
      }
    }
  }
  return req;
}

function Reset_1 () {
        window.acInit=false;

        var CR = escape( document.getElementById('Contact_Reason').value );
        var PT = escape( document.getElementById('Product_Type').value );

        var attrs = 'Action=Step_1&Product_Type='+PT+'&Contact_Reason='+CR  ;
        var myurl = '/customer-service/ContactUs.cgi?'+attrs;

        myRand = parseInt(Math.random()*999999999999999);

        var modurl = myurl+"&r="+myRand;

        http.open("GET", modurl, true);
        http.onreadystatechange = useHttpResponse;
        http.send(null);

}

function Step_1 ( flag ) {

    var errorCount = 0 ;
    document.getElementById('step_1_feedback').innerHTML = 'All values with an * are required.' ;

    if ( parseInt(document.csForm.Contact_Reason.selectedIndex) == 0 ) {
        document.getElementById('F_Contact_Reason').innerHTML = 'Required.' ;
        errorCount++ ;
    } else {
        document.getElementById('F_Contact_Reason').innerHTML = '' ;
    } 

    if ( parseInt(document.csForm.Product_Type.selectedIndex) == 0 ) {
        document.getElementById('F_Product_Type').innerHTML = 'Required.' ;
        errorCount++ ;
    } else {
        document.getElementById('F_Product_Type').innerHTML = '' ;
    }

    if ( document.getElementById('Product_Type').value == 'Full Order Form' ) {
        if ( document.getElementById('Contact_Reason').value != 'Order Online' ) {
             alert ( '"Contact Reason" has been changed to "Order Online"' ) ;
             document.getElementById('Contact_Reason').value = 'Order Online' 
        }
    }

    var myurl = '' ;
    if ( flag == 'button' ) {
        if ( errorCount == 0 ) {

            window.acInit=false;

            document.getElementById('step_1_feedback').innerHTML = '<img src="/images/wait20trans.gif">';
            document.getElementById('main').display = 'none';

            var CR = escape( document.getElementById('Contact_Reason').options[document.getElementById('Contact_Reason').selectedIndex ].innerHTML );
            var PT = escape( document.getElementById('Product_Type').options[document.getElementById('Product_Type').selectedIndex ].innerHTML );

            var attrs = 'Action=Step_2&Product_Type='+PT+'&Contact_Reason='+CR  ;
            myurl = '/customer-service/ContactUs.cgi?'+attrs;

            myRand = parseInt(Math.random()*999999999999999);

            var modurl = myurl+"&r="+myRand;

            http.open("GET", modurl, true);
            http.onreadystatechange = useHttpResponse;
            http.send(null);
        } else {
            document.getElementById('step_1_feedback').innerHTML = 'Please correct any noted issues in your form.' ;
            alert ('Please correct the noted issues in your data.');
        }
    }
    if ( errorCount == 0 && myurl.length == 0 ) {
        document.getElementById('step_1_feedback').innerHTML = 'This form can now be submitted.' ;
    }
}


function Step_2 ( flag ) {

    if ( document.getElementById('step_2_feedback') ) {
        document.getElementById('step_2_feedback').innerHTML = 'All values with an * are required.' ;
    }
    var errorString = '' ;
    var Divs = document.getElementsByTagName('div') ;
    var CheckId = new Array() ;
    var AttrList = '' ;
    var a = 0 ;
    var b = 1 ;
    var errorCount = 0 ;
    for ( i = 0 ; i < Divs.length ; i++ ) {
        if ( Divs[i].id.substr(0,2) == 'F_' ) {
            CheckId[a] = Divs[i].id.substr(2) ;
            a++ ;
            document.getElementById( Divs[i].id )
        } 
    }
    
    for ( i = 0 ; i < CheckId.length ; i++ ) {
        
        if ( document.getElementById(CheckId[i]).value.length == 0 ) {

            if ( document.getElementById(CheckId[i]).options ) {
                if ( document.getElementById(CheckId[i]).selectedIndex == 0 ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = 'Required' ;
                    errorCount++ ;
                } else {
                    document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
                    var IDValue = document.getElementById(CheckId[i]).options[ document.getElementById(CheckId[i]).selectedIndex ].innerHTML ;

                    AttrList += '&F_'+ b + '_'+CheckId[i] +'='+ escape(IDValue ) ;
                    b++ ;
                }
            } else {
                if ( CheckId[i] == 'Address_2' ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
                } else if ( CheckId[i] == 'Office_Name' ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
                } else if ( CheckId[i] == 'Fax_Number' ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
                } else if ( CheckId[i] == 'WP_Account_No' ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
                } else {
                    document.getElementById('F_'+CheckId[i]).innerHTML = 'Required' ;
                    errorCount++ ;
                }
            }
        } else {
            document.getElementById('F_'+CheckId[i]).innerHTML = '' ;
            AttrList += '&F_'+ b + '_'+CheckId[i] +'='+ escape(document.getElementById(CheckId[i]).value) ;
            b++ ;
        }

        if ( CheckId[i] == 'Zip_Code' ) {
            if ( document.getElementById( CheckId[i] ).value.length > 0 )  {

                strng = document.getElementById( CheckId[i] ).value ;

                if ( strng.length < 5 ) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = "Please enter a proper code";
                    errorCount++ ;
                } 

            }   
            
        }

        if ( CheckId[i] == 'Email' ) {
            if ( document.getElementById( CheckId[i] ).value.length > 0 )  {

                strng =  document.getElementById( CheckId[i] ).value ;
                // var email_regex = new RegExp("^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$");
                var email_regex = new RegExp("^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$");

                if (!(email_regex.test(strng))) { 
                    document.getElementById('F_'+CheckId[i]).innerHTML = "Email Format ______@____.___" ;
                    errorCount++ ;
                } else {
                    AttrList += '&Email='+ escape( strng ) ;
                }
            }   
        }
        if ( CheckId[i] == 'V_Email' ) {
            if ( document.getElementById( CheckId[i] ).value.length > 0 )  {
                if (!( document.getElementById( CheckId[i] ).value == document.getElementById( 'Email' ).value )) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = "Must match Email Address" ;
                    errorCount++ ;
                }
            }   
        }

        if ( CheckId[i] == 'Phone_Number' ) {
            var fonError = '' ;
            if ( document.getElementById( CheckId[i] ).value.length > 0 )  {

                strng =  document.getElementById( CheckId[i] ).value ;

                var valid = "0123456789-";
                var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
                //strip out acceptable non-numeric characters
                if (isNaN(parseInt(stripped))) {
                    document.getElementById('F_'+CheckId[i]).innerHTML =  "The phone number contains illegal characters.";
                    errorCount++ ;
                }

                if (!(stripped.length == 10)) {
                    document.getElementById('F_'+CheckId[i]).innerHTML = 
                         "Phone Number format<br />(123) 456-7890<br />"+
        "  We do not sell, rent, or trade your personal information to third parties."+
        '  Please review our <a class="underline" target="new" href="/privacy-policy.html">privacy policy</a> for details.';
                    errorCount++ ;
                }
            }   
        }
    }

    var myurl = '';
    if ( flag == 'button' ) {
        if ( errorCount == 0 ) {

                var attrs = 'Action=FinalData'+AttrList
                myurl = '/customer-service/ContactUs.cgi';

                http.open("POST", myurl, true);

                http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                http.setRequestHeader("Content-length", attrs.length);
                http.setRequestHeader("Connection", "close");

                http.onreadystatechange = useHttpResponse;
                http.send(attrs);

        } else {
            document.getElementById('step_2_feedback').innerHTML = 'Please correct any noted issues in your form.' ;
            alert ('Please correct the noted issues in your data.');
        }
    }
    if ( errorCount == 0 && myurl.length == 0 ) {
        if ( document.getElementById('step_2_feedback') ) {
            document.getElementById('step_2_feedback').innerHTML = 'This form can now be submitted.' ;
        }
    }
}


function useHttpResponse() {
        document.getElementById('cgiFeedback').innerHTML = 'in useHttpResponse' ;
  if (http.readyState == 4) {
    if(http.status == 200) {
      if ( http.responseText == null ) {
        document.getElementById('cgiFeedback').innerHTML = 'server error'
      } else {
        document.getElementById('cgiFeedback').innerHTML = http.responseText ;
      }
    }
  } else {
     document.getElementById('cgiFeedback').innerHTML = '<img src="/images/wait20trans.gif">';
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////


function PopTextJS( obj ) {

    var HtmlText = FindNumberHtml();
    var coors = findPos(obj);
    var objLeft = coors[0] -450 ; /** shift the image to the left to clear the text box **/
    var x = document.getElementById('PopText');
    var aScrollOffsets = getScrollXY();
    var fullHeight = getViewportHeight();
    var linkDown = fullHeight + aScrollOffsets[1] - coors[1] ;
    
    //  fullHeight      = the viewable vertical distance
    //  ScrollOffset[1] = the number of pixels scrolled down the page
    //  coors[1]        = vertical distance down the page (includes space scrolled off the page) of this obj 
    //  linkDown        = vertical visible distance down the page of this obj 

    var divHigh = 330 ; // (( HtmlText.length / 15  ) * 16 ) + 60 ;

 //   var divHigh  = divHighPad ;
    var objTop = coors[1];

    //  if the bottom of the popup window is off the visible page
    if ( coors[1] + divHigh > aScrollOffsets[1] + fullHeight ) {
         objTop = aScrollOffsets[1] + (( fullHeight - divHigh ) / 2 )  ;
    }    

    var inHtml   = '&nbsp;<div style="text-align: center; padding: 10px; border: thin solid #000000; width: 280px; height: '+divHigh+'px;'+
                      'background-color: #ffffff ; position: absolute; '+
                      'left: '+objLeft+'px; top: '+objTop+'px; ">'+unescape(HtmlText) +'</div>';

    document.getElementById('PopText').innerHTML = inHtml ;

}

function UnpopTextJS() {
    document.getElementById('PopText').innerHTML = '&nbsp;';
}

function FindNumberHtml () {
    var Number = '' ;
    Number += ' <h3>WP-360</h3>';
    Number += '  <p>To find the serial and model number you will need to remove the reservoir.  </p>';
    Number += '  <ol>';
    Number += '    <li>Lift the reservoir lid and push it back towards where the jet tip attaches. </li>';
    Number += '    <li>Push the lid until the blue reservoir separates from the white unit. </li>';
    Number += '    <li>This should separate the two parts. </li>';
    Number += '    <li>The serial and number is located on the base of the white section of the unit. ';
    Number += '    The information is molded into the plastic. </li>';
    Number += '  </ol>';
    Number += '  <h3>WP-450  </h3>';
    Number += '  <p>The model and serial number are located on the label on the bottom of the ';
    Number += '  reservoir and on the motor housing under the reservoir.  </p>';
    Number += '  <h3>WP-60/65/70/100 and all other counter top models  </h3>';
    Number += '  <p>The model and serial number are located on the bottom of the dental water jet. ';
    Number += '  Newer models have a label, older models the information is hot stamped into the plastic. </p>';
    return(escape(Number));
}




