// create a global variable for use in XMLHttpRequest functions
var xhr;

// prevent the 'enter' button from submitting forms
//var nav = window.Event ? true : false;
 var browserCheck = (document.all) ? true : false // only IE has document.all
 if (!browserCheck) { // the browser is not IE
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else { // the browser is IE
   document.onkeydown = MicrosoftEventHandler_KeyDown;
 } 
 function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit' && e.target.id == 'searchField') { return false; }
  return true;
}
function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit' && event.srcElement.id == 'searchField') {
    return false;
  }
  return true;
}

/*  the getContent function requires:
    selected_id - to be used for database queries
    target - the AJAX page being called
    elementId - the id of the element into which the AJAX content will be written */
function getContent(selected_id, target, elementId) {
    // call the function that checks for XMLHttpRequest compatibility
    xhr=GetXmlHttpObject();
    // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
    if (xhr==null) {
        alert ("Your browser configuration does not support HTTP Requests (AJAX)");
        return;
    }
    // if xhr was anything but null, that means the browser is good to go and the function continues
    
    tempClose(elementId);
    
    // set the url variable to be the target, plus any _GET variables
    // a random number is appended to prevent caching complications
    var url="_ajax/"+target;
    url=url+"?selected_id="+selected_id;
    url=url+"&sid="+Math.random();

    // call the function that processes the XMLHttpRequest
    processXhr(elementId,url)
}

function updateChowInfo() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var newName = encodeURIComponent(document.getElementById("name").value);
    var newServingId = document.getElementById("serving_id").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        if (newName=='') {
            alert("Error!\n\nCheck the chow name and try again.");
            return false;
        } else {
            if (newServingId=='') {
            alert("Error!\n\nCheck the serving size and try again.");
            return false;
            } else {
                // call the function that runs the AJAX to update the database
                processChowInfoUpdate(selected_id, newServingId, newName, 'process_Chow_Info_Update.php', 'chow_info_area')
                // By returning false, we prevent the form from actually getting submitted.
                return false;
            }
        }
    }

    /*  the processChowInfoUpdate function requires:
        selected_id - to be used for database queries
        newServingId - the new serving id to be added to the database
        newName - the new name to be added to the database
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processChowInfoUpdate(selected_id, newServingId, newName, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&newServingId="+newServingId;
        url=url+"&newName="+newName;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function updateChowNote() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var newNotes = encodeURIComponent(document.getElementById("notes").value);
    
    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        // call the function that runs the AJAX to update the database
        processNotesUpdate(selected_id, newNotes, 'process_Chow_Notes_Update.php', 'notes_area')
        // By returning false, we prevent the form from actually getting submitted.
        return false;
    }

    /*  the processNotesUpdate function requires:
        selected_id - to be used for database queries
        newNotes - the new notes to be added to the database
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processNotesUpdate(selected_id, newNotes, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&newNotes="+newNotes;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

/*  the updateNutritionDataForChow function requires:
    evt - the object that called this function as an event handler
*/
function updateNutritionDataForChow(evt) {
    var browserCheck = (document.all) ? true : false // only IE has document.all
    
    if (!browserCheck) { // the browser is not IE
    // get the object that called this function as an event handler
        var activeForm = evt.target;
    } else { // the browser is IE
    // get the object that called this function as an event handler
         var activeForm = window.event.srcElement;
    }
    
    // I have had trouble with using (evt) to determine browser functionality, so this is disabled and replaced with the above browsercheck.
    // get the object that called this function as an event handler
    //var activeForm = (evt) ? evt.target : window.event.srcElement; 
    
    // get the id of that object
    var formId = activeForm.id;
    // strip out all characters from the id except the integers that appear at the end
    var formNum = formId.match(/\d+$/);
    
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var nutr_id = document.getElementById("nutr_id"+formNum).value;
    var newNutrValue = document.getElementById("nutr_value"+formNum).value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        if (nutr_id=='') {
            alert("Error!\n\nCheck the nutrition type and try again.");
            return false;
        } else {
            if (newNutrValue=='') {
            alert("Error!\n\nCheck the nutrition value and try again.");
            return false;
            } else {
                // call the function that runs the AJAX to update the database
                processNutritionDataUpdate(selected_id, nutr_id, newNutrValue, 'process_Nutrition_Data_Update_For_Chow.php', 'nutrition_data_area')
                // By returning false, we prevent the form from actually getting submitted.
                return false;
            }
        }
    }

    /*  the processNutritionDataUpdate function requires:
        selected_id - to be used for database queries
        nutr_id - the nutrition id to be edited
        newNutrValue - the new value for the selected nutrition entry
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processNutritionDataUpdate(selected_id, nutr_id, newNutrValue, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&nutrId="+nutr_id;
        url=url+"&newNutrValue="+newNutrValue;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function updateIngredientQuantity(evt) {
    // get the object that called this function as an event handler
    var activeForm = (evt) ? evt.target : window.event.srcElement;
    // get the id of that object
    var formId = activeForm.id;
    // strip out all characters from the id except the integers that appear at the end
    var formNum = formId.match(/\d+$/);
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("ingredient_id"+formNum).value;
    var chowId = document.getElementById("chow_id").value;
    var quantity = document.getElementById("ingredient_amount"+formNum).value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected ingredient and try again.");
        return false;
    } else {
        if (chow_id=='') {
            alert("Error!\n\nCheck the recipe and try again.");
            return false;
        } else {
            if (quantity=='') {
                alert("Error!\n\nCheck the quantity and try again.");
                return false;
            } else {
                // call the function that runs the AJAX to update the database
                processUpdateIngredientQuantity(chowId, quantity, selected_id, 'process_Recipe_Ingredient_Quantity_Update.php', 'nutrition_data_area')
                // By returning false, we prevent the form from actually getting submitted.
                return false;
            }
        }
    }

    /*  the processUpdateIngredientQuantity function requires:
        chowId - to be used for database queries
        quantity - to be used for database queries
        selected_id - to be used for database queries
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processUpdateIngredientQuantity(chowId, quantity, selected_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?chow_id="+chowId;
        url=url+"&quantity="+quantity;
        url=url+"&selected_id="+selected_id;
        url=url+"&sid="+Math.random();
    
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

/*  the processAddIngredient function requires:
    chowId - to be used for database queries
    selected_id - to be used for database queries
    target - the AJAX page being called
    elementId - the id of the element into which the AJAX content will be written */
