﻿function getHandleOnControlID(controltypename, controlname)
{
    var foundElement = null;
    // server control full name or client control name
    foundElement = document.getElementById(controlname);    
    // server control partial name
    if (foundElement == null)
    {
        elems = document.getElementsByTagName(controltypename);
        for (i=0; i<elems.length; i++)
        {
            myElement = elems[i];
            if ((myElement.id != null) && (myElement.id.toUpperCase().indexOf("_" + controlname.toUpperCase()) > -1))
            {
                foundElement = myElement;
                break;
            }
        }
    }
    return foundElement;
}

// Disable buttons on submit to avoid multiple clicks especially for money transactions
function DisableThis(c)
{
    //alert(c);
    //c.disabled=true; // disabling the control won't allow it to postback; use hide instead.
    c.style.visibility="hidden"; // visibility 'hidden' hides the control but preserves the space it occupies. use visibility 'none' if you want the space it occupies hidden as well.
    return true;
}

function ConfirmDelete()
{
    return confirm("Are you sure you want to delete?");
}

function ConfirmContractClose()
{
    return confirm("This will permanently close this contract and it will no longer be serviced, however you may still view it. \n\nAre you sure you want to close this contract?");
}

function ConfirmContractArchive()
{
    return confirm("This will move this contract from your portfolio to your personal archive. \n\nAre you sure you want to archive this contract?");
}

function ConfirmContractDelete()
{
    return confirm("This will permanently delete this contract from your portfolio and you will not be able to view it anymore. \n\nAre you sure you want to delete this contract?");
}

function ConfirmSubscriptionCancel()
{
    return confirm("You are about to cancel your subscription! \n\nAny contracts created under that subscription will revert to current standard pricing. \n\nAre you sure you want to cancel the subscription?");
}

function popup_terms_and_conditions()
{
    window.open("../TermsAndConditions.aspx", "myTC", "menubar=no, scrollbars=yes, resizable=yes, width=600");
}

// do not allow delete anymore. use AlertAccountTiedToContract() instead
function ConfirmDeleteAccountTiedToContract()
{
    return confirm("Account is tied to a contract. Are you sure you want to delete?");
}

function AlertAccountTiedToContract()
{
    alert("Cannot delete this account. It is being used on a contract or a subscription.");
    // return false to stop action
    return false;
}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}

/* Textbox multiline (textarea) maxlength support */
/*  Usage: <textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea> */
function ismaxlength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}




/* used to control check boxes on BillPay control */
function setChecked(selected)
{
    var checkIt = true;
    var i = 0;
    var iSelected = -1;
    var testingControl = document.getElementById( "payment" + i);
    var prinPaymentCB = document.getElementById("principalPaymentCB");
    var prinPaymentTB = document.getElementById("principalPaymentTB");
    var paymentTotal = 0.0;

    if (prinPaymentTB != null)
    {
        prinPaymentTB.value = prinPaymentTB.value.replace("$", "");
        prinPaymentTB.value = prinPaymentTB.value.replace(" ", "");
        prinPaymentTB.value = prinPaymentTB.value.replace(",", "");    
    }

    while ( testingControl != null) 
    {
        if ( testingControl.name == selected.name ) 
            checkIt = false;
        else if ( "principalPaymentCB" != selected.name )
            testingControl.checked = checkIt;
        if ( testingControl.checked) 
        {
            paymentTotal += parseFloat(testingControl.value);
            iSelected = i;
        }
            
        i++;
        testingControl = document.getElementById( "payment" + i);
    }    
    iLastCB = --i;
    testingControl = document.getElementById("payment" + iLastCB);
    
    // check principal payment    
    if ((prinPaymentCB != null) && (prinPaymentCB.checked))
    {
        if (!isNaN(prinPaymentTB.value) && (prinPaymentTB.value != ""))
        {
            paymentTotal += parseFloat(prinPaymentTB.value);
        }
    }

    // validate if required due amounts where all selected    
    if (iLastCB > 0)
    { // there are over due amounts
        if (iSelected < (iLastCB - 1))
        { // not all over due amounts were selected
            
            if (testingControl.checked)
                paymentTotal -= parseFloat(testingControl.value);            
            if ((prinPaymentCB != null) && (prinPaymentCB.checked))
            {
                if (!isNaN(prinPaymentTB.value) && (prinPaymentTB.value != ""))
                {
                    paymentTotal -= parseFloat(prinPaymentTB.value);
                }
            }
            
            testingControl.checked = false;
            if (prinPaymentCB!=null)
                prinPaymentCB.checked = false;
            if (prinPaymentTB!=null)
                prinPaymentTB.value = "";
                        
            testingControl.disabled = true;
            if (prinPaymentCB!=null)
                prinPaymentCB.disabled = true;
            if (prinPaymentTB!=null)
                prinPaymentTB.disabled = true;            
        }
        else
        {
            testingControl.disabled = false;
            if (prinPaymentCB!=null)
                prinPaymentCB.disabled = false;
            if (prinPaymentTB!=null)
                prinPaymentTB.disabled = false;            
        }
    }
        
    var total = document.getElementById("total");
    total.innerHTML = formatCurrency( paymentTotal);    
}

function onKeyUpPrincipalPayment()
{
    var prinPaymentCB = document.getElementById("principalPaymentCB");
    prinPaymentCB.checked = true;
    setChecked(prinPaymentCB);
}

function checkPrincipalPayment()
{
    valid = true;        
    
    var prinPaymentCB = document.getElementById("principalPaymentCB");
    var prinPaymentTB = document.getElementById("principalPaymentTB");
    
    if ( (prinPaymentCB == null) || (prinPaymentTB == null) )
    {
        return valid;
    }
    
    if (prinPaymentCB.checked)
    {
        prinPaymentTB.value = prinPaymentTB.value.replace("$", "");
        prinPaymentTB.value = prinPaymentTB.value.replace(" ", "");
        prinPaymentTB.value = prinPaymentTB.value.replace(",", "");
        if (isNaN(prinPaymentTB.value))
        {
            alert('Please enter a valid Principal Payment amount.');
            valid = false;
        }
        else if (prinPaymentTB.value == "")
        {
            alert('Please enter a Principal Payment amount.');
            valid = false;        
        }        
    }
    if (!valid)
        prinPaymentTB.focus();
    return valid;
}
