
var site_url = "/"

function rollover_image(img_name,img_src) {
if (document.images)
  {
  if (img_src != "none") {document.images[img_name].src = img_src;}
  }
}


function clear_box(initvalue,field) {
	box = document.getElementById(field);

	if (box.value==initvalue) {
	box.value = ''
	}
}

function check_box(initvalue,box) {
	if (box.value.length==0) {
	box.value = initvalue	
	}

}

function check_login() {
	if (document.login_form.remember.checked) {
		input_box = confirm("You have chosen to remember your password on this computer\n\nThis will keep you logged in to the portal on this computer with this user account\n\nIf you are using a shared account, it is advised you do not use this feature.\n\nClick OK to continue");
		if (input_box==true) {
							 return true;
							 }
		else {
			return false;
			 }	
		}
	}

function Load_Help(help_file) {

	var height;

	if (document.all)
		height = screen.height - document.body.offsetHeight
	else if (document.layers)
		height = screen.height - window.innerHeight

	var windowY = (height+80);

	window.open(site_url + 'Help/Default.asp?help_file=' + help_file,'Help','width=587,height=450,left=173,top='+windowY);
	return true;
}

function Main_Win(url,name,scrollbars) {
	mainwin = window.open(url,name,'width=500,height=400,scrollbars='+scrollbars+',resizable=yes');
	mainwin.focus();
	return true;
}

function load_href(url,target,name) {
	if (target=='_self') {
		window.location.href = url
		}
		else {
		window.open(url,name);
		}
	return true;
}

function NN_Resize(Reload) {
  if (Reload==true)
   		{
  		document.pageWidth=innerWidth;
		document.pageHeight=innerHeight;
		onresize=NN_Resize;
		}
  else if (innerWidth!=document.pageWidth || innerHeight!=document.pageHeight) {
  		location.reload();
		}
	}
	
