﻿var strEntrySeparator = '~_~';
var strFieldSeparator = '¬';

function fnSetForm(strFormName){
    var intErrors = 0;
    if (!fnValidateFormField('ctl00_cntMainPlaceholder_careerdescription', 'val_careerdescription', 'Please enter career description.')){intErrors++}
    if (!fnValidateFormField('startdate', 'val_startdate', 'Please enter your earliest start date.')){intErrors++}
    
   
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        document.getElementById('hdnFormName').value = strFormName;
        document.forms[0].submit();
    }

}


function fnSetAppFormStage(intDestinationStage, intCurrentStage, blnValidate)
{
    if (blnValidate)
    {
        if (fnValidateAppFormStage(intCurrentStage))
        {
            for (i=1;i<=6;i++)
            {
                document.getElementById('AppFormStep' + i).style.display = 'none';
            }
            document.getElementById('AppFormStep' + intDestinationStage).style.display = 'block';
        }
        else
        {
            alert('Please ensure all required fields are completed.');
        }
    }
    else
    {
        for (i=1;i<=6;i++)
        {
            document.getElementById('AppFormStep' + i).style.display = 'none';
        }
        document.getElementById('AppFormStep' + intDestinationStage).style.display = 'block';
    }
    
}

function fnValidateAppFormStage(intStage)
{
    var intErrors = 0;

    switch(intStage)
    {
        case 1:
            if (!fnValidateFormField('surname', 'val_surname', 'Please enter your surname.')){intErrors++}
            if (!fnValidateFormField('othernames', 'val_othernames', 'Please enter other names.')){intErrors++}
            if (!fnValidateFormField('ctl00_cntMainPlaceholder_address', 'val_address', 'Please enter your address.')){intErrors++}
            if (!fnValidateFormField('postcode', 'val_postcode', 'Please enter your postcode.')){intErrors++}
            if (!fnValidateFormField('email', 'val_email', 'Please enter your email address.')){intErrors++}
            if (!fnValidateFormField('telephone', 'val_telephone', 'Please enter your telephone number.')){intErrors++}
            if (!fnValidateFormField('mobile', 'val_mobile', 'Please enter your mobile phone number.')){intErrors++}
            if (!fnValidateSelectFormField('ukeligible', 'val_ukeligible', 'Please select..', 'Please select whether you are eligible to work in the UK.')){intErrors++}
            
            var objUKEligible = document.getElementById('ukeligible');
            var objUKEligibleOption = objUKEligible.options[objUKEligible.selectedIndex];
            if (objUKEligibleOption.text == 'No')
            {
                if (!fnValidateFormField('ctl00_cntMainPlaceholder_notukeligible', 'val_notukeligible', 'Please enter any restrictions that may apply.')){intErrors++}
            }
            else
            {
                document.getElementById('val_notukeligible').style.display = 'none';
            }
            
            if (intErrors>0){return false;}else{return true;}
            break;
        case 6:
            if (!fnValidateFormField('ctl00_cntMainPlaceholder_careerdescription', 'val_careerdescription', 'Please enter career description.')){intErrors++}
            if (!fnValidateFormField('startdate', 'val_startdate', 'Please enter your earliest start date.')){intErrors++}
            
           
            if (intErrors>0){return false;}else{return true;}
            break;
        default:
            return true;
       
    }
}

//***********************************************************************************************
//                                  START GENERIC FUNCTIONS
//***********************************************************************************************

function fnValidateFormField(strFormElementID, strValidationElementID, strValidationText) 
{
    var objFormElement = document.getElementById(strFormElementID);
    var objValidationElement = document.getElementById(strValidationElementID);
    var blnIsValid = false;
    
    if (objFormElement.value.length == 0)
    {
        objValidationElement.innerHTML = strValidationText;
        objValidationElement.style.display = 'block';
    }
    else
    {
        objValidationElement.style.display = 'none';
        blnIsValid = true;
    }
    
    return blnIsValid;
}

function fnValidateSelectFormField(strFormElementID, strValidationElementID, strCheckAgainstText, strValidationText) 
{
    var objFormElement = document.getElementById(strFormElementID);
    var objSelectedOption = objFormElement.options[objFormElement.selectedIndex];
    var objValidationElement = document.getElementById(strValidationElementID);
    var blnIsValid = false;
    
    if (objSelectedOption.text == strCheckAgainstText)
    {
        objValidationElement.innerHTML = strValidationText;
        objValidationElement.style.display = 'block';
    }
    else
    {
        objValidationElement.style.display = 'none';
        blnIsValid = true;
    }
    
    return blnIsValid;
}

function fnGetFieldValue(strFieldID)
{
    var objFormElement = document.getElementById(strFieldID);
    return objFormElement.value;
}

function fnSetFieldValue(strFieldID, strValue)
{
    var objFormElement = document.getElementById(strFieldID);
    objFormElement.value = strValue;
}

//***********************************************************************************************
//                                  END GENERIC FUNCTIONS
//***********************************************************************************************

//***********************************************************************************************
//                             START FURTHER EDUCATION FUNCTIONS
//***********************************************************************************************

