///--------------------------------------------------
/// UKJobsNet - jQuery Version
///--------------------------------------------------

	jQuery(document).ready(function(){

		var ukjPageTitle = $("title").text();
		
		/// create clickable links for the jobs
		$('div#content3 span').each(function(i) {
			var ukjJobTitle = $(this).html();
			$(this).html('<a href="javascript:ukjJobClick(\''+$(this).attr('id')+'\');">'+ukjJobTitle+' <img src="/images/template3/externalLink.gif" alt="icon" title="Open link in a new window" style="vertical-align:top;margin-right:10px;" /></a>');
		});

		/// create clickable links for the CV submissions
/*		$('div#content3 span').each(function(i) {
			var ukjJobTitle = $(this).html();
			$(this).html('<a href="/rsm/14/job-click/'+$(this).attr('id')+'" target="_blank">'+ukjJobTitle+' <img src="/images/template3/externalLink.gif" alt="icon" title="Open link in a new window" style="vertical-align:top;margin-right:10px;" /></a>');
		});*/

		/// create an alert form
		if ($('div#emailAlertForm2').length > 0) {
			$('div#emailAlertForm2').html(
				"<form id=\"emailAlertForm\" action=\"#1\" method=\"post\" onSubmit=\"return ukjCreateEmailAlert();\">"+
				"<div id=\"emailAlertFormFields\">"+
				"<input type=\"text\" name=\"email\" id=\"alertEmailAddress\" class=\"emailAlertField\" value=\""+ukjAlertEmail+"\" /> "+
				"<input type=\"image\" src=\"/images/template3/alertButton.gif\" alt=\"submit button\" id=\"alertEmailButton\" /> "+
				"<img src=\"/images/template3/ajaxFormLoader.gif\" alt=\"loading\" id=\"alertEmailLoading\" />"+
				"</div>"+
				"<div id=\"emailAlertFormStatus\" style=\"display:none;\"></div>"+
				"</form>"
				);
		}

		/// add social sharing options
		if ($('#addThisSocial').length > 0) { $('body').append("<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=ukjobsnet\"></script>"); }
		
		/// setup the autocomplete search box
		//// $('#jobSearchFormField').autocomplete('/includes/autocomplete.php', { minChars:3, maxItemsToShow:10, autoFill:false, });
  		$('#jobSearchFormField').suggestion({ url:"/includes/autocomplete.php?chars=",minChars:2,width:410});

	});


	/// function to handle alert form submissions
	function ukjCreateEmailAlert() {
		var emailAddress = $('#alertEmailAddress').val();
		var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!emailRegex.test(emailAddress)) { $('div#emailAlertFormStatus').html('Ooops! Your email address is incorrect, please check you have entered it correctly.').fadeIn('fast'); }
		else {
			$('div#emailAlertFormStatus').fadeOut('fast',function() {
				$('#alertEmailButton').fadeOut('fast',function() { $('#alertEmailLoading').fadeIn('fast'); } );
				var params = "action=create&email="+escape(emailAddress)+"&cid="+escape(ukjAlertCat)+"&lid="+escape(ukjAlertLoc)+"&pt="+escape(ukjAlertPT)+"&gr="+escape(ukjAlertGD)+"&search="+escape(ukjAlertST)+"&location="+escape(ukjAlertLT)+"&chk="+escape(ukjAlertCK);
				jQuery.ajax({url:'/includes/emailAlert.php',type:'POST',data:params,dataType:'xml',
					success:function(xml){ 
						var status = $(xml).find('status').text();
						var message = $(xml).find('message').text();
						$('#alertEmailLoading').fadeOut('fast',function() {
							if (status == 'error') { $('#alertEmailButton').fadeIn('fast',function() { $('div#emailAlertFormStatus').html(message).fadeIn('fast'); }); }
							else { $('div#emailAlertFormFields').fadeOut('fast',function() { $('div#emailAlertFormStatus').html(message).fadeIn('fast'); }); }
						});
					}
				});	
			});
		}
		return false;
	}


	function ukjJobClick(j) {
		var newWin = null;
		var w = j.replace(/[^A-Z0-9]/g,'');
		newWin = window.open("http://"+document.domain+"/"+"rsm"+"/"+"14"+"/"+"job-click/"+j,w);
		if (newWin==null || typeof(newWin)=="undefined") { window.location = "http://"+document.domain+"/"+"rsm"+"/"+"14"+"/"+"job-click/"+j; }
	}

	function ukjJobRedirect() {
		var ukjJCForm = document.getElementById('ukjJCForm');
		ukjJCForm.action = "http://"+document.domain+"/"+"rsm"+"/"+"19"+"/"+"job-redirect";
		ukjJCForm.submit();
	}

	function ukjPostCVClick(c) {
		var newWin = null;
		var w = c.replace(/[^A-Z0-9]/g,'');
		newWin = window.open("http://"+document.domain+"/"+"rsm"+"/"+"21"+"/"+"post-cv-click/"+c,w);
		if (newWin==null || typeof(newWin)=="undefined") { window.location = "http://"+document.domain+"/"+"rsm"+"/"+"21"+"/"+"post-cv-click/"+c; }
	}

	function ukjPostCVRedirect() {
		var ukjPostCVForm = document.getElementById('ukjPostCVForm');
		ukjPostCVForm.action = "http://"+document.domain+"/"+"rsm"+"/"+"22"+"/"+"post-cv-redirect";
		ukjPostCVForm.submit();
	}

	function ukjBannerClick(c) {
		var newWin = null;
		var w = c.replace(/[^A-Z0-9]/g,'');
		newWin = window.open("http://"+document.domain+"/"+"rsm"+"/"+"44"+"/"+"banner-click/"+c,w);
		if (newWin==null || typeof(newWin)=="undefined") { window.location = "http://"+document.domain+"/"+"rsm"+"/"+"44"+"/"+"banner-click/"+c; }
	}

	function ukjBannerRedirect() {
		var ukjBannerForm = document.getElementById('ukjBannerForm');
		ukjBannerForm.action = "http://"+document.domain+"/"+"rsm"+"/"+"45"+"/"+"banner-redirect";
		ukjBannerForm.submit();
	}

	function ukjChangeAnswer(n) {
		var optionList = document.getElementById('contactQuestion');
		if (n < 1) { n = optionList.value; }
		for (x=1;x<7;x++) {
			var contactAnswersOption = document.getElementById('contactAnswer'+x);
			contactAnswersOption.style.display = (n == x)? 'block' : 'none' ;
		}
	}

	function funcCFC(checksum) {
		formChkDiv = document.getElementById('formChk');
		formChkDiv.innerHTML = '<input '+'type="hidden" '+'value="'+checksum+'" name="'+'cfc'+'" />';
	}
