var regExp_name = /^[A-Za-z][A-Za-z\s'-]{2,40}$/;
var regExp_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var regExp_comments = /^[\w\W\s]{5,10000}$/;
var regExp_year = /^[0-9]{4}$/;
var regExp_phone_numbers = /^[0-9\s+()]+$/;
var regExp_address = /^[\w\W\s]{1,40}$/;
var regExp_address_postcode = /^([A-PR-UWYZ0-9][A-HK-Y0-9][ABCDEFGHJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)$/;

//Not Used
//var regExp_site_info = /^[A-Za-z\s-]+$/;
//var regExp_school_name = /^[A-Za-z\s]+$/;
//var regExp_start_finish_year = /^[0-9]{4}$/;


function readValidationArray(validationArray, validationField)
{
    for (i=0; i<validationArray.length; i++)
    {
        if (validationField == validationArray[i].split("=")[0])
        {
            if ((validationArray[i].split("=")[1])== 'true')
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

// ----------------------------------------------------------------------

// Form setup

function formSetup(validationArray)
{
    changeForm("fb_Submit", "", "blank");
    
    if(readValidationArray(validationArray, "name"))
    {
        changeRow("row_First_Name", "color_red");
        changeRow("row_Last_Name", "color_red");
        changeForm("fb_First_Name", "", "blank");
        changeForm("fb_Last_Name", "", "blank");

    }
    else
    {
        changeRow("row_First_Name", "color_blue");
        changeRow("row_Last_Name", "color_blue");
        changeForm("fb_First_Name", "", "blank");
        changeForm("fb_Last_Name", "", "blank");

    }

    if(readValidationArray(validationArray, "email"))
    {
        changeRow("row_Email_Address", "color_red");
        changeRow("row_Email_Address_Con", "color_red");
        changeForm("fb_Email_Address", "", "blank");
    }
    else
    {
        changeRow("row_Email_Address", "color_blue");
        changeRow("row_Email_Address_Con", "color_blue");
        changeForm("fb_Email_Address", "", "blank");
    }

    if(readValidationArray(validationArray, "studentTeacher"))
    {
        changeRow("row_Student_Teacher", "color_red");
        changeForm("fb_Student_Teacher", "", "blank");
    }
    else
    {
        changeRow("row_Student_Teacher", "color_blue");
        changeForm("fb_Student_Teacher", "", "blank");
    }
	
    if(readValidationArray(validationArray, "schoolname"))
    {
        changeRow("row_School_Name", "color_red");
        changeForm("fb_School_Name", "", "blank");
    }
    else
    {
        changeRow("row_School_Name", "color_blue");
        changeForm("fb_School_Name", "", "blank");
    }


    if(readValidationArray(validationArray, "yearStartFinish"))
    {
        changeRow("row_Start_Year", "color_red");
        changeRow("row_Finish_Year", "color_red");
        changeForm("fb_Year", "", "blank");
    }
    else
    {
        changeRow("row_Start_Year", "color_blue");
        changeRow("row_Finish_Year", "color_blue");
        changeForm("fb_Year", "", "blank");
    }

    if(readValidationArray(validationArray, "text"))
    {
        changeRow("row_Comments", "color_red");
        changeForm("fb_Comments", "", "blank");
    }
    else
    {
        changeRow("row_Comments", "color_blue");
        changeForm("fb_Comments", "", "blank");
    }
	
    if(readValidationArray(validationArray, "siteInfo"))
    {
        changeRow("row_Site_Info", "color_red");
        changeForm("fb_Site_Info", "", "blank");
    }
    else
    {
        changeRow("row_Site_Info", "color_blue");
        changeForm("fb_Site_Info", "", "blank");
    }

    if(readValidationArray(validationArray, "houseAddress"))
    {
        changeRow("row_House_No_Name", "color_red");
        changeRow("row_House_Street", "color_red");
        changeRow("row_House_Area", "color_red");
        changeRow("row_House_City", "color_red");
        changeRow("row_House_County", "color_red");
        changeRow("row_House_Post_Code", "color_red");
        changeRow("row_House_Country", "color_red");
        changeForm("fb_House", "", "blank");
    }
    else
    {
        changeRow("row_House_No_Name", "color_blue");
        changeRow("row_House_Street", "color_blue");
        changeRow("row_House_Area", "color_blue");
        changeRow("row_House_City", "color_blue");
        changeRow("row_House_County", "color_blue");
        changeRow("row_House_Post_Code", "color_blue");
        changeRow("row_House_Country", "color_blue");
        changeForm("fb_House", "", "blank");
    }


    if(readValidationArray(validationArray, "phoneHome"))
    {
        changeRow("row_Phone_Home", "color_red");
        changeForm("fb_Phone_Home", "", "blank");
    }
    else
    {
        changeRow("row_Phone_Home", "color_blue");
        changeForm("fb_Phone_Home", "", "blank");
    }


    if(readValidationArray(validationArray, "phoneWork"))
    {
        changeRow("row_Phone_Work", "color_red");
        changeForm("fb_Phone_Work", "", "blank");
    }
    else
    {
        changeRow("row_Phone_Work", "color_blue");
        changeForm("fb_Phone_Work", "", "blank");
    }


    if(readValidationArray(validationArray, "phoneMobile"))
    {
        changeRow("row_Phone_Mobile", "color_red");
        changeForm("fb_Phone_Mobile", "", "blank");
    }
    else
    {
        changeRow("row_Phone_Mobile", "color_blue");
        changeForm("fb_Phone_Mobile", "", "blank");
    }

}

// ----------------------------------------------------------------------

function changeForm(destination_id, message ,class_error_info) 
{
    var elem = document.getElementById(destination_id);
    elem.firstChild.nodeValue = message;
    elem.className = class_error_info;
}

function changeRow(destination_id, class_row_color) 
{
    var elem = document.getElementById(destination_id);
    elem.className = class_row_color;
}

// ----------------------------------------------------------------------


function validate_First_Name(validationArray,First_Name,destination_id_First_Name)
{
    // Enables the Name validation
    if(readValidationArray(validationArray, "name"))
    {
        // check to see if the name is valid
        if (!regExp_name.test(First_Name.value))
        {
            changeForm(destination_id_First_Name, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Your 'Christian' must be between 2 and 40 letters long", "message");
            return false;
        }
        changeForm(destination_id_First_Name, "", "error");
        return true;
    }
    return true;
}

function validate_Last_Name(validationArray,Last_Name,destination_id_Last_Name)
{
    // Enables the Name validation
    if(readValidationArray(validationArray, "name"))
    {
        // check to see if the name is valid
        if (!regExp_name.test(Last_Name.value))
        {
            changeForm(destination_id_Last_Name, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Your 'Surname' must be between 2 and 35 letters long", "message");
            return false;
        }
        changeForm(destination_id_Last_Name, "", "error");
        return true;
    }
    return true;
}

function validate_Email(validationArray, Email_Address, Email_Address_Con, destination_id_Email_Address)
{
    // Enables the Email validation
    if(readValidationArray(validationArray, "email"))
    {
        // check to see if the email field is valid
        if (!regExp_email.test(Email_Address.value))
        {
            changeForm(destination_id_Email_Address, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Your 'Email Address' must contain the following characters:- @ .", "message");
            return false;
        }
        // only gets here if the email field contains valid data
        else
        {
            if (Email_Address.value !== Email_Address_Con.value)
            {
			
                changeForm(destination_id_Email_Address, "** ERROR **", "error");
                changeForm('fb_Error_Display', "Your 'Email Addresses' do mot match", "message");
                return false;
            }
        }
        changeForm(destination_id_Email_Address, "", "error");
        return true;
    }
    return true;
}

function validate_Start_Finish_Year(validationArray,Start_Year,Finish_Year,destination_id_Year)
{
    var message_1 = "The 'Start Year' you have selected " + Start_Year.value + ", must be before your 'Finish Year' " + Finish_Year.value;
    var message_2 = "How long were you at school? It should not have been more than 6 years! You have entered "+ (Finish_Year.value-Start_Year.value) + " years";

    // Enables the Start aand Finish year validation
    if(readValidationArray(validationArray, "yearStartFinish"))
    {
        // Check that a start year has been picked
        if (!regExp_year.test(Start_Year.value))
        {
            changeForm(destination_id_Year, "** ERROR **", "error");
            changeForm('fb_Error_Display', "You have not selected a 'Start Year'", "message");
            return false;
        }
        else
        {
            // Check that a finish year has been picked
            if (!regExp_year.test(Finish_Year.value))
            {
                changeForm(destination_id_Year, "** ERROR **", "error");
                changeForm('fb_Error_Display', "You have not selected a 'Finish Year'", "message");
                return false;
            }
            else
            {
                if (Start_Year.value > Finish_Year.value)
                {
                    changeForm(destination_id_Year, "** ERROR **", "error");
                    changeForm('fb_Error_Display',message_1, "message");
                    return false;
                }
                else
                {
                    if ((Finish_Year.value-Start_Year.value) > 6 )
                    {
                        changeForm(destination_id_Year, "** ERROR **", "error");
                        changeForm('fb_Error_Display',message_2, "message");
                        return false;
                    }
                }
            }
        }
        changeForm(destination_id_Year, "", "error");
        return true;
    }
    return true;
}

function validate_Comments(validationArray,Comments,destination_id_Comments)
{
    // Enables the Comments validation
    if(readValidationArray(validationArray, "text"))
    {
        if (!regExp_text.test(Comments.value))
        {
            changeForm(destination_id_Comments, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Please enter some details in the 'Comment Field'. The Comments must be between 5 and 10000 characters long.", "message");
            return false;
        }
        changeForm(destination_id_Comments, "", "error")
        return true;
    }
    return true;
}

function validate_Student_Teacher(validationArray,Student_Teacher,destination_id_Student_Teacher)
{
    // Enables the Start aand Finish year validation

    if(readValidationArray(validationArray, "studentTeacher"))
    {
        // Check that a school name has been picked
        if (Student_Teacher.selectedIndex == 0)
        {
            changeForm(destination_id_Student_Teacher, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Were you a 'Student' or a 'Teacher'", "message");
            return false;
        }
        changeForm(destination_id_Student_Teacher, "", "error");
        return true;
    }
    return true;
}

function validate_School_Name(validationArray,School_Name,destination_id_School_Name)
{
	
    // Enables the Start aand Finish year validation
    if(readValidationArray(validationArray, "schoolname"))
    {
        // Check that a school name has been picked
        if (School_Name.selectedIndex == 0)
        {
            changeForm(destination_id_School_Name, "** ERROR **", "error");
            changeForm('fb_Error_Display', "You have not selected a 'School'", "message");
            return false;
        }
        changeForm(destination_id_School_Name, "", "error");
        return true;
    }
    return true;
}

function validate_Comments(validationArray,Comments,destination_id_Comments)
{
    // Enables the Comments validation
    if(readValidationArray(validationArray, "text"))
    {
        if (!regExp_comments.test(Comments.value))
        {
            changeForm(destination_id_Comments, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Please enter some details in the 'Comment Field'. The Comments must be between 5 and 5000 characters long.", "message");
            return false;
        }
        changeForm(destination_id_Comments, "", "error")
        return true;
    }
    return true;
}


function validate_Site_Info(validationArray,Site_Info,destination_id_Site_Info)
{
    // Enables the Site Info validation
    if(readValidationArray(validationArray, "siteInfo"))
    {
        if (Site_Info.selectedIndex == 0)
        {
            changeForm(destination_id_Site_Info, "** ERROR **", "error");
            changeForm('fb_Error_Display', "How did you find the site. Please select one of the options", "message");
            return false;
        }
        changeForm(destination_id_Site_Info, "", "error")
        return true;
    }
    return true;
}

function validate_House(validationArray,House_Country,House_Post_Code,House_County,House_City,House_Area,House_Street,House_No_Name,destination_id_House)
{
    // Enables mobile phone validation
    if(readValidationArray(validationArray, "houseAddress"))
    {
        // check to see if the number is valid
        if (!regExp_address.test(House_No_Name.value))
        {
            changeForm(destination_id_House, "** ERROR **", "error");
            changeForm('fb_Error_Display', "You must enter a valid 'House Name or Number'", "message");
            return false;
        }
        else
        {
            if (!regExp_address.test(House_Street.value))
            {
                changeForm(destination_id_House, "** ERROR **", "error");
                changeForm('fb_Error_Display', "You must enter a valid 'Street Name'", "message");
                return false;
            }
            else
            {
                if (!regExp_address.test(House_Area.value))
                {
                    changeForm(destination_id_House, "** ERROR **", "error");
                    changeForm('fb_Error_Display', "You must enter a valid 'Area Name'", "message");
                    return false;
                }
                else
                {
                    if (!regExp_address.test(House_City.value))
                    {
                        changeForm(destination_id_House, "** ERROR **", "error");
                        changeForm('fb_Error_Display', "You must enter a valid 'City Name'", "message");
                        return false;
                    }
                    else
                    {
                        if (!regExp_address.test(House_County.value))
                        {
                            changeForm(destination_id_House, "** ERROR **", "error");
                            changeForm('fb_Error_Display', "You must enter a valid 'County Name'", "message");
                            return false;
                        }
                        else
                        {
                            if (!regExp_address_postcode.test(House_Post_Code.value))
                            {
                                changeForm(destination_id_House, "** ERROR **", "error");
                                changeForm('fb_Error_Display', "You must enter a valid 'Post Code'", "message");
                                return false;
                            }
                            else
                            {
                                if (!regExp_address.test(House_Country.value))
                                {
                                    changeForm(destination_id_House, "** ERROR **", "error");
                                    changeForm('fb_Error_Display', "You must enter a valid 'Country Name'", "message");
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
        }
        changeForm(destination_id_House, "", "error")
        return true;
    }
    return true;
}

function validate_Phone_Home(validationArray,Phone_Home,destination_id_Phone_Home)
{
    // Enables mobile phone validation
    if(readValidationArray(validationArray, "phoneHome"))
    {
        // check to see if the number is valid
        if (!regExp_phone_numbers.test(Phone_Home.value))
        {
            changeForm(destination_id_Phone_Home, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Invalid 'HOME Phone Number'. Your phone number should be 6 to 16 digits long", "message");
            return false;
        }
        changeForm(destination_id_Phone_Home, "", "error")
        return true;
    }
    return true;
}

function validate_Phone_Work(validationArray,Phone_Work,destination_id_Phone_Work)
{
    // Enables mobile phone validation
    if(readValidationArray(validationArray, "phoneWork"))
    {
        // check to see if the number is valid
        if (!regExp_numericregExp_phone_numbers.test(Phone_Work.value))
        {
            changeForm(destination_id_Phone_Work, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Invalid 'WORK Phone Number'. Your phone number should be 6 to 16 digits long", "message");
            return false;
        }
        changeForm(destination_id_Phone_Work, "", "error")
        return true;
    }
    return true;
}

function validate_Phone_Mobile(validationArray,Phone_Mobile,destination_id_Phone_Mobile)
{
    // Enables mobile phone validation
    if(readValidationArray(validationArray, "phoneMobile"))
    {
        // check to see if the number is valid
        if (!regExp_phone_numbers.test(Phone_Mobile.value))
        {
            changeForm(destination_id_Phone_Mobile, "** ERROR **", "error");
            changeForm('fb_Error_Display', "Invalid 'Mobile Phone Number'. Your phone number should be 6 to 16 digits long", "message");
            return false;
        }
        changeForm(destination_id_Phone_Mobile, "", "error")
        return true;
    }
    return true;
}

