﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="modalDialog.js" />
/// <reference path="../wsPaymentCalc.asmx" />
/// <reference path="../wsContact.asmx" />

/********************   Map zoom methods   ********************/

var maps = new Array();
maps[0] = 'files/media/BigMap.jpg';
maps[1] = 'files/media/BigUPmap.jpg';
maps[2] = 'files/media/MediumUPmap.jpg';
maps[3] = 'files/media/BigLakeGogebic.jpg';
maps[4] = 'files/media/EwenMap.jpg';

var idxMap = 0;
function zoomMap(dir) {
	idxMap += dir;
	var btnNext = $get('elbtnnext');
	var btnBack = $get('elbtnback');
	if (idxMap == maps.length - 1) {
		//hide the next button
		btnNext.style.display = 'none';
	}
	else {
		btnNext.style.display = 'inline';
	}
	if (idxMap == 0) {
		//hide the back button
		btnBack.style.display = 'none';
	}
	else {
		btnBack.style.display = 'inline';
	}
	
	$get('elmap').src = maps[idxMap];
}

/********************   image methods   ********************/

var effectTO = 0;
var lastSrc = '';
function setImage(targetID, src, remove) {

    if (typeof remove == 'string') {
        var re = new RegExp(remove, 'gi');
        src = src.replace(re, '');
    }
    effectTO = setTimeout("startEffect('" + targetID + "', '" + src + "')", 700);
}

function startEffect(targetID, src) {
    if (lastSrc == src) return;
    lastSrc = src;
    var target = $get(targetID);
    if (target.tagName == 'IMG') {
        if (target.src.toLowerCase() == src.toLowerCase()) return;
        target.style.display = 'none';
        target['onload'] = new Function('new Effect.Appear("' + targetID + '");'); 
        target.src = src;
    }
}

function clearEffect() {
    if (effectTO > 0) {
        clearTimeout(effectTO);
    }
}

/********************   payment calculator methods   ********************/

function popPaymentCalculator(price) {
    price = toNumeric(price);
    if (ModalDialog.status == 'new') ModalDialog.construct();
    wsPaymentCalc.GetCalculator(parseInt(price), setPaymentCalc);
}

function setPaymentCalc(html) {
    ModalDialog.showModalDialog(500, 280, html, 'auto', 'Payment Calculator');
}

function toNumeric(val) {
    var iChars = '.1234567890';
    var num = c = '';
    for (var i=0; i<val.length; i++) {
        c = val.charAt(i);
        if (iChars.indexOf(c) > -1) num += c;
    }
    return num;
}

/* This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com */
/* Modified for use on this site */
function checkNumber(str, min, max, msg) 
{
    msg = msg + " field has invalid data: " + str;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert(msg);
            return false;
        }
    }

    var num = parseFloat(str)
    if (num < min || max < num) {
        alert(msg + " not in range [" + min + ".." + max + "]");
        return false;
    }
    return true;
}

function computeField(input)
{
    var val = input.value;
    if (val != null && val.length != 0) {
        input.value = toNumeric(val);
        computeForm();
    }
}

function computeForm() 
{
    var p = $get('payments').value;
    var i = $get('interest').value;
    var pp = $get('principal').value;
    var d = $get('downPayment').value;
    var oPayment = $get('payment');
    if ((p == null || p.length == 0) ||
    (i == null || i.length == 0) ||
    (pp == null || pp == 0)) {
        return;
    }
    
    if (!checkNumber(p, 1, 480, "# of payments") ||
    !checkNumber(i, .001, 99, "Interest") ||
    !checkNumber(pp, 100, 10000000, "Principal")) {
        oPayment.value = "Invalid";
        return;
    }
    
    if (i > 1.0) {
        i = i / 100.0;
        $get('interest').value = i;
    }
    i /= 12;
    var pow = 1;
    for (var j = 0; j < p; j++)
        pow = pow * (1 + i);
        money = "" + .01* Math.round(100*((pp - d) * pow * i) / (pow - 1));
        dec = money.indexOf(".");
        dollars = money.substring(0,dec); 
        cents = money.substring(dec+1,dec+3);
        cents = (cents.length < 2) ? cents + "0" : cents;
        money = dollars + "." + cents;
        oPayment.value = money;
    }

function clearForm()
{
    $get('payments').value = '';
    $get('interest').value = ''
    $get('downPayment').value = '';
    $get('payment').value = '';
}

/********************   contact methods   ********************/

function submitContact() {
    var emailTo = $get('hdnemailto').value;
    var fromName = $get('tbname').value;
    //require name
    if (fromName == '') {
        alert('Please tell us your name.');
        return;
    }
    var fromEmail = $get('tbemail').value;
    var fromPhone = $get('tbphone').value;
    if (fromEmail == '' && fromPhone == '') {
        alert('Please provide either an email address or a phone number so that we can contact you.');
        return;
    }
    var fromComment = $get('tbcomment').value;
    wsContact.SubmitContact(emailTo, fromName, fromEmail, fromPhone, fromComment, submitContactResult);
}

