function doIPLogin() {
	theForm=document.IPLoginForm;
	if(theForm.Email.value=="") {
		alert("Please enter your email address");
		return false;
	}
	if(theForm.Password.value=="") {
		alert("Please enter your password");
		return false;
	}
	theForm.submit();
}

function doLogin() {
	if($("#logem").val() != "") {
		document.loginForm.submit();
	} else {
		alert("Please enter your email address & password to login");
		return false;
	}
}

function dofbLogin() {
	if($("#logem").val() != "") {
		document.loginFormfb.submit();
	} else {
		alert("Please enter your email address & password to login");
		return false;
	}
}

function confirmDeleteComment(which) {
	if(confirm("Are you sure you want to delete your comment?")) {
		$("#cmnt"+which).load("/ajax-comment_status.cfm?ArticleCommentID="+which+"&a=0");
	}
}

function loadWhatsNewBox(which) {
	if(which=="new") {
		$("#wn1").addClass("selected");
		$("#wn2").removeClass("selected");
		$("#home-most-read").load("/ajax-load_shop_teaser.cfm?type=new")
	} else {
		$("#wn1").removeClass("selected");
		$("#wn2").addClass("selected");
		$("#home-most-read").load("/ajax-load_shop_teaser.cfm?type=best")
	}
}

function loadFaceBoxTabs(which) {
	var randomnumber=Math.floor(Math.random()*11000000);
	if(which=="1") {
		$("#fb1").addClass("selected");
		$("#fb2").removeClass("selected");
		$('#facebox-tabs-content').load('/ajax-login_facebox_inner.cfm?action=login&rand='+randomnumber);
	} else {
		$("#fb1").removeClass("selected");
		$("#fb2").addClass("selected");
		$('#facebox-tabs-content').load('/ajax-login_facebox_inner.cfm?action=trial&rand='+randomnumber);
	}
}

function loadFaceBoxSearchTabs(which) {
	if(which=="1") {
		$("#fb1").addClass("selected");
		$("#fb2").removeClass("selected");
		$('#facebox-tabs-content').load('/ajax-advanced_search.cfm?action=advanced');
	} else {
		$("#fb1").removeClass("selected");
		$("#fb2").addClass("selected");
		$('#facebox-tabs-content').load('/ajax-advanced_search.cfm?action=basic');
	}
}

function loadFaceBoxUserTabs(which) {
	var randomnumber=Math.floor(Math.random()*11000000);
	if(which=="1") {
		$("#fb1").addClass("selected");
		$("#fb2").removeClass("selected");
		$("#fb3").removeClass("selected");
		$('#facebox-tabs-content').load('/ajax-edit_preferences.cfm?type=details&rand='+randomnumber);
	} else if(which=="2") {
		$("#fb1").removeClass("selected");
		$("#fb2").addClass("selected");
		$("#fb3").removeClass("selected");
		$('#facebox-tabs-content').load('/ajax-edit_preferences.cfm?type=email&rand='+randomnumber);
	} else if(which=="3") {
		$("#fb1").removeClass("selected");
		$("#fb2").removeClass("selected");
		$("#fb3").addClass("selected");
		$('#facebox-tabs-content').load('/ajax-edit_company_users.cfm?rand='+randomnumber);
	}
}

function doEmailToFriend() {
	theForm=document.emailToFriend;
	val="";
	var flag = true;
	if(theForm.FirstName.value == "") {
		flag = false;
		val+="* please enter their first name.\n";
	}
	if(theForm.LastName.value == "") {
		flag = false;
		val+="* please enter their last name.\n";
	}
	if(theForm.Email.value == "") {
		flag = false;
		val+="* please enter their email address.\n";
	}
	else if(ValidateEmailAddress(theForm.Email.value) == false) {
		flag = false;
	}
	if(flag==false) {
		if(val!="") {
			alert("Sorry, please complete the following details:\n\n"+val);
		}
	} else {
		theForm.submit();
	}
}

function refineSearch() {
	pid="";
	if(document.getElementById("pid0").checked==true) { pid = pid + "0," }
	if(document.getElementById("pid1").checked==true) { pid = pid + "1," }
	if(document.getElementById("pid2").checked==true) { pid = pid + "2," }
	if(document.getElementById("pid6").checked==true) { pid = pid + "6," }
	if(document.getElementById("pid7").checked==true) { pid = pid + "7," }
	if(pid=="") {
		alert("You must select a publication or report");
	}
	document.location.href="/search/?q="+$("#SearchInput").val()+"&pid="+pid+"&df="+$("#df").val()+"&dt="+$("#dt").val();
}

