var isIE4 = false;
var cache;
	
if(navigator.appName.indexOf("Microsoft") != -1  &&  parseInt(navigator.appVersion) >= 4)
 isIE4 = true;
	
function ReadDollarDecision(whichVal) {
	dwnPayFld 	= document.frmCalc.downpay;
	price 	= document.frmCalc.price;
	
	if(whichVal < ReadDollarField(dwnPayFld)) { 
		dwnPayFld.value = price.value; 
	}
}
function printWindow() {
	window.print();
}
function checkCMA(){   

	firstname = trimSpaces(document.cmaform.firstname.value);
	lastname = trimSpaces(document.cmaform.lastname.value); 
	address = trimSpaces(document.cmaform.address.value); 
	city = trimSpaces(document.cmaform.city.value); 
	state = trimSpaces(document.cmaform.state.value); 
	zipcode = trimSpaces(document.cmaform.zipcode.value); 
	contactemail = trimSpaces(document.cmaform.email.value); 
	if(firstname.length <= 0){
		alert("Please enter your first name.");
		document.cmaform.firstname.focus();
		return false;
	}
	
	if(lastname.length <= 0){
		alert("Please enter your last name.");
		document.cmaform.lastname.focus();
		return false;
	}
	
	if(!checkEmail(contactemail)) {
		document.cmaform.email.focus();
		return false;
	}
	if(address.length <= 0){
		alert("Please enter your Address");
		document.cmaform.address.focus();
		return false;
	}
	
	if(city.length <= 0){
		alert("Please enter your City.");
		document.cmaform.city.focus();
		return false;
	}
	if(state.length <= 0){
		alert("Please enter your State.");
		document.cmaform.state.focus();
		return false;
	}
	
	if(zipcode.length <= 0){
		alert("Please enter Zip code.");
		document.cmaform.zipcode.focus();
		return false;
	}

	
		phone1 = trimSpaces(document.cmaform.phone1.value);
		phone2 = trimSpaces(document.cmaform.phone2.value);
		phone3 = trimSpaces(document.cmaform.phone3.value);
		if(phone1.length <= 0){
			alert("Please enter Phone.");
			document.cmaform.phone1.focus();
			return false;
		}
		if(phone2.length <= 0){
			alert("Please enter Phone.");
			document.cmaform.phone2.focus();
			return false;
		}
		if(phone3.length <= 0){
			alert("Please enter Phone.");
			document.cmaform.phone3.focus();
			return false;
		}
	document.cmaform.frmAction.value = "yes";
}
function CheckFloatField(field) {
   var val = field.value;
	
   // lop off trailing "0"s after a decimal point first
   if(val.indexOf(".") != -1) {
      while(val.charAt(val.length-1) == "0")
         val = val.substring(0,val.length-1);
      if(val.charAt(val.length-1) == ".")
         val = val.substring(0,val.length-1);
   }
	
   if("" + parseFloat(val) != val)
      field.value = field.defaultValue;
}
function CheckIntField(field) {
   var val = field.value;
   if("" + parseInt(val) != val)
      field.value = field.defaultValue;
}
	
	
function CheckDollarField(field) {
   var flt = ReadDollarField(field);
   if(isNaN(flt))
      field.value = cache;
   else {
      str = FloatToDollarString(flt);
      field.value = str;
   }
}
	
	
function ReadDollarField(field) {
   var str = field.value;
   if(str.charAt(0) == "$")
      str = str.substring(1, str.length);
	
   var pos = str.lastIndexOf(",");
   while(pos != -1) {
      str = str.substring(0,pos) + str.substring(pos+1, str.length);
      pos = str.lastIndexOf(",", pos);
   }
	
   return parseFloat(str);
}
	
function FloatToDollarString(flt) {
   // round off to nearest dollar
   var str = "" + Math.round(flt);
	
   // add commas
   pos = str.length;  // str.indexOf(".");
   pos -= 4;
   while(pos >= 0) {
      str = str.substring(0,pos+1) + "," + str.substring(pos+1, str.length);
      pos -= 3;
   }
	
   return str;
}

	
	
