var imgEducationConsultingOn = new Image();
var imgEducationConsultingOff = new Image();
var imgCommunityServicesOn = new Image();
var imgCommunityServicesOff = new Image();
var imgResidentialServicesOn = new Image();
var imgResidentialServicesOff = new Image();
var imgHomeEducationOn = new Image();
var imgHomeEducationOff = new Image();
var imgHomeCommunityOn = new Image();
var imgHomeCommunityOff = new Image();
var imgHomeResidentialOn = new Image();
var imgHomeResidentialOff = new Image();
imgEducationConsultingOn.src = "/img/btn_education_cons_on.jpg";
imgEducationConsultingOff.src = "/img/btn_education_cons_off.jpg";
imgCommunityServicesOn.src = "/img/btn_community_service_on.jpg";
imgCommunityServicesOff.src = "/img/btn_community_service_off.jpg";
imgResidentialServicesOn.src = "/img/btn_residential_service_on.jpg";
imgResidentialServicesOff.src = "/img/btn_residential_service_off.jpg";
imgHomeEducationOn.src = "/img/home_nav_education_on.jpg";
imgHomeEducationOff.src = "/img/home_nav_education_off.jpg";
imgHomeCommunityOn.src = "/img/home_nav_community_on.jpg";
imgHomeCommunityOff.src = "/img/home_nav_community_off.jpg";
imgHomeResidentialOn.src = "/img/home_nav_residential_on.jpg";
imgHomeResidentialOff.src = "/img/home_nav_residential_off.jpg";

jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox();
  $('.rounded').corners("4px");
});

function fFocus(tthis) {
	tthis.className = "cssFocus";
}

function fBlur(tthis) {
	tthis.className = "";
}

function fMouseOut(tthis) {
	if(tthis.className != "cssFocus") {
		tthis.className = "";
	}
}

function fMouseOver(tthis) {
	if(tthis.className != "cssFocus") {
		tthis.className = "cssOver";
	}
}

function fVerifyEmail(strEmail) {
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	if (re.test(strEmail)) {
		return true;
	} else {
		return false;
	}
}

function fVerifyInputLength(strID,strText,intMin,intMax) {
	var oInput = document.getElementById(strID);
	if(oInput.value.length < intMin) {
		oInput.style.background = "#ffcccc";
		return strText;
	}
	if(oInput.value.length > intMax) {
		oInput.style.background = "#ffcccc";
		return strText;
	}
	oInput.style.background = "";
	return "";
}

function fVerifyInput(strID, strText) {
	var oInput = document.getElementById(strID);
	if(oInput.value == "") {
		oInput.style.background = "#ffcccc";
		return strText;
	} else {
		oInput.style.background = "";
		return "";
	}
}

function fVerifyRadio(strID, strText) {
	var oRadio = document.getElementsByName(strID);
	for(i=0; i<oRadio.length; i++) {
		if(oRadio[i].checked) {
			return "";
		}
	}
	return strText;
}

function fVerifyCheck(strID, strText) {
	var oCheck = document.getElementById(strID);
	if(oCheck.checked) {
		return "";
	}
	
	return strText;
}

function fVerifySelect(strID, strText) {
	var oElement = document.getElementById(strID);
	if(oElement.selectedIndex == 0) {
		oElement.style.background = "#ffcccc";
		oElement.focus();
		return strText;
	} else {
		oElement.style.background = "#ffffff";
	}
	return "";
}

function fInputValue(strID) {
	return document.getElementById(strID).value;
}

function fShow(strDiv) {
	document.getElementById(strDiv).style.visibility = "visible";
	document.getElementById(strDiv).style.display = "block";
}

function fHide(strDiv) {
	document.getElementById(strDiv).style.visibility = "hidden";
	document.getElementById(strDiv).style.display = "none";
}


function fToggle(strID) {
	if(document.getElementById(strID).style.visibility == "hidden") {
		fShow(strID);
	} else {
		fHide(strID);
	}
}

function fAJAXRequest(strTarget, strURL) {
	if(window.XMLHttpRequest) {
		oRequest = new XMLHttpRequest();
		oTarget = strTarget;
		oRequest.onreadystatechange = fAJAXChangeWhenReady;
		oRequest.open("GET", strURL, true);
		oRequest.send(null);
	} else if(window.ActiveXObject) {
		oRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if(oRequest) {
			oTarget = strTarget;
			oRequest.onreadystatechange = fAJAXChangeWhenReady;
			oRequest.open("GET", strURL, true);
			oRequest.send();
		}
	}
}

