﻿
function initCalc(amt, ir, term) {
//        document.aspnetForm.amt.value = amt;
//        document.aspnetForm.ir.value = ir;
//        document.aspnetForm.term.value = term;
//        calcpayment();
    }
    function initMap(inAddress,inCity,inState, canvas) {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(0, 0);
        var myOptions = { zoom: 10, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }
        map = new google.maps.Map(canvas, myOptions);
        //codeCityState(inAddress, inCity, inState, map);
        //alert(map);
        codeAddress(inAddress, inCity, inState, map);

        //alert(map);
    }
    function initSatelliteMap(inAddress,incCity,inState, canvas) {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(0, 0);
        var myOptions = { zoom: 18, center: latlng, mapTypeId: google.maps.MapTypeId.SATELLITE }
        satmap = new google.maps.Map(canvas, myOptions);
        codeAddress(inAddress, inCity, inState, satmap);
    }
    function codeAddress(inAddress, inCity, inState, map) {

        var address = inAddress; //document.getElementById("address").value;
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                    //alert("address");
                }
                else {
                    codeCityState(inAddress, inCity, inState, map);
                    //alert("Geocode was not successful for the following reason: " + status);


                }
            });
        }
    }
    function codeCityState(inAddress,inCity,inState,map) {
        geocoder2 = new google.maps.Geocoder();
        
        if (geocoder2) {
            geocoder2.geocode({ 'address': inCity + " " + inState }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                }
                else {


                    //alert("Geocode was not successful for the following reason: " + status);


                }
            });
        }
    }
function modalWin(url) {

    if (window.showModalDialog) {
        window.showModalDialog(url, "name", "dialogWidth:660px;dialogHeight:650px");
    }
    else {
        window.open(formName, 'name', 'height=650,width=660,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
    }
}

function openDetails(url) {
    window.location.href = url;

}
function highlight(tableRow, active) {
    if (active) {
        tableRow.originalstyle = tableRow.style.backgroundColor;
        tableRow.originalBackgroundImage = tableRow.style.backgroundImage;
        
        tableRow.style.backgroundColor = '#dae7ef';
        tableRow.style.backgroundImage = 'none';
    }
    else {

        tableRow.style.backgroundColor = tableRow.originalstyle;
        if(tableRow.style.originalBackgroundImage!=null)
        tableRow.style.backgroundImage = tableRow.style.originalBackgroundImage;
    }
}
function highlightWithBackground(tableRow, active) {
    if (active) {
        tableRow.originalstyle = tableRow.style.backgroundColor;
        tableRow.originalBackgroundImage = tableRow.style.backgroundImage;

        tableRow.style.backgroundColor = '#dae7ef';
        tableRow.style.backgroundImage = 'none';

    }
    else {

        tableRow.style.backgroundColor = tableRow.originalstyle;
        tableRow.style.backgroundImage = "url(images/grd_alt.png)"; //tableRow.style.originalBackgroundImage;
    }
}

// calculates the payments and updates for fields to
// display the result
function calcpayment() {


    var amt = document.aspnetForm.amt.value;
    var ir = document.aspnetForm.ir.value / 1200;
    var term = document.aspnetForm.term.value * 12;
    var total = 1;
    amt = amt.toString().replace(/\$|\,/g, '');

    for (i = 0; i < term; i++) {
        total = total * (1 + ir);
    }
    mp = amt * ir / (1 - (1 / total));

    // use Math object to chop all numbers after 2 digits
    document.aspnetForm.payment.value = formatCurrency(Math.round(mp * 100) / 100);
    //document.aspnetForm.total.value = formatCurrency(Math.round(mp * term * 100) / 100);
}


function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}


function windowInit() {

    initMap();


}
function initCalculator() {
    document.aspnetForm.amt.value = formatCurrency('<% = GetPrice() %>');
    document.aspnetForm.ir.value = '5.0';
    document.aspnetForm.term.value = '30';

    calcpayment();
}

