var AlertWONo = 'Please enter the WO No.';
var AlertStartDate = 'Please enter the Start Date.';
var AlertEDC = 'Please enter the Expected Closed Date.';
var AlertWarrantyDate = 'Please enter the Warranty End Date.';
var AlertRemarks = 'Please enter the Remarks.';

var AlertServiceType = 'Please select a Service Type.';
var AlertWanrantyType = 'Please select a Warranty.';
var AlertVendor = 'Please select a Vendor.';
var AlertAssignedTo = 'Please select a User.';
var AlertGreaterThanFromDate = "Please select start date less than EDC.";

function ValidateWOInfo(CtrlID) {
    var txtWONo = document.getElementById('txtWONo');
    var txtStartDate = document.getElementById('txtdate1');
    var txtEDC = document.getElementById('textdate2');
    var txtWarrantyEndDate = document.getElementById('textdate3');
    var txtRemarks = document.getElementById('txtRemarks');
    var ddlServiceType = document.getElementById('ddlServiceType');
    var ddlwaranty = document.getElementById('ddlwaranty');
    var ddlVendorName = document.getElementById('ddlVendorName');
    var ddlPerson = document.getElementById('ddlPerson');

    var errorMessage = '';
    var isError = false;
    if (RemoveSpaces(txtWONo).length == 0) {
        isError = true;
        errorMessage = AlertWONo;
        txtWONo.focus();
        txtWONo.style.backgroundColor = 'lightyellow';
    }
    else {
        txtWONo.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtStartDate).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertStartDate;
        if (!isError) {
            txtStartDate.focus();
            isError = true;
        }
        txtStartDate.style.backgroundColor = 'lightyellow';
    }
    else {
        txtStartDate.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtEDC).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertEDC;
        if (!isError) {
            txtEDC.focus();
            isError = true;
        }
        txtEDC.style.backgroundColor = 'lightyellow';
    }
    else {
        txtEDC.style.backgroundColor = 'white';
    }   
    if (RemoveSpaces(txtWarrantyEndDate).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertWarrantyDate;
        if (!isError) {
            txtWarrantyEndDate.focus();
            isError = true;
        }
        txtWarrantyEndDate.style.backgroundColor = 'lightyellow';
    }
    else {
        txtWarrantyEndDate.style.backgroundColor = 'white';
    }   
    if (RemoveSpaces(txtRemarks).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertRemarks;
        if (!isError) {
            txtRemarks.focus();
            isError = true;
        }
        txtRemarks.style.backgroundColor = 'lightyellow';
    }
    else {
        txtRemarks.style.backgroundColor = 'white';
    }    
    if (!CheckIsSelectedStatic(ddlServiceType.value)) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertServiceType;
        if (!isError) {
            controlToBeFocused = ddlServiceType;
            isError = true;
        }
        ddlServiceType.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlServiceType.style.backgroundColor = 'white';
    }
    if (!CheckIsSelectedStatic(ddlwaranty.value)) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertWanrantyType;
        if (!isError) {
            controlToBeFocused = ddlwaranty;
            isError = true;
        }
        ddlwaranty.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlwaranty.style.backgroundColor = 'white';
    }
    if (!CheckIsSelected(ddlVendorName.value)) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertVendor;
        if (!isError) {
            controlToBeFocused = ddlVendorName;
            isError = true;
        }
        ddlVendorName.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlVendorName.style.backgroundColor = 'white';
    }  
    if (!CheckIsSelected(ddlPerson.value)) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertAssignedTo;
        if (!isError) {
            controlToBeFocused = ddlPerson;
            isError = true;
        }
        ddlPerson.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlPerson.style.backgroundColor = 'white';
    }
    if (RemoveSpaces(txtStartDate).length != 0 && RemoveSpaces(txtEDC).length != 0) {
        var FromdateStr = new Array(3);
        FromdateStr = String(document.getElementById("txtdate1").value).split('-');
        var TodateStr = new Array(3);
        TodateStr = String(document.getElementById("textdate2").value).split('-');
        var day = FromdateStr[0];
        var month = FromdateStr[1];
        var year = FromdateStr[2];
        var day1 = TodateStr[0];
        var month1 = TodateStr[1];
        var year1 = TodateStr[2];
        var fromDate = new Date(month + "/" + day + "/" + year);
        var ToDate = new Date(month1 + "/" + day1 + "/" + year1);       
        if (fromDate > ToDate) {
            errorMessage = (isError ? errorMessage + '<br>' : '') + AlertGreaterThanFromDate;
            if (!isError) {
                txtStartDate.focus();
                isError = true;
            }
            txtStartDate.style.backgroundColor = 'lightyellow';
        }
        else {
            txtStartDate.style.backgroundColor = 'white';
        }
    }
    if (isError) {
        TrinetraCustomAlerts(errorMessage);
        return false;
    }
    else {       
        return TrinetraConfirm(CtrlID, 'Do you want to proceed?');
    }
}
function ValidateWOApprovalInfo(CtrlID) {
    
    var txtRemarks = document.getElementById('txtRemarks');    
    var ddlPerson = document.getElementById('ddlPerson');

    var errorMessage = '';
    var isError = false;   
    if (RemoveSpaces(txtRemarks).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertRemarks;
        if (!isError) {
            txtRemarks.focus();
            isError = true;
        }
        txtRemarks.style.backgroundColor = 'lightyellow';
    }
    else {
        txtRemarks.style.backgroundColor = 'white';
    }   
    if (!CheckIsSelected(ddlPerson.value)) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertAssignedTo;
        if (!isError) {
            controlToBeFocused = ddlPerson;
            isError = true;
        }
        ddlPerson.style.backgroundColor = 'lightyellow';
    }
    else {
        ddlPerson.style.backgroundColor = 'white';
    }

    if (isError) {
        TrinetraCustomAlerts(errorMessage);
        return false;
    }
    else
        return TrinetraConfirm(CtrlID, 'Do you want to proceed?');
}
function ValidateWOApprovalInfoRedo(CtrlID) {

    var txtRemarks = document.getElementById('txtRemarks');
    var ddlPerson = document.getElementById('ddlPerson');

    var errorMessage = '';
    var isError = false;
    ddlPerson.style.backgroundColor = 'white';
    if (RemoveSpaces(txtRemarks).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertRemarks;
        if (!isError) {
            txtRemarks.focus();
            isError = true;
        }
        txtRemarks.style.backgroundColor = 'lightyellow';
    }
    else {
        txtRemarks.style.backgroundColor = 'white';
    }   

    if (isError) {
        TrinetraCustomAlerts(errorMessage);
        return false;
    }
    else
        return TrinetraConfirm(CtrlID, 'Do you want to proceed?');
}
function ValidateWOCloseInfo(CtrlID) {

    var txtRemarks = document.getElementById('txtRemarks');    

    var errorMessage = '';
    var isError = false;
    if (RemoveSpaces(txtRemarks).length == 0) {
        errorMessage = (isError ? errorMessage + '<br>' : '') + AlertRemarks;
        if (!isError) {
            txtRemarks.focus();
            isError = true;
        }
        txtRemarks.style.backgroundColor = 'lightyellow';
    }
    else {
        txtRemarks.style.backgroundColor = 'white';
    }   

    if (isError) {
        TrinetraCustomAlerts(errorMessage);
        return false;
    }
    else
        return TrinetraConfirm(CtrlID, 'Do you want to proceed?');
}
function CheckIsSelected(dropdownvalue) {
    return dropdownvalue != '-Select-' ? true : false;
}
function CheckIsSelectedStatic(dropdownvalue) {
    return dropdownvalue != '0' ? true : false;
}
var Alert_Delete = "Do you want to delete this WO?.";