function fnAddFurtherEducation() 
{
    var intErrors = 0;
    var intCount = parseInt(fnGetFieldValue('furthereducationcount'));
   
    if (!fnValidateFormField('furthereducationestablishment', 'val_furthereducationestablishment', 'Please enter the establishment.')){intErrors++}
    if (!fnValidateFormField('furthereducationdate', 'val_furthereducationdate', 'Please enter date.')){intErrors++}
    if (!fnValidateFormField('furthereducationexaminations', 'val_furthereducationexaminations', 'Please enter the examination.')){intErrors++}
    if (!fnValidateFormField('furthereducationresults', 'val_furthereducationresults', 'Please enter the result.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strEstablishment = fnGetFieldValue('furthereducationestablishment');
        var strDate = fnGetFieldValue('furthereducationdate');
        var strExam = fnGetFieldValue('furthereducationexaminations');
        var strResult = fnGetFieldValue('furthereducationresults');
        var intID = intCount + 1;
        
        fnSetFieldValue('furthereducationcount', intID);
        fnSetFieldValue('furthereducationestablishment', '');
        fnSetFieldValue('furthereducationdate', '');
        fnSetFieldValue('furthereducationexaminations', '');
        fnSetFieldValue('furthereducationresults', '');
        
        var strCurrentData = fnGetFieldValue('furthereducationdata');
        var strNewData = intID;
        strNewData += strFieldSeparator + strEstablishment;
        strNewData += strFieldSeparator + strDate;
        strNewData += strFieldSeparator + strExam;
        strNewData += strFieldSeparator + strResult;
        
        if (strCurrentData.length == 0)
        {
            strCurrentData += strNewData;
        }
        else
        {
            strCurrentData += strEntrySeparator + strNewData;
        }
        
        fnSetFieldValue('furthereducationdata', strCurrentData)
        
        fnBuildFurtherEducationTable();
        
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnBuildFurtherEducationTable()
{
    var intCount = parseInt(fnGetFieldValue('furthereducationcount'));
    var strCurrentData = fnGetFieldValue('furthereducationdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var tblBody = document.getElementById('furthereducationtable').tBodies[0];
    var objCurrentRows = document.getElementById('furthereducationtable').tBodies[0].rows;
 
           
        
        for (y=objCurrentRows.length-1;y>-1;y--)
        {
            tblBody.deleteRow(y);
        }
        
        for (z=0;z<intCount;z++)
        {
            
            var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrFurtherEducationEntry[0];
            var strEstablishment = arrFurtherEducationEntry[1];
            var strDate = arrFurtherEducationEntry[2];
            var strExam = arrFurtherEducationEntry[3];
            var strResult = arrFurtherEducationEntry[4];
            
            
            var strEdit = '<a href=\"#\" onclick=\"fnEditFurtherEducation(' + (z + 1) + ');return false;\" title="Edit">Edit</a>';
            var strDelete = '<a href=\"#\" onclick=\"fnDeleteFurtherEducation(' + (z + 1) + ');return false;\" title="Delete">Delete</a>';
        
            var newRow = tblBody.insertRow(z);

            if (z % 2 == 0){newRow.className = 'light';}
            
            var newCell0 = newRow.insertCell(0);
            newCell0.innerHTML = strEstablishment;
            var newCell1 = newRow.insertCell(1);
            newCell1.innerHTML = strDate;
            var newCell2 = newRow.insertCell(2);
            newCell2.innerHTML = strExam;
            var newCell3 = newRow.insertCell(3);
            newCell3.innerHTML = strResult;

            var newCell4 = newRow.insertCell(4);
            newCell4.innerHTML = strEdit;
            var newCell5 = newRow.insertCell(5);
            newCell5.innerHTML = strDelete;
           
        }
    
        
    if (strCurrentData.length == 0)
    {
        var newRow = tblBody.insertRow(z);
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = 'No further education entries currently added';
        newCell0.colSpan = 6;
        newCell0.align = 'center';
    }
    
    
    
}

function fnEditFurtherEducation(intID)
{
    fnSetFieldValue('furthereducationeditid', intID)
        
    var strCurrentData = fnGetFieldValue('furthereducationdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        if (intCurrentID == intID)
        {
            fnSetFieldValue('furthereducationestablishment', arrFurtherEducationEntry[1]);
            fnSetFieldValue('furthereducationdate', arrFurtherEducationEntry[2]);
            fnSetFieldValue('furthereducationexaminations', arrFurtherEducationEntry[3]);
            fnSetFieldValue('furthereducationresults', arrFurtherEducationEntry[4]);
            
            var objEditFurtherEducation = document.getElementById('editFurtherEducation');
            var objCancelFurtherEducation = document.getElementById('cancelFurtherEducation');
            var objAddFurtherEducation = document.getElementById('addFurtherEducation');
            
            objEditFurtherEducation.style.display = 'block';
            objCancelFurtherEducation.style.display = 'block';
            objAddFurtherEducation.style.display = 'none';
            
        }
    }
}

function fnSubmitEditFurtherEducation()
{
    var intErrors = 0;
    if (!fnValidateFormField('furthereducationestablishment', 'val_furthereducationestablishment', 'Please enter the establishment.')){intErrors++}
    if (!fnValidateFormField('furthereducationdate', 'val_furthereducationdate', 'Please enter date.')){intErrors++}
    if (!fnValidateFormField('furthereducationexaminations', 'val_furthereducationexaminations', 'Please enter the examination.')){intErrors++}
    if (!fnValidateFormField('furthereducationresults', 'val_furthereducationresults', 'Please enter the result.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strCurrentData = fnGetFieldValue('furthereducationdata');
        var arrCurrentData = strCurrentData.split(strEntrySeparator);
        
        var intID = fnGetFieldValue('furthereducationeditid');
        var strEstablishment = fnGetFieldValue('furthereducationestablishment');
        var strDate = fnGetFieldValue('furthereducationdate');
        var strExam = fnGetFieldValue('furthereducationexaminations');
        var strResult = fnGetFieldValue('furthereducationresults');
        
        var strNewData = intID;
        strNewData += strFieldSeparator + strEstablishment;
        strNewData += strFieldSeparator + strDate;
        strNewData += strFieldSeparator + strExam;
        strNewData += strFieldSeparator + strResult;
        
        var strFinalData = '';
        
        for (z=0;z<arrCurrentData.length;z++)
        {
            var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrFurtherEducationEntry[0];
            
            
            if (intCurrentID != intID)
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = arrCurrentData[z];
                }
                else
                {
                    strFinalData += strEntrySeparator + arrCurrentData[z];
                }
            }
            else
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = strNewData;
                }
                else
                {
                    strFinalData += strEntrySeparator + strNewData;
                }
            }
        }
        
        fnSetFieldValue('furthereducationdata', strFinalData)
        fnCancelEditFurtherEducation();
        fnBuildFurtherEducationTable();
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnCancelEditFurtherEducation()
{
    fnSetFieldValue('furthereducationestablishment', '');
    fnSetFieldValue('furthereducationdate', '');
    fnSetFieldValue('furthereducationexaminations', '');
    fnSetFieldValue('furthereducationresults', '');
    
    var objEditFurtherEducation = document.getElementById('editFurtherEducation');
    var objCancelFurtherEducation = document.getElementById('cancelFurtherEducation');
    var objAddFurtherEducation = document.getElementById('addFurtherEducation');
    
    objEditFurtherEducation.style.display = 'none';
    objCancelFurtherEducation.style.display = 'none';
    objAddFurtherEducation.style.display = 'block';
}

function fnDeleteFurtherEducation(intID)
{
    var intCount = parseInt(fnGetFieldValue('furthereducationcount'));
    var strCurrentData = fnGetFieldValue('furthereducationdata');
    var strResult = '';
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var blnFoundMatch = false;
    
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strReplacementFurthEducationEntry = '';
        
        
        if (blnFoundMatch)
        {
            //we found a match, didnt add it, now we need to set all subsequent ids - 1 to keep in sync
            strReplacementFurthEducationEntry = (parseInt(arrFurtherEducationEntry[0])-1);
        }
        else
        {
            strReplacementFurthEducationEntry = arrFurtherEducationEntry[0];
        }
        
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[1];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[2];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[3];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[4];
        
        if (intCurrentID != intID)
        {
            if (strResult.length == 0)
            {
                strResult = strReplacementFurthEducationEntry;
            }
            else
            {
                strResult += strEntrySeparator + strReplacementFurthEducationEntry;
            }
        }
        else
        {
            blnFoundMatch = true;
        }
    }
    
    if (blnFoundMatch)
    {
        fnSetFieldValue('furthereducationcount', intCount - 1);
        fnSetFieldValue('furthereducationdata', strResult)
    }
    
    fnBuildFurtherEducationTable();
    //alert(fnGetFieldValue('furthereducationdata'));
}

//***********************************************************************************************
//                                 END FURTHER EDUCATION FUNCTIONS
//***********************************************************************************************

//***********************************************************************************************
//                                  START QUALIFICATIONS FUNCTIONS
//***********************************************************************************************

function fnAddQualification() 
{
    var intErrors = 0;
    var intCount = parseInt(fnGetFieldValue('qualificationscount'));
   
    if (!fnValidateFormField('qualificationdescription', 'val_qualificationdescription', 'Please enter a description.')){intErrors++}
    if (!fnValidateFormField('qualificationdate', 'val_qualificationdate', 'Please enter date.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strDesc = fnGetFieldValue('qualificationdescription');
        var strDate = fnGetFieldValue('qualificationdate');
        
        var intID = intCount + 1;
        
        fnSetFieldValue('qualificationscount', intID);
        fnSetFieldValue('qualificationdescription', '');
        fnSetFieldValue('qualificationdate', '');
        
        
        var strCurrentData = fnGetFieldValue('qualificationsdata');
        var strNewData = intID;
        strNewData += strFieldSeparator + strDesc;
        strNewData += strFieldSeparator + strDate;
        
        
        if (strCurrentData.length == 0)
        {
            strCurrentData += strNewData;
        }
        else
        {
            strCurrentData += strEntrySeparator + strNewData;
        }
        
        fnSetFieldValue('qualificationsdata', strCurrentData)
        
        fnBuildQualificationsTable();
        
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnBuildQualificationsTable()
{
    var intCount = parseInt(fnGetFieldValue('qualificationscount'));
    var strCurrentData = fnGetFieldValue('qualificationsdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var tblBody = document.getElementById('qualificationstable').tBodies[0];
    var objCurrentRows = document.getElementById('qualificationstable').tBodies[0].rows;
    
    for (y=objCurrentRows.length-1;y>-1;y--)
    {
        tblBody.deleteRow(y);
    }
    
    for (z=0;z<intCount;z++)
    {
        
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strDesc = arrFurtherEducationEntry[1];
        var strDate = arrFurtherEducationEntry[2];
       
        
        
        var strEdit = '<a href=\"#\" onclick=\"fnEditQualification(' + (z + 1) + ');return false;\" title="Edit">Edit</a>';
        var strDelete = '<a href=\"#\" onclick=\"fnDeleteQualification(' + (z + 1) + ');return false;\" title="Delete">Delete</a>';
    
        var newRow = tblBody.insertRow(z);

        if (z % 2 == 0){newRow.className = 'light';}
        
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = strDesc;
        var newCell1 = newRow.insertCell(1);
        newCell1.innerHTML = strDate;
        var newCell2 = newRow.insertCell(2);
        newCell2.innerHTML = '';
        var newCell3 = newRow.insertCell(3);
        newCell3.innerHTML = '';
        
        var newCell4 = newRow.insertCell(4);
        newCell4.innerHTML = strEdit;
        var newCell5 = newRow.insertCell(5);
        newCell5.innerHTML = strDelete;
       
    }
    
    if (strCurrentData.length == 0)
    {
        var newRow = tblBody.insertRow(z);
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = 'No qualifications currently added.';
        newCell0.colSpan = 6;
        newCell0.align = 'center';
    }
    
    
}

function fnEditQualification(intID)
{
    fnSetFieldValue('qualificationseditid', intID)
        
    var strCurrentData = fnGetFieldValue('qualificationsdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        if (intCurrentID == intID)
        {
            fnSetFieldValue('qualificationdescription', arrFurtherEducationEntry[1]);
            fnSetFieldValue('qualificationdate', arrFurtherEducationEntry[2]);
            
            
            var objEditQualification = document.getElementById('editQualification');
            var objCancelQualification = document.getElementById('cancelQualification');
            var objAddQualification = document.getElementById('addQualification');
            
            objEditQualification.style.display = 'block';
            objCancelQualification.style.display = 'block';
            objAddQualification.style.display = 'none';
            
        }
    }
}

function fnCancelEditQualification()
{
    fnSetFieldValue('qualificationdescription', '');
    fnSetFieldValue('qualificationdate', '');
    
    
    var objEditQualification = document.getElementById('editQualification');
    var objCancelQualification = document.getElementById('cancelQualification');
    var objAddQualification = document.getElementById('addQualification');
    
    objEditQualification.style.display = 'none';
    objCancelQualification.style.display = 'none';
    objAddQualification.style.display = 'block';
}


function fnSubmitEditQualification()
{
    var intErrors = 0;
    if (!fnValidateFormField('qualificationdescription', 'val_qualificationdescription', 'Please enter a description.')){intErrors++}
    if (!fnValidateFormField('qualificationdate', 'val_qualificationdate', 'Please enter date.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strCurrentData = fnGetFieldValue('qualificationsdata');
        var arrCurrentData = strCurrentData.split(strEntrySeparator);
        
        var intID = fnGetFieldValue('qualificationseditid');
        var strDesc = fnGetFieldValue('qualificationdescription');
        var strDate = fnGetFieldValue('qualificationdate');
        
        var strNewData = intID;
        strNewData += strFieldSeparator + strDesc;
        strNewData += strFieldSeparator + strDate;
        
        var strFinalData = '';
        
        for (z=0;z<arrCurrentData.length;z++)
        {
            var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrFurtherEducationEntry[0];
            
            
            if (intCurrentID != intID)
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = arrCurrentData[z];
                }
                else
                {
                    strFinalData += strEntrySeparator + arrCurrentData[z];
                }
            }
            else
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = strNewData;
                }
                else
                {
                    strFinalData += strEntrySeparator + strNewData;
                }
            }
        }
        
        fnSetFieldValue('qualificationsdata', strFinalData)
        fnCancelEditQualification();
        fnBuildQualificationsTable();
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnDeleteQualification(intID)
{
    var intCount = parseInt(fnGetFieldValue('qualificationscount'));
    var strCurrentData = fnGetFieldValue('qualificationsdata');
    var strResult = '';
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var blnFoundMatch = false;
    
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strReplacementFurthEducationEntry = '';
        
        
        if (blnFoundMatch)
        {
            //we found a match, didnt add it, now we need to set all subsequent ids - 1 to keep in sync
            strReplacementFurthEducationEntry = (parseInt(arrFurtherEducationEntry[0])-1);
        }
        else
        {
            strReplacementFurthEducationEntry = arrFurtherEducationEntry[0];
        }
        
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[1];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[2];
        
        
        if (intCurrentID != intID)
        {
            if (strResult.length == 0)
            {
                strResult = strReplacementFurthEducationEntry;
            }
            else
            {
                strResult += strEntrySeparator + strReplacementFurthEducationEntry;
            }
        }
        else
        {
            blnFoundMatch = true;
        }
    }
    
    if (blnFoundMatch)
    {
        fnSetFieldValue('qualificationscount', intCount - 1);
        fnSetFieldValue('qualificationsdata', strResult)
    }
    
    fnBuildQualificationsTable();
    //alert(fnGetFieldValue('furthereducationdata'));
}

//***********************************************************************************************
//                                  START PROFESSIONAL BODIES FUNCTIONS
//***********************************************************************************************

function fnAddProfessionalBody()
{
    var intErrors = 0;
    var intCount = parseInt(fnGetFieldValue('professionalbodycount'));
   
    if (!fnValidateFormField('professionalbody', 'val_professionalbody', 'Please enter the name of the professional body.')){intErrors++}
    if (!fnValidateFormField('membershipcategory', 'val_membershipcategory', 'Please enter the membership category.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strBody = fnGetFieldValue('professionalbody');
        var strCat = fnGetFieldValue('membershipcategory');
        
        var intID = intCount + 1;
        
        fnSetFieldValue('professionalbodycount', intID);
        fnSetFieldValue('professionalbody', '');
        fnSetFieldValue('membershipcategory', '');
        
        
        var strCurrentData = fnGetFieldValue('professionalbodydata');
        var strNewData = intID;
        strNewData += strFieldSeparator + strBody;
        strNewData += strFieldSeparator + strCat;
        
        
        if (strCurrentData.length == 0)
        {
            strCurrentData += strNewData;
        }
        else
        {
            strCurrentData += strEntrySeparator + strNewData;
        }
        
        fnSetFieldValue('professionalbodydata', strCurrentData)
        
        fnBuildProfessionalBodyTable();
        
    }
}

function fnBuildProfessionalBodyTable()
{
    var intCount = parseInt(fnGetFieldValue('professionalbodycount'));
    var strCurrentData = fnGetFieldValue('professionalbodydata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var tblBody = document.getElementById('professionalbodytable').tBodies[0];
    var objCurrentRows = document.getElementById('professionalbodytable').tBodies[0].rows;
    
    for (y=objCurrentRows.length-1;y>-1;y--)
    {
        tblBody.deleteRow(y);
    }
    
    for (z=0;z<intCount;z++)
    {
        
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strBody = arrFurtherEducationEntry[1];
        var strCat = arrFurtherEducationEntry[2];
       
        
        
        var strEdit = '<a href=\"#\" onclick=\"fnEditProfessionalBody(' + (z + 1) + ');return false;\" title="Edit">Edit</a>';
        var strDelete = '<a href=\"#\" onclick=\"fnDeleteProfessionalBody(' + (z + 1) + ');return false;\" title="Delete">Delete</a>';
    
        var newRow = tblBody.insertRow(z);

        if (z % 2 == 0){newRow.className = 'light';}
        
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = strBody;
        var newCell1 = newRow.insertCell(1);
        newCell1.innerHTML = strCat;
        var newCell2 = newRow.insertCell(2);
        newCell2.innerHTML = '';
        var newCell3 = newRow.insertCell(3);
        newCell3.innerHTML = '';
        
        var newCell4 = newRow.insertCell(4);
        newCell4.innerHTML = strEdit;
        var newCell5 = newRow.insertCell(5);
        newCell5.innerHTML = strDelete;
       
    }
    
    if (strCurrentData.length == 0)
    {
        var newRow = tblBody.insertRow(z);
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = 'No professional bodies currently added.';
        newCell0.colSpan = 6;
        newCell0.align = 'center';
    }
    
    
}

function fnEditProfessionalBody(intID)
{
    fnSetFieldValue('professionalbodyeditid', intID)
        
    var strCurrentData = fnGetFieldValue('professionalbodydata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        if (intCurrentID == intID)
        {
            fnSetFieldValue('professionalbody', arrFurtherEducationEntry[1]);
            fnSetFieldValue('membershipcategory', arrFurtherEducationEntry[2]);
            
            
            var objEditProfessionalBody = document.getElementById('editProfessionalBody');
            var objCancelProfessionalBody = document.getElementById('cancelProfessionalBody');
            var objAddProfessionalBody = document.getElementById('addProfessionalBody');
            
            objEditProfessionalBody.style.display = 'block';
            objCancelProfessionalBody.style.display = 'block';
            objAddProfessionalBody.style.display = 'none';
            
        }
    }
}

function fnCancelEditProfessionalBody()
{
    fnSetFieldValue('professionalbody', '');
    fnSetFieldValue('membershipcategory', '');
    
    
    var objEditProfessionalBody = document.getElementById('editProfessionalBody');
    var objCancelProfessionalBody = document.getElementById('cancelProfessionalBody');
    var objAddProfessionalBody = document.getElementById('addProfessionalBody');
    
    objEditProfessionalBody.style.display = 'none';
    objCancelProfessionalBody.style.display = 'none';
    objAddProfessionalBody.style.display = 'block';
}


function fnSubmitEditProfessionalBody()
{
    var intErrors = 0;
    if (!fnValidateFormField('professionalbody', 'val_professionalbody', 'Please enter the name of the professional body.')){intErrors++}
    if (!fnValidateFormField('membershipcategory', 'val_membershipcategory', 'Please enter the membership category.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strCurrentData = fnGetFieldValue('professionalbodydata');
        var arrCurrentData = strCurrentData.split(strEntrySeparator);
        
        var intID = fnGetFieldValue('professionalbodyeditid');
        var strBody = fnGetFieldValue('professionalbody');
        var strCat = fnGetFieldValue('membershipcategory');
        
        var strNewData = intID;
        strNewData += strFieldSeparator + strBody;
        strNewData += strFieldSeparator + strCat;
        
        var strFinalData = '';
        
        for (z=0;z<arrCurrentData.length;z++)
        {
            var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrFurtherEducationEntry[0];
            
            
            if (intCurrentID != intID)
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = arrCurrentData[z];
                }
                else
                {
                    strFinalData += strEntrySeparator + arrCurrentData[z];
                }
            }
            else
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = strNewData;
                }
                else
                {
                    strFinalData += strEntrySeparator + strNewData;
                }
            }
        }
        
        fnSetFieldValue('professionalbodydata', strFinalData)
        fnCancelEditProfessionalBody();
        fnBuildProfessionalBodyTable();
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnDeleteProfessionalBody(intID)
{
    var intCount = parseInt(fnGetFieldValue('professionalbodycount'));
    var strCurrentData = fnGetFieldValue('professionalbodydata');
    var strResult = '';
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var blnFoundMatch = false;
    
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strReplacementFurthEducationEntry = '';
        
        
        if (blnFoundMatch)
        {
            //we found a match, didnt add it, now we need to set all subsequent ids - 1 to keep in sync
            strReplacementFurthEducationEntry = (parseInt(arrFurtherEducationEntry[0])-1);
        }
        else
        {
            strReplacementFurthEducationEntry = arrFurtherEducationEntry[0];
        }
        
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[1];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[2];
        
        
        if (intCurrentID != intID)
        {
            if (strResult.length == 0)
            {
                strResult = strReplacementFurthEducationEntry;
            }
            else
            {
                strResult += strEntrySeparator + strReplacementFurthEducationEntry;
            }
        }
        else
        {
            blnFoundMatch = true;
        }
    }
    
    if (blnFoundMatch)
    {
        fnSetFieldValue('professionalbodycount', intCount - 1);
        fnSetFieldValue('professionalbodydata', strResult)
    }
    
    fnBuildProfessionalBodyTable();
    //alert(fnGetFieldValue('furthereducationdata'));
}


//***********************************************************************************************
//                                  START OTHER ORGANISATIONS FUNCTIONS
//***********************************************************************************************

function fnAddOtherOrganisation() 
{
    var intErrors = 0;
    var intCount = parseInt(fnGetFieldValue('otherorganisationcount'));
   
    if (!fnValidateFormField('otherorganisation', 'val_otherorganisation', 'Please enter the name of the organisation.')){intErrors++}
    if (!fnValidateFormField('position', 'val_position', 'Please enter the position occupied.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strBody = fnGetFieldValue('otherorganisation');
        var strCat = fnGetFieldValue('position');
        
        var intID = intCount + 1;
        
        fnSetFieldValue('otherorganisationcount', intID);
        fnSetFieldValue('otherorganisation', '');
        fnSetFieldValue('position', '');
        
        
        var strCurrentData = fnGetFieldValue('otherorganisationdata');
        var strNewData = intID;
        strNewData += strFieldSeparator + strBody;
        strNewData += strFieldSeparator + strCat;
        
        
        if (strCurrentData.length == 0)
        {
            strCurrentData += strNewData;
        }
        else
        {
            strCurrentData += strEntrySeparator + strNewData;
        }
        
        fnSetFieldValue('otherorganisationdata', strCurrentData)
        
        fnBuildOtherOrganisationTable();
        
    }
}

function fnBuildOtherOrganisationTable()
{
    var intCount = parseInt(fnGetFieldValue('otherorganisationcount'));
    var strCurrentData = fnGetFieldValue('otherorganisationdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var tblBody = document.getElementById('otherorganisationtable').tBodies[0];
    var objCurrentRows = document.getElementById('otherorganisationtable').tBodies[0].rows;
    
    for (y=objCurrentRows.length-1;y>-1;y--)
    {
        tblBody.deleteRow(y);
    }
    
    for (z=0;z<intCount;z++)
    {
        
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strBody = arrFurtherEducationEntry[1];
        var strCat = arrFurtherEducationEntry[2];
       
        
        
        var strEdit = '<a href=\"#\" onclick=\"fnEditOtherOrganisation(' + (z + 1) + ');return false;\" title="Edit">Edit</a>';
        var strDelete = '<a href=\"#\" onclick=\"fnDeleteOtherOrganisation(' + (z + 1) + ');return false;\" title="Delete">Delete</a>';
    
        var newRow = tblBody.insertRow(z);

        if (z % 2 == 0){newRow.className = 'light';}
        
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = strBody;
        var newCell1 = newRow.insertCell(1);
        newCell1.innerHTML = strCat;
        var newCell2 = newRow.insertCell(2);
        newCell2.innerHTML = '';
        var newCell3 = newRow.insertCell(3);
        newCell3.innerHTML = '';
        
        var newCell4 = newRow.insertCell(4);
        newCell4.innerHTML = strEdit;
        var newCell5 = newRow.insertCell(5);
        newCell5.innerHTML = strDelete;
       
    }
    
    if (strCurrentData.length == 0)
    {
        var newRow = tblBody.insertRow(z);
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = 'No organisations currently added.';
        newCell0.colSpan = 6;
        newCell0.align = 'center';
    }
}

function fnEditOtherOrganisation(intID)
{
    fnSetFieldValue('otherorganisationeditid', intID)
        
    var strCurrentData = fnGetFieldValue('otherorganisationdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        if (intCurrentID == intID)
        {
            fnSetFieldValue('otherorganisation', arrFurtherEducationEntry[1]);
            fnSetFieldValue('position', arrFurtherEducationEntry[2]);
            
            
            var objEditOtherOrganisation = document.getElementById('editOtherOrganisation');
            var objCancelOtherOrganisation = document.getElementById('cancelOtherOrganisation');
            var objAddOtherOrganisation = document.getElementById('addOtherOrganisation');
            
            objEditOtherOrganisation.style.display = 'block';
            objCancelOtherOrganisation.style.display = 'block';
            objAddOtherOrganisation.style.display = 'none';
            
        }
    }
}

function fnCancelEditOtherOrganisation()
{
    fnSetFieldValue('otherorganisation', '');
    fnSetFieldValue('position', '');
    
    
    var objEditOtherOrganisation = document.getElementById('editOtherOrganisation');
    var objCancelOtherOrganisation = document.getElementById('cancelOtherOrganisation');
    var objAddOtherOrganisation = document.getElementById('addOtherOrganisation');
    
    objEditOtherOrganisation.style.display = 'none';
    objCancelOtherOrganisation.style.display = 'none';
    objAddOtherOrganisation.style.display = 'block';
}


function fnSubmitEditOtherOrganisation()
{
    var intErrors = 0;
    if (!fnValidateFormField('otherorganisation', 'val_otherorganisation', 'Please enter the name of the organisation.')){intErrors++}
    if (!fnValidateFormField('position', 'val_position', 'Please enter the position occupied.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strCurrentData = fnGetFieldValue('otherorganisationdata');
        var arrCurrentData = strCurrentData.split(strEntrySeparator);
        
        var intID = fnGetFieldValue('otherorganisationeditid');
        var strBody = fnGetFieldValue('otherorganisation');
        var strCat = fnGetFieldValue('position');
        
        var strNewData = intID;
        strNewData += strFieldSeparator + strBody;
        strNewData += strFieldSeparator + strCat;
        
        var strFinalData = '';
        
        for (z=0;z<arrCurrentData.length;z++)
        {
            var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrFurtherEducationEntry[0];
            
            
            if (intCurrentID != intID)
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = arrCurrentData[z];
                }
                else
                {
                    strFinalData += strEntrySeparator + arrCurrentData[z];
                }
            }
            else
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = strNewData;
                }
                else
                {
                    strFinalData += strEntrySeparator + strNewData;
                }
            }
        }
        
        fnSetFieldValue('otherorganisationdata', strFinalData)
        fnCancelEditOtherOrganisation();
        fnBuildOtherOrganisationTable();
        //alert(fnGetFieldValue('furthereducationdata'));
    }
}

function fnDeleteOtherOrganisation(intID)
{
    var intCount = parseInt(fnGetFieldValue('otherorganisationcount'));
    var strCurrentData = fnGetFieldValue('otherorganisationdata');
    var strResult = '';
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var blnFoundMatch = false;
    
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrFurtherEducationEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrFurtherEducationEntry[0];
        var strReplacementFurthEducationEntry = '';
        
        
        if (blnFoundMatch)
        {
            //we found a match, didnt add it, now we need to set all subsequent ids - 1 to keep in sync
            strReplacementFurthEducationEntry = (parseInt(arrFurtherEducationEntry[0])-1);
        }
        else
        {
            strReplacementFurthEducationEntry = arrFurtherEducationEntry[0];
        }
        
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[1];
        strReplacementFurthEducationEntry += strFieldSeparator + arrFurtherEducationEntry[2];
        
        
        if (intCurrentID != intID)
        {
            if (strResult.length == 0)
            {
                strResult = strReplacementFurthEducationEntry;
            }
            else
            {
                strResult += strEntrySeparator + strReplacementFurthEducationEntry;
            }
        }
        else
        {
            blnFoundMatch = true;
        }
    }
    
    if (blnFoundMatch)
    {
        fnSetFieldValue('otherorganisationcount', intCount - 1);
        fnSetFieldValue('otherorganisationdata', strResult)
    }
    
    fnBuildOtherOrganisationTable();
    //alert(fnGetFieldValue('furthereducationdata'));
}


//***********************************************************************************************
//                             START PREVIOUS EMPLOYMENT FUNCTIONS
//***********************************************************************************************

function fnAddPreviousEmployment() 
{
    var intErrors = 0;
    var intCount = parseInt(fnGetFieldValue('previousemploymentcount'));
   
    if (!fnValidateFormField('previousemploymentemployer', 'val_previousemploymentemployer', 'Please enter the Employer.')){intErrors++}
    if (!fnValidateFormField('previousemploymentdatefrom', 'val_previousemploymentdatefrom', 'Please enter start date.')){intErrors++}
    if (!fnValidateFormField('previousemploymentdateto', 'val_previousemploymentdateto', 'Please enter end date.')){intErrors++}
    if (!fnValidateFormField('previousemploymentsalary', 'val_previousemploymentsalary', 'Please enter the Salary.')){intErrors++}
    if (!fnValidateFormField('previousemploymentreason', 'val_previousemploymentreason', 'Please enter the reason for leaving.')){intErrors++}
    if (!fnValidateFormField('previousemploymentresponsibilities', 'val_previousemploymentresponsibilities', 'Please enter responsibilities.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strEmployer = fnGetFieldValue('previousemploymentemployer');
        var strDate = fnGetFieldValue('previousemploymentdatefrom');
        var strDateTo = fnGetFieldValue('previousemploymentdateto');
        var strSalary = fnGetFieldValue('previousemploymentsalary');
        var strReason = fnGetFieldValue('previousemploymentreason');
        var strResponsibilities = fnGetFieldValue('previousemploymentresponsibilities');
        var intID = intCount + 1;
        
        fnSetFieldValue('previousemploymentcount', intID);
        fnSetFieldValue('previousemploymentemployer', '');
        fnSetFieldValue('previousemploymentdatefrom', '');
        fnSetFieldValue('previousemploymentdateto', '');
        fnSetFieldValue('previousemploymentsalary', '');
        fnSetFieldValue('previousemploymentreason', '');
        fnSetFieldValue('previousemploymentresponsibilities', '');
        
        var strCurrentData = fnGetFieldValue('previousemploymentdata');
        var strNewData = intID;
        strNewData += strFieldSeparator + strEmployer;
        strNewData += strFieldSeparator + strDate;
        strNewData += strFieldSeparator + strDateTo;
        strNewData += strFieldSeparator + strSalary;
        strNewData += strFieldSeparator + strReason;
        strNewData += strFieldSeparator + strResponsibilities;
        
        if (strCurrentData.length == 0)
        {
            strCurrentData += strNewData;
        }
        else
        {
            strCurrentData += strEntrySeparator + strNewData;
        }
        
        fnSetFieldValue('previousemploymentdata', strCurrentData)
        
        fnBuildPreviousEmploymentTable();
        
        //alert(fnGetFieldValue('previousemploymentdata'));
    }
}

function fnBuildPreviousEmploymentTable()
{
    var intCount = parseInt(fnGetFieldValue('previousemploymentcount'));
    var strCurrentData = fnGetFieldValue('previousemploymentdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var tblBody = document.getElementById('previousemploymenttable').tBodies[0];
    var objCurrentRows = document.getElementById('previousemploymenttable').tBodies[0].rows;
    
    for (y=objCurrentRows.length-1;y>-1;y--)
    {
        tblBody.deleteRow(y);
    }
    
    for (z=0;z<intCount;z++)
    {
        
        var arrPreviousEmploymentEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrPreviousEmploymentEntry[0];
        var strEmployer = arrPreviousEmploymentEntry[1];
        var strDate = arrPreviousEmploymentEntry[2];
        var strDateTo = arrPreviousEmploymentEntry[3];
        var strSalary = arrPreviousEmploymentEntry[4];
        var strReason = arrPreviousEmploymentEntry[5];
        var strResponsibilities = arrPreviousEmploymentEntry[6];
        
        
        var strEdit = '<a href=\"#\" onclick=\"fnEditPreviousEmployment(' + (z + 1) + ');return false;\" title="Edit">Edit</a>';
        var strDelete = '<a href=\"#\" onclick=\"fnDeletePreviousEmployment(' + (z + 1) + ');return false;\" title="Delete">Delete</a>';
    
        var newRow = tblBody.insertRow(z);

        if (z % 2 == 0){newRow.className = 'light';}
        
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = strEmployer;
        var newCell1 = newRow.insertCell(1);
        newCell1.innerHTML = strDate;
        var newCell2 = newRow.insertCell(2);
        newCell2.innerHTML = strDateTo;
        var newCell3 = newRow.insertCell(3);
        newCell3.innerHTML = strSalary;
        var newCell4 = newRow.insertCell(4);
        newCell4.innerHTML = strReason;
        var newCell5 = newRow.insertCell(5);
        newCell5.innerHTML = strResponsibilities;

        var newCell6 = newRow.insertCell(6);
        newCell6.innerHTML = strEdit;
        var newCell7 = newRow.insertCell(7);
        newCell7.innerHTML = strDelete;
       
    }
    
    if (strCurrentData.length == 0)
    {
        var newRow = tblBody.insertRow(z);
        var newCell0 = newRow.insertCell(0);
        newCell0.innerHTML = 'No employment entries currently added';
        newCell0.colSpan = 8;
        newCell0.align = 'center';
    }
    
    
}

function fnEditPreviousEmployment(intID)
{
    fnSetFieldValue('previousemploymenteditid', intID)
        
    var strCurrentData = fnGetFieldValue('previousemploymentdata');
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrPreviousEmploymentEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrPreviousEmploymentEntry[0];
        if (intCurrentID == intID)
        {
            fnSetFieldValue('previousemploymentemployer', arrPreviousEmploymentEntry[1]);
            fnSetFieldValue('previousemploymentdatefrom', arrPreviousEmploymentEntry[2]);
            fnSetFieldValue('previousemploymentdateto', arrPreviousEmploymentEntry[3]);
            fnSetFieldValue('previousemploymentsalary', arrPreviousEmploymentEntry[4]);
            fnSetFieldValue('previousemploymentreason', arrPreviousEmploymentEntry[5]);
            fnSetFieldValue('previousemploymentresponsibilities', arrPreviousEmploymentEntry[6]);
            
            var objEditPreviousEmployment = document.getElementById('editPreviousEmployment');
            var objCancelPreviousEmployment = document.getElementById('cancelPreviousEmployment');
            var objAddPreviousEmployment = document.getElementById('addPreviousEmployment');
            
            objEditPreviousEmployment.style.display = 'block';
            objCancelPreviousEmployment.style.display = 'block';
            objAddPreviousEmployment.style.display = 'none';
            
        }
    }
}

function fnSubmitEditPreviousEmployment()
{
    var intErrors = 0;
    if (!fnValidateFormField('previousemploymentemployer', 'val_previousemploymentemployer', 'Please enter the Employer.')){intErrors++}
    if (!fnValidateFormField('previousemploymentdatefrom', 'val_previousemploymentdatefrom', 'Please enter start date.')){intErrors++}
    if (!fnValidateFormField('previousemploymentdateto', 'val_previousemploymentdateto', 'Please enter end date.')){intErrors++}
    if (!fnValidateFormField('previousemploymentsalary', 'val_previousemploymentsalary', 'Please enter the Salary.')){intErrors++}
    if (!fnValidateFormField('previousemploymentreason', 'val_previousemploymentreason', 'Please enter the reason for leaving.')){intErrors++}
    if (!fnValidateFormField('previousemploymentresponsibilities', 'val_previousemploymentresponsibilities', 'Please enter responsibilities.')){intErrors++}
    
    if (intErrors>0)
    {
        alert('Please ensure all required fields are completed.');
    }
    else
    {
        var strCurrentData = fnGetFieldValue('previousemploymentdata');
        var arrCurrentData = strCurrentData.split(strEntrySeparator);
        
        var intID = fnGetFieldValue('previousemploymenteditid');
        var strEmployer = fnGetFieldValue('previousemploymentemployer');
        var strDate = fnGetFieldValue('previousemploymentdatefrom');
        var strDateTo = fnGetFieldValue('previousemploymentdateto');
        var strSalary = fnGetFieldValue('previousemploymentsalary');
        var strReason = fnGetFieldValue('previousemploymentreason');
        var strResponsibilities = fnGetFieldValue('previousemploymentresponsibilities');
        
        var strNewData = intID;
        strNewData += strFieldSeparator + strEmployer;
        strNewData += strFieldSeparator + strDate;
        strNewData += strFieldSeparator + strDateTo;
        strNewData += strFieldSeparator + strSalary;
        strNewData += strFieldSeparator + strReason;
        strNewData += strFieldSeparator + strResponsibilities;
        
        var strFinalData = '';
        
        for (z=0;z<arrCurrentData.length;z++)
        {
            var arrPreviousEmploymentEntry = arrCurrentData[z].split(strFieldSeparator);
            var intCurrentID = arrPreviousEmploymentEntry[0];
            
            
            if (intCurrentID != intID)
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = arrCurrentData[z];
                }
                else
                {
                    strFinalData += strEntrySeparator + arrCurrentData[z];
                }
            }
            else
            {
                if (strFinalData.length == 0)
                {
                    strFinalData = strNewData;
                }
                else
                {
                    strFinalData += strEntrySeparator + strNewData;
                }
            }
        }
        
        fnSetFieldValue('previousemploymentdata', strFinalData)
        fnCancelEditPreviousEmployment();
        fnBuildPreviousEmploymentTable();
        //alert(fnGetFieldValue('previousemploymentdata'));
    }
}

function fnCancelEditPreviousEmployment()
{
    fnSetFieldValue('previousemploymentemployer', '');
    fnSetFieldValue('previousemploymentdatefrom', '');
    fnSetFieldValue('previousemploymentdateto', '');
    fnSetFieldValue('previousemploymentsalary', '');
    fnSetFieldValue('previousemploymentreason', '');
    fnSetFieldValue('previousemploymentresponsibilities', '');
    
    var objEditPreviousEmployment = document.getElementById('editPreviousEmployment');
    var objCancelPreviousEmployment = document.getElementById('cancelPreviousEmployment');
    var objAddPreviousEmployment = document.getElementById('addPreviousEmployment');
    
    objEditPreviousEmployment.style.display = 'none';
    objCancelPreviousEmployment.style.display = 'none';
    objAddPreviousEmployment.style.display = 'block';
}

function fnDeletePreviousEmployment(intID)
{
    var intCount = parseInt(fnGetFieldValue('previousemploymentcount'));
    var strCurrentData = fnGetFieldValue('previousemploymentdata');
    var strResult = '';
    var arrCurrentData = strCurrentData.split(strEntrySeparator);
    var blnFoundMatch = false;
    
    for (z=0;z<arrCurrentData.length;z++)
    {
        var arrPreviousEmploymentEntry = arrCurrentData[z].split(strFieldSeparator);
        var intCurrentID = arrPreviousEmploymentEntry[0];
        var strReplacementFurthEducationEntry = '';
        
        
        if (blnFoundMatch)
        {
            //we found a match, didnt add it, now we need to set all subsequent ids - 1 to keep in sync
            strReplacementFurthEducationEntry = (parseInt(arrPreviousEmploymentEntry[0])-1);
        }
        else
        {
            strReplacementFurthEducationEntry = arrPreviousEmploymentEntry[0];
        }
        
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[1];
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[2];
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[3];
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[4];
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[5];
        strReplacementFurthEducationEntry += strFieldSeparator + arrPreviousEmploymentEntry[6];
        
        if (intCurrentID != intID)
        {
            if (strResult.length == 0)
            {
                strResult = strReplacementFurthEducationEntry;
            }
            else
            {
                strResult += strEntrySeparator + strReplacementFurthEducationEntry;
            }
        }
        else
        {
            blnFoundMatch = true;
        }
    }
    
    if (blnFoundMatch)
    {
        fnSetFieldValue('previousemploymentcount', intCount - 1);
        fnSetFieldValue('previousemploymentdata', strResult)
    }
    
    fnBuildPreviousEmploymentTable();
    //alert(fnGetFieldValue('previousemploymentdata'));
}
