//######################################################//
//######################################################//
var oTagWindow = null;
function openTagEditor(sParams) {
//	oTagWindow = window.open("/publisha.content/tags/tags.publisha?" + sParams, "TAGS", "alwaysRaised, dependent,resizable,width=400,height=500");
	oTagWindow = window.open("/publisha.content/tags/tags.publisha?" + sParams, "TAGS", "resizable=0,width=420,height=520");
}

var oPopupWindow = null;
function openPopup(sUrl) {
	oPopupWindow = window.open(sUrl, "PopupBox", "resizable=1,width=420,height=520");
}
function openHelp(sUrl) {
	openPopup("/publisha.content/help/popup.publisha?src=" + encodeURIComponent(sUrl));
}

function openMFactorHelp() {
	oPopupWindow = window.open("/publisha.content/help/mfactor.publisha", "PopupBox", "resizable=0,width=680,height=500");
}
function openThankYou() {
	oPopupWindow = window.open("/publisha.content/info/thankyoubank.publisha", "PopupBox", "resizable=0,width=473,height=259");
}
function openThankMe() {
	oPopupWindow = window.open("/publisha.content/info/thankmebank.publisha", "PopupBox", "resizable=0,width=473,height=259");
}

function trimStr(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

//######################################################//
// This function enforces the custom maxlength property
// onto textareas, it also creates a counter as feedback
// to the user
//######################################################//
var iTextAreaCount = 0;
function setTextAreaMaxLengths() {
	try {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Check all text areas in the page
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		var oTextAreaCollection = document.getElementsByTagName("textarea");
		for (var ii=0; ii<oTextAreaCollection.length; ii++) {
			var oTextArea = oTextAreaCollection[ii];
			if (oTextArea.getAttribute("maxlength")) {
				//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
				// The text area has the maxlength attribute set, now
				// trap events and add some UI feedback (a counter)
				//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
				iTextAreaCount++;
				var oCounterUI = document.createElement("div");
				oCounterUI.innerHTML = "<span id=\"tac" + iTextAreaCount + "\">0</span>/" + oTextArea.getAttribute("maxlength");
				oTextArea.parentNode.insertBefore(oCounterUI, oTextArea.nextSibling);
				oTextArea.counter = iTextAreaCount;
				oTextArea.onkeyup = oTextArea.onchange = oTextArea.onpaste = checkTextAreaMaxLength;
				oTextArea.onkeyup();
			}
		}
	}
	catch (e) {}
}

//######################################################//
// Called by setTextAreaMaxLengths()
//######################################################//
function checkTextAreaMaxLength() {
	var iLength = this.value.length;
	var iMaxLength = this.getAttribute("maxlength");
	if (iLength > iMaxLength) {
		this.value = this.value.substring(0, iMaxLength);
		iLength = this.value.length;
	}
	var oCounter = document.getElementById("tac" + this.counter);
	oCounter.firstChild.nodeValue = iLength;
}

//######################################################//
// These functions are used in the registration
// process
//######################################################//
function formatPhoneNumber(sNumber) {
	var	sFormattedNumber = sNumber.replace(/\s/g, "");
	if (sFormattedNumber.substring(0, 1) == "+") sFormattedNumber = sFormattedNumber.substring(1);
	if (sFormattedNumber.substring(0, 3) == "440") sFormattedNumber = sFormattedNumber.substring(3);
	if (sFormattedNumber.substring(0, 1) == "0") sFormattedNumber = sFormattedNumber.substring(1);
	if (sFormattedNumber.substring(0, 2) != "44") sFormattedNumber = "44" + sFormattedNumber;

	return sFormattedNumber;
}

function isValidUKMobileNumber(sNumber) {
	var	oValidator	= /^447[0-9]{9}$/;

	return oValidator.test(sNumber);
}




//######################################################//
// These functions are used by the searches to show/hide
// the full text
//######################################################//
function more(oAnchor) {
	var oDiv = oAnchor.parentNode;
	oDiv.style.display = "none";
	getNextSibling(oDiv).style.display = "block";
}
function less(oAnchor) {
	var oDiv = oAnchor.parentNode;
	oDiv.style.display = "none";
	getPreviousSibling(oDiv).style.display = "block";
}
function getNextSibling(oElement) {
	do oElement = oElement.nextSibling; 
	while (oElement && oElement.nodeType != 1); 
	return oElement; 
}
function getPreviousSibling(oElement) {
	do oElement = oElement.previousSibling; 
	while (oElement && oElement.nodeType != 1); 
	return oElement; 
}

//######################################################//
// Rate an item
// nb assumes that the receiving page expects 2 paramaters
// rid & rs
//######################################################//
function rateItem(rid, bUseful, sUrl) {
	sUrl = sUrl || window.location.href;
	sUrl = sUrl.replace(/&rid=[0-9]+/g, "");
	sUrl = sUrl.replace(/&rs=[0-1]/g, "");
	sUrl += "&rid=" + rid + "&rs=" + (bUseful ? "1" : "0");
	window.location = sUrl
}



//######################################################//
// Cross browser modal dialog box
//######################################################//
function openDialogDialog(sUrl, vWidth, vHeight) {
	if (window.showModalDialog) {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Comes as standard
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		window.showModalDialog(sUrl, window, "dialogWidth: " + vWidth + "px; dialogHeight: " + vHeight + "px; center: yes; help: no; status: no; resizable: no");
	}
	else if (window.addEventListener) {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Try and reproduce the IE modal dialog
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		var ModalWindow = window.open(sUrl, "ModalDialogBox", "modal=1,width=" + vWidth + ",height=" + vHeight + ",resizable=0");
		var SetModalFocus = function() {
			if(!ModalWindow.closed){
				ModalWindow.focus();
			}
			else {
				ModalWindow = null;
				window.removeEventListener(SetModalFocus, "focus");
				ModalFocus = null;
			}
		}
		window.addEventListener("focus", SetModalFocus, false ); 
	}
	else {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Oh dear, browser doesn't support anything so fancy,
		// plain old window.open
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		var StandardWindow = window.open(sUrl, "StandardDialogBox", "modal=1,width=" + vWidth + ",height=" + vHeight + ",resizable=0");
	}
}

//######################################################//
// Are they flash enabled?
//######################################################//
function isFlashEnabled() {
	var iFlashVersions = 10;
	if (window.ActiveXObject) {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Internet explorer
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		for (var ii=0; ii<=iFlashVersions; ii++) {
			try {
				var oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + ii + "');");
				if (oFlash) return true;
			}
			catch(e) {}
		}
	}
	else if (navigator.mimeTypes) {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// Non IE (modern browsers)
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		if(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
			return true;
		}
	}
	else if (navigator.plugins) {
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		// old fashioned
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
		if (navigator.plugins['Shockwave Flash'] || navigator.plugins['Shockwave Flash 2.0']) {
			return true;
		}
	}
	else {
//		alert("nothing!");
		return false;
	}
}
window.onload = isFlashEnabled;

