
$(document).ready(function() {
	setSiteMap();

	// Setup Search function
	setSearch();

	// Setup Flash text
	//setSIFR();  // Moved this into the cookie check.

	//Setup high contrast switching
	if($('#toggle_high_contrast').length){
    	if(!checkContrastCookie()){
    	    setSIFR();
    		setContrast();
    	} else{
    		enableHighContrast();
			$('#toggle_high_contrast').text('View site in normal contrast');                        
            $('#toggle_high_contrast').attr("title", "Click here to view site in normal contrast");
		}		
    }
    

	setFAQ();
		
	//set Carousel Hover
	setCarouselHover();
	
	setLogin();

	//set Email to a friend popup
	setEmailButton();

	setKeyboardNav();
	setInputClear();
	setIE6Background();
	dropDownFix();
	$('.timeline-list-item').css("display","none");
	$('#item06').css("display","block");
	
	$(".timeline-nav a").each(function() {
	$(this).bind("click", function(){
								  $('.timeline-list-item').css("display","none"); 
								  $('a').removeClass("active"); 
								   $($(this).attr("href")).show();
								   $(this).addClass("active");
								   return false;
});
})
	
	
	
	// If image feature exists call function to initiate
	if($('div.image-feature.gallery').length){
		$('div.image-feature.gallery').wtdgallery();
	}
	$('select, div.form-container input, div.form-container textarea, #email-content-container input, #email-content-container textarea').wtdforms();

	setCheckBox();
	loadFancyBox();
	
	// If ibanner feature exists call function to initiate
	if($('div.banner-feature').length){
		$('div.banner-feature').wtdbanner();
	}

	// If top tips exists call function to initiate
	if($('div.toptips').length){
		$('div.toptips').wtdtoptips();
	}

	// If carousel exists call function to initiate
	if($('div.carousel-container').length){
		$('div.carousel-container').wtdcarousel();
	}

	// If flickr exists call function to initiate
	if($('div.flickr-feed').length){
		$('div.flickr-feed').wtdflickr();
	}
	
	// Form Validation Initialised
	if($('.validate-form').length){
		setDatePicker();		
		
		$('.validate-form').validate({
			rules: {
				newPassword: "required",
				confNewPassword: {
					equalTo: "#newPassword"
				}
			},
			debug: true,
			errorElement: "em",
			errorPlacement: function(error, element) {
				if(element.hasClass('select-replacement')){
					error.appendTo( element );
				}
				else{
					error.appendTo( element.parent() );
				}
			},
			success: function(label) {
				label.addClass("success");
				if(label.parent().hasClass('select-replacement')){
					label.parent().css({
						width:"373px"
					});
				}
			}
		});
		
		clearLabels();
		accountNumberPopup();

		setaddNames();
		backtoTop();
	}
	
	thamesTunnel();
});

function thamesTunnel(){

	if($('#os-body-main').length){
		$('ul.primary-nav li a').click(function(){
			return false;
		});

		if (window.PIE) {
			$('fieldset.question-container').each(function() {
				PIE.attach(this);
		   });
		}
	}
	
/*	var bgImages = ['bg-image1.jpg','bg-image2.jpg','bg-image3.jpg']

	$('div.content').css('background','url("common/images/' + bgImages[Math.floor(Math.random()*3)] + '") top center no-repeat #414340'); */

	if($('div.content.content-page div.section').height() > 1000){
		$('div.content.content-page div.sidebar#leftside').height($('div.content.content-page div.section').height() + 10);
	}
	else if($('div.content.content-page div.sidebar#leftside').height() > 1000){
		$('div.content.content-page div.section').height($('div.content.content-page div.sidebar#leftside').height() + 10);
	}

	if($('#map').length){
		var latlng = new google.maps.LatLng(51.502777, -0.254716);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI:true,
			zoomControl:true,
			streetViewControl:true,
				panControl:true
		};
		var map = new google.maps.Map(document.getElementById("map"),
		myOptions);

		var image = TEMPLATE_DIR + '/view/images/pin.png';
		var marker = new google.maps.Marker({
			position: latlng, 
			/*animation: google.maps.Animation.DROP,*/
			map: map, 
			icon: image
		});   
		//google.maps.event.addListener(marker, 'click', toggleBounce);
	}
}

function toggleBounce() {

  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}

function setSearch(){
	//Direct address search to Live Maps page
	$('input#submit-address').click(function(){
		var searchVal = $('input#address-search').val();
		window.open('http://192.168.0.82/index.htm?k=' + searchVal);
	});
}

function setCarouselHover(){
	$('div.carousel-hover').hide();
	$('div.carousel ul li.carousel-item').live('mouseenter',function(){
		$(this).find('div.carousel-hover').show();
	}).live('mouseleave',function(){
		$(this).find('div.carousel-hover').hide();
	});

	$('div.carousel ul li.carousel-item h3 a').live('focus',function(){
		$(this).parent().parent().find('div.carousel-hover').show();
	}).live('focusout',function(){
		$(this).parent().parent().find('div.carousel-hover').hide();
	});
}


function loadFancyBox(){
	if($('a.lightbox, div.lightbox a').length){
		if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
			return false;
		}
		else{
			$('a.lightbox, div.lightbox a').fancybox({
				padding : 7,
				titlePosition : 'over',
				'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-overlay">' + title + '</span>';
				}
			});
		}
	}
}

function setSiteMap(){
	
	// Hide Sitemap
	$('div.sitemap').css('display', 'none');
	
	$('div.sitemap-link ul li a').live('click', function(event) {
		if($('div.sitemap').is(':visible')){
			if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
				$('div.sitemap').css('display', 'none');
				$(this).removeClass('active');
			}
			else{
				$('div.sitemap').slideUp(1000, 'easeOutExpo');
				$(this).removeClass('active');
			}
				event.preventDefault();
		} 
		else {
			if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
				$('div.sitemap').css('display', 'block');
				$(this).removeClass('active');
			}
			else{
				$('div.sitemap').slideDown(1000, 'easeOutExpo');
				$(this).addClass('active');
			}
				event.preventDefault();
		}
	});

}

function setLogin(){
	$('a.logout').live('click', function(){
		$('div.profile-bar ul li:first-child').remove();
		$('div.profile-bar ul li:first-child').html('<a href="#" title="Logout" class="login">Login</a>');
		$('div.profile-bar ul li:last-child').html('<a href="#" title="Register">Register</a>');
	});
	$('a.login').live('click', function(){
		$('div.content-overlay').remove();

		// Find the height and width of the document for the overlay							   
		$documentHeight = $(document).height();
		$documentWidth = $(document).width();
			
		// Set the overlay
		$('body').prepend('<div class="content-overlay"><div class="content-box"></div></div>');
		$contentOffset = ($documentWidth / 2) - ($('div.content-box').width() / 2)
		$('div.content-box').css('left',$contentOffset);
		$('div.content-box').load('login.html' + ' #login-content-container', function() {
			// If content loaded then display overlay and content
			$('div.content-box').append('<ul class=\'close\'><li><a href=\'#\'>Close</a></li></ul>');
			$('div.content-box').fadeIn(600, 'easeOutExpo');

			sIFR.replace(albertExtraBold, {
			  onReplacement: function(fi){resetMovie(fi);},
			  selector: 'h1',
			  css: '.sIFR-root {  color:#9ebe2f; font-size:25px;}',
			  wmode: 'transparent',
			  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
			});
			
			$('ul.close li a, div.cancel-button a').click(function(){
				$('div.content-box, div.content-overlay').fadeOut(400, 'easeOutExpo', function(){
					$('div.content-box, div.content-overlay').remove();
				});
			});
			$('#login-content-container input').wtdforms();
			$('#login-content-container input').eq(0).focus();
		});
		return false;
	});
	
	$('div#login-content-container input.required').live('click',function(){
		$(this).parent().removeClass('error');
	});
}

