function OpenJob (JobID){
	PopUpWindow ('Valuation.asp?JobID=' + JobID, 'Valuation_' + JobID, 1024, 600);
}

function PrintJob (JobID){
	PopUpWindow ('PrintFrame.asp?JobID=' + JobID, 'PrintValuation_' + JobID, 900, 600);
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue (buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function setSelectedRadio (buttonGroup, index) {
	if (buttonGroup[index]) { // Make sure the button group is an array (not just one button)
		buttonGroup[index].checked = true;
	} else { // The button group is just the one button
		buttonGroup.checked
	}
}

function setSelectedRadioValue (buttonGroup, value) {
	if (buttonGroup[0]) { // Make sure the button group is an array (not just one button)
		for (var i = 0; i < buttonGroup.length; i++){
			buttonGroup[i].checked = (buttonGroup[i].value == value);
		}
	} else { // The button group is just the one button
		buttonGroup.checked;
	}
}

function IsDefinedWindow (FieldID, TargetWindow){
	if (TargetWindow.document != undefined){
		// this is a window handle
		return TargetWindow.document.getElementById(FieldID) != undefined;
	} else {
		// this is an iframe
		return document.getElementById(TargetWindow).contentWindow.document.getElementById(FieldID) != undefined;
	}
}

function IsDefined (FieldID){
	return IsDefinedWindow (FieldID, this);
}

function GetValueWindow (FieldID, TargetWindow){
	if (IsDefinedWindow (FieldID, TargetWindow)){
		if (TargetWindow.document != undefined){
			return TargetWindow.document.getElementById(FieldID).value;
		} else {
			return document.getElementById(TargetWindow).contentWindow.document.getElementById(FieldID).value;
		}
	}
	return 0;
}

function GetValue (FieldID){
	return GetValueWindow (FieldID, this);
}

function SetValueWindow (FieldID, TargetWindow, Value){
	if (IsDefinedWindow (FieldID, TargetWindow)){
		if (TargetWindow.document.getElementById(FieldID).value != undefined){
			// assume this to be a form field: write into the .value attribute
			TargetWindow.document.getElementById(FieldID).value = Value;
		} else {
			// assume this to be a div, etc: write into the .innerHTML attribute
			Value = Value.replace (/\n/g, "<br/>");
			TargetWindow.document.getElementById(FieldID).innerHTML = Value;
		}
	}
}

function SetValue (FieldID, Value){
	SetValueWindow (FieldID, this, Value);
}

function GetField (FieldID){
	if (IsDefinedWindow (FieldID, this)){
		return this.document.getElementById(FieldID);
	} else {
		return null;
	}
}

function GetFieldWindow (FieldID, TargetWindow){
	if (IsDefinedWindow (FieldID, TargetWindow)){
		if (TargetWindow.document != undefined){
			return TargetWindow.document.getElementById(FieldID);
		} else {
			return document.getElementById(TargetWindow).contentWindow.document.getElementById(FieldID);
		}
	} else {
		return null;
	}
}

function CheckDateValidity (FieldID, TargetWindow){
	if (ValidateDateWindow (FieldID, TargetWindow)){
		return true;
	}
	TargetField = TargetWindow.document.getElementById(FieldID);
	alert ("Error: Date field '" + TargetField.FriendlyName + "' is not valid, please enter date in the format dd/mm/yyyy.");
	setTimeout ("TargetField.focus();", 100);
	return false;
}

function ValidateDateWindow (FieldID, TargetWindow){
	var DateValid = false;
	if (IsDefinedWindow (FieldID, TargetWindow)){
		// check date is property formed
		var pattern = new RegExp ("^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2}|[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}|)$");
		if (pattern.test (GetValueWindow (FieldID, TargetWindow))){
			// check date itself is valid
			if (GetValueWindow (FieldID, TargetWindow).length == 0){
				DateValid = true;
			} else {
				var DateParts = GetValueWindow (FieldID, TargetWindow).split ("/");
				if (DateParts.length == 3){
					var ThisDay   = DateParts[0];
					var ThisMonth = DateParts[1] - 1;
					var ThisYear  = DateParts[2];
					var ThisDate  = new Date (ThisYear, ThisMonth, ThisDay);
					if (ThisDate.getDate()  == ThisDay   &&
						ThisDate.getMonth() == ThisMonth &&
						((ThisYear.length == 2 && ThisDate.getYear()     == ThisYear) ||
						 (ThisYear.length == 4 && ThisDate.getFullYear() == ThisYear))){
						DateValid = true;
					}
				}
			}
		}
	}
	return DateValid;
}

function ValidateDate (FieldID){
	return ValidateDateWindow (FieldID, this);
}

function GetParentTabWindow (FieldID, TargetWindow){
	var TabTest = new RegExp ("^Tab[0-9]+$");

	var CurrentElement = TargetWindow.document.getElementById (FieldID);
	while (CurrentElement != undefined && ! TabTest.test (CurrentElement.id)){
		CurrentElement = CurrentElement.parentNode;
	}

	return CurrentElement;
}

function GetParentTab (FieldID){
	return GetParentTabWindow (FieldID, this);
}

document.getElementsByClassName = function (clsName){
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");

	for (var i = 0; i < elements.length; i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
				for (var j = 0; j < classes.length; j++){
					if (classes[j] == clsName) retVal.push(elements[i]);
				}
			}
		else if (elements[i].className == clsName) retVal.push(elements[i]);
	}

	return retVal;
}

document.getElementsByClassNameAndType = function (clsName, type){
	var retVal = new Array();
	var elements = document.getElementsByTagName(type);

	for (var i = 0; i < elements.length; i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
				for (var j = 0; j < classes.length; j++){
					if (classes[j] == clsName) retVal.push(elements[i]);
				}
			}
		else if (elements[i].className == clsName) retVal.push(elements[i]);
	}

	return retVal;
}

