// These are customised data validation functions
// These variables are global variables that some of these functions make reference to.

// ********************************************************************************************* //

function Validate_Postcode(obj, prevPCode) {
// This function validates postcode entries.
// Intended for use with the 'OnBlur' event.
// If the field had a previous entry, then it restores this old value if the new postcode is invalid.
var Invalid_Postcode = false;

	if ((trimString(obj.value).search(" ") == -1) || (trimString(obj.value).length < 6 && trimString(obj.value) != "")) {
		alert("Invalid postcode! - Please make sure you have included a space.");
		Invalid_Postcode = true;
	} else {
		if (obj.value.indexOf(" ") != 2 && obj.value.indexOf(" ") != 3 && obj.value.indexOf(" ") != 4) {
			alert("Invalid postcode!");
			Invalid_Postcode = true;
		}
	}
	if (Invalid_Postcode) {
		obj.value = prevPCode;
		obj.focus();
		obj.select();
	} else {
		Invalid_Postcode = false;
		obj.value = trimString(obj.value).toUpperCase();
	}
}

// ********************************************************************************************* //

function isValidPostcode(strPostcode) {
// This function validates a postcode.
// returns true or false
var Invalid_Postcode = false;

	if ((trimString(obj.value).search(" ") == -1) || (trimString(obj.value).length < 6 && trimString(obj.value) != "")) {
		Invalid_Postcode = true;
	} else {
		if (obj.value.indexOf(" ") != 2 && obj.value.indexOf(" ") != 4 && obj.value.indexOf(" ") != 5) {
			Invalid_Postcode = true;
		}
	}
	
	if (Invalid_Postcode) {
		return false;
	} else {
		return true;
	}
}

// ********************************************************************************************* //

function doPostcodeSearch(frm, obj, dir, addrFldName, showMsg) {
// this function uses capscan zap tool to search for a postcode using file zap.asp 
// stored in the capscan dir
var vReturnValue;
var vAddress;

	if (((obj.value.search(" ") == -1 || obj.value.length < 6) && (obj.value != "")) || (obj.value == "")) {
		alert("Incomplete or invalid postcode!");
	} else {
		vReturnValue = window.showModalDialog(dir + "zap.asp?pc=" + obj.value, '', 'dialogWidth:10px; dialogHeight:10px;');
		if (vReturnValue == undefined) {
			if (showMsg) {
				alert("No matching address found for postcode: " + obj.value);
			}
		} else {
			vAddress = vReturnValue.split(",");
			var tempStr = "";
			for (var i = 0; i < vAddress[7]; i++) {
				tempStr = tempStr + ", " + vAddress[i];
			}
		
			if (showMsg) {
				var confirmUpdate = (window.confirm("Address match found:\n" + tempStr.substr(2) + "\n\nPlease click on the 'Ok' button to update the address fields to the address found\nor the 'Cancel' button to leave the current address."))
			}
			
			if (!showMsg || (confirmUpdate && showMsg)) {
				for (var i = 0; i < 6; i++) {
					frm.elements(addrFldName[i]).value = "";
				}
			
				if (vAddress.length <= 2) {
					//frm.elements(addrFldName[5]).value = vAddress[0];
				} else {
					if (vAddress[parseInt(vAddress[7]) - 2] == vAddress[parseInt(vAddress[7]) - 2].toUpperCase()) {
						for (var i = 1; i <= (parseInt(vAddress[7]) - 2); i++) {
							frm.elements(addrFldName[i]).value = vAddress[i - 1];
						}
						frm.elements(addrFldName[4]).value = vAddress[parseInt(vAddress[7]) - 2];
					} else {
						for (var i = 1; i <= (parseInt(vAddress[7]) - 3); i++) {
							frm.elements(addrFldName[i]).value = vAddress[i - 1];
						}
						frm.elements(addrFldName[4]).value = vAddress[parseInt(vAddress[7]) - 3];
						frm.elements(addrFldName[5]).value = vAddress[parseInt(vAddress[7]) - 2];
					}
				}
				
				if (showMsg) {
					alert("Please do not forget to update the Building Number field.");
				}
				frm.elements(addrFldName[0]).focus();
			}
		}
	}
}

// ********************************************************************************************* //

function isValidDate (strDate) {
// validates a date string
	
	if (Date.parse(strDate) == "NaN") {
		return false;
	} else {
		return true;
	}
}

// ********************************************************************************************* //

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

// ********************************************************************************************* //

function isNotInitial(obj)
//validates a name - making sure the user has input a name at least two xters in length
{
	if ((trimString(obj.value).length < 2) || (trimString(obj.value).length == 3 && trimString(obj.value).search(" ") != -1) || (trimString(obj.value).length == 4 && trimString(obj.value).search("  ") != -1)) {
		alert("Invalid Forename!\nPlease input a full forename(s) and not an initial");
		obj.value = "";
		obj.focus();
		obj.select();
	} else {
		//return true;
	}
}

// ********************************************************************************************* //

//function tt() {
//   var s;
//   var re = new RegExp("d(c+)(d)","ig");
//   var str = "cdbBdbsbdbdz";
//   var str = "cdcCdbsbdbdz";
//   var arr = re.exec(str);
//   s = "$1 contains: " + RegExp.$1 + "\n";
//   s += "$2 contains: " + RegExp.$2 + "\n";
//   s += "$3 contains: " + RegExp.$3;
//   return(s);
//}

// ********************************************************************************************* //


//function replaceAbbrevText(str) {
// this function is used to replace abbreviated field names
// e.g. company_add1 becomes company address
// var returnStr = str;

//	returnStr = returnStr.replace("company_add", "Company Address");
//	returnStr = returnStr.replace("Company_Add", "Company Address");
//	returnStr = returnStr.replace("contact_add", "Contact Address");
//	returnStr = returnStr.replace("Contact_Add", "Contact Address");
//	returnStr = returnStr.replace("delivery_add", "Delivery Address");
//	returnStr = returnStr.replace("Delivery_Add", "Delivery Address");
//	return returnStr;
//}