//div collapse
function startSearch(obj1, obj2) {
	var o1 = document.getElementById(obj1);
	var o2 = document.getElementById(obj2);
	var button = document.getElementById('start_search_button');
	
	if ( o1.style.display != 'none' ) {
		o1.style.display = 'none';
		o2.style.display = 'block';
		button.style.display = 'none';
	}
	else {
		o1.style.display = 'block';
		o2.style.display = 'none';
	}//end if else
}//end function


function switchFeatured(obj1, obj2) {
	var pano = document.getElementById(obj1);
	var pics = document.getElementById(obj2);
	
	if (pano.style.width != '776px') {
		pano.style.display = '';
		pano.style.width = '776px';
		pano.style.height = '291px';
		pics.style.display = 'none';
	}
	else{
		pano.style.display = 'none';
		pano.style.width = '320px';
		pano.style.height = '213px';
		pics.style.display = '';
	}//end if else
}//end function


function switchIt(obj1, obj2, obj3, type, lat , long) {
	var pano = document.getElementById(obj1);
	var pics = document.getElementById(obj2);
	var map = document.getElementById(obj3);
		
	var zoom = 0;
	
	if(type =='pics'){
		
		zoom = 1;
		
		pics.style.display = '';
		pano.style.display ='';
		map.style.display = 'none'; 
		pano.style.width = '457px';
		pano.style.height = '248px';
		pano.style.left = '-1px';
		pano.style.top = '56px';
		pano.style.border = '1px #FFF solid';
		
		
		//init pano
		
		initializePano();	
		
	}else if(type =='map'){
		status = 'map';
		map.style.display = '';
		pano.style.display = 'none';
		pics.style.display = 'none';
		initializeMap(lat, long);
		
	}else if(type =='pano'){
		//is map
		if(zoom = '1'){			
			map.style.display = 'none';
			pics.style.display = 'none';
			pano.style.display = '';
			pano.style.width = '776px';
			pano.style.height = '248px';
			pano.style.left = '0px';
			pano.style.top = '56px';
			pano.style.paddingRight = '0px';
			initializePano();		
		}
		
	}else{alert("A virtual tour is not available for this property.");}
}//end function


//////////////////////////////////////////////////////////////////////////////////////
function initializeMap(lat, long){
	if (GBrowserIsCompatible()) {
       	var map = new GMap2(document.getElementById("map_lrg"));
		map.setCenter(new GLatLng(lat, long),12);
		map.setMapType(G_HYBRID_MAP); 
      	//alert("lat: "+ lat + "long: " + long);
		var icon = new GIcon();
    	icon.image = "/images/marker.png";
    	icon.iconSize = new GSize(32.0, 32.0);
        icon.iconAnchor = new GPoint(29,26);
		icon.infoWindowAnchor = new GPoint(9,2);
	  
		var point = new GLatLng(lat, long);
		//map.addOverlay(new GMarker(point));
	  	map.addOverlay(new GMarker(point, icon));
	}// end if
}


////////////////////////////////////////////////////////////////////////////////////////
//handle browser errors
function handleNoFlash(errorCode) {
	if (errorCode == FLASH_UNAVAILABLE) {
    	alert("Error: Flash doesn't appear to be supported by your browser");
        return;
	}//end if
}//end function  
/////////////////////////////////////////////////////////////////////////////////////////



//FORM FUNCTIONS
function cleanForm(){
	for (var i = 0; i < $('IDX-formSelector').elements.length; i++){
		
		if ($('IDX-formSelector').elements[i].value == '' && $('IDX-formSelector').elements[i].type != 'select-multiple'){
			$('IDX-formSelector').elements[i].disabled = true;
		} // end if
	} // end for

	$('lowPrice').value = $('lowPrice').value.replace(/\D/g,"");
	$('highPrice').value = $('highPrice').value.replace(/\D/g,"");
	return true;
} // end disable empty