function getOffsetLeft (currentElem){
	var offset = 0;

	while (currentElem != null)
	{
		if (currentElem.offsetLeft != undefined) offset += currentElem.offsetLeft;
		currentElem = currentElem.offsetParent;
	}

	return offset;
}

function getOffsetTop (currentElem){
	var offset = 0;

	while (currentElem != null)
	{
		if (currentElem.offsetTop != undefined) offset += currentElem.offsetTop;
		currentElem = currentElem.offsetParent;
	}

	return offset;
}

function JumpToLetter (letter, target){
	letter = letter.substring (0, 1).toLowerCase();

	if (!letter.match(/[a-z]/)) return false;

	var allAnchors = document.getElementsByClassNameAndType ("anchor", "div");
	for (var i = 0; i < allAnchors.length; i++)
		if (allAnchors[i].name == letter){
			target.scrollLeft = getOffsetLeft (allAnchors[i]);
			target.scrollTop  = getOffsetTop  (allAnchors[i]);
		}
}

function LogFileSize (Filename){
	document.getElementById('LogAction').src = 'LogAction.asp?Activity=Loaded%20file%20' + Filename + ':%20' + document.body.innerHTML.length + 'KB';
}

function InitSplash (SplashName, SplashText){
	//alert ('a');
	//var splash = GetFieldWindow (TargetWindow).createElement ('div');
	var splash = document.createElement ('div');
	//alert ('b');

	splash.id = SplashName;
	splash.style.position		= "absolute";
	splash.style.display		= "none";
	splash.style.top			= "0px";
	splash.style.left			= "0px";
	splash.style.width			= "100%";
	splash.style.height			= "100%";
	splash.style.cursor			= "wait";
	/*
	splash.style.borderLeft		= "thin solid LightGrey";
	splash.style.borderTop		= "thin solid LightGrey";
	splash.style.borderRight	= "thin solid Black";
	splash.style.borderBottom	= "thin solid Black";
	*/
	splash.style.textAlign		= "center";
	splash.style.verticalAlign	= "middle";
	splash.innerHTML            = "<div style=\"position: absolute; top: 0px; left: 0px; height: 100%; width: 100%; background-color: Black; filter:alpha(opacity=25); z-index: -1;\"></div>" +
								  "<table height=\"100%\" width=\"100%\">" +
								  "   <tr>" +
								  "      <td style=\"text-align: center; vertical-align: middle;\">" +
								  "         <p style=\"border: solid 2px black; background-color: white; width: 100px; height: 20px; text-align: center;\">" + SplashText + "</p>" +
								  "      </td>" +
								  "   </tr>" +
								  "</table>";

	document.getElementsByTagName('body')[0].appendChild(splash);
}

