// non-Microsoft form validation script
var checkobj

function isblank(data)
{
	// is it empty?
	for(var i = 0; i < data.length; i++)
	{
	var c = data.charAt(i);
	if((c!=' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function validation (field)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for(var i = 0; i < field.length; i++)
	{
		var strg = field.elements[i];
		if(((strg.type == "text") || (strg.type == "textarea")) && !strg.optional)
		{
			if((strg.value == null) || (strg.value == "") || isblank(strg.value))
			{
				empty_fields += "\n           "+strg.name;
				continue;
			}
			if(strg.numeric || (strg.min != null) || (strg.max != null))
			{
				var v = parseFloat(strg.value);
				if (!isNaN(v) || ((strg.min != null) && (v < strg.min)) || ((strg.max != null) && (v > strg.max)))
				{
					errors += " The field "+strg.name+" must be numeric";
					if(strg.min != null)
						errors += " that is greater than "+strg.min;
					if(strg.max != null && strg.min != null)
						errors += " that is less than "+strg.max;
					else if(strg.max != null)
						errors += " that is less than "+strg.max;
					errors += ".\n";
				}
			}
		}
	}
	if(!empty_fields && !errors) return true;
	
	msg = "-------------------------------\n\n"
	msg += "The form was not submitted due to the following errors.\n";
	msg += "Please correct the errors and re-submit:\n";
	msg += "------------------------------\n\n"
	
	if(empty_fields)
	{
		msg +=  "The following fields are empty:" +empty_fields+"\n";
		if (errors) msg += "\n";
	}
	msg += errors;
	alert(msg);
	return false;
}
//end non-Microsoft form validation script


//mortgage calculator script

function fixFloatFormat(input)
{
	var str = input.value;
	var index = 0;
	var newstr = 0;
	
	for (var i = 0; i < str.length; i++) {
		var ch = str.substring(i, i + 1)
		if (ch == '.') {
		index = i;
			   }
				    }
		newstr = str.substring(0, index);
		input.value = newstr + "." + str.substring(index+1, index+3);
		return true;
}

function checkNumber(input, min, max, msg)
{
	msg = msg + " Field has Invalid Data: " + input.value;
	var str = input.value;
	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 = 0 + str
	if (num < min || max < num) {
		alert(msg + " not in range [" + min + ".." + max + "]");
		return false;
			                 }
	    input.value = str;
	    return true;
}

function computeField(input)
{
	if (input.value != null && input.value.length != 0)
		input.value = "" + eval(input.value);
		computeMortgagePayments(input.form);
}

function computeMortgagePayments(form)
{
	if ((form.mortAmt.value == null || form.mortAmt.value.length == 0) ||
	     (form.numYears.value == null || form.numYears.value.length == 0) ||
	     (form.propTax.value == null || form.propTax.value.length == 0) ||
	     (form.debt.value == null || form.debt.value.length == 0) ||
	     (form.mortRate.value == null || form.mortRate.value.length == 0)) {
	       return;
							                  }
	if (!checkNumber(form.mortAmt, 1, 10000000, "Mortgage Amount") ||
	     !checkNumber(form.numYears, 1, 50, "Number of Years") ||
	     !checkNumber(form.mortRate, 0, 99.99, "Mortgage Rate") ||
	     !checkNumber(form.debt, 0, 1000000, "Debt.") ||
	     !checkNumber(form.propTax, 0, 100000, "Property Tax")) {
		form.mortPay.value = "Invalid";
		form.totalPay.value = "Invalid";
		form.reqdSal.value = "Invalid";
	       return;
							    }
	var mortgage = form.mortAmt.value;
	var yrTime = form.numYears.value;
	var yrRate = form.mortRate.value;
	var yrTax = form.propTax.value;
	var yrDebt = form.debt.value;
	var yrSalary = 0;
	var monTax = yrTax/12;
	var monDebt = yrDebt/12.0;
	var rate = yrRate/1200.00;
	var monTime = yrTime * 12.0;
	var poly = (1.0 + rate);
	for (i=1; i < monTime; i++) {
	        poly = poly * (1.0 + rate);
			                  }
	var monPayment  = mortgage * poly * rate / (poly - 1.0);
	var monTotal = monPayment + monTax + monDebt;
	var mon28 = (monPayment + monTax) / .28;
	var mon35 = monTotal / .35;
	if (mon28 > mon35) {
		yrSalary = 12 * mon28;
			 } else {
		yrSalary = 12 * mon35;
			              }
	var principal = 0.0;
	var interest = 0.0;
	for (i = 1; i < monTime; i++) {
		interest = interest + (principal * rate);
		principal = (principal + monPayment)/(1.0 + rate);
				}
		form.mortPay.value = monPayment;
		fixFloatFormat(form.mortPay);
		form.totalPay.value = monTotal;
		fixFloatFormat(form.totalPay);
		form.reqdSal.value = yrSalary;
		fixFloatFormat(form.reqdSal);
}

function clearForm(form)
{
	form.mortAmt.value = "";
	form.numYears.value = "";
	form.propTax.value = "";
	form.debt.value = "";
	form.mortRate.value = "";
	form.mortPay.value = "";
	form.totalPay.value = "";
	form.reqdSal.value = "";;
}
//end mortgage calculator script

//Accept terms script
function agreesubmit(el){
	
	//"Accept terms" form submission- By Dynamic Drive
	//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
	//This credit MUST stay intact for use
	
	checkobj=el
	
	if (document.all||document.getElementById){
		for (i=0;i<checkobj.form.length;i++){  //hunt down submit button
			var tempobj=checkobj.form.elements[i]
			if(tempobj.type.toLowerCase()=="submit") tempobj.disabled=!checkobj.checked
		}
	}
}

function defaultagree(el){
	if (!document.all&&!document.getElementById){
		if (window.checkobj&&checkobj.checked)
			return true
		else{
			alert("Please read/accept terms to submit form")
			return false
		}
	}
}
//end Accept terms script

//Clock script
function doClock(){ 
  var t=new Date(),a=doClock.arguments,str="",i,a1,lang="1";
  var month=new Array('January','Jan', 'February','Feb', 'March','Mar', 'April','Apr', 'May','May', 'June','Jun', 'July','Jul', 'August','Aug', 'September','Sep', 'October','Oct', 'November','Nov', 'December','Dec');
  var tday= new Array('Sunday','Sun','Monday','Mon', 'Tuesday','Tue', 'Wednesday','Wed','Thursday','Thr','Friday','Fri','Saturday','Sat');
  for(i=0;i<a.length;i++) {a1=a[i].charAt(1);switch (a[i].charAt(0)) {
  case "M":if  ((Number(a1)==3) && ((t.getMonth()+1)<10)) str+="0";
  str+=(Number(a1)>1)?t.getMonth()+1:month[t.getMonth()*2+Number(a1)];break;
  case "D": if ((Number(a1)==1) && (t.getDate()<10)) str+="0";str+=t.getDate();break;
  case "Y": str+=(a1=='0')?t.getFullYear():t.getFullYear().toString().substring(2);break;
  case "W":str+=tday[t.getDay()*2+Number(a1)];break; default: str+=unescape(a[i]);}}return str;
}
//End clock script


//Pop-up window script
function openPopWinOnce(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
  var d_winLeft = 20  // default, pixels from screen left to window left
  var d_winTop = 20   // default, pixels from screen top to window top
  var popcookies = document.cookie
  if (openPopWinOnce.arguments.length >= 4)  // any additional features? 
    winFeatures = "," + winFeatures
  else 
    winFeatures = "" 
  if (openPopWinOnce.arguments.length == 6)  // location specified
    winFeatures += getLocationOne(winWidth, winHeight, winLeft, winTop)
  else
    winFeatures += getLocationOne(winWidth, winHeight, d_winLeft, d_winTop)
  if (popcookies.indexOf(oneTimeWinName) == -1){ // cookie not found 
    window.open(winURL, oneTimeWinName, "width=" + winWidth 
           + ",height=" + winHeight + winFeatures)
    document.cookie=oneTimeWinName+"=used"
  }
}

function openPopWinOnceBack(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
  openPopWinOnce(winURL, winWidth, winHeight, winFeatures, winLeft, winTop)
  self.focus()
}

function getLocationOne(winWidth, winHeight, winLeft, winTop){
  return ""
}

function getLocationOne(winWidth, winHeight, winLeft, winTop){
  var winLocation = ""
  if (winLeft < 0)
    winLeft = screen.width - winWidth + winLeft
  if (winTop < 0)
    winTop = screen.height - winHeight + winTop
  if (winTop == "cen")
    winTop = (screen.height - winHeight)/2 - 20
  if (winLeft == "cen")
    winLeft = (screen.width - winWidth)/2
  if (winLeft>0 & winTop>0)
    winLocation =  ",screenX=" + winLeft + ",left=" + winLeft	
                + ",screenY=" + winTop + ",top=" + winTop
  else
    winLocation = ""
  return winLocation
}
//end pop-up script

//Add to favorites script
function addToFavorites() {
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
var url="http://www.wearefla.com";
var title="All Buyers and Sellers Realty of Florida-Ocala Real Estate";
document.write('<A HREF="javascript:window.external.AddFavorite(url,title)');
document.write('"><b><font face=arial color=#0000FF size=3>Click here to add us to your favorites!<\/font><\/a><\/b>');
}
else {
var alt = "<font color=#0000FF face=arial size=none>Add us to your favorites! Click here!<\/font><BR>";
if(navigator.appName == "Netscape") alt += "Press (Ctrl-D) on your keyboard.<\/font>";
document.write(alt);
}
}
//end add to favorites script