function fAJAXChangeWhenReady() {
    if(oRequest.readyState == 4) {
    	if(document.getElementById(oTarget)) {
    		document.getElementById(oTarget).innerHTML = oRequest.responseText;
    	}
    }
}

function fNewsletterSignup(strEmail) {
	if(strEmail == "") {
		alert("Please enter an email address before we continue.");
		document.getElementById("txtEmail").focus();
	} else {
		var oAjax = $.ajax({ type: "get", url: "ajaxSaveEmail.php", dataType: "html", data: "txtEmail="+strEmail, async:false });
		if(oAjax.responseText == "ADDED") {
			alert("Thank you, you have successfully signed up for the newsletter!");
			document.getElementById("txtEmail").value = "";
		} else if(oAjax.responseText == "") {
			alert("Network could not be reached! Are you online?");
		} else if(oAjax.responseText == "INVALID EMAIL") {
			alert("Please enter a valid email address.");
			document.getElementById("txtEmail").focus();
		} else {
			alert("There was an error trying to link this artwork. The server said: \n"+oAjax.responseText);
		}
	}
}

function fAddToCart(strProductID, strQuantity) {
	if(strQuantity == "") {
		alert("Please enter a quantity first");
		document.getElementById("txtQuantity"+strProductID).focus();
	} else {
		var oAjax = $.ajax({ type: "get", url: "ajaxAddToCart.php", dataType: "html", data: "pid="+strProductID+"&q="+strQuantity, async:false });
		if(oAjax.responseText == "OK") {
			location.href = "products.php?m=1";
		} else {
			alert("There was a problem adding that item to your shopping cart");
			alert(oAjax.responseText);
		}
	}
}

function fSaveAnnouncementComment(strID) {
	var strComment = document.getElementById("txtComment"+strCommentID).value;
	if(strComment  == "") {
		alert("Please enter a comment first");
	} else {
		var oAjax = $.ajax({ type: "get", url: "ajaxComments.php", dataType: "html", data: "prefix=announcement&cid="+strID+"&comment="+escape(strComment), async:false });
		if(oAjax.responseText == "OK") {
			document.getElementById("divComment"+strID).innerHTML = "<i>Your comment has been saved. Thank you for your post!</i>";
		} else {
			alert("There was a problem saving your comment:\n\n"+oAjax.responseText);
		}
	}
	return false;
}

function fSaveDiscussionComment(strID, strCommentID) {
	var strComment = document.getElementById("txtComment"+strCommentID).value;
	if(strComment  == "") {
		alert("Please enter a comment first");
	} else {
		var oAjax = $.ajax({ type: "get", url: "ajaxComments.php", dataType: "html", data: "prefix=discussion&cmid="+strCommentID+"&cid="+strID+"&comment="+escape(strComment), async:false });
		if(oAjax.responseText == "OK") {
			document.getElementById("divComment"+strCommentID).innerHTML = "<i>Your comment has been saved. Thank you for your post!</i>";
		} else {
			alert("There was a problem saving your comment:\n\n"+oAjax.responseText);
		}
	}
	return false;
}

function fAddToCart2(strProductID, strQuantity) {
	if(strQuantity == "") {
		alert("Please enter a quantity first");
		document.getElementById("txtQuantity"+strProductID).focus();
	} else {
		var oAjax = $.ajax({ type: "get", url: "ajaxAddToCart.php", dataType: "html", data: "pid="+strProductID+"&q="+strQuantity, async:false });
		if(oAjax.responseText == "OK") {
			location.href = "shopping_cart.php";
		} else {
			alert("There was a problem adding that item to your shopping cart");
			alert(oAjax.responseText);
		}
	}
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}

function fJoinCheckUsername(strValue) {
	if(strValue == "") { return; }
	var oAjax = $.ajax({ type: "get", url: "ajaxCheckUsername.php", dataType: "html", data: "user="+strValue, async:false });
	if(oAjax.responseText == "TAKEN") {
		alert("That username is already taken. Please choose another one!");
		setTimeout('document.getElementById("joinEmailAddress").focus();',500);
	}
}
	
