Monday, November 29, 2010

JavaScript Code Snippet 1

This is some JavaScript Code. This Project gave me some trouble but eventually with a little help I got it figured out.This is just the functions that were created there is a webpage that was created with this as well, but I feel the functions are more important to show than the webpage.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ITT Newspaper Subscription Form</title>
    <script type="text/javascript" >
        // project4.js

        //function to chek if the telephone field is all numberic

        function checkForNumber() {
            // concantenate phone number into a string
            var telephone = document.getElementById("phone").value + "-" + document.getElementById("phone2").value + "-" + document.getElementById("phone3").value;

            // Test the format of the phone number
            var pos = telephone.search(/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/);
            if (pos != 0) {
                alert("The phone number you entered (" + telephone + ") is not in the correct form. \n" +
    "The correct form is: 555-555-5555 \n" +
    "Please go back and fix your phone number");
                document.getElementById('phone').focus();
                document.getElementById('phone').select();
                return false;
            } else
                return true;
        }

        //function confirming all things have been selected
        function confirmSubmit() {

            if (checkBilling() != true) {
                return false;
            }

            if (checkForNumber() != true){
                return false;
            }
            if (confirmPassword() != true){
                return false;
            }
           
            if (payTime() != true) {
               return false;
            }
          

            return true;
        }

        //function to confirm user wants to reset the form
        function confirmReset() {
            alert("Are you Sure you want to Reset the Page?");
        }
        // function to confirm password
        function confirmPassword() {
            var init = document.getElementById("password1");
            var sec = document.getElementById("password2");
            if (init.value == "") {
                alert("You did not enter a password \n" +
          "Please enter one now");
                init.focus();
                return false;
            }
            if (init.value != sec.value) {
                alert("The two passwords you entered are not same \n" +
              "Please re-enter both now");
                init.focus();
                init.select();
                return false;
            } else
                return true;
        }
     

        function payTime() {
            var check;
            for (i = 0; i < document.getElementsByName("paymentTime").length; i++) {
                //document.getElementsByName("paymentTime")[i].checked;
                if (document.getElementsByName("paymentTime")[i].checked == true) {
                    return true;
                }
            }
    
                alert("You must select a delivery rate option")
                document.getElementById("4weeks").focus();
                document.getElementById("4weeks").select();
                return false;

            }
            function sameShippingInfo() {
               
                if (document.getElementById("shipping").checked == true) {
                    document.getElementById("shipname").value = document.getElementById("billname").value;
                    document.getElementById("shipaddress").value = document.getElementById("billaddress").value;
                    document.getElementById("shipcity").value = document.getElementById("billcity").value;
                    document.getElementById("shipstate").value = document.getElementById("billstate").value;
                    document.getElementById("shipzip").value = document.getElementById("billzip").value;
                }

            }
            function checkBilling() {
                var blank;
                for (i = 0; i < document.getElementsByName("billing").length; i++) {
                    document.getElementsByName("billing")[i];
                    if (document.getElementsByName("billing")[i].value == "") {

                        alert("You must enter all Billing and Shipping information")
                        -document.getElementsByName("billing")[i].focus();
                        document.getElementsByName("billing")[i].select();
                        return false;
                    }
                }return true;
                  
                   
               
            }

          
     </script>

</head>