MenuImages = new Array();
MenuImages[0] = 'home'
MenuImages[2] = 'about'
MenuImages[3] = 'portfolio'
MenuImages[4] = 'management'
MenuImages[5] = 'news'
MenuImages[6] = 'contact'
MenuImages[7] = 'tsandcs'
MenuImages[8] = 'links'

function SwapImage(ImgNum,OverOut) {
	document.images[ImgNum].src='images/menu/'+OverOut+'/'+MenuImages[ImgNum]+'.gif'
}

function LaunchWindow(ref) {
	newWin = window.open(ref,'','width=500,height=450,toolbar=no,scrollbars=yes,top=100,left=100')
}

function OpenWindow(ref){
  newWin = window.open(ref,'','width=800,height=450,toolbar=no,scrollbar=auto')
}

function BillingToDelivery() {
	f = document.userdetails
	f.saddr1.value = f.baddr1.value
	f.saddr2.value = f.baddr2.value
	f.saddr3.value = f.baddr3.value

	f.scity.value = f.bcity.value
	f.scountyprovince.value = f.bcountyprovince.value
	f.spostalcode.value = f.bpostalcode.value
	f.stelephonenumber.value = f.btelephonenumber.value
}
function Trim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function checkMail(strInput) {
	var x = strInput;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x) && x.indexOf('..')==-1 && x.indexOf('.@')==-1 && x.indexOf('@.')==-1) {return true}
	else {return false}
}
function checkAN(strInput) {
	if (Trim(strInput)=='') {return true;}
	else {
		var x = strInput;
		var filter = /^[a-zA-Z0-9\-\ ]+$/
		if (filter.test(x)) {
			return true;
		}
		else {
			return false;
		}
	}
}
function checkNum(strInput) {
	if (Trim(strInput)=='') {return true;}
	else {
		var x = strInput;
		var filter = /^[0-9\ ]+$/
		if (filter.test(x)) {
			return true;
		}
		else {
			return false;
		}
	}
}
function checkit(strInput) {
	if (Trim(strInput)=='') {return false;}
	else {
		return checkAN(strInput);
	}
}

function CheckAndSubmit() {
	f = document.userdetails;
	var error;
	error = '';

	if (!checkit(f.name.value)) {error+= 'name\n'}

	if (!checkMail(f.email.value)) {error+= 'email address\n'}

	if (!checkit(f.baddr1.value)) {error+= 'billing address line 1\n'}
	if (!checkAN(f.baddr2.value)) {error+= 'billing address line 2\n'}
	if (!checkAN(f.baddr3.value)) {error+= 'billing address line 3\n'}
	if (!checkit(f.bcity.value)) {error+= 'billing city\n'}
	if (!checkit(f.bpostalcode.value)) {error+= 'billing postcode\n'}
	if (!checkit(f.bcountyprovince.value)) {error+= 'billing county\n'}
	if (!checkNum(f.btelephonenumber.value)) {error+= 'billing telephone number\n'}

	if (!checkit(f.saddr1.value)) {error+= 'delivery address line 1\n'}
	if (!checkAN(f.saddr2.value)) {error+= 'delivery address line 2\n'}
	if (!checkAN(f.saddr3.value)) {error+= 'delivery address line 3\n'}
	if (!checkit(f.scity.value)) {error+= 'delivery city\n'}
	if (!checkit(f.spostalcode.value)) {error+= 'delivery postcode\n'}
	if (!checkit(f.scountyprovince.value)) {error+= 'delivery county\n'}
	if (!checkNum(f.stelephonenumber.value)) {error+= 'delivery telephone number\n'}

	if (error=='') {
		f.submit()
	}
	else {
		alert('The following fields are incorrect:\n\n'+error+'\nPlease ensure you are using only alphanumeric characters.')
	}
}
