var AlertStudentID = 'Please enter the StudentID.';
var AlertStudentFirstName = 'Please enter the Student First Name.';
var AlertSchoolCode = 'Please enter the School Name.';
var AlertGuardianFirstName = 'Please enter the Guardian First Name.';
var AlertRelationship = 'Please enter the Relationship.';
var AlertStationID = 'Please select a Fleet.';
var AlertStationName = 'Please select a station name.';
var AlertBusInternalNo = 'Please select a Bus Internal No.';
var AlertLandmarkname = 'Please enter the Landmark name.';
var AlertFamilyName = 'Please enter the Family name.';
var AlertPOBox = 'Please enter the PO Box No.';
var AlertAddress = 'Please enter the Address.';
var AlertCity = 'Please enter the City.';
var AlertCountry = 'Please select a Country.';
var AlertSchoolID = 'Please enter the School Code.';
var AlertEmailID = 'Please enter the Valid email address.';
var AlertGrade = 'Please select a Grade.';
var AlertEmirates = 'Please select a Emirate.';
var AlertMobileNo = 'Please enter the Mobile No.';
var AlertGeozone = 'Please select a Geozone.';
var AlertWaitingTime = 'Please select a Waiting Time.';
var AlertReportingPerson = 'Please select a Reporting Person';

function SetAlertMessageSchool(AlertStudentIDAlert, AlertStudentFirstNameAlert, AlertSchoolCodeAlert, AlertGuardianFirstNameAlert, AlertRelationshipAlert, AlertStationIDAlert, AlertStationNameAlert, AlertBusInternalNoAlert, AlertLandmarknameAlert,
AlertFamilyNameAlert, AlertPOBoxAlert, AlertAddressAlert, AlertCityAlert, AlertCountryAlert, AlertGradeAlert, AlertEmiratesAlert) {
    AlertStudentID = AlertStudentIDAlert
    AlertStudentFirstName = AlertStudentFirstNameAlert
    AlertSchoolCode = AlertSchoolCodeAlert
    AlertGuardianFirstName = AlertGuardianFirstNameAlert
    AlertRelationship = AlertRelationshipAlert
    AlertStationID = AlertStationIDAlert
    AlertStationName = AlertStationNameAlert
    AlertBusInternalNo = AlertBusInternalNoAlert
    AlertLandmarkname = AlertLandmarknameAlert
    AlertFamilyName = AlertFamilyNameAlert
    AlertPOBox = AlertPOBoxAlert
    AlertAddress = AlertAddressAlert
    AlertCity = AlertCityAlert
    AlertCountry = AlertCountryAlert
    AlertGrade = AlertGradeAlert
    AlertEmirates = AlertEmiratesAlert
}

function ValidateStudentInfo() {
    var txtStudentID = document.getElementById('txtStudentID');
    var txtStudentFirstName = document.getElementById('txtStudentFirstName');
    var ddlSchoolCode = document.getElementById('ddlSchoolCode');
    var txtRFID = document.getElementById('txtRFID');
    var txtEmailAddress = document.getElementById('txtEmailAddress');
    var ddlStationID = document.getElementById('ddlStationID');
    var txtStudentLastname = document.getElementById('txtStudentLastname');

    var txtAddress1 = document.getElementById('txtAddress1');
    var txtCity = document.getElementById('txtCity');
    var txtMobileNo = document.getElementById('txtMobile');

    var errorMessage = '';
    var isError = false;
    if (RemoveSpaces(txtStudentID).length == 0) {
        isError = true;
        errorMessage = AlertStudentID;
        txtStudentID.focus();
        txtStudentID.style.backgroundColor = 'lightyellow';
    }
    else {
        txtStudentID.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtStudentFirstName).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertStudentFirstName;
        if (!isError) {
            txtStudentFirstName.focus();
            isError = true;
        }
        txtStudentFirstName.style.backgroundColor = 'lightyellow';
    }
    else {
        txtStudentFirstName.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtStudentLastname).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter the Student Last Name.';
        if (!isError) {
            txtStudentLastname.focus();
            isError = true;
        }
        txtStudentLastname.style.backgroundColor = 'lightyellow';
    }
    else {
        txtStudentLastname.style.backgroundColor = 'white';
    }   
     if (RemoveSpaces(txtAddress1).length != 0) {        
        if (txtAddress1.value.length > 200) {
            errorMessage = (isError ? errorMessage + '\n' : '') + 'Address1 exceeds 200 characters';
            if (!isError) {
                txtAddress1.focus();
                isError = true;
            }
            txtAddress1.style.backgroundColor = 'lightyellow';
        }
        else {
            txtAddress1.style.backgroundColor = 'white';
        }
    }
    else {
        txtAddress1.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtAddress1).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter the address';
        if (!isError) {
            txtAddress1.focus();
            isError = true;
        }
        txtAddress1.style.backgroundColor = 'lightyellow';
    }
    else {
        txtAddress1.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtAddress2).length != 0) {
        if (txtAddress2.value.length > 200) {
            errorMessage = (isError ? errorMessage + '\n' : '') + 'Address2 exceeds 200 characters';
            if (!isError) {
                txtAddress2.focus();
                isError = true;
            }
            txtAddress2.style.backgroundColor = 'lightyellow';
        }
        else {
            txtAddress2.style.backgroundColor = 'white';
        }
    }
    else {
        txtAddress2.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtCity).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter the City.';
        if (!isError) {
            txtCity.focus();
            isError = true;
        }
        txtCity.style.backgroundColor = 'lightyellow';
    }
    else {
        txtCity.style.backgroundColor = 'white';
    }
