// Equal height

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if (thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	if (tallest > 0) {
		group.height(tallest);
	}
}

$(document).ready(function() {
	equalHeight($(".equal1"));
	equalHeight($(".equal2"));
});




$(document).ready(function() {

	// Banners home mothership coursor change

	$(".banners-home-m-box").css("cursor", "pointer");
	$(".banners-home-m-box").hover(
		function() {
			$("h2 a", this).css("color", "#004f94");
			$("h2 a", this).css("text-decoration", "underline");
		},
		function() {
			$("h2 a", this).css("color", "#4f4f4f");
			$("h2 a", this).css("text-decoration", "none");
		}
	);


	// Banners home local coursor change

	$(".banners-home-box").css("cursor", "pointer");
	$(".banners-home-box").hover(
		function() {
			$("h2 a", this).css("color", "#004f94");
			$("h2 a", this).css("text-decoration", "underline");
		},
		function() {
			$("h2 a", this).css("color", "#4f4f4f");
			$("h2 a", this).css("text-decoration", "none");
		}
	);


	// Banners internal pages coursor change

	$(".banner-page").css("cursor", "pointer");
	$(".banner-page").hover(
		function() {
			$(".title a", this).css("color", "#004f94");
			$(".title a", this).css("text-decoration", "underline");
		},
		function() {
			$(".title a", this).css("color", "#4f4f4f");
			$(".title a", this).css("text-decoration", "none");
		}
	);



	// Search results - filter collapsing

	$(".filter ul li ul").hide();
	$(".filter ul li ul.selected").show();
	$(".filter ul li span").addClass("toggle");
	$(".filter ul li span").click(function() {
		$(".filter ul li ul").slideUp(100);
		$('ul', $(this).parent()).toggle(100);
	});


	//if no one is logged in, the button says "Login Now" and
	//clicking it displays the login dialog.  If someone IS logged in,
	//the button says "Logout" and redirects to the "logout" page
	if ($("[id$='lnkLogin']").text() == "Login Now") {

		$("[id$='lnkLogin']").attr('href', '#');
		$("[id$='lnkLogin']").click(function() {

			if ($("[id$='divLogin']").is(':visible')) {

				$("[id$='divLogin']").slideUp(350);
				//reset stuff
				$("[id$='ctl00_ucCandidateLogin_uxMessage']").text('');
				$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('');
			}
			else {
				$("[id$='divLogin']").slideDown(350);
				//BindEnterKey("login");
			}

		});

	}
	else {

		$("[id$='lnkLogin']").unbind('click');
		$("[id$='lnkLogin']").attr('href', '/logout/');
	}

	//provides Toggle behavior for the homepage login pop-down
	//so user can switch between candidate and recruiter login
	$("[id$='lnkCandidateLogin']").click(function() {

		if ($("[id$='ctl00_ucClientLogin_pnlLogin']").is(':visible')) {
			$("[id$='ctl00_ucClientLogin_pnlLogin']").css('display', 'none');
			$("[id$='lnkRetrieve']").css('display', 'none');
			$("[id$='ctl00_ucCandidateLogin_pnlLoginCandidate']").css('display', 'block');
			$("[id$='spnLoggers']").html("Recruiters");
			$("[id$='lnkRegister']").attr('href', '/candidate/register');
			$("[id$='lnkRegister']").text('Job Seekers - Register Now');
			$("[id$='lnkReset']").click(function() { ResetCandidatePassword(); });
			$("[id$='lnkReset']").attr('href', '#');
			$("[id$='ctl00_ucClientLogin_hdSlideClient']").val('false');
			$("[id$='divLogin']").css('background-image', 'url("/_resx/images/loginbackground2_pur.png")');
		}
		else {
			$("[id$='ctl00_ucClientLogin_pnlLogin']").css('display', 'block');
			$("[id$='lnkRetrieve']").css('display', 'inline');
			$("[id$='ctl00_ucCandidateLogin_pnlLoginCandidate']").css('display', 'none');
			$("[id$='spnLoggers']").html("Job Seekers");
			$("[id$='lnkRegister']").attr('href', '/client/registration');
			$("[id$='lnkRegister']").text('Recruiters - Register Now');
			$("[id$='lnkReset']").unbind('click');
			$("[id$='lnkReset']").attr('href', '/client/login/forgotpassword.aspx');
			$("[id$='ctl00_ucCandidateLogin_hdSlideCandidate']").val('false');
			$("[id$='divLogin']").css('background-image', 'url("/_resx/images/loginbackground2.png")');
		}

	});

	//default to the appropriate login type (if possible)
	var UserType = $("[id$='hdUserType']").val() + "";

	if (UserType == "CLIENT") {
		$("[id$='ctl00_ucCandidateLogin_pnlLoginCandidate']").css('display', 'none');
		$("[id$='lnkRetrieve']").css('display', 'inline');
		$("[id$='spnLoggers']").html("Job Seekers");
		$("[id$='lnkRegister']").attr('href', '/client/registration');
		$("[id$='lnkRegister']").text('Recruiters - Register Now');
		$("[id$='lnkReset']").unbind('click');
		$("[id$='lnkReset']").attr('href', '/client/login/forgotpassword.aspx');
		$("[id$='ctl00_ucCandidateLogin_hdSlideCandidate']").val('false');
		$("[id$='divLogin']").css('background-image', 'url("/_resx/images/loginbackground2.png")');
	}
	else {  //they're a candidate, or we don't know WHAT they are
		$("[id$='ctl00_ucClientLogin_pnlLogin']").css('display', 'none');
		$("[id$='lnkRetrieve']").css('display', 'none');
		$("[id$='ctl00_ucCandidateLogin_pnlLoginCandidate']").css('display', 'block');
		$("[id$='spnLoggers']").html("Recruiters");
		$("[id$='lnkRegister']").attr('href', '/candidate/register');
		$("[id$='lnkRegister']").text('Job Seekers - Register Now');
		$("[id$='lnkReset']").click(function() { ResetCandidatePassword(); });
		$("[id$='lnkReset']").attr('href', '#');
		$("[id$='ctl00_ucClientLogin_hdSlideClient']").val('false');
		$("[id$='divLogin']").css('background-image', 'url("/_resx/images/loginbackground2_pur.png")');
	}

	var v = $("[id$='hdLoggedIn']").val();

	//display the login panel on postback if the login failed
	if ($("[id$='ctl00_ucClientLogin_hdSlideClient']").val() == "true" && $("[id$='hdLoggedIn']").val() == "false")
		$("[id$='divLogin']").slideDown(350);

	if ($("[id$='ctl00_ucCandidateLogin_hdSlideCandidate']").val() == "true") {
		$("[id$='ctl00_ucClientLogin_pnlLogin']").css('display', 'none');
		$("[id$='ctl00_ucCandidateLogin_pnlLoginCandidate']").css('display', 'block');
		$("[id$='spnLoggers']").html("Recruiters");
		$("[id$='lnkRegister']").attr('href', '/candidate/register');
		$("[id$='lnkRegister']").text('Job Seekers - Register Now');
		$("[id$='lnkReset']").click(function() { ResetCandidatePassword(); });
		$("[id$='lnkReset']").attr('href', '#');
		$("[id$='divLogin']").css('background-image', 'url("/_resx/images/loginbackground2_pur.png")');
		$("[id$='divLogin']").slideDown(350);
	}


	//alert('The UserType is ' + UserType);

	//mess with the keypress event for the Enter key to behave appropriately based on what the user seems to be doing
	$(document).keypress(function(e) {

		if (e.which == 13) {
			if ($("[id$='divLogin']").is(':visible')) {
				//do the login

				//are we a client or a candidate?
				if ($("[id$='ctl00_ucClientLogin_pnlLogin']").is(':visible')) {
					__doPostBack('ctl00$ucClientLogin$uxClientLoginButtonText', '');
				} else {
					__doPostBack('ctl00$ucCandidateLogin$uxCandidateLoginButtonText', '');
				}

				return false;
			} //otherwise, run the job search
		}
	});


});