function loginValidation(){
	var nameCheck = document.forms["login"]["username"].value;
	var passCheck = document.forms["login"]["password"].value;

	if(nameCheck == null || nameCheck == ""){
		$('input#username').parent().addClass('error');
		if(passCheck == null || passCheck == ""){
			$('input#password').parent().addClass('error');
			return false;
		}
		return false;
	}
	if(passCheck == null || passCheck == ""){
		$('input#password').parent().addClass('error');
		return false;
	}
	$('div.content-box, div.content-overlay').fadeOut(400, 'easeOutExpo', function(){
		$('div.content-box, div.content-overlay').remove();
	});

	$('div.profile-bar ul li:last-child').html('<a href="#" title="Logout" class="logout">Logout</a>');
	$('div.profile-bar ul li:first-child').html('<a href="#" title="My account">My account</a>');
	$('div.profile-bar ul').prepend('<li><span>Welcome: </span><span class="name">' + nameCheck + '</span></li>');
	if($('span.name').height() > 30){
		$('span.name').css({
			margin:"-7px 0 0 0"
		});
	}
	else{
		if($('span.name').width() > 109){
			$('span.name').css({
				'font-size':'1em',
				margin:"1px 0 0 0"
			});
			if($('span.name').width() > 109){
				$('span.name').css({
					'font-size':'0.75em',
					margin:"4px 0 0 0"
				});
				if($('span.name').width() > 109){
					$('span.name').css({
						'font-size':'0.583em',
						margin:"7px 0 0 0"
					});
				}
			}
		}
	}
	/*$('body').prepend('<div class="content-sent-box"></div>');
	$contentOffset = ($documentWidth / 2) - ($('div.content-sent-box').width() / 2)
	$('div.content-sent-box').css('left',$contentOffset);
	$('div.content-sent-box').load('emailtoafriend.html' + ' #email-thankyou-container', function() {
		// If content loaded then display overlay and content
		$('div.content-sent-box').append('<ul class=\'close\'><li><a href=\'#\'>Close</a></li></ul>');
		$('div.content-sent-box').fadeIn(600, 'easeOutExpo');

		sIFR.replace(albertExtraBold, {
		  selector: 'h1',
		  css: '.sIFR-root {  color:#3894af; font-size:25px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});

		sIFR.replace(albert, {
		  selector: 'div#email-thankyou-container p',
		  css: '.sIFR-root {  color:#000000; font-size:15px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
		
		$('ul.close li a, div.cancel-button').click(function(){
			$('div.content-sent-box').fadeOut(400, 'easeOutExpo', function(){
				$('div.content-sent-box').remove();
			});
		});
	});*/

	return false;
}

function setEmailButton(){
	$('a.button_email').live('click', function(){
		$('div.content-box').remove();

		// Find the height and width of the document for the overlay							   
		$documentHeight = $(document).height();
		$documentWidth = $(document).width();
			
		// Set the overlay
		$('body').prepend('<div class="content-box"></div>');
		$contentOffset = ($documentWidth / 2) - ($('div.content-box').width() / 2)
		$('div.content-box').css('left',$contentOffset);
		$('div.content-box').load('emailtoafriend.html' + ' #email-content-container', function() {
			// If content loaded then display overlay and content
			$('div.content-box').append('<ul class=\'close\'><li><a href=\'#\'>Close</a></li></ul>');
			$('div.content-box').fadeIn(600, 'easeOutExpo');

			sIFR.replace(albertExtraBold, {
			  onReplacement: function(fi){resetMovie(fi);},
			  selector: 'h1',
			  css: '.sIFR-root {  color:#3894af; font-size:25px;}',
			  wmode: 'transparent',
			  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
			});

			sIFR.replace(albertExtraBold, {
			  onReplacement: function(fi){resetMovie(fi);},
			  selector: 'label',
			  css: '.sIFR-root {  color:#000000; font-size:15px;}',
			  wmode: 'transparent',
			  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
			});
			
			$('ul.close li a, div.cancel-button').click(function(){
				$('div.content-box').fadeOut(400, 'easeOutExpo', function(){
					$('div.content-box').remove();
				});
			});
			$('#email-content-container input, #email-content-container textarea').wtdforms();
			$('#email-content-container input').eq(0).focus();
		});
	});
	
	$('div#email-content-container input.required').live('click',function(){
		$(this).parent().removeClass('error');
	});
}

