// currency format functions
function FormatCurrency(objNum) {
    var num = objNum.value;
    var ent, dec;
    if (num != '' && num != objNum.oldvalue) {
        num = MoneyToNumber(num);
        if (isNaN(num)) {
            objNum.value = objNum.oldvalue;
        } else {
            if (event.keyCode == 190 || !isNaN(num.split('.')[1])) {
                objNum.value = AddCommas(num.split('.')[0])+'.'+num.split('.')[1];
            } else {
                objNum.value = AddCommas(num.split('.')[0]);
            }
            objNum.oldvalue = objNum.value;
        }
    }
}
 
function MoneyToNumber(num) {
    return (num.replace(/,/g, ''));
}

 function AddCommas(number) {
    number = '' + number;
    if (number.length > 3) {
        var mod = number.length % 3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0)) {
                output += number.substring(mod + (3 * i), mod + (3 * (i + 1)));
            } else {
                output += ',' + number.substring(mod + (3 * i), mod + (3 * (i + 1)));
            }
        }
        return (output);
    } else {
        return (number);
    }
}


// load nav images
if (document.images) {
    nav_home = new Image();
    nav_home.src = "images/nav/home.gif" ;
    nav_home_roll = new Image() ;
    nav_home_roll.src = "images/nav/home_roll.gif" ;
    nav_cobrokedb = new Image();
    nav_cobrokedb.src = "images/nav/cobrokedb.gif" ;
    nav_cobrokedb_roll = new Image() ;
    nav_cobrokedb_roll.src = "images/nav/cobrokedb_roll.gif" ;
    nav_logout = new Image();
    nav_logout.src = "images/nav/logout.gif" ;
    nav_logout_roll = new Image() ;
    nav_logout_roll.src = "images/nav/logout_roll.gif" ;
    go1 = new Image();
    go1.src = "images/buttons/go.gif";
    go1_roll = new Image(); 
    go1_roll.src = "images/buttons/go.gif";
}

function gotoHome(num) {
	daForm = document.forms["form"];
	
    for (i=0; i < daForm.searchType.length; i++) {
        if (daForm.searchType[i].checked) {
            if (i == 0) {
                ptype = 'sale';
            } else if (i == 1) {
                ptype = 'rental';
            } else if (i == 2) {
                ptype = 'land';
            } else if (i == 3) {
                ptype = 'commercial';
            }
        }
    }

    if (daForm.archiveFlg.checked) {
        archiveflg = '&arch=1';
    } else {
        archiveflg = '&arch=0';
    }

    if (ptype == 'sale') {
        qstring = '?ptype=sale';
    } else if (ptype == 'rental') {
        qstring = '?ptype=rental';
    } else if (ptype == 'land') {
        qstring = '?ptype=land';
    } else if (ptype == 'commercial') {
        qstring = '?ptype=commercial';
    }

    if (num == 4) {
        location.href = 'home4.cfm' + qstring + archiveflg;
    } else if (num == 5) {
        location.href = 'home5.cfm' + qstring + archiveflg;
    } else if (num == 6) {
        location.href = 'home6.cfm' + qstring + archiveflg;
    } else if (num == 7) {
        location.href = 'home7.cfm' + qstring + archiveflg;
    } else if (num == 8) {
        location.href = 'home8.cfm' + qstring + archiveflg;
    } else if (num == 9) {
        location.href = 'home9.cfm' + qstring + archiveflg;
    } else if (num == 10) {
        location.href = 'home10.cfm' + qstring + archiveflg;
    }
}

function roll( buttonname ) {
    if (document.images) {
      document[ buttonname ].src = eval( buttonname + "_roll.src" );
    }
}

function out( buttonname ) {
    if (document.images) {
      document[ buttonname ].src = eval( buttonname + ".src" );
    }
}

function setGTP(gtp){
    document.form.destination.value=gtp;
    document.form.submit();
    return false;
}

// folionumber fields should contain only numberic, space, and commas
function checkFolioFormat(folionum) {
    var value = folionum;
    value = value.replace(/ /g,'');

    if (value == "") {
        alert("invalid folio #");
        return false;
    }

    var pinregex = /[A-Ga-g]/;;
    var match = value.match(pinregex);
    if (match) {
        alert("invalid folio #");
        return false;
    }

    var pinregex = /[I-Zi-z]/;;
    var match = value.match(pinregex);
    if (match) {
        alert("invalid folio #");
        return false;
    }

    pinregex = /[\#\@\!\$\%\^\&\*\(\)\<\>\.\?\/\\\'\"\;\:]/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid folio #");
        return false;
    }

    // cannot begin or end with comma
    pinregex = /^,/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid folio #");
        return false;
    }

    pinregex = /,$/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid folio #");
        return false;
    }

    return true;
}


// innumber fields should contain only numberic, space, and commas
function checkInnumFormat(innum) {
    var value = innum;
    value = value.replace(/ /g,'');
    value = value.replace(/H/gi,'');

    if (value == "") {
        alert("invalid IN #");
        return false;
    }

    var pinregex = /[A-Za-z]/;;
    var match = value.match(pinregex);
    if (match) {
        alert("invalid IN #");
        return false;
    }

    pinregex = /[\#\@\!\$\%\^\&\*\(\)\<\>\.\?\/\\\'\"\;\:]/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid IN #");
        return false;
    }

    // cannot begin or end with comma
    pinregex = /^,/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid IN #");
        return false;
    }

    pinregex = /,$/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid IN #");
        return false;
    }

    return true;
}