//"{ 'Email':'" + sEmailAddr + "' }"
function ResetCandidatePassword() {
	var result = false;

	$("[id$='uxMessage']").text('');
	$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('');
	$("[id$='ctl00_ucCandidateLogin_uxAuthenticate']").hide();

	var sEmailAddr = $("[id$='ctl00_ucCandidateLogin_txtCandidateEmail']").val();
	var sTxt = "";
	sEmailAddr = jQuery.trim(sEmailAddr);
	if (sEmailAddr.length > 0) {
		$.ajax({
			type: "GET",
			url: "/WebServices/QuickAlertService.asmx/ResetCandidatePassword",
			data: 'Email=' + sEmailAddr,
			async: true,
			success: function(msg) {

				if (typeof msg.text == 'undefined')
					sTxt = msg.firstChild.textContent.toString();
				else
					sTxt = msg.text;

				//alert('sTxt sez:  ' + sTxt);
				if (sTxt == 'false') {
					//couldn't find it
					$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('Could not locate your email address');
					$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").css('display', 'block');
				}
				else {
					$("[id$='uxMessage']").text('An Email has been sent and should arrive shortly');
					$("[id$='uxMessage']").css('display', 'block');
				}
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert('An Error has occurred:  ' + thrownError);

				return false;
			}
		});
	}
	else {
		$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('Your email address is required');
		$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").css('display', 'block');
	}
}


