﻿// JScript File
    
    function summary()
    {
        var index = $get('acResults_AccordionExtender_ClientState').value;
        var state = $get('hfState').value;
        
        $get('SummaryFlexNY').className = (index == 0 && state == "NY") ? '' : 'noprint';
        $get('SummaryFlexMD').className = (index == 0 && state == "MD") ? '' : 'noprint';
        $get('SummaryHENYMD').className = (index == 1 && (state == "NY" || state == "MD")) ? '' : 'noprint';
    
        $get('SummaryHEOther').className = (index == 1 && !(state == "NY" || state == "MD")) ? '' : 'noprint';
        $get('SummaryFlexOther').className = (index == 0 && !(state == "NY" || state == "MD")) ? '' : 'noprint';
        
        $get('SummaryFooter').className = '';
    }
    
	function resize()
	{
	    if (window.parent)
    		setTimeout("window.parent.resize(document.getElementById('container').offsetHeight)", 500);
	}
	
    //error handler
    function EndRequestHandler(sender, args)
    {
        if (args.get_error() != null)
        {
            var errorName = args.get_error().name;
            if (errorName.length > 0 )
            {
                args.set_errorHandled(true);  
                if ($get('lblError1') != null)
                    $get('lblError1').innerHTML = 'We were unable to fulfill your request. We apologize for inconvenience. Please try different amount/address or give us a call at 877-4-SOV-LOAN';                                           
                if ($get('lblError2') != null)
                    $get('lblError2').innerHTML = 'We were unable to fulfill your request. We apologize for inconvenience. Please try different amount/address or give us a call at 877-4-SOV-LOAN';                                           
            }
        }
        
        resize();
    }

    function highLight(listitem){
    
        if(listitem == "listOne"){
            $get('listOne').style.color = '#333';
	        $get('listTwo').style.color = '#666';
	        $get('listThree').style.color = '#666';
	    }
	    if(listitem == "listTwo"){
            $get('listOne').style.color = '#666';
	        $get('listTwo').style.color = '#333';
	        $get('listThree').style.color = '#666';
	    }
	    if(listitem == "listThree"){
            $get('listOne').style.color = '#666';
	        $get('listTwo').style.color = '#666';
	        $get('listThree').style.color = '#333';
	    }
        $get('propInfo').style.display='block';
        
        resize();
    }
    
    function continueOne()
    {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
	    var theChoice;
	    for (var i=0; i<document.hvbot.info.length; i++)
	    {
		    if (document.hvbot.info[i].checked)
		        theChoice=document.hvbot.info[i].value;
	    }
	    if (theChoice=='one'){
            __doPostBack('ContinueOneRadioOneLink','');
        }
	    
	    else if (theChoice=='two'){
            __doPostBack('ContinueOneRadioTwoLink','');
        }
	    
	    else if (theChoice=='three'){
            __doPostBack('ContinueOneRadioThreeLink','');
        }
        else
	    {
	        $get('lblError1').innerHTML = "Please select a borrowing scenario.";		    
	    }
	    
        resize();
    }
    
    function continueTwo()
    {    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);  
        __doPostBack('ContinueTwoLink','');
	 	
        resize();
    }
    
    function simpleCalc(input){
 
        var homeValue = 0;
        var balance = 0;
        var newVal = 0;
        
        homeValue = parseFloat(document.hvbot.HomeValue.value.replace('$','').replace(/,/g,''));
        
        balance = document.hvbot.balances.value;
        if (balance.indexOf('$') > -1)
            balance = parseFloat(document.hvbot.balances.value.replace('$','').replace(/,/g,''));
        
        newVal = homeValue - balance;
        if (newVal < 0)
            newVal = 0;
        
        document.hvbot.equity.value = newVal; 
        document.hvbot.borrowEquity.value = '$'+monetary(newVal);
    }
    
    function refresh()
    {
        window.location.reload( true );
    }
    
    function isNumberKey(e)
    {
        if (!e)
            e = event;
        var charCode = (e.which) ? e.which : e.keyCode;
        if ((charCode > 31 && (charCode < 48 || charCode > 57)) && (charCode != 37 && charCode != 39 && charCode != 46))
            return false;

        return true;
    }
        
    function formatCurrency(e)
    {
        var o = null;
        if (e.target) o = e.target;
        else if (e.srcElement) o = e.srcElement;
        if (o.nodeType == 3)
            o = o.parentNode;
        
        if (o) {
            var num = parseFloat(o.value.replace('$','').replace(/,/g,''));
            if (!isNaN(num))
                o.value = '$'+monetary(num);
        }
    }
    
    function monetary(amount) {
        var integer = Math.floor(amount);
        var floating = amount - integer;

        return commas(integer);// + cents(floating);
    }        
    function round(number,x) {
        return Math.round(number * Math.pow(10,x)) / Math.pow(10,x);
    }
    function cents(amount) {
        amount -= 0;
        amount = round(amount, 2);
        if (amount == 0)
            amount = '.00';
        else if (amount == Math.floor(amount))
            amount += '.00';
        else if (amount*10 == Math.floor(amount*10))
            amount += '0';
        return '' + amount;
    }
    function commas(integer) {
        integer = '' + Math.round(integer);
        if (integer.length > 3) {
            var mod = integer.length%3;
            var output = (mod > 0 ? (integer.substring(0,mod)) : '');
            for (i=0 ; i < Math.floor(integer.length/3) ; i++) {
                if ((mod ==0) && (i ==0))
                    output+= integer.substring(mod+3*i,mod+3*i+3);
                else
                    output+= ',' + integer.substring(mod+3*i,mod+3*i+3);
            }
            return output;
        }
        return integer;
    }
    
    function setOption(id)
    {
        var o = document.getElementById(id);
        if (o != null)
            o.checked = true;
    }
    
    function primaryResidence()
    {
        document.hvbot.occupy[0].checked = true;
        $get('lblError1').innerHTML = "We are unable to provide results based on your entry.<br/><br/>Please call 877.4.SOV.LOAN to learn about non-owner-occupied borrowing options.";
        
        resize();
    }
    
    function doSelect()
    {
        if (!$get('borrow2').checked) $get('borrow2').checked = true;
    }
    
    function showCalc(objId)
    {
        document.getElementById(objId).style.display = 'block';
        resize();
    }
    
    function doPrint()
    {
        summary();
        window.print();
    }
    