﻿// JScript File
window.blockConfirm = function(text, mozEvent, oWidth, oHeight, callerObj, oTitle) 
    { 
        var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually 
        //Cancel the event 
        ev.cancelBubble = true; 
        ev.returnValue = false; 
        if (ev.stopPropagation) ev.stopPropagation(); 
        if (ev.preventDefault) ev.preventDefault(); 
           
        //Determine who is the caller 
        var callerObj = ev.srcElement ? ev.srcElement : ev.target; 

        //Call the original radconfirm and pass it all necessary parameters 
        if (callerObj) 
        { 
            //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
            var callBackFn = function (arg) 
            { 
                if (arg) 
                { 
                    callerObj["onclick"] = ""; 
                    if (callerObj.click) callerObj.click(); //Works fine every time in IE, but does not work for links in Moz 
                    else if (callerObj.tagName == "A") //We assume it is a link button! 
                    { 
                        try 
                        { 
                            eval(callerObj.href) 
                        } 
                        catch(e){} 
                    } 
                } 
            } 
            radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle); 
        } 
        return false; 
    }
    
    var managerList = "";
    var webservice = '/DesktopModules/CooperateBizzoneJoinUp/JoinUp.asmx';
    
    function ValidateManagers(combo)
    {
        var enteredBusinessText = combo.get_text().trim();
        var emtyTextMessage = combo.get_emptyMessage();
        //alert(managerList);
        
        if (enteredBusinessText != "" && enteredBusinessText != emtyTextMessage)
        {
            var notificationToManagersMessage = "";
            if (managerList != "")
            {
                var numberOfManagers = managerList.length;
                if (numberOfManagers > 1) 
                {
                    notificationToManagersMessage = "<p>An email notification will be sent to the following managers for approval:</p>"
                    notificationToManagersMessage += "<ul>";
                    for( var manager in managerList)
                    {
                        notificationToManagersMessage += "<li>" + managerList[manager] + "</li>";
                    }
                    notificationToManagersMessage += "</ul>";
                }
                else
                {
                    notificationToManagersMessage = "<p>An email notification will be sent to " + managerList + " for approval.</p>";
                }
            }
            var message = notificationToManagersMessage + "<p>Are you sure you want to add " + enteredBusinessText + " to your business profile?</p>";
          
            // display message
            return blockConfirm(message, event, 450, null,'', "Connect to " + enteredBusinessText);
        }
        return true;
    }
    
    
    // Execute web service call
    function executeMethod(location, methodName, methodArguments, onSuccess, onFail) {
        $.ajax({
            type: "POST",
            url: location + "/" + methodName,
            data: methodArguments,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) { onSuccess(msg);} ,
            fail: onFail
        });
    }
    
    // Web service call to get manager lists
    function GetManagersByID(businessID)
    {
        managerList = "";
        var data = JSON.stringify({'businessID': businessID});
        executeMethod(webservice, "GetManagersByID", data, GetManagerOnSuccess,GetManagerOnFail);
    }
    
     // Web service call to get manager lists
    function GetManagersByName(businessName)
    {
        managerList = "";
        var data = JSON.stringify({'businessName': businessName});
        executeMethod(webservice, "GetManagersByName", data, GetManagerOnSuccess,GetManagerOnFail);
    }
    
    // on success
    function GetManagerOnSuccess(e)
    {
       managerList = e;
    }

    // failure
    function GetManagerOnFail(e)
    {
        //alert("Failed to get managers for the selected business. " + e);   
    }
    
    
    // rad combo selected index changed event handler
    function cboBusinessNames_OnClientSelectedIndexChangedEventHandler(item)
    {
        var businessID = item.get_value();
        if (businessID != "") GetManagersByID(businessID);
    }
    
    function cboBusinessNames_OnClientTextEventHandler(item)
    {
        // handles new business names entered.
        var enteredText = item.get_text();
        if (enteredText != "") GetManagersByName(enteredText);
    }
    
    

    
    function checkHideInfo(objIn, strDiv){
        var objDiv = document.getElementById(strDiv);
        if(objIn.value == ""){
            objDiv.style.display = 'block';
        }else{
            objDiv.style.display = 'none';
        }
    }
    function setFocus(objIn){
        if(objIn){
            objIn.focus();
        }
    }