function emailValidation(){
	var nameCheck = document.forms["emailToAFriend"]["emailName"].value;
	var emailCheck = document.forms["emailToAFriend"]["emailAddress"].value;
	var atCheck = emailCheck.indexOf("@");
	var dotCheck = emailCheck.lastIndexOf(".");

	if(nameCheck == null || nameCheck == ""){
		$('input#emailName').parent().addClass('error');
		if(atCheck < 1 || dotCheck < atCheck + 2 || dotCheck + 2 >= emailCheck.length || emailCheck == null || emailCheck == ""){
			$('input#emailAddress').parent().addClass('error');
			return false;
		}
		return false;
	}
	if(atCheck < 1 || dotCheck < atCheck + 2 || dotCheck + 2 >= emailCheck.length || emailCheck == null || emailCheck == ""){
		$('input#emailAddress').parent().addClass('error');
		return false;
	}
	$('div.content-box').fadeOut(400, 'easeOutExpo', function(){
		$('div.content-box').remove();
	});
	$('body').prepend('<div class="content-sent-box"></div>');
	$contentOffset = ($documentWidth / 2) - ($('div.content-sent-box').width() / 2)
	$('div.content-sent-box').css('left',$contentOffset);
	$('div.content-sent-box').load('emailtoafriend.html' + ' #email-thankyou-container', function() {
		// If content loaded then display overlay and content
		$('div.content-sent-box').append('<ul class=\'close\'><li><a href=\'#\'>Close</a></li></ul>');
		$('div.content-sent-box').fadeIn(600, 'easeOutExpo');

		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h1',
		  css: '.sIFR-root {  color:#3894af; font-size:25px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});

		sIFR.replace(albert, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'div#email-thankyou-container p',
		  css: '.sIFR-root {  color:#000000; font-size:15px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
		
		$('ul.close li a, div.cancel-button').click(function(){
			$('div.content-sent-box').fadeOut(400, 'easeOutExpo', function(){
				$('div.content-sent-box').remove();
			});
		});
	});

	return false;
}

function setSIFR(){

	// Setting H1 and color dependent on site area
	sIFR.replace(albert, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.content.home-page h1',
	  css: '.sIFR-root {  color:#555555; font-size:38px; leading:-7;}',
	  wmode: 'transparent',
	  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
	});
	if($('div.error').length){
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h1',
		  css: '.sIFR-root {  color:#a33f97; font-size:110px; }',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});

		sIFR.replace(albert, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h2',
		  css: '.sIFR-root {  color:#666766; font-size:30px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
		
		sIFR.replace(albert, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h2.timeline-list-item-name',
		  css: '.sIFR-root {  color:#666766; font-size:30px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
		
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h3',
		  css: '.sIFR-root {  color:#555555; font-size:20px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
	}
	if($('div.disaster').length){
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'div.header-inner ul li',
		  css: ['a {  color:#FFFFFF; font-size:40px; text-decoration:none; text-align:right;}',
			  'a:hover {  color:#FFFFFF; text-decoration:underline;}'],
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});

		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h1',
		  css: '.sIFR-root {  color:#01cae8; font-size:30px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});

		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h2',
		  css: '.sIFR-root {  color:#01cae8; font-size:20px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
	}
	if($('div.content').hasClass('landing-page') == false){
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h1',
		  css: '.sIFR-root {  color:#666666; font-size:28px;}',
		  wmode: 'transparent',
		  ratios:[7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
	}
	sIFR.replace(albert, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'p.strapline',
	  css: '.sIFR-root {  color:#FFFFFF; font-size:16px; line-height:42px;}',
	  wmode: 'transparent',
	  ratios: [8, 1.31, 12, 1.27, 16, 1.21, 17, 1.2, 23, 1.19, 24, 1.16, 25, 1.18, 27, 1.17, 32, 1.16, 33, 1.17, 41, 1.16, 58, 1.15, 64, 1.14, 65, 1.15, 95, 1.14, 96, 1.13, 100, 1.14, 101, 1.13, 102, 1.14, 104, 1.13, 109, 1.14, 113, 1.13, 114, 1.14, 1.13]
	});
	
	if($('div.sidebar h2').hasClass('create-case') == false && $('div.sidebar h2').hasClass('add-product') == false){
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'div.sidebar h2',
		  css: '.sIFR-root {  color:#FFFFFF; font-size:28px;}',
		  wmode: 'transparent',
		  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
	}

	sIFR.replace(albertExtraBold, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.search-container h3',
	  css: '.sIFR-root {  color:#FFFFFF; font-size:16px;}',
	  wmode: 'transparent'
	});

	sIFR.replace(albertThin, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.content.wide div.section h3',
	  css: ['.sIFR-root {  color:#FFFFFF; font-size:27px;}',
	  '.sIFR-root a { color: #ffffff; text-decoration:none; cursor:pointer;}',
	  '.sIFR-root a:hover { color: #ff0000; text-decoration:none; }'],
	  wmode: 'transparent'
	});
	sIFR.replace(albertThin, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'ul.landing-highlights li span.heading',
	  css: '.sIFR-root {  color:#FFFFFF; font-size:24px; cursor:pointer;}',
	  wmode: 'transparent',
	  onRelease: function(fi) { sIFRLink(fi.getAncestor()) } 
	});/**/
	sIFR.replace(albertExtraBold, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.article.amp5 div.map-header h3',
	  css: '.sIFR-root {  color:#97af51; font-size:25px;}',
	  wmode: 'transparent',
	  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
	});
	sIFR.replace(albertExtraBold, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.map-header h3',
	  css: '.sIFR-root {  color:#005599; font-size:25px;}',
	  wmode: 'transparent',
	  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
	});
	sIFR.replace(albert, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.key-projects h3',
	  css: '.sIFR-root {  color:#555555; font-size:20px;}',
	  wmode: 'transparent'
	});


	// Landing Page Tout Headings
	// Applies default landing page headings
	if($('body').hasClass('contact-us') == false){
		sIFR.replace(albertExtraBold, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'div.landing-page div.article h3',
		  css: ['.sIFR-root {  color:#FFFFFF; font-size:24px; cursor:pointer;}',
		  '.sIFR-root a { color: #FFFFFF; text-decoration:none; cursor:pointer;}',
		  '.sIFR-root a:hover { color:#FFFFFF; text-decoration:underline;}'],
		  wmode: 'transparent',
		  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
		});
	}
	// Applies section specific landing page headings on contact page
	else{
		$('div.landing-page div.article span').each(function(index, element){
			// Check which article heading color is required
			if($(element).hasClass('your-account') == true){
				var textColor = '#9ab931';
				var className = 'your-account';
			}
			else if($(element).hasClass('got-problem') == true){
				var textColor = '#7e4a80';
				var className = 'got-problem';
			}
			else if($(element).hasClass('moving-home') == true){
				var textColor = '#cd7423';
				var className = 'moving-home';
			}
			else if($(element).hasClass('water-meters') == true){
				var textColor = '#b61277';
				var className = 'water-meters';
			}
			else if($(element).hasClass('your-water') == true){
				var textColor = '#0f9ab7';
				var className = 'your-water';
			}
			// Apply color to those with the required class
			sIFR.replace(albertExtraBold, {
			  onReplacement: function(fi){resetMovie(fi);},
			  selector: 'div.landing-page div.article span.' + className,
			  css: ['.sIFR-root {  color:' + textColor + '; font-size:24px; cursor:pointer;}',
					'a:hover .sIFR-root a{ color: #FFFFFF; text-decoration:none; cursor:pointer;}'],
			  wmode: 'transparent',
			  onRelease: function(fi) { sIFRLink(fi.getAncestor()) }, 
			  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
			});
		});
	}
	sIFR.replace(albertBold, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.header div.image h4',
	  css: '.sIFR-root {  color:#FFFFFF; font-size:20px; font-weight:bold;}',
	  wmode: 'transparent',
	  forceSingleLine:true,
	  ratios:[8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 25, 1.19, 33, 1.17, 42, 1.16, 47, 1.15, 48, 1.16, 66, 1.15, 70, 1.14, 72, 1.15, 78, 1.14, 79, 1.15, 1.14]
	});

	sIFR.replace(albertBold, {
	  onReplacement: function(fi){resetMovie(fi);},
	  selector: 'div.header div.article h3',
	  css: ['.sIFR-root {  color:#193c72; font-size:20px; font-weight:bold;}',
	  '.sIFR-root.image h3 { color: #ffffff;}'],
	  wmode: 'transparent',
	  forceSingleLine:true,
	  ratios:[8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 25, 1.19, 33, 1.17, 42, 1.16, 47, 1.15, 48, 1.16, 66, 1.15, 70, 1.14, 72, 1.15, 78, 1.14, 79, 1.15, 1.14]
	});
	
	// Setting H2 and color dependent on site section

	
		sIFR.replace(albert, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'h2',
		  css: '.sIFR-root {  color:#9ebe2f; font-size:21px; leading:0;}',
		  wmode: 'transparent',
		  debug: [8, 1.31, 12, 1.27, 16, 1.21, 17, 1.2, 23, 1.19, 24, 1.16, 25, 1.18, 27, 1.17, 32, 1.16, 33, 1.17, 41, 1.16, 58, 1.15, 64, 1.14, 65, 1.15, 95, 1.14, 96, 1.13, 100, 1.14, 101, 1.13, 102, 1.14, 104, 1.13, 109, 1.14, 113, 1.13, 114, 1.14, 1.13]
		});
		/*
		sIFR.replace(albert, {
		  onReplacement: function(fi){resetMovie(fi);},
		  selector: 'span.timeline-list-item-name',
		  css: '.sIFR-root {  color:#555555; font-size:20px; leading:0;}',
		  wmode: 'transparent',
		  debug: [8, 1.31, 12, 1.27, 16, 1.21, 17, 1.2, 23, 1.19, 24, 1.16, 25, 1.18, 27, 1.17, 32, 1.16, 33, 1.17, 41, 1.16, 58, 1.15, 64, 1.14, 65, 1.15, 95, 1.14, 96, 1.13, 100, 1.14, 101, 1.13, 102, 1.14, 104, 1.13, 109, 1.14, 113, 1.13, 114, 1.14, 1.13]
		});
		*/
}