function confirm_delete(confirm_str) {
	input_box = confirm(confirm_str);
	if (input_box==true) {
						 return true;
						 }
	else {
		return false;
		 }	
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function textarea_counter(textarea_id, maxlimit) {

	if (!document.getElementById) return false;

	document.getElementById(textarea_id + '_counter').value = maxlimit - document.getElementById(textarea_id).value.length;
	
	if (maxlimit - document.getElementById(textarea_id).value.length < 0) {
		document.getElementById(textarea_id + '_counter').style.color = '#CC0000';
		document.getElementById(textarea_id).style.color = '#CC0000';
		} else {
		document.getElementById(textarea_id + '_counter').style.color = '';
		document.getElementById(textarea_id).style.color = '';			
		}
}


function textarea_counter_alert(textarea_id, maxlimit) {

	if (!document.getElementById) return false;
	
	document.getElementById(textarea_id + '_counter').value = maxlimit - document.getElementById(textarea_id).value.length;
	
	if (maxlimit - document.getElementById(textarea_id).value.length < 0) {
		alert("You have exceded the allowed " + maxlimit + " characters\n\nPlease trim back the text before submitting.");
	}
	
}

function Show_Exec_Card(grouping,ref) {
	var height;

	if (document.all)
		height = screen.height - document.body.offsetHeight
	else if (document.layers)
		height = screen.height - window.innerHeight

	var windowY = (height+80);

	window.open(site_url + 'Components/Organisation/Exec_Contact_Card.asp?grouping=' + grouping + '&ref=' + ref,'ID_Card','width=300,height=250,left=463,top='+windowY+',resizable=yes');
	return true;
}

// For person searches
function Resolve_Window(form_name,field_name,field_value) {
	newwin = window.open(site_url + 'Scripts/Get_Person.asp?form_name=' + form_name + '&field_name=' + field_name + '&field_value=' + field_value,'Resolve','width=500,height=300,scrollbars=yes,resizable=yes');
return false;
}


function get_person(field_name) {
	
	field_value = document.getElementById(field_name).value
	window.open(site_url + 'Scripts/Get_Person.asp?field_name=' + field_name + '&field_value=' + field_value,'getperson','width=500,height=300,scrollbars=yes,resizable=yes');
return false;
}




// Image Upload Window
function image_upload(field_name,image_folder,image_file,thumb_config) {
	imgwin = window.open(site_url + 'scripts/image_upload/default.asp?field_name=' + field_name + '&image_folder=' + image_folder + '&image_file=' + image_file + '&thumb_config=' + thumb_config,'imgwindow','width=600,height=500,scrollbars=yes,resizable=yes');
	}
function image_preview(field_name,folder_url) {
	
	var imgurl = folder_url + "/" + document.getElementById(field_name).value;

	alert(imgurl);

	img_preview = window.open(imgurl,'img_preview');
	img_preview.focus();
	}	


// Asset upload scripts
function asset_upload(config,folder,file) {
	imgwin = window.open(site_url + 'scripts/wysiwyg/popups/insert_asset.asp?config=' + config + '&folder=' + folder + '&file_selected=' + file,'imgwindow','width=500,height=500,scrollbars=auto,resizable=yes');
	}

// For Colour Picker
function colour_picker(field_id) {
	if (!document.getElementById(field_id + "_table")) {
		colour_load(field_id);
	}
	else {
		colour_open(field_id);
	}
}

function colour_load(field_id)
{	if (!document.getElementById) return false;
	
	var string = new Array();
	var hexstring = "0369CF"
	string.push("<table cellspacing=\"0\" id=\"" + field_id + "_table\" class=\"colour_picker\" style=\"display: none;\">");
	for (var i=0;i<6;i++)
	{
		string.push('<tr>');
		for (var j=0;j<6;j++)
		{
			if (j==3) {string.push('</tr><tr>')}
			for (var k=0;k<6;k++)
			{		
			colour = "#"+hexstring.substr(k,1)+ hexstring.substr(k,1)+hexstring.substr(i,1)+ hexstring.substr(i,1)+hexstring.substr(j,1)+ hexstring.substr(j,1);

			cell = "<td style=\"background-color:" + colour +";\" onmouseover=\"colour_over('" + field_id + "','" + colour + "'); window.status='" + colour + "'; return true;\" onmouseout=\"colour_out('" + field_id + "'); window.status=''; return true;\" onclick=\"colour_freeze('" + field_id + "','" + colour + "');return true;\">&nbsp;</td>"

			string.push(cell);
			}
		}
		string.push('</tr>');
	}
	string.push('</table>');
	var writestring = string.join('');
	
	document.getElementById(field_id + "_place").innerHTML = writestring;
	
	colour_open(field_id);
}
function colour_open(field_id) {	
if (!document.getElementById) return false;
	
	if (document.getElementById(field_id + '_table').style.display == "block") {
		document.getElementById(field_id + '_table').style.display = "none";
	} else {
		document.getElementById(field_id + '_table').style.display = "block";
	}
}
function colour_over(field_id,colour) {	
if (!document.getElementById) return false;
	document.getElementById(field_id + '_preview').style.background = colour;
	document.getElementById(field_id).value = colour;
}
function colour_out(field_id) {	
if (!document.getElementById) return false;
	
	document.getElementById(field_id + '_preview').style.background = document.getElementById(field_id + '_holder').value;
	document.getElementById(field_id).value = document.getElementById(field_id + '_holder').value;
}
function colour_freeze(field_id,colour) {	if (!document.getElementById) return false;
	document.getElementById(field_id + '_holder').value = colour;
	document.getElementById(field_id).value = colour;
	document.getElementById(field_id + '_table').style.display = "none";
}
function colour_try(field_id) {	
if (!document.getElementById) return false;
	try {
	document.getElementById(field_id + '_preview').style.background = document.getElementById(field_id).value;
	document.getElementById(field_id + '_holder').value = document.getElementById(field_id).value;
	} catch(er) {
	document.getElementById(field_id + '_preview').style.background = document.getElementById(field_id + '_holder').value;
	document.getElementById(field_id).value = document.getElementById(field_id + '_holder').value;
	}
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}






// Browser Detection
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	}
	else if  (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
		window.onload = f;
}
function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}

