function open_send_window() {

    var window_width      = 400
    var window_height     = 180
    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
    
    window.open("", "send_window", window_features + window_dimensions)
}

function setup(current_form) {
	
    if (check_errors(current_form)) {
    	return true    
    }

	open_send_window()
	document.contact_form.submit()
}

function check_errors(current_form) {

    // check to ensure that the user has entered ALL required fields.
    
    if (check_if_empty(current_form)) {
    	alert("You must fill out ALL of the fields before you can send your email.")
    	return true
    }
    
    // Check that they have selected an option from the "send to" drop-down list.
	if (current_form.email_to.selectedIndex == 0) {
		alert("You must select an option from the Send to drop-down list.")
		return true
    }
 
    // check to ensure that the email address entered is valid.
    
    if (!valid_email(current_form.email_address.value)) {
    	alert("You have entered an incorrect email address.")
    	return true
    }
        
    // Otherwise return FALSE
    return false
}

function check_if_empty(current_form) {
    
    // Loop through all the form elements and check that they have not been left blank.
    for (var counter = 0; counter < current_form.length; counter++) {
    	
    	// Is this a visible text field?
        if (current_form[counter].type == "text" || current_form[counter].type == "textarea" || current_form[counter].type == "password") {
        	if (current_form[counter].value == "") {
           		return true
            }
        }
    }
}

function valid_email(email_address) {

   	// Check the length
   	if (email_address.length < 5) {
       	return false
   	}
    
   	// Check @ and .
   	at_location = email_address.indexOf("@")
   	dot_location = email_address.lastIndexOf(".")
    
   	if (at_location == -1 || dot_location == -1 || at_location > dot_location ) {
   	    return false
   	}

   	// Is there at least one character before @?
   	if (at_location == 0) {
   	    return false
   	}
    
   	// Is there at least one character between @ and .?
   	if (dot_location - at_location < 2 ) {
   	    return false
   	}
	
   	// Is there at least one character after .?
   	if (email_address.length - dot_location < 2) {
   	    return false
   	}

   	// Otherwise, it's a valid address, so return true
   	return true
}
