//+=================================================================+
//|  File. . . . : savingsCalc.js        
//|                                   
//|  Description :  function to calculate cost savings                                       
//|                                                                 
//|  Copyright 2006  Experian Limited, Talbot House, Talbot Street, 
//|                  Nottingham, NG1 5HF, ENGLAND                   
//|      telephone   int + 44 (0)115 9410 888                       
//|                                                                 
//|  The copyright to the computer program(s) herein is the         
//|  property of Experian Limited, ENGLAND.                         
//|  The program(s) may be used and/or copied only with the written 
//|  permission of Experian Limited or in accordance with the terms 
//|  and conditions stipulated in the agreement/contract under      
//|  which the programs have been supplied.                         
//+=================================================================+

function calculate(){

    mailpack = document.getElementById('mailpack');
    mailed = document.getElementById('mailed');
    
    if (mailpack)
    {
        if (! isPositiveNumber(mailpack.value)) 
        {
            mailpack.value = defaultContactCost;
        } 
    }
    else
    {
        return;
    }
    
    if (mailed)
    {
        if (! isPositiveNumber(mailed.value)) 
        {
            mailed.value = defaultCustomers;
        }
        else
        {
            mailed.value = Math.floor(mailed.value);
        } 
    }
    else
    {
        return;
    }
    
    for(i = 1; i < costMatrix.length; i++)
    {
        currentElement = document.getElementById(costMatrix[i][0]);
        if (currentElement)
        {
            if (currentElement.checked) 
            {
                costMatrix[i][4] = 1;
            } 
            else 
            {
                costMatrix[i][4] = 0;
            }
        }
        else
        {
            return;
        }
    }
    
    var totalex = 0
    var screenexcost = 0
    var waste = 0

    for(i = 1; i < costMatrix.length; i++)
    {
	    totalex = (totalex + (costMatrix[i][4] * (mailed.value) * costMatrix[i][2]));
	    screenexcost = (screenexcost + (costMatrix[i][4] * mailed.value * costMatrix[i][1]))
	}
    		
    if (screenexcost < 25 && screenexcost > 0) 
    {
        screenexcost = 25;
    }

    setInnerText('ExcludedResult', Math.round(totalex));
    setInnerText('ContactSaving', Math.round((mailpack.value / 100 * totalex)));
    setInnerText('ScreeningCost', Math.round(screenexcost));
    setInnerText('CampaignSaving', Math.round(((mailpack.value / 100 * totalex) - screenexcost)));
}

function setInnerText(elementId, text)
{
    if(document.all)
    {
        document.getElementById(elementId).innerText = text;
    } 
    else
    {
        document.getElementById(elementId).textContent = text;
    }
}

// Has return been pressed?
function keypress(e)
{
    if (!e) 
    {
        var e = window.event;
    }
    
    var key = null;
    
	if (e.keyCode)
	{
	    key = e.keyCode;
	}
	else if (e.which) 
	{
	    key = e.which;
    }
    
    if (key == 13)
    {
       calculate();
    }
    else
    {
        return false;
    }
}

function isPositiveNumber(num)
{
    
    if (num == '')
    {
        return false;
    }
    
    if (isNaN(num))
    {
        return false;
    }

    if (num < 0)
    {
        return false;
    }

    return true;
}


