// BEGIN DOM
$(function()
{
	$('#nav').droppy({speed: 10});
	//$("#rhr_list").accordion();
	//$(document).pngFix();


	if ( ($.cookie('ufn') != null))
	{
		$('#Name').attr("value",$.cookie('ufn'));
	}
	// Lets get the information from the name field.
	$('#Name').one("blur", function() {
		var $Name = '';	
		$Name = $(this).val();
		$.cookie('ufn',$Name,{ expires: 7, path: '/'});
	});
	
	if ( ($.cookie('uPhone') != null))
	{
		$('#Phone').attr("value",$.cookie('uPhone'));
	}
	
	// Lets get the information from the name field.
	$('#Phone').one("blur", function() {
		var $Phone = '';	
		$Phone = $(this).val();
		$.cookie('uPhone',$Phone,{ expires: 7, path: '/'});
	});
	
	if ( ($.cookie('uMail') != null))
	{
		$('#UserEmail').attr("value",$.cookie('uMail'));
	}
	// Lets get the information from the name field.
	$('#UserEmail').one("blur", function() {
		var $UEmail = '';	
		$UEmail = $(this).val();
		$.cookie('uMail',$UEmail,{ expires: 7, path: '/'});
	});
	
	if ( ($.cookie('uLow') != null))
	{
		var cookieValue = $.cookie('uLow')
		var infoValue = $("#MIN_LIST_PRICE option").val();
		var foo = [];
		$('#MIN_LIST_PRICE option').each(function(i, option){
			foo[i] = $(option).val();
			if (foo[i] == cookieValue)
			{
				$(this).attr("selected","selected");
			}
		});
	}
	// Lets get the information for LOW price
	$('#MIN_LIST_PRICE').change(function() {
		var $selected = $("#MIN_LIST_PRICE option:selected").val(); 
		$.cookie('uLow',$selected,{ expires: 7, path: '/'});
	});
	if ( ($.cookie('uHigh') != null))
	{
		var cookieValue = $.cookie('uHigh')
		var infoValue = $("#MAX_LIST_PRICE option").val();
		var foo = [];
		$('#MAX_LIST_PRICE option').each(function(i, option){
			foo[i] = $(option).val();
			if (foo[i] == cookieValue)
			{
				$(this).attr("selected","selected");
			}
		});
	}
	// Lets get the information for HIGH price
	$('#MAX_LIST_PRICE').change(function() {
		var $selected = $("#MAX_LIST_PRICE option:selected").val(); 
		$.cookie('uHigh',$selected,{ expires: 7, path: '/'});
	});
	// when the form is submitted, we erase the cookies	
	$("#searchFormSend").submit(function() {
		$.cookie('ufn', null);
		$.cookie('uPhone', null);
		$.cookie('uMail', null);
		$.cookie('uLow', null);
		$.cookie('uHigh', null);
        return true;
    });	


	$('#selBlogCategory').change(function(){
						  	
		if ($(this).val()=='real-estate')
		{
			$('#services-sub-category').hide();
			$('#real-estate-rent-sale').show();
			$('#real-estate-property-type').show();
		}
		else if ($(this).val()=='services')
		{
			$('#services-sub-category').show();
			$('#real-estate-rent-sale').hide();
			$('#real-estate-property-type').hide();
		}
		else
		{
			$('#services-sub-category').hide();
			$('#real-estate-rent-sale').hide();
			$('#real-estate-property-type').hide();
		}
	});
	
	// VALIDATE : ADD BLOG
	$("#addBlogForm").validate({
		rules: {
			selBlogCategory: "required",
			//selBlogRentSale: "required",
			selBlogState: "required",
			//selBlogCounty: "required",
			//selBlogCity: "required",
			txtBlogTitle: "required",
			txtMetaDescription: "required",
			txtMetaKeywords: "required"
		},
		messages: {
			selBlogCategory: "[Required:]&nbsp;&nbsp;",
			//selBlogRentSale: "[Required:]&nbsp;&nbsp;",
			selBlogState: "[Required:]&nbsp;&nbsp;",
			//selBlogCounty: "[Required:]&nbsp;&nbsp;",
			//selBlogCity: "[Required:]&nbsp;&nbsp;",
			txtBlogTitle: "[Required:]&nbsp;&nbsp;",
			txtMetaDescription: "[Required:]&nbsp;&nbsp;",
			txtMetaKeywords: "[Required:]&nbsp;&nbsp;"
		}
	});
	

	/*
		------------------------
		THIS WAS COMMENTED OUT BECAUSE OF CHANGE TO THE HOMEPAGE
		LINKS FOR THE "FOR SALE" AND "FOR RENT" LINKS, BUT 
		DONT - DONT - DONT
		DELETE THIS JUST IN CASE THEY WANT THE TOGGLE BACK	
		------------------------
	$('a#ForSaleLinkHome').click(function() {
		$('#oneMapHomePage').show();
		$("#twoMapHomePage").hide();
		$('a#ForSaleLinkHome').addClass('selected');
		$('a#ForRentLinkHome').removeClass('selected');
		return false;
	  });
	$('a#ForRentLinkHome').click(function() {
		$('#oneMapHomePage').hide();
		$("#twoMapHomePage").show();
		$('a#ForRentLinkHome').addClass('selected');
		$('a#ForSaleLinkHome').removeClass('selected');
		return false;
	  });*/

	// Will go to the URL that is the value of the County drop down on the search form 
	$("#selFormCounty").change(function(){
		var infoValue = $("#selFormCounty :selected").val();
		location.href = infoValue;
	});
	// Will go to the URL that is the value of the Property Type drop down on the search form 
	$("#propertyType").change(function(){
		var infoValue = $("#propertyType :selected").val();
		location.href = infoValue;
	});
	// Will write a hidden field to the form table if the city dropdown was changed.	
	$("#selectCities").change(function(){
		var infoValue = $("#selectCities :selected").val();
		location.href = infoValue;
		//$('#City').replaceWith("<input type=\"hidden\" name=\"City\" id=\"City\" value=" + infoValue + " />");
	});
	
	// Used to get the value for Pre Qualify DD and then send it to the function outside the DOM
	$("#selPurposeOfLoan").change(onSelectChange);  
										
	
	$("a[href*=.pdf]").click(function()	{
		$(this).attr({"target":"_self"});
		return false;
	});
	
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
	
	$('a[id="externalLink"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

	
	// VALIDATE THE CHOOSE YOUR ACCOUNT FORM
	$("#FranchiseForm").validate({
		rules: {
			txtFirstName: "required",
			txtLastName: "required",
			txtEmailAddress: {
				required: true,
				email: true
			},			
			txtWorkPhone: "required"
		},
		messages: {
			txtFirstName: "<br/>[Required]Please Enter Your First Name",
			txtLastName: "<br/>[Required]Please Enter Your Last Name",
			txtEmailAddress: "<br/>[Required]Please Enter a Valid Email Address",
			txtWorkPhone: "<br/>[Required]Please Enter Your Work Phone Number"
		}
	});
	
	
	
	// VALIDATE THE CHOOSE YOUR ACCOUNT FORM
	$("#FranchiseFormNew").validate({
		rules: {
			txtFullName: "required",
			txtPhone: "required",
			txtEmailAddress: {
				required: true,
				email: true
			},			
			txtFranchiseArea: "required"
		},
		messages: {
			txtFullName: "<br/>[Required]Please Enter Your Full Name",
			txtPhone: "<br/>[Required]Please Enter your phone number",
			txtEmailAddress: "<br/>[Required]Please Enter a Valid Email Address",
			txtFranchiseArea: "<br/>[Required]Please Enter what area your interested in"
		}
	});
	
	
	
	// VALIDATE THE CHOOSE YOUR ACCOUNT FORM
	$("#ContactForm").validate({
		rules: {
			txtName: "required",
			txtEmailAddress: {
				required: true,
				email: true
			},			
			message: "required"
		},
		messages: {
			txtName: "<br/>[Required]Please Enter Your Name",
			txtEmailAddress: "<br/>[Required]Please Enter a Valid Email Address",
			message: "<br/>[Required]Please Enter Your Message"
		}
	});
	
	// LETS MASK SOME FIELDS FOR THE FIELD IN THE PRE-QUALIFY FORM
	$("#txtPhoneNumber").mask("(999) 999-9999");
	// VALIDATE THE CHOOSE YOUR ACCOUNT FORM
	$("#HomeEvalRequestForm").validate({
		rules: {
			txtFullName: "required",
			txtPhoneNumber: "required",
			txtEmailAddress: {
				required: true,
				email: true
			}			
		},
		messages: {
			txtFullName: "<br/>[Required]Please Enter Your Full Name",
			txtPhoneNumber: "<br/>[Required]Please Enter Your Phone Number",
			txtEmailAddress: "<br/>[Required]Please Enter a Valid Email Address"			
		}
	});
	
	// LETS MASK SOME FIELDS FOR THE FIELD IN THE PRE-QUALIFY FORM
	$("#txtDOB").mask("99/99/9999");
	$("#txtHomePhone").mask("(999) 999-9999");
	$("#txtWorkPhone").mask("(999) 999-9999");
	$("#txtCurrentZipcode").mask("99999");
	$("#txtPlannedZipcode").mask("99999");
	// VALIDATE THE CHOOSE YOUR ACCOUNT FORM
	$("#PreQualifyForm").validate({
		rules: {
			txtFullName: "required",
			txtEmailAddress: {
				required: true,
				email: true
			},			
			txtCurrentAddress: "required",
			txtCurrentZipcode: "required",
			txtPlannedAddress: "required",
			txtPlannedZipcode: "required",
			selTypeofProperty: "required",
			selPlannedUse: "required",
			txtDOB: "required"
		},
		messages: {
			txtFullName: "<br/>[Required]Please Enter Your Full Name",
			txtEmailAddress: "<br/>[Required]Please Enter a Valid Email Address",			
			txtCurrentAddress: "<br/>[Required]Please Enter your Current Address",
			txtCurrentZipcode: "<br/>[Required]Please Enter your Current ZipCode",
			txtPlannedAddress: "<br/>[Required]Please Enter your Planned Address",
			txtPlannedZipcode: "<br/>[Required]Please Enter your Planned ZipCode",
			selTypeofProperty: "<br/>[Required]Please Select Type of Property",
			selPlannedUse: "<br/>[Required]Please Select Plan of Use",
			txtDOB: "<br/>[Required]Please Enter your Date of Birth"
		}
		//$txtHomePhone --- might need this later
		//$txtWorkPhone --- might need this later
	});
	
	
	// SEARCH SCHOOLS VALIDATION
	$("#searchSchools").validate({
		rules: {
			selSchoolType: "required",
			selCity: "required"
		},
		messages: {
			selSchoolType: "<br/>[Required]Please Fill",
			selCity: "<br/>[Required]Please Fill"
		}
	});
	
	// SEARCH MOVERS VALIDATION
	$("#searchMovers").validate({
		rules: {
			selMovingType: "required",
			selCity: "required"
		},
		messages: {
			selMovingType: "<br/>[Required]",
			selCity: "<br/>[Required]"
		}
	});
	
	// SEARCH UTILITIES VALIDATION
	$("#searchUtilities").validate({
		rules: {
			selUtilType: "required",
			selCity: "required"
		},
		messages: {
			selUtilType: "<br/>[Required]",
			selCity: "<br/>[Required]"
		}
	});
	
	
	// SEARCH UTILITIES VALIDATION
	$("#FindOfficeForm").validate({
		rules: {
			officevalid: {
			required: function(){
			  return $('#countyOffice').val() || $('#citySelect').val()
			}
		  }
		},
		messages: {
			officevalid: "City or County is required.<br/><br/>"
		}
	});
	
	// SEARCH UTILITIES VALIDATION
	$("#FindAgentForm").validate({
		rules: {
			namevalid: {
			required: function(){
			  return $('#txtAgentName').val() || $('#citySelect').val()
			}
		  }
		},
		messages: {
			namevalid: "Agent Name or City is required.<br/><br/>"
		}
	});
	
	
	// VALIDATE THE SEARCH FORMS so that we get a city first
	$("#searchFormSendPre").validate({
		rules:{
			selectCities: "required"
		},
		messages:{
			selectCities: "<br/>[Required]"
		}
	});

	
	// SEARCH SCHOOLS VALIDATION
	$("#searchFormSend").validate({
		rules: {
			Name: "required",
			propertyType: "required",			
			UserEmail: {
				required: true,
				email: true
			},
			Phone: "required",
			selectCities: "required",
			MIN_LIST_PRICE: "required",
			MAX_LIST_PRICE: "required"
		},
		messages: {
			Name: "<br/>[Required]",
			propertyType: "<br/>[Required]",
			UserEmail: "<br/>[Required]",
			Phone: "<br/>[Required]",
			selectCities: "<br/>[Required]",
			MIN_LIST_PRICE: "<br/>[Required]",
			MAX_LIST_PRICE: "<br/>[Required]"
		}
	});
	
	
	

	// Lets Get the map going
	$("#map1").googleMap(28.33340, -81.93604, 3, {
		controls: ["GSmallMapControl", "GMapTypeControl"],
		markers: $(".geo")
	});
		
	// Lets Get the map 2 going - FOR PAGES THAT HAVE 2 MAPS SO WE CAN CONTROL THEM AS INDIVIDUALS
	$("#map2").googleMap(28.33341, -81.93605, 3, {
		controls: ["GSmallMapControl", "GMapTypeControl"],
		markers: $(".geo")
	});	
	
	
	
	
	
	
	$("input:radio[name=utilCat]").attr("disabled","disabled");
	$("#cityName").keyup(function(){
		var searchTerm = $("#cityName").attr("value"); 
		sendValue(searchTerm);
	});
	
	

	if($.browser.msie){
		$("input:radio[name=utilCat]").click(function(){
			getInformation();
		});
	}
	else {
		$("input:radio[name=utilCat]").change(function(){
			getInformation();
		});
	}

	

}); // END DOM


function sendValue(searchTerm){
	cleanUp();
	var searchTerm;
	$.post("/citysearch.php", { cityIs: searchTerm }, 
    function(data){
		$("#suggest").html(data);
		$("#suggest").show();
		$("#suggestLink a").click(clicked);
	});
	cleanUp();
	return false;	
}

function clicked(){
	
	var okMan = $(this).attr("rel");
	$("#cityName").attr("value",okMan);
	$("#suggest").hide();
	$(".utilRadio").removeAttr('disabled');	
	var searchTerm = $("#cityName").attr("value");
	getInformation();
	cleanUp();
	return false;
}

function getInformation(){
	var searchTerm = $("#cityName").attr("value");
	var okMan = $("input:radio[name=utilCat]:checked").val();
	$.post("/getBanners.php", { cityIs: searchTerm, categoryID: okMan }, 
    function(data){
		$("#utilContentBox").html(data);
		$("#utilContentBox .simple").hide();
	});
	cleanUp();
	return true;
}
function cleanUp(){
	$("input:radio[name=utilCat]").unbind('click', clicked);
	$("input:radio[name=utilCat]").unbind('click', getInformation);
}		
			
			
			




// For Pre-Qualification form --
function onSelectChange(){  
	var selected = $("#selPurposeOfLoan option:selected"); 
    var output = "";  
    if(selected.val() == 'Purchase a property')
	{  
        output = "Estimated sale price of the property";  
		outputTXTinput = "<input type=\"text\" name=\"txtEstSalePrice\" id=\"txtEstSalePrice\" size=\"50\" maxlength=\"10\" />";
    }  
	else {
		output = "Estimated value of the property"; 
		outputTXTinput = "<input type=\"text\" name=\"txtEstValue\" id=\"txtEstValue\" size=\"50\" maxlength=\"10\" />";
	}
    $("#selPurposeOfLoanChange").html(output);
	$('#selPurposeOfLoanChangeInput').html(outputTXTinput);
} 




// Confirm the click...to make sure that there are no mistakes
function confirmTheClick($iName)
{
	var agree="Are you sure you wish to perform this action?\nCheck your work!";
	if ( confirm( agree ) )
		return true;
	else
		return false;
}


// Safety feature to make sure nobody clicked on delete by mistake.
function confirmDelete($iName)
{
	var agree=confirm("Are you sure you wish to delete?\nThis CANNOT be undone Name!");
	if (agree)
		return true;
	else
		return false;
}
function displayState(titleValue)
{
   document.getElementById("txtStateName").innerHTML = titleValue
}
navHover = function() {
	var lis = document.getElementById("navmenu-h").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);



function showHide(show,plusMinus)
{
	$('#'+show+'').toggle();
	if ($('#'+plusMinus+'').text()=='(-)')
	{
		$('#'+plusMinus+'').html('<strong>(+)</strong>');
	}
	else
	{
		$('#'+plusMinus+'').html('<strong>(-)</strong>');
	}
}

/*
For masked input 
    a - Represents an alpha character (A-Z,a-z)
    9 - Represents a numeric character (0-9)
    * - Represents an alphanumeric character (A-Z,a-z,0-9) 
*/