function searchJobs() {
	if($("#jslid").val() != "") {
		document.location.href="https://www.jobs-in-transport.com"+$("#jscid").val()+"?location="+$("#jslid").val();
	} else {
		document.location.href="https://www.jobs-in-transport.com"+$("#jscid").val();
	}
}

function doQuickRegister() {
	theForm=document.quickRegisterForm;
	val="";
	var flag = true;
	if(theForm.FirstName.value == "") {
		flag = false;
		val+="* enter your first name.\n";
	}
	if(theForm.LastName.value == "") {
		flag = false;
		val+="* enter your last name.\n";
	}
	if(theForm.Email.value == "") {
		flag = false;
		val+="* enter your email address.\n";
	}
	else if(ValidateEmailAddress(theForm.Email.value) == false) {
		flag = false;
	}
	if(theForm.Password.value == "") {
		flag = false;
		val+="* enter a password.\n";
	}
	if(theForm.PasswordConfirm.value != theForm.Password.value) {
		flag = false;
		val+="* your passwords don't match!.\n";
	}
	if(document.getElementById("cbag1").checked==false) {
		flag = false;
		val+="* you must agree to our terms & conditions";
	}
	if(flag==false) {
		alert("Sorry, please complete the following details:\n\n"+val);
	} else {
		theForm.submit();
	}
}

function loadRelatedArticles(type,which) {
	if(type==1) {
		$("#ra1").addClass("selected");
		$("#ra2").removeClass("selected");
	} else {
		$("#ra1").removeClass("selected");
		$("#ra2").addClass("selected");
	}
	$.get("/ajax-related_articles.cfm?type="+type+"&id="+which, function(data){
		$("#relatedarticles-content").html(data);
	});
}

function forgotPassword() {
	$.post("/ajax-forgot_password.cfm", {Email: $("#FPEmail").val()}, function(data){
		if(data==1) {
			$("#replace-content").load("/ajax-forgot_password.cfm?Thanks");
		} else if(data==2) {
			$("#replace-content").load("/ajax-forgot_password.cfm?ThanksRK");
		} else if(data==3) {
			$("#replace-content").load("/ajax-edit_preferences.cfm?readerkey");
		} else {
			$("#replace-content").load("/ajax-forgot_password.cfm?Error");
		}
	});
}

function loadReportResults(which,id,what) {
	$("#authors li").removeClass("selected");
	$.get("/ajax-reports_load_results.cfm?id="+id+"&author="+which, function(data){
		$("#report-results").html(data);
		$("#ra"+what).addClass("selected");
	});
}

function submitSearch() {
	var tmp=document.getElementById('SearchInput').value;
	document.location.href="/search/?q="+escape(tmp);
}

function sendComment() {
	$.post("/ajax-leave_comment.cfm", {ArticleID: $("#ArticleID").val(), Comment: $("#Comment").val()}, function(data){
		$("#facebox-content").html(data);
	});
}

function loadHomeSummary(which) {
	$("#hs"+which).addClass("selected");
	for(i=0;i<7;i++) {
		if(i!=which) {
			$("#hs"+i).removeClass("selected");
		}
	}
	$.get("/ajax-home_summary_box.cfm?id="+which, function(data){
		$("#home-summary-content").html(data);
		$("a[rel*=facebox]").unbind("click");
		$("a[rel*=facebox]").facebox();
	});
}

// news ajax selector
function toggleSection(which) {
	$("#rc"+which).toggle("fast");
}

// select all section category ids
function selectAll(which) {
	for(i=0;i<document.getElementById("cs"+which).value;i++) {
		if(document.getElementById("rc"+which+"s"+parseInt(i+1)).checked==true) {
			document.getElementById("rc"+which+"s"+parseInt(i+1)).checked=false;
		} else {
			document.getElementById("rc"+which+"s"+parseInt(i+1)).checked=true;
		}
	}
}

// reload news articles
function updateNews(query) {
	var theIDs="";
	for(i=0;i<document.refineForm.SectionIDs.length;i++) {
		if(document.refineForm.SectionIDs[i].checked==true) {
			theIDs=theIDs+document.refineForm.SectionIDs[i].value+",";
		}
	}
	if(theIDs!="") {
		$("#nextprev").hide("fast");
		$("#loading").show("fast");
		$.get("/_news.cfm?q="+query+"&ids="+theIDs, function(data) {
			document.getElementById("news").innerHTML=data;
			$("#loading").hide("fast");
		});
	}
}