/*function setLightboxHover(){
	$('body').append('<div id=\'magnify-cursor\'></div>')
	$('div.image-feature.lightbox a').each(function(index, element){
		$(element).mouseout(function(){
			$('#magnify-cursor').hide();
			return false;
		});
		$(element).mouseenter(function(){
			$('#magnify-cursor').show();
			return false;
		});
		$(element).mousemove(function(e){
			$('#magnify-cursor').css({
				'left': e.clientX,
				'top': e.clientY
			});
		});
		  
	});
}*/

function setFAQ(){
	$('ul.open-close-list li.faq-content').hide();
	$('ul.open-close-list li a span').css("display","block");
	$('ul.open-close-list ul.downloads').hide();
	
	//$('ul.open-close-list li:first-child ul li:first-child').show();
	//$('ul.open-close-list li:first-child').addClass('active');

	$('ul.open-close-list li a').click(function(event){
		
		if($(this).parent().find('li.faq-content').is(':visible')){
			$(this).parent().find('li.faq-content').hide();
			$(this).parent().parent().find('li').removeClass('active');
			$(this).parent().removeClass('active');
			event.preventDefault();
			$(this).parent().find('ul.downloads').hide();
				$('ul.open-close-list li a span').css("display","block");
		} 
		else {
			$(this).parent().parent().children().children().children().hide();
			$(this).parent().children().children().show();
			$(this).parent().parent().find('li').removeClass('active');
			$(this).parent().addClass('active');
			event.preventDefault();
			$(this).parent().find('ul.downloads').show();
				$('ul.open-close-list li a span').css("display","block");
		}
	});
	
	$('ul.open-close-list li ul li ul li a').unbind('click');
}

function setKeyboardNav(){
	$('ul.secondary-nav li a').focus(function() {
		$('ul.secondary-nav li').removeClass('hover');
		$(this).parent().addClass('hover');
	});	
	
	$('ul.secondary-nav li a').click(function() {
		$(this).parent().removeClass('hover');
	});

	$('ul.secondary-nav li ul li a').unbind('focus');
		
	$('div.header div.search input, div.logo a').focus(function() {
		$('ul.secondary-nav li').removeClass('hover');
	});

}

function setIE6Background(){
	if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
		var homepageWidth = $(window).width();
		var contentWidth = $('div.content-inner').width();
		var backgroundImage = $('div.home-page').css('background-image') + ' #228CB4 repeat-x';
		var landingImage = $('div.landing-page').css('background-image');
		var head = $("div.header-inner");
		var offset = head.offset();

		if($('div.content').hasClass('landing-page') == true){
			offset.left = offset.left - 27;
			$('div.content').css('padding-left',offset.left);
			/*$leftOffset = (($(window).width() - contentWidth) / 4) + 5;
			$('div.content-background').css('margin-left', $leftOffset + 'px');
			//$('div.content-background').css('width',$(window).width())
			*/$('body').css({
				'background':landingImage,
				'background-position':'0 0',
				'margin':'0 auto'
			});
		}
		else if($('div.content').hasClass('home-page') == true){
			offset.left = offset.left - 35;
			$('div.content').css('padding-left',offset.left);
			$('body').css({
				'background':backgroundImage,
				'background-position':'0 136px',
				'margin':'0 auto'
			});
		}
		else{
			offset.left = offset.left - 3;
			$('div.content').css('padding-left',offset.left);
			//$leftOffset = (($(window).width() - contentWidth) / 4 - 50) ;
			//$('div.content-background').css('margin-left', $leftOffset + 'px');
			
			//var leftMargin = $('div.header-inner');
			//var offset = leftMargin.offset();
			//$('div.content-background').css('margin-left', offset.left + 'px');
			
			/*$leftOffset = (($(window).width() - contentWidth) / 4) -1;
			$('div.content-background').css('margin-left', $leftOffset + 'px');
			$('body').css({
				'background':backgroundImage,
				'background-position':'0 136px'
			});*/
		}

		//$leftOffset = (($(window).width() - contentWidth) / 4) - 1;
		//$('div.content-background').css('margin-left', $leftOffset + 'px');
		//$('div.content.home-page').css('width', $(window).width() + 'px');
	}
}

//$(window).resize(function() {
//  setIE6Background();
//});


$(window).resize(function(){
	window_resize();
});

var resizeTimeoutId;

function window_resize() {
    window.clearTimeout(resizeTimeoutId);
	resizeTimeoutId = window.setTimeout('setIE6Background();', 100);
}

function setInputClear(){
	$defaultVal = '';

	$('input[type="text"]').each(function(index) {
		$defaultVal = $(this).val();
		$(this).attr("rev", $defaultVal);
	 });

	$('input[type="text"]').focus(function(){
		$defaultVal = $(this).attr("rev");
		if ($(this).val() == $defaultVal)
		{
			$(this).val('');
		}
	});

	$('input[type="text"]').blur(function(){
		if($(this).val() == ''){
			$(this).val($defaultVal);
		}
	});
}

function dropDownFix(){	
	
	if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
		$('ul.secondary-nav li a:first-child').hover(function() {
			$(this).parent().addClass('hover');
		}, function () {
			$(this).parent().removeClass('hover');
		});
		
		$('ul.secondary-nav').hover(function() {
			$(this).append('<iframe></iframe>');
			$(this).find('iframe').css('filter', 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)');
			$(this).find('iframe').css('opacity', '0');
		}, function () {
			$(this).find('iframe').remove();
		});
	
		$('ul.secondary-nav li ul li a').unbind('hover');
	}
}


function setCheckBox(){
	
	$('input[type="checkbox"]').each(function(index, element){
		$(element).ezMark();
		if($(element).parent().parent().parent().parent().parent().hasClass('plumber-search')){
			$(element).parent().css('margin-top','6px');
		}
	});
	

	$('input#search-whole-site').click(function(){
		$wholeChecked = $(this).attr('checked');		

		$('div.search-filter input[type="checkbox"]').each(function(index, element){
			if ($wholeChecked == true)
			{
				$(element).attr('checked', 'true');
				$(element).parent().addClass('ez-checked')
			}else{
				$(element).attr('checked', '');
				$(element).parent().removeClass('ez-checked')
			}
		});
	});


	$('div.search-filter input[type="checkbox"]').click(function(){

		$thisChecked = $(this).attr('checked');

		if ($thisChecked == false)
		{
			$('input#search-whole-site').attr('checked', '')
			$('input#search-whole-site').parent().removeClass('ez-checked')
		}
	});

}

