/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
var isValid
function $(id){
return document.getElementById(id);
}
function dateDiff(startDate,endDate){
startDate= Date.parse(startDate);
endDate= Date.parse(endDate);
return (endDate-startDate)/(365.25*24*60*60*1000)
}
function validateForm(){
var c, message
resetInvalid();
isValid=true;
message = "The following fields are required or have errors.\n";
c = $("fo-email");
if(hasValues(c.value,"")==true) {
if(isEmailAddress(c.value)==false){
message += "\n - Email must contain a valid email address ie: jdoe@thompsoncigar.com.";
setInvalid(c);
}
}
c = $("fo-dob");
if(hasValues(c.value,"")==false) {
if(hasValues($("fo-email").value,"")==true) {
message += "\n - Date of Birth is empty.";
setInvalid(c);
}
} else if( isDateF(c.value)!= 'NA'){
//message += "\n - Date of Birth must in mm/dd/yyyy format. ie: 06/20/1974";
message += "\n - " + isDateF(c.value);
setInvalid(c);
} else if(dateDiff(c.value,new Date()) < 19){
message += "\n - You must be 19 or older to qualify";
setInvalid(c);
}else if(dateDiff(c.value,new Date()) > 119){
message += "\n - Thompson Cigar does not sell tobacco or tobacco related products to anyone under the age of 19";
setInvalid(c);
}
c = $("best");
if (hasValues(c.options[c.selectedIndex].value,"")==false) {
message += "\n - Please select Best Time to Call.";
setInvalid(c);
}
c = $("phone");
if(hasValues(c.value,"999-999-9999")==false){
message += "\n - Phone Number is empty.";
setInvalid(c);
} else if(isPhoneNumber(c.value)==false) {
message += "\n - Primary Phone number must be in ###-###-#### format.";
setInvalid(c);
}
c = $("cellphone");
if(c.value=="999-999-9999" || c.value.length==0) {
c.value = "";
} else if(!isPhoneNumber(c.value)) {
message += "\n - Mobile Phone number must be in ###-###-#### format.";
setInvalid(c);
}
c = $("lname");
if(hasValues(c.value,"")==false) {
message += "\n - Last Name is empty.";
setInvalid(c);
} else if(isAlphaNumeric(c.value)==false){
message += "\n - Name must contain only letters and numbers.";
setInvalid(c);
}
c = $("fname");
if(hasValues(c.value,"")==false) {
message += "\n - First Name is empty.";
setInvalid(c);
} else if(isAlphaNumeric(c.value)==false){
message += "\n - Name must contain only letters and numbers.";
setInvalid(c);
}
if (isValid == false) {
alert(message);
}
return isValid;
}
function isDateF(dateStr) {
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
var inmsg = 'NA';
if (matchArray == null) {
return 'Please enter your birth date as mm/dd/yyyy. Your current selection reads: ' + dateStr;
}
month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];
if (month < 1 || month > 12) { // check month range
return 'Month must be between 1 and 12.';
}
if (day < 1 || day > 31) {
return 'Day must be between 1 and 31.';
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return 'Month �'+ month + ' doesn`t have 31 days!';
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
return 'February ' + year + ' doesn`t have ' + day + ' days!';
}
}
return inmsg; // date is valid
}
function hasValues(value,emptyValue){
if (value==emptyValue){ return false; }
return true;
}
function isPhoneNumber(value){
//var re = /^\(?[1-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$\b/
var re = /^\d{3}-\d{3}-\d{4}$/
return re.test(value);
}
function isAlphaNumeric(value){
var re = /^([a-zA-Z0-9 .\b]+)$/
return re.test(value);
}
function isAlphaNumericBest(value){
var re = /^([a-zA-Z0-9 :.\b]+)$/
return re.test(value);
}
function isNumeric(value){
if(value==null){return true;} else {
var re = /^([0-9 \-.\b]+)$/
return re.test(value);
}
}
function isEmailAddress(value){
var re = /^(['_a-zA-Z0-9-]+)(\.['_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,5})$/
return re.test(value);
}
function isDate(value){
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
return re.test(value);
}
function setInvalid(c){
c.focus();
c.style.backgroundColor="#FFCCFF";
isValid = false;
}
function resetInvalid(){
$("fo-email").style.backgroundColor="white";
$("fo-dob").style.backgroundColor="white";
$("best").style.backgroundColor="white";
$("cellphone").style.backgroundColor="white";
$("phone").style.backgroundColor="white";
$("lname").style.backgroundColor="white";
$("fname").style.backgroundColor="white";
}
function getCharacter(e){
var character
if(window.event) {
return String.fromCharCode(e.keyCode);
} else if(e.which) {
return String.fromCharCode(e.which);
} else {
return 0;
}
}
function isChrEmail(e){
var character = getCharacter(e);
var re = /^([a-zA-Z0-9@_ .\b]+)$/
return re.test(character);
}
function isChrDate(e){
var character = getCharacter(e);
var re = /^([0-9\/ \-\b]+)$/
return re.test(character);
}
function isChrAlphaNumeric(e){
var character = getCharacter(e);
return isAlphaNumeric(character)
}
function isChrAlphaNumericBest(e){
var character = getCharacter(e);
return isAlphaNumericBest(character)
}
function isChrNumeric(e){
var character = getCharacter(e);
return isNumeric(character)
}
function setRequired(qualifier,id){
$(id).style.color=($(qualifier).value!="")?"red":"#fff4c1"
}

