﻿/* Used to validate SalesQuantity when the user updates the grid */
function ValidateSalesQuantity(strCatalogueID, strOldValueID) {
    var objOldUnit = jQuery("#" + strOldValueID);
    var strUnitID = jQuery(".Col4").find("input[value^='" + strCatalogueID + "¤']").val().split('¤')[1];
    var objUnit = jQuery("#" + strUnitID);
    var intSalesQuantity = parseInt(jQuery("#" + jQuery(".Col3").find("input[value^='" + strCatalogueID + "¤']").val().split('¤')[1]).text());
    var intUnits = parseInt(objUnit.val());

    if (intUnits % intSalesQuantity != 0) {
        alert(GetError3(strCatalogueID, intSalesQuantity));
        objUnit.val(objOldUnit.val());
        setTimeout("document.getElementById('" + strUnitID + "').focus();", 50);
    } else {
        objOldUnit.val(intUnits);
    }
}

/* Validates the password, when the user wants to change. */
function ValidatePassword(field1, field2) {
    var txtPassword1 = document.getElementById(field1);
    var txtPassword2 = document.getElementById(field2);
    var passWord1 = txtPassword1.value;
    var passWord2 = txtPassword2.value;
    if (passWord1 != passWord2) {
        alert(strMsgPass1);
        return false;
    }
    if (passWord1.length < 6) {
        alert(strMsgPass2);
        return false;
    }
    return true;
}

/* Used when the user is adding a product that is already in the basket - to notify that something has happened after pressing the Add button. */
function FlashUnitBorderOnUpdate(strCatalogueID) {
    var objUnitInput = jQuery("#" + jQuery(".Col4").find("input[value^='" + strCatalogueID + "¤']").val().split('¤')[1]);
    var strOldBorderColor = objUnitInput.css("borderColor");
    var strOldBorderWidth = objUnitInput.css("borderWidth");
    objUnitInput.animate({ borderWidth: "10px" }, 300);
    objUnitInput.animate({ borderWidth: "1px" }, 300);
}

/* set focus on a specific element after pageload */
function SetFocus(id) {
    setTimeout("document.getElementById('" + id + "').focus();", 100)
    if (navigator.userAgent.indexOf('MSIE') != -1) {
        setTimeout("document.getElementById('" + id + "').select();", 100)
    }
}

/* When the user clicks on the open calender button */
function ShowDateViaButton(objButton) {
    var arrDotNetPathName = String(objButton.id).split("_btn")[0]
    var objSpan = document.getElementById(arrDotNetPathName + "_lbl" + "Delivery")
    var objInput = document.getElementById(arrDotNetPathName + "_txb" + "DeliveryDate")
    ShowDateText(objSpan, objInput);
}

/* Toggle the date textbox after the calender is selected */
function ToggleDateViaCalender(sender, args) {
    var arrDotNetPathName = String(sender._id).split("_ctlCal")[0]
    var objSpan = document.getElementById(arrDotNetPathName + "_lbl" + "Delivery")
    var objInput = document.getElementById(arrDotNetPathName + "_txb" + "DeliveryDate")
    ToggleDate(objSpan, objInput);
}
function ToggleDate(objSpan, objInput) {
    if (objInput.style.display == 'none') {
        ShowDateText(objSpan, objInput);
    } else {
        HideDateText(objSpan, objInput)
    }
}
function HideDateText(objSpan, objInput) {
    if (objInput.value != "") objSpan.innerHTML = objInput.value;
    objSpan.style.display = 'inline';
    objInput.style.display = 'none';
}
function ShowDateText(objSpan, objInput) {
    if (objSpan.innerHTML != "ASAP") objInput.value = objSpan.innerHTML;
    objSpan.style.display = 'none';
    objInput.style.display = 'block';
}

/* Check the date after it has been selected. Only future dates are valid. */
function CheckDate(sender, args) {
    //create a new date var and set it to the value of the senders selected date
    var selectedDate = new Date();
    selectedDate = sender._selectedDate;
    //create a date var and set it's value to today
    var todayDate = new Date();
    var mssge = "";

    if (selectedDate < todayDate) {
        //set the senders selected date to today
        //sender._selectedDate = todayDate;
        sender._selectedDate = '';
        //set the textbox assigned to the cal-ex to today
        //sender._textbox.set_Value(sender._selectedDate.format(sender._format));
        sender._textbox.set_Value('');
        //alert the user what we just did and why
        alert(strMSGAddError4);
    }
}

function ConfirmAdditionOfAllreadyAddedProduct(elementID){
    var txtAddItem = document.getElementById(elementID)
    if(txtAddItem.value){
        if (jQuery(".Col4").find("input[value^='" + txtAddItem.value + "¤']").length > 0)
        {
              return confirm(strMSGConfirmAdditionOfAllreadyAddedProduct)
        }
    }
    return true;
}
