﻿function popdates() {
    var today = new Date;
    var ddMo = document.forms[0].dDateMonth;
    var ddday = document.forms[0].dDateDay;
    var ddyr = document.forms[0].dDateYr;
    var days = 32 - new Date(ddyr.options[ddyr.selectedIndex].value, ddMo.options[ddMo.selectedIndex].value - 1, 32).getDate();
    ddday.options.length = 0;
    for (i = 1; i <= days; i++) {
        ddday.options[ddday.options.length] = new Option(i, i);
    }
}

function ValidateTime() {
    var thisTime = document.forms[0].dTime.options[document.forms[0].dTime.selectedIndex].value
    if (thisTime == " " || thisTime == "") {
        alert("Please select a time for your repairs")
        focus(document.forms[0].dTime)
        return false
    }
    return true
}

function isEmpty() {
    var strfield1 = document.forms[0].fName.value
    var strfield2 = document.forms[0].lName.value
    var strfield3 = document.forms[0].Day1.value
    var strfield4 = document.forms[0].Day2.value
    var strfield5 = document.forms[0].Day3.value
    var strfield6 = document.forms[0].Eve1.value
    var strfield7 = document.forms[0].Eve2.value
    var strfield8 = document.forms[0].Eve3.value
    var strfield9 = document.forms[0].Store.options[form1.Store.selectedIndex].value

    //First Name field
    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') {
        alert("\"First Name\" is a mandatory field.\nPlease amend and retry.")
        document.forms[0].fName.focus();
        return false;
    }

    //Las Name field 
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ') {
        alert("\"Last Name\" is a mandatory field.\nPlease amend and retry.")
        document.forms[0].lName.focus();
        return false;
    }

    //phone fields
    if ((strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ') || (strfield4 == "" || strfield4 == null || strfield4.charAt(0) == ' ') || (strfield5 == "" || strfield5 == null || strfield5.charAt(0) == ' ')) {
        if ((strfield6 == "" || strfield6 == null || strfield6.charAt(0) == ' ') || (strfield7 == "" || strfield7 == null || strfield7.charAt(0) == ' ') || (strfield8 == "" || strfield8 == null || strfield8.charAt(0) == ' ')) {
            alert(" Please enter either a \"Day Phone\" or \"Evening Phone\" or Both.")
            document.forms[0].Day1.focus();
            return false;
        }
    }

    if ((strfield3.length < 3 || strfield4.length < 3 || strfield5.length < 4) &&
     !((strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ') ||
     (strfield4 == "" || strfield4 == null || strfield4.charAt(0) == ' ') ||
     (strfield5 == "" || strfield5 == null || strfield5.charAt(0) == ' '))) {
        alert("The phone number in \"Day Phone\" is not vald.  Please amend and retry.")
        document.forms[0].Day1.focus();
        return false;
    }

    if ((strfield6.length < 3 || strfield7.length < 3 || strfield8.length < 4) &&
     !((strfield6 == "" || strfield6 == null || strfield6.charAt(0) == ' ') ||
     (strfield7 == "" || strfield7 == null || strfield7.charAt(0) == ' ') ||
     (strfield8 == "" || strfield8 == null || strfield8.charAt(0) == ' '))) {
        document.forms[0].Eve1.focus();
        alert("The phone number in \"Evening Phone\" is not vald.  Please amend and retry.")
        return false;
    }

    if (strfield9 == "" || strfield9 == null || strfield9.charAt(0) == ' ') {
        alert("Please select a repair location.")
        document.forms[0].Store.focus();
        return false;
    }



    return true;
}






//function to check valid email address
function isValidEmail(strEmail) {
    validRegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    strEmail = document.forms[0].Email.value;


    // search email text for regular exp matches
    var arr0 = []
    arr0 = strEmail.split("@")
    if (validRegExp.test(strEmail) == -1 || arr0[1] == "" || arr0[1] == null || arr0[1].charAt(0) == ' ' || arr0[1].indexOf('.') == -1) {
        alert('A valid e-mail address is required.\nPlease amend and retry');
        document.forms[0].Email.focus();
        return false;
    }
    var arr1 = []
    arr1 = arr0[1].split(".");
    if (arr0[0].length < 1 || arr1[0].length < 2 || arr1[1].length < 2) {
        alert('A valid e-mail address is required.\nPlease amend and retry');
        document.forms[0].Email.focus();
        return false;
    }

    return true;
}


//function that performs all functions, defined in the onsubmit event handler

function check() {
    if (isEmpty()) {
        if (isValidEmail()) {
            if (ValidateTime()) {
                return true;
            }
        }
    }
    return false;
}

function autoTab(element, nextElement) {
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}

function timeOps() {
    var ddMo = document.forms[0].dDateMonth;
    var ddYr = document.forms[0].dDateYr;
    var ddDay = document.forms[0].dDateDay;
    var strDate = ddMo.options[ddMo.selectedIndex].value + "/" + ddDay.options[ddDay.selectedIndex].value + "/" + ddYr.options[ddYr.selectedIndex].value;
    var thisDate = new Date(strDate)
    var timeBox = document.forms[0].dTime
    timeBox.options.length = 0
    if (thisDate.getDay() == 6) {
        timeBox.options[timeBox.options.length] = new Option('8am-12pm', '8am-12pm')
    }
    if (thisDate.getDay() == 0) {
        alert("We are not open on Sundays.  Please enter a different \"Desired Date\"")
    }
    if (thisDate.getDay() >= 1 && thisDate.getDay() <= 5) {
        timeBox.options[timeBox.options.length] = new Option('7:30am - 12pm', '7:30am - 12pm')
        timeBox.options[timeBox.options.length] = new Option('12pm - 5:30pm', '12pm - 5:30pm')
    }


}