// Card payment stuff

function highlight_form(fldname) {
	if (document.getElementById) {
		if (document.getElementById(fldname)) {
			document.getElementById(fldname).className += " inputerror";
			document.getElementById(fldname).focus()
			}
	}
}

function process_card_check() {
	if (!document.getElementById) { return true; }
	
	if (document.getElementById("card_number").value.length < 16) {
		alert("Please enter your card number in full.");
		document.getElementById("card_number").className += " inputerror";
		document.getElementById("card_number").focus();
		return false;
		}
	if (document.getElementById("expiry_month").selectedIndex == 0) {
		alert("Please enter the month of your card\'s expiry date.");
		document.getElementById("expiry_month").focus();
		return false;
		}
	if (document.getElementById("expiry_year").selectedIndex == 0) {
		alert("Please enter the year of your card\'s expiry date.");
		document.getElementById("expiry_year").focus();
		return false;
		}
	if (document.getElementById("csc").value.length == 0) {
		alert("Please enter your card security code.\n\nThis is normally located at the rear of your card.");
		document.getElementById("csc").focus();
		document.getElementById("csc").className += " inputerror";
		return false;
		}

	return true;
}

function payment_cardtype() {
	if (document.getElementById("card_type").selectedIndex == 0) {
		document.getElementById("payment_startdate").style.display = "block";
	} else {
		document.getElementById("payment_startdate").style.display = "none";
	}
}






addEvent(window, "load", sortables_init);

var SORT_COLUMN_INDEX;

function sortables_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
            //initTable(thisTbl.id);
            ts_makeSortable(thisTbl);
        }
    }
}

function ts_makeSortable(table) {
    if (table.rows && table.rows.length > 0) {
        var firstRow = table.rows[0];
    }
    if (!firstRow) return;
    
    // We have a first row: assume it's the header, and make its contents clickable links
    for (var i=0;i<firstRow.cells.length;i++) {
        var cell = firstRow.cells[i];
        var txt = ts_getInnerText(cell);
        cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this);return false;">'+txt+'<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a>';
    }
}

function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

function ts_resortTable(lnk) {
    // get the span
    var span;
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = td.cellIndex;
    var table = getParent(td,'TABLE');
    
    // Work out a type for the column
    if (table.rows.length <= 1) return;
    var itm = ts_getInnerText(table.rows[1].cells[column]);
    sortfn = ts_sort_caseinsensitive;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
    if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
    SORT_COLUMN_INDEX = column;
    var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
    for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }

    newRows.sort(sortfn);

    if (span.getAttribute("sortdir") == 'down') {
        ARROW = '&nbsp;&nbsp;&uarr;';
        newRows.reverse();
        span.setAttribute('sortdir','up');
    } else {
        ARROW = '&nbsp;&nbsp;&darr;';
        span.setAttribute('sortdir','down');
    }
    
    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
    for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
    
    // Delete any other arrows there may be showing
    var allspans = document.getElementsByTagName("span");
    for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '&nbsp;&nbsp;&nbsp;';
            }
        }
    }
        
    span.innerHTML = ARROW;
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}
function ts_sort_date(a,b) {
    // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if (aa.length == 10) {
        dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
    } else {
        yr = aa.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
    }
    if (bb.length == 10) {
        dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
    } else {
        yr = bb.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
    }
    if (dt1==dt2) return 0;
    if (dt1<dt2) return -1;
    return 1;
}

function ts_sort_currency(a,b) { 
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    return parseFloat(aa) - parseFloat(bb);
}

function ts_sort_numeric(a,b) { 
    aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

function ts_sort_caseinsensitive(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

function ts_sort_default(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}


function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 
