//RUN INSTANT
var badgetooltips = { //tooltip to show Alt text for badge/ribbon images
	init:function() {
		xOffset = 10;
		yOffset = 20;

		jQuery('.badgeimg').hover(function(e){
			var tip = jQuery(this).attr('rel');
			jQuery("body").append("<p id='btooltip'>"+ tip +"</p>");
			jQuery("#btooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn(250);

		}, function(e){ jQuery('#btooltip').remove(); });

		jQuery(".badgeimg").mousemove(function(e){
			jQuery("#btooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
		}); 		
	}
}
//RUN ON PAGE LOAD COMPLETE
jQuery(document).ready(function () {
	//init tooltips for badges
	badgetooltips.init();
});
// NON-JQUERY
A4J.AJAX.onError = function (req, status, message) {
	window.location = "member-profile.html?member_id=#{memberIdentifier.memberId}";
};
A4J.AJAX.onExpired = function (loc, expiredMsg) {
	checkTimeZone();
	return loc;
};
/*
	This is used by modal panels to prevent closure whenever validation fails.
	<a4j:commandButton value="Save" action="#{myController.save}" oncomplete="if (!ajaxRequestContainsErrors()) Richfaces.hideModalPanel('myModalPanelId');"/>
	*/
function ajaxRequestContainsErrors() {
	return (document.getElementById("maximumSeverity").value == "2");
}
var EVENTS = {SELF_STATUS:new String("Update Status...")};
function resetInputField(obj, arg1, arg2) {
	var field = document.getElementById(obj);
	if (field.value == arg1) {
		field.value = arg2;
	}
}
function flipInputSecretFields(obj1, obj2) {
	//obj1 will be hidden; obj2 will be displayed
	document.getElementById(obj1).style.display = "none";
	document.getElementById(obj2).style.display = "";
}
function showInputSecretLabel(obj1, obj2) {
	if (document.getElementById(obj1).value == "") {
		flipInputSecretFields(obj1, obj2);
	}
}

//assigned 'label' only is the value is empty
function labelInputField(obj, label) {
	if (null != document.getElementById(obj) && null != document.getElementById(obj).value) {
		if (document.getElementById(obj).value == "") {
			document.getElementById(obj).value = label;
		}
	} else {
		return false;
	}
}

//assigned 'label' even if the value is not empty
function alwaysLabelInputField(obj, label) {
	if (null != document.getElementById(obj) && null != document.getElementById(obj).value) {
		document.getElementById(obj).value = label;
	} else {
		return false;
	}
}
function labelDropdown(obj, label) {
	if (null != document.getElementById(obj) && null != document.getElementById(obj).options[0]) {
		if (document.getElementById(obj).options[0].text == "") {
			document.getElementById(obj).options[0].text = label;
		}
	} else {
		return false;
	}
}
function clearInputContent(obj, str) {
	if (null != document.getElementById(obj) && null != document.getElementById(obj).value) {
		if (document.getElementById(obj).value == str) {
			document.getElementById(obj).value = "";
		}
	} else {
		return false;
	}
}
function stripOverlayLabelinBasicProfile() {
	clearInputContent("basicProfileSubmission:home_zip", "Zip Code?");
	clearInputContent("basicProfileSubmission:home_city", "What City?");
	clearInputContent("basicProfileSubmission:home_province", "What Province?");
	clearInputContent("basicProfileSubmission:current_zip", "Zip Code?");
	clearInputContent("basicProfileSubmission:current_city", "What City?");
	clearInputContent("basicProfileSubmission:current_province", "What Province?");
}
function getFocus(obj) {
	var focusHere = document.getElementById(obj);
	if (null != document.getElementById(obj)) {
		focusHere = focusHere.focus();
	}
}
window.attachEvent ? window.attachEvent("onload", setupdefaultlables) : window.addEventListener("load",setupdefaultlables,false);


function setupdefaultlables() {
	if (window.location.pathname == "/profile/community.html") {
		labelInputField("importAddresses:email-info", "Your Email");
		labelInputField("findFriends:firstName", "First Name");
		labelInputField("findFriends:lastName", "Last Name");
		labelDropdown("findFriends:service", "Branch");
		labelDropdown("findFriends:paygrade", "Paygrade");
		alwaysLabelInputField("selfStatusForm:inputStatus", "Update Status...");
	}
	if (window.location.pathname == "/profile/member-profile-basic.html") {
		labelInputField("basicProfileSubmission:home_zip", "Zip Code?");
		labelInputField("basicProfileSubmission:home_city", "What City?");
		labelInputField("basicProfileSubmission:home_province", "What Province?");
		labelInputField("basicProfileSubmission:current_zip", "Zip Code?");
		labelInputField("basicProfileSubmission:current_city", "What City?");
		labelInputField("basicProfileSubmission:current_province", "What Province?");
		labelDropdown("basicProfileSubmission:home_state", "(Select State)");
		labelDropdown("basicProfileSubmission:home_country", "(Select Country)");
		labelDropdown("basicProfileSubmission:current_state", "(Select State)");
		labelDropdown("basicProfileSubmission:current_country", "(Select Country)");
	}
	if (window.location.pathname == "/profile/member-profile.html") {
		alwaysLabelInputField("mainForm:inputStatus", EVENTS.SELF_STATUS);
	}
}

// CENTERING FOR MODAL PANELS
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == "number") {
                      //Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else {
		if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {

            //DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else {
			if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {

                //IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
		}
	}
	return [scrOfX, scrOfY];
}
function getTopOffset() {
	var scroll = getScrollXY();
	var top = scroll[1] + 100;
	return top;
}
/* #### THIS FUNCTION NEEDS DOCUMENTATION #### */
checkTimeZone = function () {
	var tzo = new Date().getTimezoneOffset();
	var tz = (tzo / 60);
	tz > 0 ? tz = "GMT" + tz * (-1) : tz = "GMT+" + tz * (-1);
	document.cookie = "timezoneOffset=" + tz;
};
window.attachEvent ? window.attachEvent("onload", function () {
	checkTimeZone();
}) : window.addEventListener("load", function () {
	checkTimeZone();
}, false);
checkAll = function (FormName, FieldName, CheckValue) {
	if (!document.forms[FormName] || !document.forms[FormName].elements.length) {
		return;
	}
	for (var i = 0; i < document.forms[FormName].elements.length; i++) {
		var e = document.forms[FormName].elements[i];
		if (e.type == "checkbox" && e.name.indexOf(FieldName) !== -1) {
			e.checked = CheckValue;
		}
	}
};
function ajaxRequestContainsErrors() {
	return document.getElementById("maximumSeverity").value == "2";
}
function closeModalPanel(panelId, hid) {
	if (!ajaxRequestContainsErrors(hid)) {
		Richfaces.hideModalPanel(panelId);
	}
}
selectAll = function (FormName, FieldName1, FieldName2) {
	var objCheckBox = document.forms[FormName].elements[FieldName1].checked;
	if (!objCheckBox) {
		checkAll(FormName, FieldName2, false);
	} else {
		checkAll(FormName, FieldName2, true);
	}
}
function activate(server, location) {
	var urlpath = server+location 
	var href = server+"member-reg.html?vlv_redir=" + escape(urlpath);
	this.location.href = href;
}
function showPanelOrActivate(activated, panel, server, location) {
	if (activated) {	
		Richfaces.showModalPanel(panel, {top:getTopOffset()});
	} else { 
		activate(server, location);
	}	
}