function ValidateMe(type) {

	$("[id$='ctl00_ucClientLogin_lblClientError']").text('');
	$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('');

	$("[id$='ctl00_ucClientLogin_uxAuthenticateClient']").hide();
	$("[id$='ctl00_ucCandidateLogin_uxAuthenticate']").hide();

	var user;
	var pwd;

	if (type == "Client") {

		user = $("[id$='ctl00_ucClientLogin_txtUserName']").val();
		user = jQuery.trim(user);

		pwd = $("[id$='ctl00_ucClientLogin_txtPassword']").val();
		pwd = jQuery.trim(pwd);

		if (user.length == 0 || pwd.length == 0) {
			$("[id$='ctl00_ucClientLogin_lblClientError']").text('A username and password are required');
			return false;
		}
		else
			return true;
	}
	else {

		user = $("[id$='ctl00_ucCandidateLogin_txtCandidateEmail']").val();
		user = jQuery.trim(user);

		pwd = $("[id$='ctl00_ucCandidateLogin_txtCandidatePassword']").val();
		pwd = jQuery.trim(pwd);

		if (user.length == 0 || pwd.length == 0) {
			$("[id$='ctl00_ucCandidateLogin_uxErrorMessage']").text('An email address and password are required');
			return false;
		}
		else
			return true;

	}
}


function popThickBox(href, title, width, height) {
	$(document).ready(function() { tb_show(title, href + (/\?/.test(href) ? '&' : '?') + 'popup=page&TB_iframe=true&height=' + height + '&width=' + width, null); });
	return false;
}
function popModalThickBox(href, title, width, height) {
	$(document).ready(function() { tb_show(title, href + (/\?/.test(href) ? '&' : '?') + 'popup=modal&TB_iframe=true&height=' + height + '&width=' + width, null); });
	return false;
}
function popModalThickBoxNoIFrame(href, title, width, height) {
	$(document).ready(function() { tb_show(title, href + (/\?/.test(href) ? '&' : '?') + 'popup=modal&height=' + height + '&width=' + width, null); });
	return false;
}

function popModalThickBoxPreview(href, title, width, height, caid, username, url) {
	var hdr = document.getElementById("ca_" + caid);
	var iHtml = hdr.innerHTML;
	var preview;
	var theDate = new Date();
	var today = theDate.getMonth() + 1;
	today += "/" + theDate.getDate();
	today += "/" + theDate.getFullYear();
	//is there already a preview?

	if (iHtml.toUpperCase().indexOf("<IMG") > -1) {
		//alert("Already has preview");
	}
	else {
		preview = "<img onmouseover=\"this.style.cursor='default'\" src=\"" + url + "\" title=\"Previewed by " + username + " on " + today + "\" >"
		hdr.innerHTML += preview;
	}

	//alert('inner html is ' + iHtml);
	$(document).ready(function() { tb_show(title, href + (/\?/.test(href) ? '&' : '?') + 'popup=modal&TB_iframe=true&height=' + height + '&width=' + width, null); });
	return false;
}

function BindEnterKey(sAction) {

	//mashing the Enter key should either execute a Job Search or perform 
	//a Candidate/Client login, depending on the circumstances
	if (sAction == "login") {

		var code = e.keyCode || e.which;

		if (code == 13) {
			__doPostBack('ctl00$ucClientLogin$uxClientLoginButtonText', '');
		}

	}

}