function recalcTermMonths(frm) {
   var tYr = parseFloat(frm.termYears.value);
   var tMon = Math.round(tYr * 12.0);
   tYr = parseFloat(tMon) / 12.0;
   frm.termYears.value = "" + tYr;
   frm.termMonths.value = "" + tMon;
}
	
	
function recalcTermYears(frm) {
   var tMon = parseInt(frm.termMonths.value);
   var tYr = parseFloat(tMon) / 12.0;
   frm.termYears.value = "" + tYr;
   frm.termMonths.value = "" + tMon;
}
	
	
function RecalcMonthlyPay(frm) {
   var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
   var AnnualInt  = parseFloat(frm.intYear.value);
   var MonthlyInt = AnnualInt / (12.0 * 100.0);
   var LenMonths  = parseInt(frm.termMonths.value);
	
   if(MonthlyInt == 0)
      var MonthlyPay = Principle / LenMonths;
   else
      var MonthlyPay = Principle * ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );
   MonthlyPay = Math.round(MonthlyPay * 100) / 100;
	
   frm.payMonth.value = FloatToDollarString(MonthlyPay);
}
	
	
function RecalcDownPay(frm) {
   var AnnualInt  = parseFloat(frm.intYear.value);
   var MonthlyInt = AnnualInt / (12.0 * 100.0);
   var LenMonths  = parseInt(frm.termMonths.value);
   var MonthlyPay = ReadDollarField(frm.payMonth);
   var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
   var OldDownPay = ReadDollarField(frm.downpay);
   var EffPrinciple
	
   if(MonthlyInt == 0)
      EffPrinciple = MonthlyPay * LenMonths;
   else
      EffPrinciple = MonthlyPay * ((1 - Math.pow((1 + MonthlyInt), -LenMonths)) / MonthlyInt);
	
   var NewDownPay = OldDownPay + (Principle - EffPrinciple);
   frm.downpay.value = "" + NewDownPay;
   CheckDollarField(frm.downpay);
	
   RecalcDownPayPerc(frm);
   RecalcMonthlyPay(frm);
}
	
	
function RecalcDownPayPerc(frm) {
   var HomePrice  = ReadDollarField(frm.price);
   var DownPay = ReadDollarField(frm.downpay);
   var DownPayPerc = 100 * DownPay / HomePrice;
	
   if(DownPayPerc >= 0  &&  DownPayPerc <= 100) {
      var DownPayPercStr = "" + DownPayPerc;
	
      var pos = DownPayPercStr.indexOf(".");
      if(DownPayPercStr.length > pos + 4)
         DownPayPercStr = DownPayPercStr.substring(0,pos+4);
	
      frm.downpayperc.value = DownPayPercStr;
   }
   else if(DownPayPerc < 0) {
      frm.downpayperc.value = "0";
      RecalcDownPayAmount(frm);
   }
   else {
      frm.downpayperc.value = "100";
      RecalcDownPayAmount(frm);
   }
}
	
	
function RecalcDownPayAmount(frm) {
   var HomePrice  = ReadDollarField(frm.price);
   var DownPayPerc = parseFloat(frm.downpayperc.value);
   if(DownPayPerc < 0) {
      frm.downpayperc.value = "0";
      RecalcDownPayAmount(frm)
   }
   else if(DownPayPerc > 100) {
      frm.downpayperc.value = "100";
      RecalcDownPayAmount(frm)
   }
   else {
      var DownPay = HomePrice * DownPayPerc / 100;
      DownPay = FloatToDollarString(DownPay);
      frm.downpay.value = "" + DownPay;
   }
}

