function object_action(strType, lngID, strMethod, strPromptText) {
	var strOutputType;
	var strOutputMethod;
	var blnContinue = true;
	
	switch(strType) {
	case "cl":
		strOutputType = "Client";
		break;
	case "ld":
		strOutputType = "Mailing List Item";
		break;
	case "ls":
		strOutputType = "Mailing List";
		break;
	case "nt":
		strOutputType = "Note";
		break;
	case "sh":
		strOutputType = "Shot";
		break;
	case "sp":
		strOutputType = "System Parameter";
		break;
	case "tp":
		strOutputType = "Template";
		break;
	case "us":
		strOutputType = "User";
		break;
	default:
		alert("Unknown Record Type: " + strType);
		return false;
	}
	
	switch(strMethod) {
	case "delete":
		strOutputMethod = "Delete";
		break;
	case "restore":
		strOutputMethod = "Restore";
		break;
	default:
		alert("Unknown Record Action: " + strMethod);
		return false;
	}
	
	if (strPromptText == "")
		strPromptText = "with " + strType + "ID = " + lngID;
	else
		strPromptText = "for '" + strPromptText + "' (ID = " + lngID + ")";
	if (confirm("Are you sure you wish to " + strOutputMethod + " " + strOutputType + " Record " + strPromptText + "?") == true) {
		document.forms[1].id.value = lngID;
		if ((strType == "hc") || (strType == "nt"))
			document.forms[1].tblnotemethod.value = strMethod;
		else
			document.forms[1].method.value = strMethod;
		document.forms[1].submit();
	}
}

function check_number(strFieldID) {
	var strNumber = document.getElementById(strFieldID).value;
	if (!strNumber == "") {
		if (isNaN(Number(strNumber))) {
			document.getElementById(strFieldID).focus();
			document.getElementById(strFieldID).select();
			alert(strNumber + " is is not a valid number!");
			return false;
		}
	}
}

function validate_emailaddress(strAddress) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(strAddress) == false) {
		return false;
	}
	return true;
}

function validate_number(datafield, blankandzeroacceptable) {
	var strTemp = String(datafield.value).split(',').join('');
	strTemp = String(strTemp).split('£').join('');
	//strTemp = String(strTemp).split(',').join('');
	if (Trim(strTemp) == '')
		if (blankandzeroacceptable)
			return true;
		else
			return false;
	if (isNaN(strTemp))
		return false;
	datafield.value = strTemp;
	if ((Number(strTemp) < 1) && (blankandzeroacceptable = false))
		return false;
	return true;
}

function validate_password(strPassword, intMinimumPasswordLength) {
	var reg = new RegExp('([A-Za-z0-9]{' + intMinimumPasswordLength + ',})');
	if(reg.test(strPassword) == false) {
		return false;
	}
	return true;
}

function validate_postcode(strPostcode) {
	var reg = /^([a-zA-Z]{1,2}[0-9][0-9A-Za-z]? [0-9][a-zA-Z]{2})$/;
	if(reg.test(strPostcode) == false) {
		return false;
	}
	return true;
}

function validate_time(datafield) {
	var strTime = datafield.value;
	if (!strTime == "") {
		if (!/^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(strTime)) {
			document.getElementById(strFieldID).focus();
			document.getElementById(strFieldID).select();
			alert(strTime + " is is not a valid time!");
			return false;
		}
	}
}

function Trim(strText) {
	if (strText == null)
		return '';
	return strText.replace(/^\s+|\s+$/g, '');
}