function setRadioButton(){
	
	$('input[type="radio"]').each(function(index, element){
		//if($(element).parent().parent().parent().parent().hasClass('plumber-search') == false){
			$(element).ezMark();
		//}
		//else{
		//	$(element).ezMark({checkboxCls:'ez-checkbox-plumber', checkedCls: 'ez-checked-plumber'});
		//}
	});
}

// IE6 PNG Fix

function fixPNG(myImage){
 
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
		var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
		var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
		var imgTitle = (myImage.title) ? 
               "title=\"" + myImage.title  + "\" " : "title=\"" + myImage.alt + "\" "
		var imgStyle = "display:block; margin-top:0px;" + myImage.style.cssText
		var myParent = myImage.parentNode
 
		if (myParent.nodeName == "A"){
			var aHref=myParent.href;
			var aTarget=""
			var aStyle="style=\" display:block; width:" + myImage.width + "px; height:" + myImage.height + "px;\" "
			if (myParent.target !=="") {
				aTarget = "target=\""+ myParent.target + "\"";
			}
			var strNewHTML = "<span " + imgID + imgClass
			+ imgTitle + " style=\"" + "width:" + myImage.width 
			+ "px; height:" + myImage.height 
			+ "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + myImage.src + "\', sizingMethod='image');\">"
			+ "<a " + aStyle +  "href=\""+aHref + "\" target=\""+ aTarget + "\" ><!-- --></a> </span>"
			myParent.outerHTML = strNewHTML
		}
		else{
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + myImage.width 
			+ "px; height:" + myImage.height 
			+ "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + myImage.src + "\', sizingMethod='image');\"></span>"
			myImage.outerHTML = strNewHTML
		}
    }
}

// Form Validation

function clearLabels(){
	jQuery('input.reset-button,input.reset-form-button').click(function(){
		jQuery('em.error').remove();
		jQuery('.error').removeClass('error');
		jQuery('.date-error').removeClass('date-error');
		jQuery('div.tooltip-container').remove();
	});
}

function accountNumberPopup(){
	
	// Hide your account number help image
	jQuery('ul.account-number li ul').css('left', '-999em');
	jQuery('div.field').css('position', 'relative');
	jQuery('div.radio').css('position', 'relative');
	jQuery('div.radio-validation-group div.radio').css('position', 'static');
	jQuery('div.check').css('position', 'relative');
	jQuery('div.date-field').css('position', 'relative');
	jQuery('form').find('iframe').remove();
	
	jQuery('ul.account-number li').unbind('mouseover');
	jQuery('ul.account-number li').unbind('mouseout');

	jQuery('ul.account-number li').mouseover(function() {
		jQuery(this).parent().css('z-index', '101');
		jQuery(this).find('ul').prepend('<iframe></iframe>');
		jQuery(this).find('iframe').css('filter', 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)');
		jQuery(this).find('iframe').css('opacity', '0');
		jQuery('div.field').css('position', 'static');
		jQuery('div.radio label').css('position', 'static');
		jQuery('div.radio').css('position', 'static');
		jQuery('div.check').css('position', 'static');
		jQuery('div.date-field').css('position', 'static');
		jQuery(this).find('ul').css('left', '-30px');
		jQuery('div.tooltip-container').remove();
		return false;
	}).mouseout(function(){
		jQuery(this).parent().css('z-index', '100');
		jQuery(this).find('ul').css('left', '-999em');
		jQuery(this).find('iframe').remove();
		jQuery('div.field').css('position', 'relative');
		jQuery('div.radio').css('position', 'relative');
		jQuery('div.radio-validation-group div.radio').css('position', 'static');
		jQuery('div.date-field').css('position', 'relative');
		return false;
	});

	jQuery('ul.account-number li a').click(function(){
		return false;
	});
}

function setaddNames(){
	// Hide all items in hidden group
	jQuery('ul.show-hidden-group li.remove-contact').css('display', 'none');	
	jQuery('div.hidden-group').css('display', 'none');	
	
	var count = 0;
	
	jQuery('ul.show-hidden-group li.add-contact a').click(function(){
		count = count + 1;
		var hiddenGroup = jQuery(this).parent().parent().parent().find('div.hidden-group');	
		jQuery('ul.show-hidden-group').css('padding-bottom', '15px');
		hiddenGroup.each(function(index, element){
			
			jQuery('ul.show-hidden-group li.remove-contact').css('display', 'block');
			
			// Check the display state of each item
			var displayState = jQuery(element).css('display');

			// The last item reached that is display:none; is made visible and then return false so loop can start again
			if(displayState == 'none'){
				jQuery(element).css('display', 'block');
				jQuery(element).find('input.hidden-required').addClass('required');
				return false;
			}
		});
		return false;												   
	});
	jQuery('ul.show-hidden-group li.remove-contact a').click(function(){
		count = count - 1;
		var hiddenGroup = jQuery(this).parent().parent().parent().find('div.hidden-group');	
		hiddenGroup.each(function(index, element){
			
			// Check the display state of each item
			var displayState = jQuery(element).css('display');

			if(count == 0){
				jQuery('ul.show-hidden-group li.remove-contact').css('display', 'none');
					jQuery('ul.show-hidden-group').css('padding-bottom', '0');
			}

			// The last item reached that is display:block; is hidden and then return false so loop can start again
			if(displayState == 'block'){
				jQuery(element).css('display', 'none');
				jQuery(element).find('input.hidden-required').removeClass('required');
				return false;
			}

		});
		return false;												   
	});
}