function fVerifyJoinForm() {
	var strError = "";
	
	strError += fVerifyInput("joinEmailAddress","Please enter your desired email address.\n");
	strError += fVerifyInput("joinPassword1","Please enter your desired password.\n");
	if(document.getElementById("joinPassword1").value != "") {
		if(document.getElementById("joinPassword1").value != document.getElementById("joinPassword2").value) {
			strError += "Please confirm your password!\n";
		}
	}
	strError += fVerifyInput("joinFirstName","Please enter your first name.\n");
	strError += fVerifyInput("joinLastName","Please enter your last name.\n");
	strError += fVerifyInput("joinAddress1","Please enter your address.\n");
	strError += fVerifyInput("joinCity","Please enter your city.\n");
	strError += fVerifySelect("joinState","Please enter your state.\n");
	strError += fVerifyInput("joinZipcode","Please enter your zipcode.\n");
	strError += fVerifyInputLength("joinZipcode","Please enter your actual zipcode (between 5 and 10 characters).\n",5,10);
	
	if(strError != "") {
		alert(strError);
		return false;
	}
	return true;
}

function fVerifyRequestInformation() {
	var strError = "";

	strError += fVerifyInput("txtFirstName","Please enter your first name.\n");
	strError += fVerifyInput("txtLastName","Please enter your last name.\n");
	strError += fVerifyInput("txtAddress","Please enter your address.\n");
	strError += fVerifyInput("txtCity","Please enter your state.\n");
	strError += fVerifySelect("txtState","Please enter your state.\n");
	strError += fVerifyInput("txtZipcode","Please enter your zipcode.\n");
	if(document.getElementById("txtPhoneNumberHome").value == "") {
				strError += "You must enter your phone number.\n";
	}
	strError += fVerifyInput("txtEmailAddress","Please enter your email address.\n");
	
	if(strError != "") {
		alert(strError);
		return false;
	}
	return true;
}

function fVerifyRequestTour() {
	var strError = "";

	strError += fVerifyInput("txtFirstName","Please enter your first name.\n");
	strError += fVerifyInput("txtLastName","Please enter your last name.\n");
	strError += fVerifyInput("txtAddress","Please enter your address.\n");
	strError += fVerifyInput("txtCity","Please enter your city.\n");
	strError += fVerifySelect("txtState","Please enter your state.\n");
	strError += fVerifyInput("txtZipcode","Please enter your zipcode.\n");
	if(document.getElementById("txtPhoneNumberHome").value == "") {
				strError += "You must enter your phone number.\n";
	}
	strError += fVerifyInput("txtEmailAddress","Please enter your email address.\n");
	
	if(strError != "") {
		alert(strError);
		return false;
	}
	return true;
}

function fVerifyRequestApplication() {
	var strError = "";

	strError += fVerifyInput("txtFirstName","Please enter your first name.\n");
	strError += fVerifyInput("txtLastName","Please enter your last name.\n");
	strError += fVerifyInput("txtAddress","Please enter your address.\n");
	strError += fVerifyInput("txtCity","Please enter your city.\n");
	strError += fVerifySelect("txtState","Please enter your state.\n");
	strError += fVerifyInput("txtZipcode","Please enter your zipcode.\n");
	if(document.getElementById("txtPhoneNumberHome").value == "") {
				strError += "You mut enter your phone number.\n";
	}
	strError += fVerifyInput("txtEmailAddress","Please enter your email address.\n");
	
	if(strError != "") {
		alert(strError);
		return false;
	}
	return true;
}


function fSurveyVote(strQuestionID, strAnswerID) {
	fAJAXRequest("divSurvey"+strQuestionID,"ajaxSurveyVote.php?qid="+strQuestionID+"&aid="+strAnswerID);
	//var oAjax = $.ajax({ type: "get", url: "ajaxSurveyVote.php", dataType: "html", data: "qid="+strQuestionID+"&aid="+strAnswerID, async:false });
	//document.getElementById("divSurvey"+strQuestionID).innerHTML = oAjax.responseText+"11";
}

function fHideDropdown(strID) {
	oTimer = setTimeout('fHideDiv(\"' + strID + '\");', 1000);
}

function fHideDiv(strID) {
	var oDiv = document.getElementById(strID);
	oDiv.style.visibility = "hidden";
	clearTimeout(oTimer);
}

function fShowDropdown(strID) {
	if(strCurrentMenu != "") {
		fHideDiv(strCurrentMenu);
	}
	strCurrentMenu = strID;
	var oDiv = document.getElementById(strID);
	oDiv.style.visibility = "visible";
	oDiv.style.display = "block";
	if(oTimer != null) {
		clearTimeout(oTimer);
	}
}

