/* =================================== Pass MLS# =================================== */
var mlsNumber = null;
function passMLS()	{
if (location.search.length > 0)
    mlsNumber = location.search.substring(1);
	document.forms[0].mls.value = mlsNumber;
}

/* =================================== Zip Code String =================================== */

function MLSField() {
		if(document.MLSform.mlsNumber.value=='') {
			makeString();
			document.searchMLS.submit();
		}
		else {
			document.MLSform.submit();
		}
}

function makeString() {
var message = ""

	for (var i = 0; i<document.subs.elements.length; i++) {
		//if ((document.subs.elements[i].name.indexOf('zipcode') > -1)) {
			if (document.subs.elements[i].checked) {
				message = message + document.subs.mapcoords[i].value + ","
				
				
			//}
		}
	}
	searchMLS.mapcoords.value = message.substring(0 , message.length-1);
}

/* =================================== Checkbox Select All s=================================== */

function uncheckparking() {
	document.searchMLS.elements['parking1[]'][0].checked = false; 

}

function uncheckAllParking() {
	document.searchMLS.elements['parking1[]'][1].checked = false;
	document.searchMLS.elements['parking1[]'][2].checked = false;
	document.searchMLS.elements['parking1[]'][3].checked = false;
	document.searchMLS.elements['parking1[]'][4].checked = false;
	document.searchMLS.elements['parking1[]'][5].checked = false;
}

function uncheckview() {
	document.searchMLS.anyView.checked = false;
}

function uncheckAllView() {
	document.searchMLS.ocean.checked = false;
	document.searchMLS.bay.checked = false;
	document.searchMLS.panoramic.checked = false;
	document.searchMLS.golfcourse.checked = false;
	document.searchMLS.mountains.checked = false;
}

function checkSelectAll() {
	document.sub.selectAll.checked = false;
}

function checkAll() {
	for (i=0,n=document.subs.elements.length;i<n;i++)
		if (document.subs.elements[i].name.indexOf('mapcoords') !=-1)
			document.subs.elements[i].checked = true;
}

function uncheckAll() {
	for (i=0,n=document.subs.elements.length;i<n;i++)
		if (document.subs.elements[i].name.indexOf('mapcoords') !=-1)
			document.subs.elements[i].checked = false;
}

/* =================================== Form Roll Over Buttons =================================== */

function rollOver(obj, new_style) {
    obj.className = new_style;
}

/* =================================== Redirect Link =================================== */
   
function goto_URL(object) {

	window.top.location.href=object;

}

/* =================================== Pop Up Window =================================== */

function popUp(fileName) {
	myFloater = window.open('','myWindow','scrollbars=yes,status=no,width=650,height=600,resizable=yes');
	myFloater.location.href = fileName;
}

/* =================================== Validate Form =================================== */

function isEmailAddr(email) {
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");

	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}

	return result;
}

function validRequired(formField,fieldLabel) {
	var result = true;

	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	return result;
}

function validEmail(formField,fieldLabel,required) {
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
	
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}

	return result;
}


function validateForm() {
	
	var z='contact';
	var y='erealestatelajolla.com';

	var x=z+'@'+y;
	
	document.forms[0].recipient.value=x;
	
	var okSoFar=true
	with (document.forms[0]) {
		
	if (document.forms[0].name.value=="" && okSoFar) {
		okSoFar=false
		alert("Please enter your name.")
		document.forms[0].name.focus()
	}	

	if (document.forms[0].friend.value=="" && okSoFar) {
		okSoFar=false
		alert("Please enter your friend's name.")
		document.forms[0].friend.focus()
	}	
	
	if (document.forms[0].thesubject) {
		if (document.forms[0].thesubject.value=="" && okSoFar) {
			okSoFar=false
			alert("Please enter the subject.")
			document.forms[0].thesubject.focus()
		}
	}
	if (document.forms[0].address) {
		if (document.forms[0].address.value=="" && okSoFar) {
			okSoFar=false
			alert("Please enter the address.")
			document.forms[0].address.focus()
		}
	}
	if (document.forms[0].city) {
		if (document.forms[0].city.value=="" && okSoFar) {
			okSoFar=false
			alert("Please enter the city.")
			document.forms[0].city.focus()
		}
	}
	if (document.forms[0].state) {
		if (document.forms[0].state.value=="" && okSoFar) {
			okSoFar=false
			alert("Please enter the state.")
			document.forms[0].state.focus()
		}
	}
	if (document.forms[0].phone1) {
		if (document.forms[0].phone1.value=="" && okSoFar) {
			okSoFar=false
			alert("Please enter the phone number.")
			document.forms[0].phone1.focus()
		}
	}
				
	var checkEmail = document.forms[0].email.value.indexOf("@",0)
	if (checkEmail < 1 && okSoFar) {
		okSoFar = false
		alert ("Please enter a valid email address.")
		document.forms[0].email.focus()
	}
	
	var checkFriendEmail = document.forms[0].friendmail.value.indexOf("@",0)
	if (checkFriendEmail < 1 && okSoFar) {
		okSoFar = false
		alert ("Please enter a valid email address for your friend.")
		document.forms[0].friendmail.focus()
	}
	
	if (okSoFar==true)  submit();
 }
}

/* =================================== Mortgage Calculator =================================== */

function MCalc()
{ form=document.CalcForm;
  Loan=form.Loan.value;
  Down=form.Down.value;
  Rate=form.Rate.value/100;
  Years=form.Years.value;
  if (Down==""){Down="0"}
  MonthlyRate=Rate/12;
  NmbrPayments=Years*12;
  Principal=Loan-Down;
  MonthlyPayment=Math.floor((Principal*MonthlyRate)/(1-Math.pow((1+MonthlyRate),(-1*NmbrPayments)))*100)/100;
  MonthlyPaymentAT=Math.floor(MonthlyPayment*1.2*100)/100

  form.MonthlyPaymentAT.value=MonthlyPaymentAT;
  form.MonthlyPayment.value=MonthlyPayment; }

function Allow()
{ 
  form=document.AllowForm;
  Agrinc=form.Agrinc.value;
  Mgrinc=Agrinc/12;
  MexpB=Mgrinc*.28
  MaxallowA=Mgrinc*.36

  Child=form.child.value;
  Auto=form.auto.value;
  Asstn=form.asstn.value;
  Other=form.other.value;
  Obligations= parseInt(Child) + parseInt(Auto) + parseInt(Asstn) + parseInt(Other);

  MexpA=MaxallowA-Obligations;

  if (MexpA < MexpB) 
  {
	Mexp=MexpA;
  } 
  else 
  {
	Mexp=MexpB;
  } 
  Mexp = Mexp * 100 / 1.2;
  Mexp = Math.round(Mexp);
  Mexp = Mexp / 100;
  form.Mexp.value = Mexp;


  MexpAT = Mexp / 1.2;
  Down=form.Down.value;
  Rate=form.Rate.value/100;
  Years=form.Years.value;
  if (Down==""){Down="0"}
  MonthlyRate=Rate/12;
  NmbrPayments=Years*12;
  
  Principal=((1-Math.pow((1+MonthlyRate),(-1*NmbrPayments)))*MexpAT)/MonthlyRate;
  PrincipalAT=Math.floor(Principal*100)/100
  Principal=PrincipalAT

  Loan = parseInt(Principal) + parseInt(Down);  
  form.Loan.value = Loan;

}