// JavaScript Document





//Greeting according to time of day
//===============================================================================
// DHTML functions, to create a common DHTML between browsers
//===============================================================================

//---------------------------------------------------------------------------------
// getBrowser - returns the browser type and version
// getObject  - gets an object id, returns a reference to the DHTML object
// getStyle   - gets an object id, returns a reference to the DHTML object's style
//---------------------------------------------------------------------------------

function getBrowser() {
    if (document.getElementById) return ( "W3C" );	//all new browsers       
    else if (document.all)       return ( "IE" );    	//IE4 +
    else if (document.layers)    return ( "N4" );	//N4  only
    else                         return ( "other" );
}

//---------------------------------------------------------------------------------
function getObj(objID) {
    var browser = getBrowser();
    if (browser == "W3C")        return( document.getElementById(objID) ); 
    if (browser == "IE")         return( document.all[objID]            ); 
    if (browser == "N4")         return( document.layers[objID]         ); 
}

//---------------------------------------------------------------------------------
function getStyle(objID) {
    var browser = getBrowser();
    if (browser == "W3C")        return( document.getElementById(objID).style ); 
    if (browser == "IE")         return( document.all[objID].style            ); 
    if (browser == "N4")         return( document.layers[objID]               ); 
}

//---------------------------------------------------------------------------------
function printGreeting() 
//---------------------------------------------------------------------------------
{
	time = new Date();				//get the data & time from system
	hour = time.getHours();				//get the hour portion

    	if (hour < 12)
	    document.write("Good Morning! ");
	else {
	    if (hour < 18)
		document.write("Good Afternoon! ");
	    else
		document.write("Good Evening! ");
	}
}





//===============================================================================
// Slideshow (ovenmamma on About Page)
//===============================================================================

//-------------- Change the following to customize your slide show ------------------

var imgName  = "images/ovenmamma";                      //URL of the images
var imgType  = ".gif";                             //type of image .gif, .jpg or .png
var total    =  4;                                //total number of images
var next     =  2;                                 //start image number
var repeat   =  false;                              //continue loop? true or false
var interval =  2000;                              //interval 2 second

//-------------- Do not alter the code below ----------------------------------------

var picArray = new Array();                        //Create Array to store images

for (var i = 1; i <= total; i++) {
    picArray[i]     =  new Image();                //Create an Image object 
    picArray[i].src = imgName + i + imgType;       //Load object with the .jpg file
}

function run() {
    document.slideShow.src = picArray[next].src;   //Move next image into slideShow
    next = next+1;
    if (repeat && next > total)                    //if repeat is requested
        next = 1;                                  //reset number to 1
    if (next > total)                    
        clearInterval(timer);                      //stop the show
}

function move(direction) {
    next += direction;
    if (next > total)
        next = 1;
    if (next < 1)
        next = total;
    document.slideShow.src = picArray[next].src;   //Move next image into slideShow
}
//===============================================================================
// Calculator (Menu Page)
//===============================================================================
function calculateTotal(frm) {
    var order_total = 0

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,4) == "prod") {

            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
    }

    // Display the total rounded to two decimal places
    frm.total.value = round_decimals(order_total, 2)
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