function setDatePicker(){
	count = 0;

	jQuery('.date-pick').each(function(index, element){
		jQuery(element).attr('id', 'date-pick' + index);
	
		$startDay = jQuery('.date-pick').parent().parent().parent().find('select.d').find('option:eq(1)').val();
		$startMonth = jQuery('.date-pick').parent().parent().parent().find('select.m').find('option:eq(1)').val();
		$startYear = jQuery('.date-pick').parent().parent().parent().find('select.y').find('option:eq(1)').val();

		$endDay = jQuery('.date-pick').parent().parent().parent().find('select.d').find('option:last-child').val();
		$endMonth = jQuery('.date-pick').parent().parent().parent().find('select.m').find('option:last-child').val();
		$endYear = jQuery('.date-pick').parent().parent().parent().find('select.y').find('option:last-child').val();
		
		// initialise the "Select date" link
		jQuery('.date-pick')
			.datePicker(
				// associate the link with a date picker
				{
					createButton:false,
					startDate:$startDay + '/' + $startMonth + '/' + $startYear,
					endDate:$endDay + '/' + $endMonth + '/' + $endYear
				}
			).bind(
				// when the link is clicked display the date picker
				'click',
				function()
				{
					$datepickID = jQuery(this).attr('id').substring(9);
					updateSelects(jQuery(this).dpGetSelected()[0], $datepickID);
					jQuery(this).dpDisplay();
					return false;
				}
			).bind(
				// when a date is selected update the SELECTs
				'dateSelected',
				function(e, selectedDate, $td, state)
				{
					$datepickID = jQuery(this).attr('id').substring(9);
					count = 1;
					updateSelects(selectedDate, $datepickID);
				}
			).bind(
				'dpClosed',
				function(e, selected)
				{
					updateSelects(selected[0], $datepickID);
				}
			);
	});
	
	jQuery('.d').each(function(index, element){
		jQuery(element).addClass('d' + index);
		jQuery(element).css('width', '65px');
	});
	jQuery('.m').each(function(index, element){
		jQuery(element).addClass('m' + index);	
		jQuery(element).css('width', '65px');
	});
	jQuery('.y').each(function(index, element){
		jQuery(element).addClass('y' + index);	
		jQuery(element).css('width', '77px');
	});
	
	var updateSelects = function (selectedDate, $myID)
	{
		var selectedDate = new Date(selectedDate);

		if(count < 1){
			jQuery('.d' + $myID + ' option[value=default-value]').attr('selected', 'selected');
			jQuery('.m' + $myID + ' option[value=default-value]').attr('selected', 'selected');
			jQuery('.y' + $myID + ' option[value=default-value]').attr('selected', 'selected');
		}
		else{
			jQuery('.d' + $myID + ' option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			jQuery('.m' + $myID + ' option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			jQuery('.y' + $myID + ' option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
			if(jQuery('#date-pick' + $myID).parent().parent().parent().find('em.error').length < 1){
				jQuery('#date-pick' + $myID).parent().parent().parent().append('<em class=\'error success\'></em>');
				jQuery('#date-pick' + $myID).parent().parent().parent().find('em.error').css('display', 'block');
			}
			else{
				jQuery('#date-pick' + $myID).parent().parent().parent().find('em.error').unbind('mouseover');
				jQuery('#date-pick' + $myID).parent().parent().parent().find('em.error').unbind('mouseout');
				jQuery('#date-pick' + $myID).parent().parent().parent().find('em.error').addClass('success');
				jQuery('#date-pick' + $myID).parent().parent().parent().find('select').removeClass('date-error');
				jQuery('#date-pick' + $myID).parent().parent().parent().find('div.select-replacement').removeClass('error');
			}
		}
		
		// Update Values in Select Box Replacements
		$selectedDay = jQuery('.d' + $myID).find(':selected').html();
		jQuery('.d' + $myID).parent().find('div').html($selectedDay);
		$selectedMonth = jQuery('.m' + $myID).find(':selected').html();
		jQuery('.m' + $myID).parent().find('div').html($selectedMonth);
		$selectedYear = jQuery('.y' + $myID).find(':selected').html();
		jQuery('.y' + $myID).parent().find('div').html($selectedYear);
		
	}
	// listen for when the selects are changed and update the picker
	 jQuery('.date-pick').each(function(index, element){
		jQuery('.d' + index + ', .m' + index + ', .y' + index + '').bind(
			'change',
			function()
			{
				var dayVal = jQuery('.d' + index).val();
				var monthVal = jQuery('.m' + index).val(); 
				var yearVal = jQuery('.y' + index).val();
				
				if(dayVal == 'default-value' || monthVal == 'default-value' || yearVal == 'default-value'){
					jQuery('#date-pick' + index).parent().parent().parent().find('em.error').removeClass('success');
					if(jQuery('#date-pick' + index).parent().parent().parent().find('em.error').length){
						jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('date-error');
						jQuery('#date-pick' + index).parent().parent().parent().find('div.select-replacement').addClass('error');
					}
					jQuery('#date-pick' + index).parent().parent().parent().find('em.error').text('Please enter a valid date');
					setPopup(jQuery('#date-pick' + index).parent().parent());
					count = 0;
					return false;
				}
				else{
					count = 1;
					if(checkDaysInMonth(dayVal, monthVal-1, yearVal) ==  true){
						var d = new Date(
									jQuery('.y' + index).val(),
									jQuery('.m' + index).val()-1,
									jQuery('.d' + index).val()
								);
								count = 1;
						jQuery(element).dpSetSelected(d.asString());
						jQuery('#date-pick' + index).parent().parent().parent().find('div.select-replacement').removeClass('error');
					}
					else{
						if(jQuery('#date-pick' + index).parent().parent().parent().find('em.error').length){
							jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('date-error');
							jQuery('#date-pick' + index).parent().parent().parent().find('em.error').removeClass('success');
							jQuery('#date-pick' + index).parent().parent().parent().find('em.error').text('Please enter a valid date');
							setPopup(jQuery('#date-pick' + index).parent().parent());
						}
						else{
							jQuery('#date-pick' + index).parent().parent().parent().append('<em class=\'error\'></em>');
							jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('date-error');
							jQuery('#date-pick' + index).parent().parent().parent().find('em.error').text('Please enter a valid date');
							jQuery('#date-pick' + index).parent().parent().parent().find('em.error').show();
							setPopup(jQuery('#date-pick' + index).parent().parent());
						}
					}
				}
			}
		);
	});
	// default the position of the selects to today
	jQuery('.date-pick').each(function(index, element){
		var today = new Date();
		updateSelects(today.getTime(), index);
	});
	
	jQuery('input.submit-button').click(function(){
		jQuery('.date-pick').each(function(index, element){
			var dayVal = jQuery('.d' + index).val();
			var monthVal = jQuery('.m' + index).val(); 
			var yearVal = jQuery('.y' + index).val();

			var requiredDate = jQuery(element).parent().parent().parent().hasClass('required');
			if(requiredDate == true){
				// Check if there are any DD, MM, or YYYY default values left and display or keep error icon
				if(dayVal == 'default-value' || monthVal == 'default-value' || yearVal == 'default-value'){
					if(jQuery('#date-pick' + index).parent().parent().parent().find('em.error').length < 1){
						jQuery('#date-pick' + index).parent().parent().parent().append('<em class=\'error\'>Please enter a valid date</em>');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('date-error');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').removeClass('valid');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('error');
						setPopup(jQuery('#date-pick' + index).parent().parent());
						jQuery('#date-pick' + index).parent().parent().parent().find('div.select-replacement').addClass('error');
					}
					else{
						jQuery('#date-pick' + index).parent().parent().parent().find('em.error').removeClass('success');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').removeClass('valid');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('error');
						setPopup(jQuery('#date-pick' + index).parent().parent());
					}
					jQuery('#date-pick' + index).parent().parent().parent().find('em.error').css('display', 'block');
				}
				else{
					// Check if it is a valid date and if so display tick icon
					if(checkDaysInMonth(dayVal, monthVal-1, yearVal) ==  true){
						jQuery('#date-pick' + index).parent().parent().parent().find('em.error').addClass('success');
						jQuery('#date-pick' + index).parent().parent().parent().find('em.error').text('');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').removeClass('date-error');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').removeClass('error');
						jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('valid');
						jQuery('#date-pick' + index).parent().parent().parent().find('em.error').css('display', 'block');
						jQuery('#date-pick' + index).parent().parent().parent().find('div.select-replacement').removeClass('error');
					}
					// Check if not a valid date and if so
					else{
						if(jQuery('#date-pick' + index).parent().parent().parent().find('em.error').length < 1){
							jQuery('#date-pick' + index).parent().parent().parent().append('<em class=\'error\'>Please enter a valid date</em>');
							jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('date-error');
							jQuery('#date-pick' + index).parent().parent().parent().find('select').removeClass('valid');
							jQuery('#date-pick' + index).parent().parent().parent().find('select').addClass('error');
							setPopup(jQuery('#date-pick' + index).parent().parent());
						}
						else{
							jQuery('#date-pick' + index).parent().parent().parent().find('em.error').removeClass('success');
							setPopup(jQuery('#date-pick' + index).parent().parent());
						}
						jQuery('#date-pick' + index).parent().parent().parent().find('em.error').css('display', 'block');
					}
				}
			}
		})
	});
}

function checkDaysInMonth($currentDay, $currentMonth, $currentYear) {
	$numDays = [31,(checkLeapYear($currentYear) ? 29:28),31,30,31,30,31,31,30,31,30,31][$currentMonth];

	if($currentDay > $numDays){
		return false;	
	}
	return true;
}

function checkLeapYear($currentYear) {
		var y = $currentYear;
		return (y%4==0 && y%100!=0) || y%400==0;
}

function setInfoIcons(){
	jQuery('em.info').mouseover(function(){
		jQuery('div.tooltip-container').remove();

		var tipText = jQuery(this).attr('title');
		if($(this).parent().parent().parent().parent().parent().hasClass('product-dashboard')){
			var sidetipText = jQuery(this).html();
			jQuery(this).parent().append('<div class=\'tooltip-side-container\'><div class=\'tooltip-side-top\'></div><div class=\'tooltip-side-content\'><p>' + sidetipText + '</p></div></div>');
		}
		else if(tipText == ''){
			return false;	
		}
		else{
			// Build HTML for tooltip
			jQuery(this).parent().append('<div class=\'tooltip-container\'><div class=\'tooltip-top\'></div><div class=\'tooltip-content\'><p>' + tipText + '</p></div></div>');
			//Find height of tooltip for top position above form element	
			$tooltipPos = jQuery('div.tooltip-container').height() - 2;
			
			// AR 06/10/2010 - set the right position for info elements differently if there is an active error
			var tooltipRight = '-15px';
			if(jQuery(this).parent().find('em.error').length){
				tooltipRight = '-43px';
			}
			//AS 08/08/2011 - Set the right position for the property search popups
			if($(this).parent().parent().hasClass('table-head')){
				tooltipRight = '-160px';
			}
			if($(this).parent().hasClass('legend-replace')){
				tooltipRight = '-197px';
			}
			if($(this).parent().hasClass('submitBtns')){
				tooltipRight = '410px';	
				$tooltipPos = jQuery('div.tooltip-container').height() - 26;
			}
			if($(this).parent().children('div').hasClass('medium')){
				tooltipRight = '105px';
			}
			if($(this).parent().parent().parent().hasClass('table-head')){
				tooltipRight = '-160px';
			}
			jQuery('div.tooltip-container').css('right', tooltipRight);
			jQuery('div.tooltip-container').css('top', '-' + $tooltipPos + 'px');
			
		}
		if($(this).hasClass('payment')){
			Paymenthtml = '<ul><li><img src="' + TEMPLATE_DIR + '/view/images/icon-diners.gif" alt="Diners"/><span>Diners</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-master.gif" alt="MasterCard"/><span>Master</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-visa.gif" alt="Visa"/><span>Visa</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-amex.gif" alt="American Express"/><span>Amex</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-maestro.gif" alt="Maestro"/><span>Maestro</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-jcb.gif" alt="JCB"/><span>JCB</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-laser.gif" alt="Laser"/><span>Laser</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-solo.gif" alt="Solo"/><span>Solo</span></li>' + 
			'<li><img src="' + TEMPLATE_DIR + '/view/images/icon-worldpay.gif" alt="WorldPay"/></li></ul>';
			$(this).parent().find('div.tooltip-container').addClass('payment').find('div.tooltip-content').append(Paymenthtml);
			if($('h3.account').length){
				$tooltipPaymentPos = jQuery('div.tooltip-container').height() - 25;
				jQuery('div.tooltip-container').css('right', '406px');
				jQuery('div.tooltip-container').css('top', '-' + $tooltipPaymentPos + 'px');
			}
			else{
				$tooltipPaymentPos = jQuery('div.tooltip-container').height() - 4;
				jQuery('div.tooltip-container').css('right', '-18px');
				jQuery('div.tooltip-container').css('top', '-' + $tooltipPaymentPos + 'px');
			}
		}
  }).mouseout(function(){
	jQuery('div.tooltip-container, div.tooltip-side-container').remove();
  });
	
}

function setPopup(element){
	$('div.check em.error').mouseover(function(){
		jQuery('div.tooltip-container').remove();
		var tipText = $(this).text();
		if(tipText == ''){
			return false;
		}
		else{
			$(this).parent().append('<div class=\'tooltip-container\'><div class=\'tooltip-top\'></div><div class=\'tooltip-content\'><p>' + tipText + '</p></div></div>');
			//Find height of tooltip for top position above form element	
			$tooltipPos = jQuery('div.tooltip-container').height();
			$tooltipPosR = $(this).position().left + 56;
			jQuery('div.tooltip-container').css('right', $tooltipPosR  + 'px');
			jQuery('div.tooltip-container').css('top', '-' + $tooltipPos + 'px');
			
			jQuery('div.tooltip-container').css('z-index', '100000');

			if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
				jQuery(element).parent().prepend('<iframe></iframe>');
				jQuery(element).parent().find('iframe').css('filter', 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)');
				jQuery(element).parent().find('iframe').css('opacity', '0');
				jQuery(element).parent().find('iframe').css('width', '230px');					
				jQuery(element).parent().find('iframe').css('height', '100px');	
				jQuery(element).parent().find('iframe').css('left', ($tooltipPosR+10)  + 'px');
				jQuery(element).parent().find('iframe').css('top', '-' + ($tooltipPos+50) + 'px');
				jQuery(element).parent().find('iframe').css('z-index', '1000');
			}
		}
	}).mouseout(function(){
		jQuery('div.tooltip-container').remove();
		if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
			jQuery('iframe').remove();
		}
	});
	if(jQuery(element).parent().find('em.error').length){
		jQuery(element).parent().find('em.error').mouseover(function(){
			// Remove any existing tooltips
	
			jQuery('div.tooltip-container').remove();
			//jQuery('iframe').remove();
			
			// Find text from label for tooltip
			if(jQuery(element).parent().find('em.error').length){
				//jQuery(element).parent().find('label.info').hide();
				var tipText = jQuery(element).parent().find('em.error').text();
				if(tipText == ''){
					return false;	
				}

				else{
					// Build HTML for tooltip
					if(jQuery(element).parent().hasClass('select-replacement')){
						jQuery(element).parent().parent().find('div.select-replacement').append('<div class=\'tooltip-container\'><div class=\'tooltip-top\'></div><div class=\'tooltip-content\'><p>' + tipText + '</p></div></div>');
					}
					else{
						jQuery(element).parent().append('<div class=\'tooltip-container\'><div class=\'tooltip-top\'></div><div class=\'tooltip-content\'><p>' + tipText + '</p></div></div>');
					}
					//Find height of tooltip for top position above form element	
					$tooltipPos = jQuery('div.tooltip-container').height();
					$tooltipPosR = jQuery(element).parent().find('em.error').position().left - 53;
					
					$parentClass = jQuery(element).parent().attr('class');
					if($parentClass == 'radio' || $parentClass == 'check'){
						$tooltipPos = $tooltipPos - 6;
						$tooltipPosR = $tooltipPosR - 10;
					}
					$isRadioGroup = jQuery(element).parent().parent().hasClass('radio-validation-group');
					if($isRadioGroup == true){
						jQuery('div.tooltip-container').css('left', $tooltipPosR  + 'px');
						jQuery('div.tooltip-container').css('bottom', '40px');
					}
					else{
						jQuery('div.tooltip-container').css('left', $tooltipPosR  + 'px');
						jQuery('div.tooltip-container').css('top', '-' + $tooltipPos + 'px');
					}
					
					jQuery('div.tooltip-container').css('z-index', '100000');

					if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
						jQuery(element).parent().prepend('<iframe></iframe>');
						jQuery(element).parent().find('iframe').css('filter', 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)');
						jQuery(element).parent().find('iframe').css('opacity', '0');
						jQuery(element).parent().find('iframe').css('width', '230px');					
						jQuery(element).parent().find('iframe').css('height', '100px');	
						jQuery(element).parent().find('iframe').css('left', ($tooltipPosR+10)  + 'px');
						jQuery(element).parent().find('iframe').css('top', '-' + ($tooltipPos+50) + 'px');
						jQuery(element).parent().find('iframe').css('z-index', '1000');
					}


				}
			}
		  }).mouseout(function(){
			jQuery('div.tooltip-container').remove();
			if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
				jQuery('iframe').remove();
			}
		  });

	}
}