//######################################################//
// Open up a confirmation popup
//######################################################//
var __ON_COMPLETE_FUNCTION = null;
function customConfirm(sKey, vOnCompleteFunction) {
	__ON_COMPLETE_FUNCTION = vOnCompleteFunction;
	openDialogDialog("/publisha.content/confirm/popup.publisha?key=" + encodeURIComponent(sKey), "420", "520");
}

//######################################################//
// Validate text boxes that contain a date (3 fields)
//######################################################//
var iMinYear = 1890;
function isValidDate(vDay, vMonth, vYear) {
	try {
		var	iDay = vDay;
		var	iMonth = vMonth - 1;
		var	iYear = vYear;
		var oDate = new Date(iYear, iMonth, iDay);
		var iCompareDay = oDate.getDate();
		var iCompareMonth = oDate.getMonth();
		var iCompareYear = oDate.getFullYear();

//		alert(iDay + "/" + iMonth + "/" + iYear + " :: " + iCompareDay + "/" + iCompareMonth + "/" + iCompareYear);

		return (iYear == iCompareYear) && (iMonth == iCompareMonth) && (iDay == iCompareDay);
	}
	catch (e) {
//		alert("error: " + e.message);
		return false;
	}
}

//######################################################//
// show hide div by setting style display
//######################################################//

function showhide(divid,linkid)
{
    var div = document.getElementById(divid);
    var link = document.getElementById(linkid) 
     
     if(div.style.display=='none'){  
        div.style.display='block';
        link.title="less options";
        link.innerHTML="fewer";
     }
     else
     {
        div.style.display='none'
        link.title="Show more options"
        link.innerHTML="more..";
     }
     
     return false;
 }   


$(function()
{    
    $('#searchbutton').click(function()
    {   
        //alert('search');
        var searchterm = $('#searchtext').val();
        
        if(searchterm == "")
        {
            alert('Please enter a search term before clicking the go button!');
        }
        else
        {
            document.forms["searchform"].submit();
        }        
    });
}); 