/* 

*/

uniqueContactList = 0

function validateForm() 
{
    formvalid = true;
    self.uniqueContactList = new Array()
    
    args = validateForm.arguments
    if (args.length > 1)
    {
        form = args[1]
        element = document[form]
        inputs = element.elements
    }
    else
        inputs = document.forms[0].elements

    for (var i=0; i<inputs.length; i++) 
    {
        input = inputs[i]
        //Get All Validators for this field
        
        functions = input.className.split(' ')
        if (input.className != '')
        {

            for (idx = 0; idx < functions.length; idx++)
            {

                if ( functions[idx] == 'Required')
                {
                    formvalid = validateRequired(input, 1)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'IsNumeric')
                {
                    formvalid = validateIsNumeric(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'PAYE')
                {
                    formvalid = validatePAYE(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'UniqueContact')
                {
                    formvalid = validateUniqueContact(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'ContactNumber')
                {
                    formvalid = validateContactNumber(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'Email')
                {
                    formvalid = validateEmail(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

                if ( functions[idx] == 'IsIDNumber')
                {
                    formvalid = validateIsIDNumber(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }


                if ( functions[idx] == 'ShortDate')
                {
                    formvalid = validateShortDate(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }
                
                if ( functions[idx] == 'IsDate')
                {
                    
                    formvalid = validateIsDate(input)
                }
                if (formvalid == false)
                {
                    input.focus()
                    break
                }

            }
        }

        if (formvalid == false)
           break

    }


    if (formvalid == false) 
    {
        //alert("All fields are required.");
        return false;                   
    }
    else 
    {
        document.forms[0].submit();                      
    }
    return true;
}

function validateContactNumber(input) 
{

    if (String(input.value).length > 0 && String(input.value).length < 10) 
    {
        alert("ContactNumber field " + input.name + ' is less that 10 characters')
        return false
    }
    else
        return true
        
}

function validateShortDate(field)
{
    length = String(field.value).length

    if (field.value != 0)
    {
        if (length > 0 && length != 4) 
        {
            alert("Date Field " + input.name + ' must be exactly 4 digits long')
            return false;
        }
    }
    return true;
}

function validateIsDate(field)
{
    
    length = String(field.value).length
    if (length > 0) 
    {
        var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
        if (re.test(field.value)) 
        {
            var dArr = field.value.split("/");
            dd = dArr[0]
            mm = dArr[1]
            yyyy = dArr[2]

            d = new Date(mm + "/" + dd + "/" + yyyy)
            valid_date = d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;

            if (! valid_date )
            {

                alert("Date Field " + input.name + ' must be in format dd/mm/yyyy')
                return false;

            }

        }
        else
        {
            alert("Date Field " + input.name + ' must be in format dd/mm/yyyy')
            return false;
        }
    }

    return true;
}

function validateEmail(field)
{
    if (String(field.value).length > 0) 
    {
        with (field)
        {
            apos=value.indexOf("@");
            dotpos=value.lastIndexOf(".");
            if (apos<1||dotpos-apos<2) 
            {
                alert("Email Field is not a valid email address")
                return false;
            }
            else 
            {
                return true;
            }
        }
    }
}

function validateUniqueContact(input)
{
    
    val  = input.value
    found = false
    for (var i=0; i<self.uniqueContactList.length; i++) 
    {
        if (self.uniqueContactList[i] == val)
        {
            found = true
            alert("Cannot have duplicate contact methods")
            return false
        }
    }
    if (found == false && parseInt(val) != 0 )
    {
        self.uniqueContactList[self.uniqueContactList.length] = val
        return true
    }
    
}

function validateIsNumeric(input)
{
    
    param = String(input.value)
    if (param.length > 0) 
    {
        if (isNaN (param)== true)
        {
            alert('A Numeric Field contains non-numeric characters')
            return false
        }
        else
        {
            return true
        }
    }
    return true
}

function validateIsIDNumber(input)
{

    if (String(input.value).length < 13) 
    {
        alert("ID Number must be 13 digits")
        return false
    }
    else
        return true
}


function validateRequired(input, display_alert) 
{


    if (input.type == "checkbox")
    {
        if (input.checked == true)
            return true
        else
        {
            alert("A Required Field is missing")
            return false
        }

    }
    if (String(input.value).length == 0) 
    {
        if (display_alert == 1)
            alert("A Required Field is missing")
        return false
    }
    else
        return true
        
}

function validatePAYE(input) 
{
    
    valid = true
    value = String(input.value)
    if (value.length > 0)
    {
        if (value.length == 10) 
        {
            if (value.charAt(0) == '7')
            {
                if (value.substr(1,2) != '99')
                {
                    split_array = value.split("")

                    total = 0

                    third_digit = parseInt(split_array[2]) * 2
                    if (third_digit > 9)
                        third_digit = (third_digit % 10 )  + 1

                    fifth_digit = parseInt(split_array[4]) * 2
                    if (fifth_digit > 9)
                        fifth_digit = (fifth_digit % 10 )  + 1

                    seventh_digit = parseInt(split_array[6]) * 2
                    if (seventh_digit > 9)
                        seventh_digit = (seventh_digit % 10 )  + 1

                    ninth_digit = parseInt(split_array[8]) * 2
                    if (ninth_digit > 9)
                        ninth_digit = (ninth_digit % 10 )  + 1

                    total = third_digit + fifth_digit + seventh_digit + ninth_digit
                    total += 8

                    second_digit = parseInt(split_array[1]) 
                    fourth_digit = parseInt(split_array[3])
                    sixth_digit = parseInt(split_array[5]) 
                    eighth_digit = parseInt(split_array[7]) 
                
                    total += second_digit + fourth_digit + sixth_digit + eighth_digit

                    total = total % 10
                    check = 10 - total

                    if (check > 10)
                        check = check - 10

                    if (check != parseInt(split_array[9]))
                        valid = false
                }
                else
                    valid = false

            }
            else
                valid = false

        }
        else
           valid = false
    }

    if (valid == true)
        return true
    else
    {
        alert("PAYE Field " + input.name + ' is not a valid PAYE Number')
        return valid
    }
        
}


function validateIndividualEssaSearchForm() 
{
    formvalid = true;


    id = document.getElementById('RSAIDNumber')
    
    if (String(id.value).length > 0)
    {
            formvalid = validateIsNumeric(id)
            if (formvalid == true)
                formvalid = validateIsIDNumber(id)
    }
    else
    {

        firstname = document.getElementById('FirstName')
        surname = document.getElementById('Surname')
        dob = document.getElementById('DateOfBirth')

        valid = validateRequired(firstname, 0)
        if (valid == false)
          formvalid = false 

        valid = validateRequired(surname, 0)
        if (valid == false)
          formvalid = false 

        valid = validateRequired(dob, 0)
        if (valid == false)
          formvalid = false 
    }
        

    if (formvalid == false) 
    {
        if (String(id.value).length == 0)
            alert("Either ID Number or First Name AND Surname AND Date Of Birth must be given");
        return false;                   
    }
    else 
    {
        document.forms[0].submit();                      
    }
    return true;
}



function validatePlacementForm(status_element, comment_element, opp_id, placementid) 
{
    stat = document.getElementById(status_element).options[document.getElementById(status_element).selectedIndex].value
    comment = document.getElementById(comment_element).value

    if (String(comment).length > 0)
    {
        document.getElementById('PlacementStatusID').value = stat
        document.getElementById('Comments').value = comment
        document.getElementById('OpportunityID').value = opp_id
        document.getElementById('PlacementID').value = placementid
        document.forms[0].submit();                      
    }
    else
    {
        alert("When Changing the status of a placement, a reason must be given in the comments field")
        return false
    }

    return true;
}

function checkDateLater(date_from, date_to)
{
    d1_exists = false
    d2_exists = false

    date1 = document.getElementById(date_from).value
    if (String(date1).length > 0)
    {
        d1_exists = true
        split_array1 = date1.split("/")        
        day1 = parseInt(split_array1[0])
        month1 = parseInt(split_array1[1])
        year1 = parseInt(split_array1[2])
    }
    date2 = document.getElementById(date_to).value
    if (String(date2).length > 0)
    {
        d2_exists = true
        split_array2 = date2.split("/")        
        day2 = parseInt(split_array2[0])
        month2 = parseInt(split_array2[1])
        year2 = parseInt(split_array2[2])
    }
    valid_date = true
    if (d1_exists && d2_exists)
    {
        d1 = (year1 * 10000) + (month1* 100) + (day1)
        d2 = (year2 * 10000) + (month2* 100) + (day2)
        
        if (d1 > d2)
            valid_date = false
    }
    else
    {
        if (d2_exists)
        {
            alert("Remember to specify a start date")
        }
    }
            
    if (valid_date == false)
        alert ("Starting Date is later than End Date")
}