//    if (mobileFormat(txtMobileNo.value) == false) {
//        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter a valid Mobile Number.';
//        txtMobileNo.style.backgroundColor = 'lightyellow';

//        if (!isError) {
//            controlToBeFocused = txtMobileNo;
//            isError = true;
//        }
//    }
//    else {
//        txtMobileNo.style.backgroundColor = 'white';
    //    }
    if (RemoveSpaces(txtMobileNo).length < 10) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter a valid Mobile Number.';
        if (!isError) {
            txtMobileNo.focus();
            isError = true;
        }
        txtMobileNo.style.backgroundColor = 'lightyellow';
    }
    else {
        txtMobileNo.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtEmailAddress).length > 0) {
        var email = txtEmailAddress;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
            errorMessage = (isError ? errorMessage + '\n' : '') + AlertEmailID;
            if (!isError) {
                txtEmailAddress.focus();
                isError = true;
            }
            txtEmailAddress.style.backgroundColor = 'lightyellow';
        }
        else {
            txtEmailAddress.style.backgroundColor = 'white';
        }


    }
    if (!CheckIsSelected(ddlSchoolCode.value)) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertSchoolCode;
        if (!isError) {
            controlToBeFocused = ddlSchoolCode;
            isError = true;
        }
        ddlSchoolCode.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlSchoolCode.style.backgroundColor = 'white';
    }

    if (RemoveSpaces(txtRFID).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter a RFID.';
        if (!isError) {
            txtRFID.focus();
            isError = true;
        }
        txtRFID.style.backgroundColor = 'lightyellow';
    }
    else {
        txtRFID.style.backgroundColor = 'white';
    }
    if (!CheckIsSelected(ddlStationID.value)) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please select a Fleet.';
        if (!isError) {
            controlToBeFocused = ddlStationID;
            isError = true;
        }
        ddlStationID.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlStationID.style.backgroundColor = 'white';
    }

    if (isError) {
        alert(errorMessage);
        return false;
    }
    else
        return true;
}


