﻿function Verify(form) {
    var objElement = GetElementByTagAndPartialID("input", "txtFirstName1");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your first name!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtLastName1");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your last name!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtAddress1");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your street address!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtCity");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your city!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtZip");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your zip code!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtEMail");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your email address!");
        objElement.focus();
        return false;
    }

    objElement = GetElementByTagAndPartialID("input", "txtPhone1");
    objElement.value = trim(objElement.value);
    if (objElement.value == "") {
        alert("Please fill in your primary phone number!");
        objElement.focus();
        return false;
    }

    return true;
}

function trim(stringvalue) {
    var intIndex = 0;
    for (intIndex = 0; intIndex < stringvalue.length && isWhitespace(stringvalue.charAt(intIndex)); intIndex++);
    var strLTrim = stringvalue.substring(intIndex, stringvalue.length);
    for (intIndex = strLTrim.length - 1; intIndex >= 0 && isWhitespace(strLTrim.charAt(intIndex)); intIndex--);
    return strLTrim.substring(0, intIndex + 1);
}

function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
}

function GetElementByTagAndPartialID(tagname, partialID) {

    var elementsWithTag = document.getElementsByTagName(tagname);
    var elementFound = null;

    if (elementsWithTag != null) {
        for (i = 0; i < elementsWithTag.length; i++) {
            if (elementsWithTag[i].id.indexOf(partialID) > -1) {
                elementFound = elementsWithTag[i];
                break;
            }
        }
    }

    return elementFound;
}

function PhoneConf(txt, typ, required) {
    var cmp, tst, newtxt, ans

    cmp = '0123456789'				// digit comparison
    newtxt = ''

    // Check if it is all digits
    if (parseInt(txt) != txt) {
        // phone number has been formatted in some manner.  Strip formatting out.
        //	alert('formatting exists'+txt);
        for (var i = 0; i < txt.length; i++) {
            tst = txt.substring(i, i + 1);
            ans = cmp.indexOf(tst);
            if (cmp.indexOf(tst) >= 0) {			// This character is a digit
                newtxt += tst; 				// So add it to the new string
            }
        }
    } else {
        // It is all digits, so just set it for formatting.
        newtxt = txt;
    }


    // Input has been reduced to digits.  Now format it with dashes.
    //	If the number is too short, notify user and return.
    // alert("preformat="+newtxt);

    if (required == 1 && newtxt.length == 0) {
        alert('A valid phone number is required.');
        return '';
    }

    if (newtxt.length == 0) {
        return '';
    }

    if (newtxt.length < 7) {
        alert('That phone number has too few digits.')
        return txt;
    }

    //	number has at least 10 digits - US format.
    if (newtxt.length < 10) {
        newtxt = newtxt.substring(0, 3) + '-' + newtxt.substring(3, 7)
    } else {
        if (typ == 1) {								// Type One is format: (xxx) xxx-xxxx
            newtxt = '(' + newtxt.substring(0, 3) + ') ' + newtxt.substring(3, 6) + '-' + txt.substring(6, 10)
        }

        if (typ == 2) {								// Type Two is format: xxx-xxx-xxxx
            // just deal with the first 10 digits of the number.  Ignore the rest.
            newtxt = newtxt.substring(0, 3) + '-' + newtxt.substring(3, 6) + '-' + newtxt.substring(6, 10)
        }
    }
    return newtxt;
}