function getPixelsFromTop(obj){
	objFromTop = obj.offsetTop;
	while(obj.offsetParent!=null) {
		objParent = obj.offsetParent;
		objFromTop += objParent.offsetTop;
		obj = objParent;
	}
	return objFromTop;
}

oTimer = null;
oTimerHome = null;
strCurrentMenu = "";
strCurrentTier = "divHomeOff";
strOpeningAnimation = "true";

function fSetTier(strTier) {
	if(strTier == "E") {
		document.getElementById("divHomeEducationImage").src = imgHomeEducationOn.src;
		document.getElementById("divHomeCommunityImage").src = imgHomeCommunityOff.src;
		document.getElementById("divHomeResidentialImage").src = imgHomeResidentialOff.src;
		fHide("divHomeOff");
		fShow("divHomeEducation");
		fHide("divHomeCommunity");
		fHide("divHomeResidential");
	}
	if(strTier == "C") {
		document.getElementById("divHomeEducationImage").src = imgHomeEducationOff.src;
		document.getElementById("divHomeCommunityImage").src = imgHomeCommunityOn.src;
		document.getElementById("divHomeResidentialImage").src = imgHomeResidentialOff.src;
		fHide("divHomeOff");
		fHide("divHomeEducation");
		fShow("divHomeCommunity");
		fHide("divHomeResidential");
	}
	if(strTier == "R") {
		document.getElementById("divHomeEducationImage").src = imgHomeEducationOff.src;
		document.getElementById("divHomeCommunityImage").src = imgHomeCommunityOff.src;
		document.getElementById("divHomeResidentialImage").src = imgHomeResidentialOn.src;
		fHide("divHomeOff");
		fHide("divHomeEducation");
		fHide("divHomeCommunity");
		fShow("divHomeResidential");
	}
}

function fAdjustTier(strTier) {
	$('#divCycle').cycle('stop');
	if(strOpeningAnimation == "true") {
		$('#divHomeEducation').css('opacity',1).show();
		$('#divHomeCommunity').css('opacity',1).show();
		$('#divHomeResidential').css('opacity',1).show();
		strOpeningAnimation = "false";
	}
	fSetTier(strTier);
}

function fScrollTiers() {
	if(strCurrentTier == "divHomeOff" || strCurrentTier == "divHomeResidential") {
		fSetTier('E');
	} else if(strCurrentTier == "divHomeEducation") {
		fSetTier('C');
	} else if(strCurrentTier == "divHomeCommunity") {
		fSetTier('R');
	}
	oTimerHome = setTimeout("fScrollTiers();",6000);
}

function fUpdateJoinForm(strIndex) {
	if(strIndex == 0) {
		fHide("divPro1");
		fHide("divPro2");
	} else {
		fShow("divPro1");
		fShow("divPro2");
	}
}

function fVerifyRegForm() {
	var strError = "";
	
	strError += fVerifyInput("joinFirstName","Please enter your first name.\n");
	strError += fVerifyInput("joinLastName","Please enter your last name.\n");
	strError += fVerifyInput("joinEmailAddress","Please enter your desired email address.\n");
	strError += fVerifyInput("joinPassword1","Please enter your desired password.\n");
	if(document.getElementById("joinPassword1").value != "") {
		if(document.getElementById("joinPassword1").value != document.getElementById("joinPassword2").value) {
			strError += "Please confirm your password!\n";
		}
	}
	strError += fVerifyInput("joinAddress1","Please enter your address.\n");
	strError += fVerifyInput("joinCity","Please enter your city.\n");
	strError += fVerifySelect("joinState","Please enter your state.\n");
	strError += fVerifyInput("joinZipcode","Please enter your zipcode.\n");
	strError += fVerifyInputLength("joinZipcode","Please enter your actual zipcode (between 5 and 10 characters).\n",5,10);

	if(document.getElementById("joinEmailAddress").value != "") {
		var oAjax = $.ajax({ type: "get", url: "ajaxCheckEmail.php", dataType: "html", data: "txtEmail="+document.getElementById("joinEmailAddress").value, async:false });
		if(oAjax.responseText == "ALREADY_EXISTS") {
			strError += "The email address you have specified already exists. Please login to your account or choose another email address.\n";
		}
	}
	
	if(strError != "") {
		alert(strError);
		return false;
	}
	return true;
}