function setAlertMessageDelete(Delete) {
    Alert_Delete = Delete;
}
function DeleteConfirm() {
    //    var Alert_Delete = "Do you want to really delete this driver?.";
    return TrinetraConfirm(UniqueID, Alert_Delete);
}
var AlertValidUpload = 'Please upload only PDF files.';
function LoadPDFFile(id) {
    if (validateFileUpload(id)) {
        document.getElementById('hdnWOQuotationFile').value = '';
        document.getElementById('hdnWOQuotationFile').value = document.getElementById('fpWOQuotation').value;        
    }

}
function validateFileUpload(obj) {
    var fileName = new String();
    var fileExtension = new String();
    var validFile = ".pdf";
    var validFileExtensions = validFile.split(',');
    var flag = false;
    // store the file name into the variable
    fileName = document.getElementById(obj).value;
    // extract and store the file extension into another variable
    fileExtension = fileName.substr(fileName.lastIndexOf('.'), fileName.length);
    // Check over the valid file extensions to compare them with uploaded file
    for (var i = 0; i < validFileExtensions.length; i++) {
        if (fileExtension.toLowerCase() == validFileExtensions[i].toString().toLowerCase()) {
            flag = true;
        }
    }
    // display the alert message box according to the flag value
    if (flag == false) {
        TrinetraCustomAlerts(AlertValidUpload);
        var file = document.getElementById(obj);
        file.value = "";
        var file1 = file.cloneNode(false);
        file1.onchange = file.onchange;
        file.parentNode.replaceChild(file1, file);
        return false;
    }
    else {
        return true;
    }
}