$(document).ready(function(){
	//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
	$("div[id^='shd']").each(function(i) {$(this).hide();});
	$("a[id^='shd']").click(function(){
		var id = $(this).attr("id");
		id = id + "d";
		$("div[id^='shd']").not("#"+id).hide("fast");
		$("#"+id).show("fast");
		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'});}
			if ($(this).attr("for") !== "") {
				var that = $(this).hide().css("cursor","text");
				$("#"+$(this).attr("for")).attr("title",$(that).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'});}
				}).triggerHandler("focus");
				$("#"+$(this).attr("for")).triggerHandler("blur");
			}
		}
	});
	//form validation
	$("form.validation input[type='submit']").click(function(){
		$("#validationmessage").html("").addClass("hide").hide();
		var isValid = 0;
		var firstError = 9999;
		if (formFields && formFields.length && formFields.length > 0) {
			for (var i=0; i<formFields.length; i+=1) {
				if (formFields[i] && validate(formFields[i])===false) {
					isValid += 1;
					if (i < firstError) {firstError = i;}
					$("#"+formFields[i].id).addClass("fielderror");
				}
			}
			if (firstError < 9999) {$("#"+formFields[firstError].id).focus();}
		}
		return ((isValid===0) ? true : false);
	});
	
// etown extermination only
	$("#messageform input:text").each(function(i) {
		$(this).focus(function() {if ($(this).val()===$(this).attr("title")) {$(this).val("");}}).blur(function() {if ($(this).val()==="") {$(this).val($(this).attr("title"));}});
	});
// end etown ext
});

var validate = function(field) {
	var id = field.id || null;
	var req = field.req || false;
	var rules = field.rules || "text";
	var msg = field.msg || "Please enter a value for the field '" + $("label[for='"+id+"']").text() + "'.";
	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 (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":
			temp = new String(val).replace(/,/g,"");
			if (isNaN(temp)) {isValid = false;}
			if (isValid) {$("#"+id).val(temp);}
			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 ($("#validationmessage").hasClass("hide")) {$("#validationmessage").fadeIn("fast").removeClass("hide");}
		if (msg) {$("#validationmessage").append("&bull;"+msg+"<br />");}
		return false;
	}
};