function object_action(strType, lngID, strMethod, strPromptText) {
	var strOutputType;
	var strOutputMethod;
	var blnContinue = true;
	
	switch(strType) {
	case "cl":
		strOutputType = "Client";
		break;
	case "ev":
		strOutputType = "Event";
		break;
	case "od":
		strOutputType = "Order";
		break;
	case "nt":
		strOutputType = "Note";
		break;
	case "nw":
		strOutputType = "Press";
		break;
	case "sp":
		strOutputType = "System Parameter";
		break;
	case "st":
		strOutputType = "Stockist";
		break;
	case "of":
		strOutputType = "Promotion";
		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[0].id.value = lngID;
		if ((strType == "hc") || (strType == "nt"))
			document.forms[0].tblnotemethod.value = strMethod;
		else
			document.forms[0].method.value = strMethod;
		document.forms[0].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_creditcard(datafield) {
	var strTemp = String(datafield.value).split('-').join('');
	strTemp = String(strTemp).split(' ').join('');
	strTemp = Trim(strTemp);
	if (strTemp.length != 16)
		return false;
	if (isNaN(strTemp))
		return false;
	datafield.value = strTemp;
	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 = '#8ea9f0';
	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 "CC":
			if (validate_creditcard(datafield))
				datafield.style.backgroundColor = strBackgroundColour;
			else {
				datafield.style.backgroundColor = strErrorBackgroundColour;
				if (Trim(datafield.value) == '')
					datafield.value = '* required';
				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')
	if (((arrArgs[0].value + '  ').substring(0, 2) == '* ') || (arrArgs[0].value == '0'))
		arrArgs[0].value = '';
}

function check_frmEvent() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLtitle', 'BLdescription', 'DPdate');

	return blnReturn;
}

function check_frmPress() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLpublication', 'BLtitle', 'BLsummary', 'BLarticle');

	return blnReturn;
}

function addtobag() {
	var blnReturn = true;
	var intPacks = document.forms[0].packs.value;
	var intPack = -1
	
	while ((blnReturn) && (intPack < intPacks)) {
		intPack++;
		blnReturn = check_field('#ffffff', 'NMquantity' + intPack);
	}
	return blnReturn;
}

function check_login() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLlname', 'PWlpassword');

	return blnReturn;
}

function check_contact() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLfirstname', 'BLlastname', 'EMemail', 'BLmessage');

	return blnReturn;
}

function check_register(intMinimumPasswordLength, intUserID) {
	var blnReturn = true;
	var errorfield;
	
	document.frmRegister.telephone.style.backgroundColor = '#ffffff';
	if (intUserID < 0) {
		document.getElementById('passworderror').innerText = '';
		document.getElementById('passwordcerror').innerText = '';
		blnReturn = check_field('#ffffff', 'BLfirstname', 'BLlastname', 'BLaddress1', 'BLaddress4', 'PCpostcode', 'EMemail', 'PWpassword', 'PWpasswordc');
		if (blnReturn == true) {
			if (validate_password(document.frmRegister.password.value, intMinimumPasswordLength)) {
				if (Trim(document.frmRegister.password.value).toLowerCase() != Trim(document.frmRegister.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;
			}
		}
	} else
		blnReturn = check_field('#ffffff', 'BLfirstname', 'BLlastname', 'BLaddress1', 'BLaddress4', 'PCpostcode', 'EMemail');
	if (blnReturn)
		if (((Trim(document.frmRegister.telephone.value) == '') || (Trim(document.frmRegister.telephone.value) == '* required')) && (Trim(document.frmRegister.mobile.value) == ''))
			blnReturn = check_field('#ffffff', 'BLtelephone')
	return blnReturn;
}

function check_frmUser() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLname', 'BLfirstname', 'BLlastname', 'BLpassword', 'BLaddress1', 'BLaddress4', 'PCpostcode', 'EMemail');

	return blnReturn;
}

function check_frmStockist() {
	var blnReturn = true;
	var errorfield;
	
	blnReturn = check_field('#ffffff', 'BLname', 'BLaddress1', 'BLaddress4', 'PCpostcode');

	return blnReturn;
}

function check_checkout(intMinimumPasswordLength, intUserID) {
	var blnReturn = true;
	var errorfield;

	if (wasSubmitted)
		return false;
	wasSubmitted = true;
	
	document.forms[0].telephone.style.backgroundColor = '#ffffff';	
	blnReturn = check_field('#ffffff', 'BLfirstname', 'BLlastname', 'BLaddress1', 'BLaddress4', 'PCpostcode', 'EMemail');
	if (blnReturn)
		if (((Trim(document.forms[0].telephone.value) == '') || (Trim(document.forms[0].telephone.value) == '* required')) && (Trim(document.forms[0].mobile.value) == ''))
			blnReturn = check_field('#ffffff', 'BLtelephone')
	if (blnReturn) {
		if ((intUserID < 0) && (document.forms[0].createaccount.value == 'y')) {
			document.getElementById('passworderror').innerText = '';
			document.getElementById('passwordcerror').innerText = '';
			blnReturn = check_field('#ffffff', 'PWpassword', 'PWpasswordc');
			if (blnReturn) {
				if (validate_password(document.forms[0].password.value, intMinimumPasswordLength)) {
					if (Trim(document.forms[0].password.value).toLowerCase() != Trim(document.forms[0].passwordc.value).toLowerCase()) {
						document.forms[0].passwordc.style.backgroundColor = '#8ea9f0';
						document.getElementById('passwordcerror').innerText = 'Your confirmation does not match your password';
						blnReturn = false;
					}
				} else {
					document.forms[0].password.style.backgroundColor = '#8ea9f0';
					document.getElementById('passworderror').innerText = 'Your password must be at least ' + intMinimumPasswordLength + ' alphanumeric characters [A-Z, 0-9]';
					blnReturn = false;
				}
			}
		}
	}
	if ((blnReturn) && (document.forms[0].deliveryaddress.checked))
		blnReturn = check_field('#ffffff', 'BLdname', 'BLdaddress1', 'BLdaddress4', 'PCdpostcode');
	if (blnReturn)
		blnReturn = check_field('#ffffff', 'DRcctype', 'CCccnumber', 'DRcctomonth', 'DRcctoyear', 'BLcccode');
	document.getElementById('agreederror').innerText = '';
	if (!blnReturn)
		document.getElementById('agreederror').innerText = 'Please correct the highlighted errors';
	if ((blnReturn) && (!document.forms[0].agreed.checked)) {
		document.getElementById('agreederror').innerText = 'Your must accept the Terms and Conditions';
		blnReturn = false;
	}
	if (!blnReturn)
		wasSubmitted = false;
		
	return blnReturn;
}

function toggle_createaccount(strCreate) {
	var blnShow = 'none';
	var blnCreateButton = '';
	document.forms[0].createaccount.value = strCreate;
	if (strCreate == 'y') {
		blnShow = '';
		blnCreateButton = 'none';
	}
	document.getElementById('newaccount').style.display = blnShow;
	document.getElementById('donotcreateButton').style.display = blnShow;
	document.getElementById('createButton').style.display = blnCreateButton;
}

function toggle_deliveryaddress() {
	var blnShow = 'none';
	if (document.forms[0].deliveryaddress.checked == true)
		blnShow = '';
	document.getElementById('differentaddress').style.display = blnShow;
}

function next_pack() {
	var intRowCounter = Number(document.forms[0].nextpakrow.value);
	if (intRowCounter < 8) {
		document.getElementById("pakrow" + intRowCounter).style.display = "";
		document.forms[0].nextpakrow.value = intRowCounter + 1;
	}
}

function GLoad(strXMLFile) {
	if (document.getElementById("map_canvas")) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map_canvas"));

			map.addControl(new GMapTypeControl());
			map.addControl(new GLargeMapControl());
			map.addControl(new GScaleControl());

			map.setCenter(new GLatLng(document.forms[0].latitude.value, document.forms[0].longitude.value), Number(document.forms[0].zoom.value));

			GDownloadUrl(strXMLFile, function(data, responseCode) {
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
					map.addOverlay(createMarker(map, point, markers[i]));
				}
			});
		}
	}
}

