$(document).ready(function(){
	var ie6 = /MSIE 6/i.test(navigator.userAgent);
	if (readCookie("OpenDiv")) {$("#"+readCookie("OpenDiv")).show("fast");}
	if (readCookie("FeatureType")) {
		$("#FeatureLink"+readCookie("FeatureType")).addClass("activeFeature");
		$("#featuretable"+readCookie("FeatureType")).show();
	}
	//ie menu hover fix
	if (window.attachEvent) {$("#nav li").hover(
		function(){$(this).addClass("iehover");},
		function(){$(this).removeClass("iehover");}
	);};
	//email functions
	$("span.sendmail").each(function(i) {
		//	username+domain+com+[display}
		var a = $(this).text().split("+");
		a[3] = a[3] || a[0]+"@"+a[1]+"."+a[2];
		$(this).text("").html('<a href="mailto:'+a[0]+'@'+a[1]+'.'+a[2]+'">'+a[3]+'</a>');
	});
	//show hide div
	$("a[id^='shd']").click(function(){
		var id = $(this).attr("id");
		id = id + "d";
		$("div[id^='shd']").not("#"+id).hide("fast");
		$("#"+id).show("fast");
		createCookie("OpenDiv",id,60);
		return false;
	});
	//toggle div
	$("div[id^='tog']").each(function(i) {$(this).hide();});
	$("a[id^='tog']").click(function(){
		var id = $(this).attr("id");
		id = id + "d";
		$("#"+id).toggle("fast");
		return false;
	});
	//flash movies
	$("div.flashmov").each(function(i){
		// url+w+h
		var a = $(this).text().split("+"); 
		var str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+a[1]+'" height="'+a[2]+'"><param name="movie" value="'+a[0]+'"> <param name="quality" value="high"><param name="wmode" value="transparent"><embed src="'+a[0]+'" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+a[1]+'" height="'+a[2]+'"></embed></object>';
		$(this).html(str);
	});
	// no label forms, with minimal no label validation
	$("form.nolabel label").each(function(i){
		if ($(this).attr("type") !== "radio" && $(this).attr("type") !== "checkbox") {
			if ($("#"+$(this).attr("for")).val() !== null) {$(this).css({'top':'-1000px','left':'-1000px'});}
			var that = $(this).hide().css("cursor","text");
			var pos = $("#"+$(this).attr("for")).offset();
			pos.left += 5;
			$(this).css({'position':'absolute','z-index':'30','top':pos.top+'px','left':pos.left+'px','color':'#000000;'}).show();
			$("#"+$(this).attr("for")).focus(function(){
				$(that).css({'top':'-1000px','left':'-1000px'});
			}).blur(function(){
				if ($(this).val() === "") {$(that).css({'top':pos.top+'px','left':pos.left+'px'});}
			});
		}
	});
	//form validation
	$("form.validation input[type='submit']").click(function(){
		$("#validationmessage").html("");
		if (formFields && formFields.length && formFields.length > 0) {
			var isValid = true;
			for (var i=0; i<formFields.length; i+=1){
				if (formFields[i]) {if (!validate(formFields[i])) {isValid = false;}}
			}
			return (isValid);
		} else {return true;}
	});
	$("input.crt, textarea.crt").each(function(){
		if ($(this).val()==="") {$(this).val($(this).attr("title"));}
	}).focus(function(){
		if ($(this).attr("title")===$(this).val()) {$(this).val("");}
	}).blur(function(){
		if ($(this).val()==="") {$(this).val($(this).attr("title"));}
	});
	$("#password").each(function(){
		$(this).addClass("hide");
		$("#password2").removeClass("hide");
	}).blur(function(){
		if ($(this).val()==="") {
			$(this).addClass("hide");
			$("#password2").removeClass("hide");
		}
	});
	$("#password2").each(function(){
		if ($("#password").val()!=="") {
			$(this).addClass("hide");
			$("#password").removeClass("hide");
		};
	}).focus(function(){
		$(this).addClass("hide");
		$("#password").removeClass("hide").focus();
	});
	$("#pwdialog").dialog({
		autoOpen: false,
		height: 125,
		modal: true,
		title:"Forgotten Password"
	});
	$("#ForgotPasswordLink").click(function(){
		if (ie6) {return true;}
		else {
			$("#pwdialog").removeClass("hide").dialog('open');
			return false;
		}
	});
	$("label[for='accountlogin']").css({
		"width":"90px",
		"display":"block",
		"float":"left",
		"text-align":"right"
	})
	$("#passwordform #RealtorAccount").click(function() {$("label[for='accountlogin']").text("Username: ")});
	$("#passwordform #UserAccount").click(function() {$("label[for='accountlogin']").text("Email Address: ")});
	$("#regdialog").dialog({
		autoOpen: false,
		height: 450,
		width: 400,
		modal: true,
		title:"Register Account"
	});
	$("#RegisterAccountLink").click(function(){
		if (ie6) {return true;}
		else {
			$("#regdialog").removeClass("hide").dialog('open');
			return false;
		}
	});
	$("#ShowAdvancedSearch").click(function(){
		$("#ssadvanced").slideDown("normal");
		return false;
	});
	$("#ssform").submit(function(){
		if ($("#ssZip").val() === $("#ssZip").attr("title")) {$("#ssZip").val("");}
		if ($("#ssMLS").val() === $("#ssMLS").attr("title")) {$("#ssMLS").val("");}
		return true;
	});
	$("a[id^='FeatureLink']").click(function(){
		$("a[id^='FeatureLink']").removeClass("activeFeature")
		var id = $(this).addClass("activeFeature").attr("id").replace("FeatureLink","");
		$("table[id^=featuretable]").hide()
		$("#featuretable"+id).fadeIn("slow");
		createCookie("FeatureType",id,60);
		return false;
	});
});

