function MM_jumpMenu(targ, selObj, restore){ //v3.0
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) 
        selObj.selectedIndex = 0;
}

function clearcell(todo){
    todo.value = "";
}

function validateForm(formobj){

    var bError = false;
    
    if (allEntered(document.forms.BookTable) == false) {
        bError = true;
    } else if (isValidDate(document.forms.BookTable.BookingDate.value) == false) {
        bError = true;
    } else if (isValidTime(document.forms.BookTable.BookingTime.value) == false) {
        bError = true;
    } else if (checkNumeric(document.forms.BookTable.Numbers, 1, 1000, '', '', '') == false) {
        bError = true;
    } else if (checkNumeric(document.forms.BookTable.Telephone, 1, 99999999999999999999, '', '', '-') == false) {
        bError = true;
    } else if (checkEmail() == false) {
        bError = true;
    }
    
    if (bError) {
        if (window.event) {
            event.returnValue = false;
        }
        return false;
    } else {
        return true;
    }
    
}


function allEntered(formobj){
    //1) Enter name of mandatory fields
    var fieldRequired = Array("CustomerName", "BookingDate", "BookingTime", "Numbers", "Telephone", "EmailAddress");
    //2) Enter field description to appear in the dialog box
    var fieldDescription = Array("Your Name", "Booking Date", "Booking Time", "Numbers in Party", "Telephone Number", "Email Address");
    //3) Enter dialog message
    var alertMsg = "Please complete the following fields:\n";
    
    var l_Msg = alertMsg.length;
    
    for (var i = 0; i < fieldRequired.length; i++) {
        var obj = formobj.elements[fieldRequired[i]];
        if (obj) {
            switch (obj.type) {
                case "select-one":
                    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "") {
                        alertMsg += " - " + fieldDescription[i] + "\n";
                    }
                    break;
                case "select-multiple":
                    if (obj.selectedIndex == -1) {
                        alertMsg += " - " + fieldDescription[i] + "\n";
                    }
                    break;
                case "text":
                case "textarea":
                    if (obj.value == "" || obj.value == null) {
                        alertMsg += " - " + fieldDescription[i] + "\n";
                    }
                    break;
                default:
                    if (obj.value == "" || obj.value == null) {
                        alertMsg += " - " + fieldDescription[i] + "\n";
                    }
            }
        }
    }
    
    if (alertMsg.length == l_Msg) {
        return true;
    } else {
        alert(alertMsg);
        return false;
    }
    
}

function isValidDate(dt){
    var sep = "";
    if (dt.indexOf('-') > -1) {
        sep = "-";
    } else {
        if (dt.indexOf('/') > -1) {
            sep = "/";
        }
    }
    
    arrdt = dt.split(sep);
    var day = arrdt[0];
    var month = arrdt[1] - 1;
    
    var date1 = new Date(arrdt[2], arrdt[1] - 1, arrdt[0]);

    if (date1.getMonth() != month || date1.getDate() != day) {
    
        alert('Please enter a correct date in the format dd/mm/yy');
        
        document.forms.BookTable.BookingDate.focus();
        document.forms.BookTable.BookingDate.value = "";
        
        return false
        
    } else {
    
        return true;
        
        
    }
}

