function swapPicture(newImage)
{
    if (document.getElementById("mainpic"))
    {
      target=document.getElementById("mainpic");
    } else {
      target=document.images.mainpic;
    }

    target.src=newImage;
}

function showtour(url) {
	// need to see if contains http...
	turl = url.toLowerCase();
	if (turl.match('http://')  ) {
		window.open(url,'virtualtour');
	} else {
		window.open('http://' + url,'virtualtour');
	}
	return;
}

function poprmi(mlsno,cid) {
    var url = 'http://nh.agentave.com/aa_include/poprmi.php?cid=' + cid + '&mlsno=' + mlsno;
    var width = 500;
    var height = 430
     var left   = (screen.width  - width)/2;
     var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'AARMI', params);
    if (window.focus) {newwin.focus()}
}

function popmcalc() {
    document.getElementById("mcalc").style.display="block";
    document.getElementById("calcform").style.display="block";
    document.getElementById("calcresults").style.display="none";

    return false;
}

function closemcalc() {
    document.getElementById("mcalc").style.display="none";
}

function popprint(mlsno,cid) {
    var url = 'http://nh.agentave.com/aa_include3/poprmi.php?cid=' + cid + '&mlsno=' + mlsno;
    var width = 500;
    var height = 430
     var left   = (screen.width  - width)/2;
     var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'AARMI', params);
    if (window.focus) {newwin.focus()}
}

function printlisting() {
    window.print();
    return false;
}

function raisePower(x,y) {
  return Math.pow(x,y)
}

function ClearHome(form) {
  form.txtMthPay.value=""
  form.txtAnnCost.value=""
  form.txtAmtFin.value=""
  form.txtLoanAmount.value="";
}


function IsBlank(form) {
  if (form.txtSalePrice.value=="") {
  alert("Please enter a Sale Price before Calculating")
    form.txtSalePrice.focus()
    return true
  }
  if (form.txtLoanAmount.value=="") {
  alert("Please enter a Loan Amount before Calculating")
    form.txtSalePrice.focus()
    return true
  }
  if (form.txtPerFin.value=="") {
  alert("Please enter the term of the Loan (in years) before Calculating")
    form.txtPerFin.focus()
    return true
  }
  if (form.txtIntRate.value=="") {
  alert("Please enter an Interest Rate before Calculating")
    form.txtIntRate.focus()
    return true
  }
 return false
}


function StripChars(inputstring, charstostrip)
{
    var found=false;
    var returnstring="";

    for (var i=0; i<=inputstring.length; i++)
    {
        found=false
        for (var j=0; j<=charstostrip.length; j++)
        {    if (inputstring.charAt(i) == charstostrip.charAt(j))
            {found=true; break}
        // else continue for loop
        }
        if (found==false)
        {    returnstring=returnstring + inputstring.charAt(i)
        }
    // else continue on without adding to string
    }  // for
    return returnstring
} // end StripChars

function CheckField(inputstring,form,validstring)
{
    var found=false;
    var isValid=true;

//    inputstring=form.elements[x].value;
    for (var i=0; i<=inputstring.length; i++)
    {
        found=false
        for (var j=0; j<=validstring.length; j++)
        {    if (inputstring.charAt(i) == validstring.charAt(j))
            {found=true; break}
        // else continue for loop
        }
        if (found==false)
        {    isValid=false; alert(inputstring.charAt(i) + " is an invalid character for this field.  Backspace to remove this character and press Calculate again.");
             break;
        }
    // else continue on
        }  // for
        return isValid
    } // end CheckField

function ValidForm(form)
{
var OK=false

if (CheckField(form.txtSalePrice.value,form,'0123456789$,') == true)
    if (CheckField(form.txtPerFin.value,form,'0123456789$') == true)
            if (CheckField(form.txtIntRate.value,form,'0123456789.') == true)
                            OK=true
            else form.txtIntRate.focus()
    else form.txtPerFin.focus()
else form.txtSalePrice.focus()
return OK
}

browserVer=parseInt(navigator.appVersion);
browserName=navigator.appName;
             if (browserName == "Netscape" && browserVer >= 3) version = "3";
             else

             if (navigator.appName.indexOf("Microsoft") != -1 && browserVer >= 2) version = "3";
             else version = "2";


function goLoc(form)
         {
           if (version == "3") {
           var a=document.calcsel.calcsel;}
           if(a.options[a.selectedIndex].value=="")
         {
           if (version == "3") {
                   /*  This Next line left out by RT no error box needed?  */
                   /*  alert("That is NOT a valid option!");               */
                           }
         }
         else
             {
               if (version == "3") {
               location.href=a.options[a.selectedIndex].value;}
             }
           }

// verifies only a number was typed into the form element
function numbersonly(e){
    var unicode=e.charCode? e.charCode : e.keyCode
    // if (unicode!=8||unicode!=9)
    if (unicode<8||unicode>9) {
        //if the key isn't the backspace key or tab key (which we should allow)
        if (unicode<48||unicode>57) {//if not a number
            return false //disable key press
        }
    }
}


