function showArticle(articleId, pageId, zoomLevel) {
	var url = '/ndp/del/article/' + articleId;
	if (pageId) {
		url += '/' + pageId;
	}
	if (zoomLevel && zoomLevel >= 0) {
		url += '?zoomLevel=' + zoomLevel;
	} else if (viewerBean) {
		url += '?zoomLevel=' + viewerBean.zoomLevel;
	}
	viewerBean = null;
//	window.location.href = url;
	document.location = url;
	return false;
} 

function printPage(pageId) {
	var url = "/ndp/imageservice/nla.news-page" + pageId + "/print";
	window.open(url);
}

function printArticle(articleId, format, openPrintPopup) {
	if (typeof openPrintPopup == 'undefined') {
		openPrintPopup = false;
	}
	var s = format.substr(0, 1).toUpperCase() + format.substr(1).toLowerCase();
	var url = "/ndp/del/printArticle" + s + "/" + articleId + "/" + viewerBean.zoomLevel + "?print=" + (openPrintPopup ? "y" : "n");
	window.open(url);
}

function showDialog(dEle) {
	 $("#" + dEle).show() ;
}

function closeDialog(dEle) {
	$("#" + dEle).hide() ;
}

function openUrl(url) {
	window.open(url, '_blank');
}

function gopage(pid, zoomLevel) {	// changed kkf16sep10 - preserve query if any
	var defaultZoomLevel = 1;
	if (zoomLevel == null || zoomLevel < 0)
		zoomLevel = (viewerBean != null) ? viewerBean.zoomLevel : defaultZoomLevel;
	var appendQuery ;
	var currentUrl = window.location.href ;	
	var i = currentUrl.indexOf('?') ;
	if (i > 0) {
		appendQuery = currentUrl.substring(i+1).replace(/zoomLevel\=./gi,'') ;
		if (appendQuery.length > 0) {
			i = appendQuery.indexOf('#') ;
			if (i > 0) appendQuery = '&' + appendQuery.substring(0, i) ;
			else if (i == 0) appendQuery = '' ;
			else appendQuery = '&' + appendQuery ;
		}
	}
	else appendQuery = '' ;
	window.location.href = '/ndp/del/page/' + pid + '?zoomLevel=' + zoomLevel + appendQuery ;
	return false;
}

function getCookieValue(cookieName) {
	var val = "";
	var ck = document.cookie;
	if (ck.length > 0) {
		var n1 = ck.indexOf(cookieName + "=");
		if (n1 >= 0) {
			n1 += cookieName.length + 1;
			var n2 = ck.indexOf(";", n1);
			if (n2 < 0) {
				n2 = ck.length;
			}
			val = ck.substring(n1, n2);
		}
	}
	return val;
}

/**
kkf 11nov10
function checkCaptcha(originalCaptchaText, captchaText, onSuccess) {
	if (captchaText == null || captchaText == '') {
		alert("Please enter the captcha.");
	} else if (originalCaptchaText == null || originalCaptchaText == '') {
		alert("Your session has expired.");
	} else if (captchaText != originalCaptchaText) {
		alert("Your captcha doesn't match the text. Please try again.");
	} else {
		// Ok, store in a cookie
		$.cookie('newspaperCaptcha', captchaText, {path: '/'});
**/

function checkCaptcha(captchaId, captchaText, onSuccess) {

	if (captchaText == null || captchaText == '') {
		alert("Please enter the captcha.");
		return ;
	}
	if (captchaId == null || captchaId == '') {
		alert("Your session has expired.");
		return ;
	}

	// check on server captchaCheck

	var resp = "" ;

	$.ajax({
		type: "POST",
		url: "/ndp/del/captchaCheck",
		async: false,
		data: "simulatedMethod=post&captchaText=" + captchaText +  "&i=" + captchaId,
		success: function(msg){
			resp = msg ;
		},
		error: function(reqObj, msg, exceptionObj){
			resp = "ERROR: " +  reqObj.statusText ;
		}
	});

	if (resp != "ok") {
		if (resp == "fail") {
			alert("Your captcha response doesn't match the text. Please try again.") ;
			return ;
		}
		alert("Unexpected error checking your captcha response:\n" + resp + 
			"\n \nPlease refresh this page and try again.") ;
		return ;
	}

	// Ok, store in a cookie
	$.cookie('newspaperCaptcha', captchaId, {path: '/'});

	// Hardcode!!!

	$('#tagId').attr('href', '/static/ndp/oxideDesign/addAnonTag.html');
	$('#annotationId').attr('href', '/static/ndp/oxideDesign/addAnonAnnotation.html');
	$('.popup').each(function(i) {
		var eId = $(this).attr('id');
		if (eId.indexOf('editAnnotation_') == 0) {
			var annoId = eId.substr(15);
			$(this).attr('href', '/ndp/del/editAnnotation/' + annoId);
		}
	});
	// Remove popup
	$('#popup, #mask').remove();
	$('body').removeClass('popup');
		
	if (onSuccess) {
		onSuccess();
	}
}

function closepopup() {
	$('#popup, #mask').remove();
	$('body').removeClass('popup'); 
}

function addViewEntirePage(pageId) {
	// Add button
	$('#header').append('<p class="buttons" id="return"><a href="javascript:gopage(' + pageId + ', 1);">View entire page</a></p>');
/*
	$('#viewer').append('<p class="buttons" id="return"><a href="javascript:gopage(' + pageId + ', 1);">View entire page</a></p>');
	// Try to center the button
	var left = parseInt($('#viewer').css('width'));
	if (isNaN(left) || left == 0) {
		left = 600;
	}
	$('#return').css('left', '' + ((left - 100) / 2) + 'px');
*/
}

function showLimits() {
	// Check if search limits div is open based on class
	if ($('.srchlimit-link').hasClass('hidelink')) {
		$('.srchlimit-link').text('Show search limits'); // change the link heading
		$('.srchlimit-link').removeClass('hidelink'); // show [+] icon
		$('#srchlimit-div').addClass('hide'); // hide the limits div
		$('#search #srchlimit-div').addClass('hide'); // hide the limits div (homepage)
	}
	else {
		$('.srchlimit-link').addClass('hidelink'); // show [-] icon
		$('.srchlimit-link').text('Hide search limits'); // change the link heading
		$('#srchlimit-div').removeClass('hide'); // show the limits div
		$('#search #srchlimit-div').removeClass('hide'); // show the limits div (homepage)
	}
}

function removeLimit(e, formInputMarker) {

	var limitDisplay = $(e).parent().parent().parent() ;
	limitDisplay.hide('slow') ;
	var searchForm = limitDisplay.parent() ;
	var correspondingInputEle = searchForm.children(".limit-" + formInputMarker) ;
	correspondingInputEle.remove() ;
//	alert("limitcount="+searchForm.children(".removeableHiddenLimit").length) ;
	if (searchForm.children(".removeableHiddenLimit").length == 0) clearAllLimits() ;
	return false ;
}

function clearAllLimits() {

	$(".removeableHiddenLimit").remove() ;
	$('#srchlimit-div').addClass('hide'); // hide the limits div
	$('.srchlimit-link').hide("slow") ;
	$('#primarysearchbox').removeClass("searchbox-limit") ; 
	return false ;
}
	
