// JavaScript for place_order page.
// ********************************

// Global Variables
// ----------------
var error_message
var error_message_body

// Functions
// ---------

function validate_order(current_form) {

	error_message		= "ERRORS\n=====\n"
	error_message_body	= ""
	
	var error_found = false
	
    // Ensure that the name, address and phone number fields are not blank.  If any of them are
    // then add the error to the error message.

	if (check_header_fields(current_form)) {
		error_message += error_message_body + "\n"
		error_found = true
	}

	// If no produce items have been selected then add to the error message. 

	if (!quantity_entered(current_form)) {
		error_message +=	"* You must select at least one item from the produce list"
		error_found = true
	}

	// If a non numeric value has been entered in one of the quantity fields then add
	// to the error message.

	if (quantity_alpha(current_form)) {
		error_message +=	"* You have entered text characters in one of the quantity fields." +
								"  You can only enter numbers."
		error_found = true
	}

	// If one or more errors was encountered than display the error message.  If there were no
	// errors then submit the form.
	
	if (error_found) {
		alert(error_message)
        current_form.cust_name.focus()
	}
	else {
		current_form.submit()
	}
}

function check_header_fields(current_form) {

	var test_result = false

	// The customer name must be non blank.
    if (field_empty(current_form.cust_name.value)) {
        error_message_body = "* You have left the Customer Name blank\n"
        test_result = true
    }

	// The customer address must be non blank.
	if (field_empty(current_form.cust_address.value)) {
        error_message_body += "* You have left the Customer Address blank\n"
        test_result = true
    }

	// The phone number must be non blank.
    if (field_empty(current_form.cust_phone.value)) {
        error_message_body += "* You have left the Phone number blank\n"
        test_result = true
    }

	// The customer name must be no greater than 50 chars.
    if (current_form.cust_name.value.length > 50) {
		error_message_body += "* The Customer Name must NOT be greater than 50 characters\n"
		test_result = true
	}

	// The customer address must be no greater than 50 chars.
    if (current_form.cust_address.value.length > 50) {
		error_message_body += "* The Customer Address must NOT be greater than 50 characters\n"
		test_result = true
	}

	// The phone number must be no greater than 20 chars.
    if (current_form.cust_phone.value.length > 20) {
		error_message_body += "* The Customer Phone number must NOT be greater than 20 characters\n"
		test_result = true
	}
	
	// The comments must be no greater than 250 chars.
    if (current_form.cust_comments.value.length > 250) {
		error_message_body += "* The Comments must NOT be greater than 250 characters\n"
		test_result = true
	}
    
    return test_result
}

function field_empty(input_string) {

	// Return True if the input_string is empty.
	if (input_string == "" || input_string == null) {
		return true
	}
}

function quantity_entered(current_form) {

	var test_result = false

	for (var i=0; i < current_form.length; i++) {
		if (current_form[i].value != "" && current_form[i].name.substring(0,4) == "txt_") {
			test_result = true
			break
		}
	}
	return test_result
}

function quantity_alpha(current_form) {

	var test_result = false
	
	for (var i=0; i < current_form.length; i++) {
		if (current_form[i].value != "" && 	current_form[i].name.substring(0,4) == "txt_" && 
			!its_floating_point(current_form[i].value)) {
			test_result = true
			break
		}
	}
	return test_result
}

function its_floating_point(string_value) {

	// Run through the rest of the characters in the string
	for (var counter = 0; counter < string_value.length; counter++) {

		current_char = string_value.charAt(counter)
		if (!its_a_digit_or_dot(current_char)) {
			return false
		}
	}
    
    // Otherwise, the string has nothing but digits, so return true
    return true
}

function its_a_digit_or_dot(character) {

	var floating_point_characters = "./0123456789"

	// If it's not in the floating_point_characters string, then it's
	// not a valid floating point character, so return false

	if (floating_point_characters.indexOf(character) == -1) {
		return false
	}

	// Otherwise, it's a digit, so return true
	return true
}

function go_to_top() {

    // go to the top of the window
    window.location = "#" + "top"
}

function reset_order(current_form,confirm_flag) {
	
	// Clear all form fields and then resubmit page resetting all session variables.
	
	var total_fields  = current_form.elements.length

	if (confirm_flag) {
		if (confirm("Are you sure you want to reset this Order.\n\nALL details will be cleared??")) {
			current_form.action = "place_order.asp?action=reset"
			current_form.submit()
		}
	}
	else {
		current_form.action = "place_order.asp?action=reset"
		current_form.submit()
	}
}

function popup_info_window() {

	// Display information window.
	
	var current_date	  = new Date()
	var start_date		  = new Date("December 10, 2003")
	var end_date		  = new Date("January 5, 2004")
		
//	alert("Today's date is " + current_date + "\n" +
//		  "Start date is   " + start_date + "\n" +
//		  "End date is     " + end_date)
	
    var window_width      = 450
    var window_height     = 250
    var window_left       = (screen.availWidth / 2) - (window_width /2)
    var window_top        = (screen.availHeight / 2) - (window_height /2)
    var window_features   = ""
    var window_dimensions = "height=" + window_height +
                            ",width=" + window_width  +
                            ",left=" + window_left    +
                            ",top=" + window_top

	if (current_date >= start_date && current_date <= end_date) {
		window.open("information.htm", "MyNewWindow", window_features + window_dimensions)
	}
}
