//Global Functions & settings version=3.3

/*******************
* global variables *
********************/

currentLink = 0;		//current highlighted link
currentPage = 0;		//current page



/************
* constants *
*************/

//date constants
var MINUTE = 60 * 1000
var HOUR = MINUTE * 60
var DAY = HOUR * 24
var WEEK = DAY * 7


/****************************
* general browser functions *
*****************************/

function checkFrame(page) {
	if (window.top == self) {
		this.location="../main.asp?page=" + page;
	} else {
		return true;
	}
}

function newWindow(url,inName,w,h) {
	if (inName == '') {
		winName = 'info';
	} else {
		winName = inName;
	}
	window.open(url,winName,'width=' + w + ',height=' + h + ',menubar=no,directories=no,toolbar=no,location=no,status=0,resizable=yes,scrollbars=yes');
}

function newWindowF(url,inName,w,h) {
	if (inName == '') {
		winName = 'info';
	} else {
		winName = inName;
	}
	window.open(url,winName,'width=' + w + ',height=' + h + ',menubar=no,directories=no,toolbar=no,location=no,status=0,resizable=no,scrollbars=no');
}

//as newWindow but without scroller
function newWin(url,inName,w,h) {
	if (inName == '') {
		winName = 'info';
	} else {
		winName = inName;
	}
	window.open(url,winName,'width=' + w + ',height=' + h + ',menubar=no,directories=no,toolbar=no,location=no,status=0,resizable=no,scrollbars=no');
}

function focusWindow() {
	this.window.focus();
}

function closeWindow() {
	this.window.close();
}

function on(image) {
	if(currentLink != 0) document['link' + currentLink].src = eval('img' + currentLink + "_off.src");
	document['link' + image].src = eval('img' + image + '_on.src');
	//custom bits of rollover script
	if (image==8){
		document['sec'].src = '../gfx/sec_client.gif';
	} else if (image==9) {
		document['sec'].src = '../gfx/sec_cand.gif';
	} else if (image==10) {
		document['sec'].src = '../gfx/sec_search.gif';
	}
}

function off(image, strap) {
	if(currentLink != 0) document['link' + currentLink ].src = eval('img' + currentLink + "_on.src");
	if(currentLink == 0 || currentLink != image) document['link' + image].src = eval('img' + image + "_off.src");
	if(typeof(strap)!='undefined') {
		document['sec'].src = strap;
	} else {
		document['sec'].src = '../gfx/pixel.gif';
	}
}

function linkOn(linkNum) {
	off(currentPage);
	on(linkNum);
}

function linkOff(linkNum) {
	off(linkNum);
	on(currentPage);
}

function backPage() {
	this.window.history.back();
}

function printPage(){
	this.focus();
	if (window.print) {
	    window.print();  
	}
}

function bookmark(url, name) {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		window.external.AddFavorite(url, name);
	} else {
		alert('Sorry, this feature is only available on Microsoft Internet Explorer');
	}
}

function setStatus(statusText) {
	window.setTimeout('window.status = "' + statusText + '";', 1);
}

/*******************
* string & numeric *
********************/

//return a string comprising a repeated substring 
function makeString(strLen,strText) {
	var outText = '';
	for(n=1; n<=strLen; n++) outText += strText;
	return outText;
}

//return number or string with specified leading 0's
function pad(inNum,padLen) {
	outText = inNum.toString();
	padLen -= outText.length;
	if(padLen > 0) outText = makeString(padLen,'0') + outText;
	return outText;
}

//validate email address
function validateEmail(email) {
	if (email != '') {
		return email.search(/.+@.+\..+/);
	} else {
		return -1;
	}
}


//check if number is numeric
function isInt(num) {
	if (num == '' || num.search(/\D+/) >= 0) {
		return false;
	} else {
		return true;
	}
}

function isFloat(num) {
	num = replace(num, '.', '');
	
	return isInt(num);
}