function check_field() {
	var blnStatus = true;
	var blnZeroAcceptable;
	var arrArgs = check_field.arguments;
	var strBackgroundColour = String(arrArgs[0]);
	var strErrorBackgroundColour = '#ff69b4';
	var datafield;

	for(i=1; i<arrArgs.length; i++) {
		if (String(arrArgs[i]).substring(0, 2) == "DP")
			datafield = document.getElementsByName(String(arrArgs[i]).substring(2))[0];
		else
			datafield = document.getElementById(String(arrArgs[i]).substring(2));
		switch (String(arrArgs[i]).substring(0, 2)) {
		case "BL": case "PW":
			if ((Trim(datafield.value) == '') || (Trim(datafield.value) == '* required')) {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				if (String(arrArgs[i]).substring(0, 2) == "BL")
					datafield.value = '* required';
				blnStatus = false;
			}
			else
				datafield.style.backgroundColor = strBackgroundColour;
			break;
		case "NM": case "N0":
			blnZeroAcceptable = true;
			if (String(arrArgs[i]).substring(0, 2) == "N0")
				blnZeroAcceptable = false;
			if (validate_number(datafield, blnZeroAcceptable))
				datafield.style.backgroundColor = strBackgroundColour;
			else {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				blnStatus = false;
			}
			break;			
		case "EM":
			if (validate_emailaddress(datafield.value))
				datafield.style.backgroundColor = strBackgroundColour;
			else {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				if (Trim(datafield.value) == '')
					datafield.value = '* required';
				blnStatus = false;
			}
			break;
		case "PC":
			if (datafield.value == '* required')
				datafield.value = '';
			strTemp = String(datafield.value).split(' ').join('');
			if (strTemp.length > 4)
				datafield.value = strTemp.substring(0, strTemp.length - 3) + ' ' + strTemp.substring(strTemp.length - 3);
			if (validate_postcode(datafield.value))
				datafield.style.backgroundColor = strBackgroundColour;
			else {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				if (Trim(datafield.value) == '')
					datafield.value = '* required';
				blnStatus = false;
			}
			break;
		case "DP": // as opposed to DT for date, DP means date picker
			if (datafield.value == 'None') {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				blnStatus = false;
			}
			else
				datafield.style.backgroundColor = strBackgroundColour;
			break;
		case "DR":
			if (datafield.value == '-42') {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				blnStatus = false;
			}
			else
				datafield.style.backgroundColor = strBackgroundColour;
			break;		
		default:
			alert("error");
		}
	}
	return blnStatus;
}

function reset_background() {
	var strBackgroundColour = '#ffffff';
	var arrArgs = reset_background.arguments;
	if (arrArgs.length > 1) 
		strBackgroundColour = String(arrArgs[1]);
	arrArgs[0].style.backgroundColor = strBackgroundColour;
	if (arrArgs[0].value == '* required')
		arrArgs[0].value = '';
}

function check_frmListData() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('EMemail');

	return blnReturn;
}

function check_index() {
	if(document.getElementById('toppostcode').value == "Enter a full postcode") document.getElementById('toppostcode').value = "";
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('transparent', 'PCtoppostcode');

	return blnReturn;
}

function check_login() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLemail', 'PWpassword');

	return blnReturn;
}

function check_register(intMinimumPasswordLength) {
	var blnReturn = true;
	var errorfield;
	
	document.getElementById('passworderror').innerText = '';
	document.getElementById('passwordcerror').innerText = '';
	blnReturn = check_field('#ffffff', 'BLfirstname', 'BLlastname', 'BLaddress1', 'BLtown', 'PCpostcode', 'BLtelephone', 'EMemail', 'PWpassword', 'PWpasswordc', 'BLabout');
	if (blnReturn == true) {
		if (validate_password(document.forms[1].password.value, intMinimumPasswordLength)) {
			if (Trim(document.forms[1].password.value).toLowerCase() != Trim(document.forms[1].passwordc.value).toLowerCase()) {
				document.getElementById('passwordcerror').innerText = 'Your confirmation does not match your password';
				blnReturn = false;
			}
		} else {
			document.getElementById('passworderror').innerText = 'Your password must be at least ' + intMinimumPasswordLength + ' alphanumeric characters [A-Z, 0-9]';
			blnReturn = false;
		}
	}
	return blnReturn;
}

