﻿function consumerOpen(id)
{
    ajaxWait("&nbsp;&nbsp;Loading...");
    currentApp = id;
    API.AppGet(id, gotApp);
}

function gotApp(r)
{
    ajaxWaitEnd();
    var editDiv = $get("editConsumer");
    var txtErrors=$get("txtErrors");
    $get("txtFeedback").style.display = "none";
    
    if(r)
    {
        txtErrors.innerHTML = "";
        txtErrors.style.display = "none";
        $get("txtAppName").value = r.ApplicationName;
        $get("txtAppDesc").value = r.ApplicationDescription;
        $get(drpCategoryID).selectedIndex = r.Category;
        $get("btnSave").onclick = function() {consumerSave(r.ConsumerID);}
        editDiv.style.display = "block";
        window.scroll(0,document.body.clientHeight);
    }
    else
    {
        consumerCancel();
        txtErrors.innerHTML = "<u>Failed to open application for editing</u><li>Press F5 and make sure you are logged in.</li>";
        txtErrors.style.display = "block";
        editDiv.style.display = "none";
    }
}

function consumerSave(consID)
{   
    if(checkApp("update"))
    {
        ajaxWait("&nbsp;&nbsp;Saving...");
        window.scroll(0,0);
        API.AppUpdate(consID, $get("txtAppName").value, $get("txtAppDesc").value, $get(drpCategoryID).selectedIndex, updatedApp);
    }
}

function updatedApp(r)
{
    ajaxWaitEnd();
    var editDiv = $get("editConsumer");
    var txtErrors=$get("txtErrors");
    $get("txtFeedback").style.display = "none";

    if(r)
    {
        //update the tablec cell to display the name app name
        $get("cellAppName"+currentApp).innerHTML = $get("txtAppName").value;
        //update the deleteImage onclick method so it reads the correct app name in the popup
        $get("cellImgDel"+currentApp).onclick = function() {consumerDelete(currentApp, $get("txtAppName").value);}
        //update the deleteImage alt text to read the correct app name
        $get("cellImgDel"+currentApp).alt = "delete " + $get("txtAppName").value;
        $get("cellImgDel"+currentApp).title = "delete " + $get("txtAppName").value;
        //update the editImage alt text to read the correct app name
        $get("cellImgEdit"+currentApp).alt = "edit " + $get("txtAppName").value;
        $get("cellImgEdit"+currentApp).title = "edit " + $get("txtAppName").value;

        var txtFeedback=$get("txtFeedback");
        txtFeedback.innerHTML = "Application details updated.";
        txtFeedback.style.display = "block";
    }
    else
    {
        txtErrors.innerHTML = "<u>Failed to save application</u><li>Press F5 and make sure you are logged in.</li>";
        txtErrors.style.display = "block";
        editDiv.style.display = "none";
    }
    consumerCancel();
}

function consumerCancel()
{
    var editDiv = $get("editConsumer");
    editDiv.style.display = "none";
}

function addApp()
{
    $get("txtFeedback").style.display = "none";
    if (checkApp("new"))
    {
        ajaxWait("&nbsp;&nbsp;Processing...");          
        $get("btnAddApp").disabled=true;
        API.AppAdd($get("txtAppName").value, $get("txtAppDesc").value, $get(drpCategoryID).value, addedApp);
    }
}

function addedApp(r)
{   
    ajaxWaitEnd();
    $get("btnAddApp").disabled=false;
    $get("txtAppName").value="";
    $get("txtAppDesc").value="";
    $get(drpCategoryID).selectedIndex=0;
    if(r)
    {
        var txtFeedback = $get("txtFeedback");
        txtFeedback.innerHTML = "<u>Application portal created</u><li>To view your application(s) go to the <a href='app_my.aspx'>my apps</a> page.</li>";
        txtFeedback.style.display = "block";
    }
    else
    {   var txtErrors=$get("txtErrors");
        txtErrors.innerHTML = "<u>Failed to create application</u><li>Press F5 and make sure you are logged in.</li>";
        txtErrors.style.display = "block";
    }
}

function checkApp(type)
{
    var progress=true;
    var txtErrors=$get("txtErrors");
    var txtAppName=$get("txtAppName");
    var txtAppDesc=$get("txtAppDesc");
    var chkAppName=$get("chkAppName");
    var chkAppDesc=$get("chkAppDesc");
    
    txtErrors.innerHTML = "";
    
    // check app name
    if(txtAppName.value.length > 2) {
         chkAppName.innerHTML = "";
    }
    else {
        progress = false;
        chkAppName.innerHTML = "*";
        txtErrors.innerHTML += "<li>Name too short</li>";
    }
    
    // check app desc
    if(txtAppDesc.value.length > 30) {
         chkAppDesc.value = "";
    }
    else {
        progress = false;
        chkAppDesc.innerHTML = "*";
        txtErrors.innerHTML += "<li>Description too short</li>";
    }
    
    if (type=="new") // we only need to agree to terms on an update
    {
        var chkTerms=$get("chkTerms");
        var chkchkTerms=$get("chkchkTerms");
    
        // check agreed to terms
        if(chkTerms.checked) {
             chkchkTerms.innerHTML = "";
        }
        else {
            progress = false;
            chkchkTerms.innerHTML = "*";
            txtErrors.innerHTML += "<li>Must agree to terms</li>";
        }
    }
    
    // show main error block
    if(!progress) {
        txtErrors.innerHTML = "<u>Error(s)</u>" + txtErrors.innerHTML + "";
        txtErrors.style.display = "block";
    }
    else { txtErrors.style.display = "none"; }
    
    return progress;
}

function ajaxWait(msg)
{
    $get("ajaxLabel").innerHTML = msg;
    $get("ajaxProg").style.display = "block";
}

function ajaxWaitEnd()
{
    $get("ajaxLabel").innerHTML = "";
    $get("ajaxProg").style.display = "none";
}