﻿
// validate the form...
$(document).ready(function() {

    $('.jsiframe').each(function() {
        var html;
        if ($.browser.msie) html = $(this).html();      	 // IE
        else if ($.browser.mozilla) html = $(this).text();   // Firefox
        else if ($.browser.safari) html = $(this).text();    // Safari
        else if ($.browser.opera) html = $(this).text();     // Opera
        else html = $(this).text();                          // Default (not sure if this should be 'html' or 'text'.

        $(this).after(html);
        $(this).remove();
    });

    $('#addNewDelegate').show();
    $('.clearDelegate').show();

    $('.clearDelegate').click(function() {
        ClearRow(this);
        return false;
    });

    $('#addNewDelegate').click(function() {
        AddRow();
        return false;
    });

    InitializeDelegateTable();

    // prevent 'paste' on the confirm email textbox

    $('.valConfirmEmail').bind("contextmenu", function() {
        return false;
    });


    // full page validation
    $('.sendButton').click(function() {
        ClearErrorMessages();
        var errors = false;


        $('.valRequired').each(function() {
            if (!isValid(this)) {
                AddErrorMessage($(this));
                errors = true;
            }
        });

        $('.valMin2Char').each(function() {
            if (!isMin2Char($(this).val())) {
                AddErrorMessage($(this));
                errors = true;
            }
        });

        $('.valNumber').each(function() {
            if (!isValidNumber($(this).val())) {
                AddErrorMessage($(this));
                errors = true;
            }
        });

        $('.valPhoneNumber').each(function() {
            if (!isValidPhoneNumber($(this).val())) {
                AddErrorMessage($(this), 'Please enter a valid telephone number');
                errors = true;
            }
            else if (is08or09($(this).val())) {
                    AddErrorMessage($(this), 'Unfortunately CCCS are unable to contact you on telephone numbers beginning 08 or 09');
                    errors = true;
            }
        });

        $('.valEmail').each(function() {
            if ($(this).val() == '') {
                AddErrorMessage($(this));
                errors = true;
            }
            else if (!isValidEmailAddress($(this).val())) {
                AddErrorMessage($(this), 'Please enter a valid email address');
                errors = true;
            }
        });

        $('.valConfirmEmail').each(function() {
            if ($(this).val() == '') {
                AddErrorMessage($(this));
                errors = true;
            }
            else if ($(this).val() != $('.valEmail').val()) {
                AddErrorMessage($(this), 'Email address does not match. Please reconfirm your email address');
                errors = true;
            }
        });


        if (errors == true) {
            return false;
        }

    });
});

function AddErrorMessage(jControl, msg) {
    if (msg == undefined) msg = jControl.attr('title');
    jControl.parent('.irow').addClass("rowError");
    jControl.after("<span class='valMessage'>" + msg + "</span>");
}

function RemoveErrorMessage(jControl) {
    jControl.parent('.irow').children('.valMessage').remove();
    jControl.parent('.irow').removeClass('rowError');
}

function ClearErrorMessages(jControl) {
    $('.irow').each(function() {
        $(this).children('.valMessage').remove();
        $(this).removeClass('rowError');
    });
}


function isValid(domObj) {


    var type = $(domObj).attr('type');
    if (type == undefined) type = $(domObj).find('input').attr('type');

    if (type == 'text') return $(domObj).val() != '';
    if (type == 'textarea') return $(domObj).val() != '';
    if (type == 'select-one') return $(domObj).val() != '';

    if (type == 'checkbox') return $(domObj).find(':checked').length > 0;
    if (type == 'radio') return $(domObj).find(':checked').length > 0;

    return false;
}

function isValidNumber(num) {
    if (num == '') return true;
    return num.match(/^[0-9 ]+$/);
}

function isValidPhoneNumber(num) {
    if (num == '') return true;
    return num.match(/^[0-9]{9,}$/);
}

function isMin2Char(textVal) {
    if (textVal.length > 1) { return true; }
    else {return false; }
}

function is08or09(num) {
    if (num.substring(0, 2) == '08' || num.substring(0, 2) == '09') { return true }
    else { return false }
}

function isValidEmailAddress(emailAddress) {
    //return emailAddress.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
    return emailAddress.match(/^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/);
    //var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    //return pattern.test(emailAddress);
}

function InitializeDelegateTable() {
    HideAllEmptyRows();
    ShowFirstRow();
}

function HideAllEmptyRows() {
    $('#tbldelegates tr').each(function() {
        var row = $(this);
        var isEmpty = true;
        var isHeader = false;
        if (row.find('th').length > 0) isHeader = true;
        row.find('input').each(function() {
            if ($(this).val() != '') isEmpty = false;
        });
        if (!isHeader && isEmpty) {
            HideRow(row);
        }
    });
}

function ShowFirstRow() {
    var hiddenRows = $('#tbldelegates .hiddenrow').length;
    var totalRows = $('#tbldelegates tr').length - 1;
    if (hiddenRows == totalRows) AddRow();
}

function ClearRow(obj) {
    var theRow = $(obj).parent().parent('tr');
    theRow.find('input').val('');
    HideRow(theRow);
}

function AddRow() {
    var theRow = $('#tbldelegates .hiddenrow');
    ShowRow($(theRow[0]));
}

function ShowRow(row) {
    row.removeClass('hiddenrow');
}

function HideRow(row) {
    row.addClass('hiddenrow');
    // move it to the bottom (do not use remove as we'll loose our event handler)
    var allRows = $('#tbldelegates tr');
    var lastRow = $(allRows[allRows.length - 1]);
    if (lastRow[0] == row[0]) return;
    row.insertAfter(lastRow);
}