function check_report() {
	var strBackgroundColour = '#ffffff';
	var strErrorBackgroundColour = '#ff69b4';
	var blnReturn = true;
	var errorfield;
	document.forms[1].fexport.value = 'false';
	if (Trim(document.forms[1].price_from.value).toLowerCase() != 'from...')
		blnReturn = check_field('#ffffff', 'NMprice_from');
	if (blnReturn) {
			if (Trim(document.forms[1].price_to.value).toLowerCase() != 'to...') {
				blnReturn = check_field('#ffffff', 'NMprice_to');
				if ((blnReturn) && (Trim(document.forms[1].price_from.value).toLowerCase() != 'from...') && (Trim(document.forms[1].price_to.value).toLowerCase() != 'to...')) {
					if ((Number(document.forms[1].price_from.value)) > (Number(document.forms[1].price_to.value))) {
						document.forms[1].price_from.style.backgroundColor = strErrorBackgroundColour;
						document.forms[1].price_to.style.backgroundColor = strErrorBackgroundColour;
						blnReturn = false;
					} else {
						document.forms[1].price_from.style.backgroundColor = strBackgroundColour;
						document.forms[1].price_to.style.backgroundColor = strBackgroundColour;
					}
				}
			}
		}
	if (blnReturn) {
		if ((document.forms[1].bedrooms_from.value != '*') && (document.forms[1].bedrooms_to.value != '*')) {
			if (document.forms[1].bedrooms_from.value > document.forms[1].bedrooms_to.value) {
				document.forms[1].bedrooms_from.style.backgroundColor = strErrorBackgroundColour;
				document.forms[1].bedrooms_to.style.backgroundColor = strErrorBackgroundColour;
				blnReturn = false;
			} else {
				document.forms[1].bedrooms_from.style.backgroundColor = strBackgroundColour;
				document.forms[1].bedrooms_to.style.backgroundColor = strBackgroundColour;
			}
		}
	}
	return blnReturn;
}

function check_addShot() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('DRtemplate');

	return blnReturn;
}

function check_frmIssue() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('BLdescription','BLlastname','PCpostcode','BLtelephone');

	if (blnReturn)
		if (document.forms[1].coldspotv.checked == true)
			blnReturn = check_field('BLnumber','BLaddress1');

	if (blnReturn)
		if (document.forms[1].email.value != "")
			blnReturn = check_field('EMemail');
	
	return blnReturn;
}



function next_text() {
	var RowCounter = document.forms[1].nexttxtrow.value;
	if (Number(RowCounter) < 16) {
		document.getElementById("txtrow" + RowCounter).style.display = "";
		document.forms[1].nexttxtrow.value = Number(RowCounter) + 1;
	}
}
function next_image() {
	var RowCounter = document.forms[1].nextimgrow.value;
	if (Number(RowCounter) < 16) {
		document.getElementById("imgrow" + RowCounter).style.display = "";
		document.forms[1].nextimgrow.value = Number(RowCounter) + 1;
	}
}

function updateChart() {
	var strURL = "/ps/includes/fnChartXML.asp?currTime=" + getTimeForURL();
	var chartObj = getChartFromId("FactTilte");
	strURL = escape(strURL);
	chartObj.setDataURL(strURL);
}

function getTimeForURL(){
	var dt = new Date();
	var strOutput = "";
	strOutput = dt.getHours() + "_" + dt.getMinutes() + "_" + dt.getSeconds() + "_" + dt.getMilliseconds();
	return strOutput;
}

function create_xmlhttprequest() {
	var xhr = null;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}	else {
		if (window.ActiveXObject) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xhr;
}

function outcode_change() {
	refresh_incodes();
	if (document.getElementById("vdp_label").innerText == '')
		refresh_agents();
	else {
		document.forms[1].agent.options.length = 0;
		document.forms[1].agent.options[document.forms[1].agent.options.length] = new Option(unescape('All'), unescape('*'), true, true);
	}
}

function incode_change() {
	refresh_agents();
}

function refresh_incodes() {
	var params = 'requesttype=1&outcode=' + escape(document.forms[1].outcode.value);
	var http = create_xmlhttprequest();
	
	document.getElementById("vdp_label").innerText = "";
	document.forms[1].incode.options.length = 0;
	document.forms[1].incode.options[document.forms[1].incode.options.length] = new Option(unescape('District'), unescape('*'), true, true);
	
	if (http) {
		http.onreadystatechange = function() { 
			var doc;
			var element;
			if (http.readyState == 4) {
				if (http.status == 200) {
					doc = http.responseXML;
					element = doc.getElementsByTagName('result').item(0);						
					if (unescape(element.firstChild.data) == "Success") {
						element = doc.getElementsByTagName('incodes').item(0);
						element = element.firstChild;
						while (element) {
							document.forms[1].incode.options[document.forms[1].incode.options.length] = new Option(unescape(element.firstChild.data), unescape(element.firstChild.data), false, false);
							element = element.nextSibling;
						}
					} else
						document.getElementById("vdp_label").innerText = unescape(element.firstChild.data);
				} else 
					document.getElementById("vdp_label").innerText = "Error code " + http.status;
			}
		};
		http.open("POST", "report_refresh.ASP",  true); 
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
//	http.setRequestHeader("Connection", "close"); leaving this line in gives Error Code 12152 on the live server, although works fine on ws-39
		http.send(params);
	}
	else
		document.getElementById("vdp_label").innerText = "Unable to Create XMLHttpRequest";
}