function selectForm(form){
		
	if (form == "basic"){
		document.getElementById("IDX-mainSearchForm").style.display = 'block';
		document.getElementById("IDX-formSelector").style.top = '55px';
		document.getElementById("IDX-buySellRent").style.top = '55px';
		document.getElementById("IDX-propertyType").style.top = '50px';
		document.getElementById("IDX-location").style.top = '50px';
		document.getElementById("IDX-minPrice").style.top = '110px';
		document.getElementById("IDX-maxPrice").style.top = '150px';
		document.getElementById("IDX-bedRooms").style.top = '110px';
		document.getElementById("IDX-bathRooms").style.top = '150px';
		document.getElementById("IDX-sqFt").style.top = '110px';
		document.getElementById("IDX-searchSortByPrice").style.top = '150px';
		document.getElementById("IDX-Submit").style.top = '190px';
		document.getElementById("IDX-Reset").style.top = '190px';				
		
		document.getElementById("IDX-buildingName").style.display = 'none';
		document.getElementById("IDX-schools").style.display = 'none';
		document.getElementById("IDX-views").style.display = 'none';
		document.getElementById("IDX-addressSearchForm").style.display = 'none';
		document.getElementById("IDX-listingSearchForm").style.display = 'none';
	}
	if (form == "advanced"){	
		document.getElementById("IDX-mainSearchForm").style.display = 'block';
		document.getElementById("IDX-formSelector").style.top = '25px';
		document.getElementById("IDX-buySellRent").style.top = '30px';
		document.getElementById("IDX-propertyType").style.top = '20px';
		document.getElementById("IDX-location").style.top = '20px';
		document.getElementById("IDX-minPrice").style.top = '80px';
		document.getElementById("IDX-maxPrice").style.top = '120px';
		document.getElementById("IDX-bedRooms").style.top = '80px';
		document.getElementById("IDX-bathRooms").style.top = '120px';
		document.getElementById("IDX-sqFt").style.top = '80px';
		document.getElementById("IDX-searchSortByPrice").style.top = '120px';
		document.getElementById("IDX-Submit").style.top = '160px';
		document.getElementById("IDX-Reset").style.top = '160px';
				
		document.getElementById("IDX-buildingName").style.display = 'block';
		document.getElementById("IDX-schools").style.display = 'block';
		document.getElementById("IDX-views").style.display = 'block';
		document.getElementById("IDX-addressSearchForm").style.display = 'none';
		document.getElementById("IDX-listingSearchForm").style.display = 'none';
	}
	if (form == "address"){
		document.getElementById("IDX-formSelector").style.top = '55px';
		document.getElementById("IDX-mainSearchForm").style.display = 'none';
									
		document.getElementById("IDX-addressSearchForm").style.display = 'block';
		document.getElementById("IDX-listingSearchForm").style.display = 'none';
	}
	if (form == "listing"){
		document.getElementById("IDX-mainSearchForm").style.display = 'none';
		document.getElementById("IDX-addressSearchForm").style.display = 'none';
		document.getElementById("IDX-listingSearchForm").style.display = 'block';
	}
}
////////////////////////////////////////////////////////////////////////////////////////////
//FORM VALIDATION
function checkForm(myName, myLast, myEmail, myPhone){
	
  if (this.name.value.length == 0 || this.last.value.length == 0 || this.email.value.length == 0 || this.phone.value.length == 0){
    alert('Please fill all required fields.');
	this.name.focus();
	return (false);
  }
  else{alert('Your information was submitted.  Please expect to hear from one of our agents shortly.')}
//onsubmit="return checkForm();" 
}

function checkForm_contact(myName, myEmail, myPhone, myNotes){

	var newName = trim(myName);
    var newEmail = trim(myEmail);
	var isValid = validate(newEmail);
	var newPhone = trim(myPhone);
	var newNotes = trim(myNotes);
	
	if(newName == '' || newEmail == '' || newPhone == '' || newNotes == ''){
		alert('Please fill out the form completely.');
		return false;
	}
	else if(! isValid){
		alert('Please enter a valid email address.');
		return false;
	}
	else{
		alert('Thank you, an MRE representative will be contacting you shortly.');
		return true;
	}
}

function checkForm2(myName, myLast, myEmail, myPhone){

	var newName = trim(myName);
    var newLast = trim(myLast);
	var newEmail = trim(myEmail);
	var isValid = validate(newEmail);
	var newPhone = trim(myPhone);
	
	if(newName == '' || newLast == '' || newEmail == '' || newPhone == ''){
		alert('Please fill out the form completely.');
		return false;
	}
	else if(! isValid){
		alert('Please enter a valid email address.');
		return false;
	}
	else{
		alert('Thank you, an MRE representative will be contacting you shortly.');
		return true;
	}
}

function validate(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
			return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}


///////////////////////////////////////////////////////////////////////////////////////////
//Map Search
function mapSearch (){
   var path="http://www.miamisrealestate.idxco.com/idx/3134/mapSearch.php";
   window.location=path;
}