function isValidTime(timeStr){

    var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
    
    var matchArray = timeStr.match(timePat);
    if (matchArray == null) {
        alert("Please enter a correct time in the format HH:MM");
        document.forms.BookTable.BookingTime.focus();
        document.forms.BookTable.BookingTime.value = "";
        return false;
    }
    hour = matchArray[1];
    minute = matchArray[2];
    second = matchArray[4];
    ampm = matchArray[6];
    
    if (second == "") {
        second = null;
    }
    if (ampm == "") {
        ampm = null
    }
    
    if (hour < 0 || hour > 23) {
        alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
        document.forms.BookTable.BookingTime.focus();
        document.forms.BookTable.BookingTime.value = "";
        return false;
    }
    if (hour <= 12 && ampm == null) {
        if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = 24hr Time")) {
            alert("You must specify AM or PM.");
            document.forms.BookTable.BookingTime.focus();
            document.forms.BookTable.BookingTime.value = "";
            return false;
        }
    }
    if (hour > 12 && ampm != null) {
        alert("You can't specify AM or PM for 24hr Time.");
        document.forms.BookTable.BookingTime.focus();
        document.forms.BookTable.BookingTime.value = "";
        return false;
    }
    if (minute < 0 || minute > 59) {
        alert("Minute must be between 0 and 59.");
        document.forms.BookTable.BookingTime.focus();
        document.forms.BookTable.BookingTime.value = "";
        return false;
    }
    if (second != null && (second < 0 || second > 59)) {
        alert("Second must be between 0 and 59.");
        document.forms.BookTable.BookingTime.focus();
        document.forms.BookTable.BookingTime.value = "";
        return false;
    }
    
    return true;
}

function checkNumeric(objName, minval, maxval, comma, period, hyphen){
    var numberfield = objName;
    if (chkNumeric(objName, minval, maxval, comma, period, hyphen) == false) {
        numberfield.select();
        numberfield.focus();
        numberfield.value = "";
        return false;
    } else {
        return true;
    }
}

function chkNumeric(objName, minval, maxval, comma, period, hyphen){
    // only allow 0-9 be entered, plus any values passed
    // (can be in any order, and don't have to be comma, period, or hyphen)
    // if all numbers allow commas, periods, hyphens or whatever,
    // just hard code it here and take out the passed parameters
    var checkOK = "0123456789" + comma + period + hyphen;
    var checkStr = objName;
    var allValid = true;
    var decPoints = 0;
    var allNum = "";
    
    for (i = 0; i < checkStr.value.length; i++) {
        ch = checkStr.value.charAt(i);
        for (j = 0; j < checkOK.length; j++) 
            if (ch == checkOK.charAt(j)) 
                break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
        if (ch != ",") 
            allNum += ch;
    }
    if (!allValid) {
        alertsay = "Please enter only these values \""
        alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
        alert(alertsay);
        return false;
    }
    
    // set the minimum and maximum
    var chkVal = allNum;
    var prsVal = parseInt(allNum);
    if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval)) {
        alertsay = "Please enter a value greater than or "
        alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
        alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
        alert(alertsay);
        return false;
    }
    
}

function checkEmail(){

    var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    string = document.forms.BookTable.EmailAddress.value;
    if (addressPattern.test(string) != true) {
        alert('Please enter a valid email address.');
        document.forms.BookTable.EmailAddress.focus();
        document.forms.BookTable.EmailAddress.value = "";
        return false;
    } else {
    
        return true;
        
    }
    
}


var highlightcolor = "#F1B9B9";
var texthighlightcolor = "#000";

var ns6 = document.getElementById && !document.all
var previous = ''
var eventobj

//Regular expression to highlight only form elements
var intended = /INPUT|TEXTAREA|SELECT|OPTION/

//Function to check whether element clicked is form element
function checkel(which){
    if (which.style && intended.test(which.tagName)) {
        if (ns6 && eventobj.nodeType == 3) 
            eventobj = eventobj.parentNode.parentNode
        return true
    } else 
        return false
}

//Function to highlight form element
function highlight(e, todo){

    if (todo.className.indexOf("b-time") !== -1) {
      todo.select();      
    }

    eventobj = ns6 ? e.target : event.srcElement
    if (previous != '') {
        if (checkel(previous)) 
            previous.style.backgroundColor = ''
            previous.style.color = ''
        previous = eventobj
        if (checkel(eventobj)) 
            eventobj.style.backgroundColor = highlightcolor
             eventobj.style.color = texthighlightcolor
    } else {
        if (checkel(eventobj)) 
            eventobj.style.backgroundColor = highlightcolor
            eventobj.style.color = texthighlightcolor
            
        previous = eventobj
    }
}


