/* JavaScript Document
 * Contains methods used for form validation & masking inputs
 * NB: Most validation methods are called by CustomValidatorControls
 */

// DJA Added a trim function
function rightTrim(sString) {
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

// checks that a value is present
function Req(source, arguments) {
//    arguments.IsValid = arguments.Value != "";
    arguments.IsValid = rightTrim(arguments.Value) != "";
    if (!arguments.IsValid) {
        var feedback_req = document.getElementById("feedback_req");
        if (feedback_req != null) {
            feedback_req.innerHTML = "* Please fill out all of the mandatory fields marked with an *, including either your home or mobile phone number.";
            feedback_req.className = "feedback";
        }
        source.innerHTML = "*";
    }
}
// checks that a value is present and that it is a valid date
function ReqDate(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^(([0-2][0-9]|30)\/(04|06|09|11)|([0-2][0-9]|3[01])\/(01|03|05|07|08|10|12)|([0-2][0-9])\/02)\/[12]\d{3}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid date");
        } else {
            source.innerHTML = " ";
        }
    }
}
// checks that if a value is present, that it is a jpg file
function PossJpg(source, arguments) {
    if (arguments.Value != "") {
        arguments.IsValid = arguments.Value.match(/jpg|JPG|jpeg|JPEG$/);
        if (!arguments.IsValid) source.innerHTML = "Please only upload .JPG images";
        else source.innerHTML = " ";
    }
}
// checks that if a value is present, that it is a doc file
function PossDoc(source, arguments) {
    if (arguments.Value != "") {
        arguments.IsValid = !arguments.Value.match(/docx|DOCX$/);
        if (arguments.IsValid)
            arguments.IsValid = arguments.Value.match(/doc|DOC$/);
        if (!arguments.IsValid) source.innerHTML = "Please only upload .DOC files";
        else source.innerHTML = " ";
    }
}
// checks that a value is present and that it is a valid phone number
function ReqPhoneNumber(source, arguments) {
    var prefix = "ctl00_MCPH_formView_";
    if (document.location.href.indexOf("apply_online.aspx") != -1) var prefix = "ctl00_MCPH_";
    
    var peo_home_tel = document.getElementById(prefix + "peo_home_tel").value;
    var peo_other_tel = document.getElementById(prefix + "peo_other_tel").value;
    arguments.IsValid = peo_home_tel + "" + peo_other_tel != "";
    if (!arguments.IsValid) source.innerHTML = "*";
    if (arguments.IsValid && arguments.Value != "") {
        arguments.IsValid = arguments.Value.match(/^[0-9 ()+]{10,20}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid telephone number");
        }
    }
}
// checks that a value is present and that it is a valid email address
function ReqEmail(source, arguments) {
    Req(source, arguments);
    if (rightTrim(arguments.Value) == "") arguments.IsValid = false;
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid email address");
        }
    }
}
// checks that a value is present and that it is a valid postcode
function ReqPostcode(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^[a-zA-Z](\d\w{0,1}|[a-zA-Z]\d\w{0,1}) \d\w{2}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid postcode\ne.g: SW14 6NU");
        }
    }
}
// checks that if a value is present, that it is a valid date
function PossDate(source, arguments) {
    if (rightTrim(arguments.Value) != "") {
        arguments.IsValid = arguments.Value.match(/^(([0-2][0-9]|30)\/(04|06|09|11)|([0-2][0-9]|3[01])\/(01|03|05|07|08|10|12)|([0-2][0-9])\/02)\/[12]\d{3}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid date");
        } else {
            source.innerHTML = " ";
        }
    }
}
// checks that a value is present and that it is a valid time
function ReqTime(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        var matchArray = arguments.Value.match(/^(\d{1,2})(:)(\d{1,2})$/); // is the format ok?

        if (matchArray == null || matchArray[1] > 23 || matchArray[3] > 59) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid time");
            arguments.IsValid = false;
        } else {
            source.innerHTML = " ";
            arguments.IsValid = true;
        }

//        arguments.IsValid = arguments.Value.match(/^(([0|1][0-9])|([2][0-3])):([0-5][0-9])$/);
//        if (!arguments.IsValid) {
//            source.innerHTML = "?";
//            source.setAttribute("title", "This field requires a valid time");
//        } else {
//            source.innerHTML = " ";
//        }
    }
}
// combines home & mobile fields so that they can be validated together
function addToCombineField(e, input) {
    var keychar = GetKeyChar(e);
    if (keychar == ' ') return keychar;
    else if (keychar == '+' && input.value.length == 0) return keychar;
    var isDigit = keychar.match(/\d/) != null;
    if (isDigit) {
        var vals = document.getElementById("ctl00_MCPH_formView_peo_home_tel").value + document.getElementById("ctl00_MCPH_formView_peo_other_tel").value;
        var phoneNumbers = document.getElementById("ctl00_MCPH_formView_phoneNumbers");
        phoneNumbers.value = vals;
        if (phoneNumbers.value == "") phoneNumbers.value = keychar;
    }
    return isDigit;
}

// masks input to textbox and only allows date characters through, i.e. 0-9 & '/'
function maskDateInput(e) {
    var keychar = GetKeyChar(e);
    if (keychar == '/') return keychar;
    var isDigit = keychar.match(/\d/) != null;
    return isDigit;
}
// masks input to textbox and only allows integers through
function maskIntInput(e) {
    var keychar = GetKeyChar(e);
    var isDigit = keychar.match(/\d/) != null;
    return isDigit;
}
// masks input to textbox and only allows phone number characters through, i.e. 0-9, ' ' & '+'
function maskPhoneNumberInput(e, input) {
    var keychar = GetKeyChar(e);
    if (keychar == ' ') return keychar;
    else if (keychar == '+' && input.value.length == 0) return keychar;
    var isDigit = keychar.match(/\d/) != null;
    return isDigit;
}

// Gets the character pressed from the event argument
function GetKeyChar(e) {
    var keynum;
    // IE
    if (window.event) keynum = e.keyCode;
    // Netscape/Firefox/Opera
    else if (e.which) keynum = e.which;
    return String.fromCharCode(keynum);
}
