function popWindow(url, name, width, height) {
	var x = (screen.width - width)/2;
	var y = (screen.height - height)/2;
    var opts = 'height=' + height + ',width=' + width + ",screenX=" + x + ",left=" + x + ",screenY=" + y + ",top=" + y + ',location=no,scrollbars=yes,menubar=no,resizable=no,status=no,toolbar=no';

    var newWindow = window.open(url, name, opts);
    newWindow.focus();

} 

if(document.addEventListener){ 
	document.addEventListener("keypress", HandleEnterKey, true); 
} 
else{ 
	document.attachEvent("onkeypress", HandleEnterKey); 
} 


// Handle the enter key for a section of a form, binding it to the provided submit buton 
function HandleEnterKey(event) { 
	var nav = window.Event ? true : false; 
	if (nav) { 
		return NetscapeEventHandler_KeyDown(event); 
	} else { 
		return MicrosoftEventHandler_KeyDown(); 
	} 
} 

function NetscapeEventHandler_KeyDown(e) { 
	if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { 
		e.returnValue = false; 
		e.cancel = true; 
		e.preventDefault(); 
		var att = e.target.attributes['SubmitControl']; 
			if(att!=null) 
				CallSubmit(att.value) 
				return false; 
	} 
	return true; 
} 

function MicrosoftEventHandler_KeyDown() { 
	if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') { 
		event.returnValue = false; 
		event.cancel = true; 
		var att = event.srcElement.attributes['SubmitControl']; 
			if(att!=null) 
				CallSubmit(att.value) 
		return false; 
	} 
	return true; 
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function checkContactForm(frm){

  var bAddressRequired = (frm.contactvia[0].checked);
  var bEmailRequired = (frm.contactvia[1].checked);
  var bPhoneRequired = (frm.callme[0].checked || frm.callme[1].checked || frm.callme[2].checked );
  var ErrorMsg = 'The following fields are required:\n\n';
  var testErrorMsg = ErrorMsg;

  if (trim(frm.name.value) == ''){
  	ErrorMsg += 'Name\n'
  }
  
  if (bAddressRequired && ( (trim(frm.address.value) == '') || (trim(frm.city.value) == '') || (trim(frm.state.value) == '') || (trim(frm.zip.value) == '')) ){
  	ErrorMsg += 'Complete Mailing Address\n'
  }
  
  if (bEmailRequired && (trim(frm.email.value) == '')){
  	ErrorMsg += 'Email Address\n'
  }
  
  if (bPhoneRequired  && (trim(frm.phone.value) == '')){
  	ErrorMsg += 'Phone Number\n'
  }

  if (ErrorMsg != testErrorMsg){
  	alert(ErrorMsg);
  } else {
  	frm.submit();
  }

}

function checkEstimateForm(frm){
  var ErrorMsg = 'The following fields are required:\n\n';
  var testErrorMsg = ErrorMsg;
  
  if (trim(frm.firstname.value) == ''){
  	ErrorMsg += 'First Name\n'
  }
  
  if (trim(frm.lastname.value) == ''){
  	ErrorMsg += 'Last Name\n'
  }

  var bHaveMethodOfContact = false;
  for (i=0;i<4;i++){
       bHaveMethodOfContact = (bHaveMethodOfContact || frm.contactvia[i].checked)
  }
  if (!bHaveMethodOfContact){
  	ErrorMsg += 'Method by which we can contact you.\n'
  }

  var bPhoneRequired = (frm.contactvia[0].checked || frm.contactvia[3].checked);
  var bAddressRequired = (frm.contactvia[1].checked);
  var bEmailRequired = (frm.contactvia[2].checked);

  //if (bAddressRequired && ( (trim(frm.address.value) == '') || (trim(frm.city.value) == '') || (trim(frm.state.value) == '') || (trim(frm.zip.value) == '')) ){
  //Always validate mailing address
   if ( (trim(frm.address.value) == '') || (trim(frm.city.value) == '') || (trim(frm.state.value) == '') || (trim(frm.zip.value) == '')) {
  	ErrorMsg += 'Complete Mailing Address\n'
  }
  
  //if (bEmailRequired && (trim(frm.email.value) == '')){
  //Always validate email address
    if (trim(frm.email.value) == ''){
  	ErrorMsg += 'Email Address\n'
  }
    if (isEmail(frm.email.value) == false){
		ErrorMsg += 'Valid Email Address\n'
  }  
  
  //if (bPhoneRequired  && (trim(frm.phone.value) == '')){
  //Always validate phone number
  if (trim(frm.phone.value) == ''){
  	ErrorMsg += 'Phone Number\n'
  }
  
  bHaveMoveInBy = false;
  for(i=0;i<3;i++){
  	bHaveMoveInBy = (bHaveMoveInBy || frm.movein[i].checked)
  }
  if (!bHaveMoveInBy){
  	ErrorMsg += 'Move-in date\n'
  }
  
  var bHaveCommunitySelected = false;
  for (i=0;i<6;i++){
  	bHaveCommunitySelected = (bHaveCommunitySelected || frm.community[i].checked)
  }
  if (!bHaveCommunitySelected){
  	ErrorMsg += 'Community of Interest\n'
  }

  var bHaveFloorPlanSelected = false;
  for (i=0;i<21;i++){
  	bHaveFloorPlanSelected = (bHaveFloorPlanSelected || frm.floorplan[i].checked)
  }
  if (!bHaveFloorPlanSelected){
  	ErrorMsg += 'Floor plan of Interest\n'
  }

  if (ErrorMsg != testErrorMsg){
  	alert(ErrorMsg);
  } else {
  	frm.submit();
  }

}

function trim(str){
  return str.replace(/^\s*|\s*$/g,"");
}