function createMarker(map, point, xmlnode) {
	var PWCicon = new GIcon(G_DEFAULT_ICON);
	PWCicon.shadow = '';
	PWCicon.iconSize = new GSize(30, 43);
	PWCicon.image = "http://davenportschocolates.co.uk/images_general/stockists_map_pin2.png";
	
	var marker = new GMarker(point, {icon:PWCicon, title:xmlnode.getAttribute("name")});
	GEvent.addListener(marker, "click", function() {
		//var myHtml = "Longitude is " + xmlnode.getAttribute("lng");
		//map.openInfoWindowHtml(point, myHtml);
		var strAddress = "<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#4D221A;'><span style='font-size:16px;font-weight:bold;'>" + xmlnode.getAttribute("name") + "</span><br />" + xmlnode.getAttribute("address") + "</span>";
		var strOther = "";
		if (xmlnode.getAttribute("tel") != "")
			strOther += "<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#4D221A;'><span style='font-size:16px;font-weight:bold;'>Telephone:&nbsp;</span>" + xmlnode.getAttribute("tel") + "</span><br />";
		if (xmlnode.getAttribute("web") != "")
			strOther += "<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#4D221A;'><span style='font-size:16px;font-weight:bold;'>Website:&nbsp;</span><a href='http://" + xmlnode.getAttribute("web") + "' target='_blank'>" + xmlnode.getAttribute("web") + "</a></span>";
          marker.openInfoWindowTabs([new GInfoWindowTab("Address", 
strAddress), new GInfoWindowTab("Tel, etc.", strOther)]);   });
		
	return marker;
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function fadeIn(strDIV, strIMG, intNewImage) {
	var timer = 0;

	document.getElementById(strDIV).style.backgroundImage = "url(" + document.getElementById(strIMG).src + ")";
	changeOpac(0, strIMG);
	document.getElementById(strIMG).src = image[intNewImage].src;
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + strIMG + "')",(timer * 10));
		timer++;
	}
}

function textCounter(objField, objDisplayField, intLimit) {
	if (objField.value.length > intLimit)
		objField.value = objField.value.substring(0, intLimit);
	else
		objDisplayField.innerText = intLimit - objField.value.length + ' characters left';
}

function geoCode(strPostcode) {
	var localSearch = new GlocalSearch();
	
	localSearch.setSearchCompleteCallback(null, function() {
		if (localSearch.results[0]) {
			setLatitudeLongitude(localSearch.results[0].lat, localSearch.results[0].lng);
		} else {
			document.forms[0].geocodestatus.value = 0;
			alert("Unable to locate: " + strPostcode);
		}
	});
	localSearch.execute(strPostcode + ", UK");
}

function setLatitudeLongitude(dblLatitude, dblLongitude) {
	document.forms[0].latitude.value = dblLatitude;
	document.forms[0].longitude.value = dblLongitude;
	document.forms[0].geocodestatus.value = -1;
}