function InitSplashWindow (SplashName, SplashText, TargetWindow){
	//alert ('a');
	//var splash = GetFieldWindow (TargetWindow).createElement ('div');
	var splash = document.createElement ('div');
	//alert ('b');

	splash.id = SplashName;
	splash.style.position		= "absolute";
	splash.style.display		= "none";
	splash.style.top			= "0px";
	splash.style.left			= "0px";
	splash.style.width			= "100%";
	splash.style.height			= "100%";
	splash.style.cursor			= "wait";
	/*
	splash.style.borderLeft		= "thin solid LightGrey";
	splash.style.borderTop		= "thin solid LightGrey";
	splash.style.borderRight	= "thin solid Black";
	splash.style.borderBottom	= "thin solid Black";
	*/
	splash.style.textAlign		= "center";
	splash.style.verticalAlign	= "middle";
	splash.innerHTML            = "<div style=\"position: absolute; top: 0px; left: 0px; height: 100%; width: 100%; background-color: Black; filter:alpha(opacity=25); z-index: -1;\"></div>" +
								  "<table height=\"100%\" width=\"100%\">" +
								  "   <tr>" +
								  "      <td style=\"text-align: center; vertical-align: middle;\">" +
								  "         <p style=\"border: solid 2px black; background-color: white; width: 100px; height: 20px; text-align: center;\">" + SplashText + "</p>" +
								  "      </td>" +
								  "   </tr>" +
								  "</table>";

	document.getElementsByTagName('body')[0].appendChild(splash);
}

function ShowSplash (SplashName){
	ShowSplashWindow (SplashName, this);
}

function ShowSplashWindow (SplashName, TargetWindow){
	if (IsDefinedWindow (SplashName, TargetWindow)){
		GetFieldWindow (SplashName, TargetWindow).style.top = document.body.scrollTop;
		GetFieldWindow (SplashName, TargetWindow).style.display = 'block';
	}
}

function ClearSplash (SplashName){
	ClearSplashWindow (SplashName, this);
}

function ClearSplashWindow (SplashName, TargetWindow){
	if (IsDefinedWindow (SplashName, TargetWindow)){
		GetFieldWindow (SplashName, TargetWindow).style.display = 'none';
	}
}

// Copy a URL to the user's clipboard.  This provides easy access to diagnose AJAX problems and other internal page references
// Arguments: url - the URL to copy
// Hint: CopyURL (null) will use the current browser URL
function CopyURL (url){
	if (url == undefined){
		// use the current page URL
		url = window.location.href;
	} else {
		// check the URL provided contains protocol and host info

		// check for the "//" string - this should be the protocol separator
		if (url.indexOf ("//") < 0){
			// no protocol provided, check whether the beginning of the URL appears to be a host reference
			if (url.indexOf ("/") < 0){
				if (url.indexOf (".") < 0 || url.indexOf ("?") >= 0){
					// url is a single word, eg "mysite", assume this is a page/subfolder of the current host
					url = window.location.protocol + "//" + window.location.host + "/" + url;
				} else {
					// url is of the form "mysite.com"
					if (url == window.location.host){
						// url is for the current site, so use current protocol
						url = window.location.protocol + "//" + url;
					} else {
						// url is for another site (or a different subdomain of the current host), assume http protocol
						url = "http://" + url;
					}
				}
			} else {
				// url is of the form "mysite.com/page.html"
				if (url.substring (0, url.indexOf ("/") - 1) == window.host){
					// url is for the current site, so use current protocol
					url = window.location.protocol + "//" + url;
				} else {
					// url is for another site (or a different subdomain of the current host), assume http protocol
					url = "http://" + url;
				}
			}
		} else {
			// protocol is provided, assume host is also provided (ie don't modify url)
		}
	}

	// confirm with the user
	if (confirm ("URL: " + url + "\n\nCopy to clipboard? (NB: current clipboard data will be overwritten!)")) window.clipboardData.setData ("Text", url);
}

// Copy text to the user's clipboard
function CopyText (text){
	if (confirm ("Text: " + text + " (" + text.length + " chars)\n\nCopy to clipboard?")) window.clipboardData.setData ("Text", text);
}

function PasteURL (){
	var url = window.clipboardData.getData ("Text");
	if (confirm ("URL: " + url + "\n\nGo to this address?")) window.location.href = url;
}

function PasteText (){
	var text = window.clipboardData.getData ("Text");
	if (confirm ("Text: " + text + " (" + text.length + ")\n\nPaste into active field?")){
		// check an element has focus
		if (document.activeElement != undefined){
			if (document.activeElement.value != undefined){
				document.activeElement.value = text;
			} else {
				document.activeElement.innerHTML = text;
			}
		} else {
			alert ("ERROR: No current active element, please select a field and try again.");
		}
	}
}

function MakeNumber (str){
	str = str.replace (/[\$\, ]/g, "");
	return str;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