function setContrast(){
	var hiContrastLinks = $('#toggle_high_contrast');
	hiContrastLinks.unbind("click");
	for(i=0; i<hiContrastLinks.length; i++){
		hiContrastLinks.bind('click',function(){
			hiContrastLinks.text('View site in normal contrast');                        
                        $('#toggle_high_contrast').attr("title", "Click here to view site in normal contrast");
                        enableHighContrast();
			return false;
		});	
	}
}

function enableHighContrast(){
	var HTMLhead = document.getElementsByTagName("head")[0];         
	var headerCSS = document.createElement('link');
	headerCSS.type = 'text/css';
	headerCSS.rel = 'stylesheet';
	headerCSS.className = 'hc-stylesheet';
	headerCSS.href = TEMPLATE_DIR + '/view/css/highcontrast.css';
	headerCSS.media = 'screen';
	HTMLhead.appendChild(headerCSS);

	sIFR.rollback();
	setCookie('TTCHC', 'hc-stylesheet', null);
	setContrastClose();	
	
}

function setContrastClose(){
	var hiContrastClose = $('#toggle_high_contrast');
	
	$('#toggle_high_contrast').unbind("click");
	$('#toggle_high_contrast').bind('click',function(){
		hiContrastClose.text('View site in high contrast');
         			$('#toggle_high_contrast').attr("title", "Click here to view site in high contrast");
		$('.hc-stylesheet').remove();
		
		setContrast();
		setCookie('TTCHC','',null);
		setSIFR();
		return false;
	});
	
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie= c_name + "=" +escape(value) + "; path=/" + ((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function deleteCookie( cookie_name ){
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name + "=; path=/; expires=" + cookie_date.toGMTString();
}
//start high contrast fix
function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}
function checkContrastCookie() {
    var contrast=getCookie("TTCHC");
    if (contrast!=null && contrast!="")
    {
	    return true;
    }
    return false;
}

function backtoTop(){
	$('ul li.back-to-top a').click(function(){
			// Calculate the top offset of the element
			var targetOffset = $('#container').offset();
			var targetTop = targetOffset.top;
			
			//Slide to the element by setting the body scroll top to anchor top
			$('html, body').animate({
				scrollTop:targetTop
			}, 'slow', 'easeOutExpo');
		return false;
	});	
}

/*  sIFR Unofficial Rollback addon for sIFR 3
    Modified by Paul Hassinger - hassinger.paul@ipaul.com - http://www.ipaul.com
    (modified from sIFR 2.0.1 Official Add-ons 1.2)

    Copyright 2005 Mark Wubben

    This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

if(typeof sIFR == "object"){
    sIFR.rollback = function(){
        function rollback(sSelector){
            if(sSelector == null){
                sSelector = "";
            } else {
                sSelector += ">";
            };

            sIFR.removeFlashClass();

            if(doRollback(sSelector+"embed") == false){
                doRollback(sSelector+"object");
            };
        };

        function doRollback(sSelector){
            var node, nodeParent, nodeAlternate, nodeAlternateChild, nodeAlternateNextChild, indexNodeToRemove;
            var listNodes = parseSelector(sSelector);
            var i = listNodes.length - 1;
            var bHasRun = false;

            while(i >= 0){
                node = listNodes[i];
                listNodes.length--;
                nodeParent = node.parentNode;

                if(node.className == 'sIFR-flash'){
                    /*  Flash blockers may add other nodes as siblings to the Flash element. 
                        Thus, we remove all children of nodeParent, and look for nodeAlternate at the same time */
                    indexNodeToRemove = 0;

                    while(indexNodeToRemove < nodeParent.childNodes.length){
                        node = nodeParent.childNodes[indexNodeToRemove];
                        if(node.className == "sIFR-alternate"){
                            nodeAlternate = node;
                            indexNodeToRemove++;
                            continue;
                        };
                        nodeParent.removeChild(node);
                    };

                    if(nodeAlternate != null){
                        nodeAlternateChild = nodeAlternate.firstChild;
                        while(nodeAlternateChild != null){
                            nodeAlternateNextChild = nodeAlternateChild.nextSibling;
                            nodeParent.appendChild(nodeAlternate.removeChild(nodeAlternateChild));
                            nodeAlternateChild = nodeAlternateNextChild;
                        };
                        nodeParent.removeChild(nodeAlternate);
                    };

                    nodeParent.className = nodeParent.className.replace(/\bsIFR\-replaced\b/, "");
                    bHasRun = true;
                };

                i--;
            };

            return bHasRun;
        };

        return rollback;
    }();
};

function buildFlashMap (divId, swfLoc, swfName, swfWidth, swfHeight, swfVar){
	var flashVersion = 8;

	var so = new SWFObject(swfLoc, swfName, swfWidth, swfHeight, flashVersion, "#000000");
	so.addParam("wmode", "transparent");
	so.addParam("scale", "noscale");
	so.addParam("allowFullScreen", "true");

	if (swfVar){
		so.addVariable("xmlPath", swfVar);
	}
	so.write(divId);
}