function submitContactResult(result) {
    if(result == 'success') {
        alert('Thank you for your interest!\n\nThe message has been sent, we\'ll be in touch with you shortly.');
        resetContactForm();
    }
}

function getContactForm(contactInfo) {
    wsContact.GetContactForm(contactInfo, popContactForm);
}

function resetContactForm() {
    if (ModalDialog.status == 'visible') {
        ModalDialog.hideModalDialog();
        return;
    }
    $get('tbname').value = '';
    $get('tbemail').value = '';
    $get('tbphone').value = '';
    $get('tbcomment').value = '';
}

function popContactForm(html) {
    ModalDialog.showModalDialog(450, 280, html, 'auto', 'Send a Message');
}

/********************   insurance quote request methods   ********************/

function submitQuoteRequest() {
    var fName = $get('tbfname').value;
    if (fName == '') {
        alert('Please tell us your first name.');
        return;
    }
    var lName = $get('tblname').value;
    var address = $get('tbaddress').value;
    var citystatezip = $get('tbcitystatezip').value;
    var phone = $get('tbphone').value;
    var calltime = $get('tbcalltime').value;
    var email = $get('tbemail').value;
    if (email == '' && phone == '') {
        alert('Please provide a phone number or email address so that we can respond to your request.');
        return;
    }
    //get the checkboxes
    var sInquery = getCheckedBoxes('cblitems');
    if (sInquery == '') {
        alert('Please tell us what kind of insurance quote you are requesting.');
        return;
    }
    var additional = $get('tbadditional').value;
    showSubmitDialog();
    //create a message using the existing contact form
    wsContact.SubmitContact(0, fName + ' ' + lName, email, phone, '\n\nRequesting insurance quote\n\nAddress:\n' + address + '\n' + citystatezip + '\n\nType:\n' + sInquery + '\n\nAdditional comments:\n' + additional + '\n\nBest call time:\n' + calltime, submitQuoteRequestResult);
}

function submitQuoteRequestResult(result) {
    if(result == 'success') {
        alert('Thank you for your interest!\n\nYour quote request has been sent successfully, we\'ll be in touch with you shortly.');
        document.location.href = 'default.aspx?content=insurance';
    }
}

function getCheckedBoxes(cblID) {
    var oCBL = $get(cblID);
    if (!oCBL) alert('lost');
    var sReturn = '';
    var oCBs = oCBL.getElementsByTagName('INPUT');
    for (var i=0; i<oCBs.length; i++) {
        if (oCBs[i].checked) sReturn += oCBs[i].value + ', ';
    }
    //trim the last ,
    sReturn = sReturn.substring(0, sReturn.length - 2);
    return sReturn;
}

/********************   preferred buyer methods   ********************/

function submitPreferredBuyer() {
    var name = $get('tbname').value;
    if (name == '') {
        alert('Please tell us your name.');
        return;
    }
    var email = $get('tbemail').value;
    var phone = $get('tbphone').value;
    if (email == '' && phone == '') {
        alert('Please provide a phone number or email address so that we can respond to your request.');
        return;
    }
    var propertyType = getSelectedRadio('rblproptype');
    var minPrice = $get('tbminprice').value;
    var maxPrice = $get('tbmaxprice').value;
    var location = $get('tblocation').value;
    var waterfront = $get('cbwaterfront').checked;
    var details = $get('tbdetails').value;
    showSubmitDialog();
    wsContact.SubmitContact(0, name, email, phone, '\n\nPreferred Buyer Service Request\n\nType of Property:\n' + propertyType + '\n\nMinimum Price:\n' + minPrice + '\n\nMaximum Price:\n' + maxPrice + '\n\nLocation/Area:\n' + location + '\n\nWaterfront:\n' + waterfront + '\n\nOther details:\n' + details, submitPreferredBuyerResult);
}

function submitPreferredBuyerResult(result) {
    if(result == 'success') {
        alert('Thank you for your interest!\n\nYour preferred buyer request has been sent successfully, we\'ll be in touch with you shortly.');
        document.location.href = 'default.aspx?content=marketingsvc';
    }
}

function getSelectedRadio(rblID) {
    var oRBL = $get(rblID);
    if (!oRBL) return '';
    var oRBs = oRBL.getElementsByTagName('INPUT');
    for (var i=0; i<oRBs.length; i++) {
        if (oRBs[i].checked) return oRBs[i].value;
    }
    return '';
}

function showSubmitDialog() {
    ModalDialog.showModalDialog(350, 100, "Submitting your request...", "hidden", "Submitting Your Request");
}