var validate = function(field) {
	var id = field.id || "nofieldshouldhavethisforanid";
	var req = field.req || false;
	var rules = field.rules || "text";
	var msg = field.msg || null;
	var nmin = field.nmin || null;
	var nmax = field.nman || null;
	var isValid = true;
	var val = $("#"+id).removeClass("fielderror").val();
	var len = ((val && val.length && val.length>0) ? val.length : 0);
	if (len && nmin && len<nmin) {isValid=false;}
	if (len && nmax && len>nmax) {isValid=false;}
	if (req && !val) {isValid=false;}
	if (!req && !val) {return true;}
	if (isValid) {
	switch (rules){
		case "text":
			if (val <= 0) {isValid = false;}
			break;
		case "email":
			at = val.indexOf("@");
			dot = val.indexOf(".",at);
			if (val.indexOf("@")===-1 || val.indexOf("@")===0 || val.indexOf("@")===len) {isValid = false;}
			if (val.indexOf(".")===-1 || val.indexOf(".")===0 || val.indexOf(".")===len) {isValid = false;}
			if (val.indexOf("@",(at+1))!==-1) {isValid = false;}
			if (val.substring(at-1,at)==="." || val.substring(at+1,at+2)===".") {isValid = false;}
			if (val.indexOf(".",(at+2))===-1) {isValid = false;}
			if (val.indexOf(" ")!==-1) {isValid = false;}
			if (val.substring(0,at+1).length < 3) {isValid = false;}
			if (val.substring(at,dot+1).length < 4) {isValid = false;}
			if (val.substring(dot).length < 3) {isValid = false;}
			break;
		case "phone":
			temp = new String(val).replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"").replace(/\./g,"").replace(/-/g,"");
			if (temp.length !== 7 && temp.length !== 10 && temp.length !== 11) {isValid = false;}
			if (isNaN(temp)) {isValid = false;}
			if (isValid) {
				if (temp.length === 10) {$("#"+id).val("(" + temp.substring(0,3) + ") " + temp.substring(3,6) + "-" + temp.substring(6));}
				else if (temp.length === 7) {$("#"+id).val(temp.substring(0,3) + "-" + temp.substring(3));}
				else if (temp.length === 11) {$("#"+id).val("(" + temp.substring(1,4) + ") " + temp.substring(4,7) + "-" + temp.substring(7));}
			}
			break;
		case "zip":
			temp = new String(val).replace(/-/g,"").replace(/ /g,"").replace(/\./g,"");
			if (temp.length !== 5 && temp.length !== 9) {isValid = false;}
			if (isNaN(temp)) {isValid = false;}
			if (isValid && temp.length === 9) {$("#"+id).val(temp.substring(0,5) + "-" + temp.substring(5));}
			break;
		case "url":
			dot = val.lastIndexOf(".");
			if (val.indexOf("@") !==-1) {isValid = false;}
			if (val.indexOf("\"") !==-1) {isValid = false;}
			if (val.indexOf("'") !==-1) {isValid = false;}
			if (val.indexOf(".")===-1 || val.indexOf(".")===0 || val.indexOf(".")===len) {isValid = false;}
			if (val.substring(dot).length < 3) {isValid = false;}
			if (val.substring(0,dot).length < 2) {isValid = false;}
			if (val.length < 5) {isValid = false;}
			if (isValid && val.indexOf("http")===-1) {$("#"+id).val("http://" + val);}
			break;
		case "ssn":
			temp = new String(val);
			temp = temp.replace(/ /g,"").replace(/\./g,"").replace(/-/g,"");
			if (temp.length !== 9) {isValid = false;}
			if (isNaN(temp)) {isValid = false;}
			if (isValid) {$("#"+id).val(temp.substring(0,3) + "-" + temp.substring(3,5) + "-" + temp.substring(5));}
			break;
		case "currency":
			temp = new String(val).replace(/\$/g,"").replace(/,/g,"");
			if (isNaN(temp)) {isValid = false;}
			if (isValid) {
				temp = Math.floor(temp * 100);
				temp = temp / 100;
				$("#"+id).val("$" + temp);
				}
			break;
		case "number":
			if (isNaN(val)) {isValid = false;}
			break;
		case "date":
			temp = new String(val).replace(/ /g,"~").replace(/\./g,"~").replace(/-/g,"~").replace(/\\/g,"~").replace(/\//g,"~").replace(/-/g,"~").replace(/_/g,"~");
			var s1 = temp.indexOf("~");
			var s2 = temp.lastIndexOf("~");
			var date1 = temp.substring(0,s1);
			var date2 = temp.substring(s1+1,s2);
			var date3 = temp.substring(s2+1);
			if (isNaN(date1) || isNaN(date2) || isNaN(date3)) {isValid = false;}
			if (date1 > 31 || date1 < 1) {isValid = false;}
			if (date2 > 31 || date2 < 1) {isValid = false;}
			if (date1 > 12 && date2 > 12) {isValid = false;}
			if (date1.length > 2 || date1.length < 1) {isValid = false;}
			if (date2.length > 2 || date2.length < 1) {isValid = false;}
			if (date3.length !== 2 && date3.length !== 4) {isValid = false;}
			if (date3.length === 2) {if (date3 < 00 || date3 > 99) {isValid = false;}}
			if (isValid) {$("#"+id).val(date1 + "/" + date2 + "/" + date3);}
			break;
		case "time":
			temp = temp = new String(val).replace(/ /g,"");
			var tz, hh, mm = null;
			if (temp.indexOf("AM")!==-1 || temp.indexOf("am")!==-1) {
				tz = " AM";
				temp = temp.replace(/AM/i,"");
			}
			if (temp.indexOf("PM")!==-1 || temp.indexOf("pm")!==-1) {
				tz = " PM";
				temp = temp.replace(/PM/i,"");
			}
			var col = temp.indexOf(":");
			if (col !== -1){
				hh = temp.substring(0,col);
				mm = temp.substring(col+1);
				if (hh - 12 > 0) {
					tz = " PM";
					hh = hh - 12;
				} else {tz = " AM";}
			} else {
				mm = "00";
				hh = temp;
				if (temp - 12 > 0) {
					if (!tz) {tz = " PM";}
					hh = temp - 12;
				} else {if (!tz) {tz = " AM";}}
			}
			if (isNaN(hh)) {isValid = false;}
			if (isNaN(mm)) {isValid = false;}
			if (hh < 0 || hh > 12) {isValid = false;}
			if (mm < 0 || mm > 59) {isValid = false;}
			if (tz !== " AM" && tz !== " PM") {isValid = false;}
			if (isValid) {$("#"+id).val(hh+":"+mm+tz);}
			break;
		case "cc":
			temp = new String(val).replace(/-/g,"").replace(/ /g,"").replace(/\./g,"");
			if (temp.length !== 15 && temp.length !== 16) {isValid = false;}
			if (isNaN(temp)) {isValid = false;}
			if (isValid){
				if (temp.length === 16) {$("#"+id).val(temp.substring(0,4)+"-"+temp.substring(4,8)+"-"+temp.substring(8,12)+"-"+temp.substring(12));}
				else {$("#"+id).val(temp.substring(0,3)+"-"+temp.substring(3,7)+"-"+temp.substring(7,11)+"-"+temp.substring(11));}
			}
			break;
		case "ccv":
			if (len !== 3 && len !== 4) {isValid = false;}
			if (isNaN(val)) {isValid = false;}
			break;
		}
	}
	if (!isValid){
		$("#"+id).addClass("fielderror");
		if (msg) {$("#validationmessage").append("&bull;"+msg+"<br />").fadeIn("slow");}
		return false;
	} else {return true;}
};

var createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {var expires = "";}
	document.cookie = name+"="+value+expires+"; path=/";
};

var readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i=0; i<ca.length; i+=1) {
		var c = ca[i];
		while (c.charAt(0) === ' ') {c = c.substring(1,c.length);}
		if (c.indexOf(nameEQ) === 0) {return c.substring(nameEQ.length,c.length);}
	}
	return null;
};