function setAlertMessageN(AlertSchoolIDAlert, AlertSchoolCodeAlert, AlertStationIDAlert, AlertStationNameAlert, AlertPOBoxAlert, AlertAddressAlert, AlertCityAlert, AlertCountryAlert, AlertReportingPersonAlert) {
    AlertSchoolID = AlertSchoolIDAlert
    AlertSchoolCode = AlertSchoolCodeAlert
    AlertStationID = AlertStationIDAlert
    AlertStationName = AlertStationNameAlert
    AlertPOBox = AlertPOBoxAlert
    AlertAddress = AlertAddressAlert
    AlertCity = AlertCityAlert
    AlertCountry = AlertCountryAlert
    AlertReportingPerson = AlertReportingPersonAlert
}
function ValidateSchoolInfo() {
    var txtSchoolCode = document.getElementById('txtSchoolCode');
    //var ddlStationID = document.getElementById('ddlStationID');
    var ddlStationName = document.getElementById('ddlStationName');
    var txtAddress1 = document.getElementById('txtAddress1');
    var txtPOBox = document.getElementById('txtPOBox');
    var txtCity = document.getElementById('txtCity');
    var txtCountry = document.getElementById('ddlCountry');
    var txtCity = document.getElementById('txtCity');
    var txtMobileNo = document.getElementById('txtMobileNo');
    var ddlGeozone = document.getElementById('ddlGeozone');
    var ddlWaitingTime = document.getElementById('ddlWaitingTime');

    var errorMessage = '';
    var isError = false;

    if (RemoveSpaces(txtSchoolCode).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertSchoolCode;
        if (!isError) {
            txtSchoolCode.focus();
            isError = true;
        }
        txtSchoolCode.style.backgroundColor = 'lightyellow';
    }
    else {
        txtSchoolCode.style.backgroundColor = 'white';
    }

    //    if (!CheckIsSelected(ddlStationID.value) || ddlStationID.value == null) {
    //        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please select a Fleet.';
    //        if (!isError) {
    //            controlToBeFocused = ddlStationID;
    //            isError = true;
    //        }
    //        ddlStationID.style.backgroundColor = 'lightyellow';
    //    }
    //    else {
    //        ddlStationID.style.backgroundColor = 'white';
    //    }

  
    if (RemoveSpaces(txtAddress1).length != 0) {        
        if (txtAddress1.value.length > 200) {
            errorMessage = (isError ? errorMessage + '\n' : '') + 'Address1 exceeds 200 characters';
            if (!isError) {
                txtAddress1.focus();
                isError = true;
            }
            txtAddress1.style.backgroundColor = 'lightyellow';
        }
        else {
            txtAddress1.style.backgroundColor = 'white';
        }
    }
    else {
        txtAddress1.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtAddress1).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter the address';
        if (!isError) {
            txtAddress1.focus();
            isError = true;
        }
        txtAddress1.style.backgroundColor = 'lightyellow';
    }
    else {
        txtAddress1.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtAddress2).length != 0) {
        if (txtAddress2.value.length > 200) {
            errorMessage = (isError ? errorMessage + '\n' : '') + 'Address2 exceeds 200 characters';
            if (!isError) {
                txtAddress2.focus();
                isError = true;
            }
            txtAddress2.style.backgroundColor = 'lightyellow';
        }
        else {
            txtAddress2.style.backgroundColor = 'white';
        }
    }
    else {
        txtAddress2.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtCity).length == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertCity;
        if (!isError) {
            txtCity.focus();
            isError = true;
        }
        txtCity.style.backgroundColor = 'lightyellow';
    }
    else {
        txtCity.style.backgroundColor = 'white';
    }
    if (!CheckIsSelected(ddlGeozone.value)) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertGeozone;
        if (!isError) {
            controlToBeFocused = ddlGeozone;
            isError = true;
        }
        ddlGeozone.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlGeozone.style.backgroundColor = 'white';
    }
    //if (!CheckIsSelected(ddlWaitingTime.value)) {
    //    errorMessage = (isError ? errorMessage + '\n' : '') + AlertWaitingTime;
    //    if (!isError) {
    //        controlToBeFocused = ddlWaitingTime;
    //        isError = true;
    //    }
    //    ddlWaitingTime.style.backgroundColor = 'lightyellow';
    //}
    //else {
    //    ddlWaitingTime.style.backgroundColor = 'white';
    //}

    //    if (RemoveSpaces(txtPOBox).length == 0) {
    //        errorMessage = (isError ? errorMessage + '\n' : '') + AlertPOBox;
    //        if (!isError) {
    //            txtPOBox.focus();
    //            isError = true;
    //        }
    //        txtPOBox.style.backgroundColor = 'lightyellow';
    //    }
    //    else {
    //        txtPOBox.style.backgroundColor = 'white';
    //    }

    if (!CheckIsSelected(ddlCountry.value)) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertCountry;
        if (!isError) {
            controlToBeFocused = ddlCountry;
            isError = true;
        }
        //ddlCountry.style.backgroundColor = 'lightyellow';        
    }
    else {
        //ddlCountry.style.backgroundColor = 'white';
    }


    //    if (mobileFormat(txtMobileNo.value) == false) {
    //        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter a valid Mobile Number.';
    //        txtMobileNo.style.backgroundColor = 'lightyellow';

    //        if (!isError) {
    //            controlToBeFocused = txtMobileNo;
    //            isError = true;
    //        }
    //    }
    //    else {
    //        txtMobileNo.style.backgroundColor = 'white';
    //    }
   
    if (RemoveSpaces(txtMobileNo).length < 10) {
        errorMessage = (isError ? errorMessage + '\n' : '') + 'Please enter a valid Mobile Number.';
        if (!isError) {
            txtMobileNo.focus();
            isError = true;
        }
        txtMobileNo.style.backgroundColor = 'lightyellow';
    }
    else {
        txtMobileNo.style.backgroundColor = 'white';
    }
    if (ddlReportingPerson.GetSelectedIndex() == 0) {
        errorMessage = (isError ? errorMessage + '\n' : '') + AlertReportingPerson;
        if (!isError) {
            controlToBeFocused = ddlReportingPerson;
            isError = true;
        }
    }

    if (isError) {
        alert(errorMessage);
        return false;
    }
    else {
        ShowPopUp();
        return true;
    }
}
//format is +91-9047790266 or 9047790266    
function mobileFormat(value) {
    var mobileReg = /^((\d{2}))?(\d{10}){1}?$/;
    if ((mobileReg.test(value) == false) || (value.length < 12)) {
        return false;
    }
    else {
        return true;
    }
}
function CheckIsSelected(dropdownvalue) {
    return dropdownvalue != '- Select -' ? true : false;
}

