// =========================================
// VECTOR OBJECT
// =========================================

function oVector(strData) {
	// properties
	this.data			= new Array();
	this.length			= 0;
	this.cursor			= 0;
	this.atEnd			= false;

	// methods
	this.add				= oVectorAdd;
	this.item				= oVectorItem;
	this.first				= oVectorFirst;
	this.last				= oVectorLast;
	this.next			= oVectorNext;
	this.reset			= oVectorReset;
	this.toString			= oVectorToString;
	
	return this;
}

function oVectorAdd(strItem) {
	this.data[this.length] = strItem;
	this.length++;
}

function oVectorItem() {
	return this.data[this.cursor];
}

function oVectorFirst() {
	return this.data[0];
}

function oVectorLast() {
	return this.data[this.length - 1];
}

function oVectorNext() {
	this.cursor++;
	if (this.cursor >= this.length) {
		strValue = null;
		this.atEnd = true;
	} else {
		strValue = this.data[this.cursor];
	}
	return strValue;
}

function oVectorReset() {
	this.cursor = 0;
	this.atEnd = false;
}

function oVectorToString() {
	var strValue = "";
	for (var i = 0; i < this.length; i++) {
		strValue += this.data[i] + "\n";
	}
	return strValue;
}

// =========================================
// FORM ELEMENT INPUT CHECKS
// =========================================

function fnFormCheckString(obj) {
	var val = obj.value;
	if (val == "") { return; }
	obj.value = val;
}

function fnFormCheckCity(obj) {
	var val = obj.value;
	if (val == "") { return; }
	val = fnRemoveIllegalCharacters(val);
	val = fnRemoveNumbers(val);
	obj.value = val;
}

function fnFormCheckZipPostcode(obj) {
	var val = obj.value;
	if (val == "") { return; }
	val = fnRemoveIllegalCharacters(val);
	val = fnRemoveWhitespace(val);
	if (val.length == 5) {
		val = fnRemoveLetters(val);
		if (val.length != 5) {
			val = fnError(val);
		}
	} else {
		if (val.length == 6) {
			val = val.charAt(0) + val.charAt(1) + val.charAt(2) + " " + val.charAt(3) + val.charAt(4) + val.charAt(5);
		} else {
			if (val.length == 9) {
				val = fnRemoveLetters(val);
				if (val.length == 9) {
					val = val.charAt(0) + val.charAt(1) + val.charAt(2) + val.charAt(3) + val.charAt(4) + "-" + val.charAt(5) + val.charAt(6) + val.charAt(7) + val.charAt(8);
				} else {
					val = fnError(val);
				}
			} else {
				if (val.length == 10) {
					val = fnRemoveLetters(val);
					val = val.replace("-","");
					if (val.length == 9) {
						val = val.charAt(0) + val.charAt(1) + val.charAt(2) + val.charAt(3) + val.charAt(4) + "-" + val.charAt(5) + val.charAt(6) + val.charAt(7) + val.charAt(8);
					} else {
						val = fnError(val);
					}
				} else {
					val = fnError(val);
				}
			}
		}
	}
	obj.value = val;
}

function fnFormCheckPhone(obj) {
	var val = obj.value;
	if (val == "") { return; }
	val = fnRemoveIllegalCharacters(val);
	val = fnRemoveLetters(val);
	val = fnRemoveWhitespace(val);
	val = val.replace(/-*/g,"");
	if (val.length == 10) {
		val = "(" + val.charAt(0) + val.charAt(1) + val.charAt(2) + ") " + val.charAt(3) + val.charAt(4) + val.charAt(5) + "-" + val.charAt(6) + val.charAt(7) + val.charAt(8) + val.charAt(9);
	} else {
		val = fnError(val);
	}
	obj.value = val;
}

function fnFormCheckEmail(obj) {
	var val = obj.value;
	if (val == "") { return; }
	if ((val.indexOf("@") < 1) || (val.indexOf(".") < 1)) {
		val = fnError(val);
	}
	obj.value = val;
}

function fnFormCheckInteger(obj) {
	var val = obj.value;
	if (val == "") { return; }
	val = fnRemoveIllegalCharacters(val);
	val = fnRemoveLetters(val);
	obj.value = val;
}

function fnError(str) {
	alert("Sorry, the value you entered couldn't be interpreted.\nPlease try again.");
	str = "error";
	return str;
}

// STRING MANIPULATION FUNCTIONS

function fnRemoveIllegalCharacters(str) {
	str = str.replace(/[^A-Za-z0-9\s-]/g,"");
	return str;
}

function fnRemoveNumbers(str) {
	str = str.replace(/[0-9]*/g,"");
	return str;
}

function fnRemoveLetters(str) {
	str = str.replace(/[a-zA-Z]*/g,"");
	return str;
}

function fnRemoveWhitespace(str) {
	str = str.replace(/[\s]/g,"");
	return str;
}