function showNews(newsId, pageType) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 550;
	winHeight = 400;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	if(pageType == "sub") {
		window.open("newsDetails.php?newsid=" + newsId, "newsFrame", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	}
	else {
		window.open("php/newsDetails.php?newsid=" + newsId, "newsFrame", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	}
}

function openCustomWindow(pageURL) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 550;
	winHeight = 400;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	window.open(pageURL, "custFrame", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
}

function showFullPicture(imgURL) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 550;
	winHeight = 400;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	window.open("../images/" + imgURL, "imgFrame", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
}

function showPhoto(field,houseid) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 600;
	winHeight = 550;
	
	sLeft = 100;
	sTop = 10;

	window.open("showPicture.php?field=" + field + "&houseid=" + houseid, "picture", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	
	return;
}


function checkFeedBack() {
	document.forms[0].visitorName.value=trimSpaces(document.forms[0].visitorName.value);
	if(document.forms[0].visitorName.value.length <= 0) {
		alert("Please enter your name");
		document.forms[0].visitorName.focus();
		return false;
	}

	document.forms[0].emailAddress.value = trimSpaces(document.forms[0].emailAddress.value);
	if(document.forms[0].emailAddress.value.length <= 0) {
		alert("Please enter your email address");
		document.forms[0].emailAddress.focus();
		return false;
	}
	if(!checkEmail(document.forms[0].emailAddress.value)) {
		document.forms[0].emailAddress.focus();
		document.forms[0].emailAddress.select();
		return false;
	}
	document.forms[0].visitorComments.value = trimSpaces(document.forms[0].visitorComments.value);
	if(document.forms[0].visitorComments.value.length <= 0) {
		alert("Please enter your comments");
		document.forms[0].visitorComments.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function checkRequestShow(){
	name = trimSpaces(document.forms[0].name.value);
	subject = trimSpaces(document.forms[0].subject.value);
	email = trimSpaces(document.forms[0].email.value);
	phone = trimSpaces(document.forms[0].phone.value);
	comments = trimSpaces(document.forms[0].comments.value);

	if(subject.length <= 0){
		alert("Please enter subject.");
		document.forms[0].subject.focus();
		return false;
	}	
	if(name.length <= 0){
		alert("Please enter your name.");
		document.forms[0].name.focus();
		return false;
	}
	if(phone.length <= 0){
		alert("Please enter your Phone #.");
		document.forms[0].phone.focus();
		return false;
	}
	email = trimSpaces(document.forms[0].email.value);
	if(email.length <= 0) {
		alert("Please enter your email.");
		document.forms[0].email.focus();
		return false;
	}
	if(email.length > 0){
		if(!checkEmail(email)){
			document.forms[0].email.focus();
			return false;
		}
	}	
	if(comments.length <= 0){
		alert("Please enter your comments.");
		document.forms[0].comments.focus();
		return false;
	}
	
	document.forms[0].frmAction.value = "yes";
	document.forms[0].submit();
}

function openWindow(url,window_name,winWidth,winHeight,fscroll) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;

	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;

	if(fscroll == '') {fscroll = 0}
	window.open(url,window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable=0");
}
function checkEmailProperty(){
	toName = trimSpaces(document.forms[0].toName.value);
	toEmail = trimSpaces(document.forms[0].toEmail.value);
	fromName = trimSpaces(document.forms[0].fromName.value);
	fromEmail = trimSpaces(document.forms[0].fromEmail.value);
	subject = trimSpaces(document.forms[0].subject.value);

	if(toName.length <= 0){
		alert("Please enter Sender Name.");
		document.forms[0].toName.focus();
		return false;
	}
	if(toEmail.length <= 0) {
		alert("Please enter Sender Email.");
		document.forms[0].toEmail.focus();
		return false;
	}
	if(toEmail.length > 0){
		if(!checkEmail(toEmail)){
			document.forms[0].toEmail.focus();
			return false;
		}
	}
	if(fromName.length <= 0){
		alert("Please enter your Name.");
		document.forms[0].fromName.focus();
		return false;
	}
	if(fromEmail.length <= 0) {
		alert("Please enter your Email.");
		document.forms[0].fromEmail.focus();
		return false;
	}
	if(fromEmail.length > 0){
		if(!checkEmail(fromEmail)){
			document.forms[0].fromEmail.focus();
			return false;
		}
	}	


	if(subject.length <= 0){
		alert("Please enter Subject.");
		document.forms[0].subject.focus();
		return false;
	}	

	document.forms[0].frmAction.value = "yes";
	document.forms[0].submit();
}
function checkContact(){
	contactname = trimSpaces(document.forms[0].contactname.value);
	contactemail = trimSpaces(document.forms[0].contactemail.value);
	contactphone = trimSpaces(document.forms[0].contactphone.value);
	suggestions = trimSpaces(document.forms[0].suggestions.value);

	if(contactname.length <= 0){
		alert("Please enter your name.");
		document.forms[0].contactname.focus();
		return false;
	}
	if(!checkEmail(contactemail)) {
		document.forms[0].contactemail.focus();
		return false;
	}
	if(contactphone.length <= 0){
		alert("Please enter your telephone #.");
		document.forms[0].contactphone.focus();
		return false;
	}
	if(suggestions.length <= 0){
		alert("Please enter your queries/suggestions.");
		document.forms[0].suggestions.focus();
		return false;
	}
	
	document.forms[0].frmAction.value = "yes";

}