function checkSirNoFormat(sirno) {
    var value = sirno;
    value = value.replace(/ /g,'');

    if (value == "") {
        alert("invalid SIR #");
        return false;
    }

    var pinregex = /[A-Za-z]/;;
    var match = value.match(pinregex);
    if (match) {
        alert("invalid SIR #");
        return false;
    }

    pinregex = /[\#\@\!\$\%\^\&\*\(\)\<\>\.\?\/\\\'\"\;\:]/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid SIR #");
        return false;
    }

    // cannot begin or end with comma
    pinregex = /^,/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid SIR #");
        return false;
    }

    pinregex = /,$/;;
    match = value.match(pinregex);
    if (match) {
        alert("invalid SIR #");
        return false;
    }

    return true;
}

function isInteger(object_value) {
    var number_format = "0123456789";
    var check_char;
    var digits = false;

    for (var i = 0; i < object_value.length; i++) {
        check_char = number_format.indexOf(object_value.charAt(i))
        if (check_char < 0){
            return 0;
        }
    }

    return 1;
}

function newPopUpWindow(url, width, height) {
    var popup = window.open('',"SameWindow",'directories=no,location=no,menubar=yes,toolbar=no,resizable=yes,scrollbars=yes');
    popup.resizeTo(parseInt(width), parseInt(height));
    popup.location.href = url;
    popup.focus();   
    return false;
}

function newPopUpWindow2(url, width, height) {
    var popup = window.open('',"SameWindow2",'directories=no,location=no,menubar=yes,toolbar=no,resizable=yes,scrollbars=yes');
    popup.resizeTo(parseInt(width), parseInt(height));
    popup.location.href = url;
    popup.focus();   
    return false;
}

function newPopUpWindow3(url, width, height) {
    // make the title of the window "" to get around the access denied problem when opening hreo windows
    var popup = window.open('',"",'directories=no,location=no,menubar=yes,toolbar=no,resizable=yes,scrollbars=yes');
    popup.resizeTo(parseInt(width), parseInt(height));
    popup.location.href = url;
    popup.focus();   
    return false;
}

function newPopUpWindow4(url, width, height) {
	// with tool bar
    var popup = window.open('',"SameWindow4",'directories=no,location=no,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes');
    popup.resizeTo(parseInt(width), parseInt(height));
    popup.location.href = url;
    popup.focus();   
    return false;
}


function setPriceDiv() {
    if (document.form.propertyType[0].checked) {
        price_string = '<table cellpadding="0" cellspacing="0" border="0">';
        price_string += '<tr>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01">min $</td>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01">max $</td>';
        price_string += '</tr>';
        price_string += '<tr>';
        price_string += '<td class="small_01">price:&nbsp;</td>';
        price_string += '<td class="small_01"><input type="text" name="minPrice" size="13" maxlength="11" class="xtextbox_02" oldvalue="" onkeyup="FormatCurrency(this);"></td>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01"><input type="text" name="maxPrice" size="13" maxlength="11" class="xtextbox_02" oldvalue="" onkeyup="FormatCurrency(this);"></td>';
        price_string += '</tr>';
        price_string += '<tr>';
        price_string += '<td colspan="4"><img src="images/misc/spacer.gif" height="2" width="1" border="0"></td>';
        price_string += '</tr>';
        price_string += '</table>';
    } else if (document.form.propertyType[1].checked) {
        price_string = '';
    } else if (document.form.propertyType[2].checked) {
        price_string = '<table cellpadding="0" cellspacing="0" border="0">';
        price_string += '<tr>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01">min $</td>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01">max $</td>';
        price_string += '</tr>';
        price_string += '<tr>';
        price_string += '<td class="small_01">price:&nbsp;</td>';
        price_string += '<td class="small_01"><input type="text" name="minPrice" size="13" maxlength="11" class="xtextbox_02" oldvalue="" onkeyup="FormatCurrency(this);"></td>';
        price_string += '<td class="small_01">&nbsp;</td>';
        price_string += '<td class="small_01"><input type="text" name="maxPrice" size="13" maxlength="11" class="xtextbox_02" oldvalue="" onkeyup="FormatCurrency(this);"></td>';
        price_string += '</tr>';
        price_string += '<tr>';
        price_string += '<td colspan="4"><img src="images/misc/spacer.gif" height="2" width="1" border="0"></td>';
        price_string += '</tr>';
        price_string += '</table>';
    } else if (document.form.propertyType[3].checked) {
        price_string = '';
    }

    pricediv.innerHTML = price_string;
}


function PopupShowingLog(folionum) {
    var popup = window.open("propertyShowingLog.cfm?folionum="+folionum, "ShowingLog", "width=825,height=400,left=200,top=200,resizable=no,scrollbars=yes,status=no,location=no,menubar=no");
    popup.focus();
}


function validateDateFormat(inputstr) {
	var regex = /[0-9]{2}\/[0-9]{2}\/[0-9]{2}/;

	if (regex.test(inputstr)) {
		return true;
	} else {
		return false;
    }
}

function isNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}

	return IsNumber;
}

function isPercent(sText) {
	var ValidChars = "0123456789.% ";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}

	return IsNumber;
}


