﻿function ValidatePhoneNumber(txtName, strMessage, isFormValid) {
    if (isFormValid == false) {
        return false;
    } else {
        isFormValid = true;
        var txtPhoneNumber = FindFormControl(txtName);
        if (txtPhoneNumber != undefined) {
            var strPhone = txtPhoneNumber.value;
            var re = /[\(\)\+\-\s0-9]/g
            var strMatch;  // = new String;
            strMatch = strPhone.match(re);
            var strResult = (strMatch + "").replace(/,/g, "");
            if (strResult == strPhone) {
                isFormValid = true;
            } else {
                isFormValid = false;
            }
         }
    }
    if (isFormValid == false) {
        alert(strMessage);
    }
    return isFormValid;
}

function ValidateField(fieldId, type, defaultValue, validationAlert, isValidResult){
    if (isValidResult){
        var fieldObject = FindFormControl(fieldId);
        if (fieldObject != null){
            if (type == "text"){
                if (fieldObject.value == defaultValue){
                    alert(validationAlert);
                    fieldObject.focus();
                    return false;
                }
            }
            
            if (type == "numeric"){
                if (fieldObject.value == defaultValue){
                    if (validationAlert != ""){
                        alert(validationAlert);
                    }
                    fieldObject.focus();
                    return false;
                }else{
                    if (!parseFloat(fieldObject.value)){
                        alert("Please ensure that you have only used numeric values.");
                        fieldObject.focus();
                        return false;
                    }
                }
            }
            
            if (type == "drop"){
                if (fieldObject.options[fieldObject.selectedIndex].value == defaultValue){
                    if (validationAlert != ""){
                        alert(validationAlert);
                    }
                    fieldObject.focus();
                    return false;
                }
            }
            
            if (type == "check"){
                if (!fieldObject.checked){
                    if (validationAlert != ""){
                        alert(validationAlert);
                    }
                    return false;
                }
            }
            
            if (type == "time"){
                if (fieldObject.value == ""){
                    if (validationAlert != ""){
                        alert(validationAlert);
                    }
                    fieldObject.focus();
                    return false;
                }else{
                    var isTimeValid = ValidateTime(fieldObject.value);
                    if (!isTimeValid){
                        alert("Please ensure the time value is a valid time. (format = hh:mm)");
                        fieldObject.focus();
                        return false;
                    }
                }
            }
            
            
            if (type == "date"){
                if (fieldObject.value == ""){
                    if (validationAlert != ""){
                        alert(validationAlert);
                    }
                    fieldObject.focus();
                    return false;
                }else{
                    var isDateValid = ValidateDate(fieldObject.value);
                    if (!isDateValid){
                        fieldObject.focus();
                        return false;
                    }
                }
            }
        }
        
        return true;
    }
    
    return isValidResult;
}

function ValidateRadioListItem(objectid, alertMsg, isFormValid){
    if (isFormValid){
        var isItemSelected = false;
        var frm = document.forms[0];
        if (frm != undefined){
            for(var i = 0; i < frm.length; i++){
                e = frm.elements[i];
                if (e.id.indexOf(objectid) != -1){
                    
                    if (e.checked){
                        isItemSelected = true;
                        break;
                    }
                    
                }
            }
        }
        
        if (!isItemSelected){
            if (alertMsg != ""){
                alert(alertMsg);
            }
        }
        
        return isItemSelected;
    }
    
    return false;
}

function ValidateTime(timeValue){
    if (timeValue.indexOf(":") != -1){
        var timeValueSplit = timeValue.split(":");
        if (parseFloat(timeValueSplit[0]) && parseFloat(timeValueSplit[1])){
            var hoursValue = parseFloat(timeValueSplit[0]);
            var minutesValue = parseFloat(timeValueSplit[1]);
            if (hoursValue <= 23){
                if (minutesValue <= 59){
                    return true;
                }
            }
        }
    }
    
    return false;
}

function ValidateDate(dateValue){
    if (dateValue.indexOf("/") != -1){
        var dateSplit = dateValue.split("/");
        if (parseFloat(dateSplit[0]) && parseFloat(dateSplit[1]) && parseFloat(dateSplit[2])){
            var dayValue = parseFloat(dateSplit[0]);
            var monthValue = parseFloat(dateSplit[1]);
            var yearValue = parseFloat(dateSplit[2]);
            var currentDate = new Date();
            
            if (monthValue > 0 && monthValue < 13){
                if ((yearValue + "").length == 4){
                    var maxDays = 31;
                    var divYearValue = (yearValue / 4) + "";
                    if (divYearValue.indexOf(".") == -1){
                        //IS LEAP YEAR
                        if (monthValue == 2){
                            maxDays = 29;
                        }
                    }
                    
                    if (dayValue > 0 && dayValue <= maxDays){
                        return true;
                    }else{
                        alert("Please ensure the day value is valid.");
                    }
                }else{
                    alert("Please ensure the year value has 4 numeric digits in it.");
                }
            }else{
                alert("Please ensure the month value is greater than 0 and lower than 13.");
            }
        }
        
    }else{
        alert("Please ensure the date value is a valid date. (format = dd/mm/yyyy)");
    }
    
    return false;
}

function DeleteConfirm(){
    return confirm("Are you sure you would like to delete this item?");
}

function CancelConfirm(){
    return confirm("Are you sure you would like to clear content?");
}

function FindFormControl(controlId){
    var frm = document.forms[0];
    if (frm != undefined){
        for(var i = 0; i < frm.length; i++){
            e = frm.elements[i];
            if (e.id.indexOf(controlId) != -1){
                return e;
            }
        }
    }
    
    return null;
}

function remove(s, t) {
    /*
    **  Remove all occurrences of a token in a string
    **    s  string to be processed
    **    t  token to be removed
    **  returns new string
    */
    var i = s.indexOf(t);
    r = "";
    if (i == -1) return s;
    r += s.substring(0, i) + remove(s.substring(i + t.length), t);
    return r;
}