﻿
// ***** Common functions specific to WebLink *********



function ProcessError(exType, errMsg, suppressMsg) {

    // Exception type may come with "+" in which case only care about part after it.
    // Example: "SoftwareUnlimited.Business.WebLink+InvalidDataError"
    var arrExType = exType.split("+");
    exType = (arrExType.length === 2) ? arrExType[1] : exType;

    switch (exType) {
        case "SiteLockedError":
            //window.location.href = "./Maintenance.aspx";
            window.location.href = "./Logout.aspx";
            break;
        case "InvalidCredentialError":
            //window.location.href = "./Login.aspx";
            var strLoginURL = "./Login.aspx";
            var strCustID = getCookie('CustID');
            if (strCustID.length > 0) { strLoginURL = strLoginURL + "?cust=" + strCustID; }
            window.location.href = strLoginURL;
            break;
        default:
            if (suppressMsg) { return; }
            alert(errMsg);
    }
    
}



// If find "ERROR^" at beginning then show error message that comes after it and return true. Otherwise return false because there is no error.
function onCheckForError(result, suppressMsg) {

    // If it's an error then param "result" should be in 3 parts
    // 1) "ERROR" to identify it as an error
    // 2) Exception Type
    // 3) Error Message to display as an alert
    // Example: "ERROR^InvalidCredentialError^Batch doesn't belong to this user."

    if (result === null) { return false; }

    var arr = result.split("^");
    if (arr[0].toUpperCase() !== "ERROR") { return false; } // No error so return false and let javascript continue.


    // Error was passed so process as needed...
    var exType = "";
    var errMsg = "";

    // If only length of 2 then assume exception type (middle param) was mistakenly excluded.
    if (arr.length === 2) {
        errMsg = arr[1];
    } else {
        exType = arr[1];
        errMsg = arr[2];
    }

    ProcessError(exType, errMsg, suppressMsg);

    return true;

}



function onSuccess(result, ctx, methodName) {
    // Use if never need a definite successful function.
    if (onCheckForError(result)) { return; }
}


// We found that all our server-sider errors do not go through to client because CustomErrors set to ON in web.config.
// This resulted in them only getting the generic message "There was an error processing the request." which is unhelpful.
// So had to create our own function below called onError where we handle errors manually. But leaving this onFail here and
// still referring to it with javascript AJAX calls in case of other unhandled errors, though will give a generic message.
function onFail(ex, ctx, methodName) {
    var exType = ex.get_exceptionType();
    var errMsg = ex.get_message();
    ProcessError(exType, errMsg);
}




// lbl is an optional label that can go along with the textbox
function ClearTextBox(txt, lbl) {
    txt.value = "";
    txt.title = ""; // Tooltip
    if (lbl) { lbl.innerHTML = ""; }
}

function ClearTextBoxRAD(txt) {
    txt.set_value('');
    txt._textBoxElement.title = "";
}

//function ClearComboBoxRAD(cb) {
//    cb.set_value('');
//    //txt._textBoxElement.title = "";
//}

//function uppercaseTextBox() {
//    key = window.event.keyCode;
//    if (key > 0x60 && key < 0x7B) {
//        window.event.keyCode = key - 0x20;
//    }
//}



//// For asp:textbox, if TextMode="MultiLine" then this is required to limit the length of the textbox.
//// Would use it like this on textbox: onkeyup="return checkMaxLen(this,1001)"
//// However, have just been using RadTextBox instead, so may never need this.
//function checkMaxLen(txt, maxLen) {
//    // "return false;" cancels any changes made to textbox.
//    // "window.event.keyCode = null;" changes the value being added to textbox to nothing (for example, to prevent + or - from getting entered)
//    try {
//        if (txt.value.length > (maxLen - 1)) {
//            return false;
//        };
//    } catch (e) {
//    }
//}


function GetControlExact(name) {
    // This returns a DOM object.
    return document.getElementById(name);
}

// Having a MasterPage creates the prefix below when controls are rendered in it.
function GetControl(name) {
    // This returns a DOM object.
    return document.getElementById("ctl00_ContentPlaceHolder1_" + name);
}

// Having a MasterPage creates the prefix below when controls are rendered in it.
function GetControlWLMonthDatePicker(name) {
    // Use $find since document.getElementById returns DOM object, which won't know special RAD methods.
    // Also, need to append "_RadDatePicker1" because that's the imbedded control needed in my custom control.
    return $find("ctl00_ContentPlaceHolder1_" + name + "_RadDatePicker1");
}

// Having a MasterPage creates the prefix below when controls are rendered in it.
function GetControlRAD(name) {
    // Use $find since document.getElementById returns DOM object, which won't know special RAD methods.
    return $find("ctl00_ContentPlaceHolder1_" + name);
}

function GetControlWLMonthDatePickerExact(name) {
    // Use $find since document.getElementById returns DOM object, which won't know special RAD methods.
    // Also, need to append "_RadDatePicker1" because that's the imbedded control needed in my custom control.
    return $find(name + "_RadDatePicker1");
}

function GetControlRADExact(name) {
    // Use $find since document.getElementById returns DOM object, which won't know special RAD methods.
    return $find(name);
}