function processAddIngredient(chowId, selected_id, target, elementId) {
    // call the function that checks for XMLHttpRequest compatibility
    xhr=GetXmlHttpObject();
    // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
    if (xhr==null) {
        alert ("Your browser configuration does not support HTTP Requests (AJAX)");
        return;
    }
    // if xhr was anything but null, that means the browser is good to go and the function continues
    
    tempClose(elementId);
    
    // set the url variable to be the target, plus any _GET variables
    // a random number is appended to prevent caching complications
    var url="_ajax/"+target;
    url=url+"?chow_id="+chowId;
    url=url+"&selected_id="+selected_id;
    url=url+"&sid="+Math.random();

    // call the function that processes the XMLHttpRequest
    processXhr(elementId,url)
}

function updateChowAddNutr() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var newNutr = document.getElementById("add_nutrition_to_chow_selection").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        if (newNutr=='') {
            alert("Error!\n\nCheck the nutrition type and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processChowAddNutr(selected_id, newNutr, 'process_Chow_Add_Nutr.php', 'nutrition_data_area')
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }

    /*  the processChowAddNutr function requires:
        selected_id - to be used for database queries
        new_nutr_id - the new nutrition type id to be added to the chow
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processChowAddNutr(selected_id, new_nutr_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&newNutr="+new_nutr_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

/*  the deleteNutrFromChow function requires:
    evt - the object that called this function as an event handler
*/
function deleteNutrFromChow(evt) {
    // get the object that called this function as an event handler
    var activeForm = (evt) ? evt.target : window.event.srcElement;
    // get the id of that object
    var formId = activeForm.id;
    // strip out all characters from the id except the integers that appear at the end
    var formNum = formId.match(/\d+$/);
    
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var nutr_id = document.getElementById("nutr_id"+formNum).value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        if (nutr_id=='') {
            alert("Error!\n\nCheck the nutrition type and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processDeleteNutrFromChow(selected_id, nutr_id, 'process_Delete_Nutr_From_Chow.php', 'nutrition_data_area')
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }

    /*  the processDeleteNutrFromChow function requires:
        selected_id - to be used for database queries
        nutr_id - the nutrition id to be edited
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processDeleteNutrFromChow(selected_id, nutr_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&nutrId="+nutr_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

/*  the deleteIngredientFromRecipe function requires:
    evt - the object that called this function as an event handler
*/
function deleteIngredientFromRecipe(evt) {
    // get the object that called this function as an event handler
    var activeForm = (evt) ? evt.target : window.event.srcElement;
    // get the id of that object
    var formId = activeForm.id;
    // strip out all characters from the id except the integers that appear at the end
    var formNum = formId.match(/\d+$/);
    
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;
    var ingredient_id = document.getElementById("ingredient_id"+formNum).value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        if (ingredient_id=='') {
            alert("Error!\n\nCheck the ingredient and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processDeleteIngredientFromRecipe(selected_id, ingredient_id, 'process_Delete_Ingredient_From_Recipe.php', 'nutrition_data_area')
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }

    /*  the processDeleteIngredientFromRecipe function requires:
        selected_id - to be used for database queries
        ingredient_id - the ingredient id to be edited
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processDeleteIngredientFromRecipe(selected_id, ingredient_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&ingredientId="+ingredient_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function addChow() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var name = encodeURIComponent(document.getElementById("Name").value);
    var serving_id = document.getElementById("Serving_Size").value;
    
    if (name=='') {
        alert("Error!\n\nCheck the chow name and try again.");
        return false;
    } else {
        if (serving_id=='') {
            alert("Error!\n\nCheck the serving size and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processAddChow(name, serving_id, 'process_Add_Chow.php', 'add_chow_area');
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }
    
    /*  the processAddChow function requires:
        name - the name of the new chow
        serving_id - the serving id associated with the new chow
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processAddChow(name, serving_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?name="+name;
        url=url+"&serving_id="+serving_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function deleteChow() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("chow_id").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected chow and try again.");
        return false;
    } else {
        // call the function that runs the AJAX to update the database
        processDeleteChow(selected_id, 'process_delete_chow.php', 'chow_area')
        // By returning false, we prevent the form from actually getting submitted.
        return false;
    }

    /*  the processDeleteChow function requires:
        selected_id - to be used for database queries
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processDeleteChow(selected_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function updateServingInfo() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("serving_id").value;
    var newUnit = document.getElementById("unit").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected serving and try again.");
        return false;
    } else {
        if (newUnit=='') {
            alert("Error!\n\nCheck the serving and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processServingInfoUpdate(selected_id, newUnit, 'process_Serving_Info_Update.php', 'serving_info_area')
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }

    /*  the processServingInfoUpdate function requires:
        selected_id - to be used for database queries
        newUnit - the new name to be added to the database
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processServingInfoUpdate(selected_id, newUnit, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&newUnit="+newUnit;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function deleteServing() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("serving_id").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected serving and try again.");
        return false;
    } else {
        // call the function that runs the AJAX to update the database
        processDeleteServing(selected_id, 'process_delete_serving.php', 'serving_info_area')
        // By returning false, we prevent the form from actually getting submitted.
        return false;
    }

    /*  the processDeleteServing function requires:
        selected_id - to be used for database queries
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processDeleteServing(selected_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function addServing() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var name = document.getElementById("Name").value;
    
    if (name=='') {
        alert("Error!\n\nCheck the serving name and try again.");
        return false;
    } else {
        // call the function that runs the AJAX to update the database
        processAddServing(name, 'process_Add_Serving.php', 'add_serving_area');
        // By returning false, we prevent the form from actually getting submitted.
        return false;
    }
    
    /*  the processAddServing function requires:
        name - the name of the new serving
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processAddServing(name, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?name="+name;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function updateNutritionInfo() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("nutr_id").value;
    var newType = document.getElementById("type").value;
    var newUnits = document.getElementById("units").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected nutrition type and try again.");
        return false;
    } else {
        if (newType=='') {
            alert("Error!\n\nCheck the nutrition type name and try again.");
            return false;
        } else {
            // call the function that runs the AJAX to update the database
            processNutritionInfoUpdate(selected_id, newType, newUnits, 'process_Nutrition_Info_Update.php', 'nutrition_info_area')
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }

    /*  the processNutritionInfoUpdate function requires:
        selected_id - to be used for database queries
        newType - the new nutrition type name to be added to the database
        newUnits - the units in which the nutrition type is measured
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processNutritionInfoUpdate(selected_id, newType, newUnits, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&newType="+newType;
        url=url+"&newUnits="+newUnits;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function deleteNutrition() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var selected_id = document.getElementById("nutr_id").value;

    if (selected_id=='') {
        alert("Error!\n\nCheck the selected nutrition type and try again.");
        return false;
    } else {
        // call the function that runs the AJAX to update the database
        processDeleteNutrition(selected_id, 'process_delete_nutrition.php', 'nutrition_info_area')
        // By returning false, we prevent the form from actually getting submitted.
        return false;
    }

    /*  the processDeleteServing function requires:
        selected_id - to be used for database queries
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processDeleteNutrition(selected_id, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?selected_id="+selected_id;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

function addNutrition() {
    // getElementById("argument") selects the element with an ID equal to the argument
    // .value stores the value of that ID
    // so the variable will be equal to whatever value the ID has
    var type = document.getElementById("type").value;
    var units = document.getElementById("units").value;
    
    if (type=='') {
        alert("Error!\n\nCheck the nutrition type and try again.");
        return false;
    } else {
        if (units=='') {
        alert("Error!\n\nCheck the units and try again.");
        return false;
        } else {
            // call the function that runs the AJAX to update the database
            processAddNutrition(type, units, 'process_Add_Nutr.php', 'add_nutrition_area');
            // By returning false, we prevent the form from actually getting submitted.
            return false;
        }
    }
    
    /*  the processAddNutrition function requires:
        type - the new nutrition type
        units - the units in which the nutrition is measured
        target - the AJAX page being called
        elementId - the id of the element into which the AJAX content will be written */
    function processAddNutrition(type, units, target, elementId) {
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
        // if xhr was anything but null, that means the browser is good to go and the function continues
        
        tempClose(elementId);
        
        // set the url variable to be the target, plus any _GET variables
        // a random number is appended to prevent caching complications
        var url="_ajax/"+target;
        url=url+"?type="+type;
        url=url+"&units="+units;
        url=url+"&sid="+Math.random();
        
        // call the function that processes the XMLHttpRequest
        processXhr(elementId,url)
    }
}

// check to see if the browser supports XMLHttpRequests, and if so then create a new one
function GetXmlHttpObject() {
    var xhr=null;
    // Try to make a new XMLHttpRequest
    try {
        // This should work for Firefox, Opera 8.0+, Safari, Chrome
        xhr=new XMLHttpRequest();
    }
    // if that fails, catch the error and try something else
    catch (e) {
        // Either this should work for Internet Explorer...
        try {
            xhr=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            // or this should
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    // at this point, xhr is either still equal to null, or it is now a shiny new XMLHttpRequest object!
    return xhr;
}

/*  the processXhr function requires:
    url - the AJAX page being called
    elementId - the id of the element into which the AJAX content will be written */
function processXhr(elementId,url) {
    // creat an XMLHttpRequest object
    xhr=GetXmlHttpObject();
    // any time the state of the XMLHttpRequest changes, it calls the specified function
    xhr.onreadystatechange=stateChanged;
    // open the request
    xhr.open("GET",url,true);
    // send the request to the server
    xhr.send(null);

    function stateChanged() {
            // check that the readyState is 0, 1, 2, or 3
            if (xhr.readyState==0 || xhr.readyState==1 || xhr.readyState==2 || xhr.readyState==3) {
            // create a variable for the loading element
            var divLoading = document.getElementById(elementId+'_loading');
                divLoading.innerHTML="<img src='_images/ajax-loader.gif' alt='loading' />";
            }
        // check that the readyState is 4 OR complete, both of which mean the XMLHttpRequest is complete
        if (xhr.readyState==4 || xhr.readyState=="complete") {
            // check that the status is 200, which means the page is valid. This is in distinction to a 404 error or the like
            if (xhr.status==200) {
                // create a variable for the selected elementId
                var div = document.getElementById(elementId);
                // set the elementID's innerHTML to the XMLHttpRequest response
                div.innerHTML=xhr.responseText;
                
                // select all script tags that came through in the XMLHttpRequest response
                var allScripts=div.getElementsByTagName("script");
                // loop through all the scripts and run (evaluate) them
                for(var i=0;i<allScripts.length;i++) {  
                    eval(allScripts[i].text);  
                }
            }
            else {
                // if the status wasn't 200, throw up an error message
                alert("There was a problem with the request " + xhr.status);
            }
        }
    }
}

// initialize various functions for the given filename
function initAll(filename) {
    if (filename == 'get_select_chow.php') {
        searchArray = new Array();
        document.getElementById("searchField").value = "type in the chow name...";
        $(document.getElementById("searchField")).focus(function () {
            document.getElementById("searchField").value = "";
        });
        $(document.getElementById("searchField")).blur(function () {
            document.getElementById("searchField").value = "type in the chow name...";
        });
        document.getElementById("searchField").onkeyup = searchSuggest;
        
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
       
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = setSearchArray;
        // open the request
        xhr.open("GET", "_ajax/chow_search_xml.php", true);
        // send the request to the server
        xhr.send(null);
    }
    if (filename == 'get_edit_chow.php') {
        if(no_ingredients.checked==1) {
            getContent(no_ingredients.value, 'get_add_nutrition_data.php', 'add_nutrition_data_area');
        }
        if(yes_ingredients.checked==1) {
            getContent(yes_ingredients.value, 'get_add_ingredients.php', 'add_nutrition_data_area');
        }
        // get the element with the specified ID
        // the .onsubmit says to run a function when that form is submitted
        document.getElementById("chow_info_form").onsubmit = updateChowInfo;
        document.getElementById("delete_chow").onclick = deleteChow;
        document.getElementById("note_form").onsubmit = updateChowNote;
    }
    if (filename == 'get_add_ingredients.php') {
        searchArray = new Array();
        document.getElementById("searchField").value = "type in the chow name...";
        $(document.getElementById("searchField")).focus(function () {
            document.getElementById("searchField").value = "";
        });
        $(document.getElementById("searchField")).blur(function () {
            document.getElementById("searchField").value = "type in the chow name...";
        });
        document.getElementById("searchField").onkeyup = searchSuggest;
        
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
       
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = setSearchArray;
        // open the request
        xhr.open("GET", "_ajax/chow_search_xml.php", true);
        // send the request to the server
        xhr.send(null);
        
        // create an array of all the elements with the specified name in the specified div
        var allIngredientQuantityUpdateButtons = getElementsByClass("update_ingredient_quantity");
        // loop through each named object in the array and add an onclick event handler
        for(var i=0;i<allIngredientQuantityUpdateButtons.length;i++) {
            var ingredientQuantityUpdateButton = "edit_ingredient_quantity"+i;
            document.getElementById(ingredientQuantityUpdateButton).onclick = updateIngredientQuantity; 
        }
        
        // create an array of all the elements with the specified name in the specified div
        var allIngredientDeleteButtons = getElementsByClass("delete_recipe_ingredient");
        // loop through each named object in the array and add an onclick event handler
        for(var i=0;i<allIngredientDeleteButtons.length;i++) {
            var ingredientDeleteButton = "delete_recipe_ingredient"+i;
            document.getElementById(ingredientDeleteButton).onclick = deleteIngredientFromRecipe; 
        }
    }
    if (filename == 'get_add_nutrition_data.php') {
        document.getElementById("add_nutrition_to_chow_selection").onchange = updateChowAddNutr;
        
        var div = document.getElementById("nutrition_data_area");
        // create an array of all the forms in the specified div
        var allNutrForms = div.getElementsByTagName("form");
            // loop through each form in the array and add an onsubmit event handler
            for(var i=0;i<allNutrForms.length;i++) {
                var nutritionForm = "nutrition_data_form"+i;
                document.getElementById(nutritionForm).onsubmit = updateNutritionDataForChow; 
            }
        // create an array of all the elements with the specified name in the specified div
        var allNutrDeleteButtons = getElementsByClass("delete_chow_nutr_type");
        // loop through each named object in the array and add an onclick event handler
        for(var i=0;i<allNutrDeleteButtons.length;i++) {
            var nutritionDeleteButton = "delete_chow_nutr_type"+i;
            document.getElementById(nutritionDeleteButton).onclick = deleteNutrFromChow; 
        }
    }
    if (filename == 'get_add_chow.php') {
        document.getElementById("add_chow_form").onsubmit = addChow;
    }
    if (filename == 'get_select_serving.php') {
        searchArray = new Array();
        document.getElementById("searchField").value = "type in the serving name...";
        $(document.getElementById("searchField")).focus(function () {
            document.getElementById("searchField").value = "";
        });
        $(document.getElementById("searchField")).blur(function () {
            document.getElementById("searchField").value = "type in the serving name...";
        });
        document.getElementById("searchField").onkeyup = searchSuggest;
        
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
       
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = setSearchArray;
        // open the request
        xhr.open("GET", "_ajax/serving_search_xml.php", true);
        // send the request to the server
        xhr.send(null);
    }    
    if (filename == 'get_add_serving.php') {
        document.getElementById("add_serving_form").onsubmit = addServing;
    }
    if (filename == 'get_add_nutrition.php') {
        document.getElementById("add_nutrition_form").onsubmit = addNutrition;
    } 
    if (filename == 'get_edit_serving.php') {
        document.getElementById("serving_info_form").onsubmit = updateServingInfo;
        document.getElementById("delete_serving").onclick = deleteServing;
    }
    if (filename == 'get_select_nutrition.php') {
        searchArray = new Array();
        document.getElementById("searchField").value = "type in the nutrition name...";
        $(document.getElementById("searchField")).focus(function () {
            document.getElementById("searchField").value = "";
        });
        $(document.getElementById("searchField")).blur(function () {
            document.getElementById("searchField").value = "type in the nutrition name...";
        });
        document.getElementById("searchField").onkeyup = searchSuggest;
        
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
       
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = setSearchArray;
        // open the request
        xhr.open("GET", "_ajax/nutrition_search_xml.php", true);
        // send the request to the server
        xhr.send(null);
    }
    if (filename == 'get_edit_nutrition.php') {
        document.getElementById("nutrition_info_form").onsubmit = updateNutritionInfo;
        document.getElementById("delete_nutr").onclick = deleteNutrition;
    }
    if (filename == 'edibull_entry.php') {
        searchArray = new Array();
        document.getElementById("searchField").value = "type in the chow name...";
        $(document.getElementById("searchField")).focus(function () {
            document.getElementById("searchField").value = "";
        });
        document.getElementById("searchField").onkeyup = searchSuggest;
        
        // call the function that checks for XMLHttpRequest compatibility
        xhr=GetXmlHttpObject();
        // if xhr ends up being null, that means the browser can't handle XMLHttpRequests
        if (xhr==null) {
            alert ("Your browser configuration does not support HTTP Requests (AJAX)");
            return;
        }
       
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = setSearchArray;
        // open the request
        xhr.open("GET", "_ajax/chow_search_xml.php", true);
        // send the request to the server
        xhr.send(null);
    }    
}

function searchSuggest(evt) {
    var evt = evt || event;
    var keys = {
        13: 'Enter', 16:'Shift', 17:'Ctrl', 18:'Alt', 19:'Pause', 
        37:'Arrow Left', 38:'Arrow Up', 39:'Arrow Right', 
        40:'Arrow Down' 
    }
    if (keys[evt.keyCode] == 'Arrow Down') {
        removeFocus();
        selectionCount++;
        var selectionList = document.getElementById("searchFieldPopups").childNodes.length;
        if (selectionCount > selectionList-1) {
            selectionCount = 0;
        }
        arrowSelection = document.getElementById("searchFieldPopups").childNodes[selectionCount];
        $(arrowSelection).addClass("onFocus");
    } else {
        if (keys[evt.keyCode] == 'Arrow Up') {
            removeFocus();
            selectionCount--;
            var selectionList = document.getElementById("searchFieldPopups").childNodes.length;
            if (selectionCount < 0) {
                selectionCount = selectionList-1;
            }
            var arrowSelection = document.getElementById("searchFieldPopups").childNodes[selectionCount];
            $(arrowSelection).addClass("onFocus");
        } else {
            if (keys[evt.keyCode] == 'Enter') {
                var arrowSelection = document.getElementById("searchFieldPopups").childNodes[selectionCount];
                makeChoice(arrowSelection);
            } else {    
                selectionCount = -1;
                var str = document.getElementById("searchField").value;
                document.getElementById("searchField").className = "";
                if (str != "") {
                    document.getElementById("searchFieldPopups").innerHTML = "";
                    
                    for (var i=0; i<searchArray.length; i++) {
                        var thisItem = searchArray[i].itemName;
                        var thisItemId = searchArray[i].itemId;
                        
                        // compare the thisItem variable (set to lowercase) to the str variable (also lowercase)
                        // indexOf does the compare
                        // by saying "if >= 0" we're saying "if 1 or more matching character is found"
                        if (thisItem.toLowerCase().indexOf(str.toLowerCase()) >= 0) {
                            var tempDiv = document.createElement("div");
                            tempDiv.id = thisItemId;
                            tempDiv.className = "suggestions";
                            tempDiv.innerHTML = thisItem;
                            tempDiv.onclick = makeChoice;
                            document.getElementById("searchFieldPopups").appendChild(tempDiv);
                        }
                    }
                    var foundCt = document.getElementById("searchFieldPopups").childNodes.length;
                    if (foundCt == 0) {
                        document.getElementById("searchField").className = "error";
                    }
                } else {
                    document.getElementById("searchFieldPopups").innerHTML = "";
                }
            }
        }
    }
}

function removeFocus() {
    var allDivs = document.getElementsByTagName("div");
    $(allDivs).removeClass("onFocus");
}

function makeChoice(evt) {
    var thisDiv = (evt.id) ? evt : (evt) ? evt.target : window.event.srcElement;
    document.getElementById("searchField").value = thisDiv.innerHTML;
    document.getElementById("searchFieldPopups").innerHTML = "";
    var url = document.getElementById("url").value;
    var elementId = document.getElementById("elementId").value;
    var idValue = thisDiv.id;
    //determine what page sent the original search request. some pages need additional variables
    var sourcePage = document.getElementById("page_name").value;
    if (sourcePage == 'get_select_chow.php') {
        getContent(idValue, url, elementId);
    }
    if (sourcePage == 'get_edit_chow.php') {
        var chowId = document.getElementById("chow_id").value;
        processAddIngredient(chowId, idValue, url, elementId);
    }
    if (sourcePage == 'edibull_entry.php') {
        getContent(idValue, url, elementId);
    }
}

function setSearchArray() {
    // check that the readyState is 4
    if (xhr.readyState==4 || xhr.readyState=="complete") {
        // check that the status is 200
        if (xhr.status == 200) {
            // check that the XMLHttpResponse is an XML
            if (xhr.responseXML) {
                // set a variable equal to an array of all the specified tags
                var allItems = xhr.responseXML.getElementsByTagName("searchObject");
                // create a function to get values from the xml, to be used below
                function getVal(theData,theTag) {
                    return theData.getElementsByTagName(theTag)[0].firstChild.nodeValue
                }
                for (var i=0; i<allItems.length; i++) {
                    // fill the searchArray array that was initalized at the beginning of this file
                    var tempObj = new Object;
                    tempObj.itemName = getVal(allItems[i],"name");
                    tempObj.itemId = getVal(allItems[i],"item_id");
                    searchArray[i] = tempObj;
                }
            } else {
            // deliver an error message in case something didn't work right
            alert("There was a problem with the request, the responseXML is odd.");
            }
        }
        else {
            // deliver a message in case something didn't work right
            alert("There was a problem with the request " + xhr.status);
        }
    }
}

// because this function is being called by an event,
// it is getting passed an argument, which we'll call evt
function showPreview(evt) {
    // if the argument passed in by the event is not void,
    // then make a variable with the value of the target of the event.
    // targets are the targeted link in a URL
    if (evt) {
        var url = evt.target;
    }
    // sometimes the browser or user settings prevent the function
    // from being passed an event.
    else {
        // if that happens, these two steps achieve the same results as above
        evt = window.event;
        var url = evt.srcElement;
    }
    // set the x and y coordinates to the same location as the client's mouse pointer
    xPos = evt.clientX;
    yPos = evt.clientY;
    
    // Ask if the browser knows what XMLHttpRequests are,
    // and if it does, make a new one, called xhr for simplicity
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        // This asks if the browser knows what ActiveXObjects are,
        // which is basically asking if the browser is IE6. IE7 gets the XMLHttpRequest
        // above, so only IE6 will be triggering this statement.
        if (window.ActiveXObject) {
            // try is a simple way of saying 'try this' and if it fails, it will look
            // for something called 'catch'.
            try {
                // this has the browser make an ActiveXObject that does what XMLHttpRequests do,
                // and we call it xhr for simplicity
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            // this is invoked if the browser understands ActiveXObject, but for some reason
            // the user has disabled it.
            catch(e) { }
        }
    }
    
    // this checks to see if xhr is not false
    if (xhr) {
        // any time the state changes, it calls the specified function
        xhr.onreadystatechange = showContents;
        // open the request
        xhr.open("GET", url, true);
        // send the request to the server
        xhr.send(null);
    }
    else {
        // if the xhr was false, it does a simple javascript innerHTML call that reports an error.
        document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
    }
}

function showContents() {
    // check that the readyState is 4
    if (xhr.readyState == 4) {
        // check that the status is 200
        if (xhr.status == 200) {
            // set a variable to the XMLHttpRequest response 
            var outMsg = xhr.responseText;
        }
        else {
            // set the outMsg variable to an error message with the XMLHttpRequest status, which is probably an error
            var outMsg = "There was a problem with the request " + xhr.status;
        }
        // set a variable to equal the specified ID
        var prevWin = document.getElementById("previewWin");
        // set the specified ID's contents to the outMsg
        prevWin.innerHTML = outMsg;
        // change the style for the ID to be about equal to the yPos and xPos from above
        // this makes the ID appear slightly off from the mouse pointer
        prevWin.style.top = parseInt(yPos)+2 + "px";
        prevWin.style.left = parseInt(xPos)+2 + "px";
        prevWin.style.visibility = "visible";
        
        // and an event handler to the ID, specifically when the mouse is moved off of the ID element
        prevWin.onmouseout = function() {
            // hide the ID element
            document.getElementById("previewWin").style.visibility = "hidden";
        }
    }
}

function tempAlert() {
    alert('huh...');
}

// close the specified ID by replacing all of its contents with a loading div
// the loading div is used to hold the animated loading gif in AJAX calls
function tempClose(elementId) {
    $('#' + elementId).html('<div id=\"' + elementId + '_loading\"></div>'); 
}

/* getElementsByClass
  Simply add a class name to the beginning of the funciton and the 2nd and 3rd arguments are optional and the magic is done for you!
  code from: http://www.dustindiaz.com/top-ten-javascript/
/**********************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function obfuscatePass() {
	var el = document.getElementById('password');
	if ( el.type != 'text' ) {
		el.type = 'text';
	}
	else {
		el.type = 'password';
	}
}