function loadMostReadBox(type) {
	if(type=="read") {
		$("#mr1").addClass("selected");
		$("#mr2").removeClass("selected");
	} else {
		$("#mr1").removeClass("selected");
		$("#mr2").addClass("selected");
	}
	$.get("/ajax-most_read.cfm?type="+type, function(data){
		$("#home-most-read").html(data);
		$("a[rel*=facebox]").unbind("click");
		$("a[rel*=facebox]").facebox();
	});
}

function loadLoginBox(type) {
	if(type=="login" || type=="status") {
		$("#lb1").addClass("selected");
		$("#lb2").removeClass("selected");
	} else {
		$("#lb1").removeClass("selected");
		$("#lb2").addClass("selected");
	}
	$.get("/ajax-login_box.cfm?type="+type, function(data){
		$("#login-box-content").html(data);
		if(type=="login" || type=="trial" || type=="status") {
			$("#theURL").val(window.location.href);
		}
		$("a[rel*=facebox]").unbind("click");
		$("a[rel*=facebox]").facebox();
	});
}

function loadLatestJobsBox(type) {
	if(type=="jobs") {
		$("#ljb1").addClass("selected");
		$("#ljb2").removeClass("selected");
	} else {
		$("#ljb1").removeClass("selected");
		$("#ljb2").addClass("selected");
	}
	$.get("/ajax-latest_jobs.cfm?type="+type, function(data){
		document.getElementById("latest-jobs-content").innerHTML=data;
		$("a[rel*=facebox]").unbind("click");
		$("a[rel*=facebox]").facebox();
	});
}

function viewSpecifiedImage(which) {
	var x = window.open('','pictureViewer','width=800,height=800,resizable=1');
	if( !x ) { return true; }
	x.document.open();
	x.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>Picture Viewer<\/title>');
	x.document.write('<sc'+'ript type="text/javascript">');
	x.document.writeln('var isNN,isIE;');
	x.document.writeln('function reSizeToImage(){');
	if (navigator.appName.indexOf("Microsoft")==-1)
	{
		x.document.writeln('window.innerWidth=document.images["ex_popimage"].width;');
		x.document.writeln('window.innerHeight=document.images["ex_popimage"].height;');
		x.document.writeln('document.title="Picture Viewer";');
	}else if(window.opera){
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=130-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}else{
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}
	x.document.writeln('}<\/sc'+'ript>');
	x.document.write('<\/head><body style="margin:0px;" id="ex_popbody" onload="reSizeToImage();self.focus();">');
	x.document.write('<img id="ex_popimage" name="ex_popimage" src="'+which+'" onclick="window.close();" alt="" \/><\/body><\/html>');
	x.document.close();
	if( x.focus ) { x.focus(); }
}

// Window opening
function winOpenCenter(url,name,statbar,scroll,locate,resize,xWidth,yWidth)	{
var adjustedleft = 8//optional
var adjustedheight = 30//adjust height because of windows taskbar
var screenwidthremainder = screen.availWidth%2//really not needed, but won't hurt
var screenheightremainder = screen.availHeight%2
var screenwidth = screen.availWidth - screenwidthremainder
var screenheight = screen.availHeight - screenheightremainder
var winheight = yWidth //set new window height properties
var winwidth = xWidth //set new window width properties
var winleft = parseInt(screenwidth/2) - (winwidth/2) - adjustedleft//optional
var wintop = parseInt(screenheight/2) - (winheight/2) - adjustedheight

var win = window.open(url,name,'width=' +winwidth+ ',height=' +winheight+',status=' +statbar+',resizable='+resize+',scrollbars='+scroll+',location='+locate+',top='+wintop+',left='+winleft);
return win;
}

// Email checker
function ValidateEmailAddress(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		var textObj =document.getElementById("Email"); 
		alert("Please enter a valid email address.")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		var textObj =document.getElementById("Email"); 
	    alert("Email address username is not valid.")
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
				var textObj =document.getElementById("Email"); 
		        alert("Email address destination IP is invalid.")
			return false
		    }
	    }
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		var textObj =document.getElementById("Email"); 
		alert("Email address domain name doesn't seem to be valid.")
	    return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
		var textObj =document.getElementById("Email"); 
		alert("Email address must end in a three or four-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		var textObj=document.getElementById("Email"); 
		var errStr="Email address is missing a hostname."
		alert(errStr)
		return false
	}
	return true;
}

// Image stuff
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