function refresh_agents() {
	var params = 'requesttype=2&outcode=' + escape(document.forms[1].outcode.value) + '&incode=' + escape(document.forms[1].incode.value) + '&weekfrom=' + escape(document.forms[1].week_from.value) + '&weekto=' + escape(document.forms[1].week_to.value) + '&ignoreweekto=' + escape(document.forms[1].ignoreweek_to.value);
	var http = create_xmlhttprequest();
	var strAgent = document.forms[1].agent.value;

	document.getElementById("vdp_label").innerText = "";
	document.forms[1].agent.options.length = 0;
	document.forms[1].agent.options[document.forms[1].agent.options.length] = new Option(unescape('All'), unescape('*'), true, true);
	
	if (http) {
		http.onreadystatechange = function() { 
			var doc;
			var element;
			if (http.readyState == 4) {
				if (http.status == 200) {
					doc = http.responseXML;
					element = doc.getElementsByTagName('result').item(0);						
					if (unescape(element.firstChild.data) == "Success") {
						element = doc.getElementsByTagName('agents').item(0);
						element = element.firstChild;
						while (element) {
							blnSelected = false;
							if (unescape(element.firstChild.data) == strAgent)
								blnSelected = true;
							document.forms[1].agent.options[document.forms[1].agent.options.length] = new Option(unescape(element.firstChild.data), unescape(element.firstChild.data), false, blnSelected);
							element = element.nextSibling;
						}
					} else
						document.getElementById("vdp_label").innerText = unescape(element.firstChild.data);
				} else 
					document.getElementById("vdp_label").innerText = "Error code " + http.status;
			}
		};
		http.open("POST", "report_refresh.ASP",  true); 
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
//	http.setRequestHeader("Connection", "close"); leaving this line in gives Error Code 12152 on the live server, although works fine on ws-39
		http.send(params);
	}
	else
		document.getElementById("vdp_label").innerText = "Unable to Create XMLHttpRequest";
}

function click_search() {
	if (check_report() == true) {
		document.forms[1].currentpage.value = '';
		document.forms[1].submit();
	}
}

function click_export() {
	if (check_report() == true) {
		document.forms[1].fexport.value = 'true';
		document.forms[1].currentpage.value = '';
		document.forms[1].submit();
	}
}

function click_report(intReport) {
	if (check_report() == true) {
		document.forms[1].report.value = intReport;
		document.forms[1].viewsort.value = '';
		document.forms[1].currentpage.value = '';
		document.forms[1].submit();
	}
}

function click_style(intStyle) {
	if (check_report() == true) {
		document.forms[1].style.value = intStyle;
		document.forms[1].viewsort.value = '';
		document.forms[1].currentpage.value = '';
		document.forms[1].submit();
	}
}

function click_branch(intBranch) {
	if (check_report() == true) {
		document.forms[1].branch.value = intBranch;
		document.forms[1].currentpage.value = '';
		document.forms[1].submit();
	}
}

function click_page(intPage) {
	if (check_report() == true) {
		document.forms[1].currentpage.value = intPage;
		document.forms[1].submit();
	}
}

function move_strip(intMove) {
	var intCurrent = Number(document.forms[1].menupage.value);
	var intMenuPage = intCurrent + intMove;
	if (intMenuPage > Number(document.forms[1].maxpage.value) - 9)
		intMenuPage = Number(document.forms[1].maxpage.value) - 9;
	if (intMenuPage < 1)
		intMenuPage = 1;
	if (intMenuPage != intCurrent) {
		for (i = 0; i < 10; i++)
			document.getElementById('pageid' + (intCurrent + i)).className = 'hidden';
		for (i = 0; i < 10; i++)
			if (intMenuPage + i == Number(document.forms[1].currentpage.value))
				document.getElementById('pageid' + (intMenuPage + i)).className = 'chosen';
			else
				document.getElementById('pageid' + (intMenuPage + i)).className = 'regular';
		document.forms[1].menupage.value = intMenuPage;
	}
	return false;		
}

function trigsearch() {
	document.getElementById('searchl').style.visibility = "visible";
	document.getElementById('searchb').style.visibility = "visible";
}
