﻿// JScript File

Cufon.replace('#maincol h1');

$(document).ready(
	function() {
		$("body").addClass('hasJS');
		setupTextFieldClear();
		togglePasswordField();
		inputBoxText();
		newsHome();
		gaTracking();
		firstChildSelector();
		navBUcorners();
		whatwedoContainer();
		login();
		externalLinks();
	}
);


function externalLinks() {
	$('a[href^=http],a[href^=/relatedlink.html?class=link&link=http]').each(function() {
		if ($(this).attr("class") != "noExternalLinks" && $(this).attr("class") != "close")
		{
			var titleStr = $(this).attr("title");
			if (titleStr!="" && titleStr!=undefined) {
				titleStr += " (Press shift and click to open a new browser window)";
			} else {
				titleStr = "(Press shift and click to open a new browser window)";	
			}
			$(this).attr("title", titleStr);

			$(this).click(function() {
				var strFromURL = window.location.href;
				var strToURL = this.href.substring(this.href.lastIndexOf("http"));
				window.open("/page.aspx?class=link&ToURL=" + escape(strToURL) + "&FromURL=" + escape(strFromURL));
				return false;
			});
		}
	});
	$('a[href^=/relatedlink.html?class=file&file=/cms/files/]').each(function() {
		var titleStr = $(this).attr("title");
		if (titleStr!="" && titleStr!=undefined) {
			titleStr += " (new window)";
		} else {
			titleStr = "(new window)";	
		}
		$(this).attr("title", titleStr);
		//alert(this.href.substring(this.href.indexOf("/cms")));
		$(this).click(function() {
			var strFromURL = window.location.href;
			var strToURL = this.href.substring(this.href.lastIndexOf("/cms"));
			window.open("/redirect.aspx?class=file&ToURL=" + escape(strToURL) + "&FromURL=" + escape(strFromURL));
			return false;
		});
	});
}


function login() {
  $("#login").click(
		function() {
			var redirect = $("#redirect").val();
			var url = "http://" + document.domain + "/" + redirect;
			var param = $("#login").parents("form").serialize();
      
      jQuery.ajax({
        type: 'post',
        url: '/ajax.aspx', 
        data: param,
        success: function(msg){
					var headerText = "";

					jQuery.get(url, 
						function(data) {
							var welcomeBlock = $("#welcomeBlock", data);
							jQuery.facebox(welcomeBlock.html());
							
							headerText = headerText + "<p>" + $("#facebox div.content h1").html() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
							
							var applicationList = $("#facebox div.content div.logo");
							var currentDivLogo;
							var currentLink;
							
							for (var i=0; i<applicationList.length; i++)
							{
								currentDivLogo = applicationList[i];
								currentLink = $(currentDivLogo).find("a").attr("href");
								currentLink = currentLink.substring(currentLink.indexOf("link=") + 5);
								headerText = headerText + "<a target=\"_new\" href=\"" + currentLink + "\">" + $(currentDivLogo).find("a").attr("title") + "</a> | ";
							}
							
							headerText = headerText + "<a href=\"?logout=1\" class=\"noExternalLinks\">Logout</a></p>";
							
							$("#customerLogin").html(headerText);
							$("#loginError").html(" ");
							Cufon('h1');
							externalLinks();
						}
					)
        },
        error: function(xhr) {
					if (xhr.responseText != '')
					{
						$("#loginError").html(xhr.responseText);
					}
					else
					{
						$("#loginError").html("The email address or password is incorrect. Please try again or <a href=\"/10686/Forgotten-your-password.html\">reset your password</a>.");
					}
        },
				complete: function() {
					//alert('complete !!!');
				}
			})	
			
		  return false;
		}
  );
}

//hide original text on focus
function setupTextFieldClear() {
	$(".EnterClear").bind("focus", enterClearText);
}

function enterClearText() {
	this.value = '';
	$(this).unbind("focus", enterClearText);
	return false;
}

//function to toggle from uncrypted password field to the crypted one
function togglePasswordField() {
	$("#passwordUncrypted").removeClass("hide");
	$("#passwordCrypted").addClass("hide");
	$("#passwordUncrypted").focus(
		function() {
			$(this).addClass("hide");
			$("#passwordCrypted").removeClass("hide");
			$("#passwordCrypted").focus();
		}
	)
}


//show original text on blur if no text entered
function inputBoxText() {
	$("#searchForm #sField").blur(
		function() {
			if (this.value == "") {
				this.value = "Enter your keywords here";
				$(this).bind("focus", enterClearText);
			}
		}
	)

	$("#customerLogin #9476").blur(
		function() {
			if (this.value == "") {
				this.value = "Please Enter your Email Address";
				$(this).bind("focus", enterClearText);
			}
		}
	)

	$(".subscribeForm #7124").blur(
		function() {
			if (this.value == "") {
				this.value = "Name";
				$(this).bind("focus", enterClearText);
			}
		}
	)

	$(".subscribeForm #7112").blur(
		function() {
			if (this.value == "") {
				this.value = "Email";
				$(this).bind("focus", enterClearText);
			}
		}
	)
}

//home page functionality
function newsHome() {
	$("#newsHome li, #whatwedo li, #otherArticles li, #featureArticle").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	$('#newsHome li, #whatwedo li, #otherArticles li, #featureArticle').click(
		function() {
			location.href = $(this).find('a:eq(0)').attr('href');
		}
	);
}

function validEmail(email)
{
	var pattern = "^((?:(?:(?:[a-zA-Z0-9][\.\-/+_]?)*)))\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$";
	var modifiers = "g";
	var reg = new RegExp(pattern, modifiers);
	
	return reg.test(email);
}

//Adds tracking events where appropriate
function gaTracking() {
	$('a[href$=pdf]').click(
		function() {
			pageTracker._trackEvent('downloads', 'click', $(this).attr('href'));
		}
	);
	
	if (window.location.search == "?utm_source=Jobs-Alert&utm_medium=email&utm_campaign=recruitment")
	{
		var url = window.location.host + window.location.pathname;
		pageTracker._trackEvent('jobAlert', 'email_click', url);
	}

	if (window.location.pathname.indexOf("/6946/") > -1)
	{
		$("input[type=submit]").click(
			function() {
				if ($("#7124").val() != '' && validEmail($("#7112").val()))
				{
					pageTracker._trackEvent('Job Alert Signup', 'click', window.location.pathname);
					//alert("name: " + $("#11267").val() + "\r\nemail: " + $("#11266").val());
				}
			}
		);
	}

	if (window.location.pathname.indexOf("/11269/") > -1)
	{
		$("input[type=submit]").click(
			function() {
				if ($("#11267").val() != '' && validEmail($("#11266").val()))
				{
					pageTracker._trackEvent('Job Alert Signup', 'click', window.location.pathname);
					//alert("name: " + $("#11267").val() + "\r\nemail: " + $("#11266").val());
				}
			}
		);
	}
}

// Adds a class to the first h3 of every .featureBlock
function firstChildSelector() {
	$(".featureBlock h3:first-child").addClass("firstChild");
};

// Creates the corners on the business units nav
function navBUcorners() {
	$("#navBU ul li.first").append('<span class="cornerTL"></span><span class="cornerBL"></span>');
	$("#navBU ul li.last").append('<span class="cornerTR"></span><span class="cornerBR"></span>');
}

// Creates a container for whatwedo, adding a top border
function whatwedoContainer() {
	$("#whatwedo").wrap('<div id="whatwedoContainer"></div>');
}