//***********************************************************
// The following script was copied by permission from:
//   http://home.flash.net/~timbobo/FunStuff/errorcheck.html
//
// Revised by: Maz Ameli
// Date: 2/09/2006
//***********************************************************

function showStatus(msg) {
		window.status = msg
		return true
	}

    // ================================================================================
    // NOTES ABOUT SCRIPT
    // ================================================================================
    // JavaScript Slick Form Validation
    // by Tim Bobo
    // Adapted loosely from a script by Paul Colton
    // if you use any of these functions please send me some e-mail (timbobo@leftbrainmedia.com) 
    // to let me know; I always like to know when someone likes my work.
    // Also give me a little credit in your code, like I have done for Paul Colton (above)     
    // and Danny Goodman (below).  If you make some improvements to my script, send them
    // my way.
    

    // ================================================================================
    // FUNCTION submit_pages() 
    // ================================================================================
    
    function submit_page(form) {
    
        foundError = false; // used at end of script to decide if to submit or not

        // ............................................................................
        // CHECK FIELDS FOR PROBLEMS
        // ............................................................................       
        
        // First Name
        firstnameError = "";  // if the script finds an error it places the message in this varible
        if(form.fname.value == "") {
            firstnameError = "First Name\n";
            foundError = true;
        }
        
        
        // Last Name
        lastnameError = "";  // if the script finds an error it places the message in this varible
        if(form.lname.value == "") {
            lastnameError = "Last Name\n";
            foundError = true;
        }
        
        // Address
        addressError = "";  // if the script find an error is places the message in this varible
        if(form.address.value == "") {
            addressError = "Address\n";
            foundError = true;
        }        
        
        
        phoneError = "";  // if the script find an error is places the message in this varible
        if(form.phone.value == "") {
            phoneError = "Best Contact phone #\n";
            foundError = true;
        }


        // ............................................................................
        // CHECK E-MAIL FIELD FOR PROBLEMS
        // ............................................................................
        
        emailError = ""  // if the scripts find an error it places the message in this varible
        
        // (1) MAKE SURE THE E-MAIL FIELD IS NOT BLANK

        if(form.email.value == "") {
            emailError = "E-mail\n";
            foundError = true;
        }

        // (2) NOW MAKE SURE THE E-MAIL FIELD IS VALID
        
        if((emailError == "") && (isValidEmail(form.email) == false)) { // see function below
            emailError = "Please enter a valid e-mail address.\n";
            foundError = true;
        }
               
        
        // ............................................................................
        // INFORM USER ABOUT PROBLEMS OR DISPLAY THANK YOU MESSAGE
        // ............................................................................

        if(foundError == false) {
             return true;
            // go to specific URL here.
        }
        else { // SELECT THE FIRST PROBLEM FIELD
            errorMessage = "You left some required fields blank. Please fill in the following fields: \n\n" + firstnameError + lastnameError + addressError + phoneError + emailError;
            alert (errorMessage)
            return false;                       
        }   
    }

    // ================================================================================
    // FUNCTION isValidEmail(theField)
    // ================================================================================
    
    // Check for a valid email address
    // This is not fool proof.  It simply looks for text before and after an @ symbol.
    // it calles on getFront & getEnd which are listed below this function
    
    function isValidEmail(theField) {
        if((getFront(theField.value,"@") != null) && (getEnd(theField.value,"@") != "")) {
            // alert (getFront(theField.value,"@"))
            // alert (getEnd(theField.value,"@"))
            return true; // the e-mail address is considered valid
        } else {
            return false; // the e-mail address is considered invalid
        }
    }
    
    // ================================================================================
    // FUNCTIONS getFront & getEnd
    // ================================================================================

    // The following 2 functions are adapted from Danny Goodman's JavaScript Handbook
    // and boy are they handy for a not much of a scripting language called JavaScript
    
    // ................................................................................
    // EXTRACT FRONT PART OF STRING PRIOR TO SEARCHSTRING
    // ................................................................................
    
    function getFront(mainStr,searchStr){
        foundOffset = mainStr.indexOf(searchStr)
        if (foundOffset <= 0) {
            return null // if the @ symbol is missing the value is -1
                        // if the @ symbol is the first char the value is 0
        } 
        else {
            return mainStr.substring(0,foundOffset)
        }
    }
    
    // ................................................................................
    // EXTRACT BACK END OF STRING AFTER TO SEARCHSTRING
    // ................................................................................
    
    function getEnd(mainStr,searchStr) {
        foundOffset = mainStr.indexOf(searchStr)
        if (foundOffset <= 0) {
            return ""   // if the @ symbol is missing the value is -1
                        // if the @ symbol is the first char the value is 0
                        // I switched this to return "" so it would match the result of an empty
                        // string below
        }
        else {
            return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
                        // if nothing exists after the @ symbol this will return an empty string
        }
    }