// google map code below here
var map = null;
var geocoder = null;

    function initialize2(lat,lon) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(lat,lon), 9);
        //alert("latlon: " + lat + "," + lon);
        geocoder = new GClientGeocoder();
      }
    }

function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var point = new GLatLng(lat,lon);

        map.setCenter(point, 10);
          map.addOverlay(new GMarker(point));
        }
    }


    function showAddress(address,townstate) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              // alert(address + " not found");
              var inaddress = document.getElementById("mapaddress").innerHTML;
              var outaddress = inaddress + "<br><p style='font-size: 10px;'>Address not geocoded</p>";
              document.getElementById("mapaddress").innerHTML=outaddress;
              // if the address wasn't found, try coding just the town.
              // if THAT isn't found, then just code Concord...
              showAddress(townstate,'Concord NH')
            } else {
                // alert("point" + point);
              map.setCenter(point, 11);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              // marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }



    function showmap(lat,lon, address, townstate) {
        document.getElementById("mapholder").style.display="block";
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            var point = new GLatLng(lat,lon);
            // alert ("Point: " + point);
            map.setCenter(point, 8);
            // map.setCenter(new GLatLng(lat, lon), 13);

            map.addOverlay(new GMarker(point));
        }

        // initialize(lat,lon);
        //showAddress(address,townstate);
        return false;
    }


    function closemap() {
        document.getElementById("mapholder").style.display="none";
        GUnload();
        return false;
    }


//mortgage calc

function calculate(form) {

  var intPerFin=0
  var intAmtFin=0

  var intPayPer = 0
  var intMthPay = 0
  var intMthInt = 0
  var intTotAmt = 0
  var intInterest= 0
  var intLoanAmt = 0
  var intIntRate = 0
  var intAnnCost = 0
  var intVal = 0
  var salePrice = ""

 // alert(form.name);

if ((IsBlank(form)==false) && (ValidForm(form)==true))
    {
    document.getElementById("calcform").style.display="none";
    document.getElementById("calcresults").style.display="block";
// LINE 50
// STRIP "$" or ","
    salePrice = StripChars(form.txtSalePrice.value,"$,")
    salePrice = StripChars(form.txtSalePrice.value,",")
    // alert("sale price: " + form.txtSalePrice.value);
// SalePrice=StripChars(form.txtSalePrice.value, "$,")
// CheckString(SalePrice, "0123456789.")

// alert (form.txtPerFin.value)

// PayPer is number of pay periods = no. of years x 12 months/per year-->
  intPayPer = eval(form.txtPerFin.value * 12)
  intAnnualTaxes = eval(form.txtAnnualTaxes.value)
  intAmtFin = eval(form.txtLoanAmount.value);

  intMonthlyTaxes = intAnnualTaxes/12
// convert interest rate to numerical value -->
  intIntRate = eval(form.txtIntRate.value)
// --calculate mthly interest in decimal form -->
  intMthInt = intIntRate / (12 * 100)
// -- call raise power function to get parameter for calculation -->
  intVal = raisePower(1+intMthInt, -intPayPer)
// -- calculate mthly payment -->
// if sale amound - loan amount < 20% sale amount then need pmi
// pmi calculated at .00087 of sale value...
  var intPMI;
  if (salePrice-intAmtFin < intAmtFin*.2) {
    intPMI = Math.round(intAmtFin * .00087);
  } else {
    intPMI = 0;
  }

  intMthPay = intAmtFin * (intMthInt / (1 - intVal))   + intMonthlyTaxes
  var intPI =  Math.round(intAmtFin * (intMthInt / (1 - intVal)));
  var intIns = Math.round(eval(form.txtInsurance.value/12));
  var intHOA = Math.round(eval(form.txtHOA.value/12));
  var intMT = Math.round(intMonthlyTaxes);
  var intMI = Math.round(intIns);
  var intMP = intPI + intMT + intMI + intPMI + intHOA;
  intAnnCost = intMthPay * 12

  document.getElementById('principle_interest').innerHTML = '$' + intPI;
  document.getElementById('property_tax').innerHTML = '$' + intMT;
  document.getElementById('insurance').innerHTML = '$' + intMI;
  document.getElementById('hoadues').innerHTML = '$' + intHOA;
  document.getElementById('pmi').innerHTML = '$' + intPMI;

  document.getElementById('monthly_payment').innerHTML = '$' + intMP;
  document.getElementById('instructions').style.display="none";
  document.getElementById('mrtresults').style.display="block";

  }

  return false;
}

function showcomps() {
    // get the comp data and then show the comp box.
    $("#compsinner").load("http://nh.agentave.com/aa_include/agentave.php?action=recentsales&cid=1173&mlsno=2829850");
    document.getElementById('compbox').style.display="block";
}

function closecomps() {
        document.getElementById('compbox').style.display="none";
}