// Function to restrict the status change for 'Administrator' and the logged in user
function AllowActiveStatus(Type, IsActiveStatus, allowChange) {
    if (allowChange == 1) {
        if (Type == 'U') {
            (IsActiveStatus == 1) ? alert(cannotChangeAdminStatus) :
                    alert(cannotChangeAdminStatus);
        }
        return false;
    }
    else {
        return true;
    }
}
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}
function floatValidation(e, control) {
    var Long = '';
    var hdnArray = new Array();
    var charCode = (e.which) ? e.which : event.keyCode
    if (charCode == 46) {
        var patt1 = new RegExp("\\.");
        var ch = patt1.exec(control.value);
        if (ch == ".") {
            return false;
            //charCode = 0;
        }
    }
    else if ((charCode >= 48 && charCode <= 57) || charCode == 8)//Numbers or BackSpace
    {
        hdnArray = control.value;
        if (control.id == "txtLongtitude") {
            if (charCode != 53 && hdnArray == "") {
                //charCode = 0;
                return false;
            }
            else if (hdnArray.length < 3) {
                if (charCode >= 50 && charCode <= 54) {
                    return true;
                }
                else {
                    if (hdnArray.length < 3) {
                        //charCode = 0;
                        //return false;
                    }

                }
            }
            else {
                if (hdnArray.length < 3) {
                    //charCode = 0;
                    return false;
                }
            }
        }
        else {
            if (charCode != 50 && hdnArray == "") {
                //charCode = 0;
                return false;
            }
            else if (hdnArray.length < 3) {
                if (charCode >= 50 && charCode <= 53) {
                    return true;
                }
                else {
                    if (hdnArray.length < 3) {
                        //charCode = 0;
                        //return false;
                    }

                }
            }
            else {
                if (hdnArray.length < 3) {
                    //charCode = 0;
                    // return false;
                }
            }
        }

        if (control.value.indexOf('.') != -1)//. Exisist in TextBox 
        {
            var pointIndex = control.value.indexOf('.');
            var beforePoint = control.value.substring(0, pointIndex);
            var afterPoint = control.value.substring(pointIndex + 1);
            var iCaretPos = 0;
            if (document.selection) {
                if (control.type == 'text') // textbox
                {
                    var selectionRange = document.selection.createRange();
                    selectionRange.moveStart('character', -control.value.length);
                    iCaretPos = selectionRange.text.length;
                }
            }
            if (iCaretPos > pointIndex && afterPoint.length >= 10) {
                //charCode = 0;
                return false;
            }
            else if (iCaretPos <= pointIndex && beforePoint.length >= 15) {
                //charCode = 0;
                return false;
            }
        }
        else//. Not Exisist in TextBox
        {
            if (control.value.length >= 15) {
                //charCode = 0;
                return false;
            }
        }

    }
    else {
        //charCode = 0;
        return false;
    }
}
function chkCharacter_only_School(e) {
    if (!e) e = window.event;
    var c = e.keyCode ? e.keyCode : e.which;  //event.keyCode;
    if (e.charCode != null) {
        if (e.charCode == 0) {
            return true;
        }
    }
    if (e.shiftKey && c == 37) return false;
    if (((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 45) || (c == 32)) || c == 8 || c == 9 || c == 13 || c == 0 || c > 60000 || c == 127 || c == 37 || c == 39)
    { return true; }
    else { return false; }
}