function replace(string, text, by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

//create random number
function getRand(maxNum) {
	return Math.floor(Math.random() * maxNum) + 1;
}

/*****************
* form functions *
******************/

//select all rows in select tag (formname = 'details')
function selectFields(selectStatus, fieldName, selectField) {
	fieldObj = eval('document.details.' + fieldName);
	
	if (selectField) {
		firstField = 1;
		if (selectStatus) {
			selStatus = false;
		} else {
			selStatus = true;
		}
		fieldObj[0].selected = selStatus;
	} else {
		firstField = 0;
	}
	
	for (i = firstField; i < fieldObj.length; i++) {
		fieldObj[i].selected = selectStatus;
	}
}

function getSelectedText() {
	if (document.getSelection) {
		str = document.getSelection();
	} else if (document.selection && document.selection.createRange) {
		str = document.selection.createRange().text;
	} else {
		str = '';
	}

	return str;
}


/*****************
* date functions *
******************/

//builds date string from date part fields
function buildDate(fieldName,formName) {
	dateDay = eval('document.' + formName + '.p_d_' + fieldName + '.value');
	dateMonth = eval('document.' + formName + '.p_m_' + fieldName + '.value');
	dateYear = eval('document.' + formName + '.p_y_' + fieldName + '.value');

	if(typeof eval('document.' + formName + '.p_h_' + fieldName) == 'object') {
		dateHour = pad(eval('document.' + formName + '.p_h_' + fieldName + '.value'),2);
		dateMin = pad(eval('document.' + formName + '.p_mm_' + fieldName + '.value'),2);
		timeStr = ' ' + dateHour + ':' + dateMin;
	} else {
		timeStr = '';
	}
	
	formField = eval('document.' + formName + '.' + fieldName);
	formField.value = pad(dateDay,2) + '/' + pad(dateMonth,2) + '/' + pad(dateYear,2) + timeStr;
	if(formField.value == '00/00/00') formField.value = '';
}

//put day, month & year in respective form fields
function setDate(fieldName,formName,dayVal,monthVal,yearVal) {
	eval('document.' + formName + '.p_d_' + fieldName + '.value = ' + pad(dayVal,2));
	eval('document.' + formName + '.p_m_' + fieldName + '.value = ' + pad(monthVal,2));
	eval('document.' + formName + '.p_y_' + fieldName + '.value = ' + pad(yearVal,2));

	buildDate(fieldName,formName);
}

//convert dd/mm/yy string & return millisecs
function getUKDate(inDate) {
	dayVal		= inDate.substr(0,2);
	monthVal	= inDate.substr(3,2);
	yearVal		= inDate.substr(6,2);

	var newDate = new Date();

	newDate.setDate(dayVal);
	newDate.setMonth(monthVal - 1);
	newDate.setFullYear(yearVal);

	return newDate.valueOf();

}


/**************
* right click *
***************/
function clickIE() {
	if (document.all) {
		return false;
	}
}

function clickNS(e) {
	if (document.layers||(document.getElementById&&!document.all)) {
		if (e.which==2||e.which==3) {
			return false;
		}
	}
}


/********************************
* Functions for formatting text *
*********************************/

//textarea tag MUST HAVE: onkeyup="storeCaret(this);" onclick="storeCaret(this);"

function storeCaret (textEl) {
	if (textEl.createTextRange) {
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

function insertAtCaret (textEl, text, currentSelection) {
	if (textEl.createTextRange && textEl.caretPos) {
		if(currentSelection != '')  document.selection.createRange().text = '';	//del existing selection
		
		var caretPos = textEl.caretPos;
		caretPos.text =
			caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
			text + ' ' : text;
	} else {
		textEl.value  = textEl.value + text; // for non MSIE browsers just append it
	}

	return true;
}

function encloseTags(tag, fieldName) {
	formField = eval('document.details.' + fieldName)
	currentSelection = getSelectedText();
	
	if(currentSelection != '') {
		newText = '<' + tag + '>' + currentSelection + '</' + tag + '>';
		newText = newText.replace(' </' + tag + '>', '</' + tag + '>');
		newText = newText + ' ';
	} else {
		var newText = prompt("Enter the text you wish to format.", currentSelection);
		if(newText != '') newText = '<' + tag + '>' + newText + '</' + tag + '> ';
	}
	
	insertAtCaret(formField, newText, currentSelection);
	formField.focus();
}

function insertTag(tag, promptText, fieldName) {
	formField = eval('document.details.' + fieldName)
	
	if(promptText != '') {
		thisItem = prompt(promptText, '');
		if (thisItem == null){return;}
	} else {
		thisItem = '';
	}

	insertAtCaret(formField, tag + thisItem, '');
	formField.focus();
}

function createLink(fieldName) {
	formField = eval('document.details.' + fieldName)
	currentSelection = getSelectedText();
	linkText = currentSelection;
	
	if(linkText == '') var linkText = prompt("Enter the text you wish to make a link.", '');
	
	if(linkText != '') {
		var linkUrl = prompt('Enter the URL.', 'http://www.');
		
		if(linkUrl != '' && linkUrl != 'http://www.') {
			linkTarget = '';
			if(confirm("Click OK to open this link in a new window or Cancel to open in the main site.")) linkTarget = ' target="_blank"';

			newText = '<a href="' + linkUrl + '"' + linkTarget + '>' + linkText + '</a> ';
			insertAtCaret(formField, newText, currentSelection);
			formField.focus();
		}
	}
}
