/*
'**********************************************************************'
'* Version   Date        Who  Description
'* 01.00.00  30/03/2010  NF   Initial Release
'* 01.01.00  03/06/2010  NF   Added FP_ and mailshot functions
'**********************************************************************'
*/
var g_arrDocument = document.getElementsByTagName('*');
var b_SpecialitiesRecorded = false;

String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
String.prototype.ltrim = function() {
	return this.replace(/^\s\s*/, '');
}
String.prototype.rtrim = function() {
	return this.replace(/\s\s*$/, '');
}

document.getElementsByIdPrefix = function (strIdPrefix) {
    var arrFound = new Array();
    var i, j;

    for (i = 0, j = 0; i < g_arrDocument.length; i++) {
		if (typeof g_arrDocument[i].id != 'undefined') {
	        var strCurrent = 'xx' + g_arrDocument[i].id;
	        if (strCurrent.indexOf('xx' + strIdPrefix) != -1)
	            arrFound[j++] = g_arrDocument[i];
		}
    }
    return arrFound;
}

function FP_swapImg() {//v1.0
	var doc=document ,args=arguments,elm,n;
	doc.$imgSwaps=new Array();
	for(n=2; n<args.length; n+=2) { 
		elm=FP_getObjectByID(args[n]);
		if(elm) { 
			doc.$imgSwaps[doc.$imgSwaps.length]=elm;
			elm.$src=elm.src;
			elm.src=args[n+1] 
		} 
	}
}

function FP_preloadImgs() {//v1.0
	var d=document,a=arguments;
	if(!d.FP_imgs) d.FP_imgs=new Array();

	for(var i=0; i<a.length; i++) { 
 		d.FP_imgs[i]=new Image;
 		d.FP_imgs[i].src=a[i];
	}
}

function FP_getObjectByID(id,o) {//v1.0
	var c,el,els,f,m,n;
	if(!o)o=document;
	if(o.getElementById) el=o.getElementById(id);
	else if(o.layers) c=o.layers;
	else if(o.all) el=o.all[id];
	if(el) return el;
	
	if(o.id==id || o.name==id) return o;
	if(o.childNodes) c=o.childNodes;
	if(c)
		for(n=0;n<c.length;	n++) {
			el=FP_getObjectByID(id,c[n]);
			if(el) return el;
		}
	f=o.forms;
	if(f) 
	for(n=0;n<f.length;n++) { 
		els=f[n].elements;
	
		for(m=0;m<els.length;m++){ 
			el=FP_getObjectByID(id,els[n]);
			if(el) return el;
		} 
	}
	return null;
	
}

function FP_playSound(path) {//v1.0
	var b,d=document,e,es,i,se="<EMBED SRC='"+path+"' HIDDEN=TRUE LOOP=FALSE AUTOSTART=TRUE>";
		
	if(d.body)b=d.body;
	if(d.getElementsByTagName) { 
		es=d.getElementsByTagName('embed')		
		for(i=0;i<es.length;i++) { 
			e=es(i);
			if( e.src==path ) { 
				if(e.removeNode) e.removeNode();
			break;
			} 
		} 
		if(b&&b.insertAdjacentHTML) b.insertAdjacentHTML("beforeend",se);
	}
}
function isElementVisible(objElem) {
	var blnRetVal = false;
	while (objElem.style.display != 'none' && objElem.tagName != 'BODY') {
		objElem = objElem.parentNode;
	}
	if (objElem.tagName == 'BODY' && objElem.style.display != 'none') {
		return true;
	} else {
		return false;
	}
}
function SetFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for	(i = 0; i < field.length; i++) {
			if (((field.elements[i].type == 'text') || (field.elements[i].type == 'checkbox') || (field.elements[i].type == 'textarea') || (field.elements[i].type == 'password')) && field.elements[i].disabled == false && isElementVisible(field.elements[i])) {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}
function capitalizeWords(words){
	arrWords = words.split(' ');
	for 	(i = 0; i < arrWords.length; i++) {
		arrWords[i] = capitalizeFirst(arrWords[i]);
	}
	return arrWords.join(' ');
}
function capitalizeFirst(word) {
	return word.substr(0, 1).toUpperCase() + word.substr(1);
}
function onlyNumbers(e) {
	var charCode = (e.which) ? e.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	
	return true;
}
function onlyDecimals(e) {
	var charCode = (e.which) ? e.which : event.keyCode
	if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) 
	return false;

	return true;
}
function onlyDates(e) {
	var charCode = (e.which) ? e.which : event.keyCode
	if (charCode != 47 && charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	
	return true;
}
function onlyPhoneNumbers(e) {
	var charCode = (e.which) ? e.which : event.keyCode
	if (charCode != 40 && charCode != 41 && charCode != 43 && charCode != 45 && charCode > 32 && (charCode < 48 || charCode > 57))
	return false;
	
	return true;
}
function isEmail(p_Email) {
	if (p_Email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	} else {
		return false;
	}
}

function showAgencyCerts(objAgency) {
	var colPADIRows 	= document.getElementsByIdPrefix('trPADI');
	var colProPADIRows 	= document.getElementsByIdPrefix('trProPADI');
	var colBSACRows 	= document.getElementsByIdPrefix('trBSAC');
	var colOthRows 		= document.getElementsByIdPrefix('trOther');
	var colExpRows 		= document.getElementsByIdPrefix('trExperience');
	var selAgency		= objAgency.value.toUpperCase();


	if (colProPADIRows.length > 0) {
		for (var i = 0; i < colProPADIRows.length; i++) {
			colProPADIRows[i].style.display = 'none';
		}
	}		
	if (colPADIRows.length > 0) {
		for (var i = 0; i < colPADIRows.length; i++) {
			colPADIRows[i].style.display = 'none';
		}
	}		
	if (colBSACRows.length > 0) {
		for (var i = 0; i < colBSACRows.length; i++) {
			colBSACRows[i].style.display = 'none';
		}
	}		
	if (colOthRows.length > 0) {
		for (var i = 0; i < colOthRows.length; i++) {
			colOthRows[i].style.display = 'none';
		}
	}		
	
	if (selAgency == 'NON-DIVER') {
		expStyle = 'none';
	} else {
		expStyle = '';
	}
	if (colExpRows.length > 0) {
		for (var i = 0; i < colExpRows.length; i++) {
			colExpRows[i].style.display = expStyle;
		}
	}
	
	switch (selAgency)
	{
		case 'PADI': {
			if (colPADIRows.length > 0) {
				for (var i = 0; i < colPADIRows.length; i++) {
					colPADIRows[i].style.display = '';
				}
			}		
			break;
		}
		case 'PADI PRO': {
			if (colProPADIRows.length > 0) {
				for (var i = 0; i < colProPADIRows.length; i++) {
					colProPADIRows[i].style.display = '';
				}
			}		
			break;
		}
		case 'BSAC': {
			if (colBSACRows.length > 0) {
				for (var i = 0; i < colBSACRows.length; i++) {
					colBSACRows[i].style.display = '';
				}
			}		
			break;
		}
		case 'OTHER': {
			if (colOthRows.length > 0) {
				for (var i = 0; i < colOthRows.length; i++) {
					colOthRows[i].style.display = '';
				}
			}		
			break;
		}
	}	
}
function openSpecialities() {
	objSpecWindow=showDivAsPopup('PADISpecDiv','PadiSpecialities','Padi Specialities',450,500,1);
	b_SpecialitiesRecorded = false;

	var colHeld = document.getElementsByIdPrefix('HELD-');
	var colInterested = document.getElementsByIdPrefix('INTEREST-');
	
	var objSpecHeld	= parent.document.getElementById('AppSpecHeld');
	if (objSpecHeld) {
		var arrHeldList = objSpecHeld.value.split(',');
		var specHeldList =':' + arrHeldList.join(':') + ':';
	}

	var objSpecInterest	= parent.document.getElementById('AppSpecInterest');
	if (objSpecInterest) {
		var arrInterestedList = objSpecInterest.value.split(',');
		var specInterestedList = ':' + arrInterestedList.join(':') + ':';
	}

	if (colHeld.length > 0 && specHeldList.length > 2) {
		b_SpecialitiesRecorded = true;
		for (var i = 0; i < colHeld.length; i++) {
			if (specHeldList.indexOf(':' + colHeld[i].value.toString() + ':') != -1) {
				colHeld[i].checked = true;
			}
		}
	}		
	if (colInterested.length > 0 && specInterestedList.length > 2) {
		b_SpecialitiesRecorded = true;
		for (var i = 0; i < colInterested.length; i++) {
			if (specInterestedList.indexOf(':' + colInterested[i].value.toString() + ':') != -1) {
				colInterested[i].checked = true;
			}
		}
	}

	objSpecWindow.focus();
	
	objSpecWindow.onclose = function(){
		if (!b_SpecialitiesRecorded) {
			return window.confirm('Specialities have not been recorded.\nClose window?');
		} else {
			return true;
		}
		
	}
}
function saveSpecialities() {
	var specHeldList = '';
	var specInterestedList = '';
	var colHeld = document.getElementsByIdPrefix('HELD-');
	var colInterested = document.getElementsByIdPrefix('INTEREST-');

	if (colHeld.length > 0) {
		for (var i = 0; i < colHeld.length; i++) {
			if (colHeld[i].checked) {
				if (specHeldList != '') specHeldList += ',';
				specHeldList += colHeld[i].value.toString();
			}
		}
	}		
	if (colInterested.length > 0) {
		for (var i = 0; i < colInterested.length; i++) {
			if (colInterested[i].checked) {
				if (specInterestedList != '') specInterestedList += ',';
				specInterestedList += colInterested[i].value.toString();
			}
		}
	}
	var objSpecHeld	= parent.document.getElementById('AppSpecHeld');
	if (objSpecHeld) objSpecHeld.value = specHeldList;
	
	var objSpecInterest	= parent.document.getElementById('AppSpecInterest');
	if (objSpecInterest) objSpecInterest.value = specInterestedList;
	

	b_SpecialitiesRecorded = true;
	objSpecWindow.close();
	
}
function toggleSpec(objSpec) {
	var nameElem = objSpec.name.split('-');
	var objAltSpec;
	if (objSpec.checked) {		
		if (nameElem[0] == 'HELD') {
			objAltSpec = document.getElementById('INTEREST-' + nameElem[1]);
		} else {
			objAltSpec = document.getElementById('HELD-' + nameElem[1]);
		}
		if (objAltSpec) {
			if (objAltSpec.checked) {
				objAltSpec.checked = false;
			}
		}
	}		
}

function showSpec1() {
	var specFound = false;
	var colSpecs = document.getElementsByIdPrefix('SPEC-');

	if (colSpecs.length > 0) {
		for (var objSpec in colSpecs) {
			specFound = false;
			for (var i=0; i < objSpec.length; i++) {
				if (objSpec[i].checked) {
					specFound = true;
					break;
				}
			}
			if (specFound != null) {
				alert(objSpec.name + '=' + objSpec[i].value);
			} else {
				alert(objSpec.name + ' not selected');
			}
		}
	}		
}
function actionSubmit(p_Action) {
	var objAction = document.getElementById('Action');
	if (objAction) {
		objAction.value = p_Action;
		objAction.click();
	}
}
function validateForm(objForm) {

	var objFocus = null;
	var fieldRequired = Array('AppForename','AppSurname','AppAddr1','AppPostcode','AppEmail','SelAgency');
	var fieldDescription = Array('Forename','Surname','Address','Postcode','Email','Cerifying Agency');
	// dialog message
	var alertMsg = 'Please correct the following errors:\n';
	
	var l_Msg = alertMsg.length;
	
	var newMsg = validateRequired(objForm, objFocus, fieldRequired, fieldDescription, alertMsg);
	if (alertMsg != newMsg) alertMsg = newMsg;

	
	//Validate and format DoB if present
	var objDoB = objForm.elements['AppDoB'];
	var objFormattedDoB = objForm.elements['AppFormattedDoB'];
	var objErr = document.getElementById('errAppDoB');
	var newMsg = validateDate(objFocus, objDoB, objFormattedDoB, objErr, alertMsg, 'Date of Birth');
	if (alertMsg != newMsg) alertMsg = newMsg;


	//Validate certification level if mandatory
	var objCertAgency = objForm.elements['SelAgency'];
	var objCAMandatory = document.getElementById('errSelAgency');
	if (objCertAgency && objCAMandatory) {
		bMandatory = false;
		if (objCAMandatory.innerHTML == '*') {
			bMandatory = true;
		}
		for (var j = 0; j < objCertAgency.length; j++){
			if (objCertAgency[j].checked){
				var CertAgency = objCertAgency[j].value;
				objForm.elements['AppAgency'].value = objCertAgency[j].value;
				objForm.elements['AppCertLevel'].value = '';
			}
		}
		if (CertAgency){
			switch (CertAgency.toUpperCase()) {
				case 'PADI':
					var objCertLevel = objForm.elements['PADICert'];
					var objErr = document.getElementById('errPADICert');
					break;
				case 'PADI PRO':
					var objCertLevel = objForm.elements['ProPADICert'];
					var objErr = document.getElementById('errProPADICert');
					break;
				case 'BSAC':
					var objCertLevel = objForm.elements['BSACCert'];
					var objErr = document.getElementById('errBSACCert');
					break;
				case 'OTHER':
					var objOtherAgency = objForm.elements['OtherAgency'];
					var objErr = document.getElementById('errOtherAgency');
					var objCertLevel = objForm.elements['OtherCert'];
					var objErr2 = document.getElementById('errOtherCert');
					break;
			}
		}
		if (objCertLevel) {
			if (objCertLevel.type == 'text') {
				if (!objOtherAgency || objOtherAgency.value =='' || objOtherAgency.value == null) {
					objForm.elements['AppOtherAgency'].value = '';
					if (bMandatory) {
						alertMsg += ' - Agency (Other) required\n';
						if (objFocus == null) objFocus = objOtherAgency;
						if (objErr) {
							objErr.style.color = 'red';
							objErr.style.fontWeight = 'bold';
						}
					}						
				} else {
					objForm.elements['AppOtherAgency'].value = objOtherAgency.value;
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
				if (objCertLevel.value =='' || objCertLevel.value == null) {
					objForm.elements['AppCertLevel'].value = '';
					if (bMandatory) {
						alertMsg += ' - Certification Level required\n';
						if (objFocus == null) objFocus = objCertLevel;
						if (objErr2) {
							objErr2.style.color = 'red';
							objErr2.style.fontWeight = 'bold';
						}
					}						
				} else {
					objForm.elements['AppCertLevel'].value = objCertLevel.value;
					if (objErr2) {
						objErr2.style.color = '#00FFFF';
						objErr2.style.fontWeight = 'normal';
					}
				}
			} else {
				var blnchecked = false;
				for (var j = 0; j < objCertLevel.length; j++){
					var objTest = objCertLevel[j];
					if (objCertLevel[j].checked){
						objForm.elements['AppCertLevel'].value = objCertLevel[j].value;
						blnchecked = true;
					}
				}
				if (!blnchecked){
					if (bMandatory) {					
						alertMsg += ' - Certification Level required\n';
						if (objErr) {
							objErr.style.color = 'red';
							objErr.style.fontWeight = 'bold';
						}
					}						
				} else {
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
			}
		}
	}
	
	//Validate email if entered
	var objEmail = objForm.elements['AppEmail'];
	var objErr = document.getElementById('errAppEmail');
	if (objEmail) {
		if (objEmail.value.trim() != '' && objEmail.value != null) {
			if (!isEmail(objEmail.value)) {
				alertMsg += ' - Invalid email address entered\n';
				if (objFocus == null) objFocus = objEmail;
				if (objErr) {
					objErr.style.color = 'red';
					objErr.style.fontWeight = 'bold';
				}
			} else {
				if (objErr) {
					objErr.style.color = '#00FFFF';
					objErr.style.fontWeight = 'normal';
				}
			}
		}
	}	
		
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		if (objFocus != null) 
			objFocus.focus();
		alert(alertMsg);
		return false;
	}
}
function openHelp() {
	objHelpWindow=showDivAsPopup('HelpDiv','HelpMePay','Payment Options',460,420,0);
	objHelpWindow.focus();
}
function closeHelp() {
	objHelpWindow.close();
}

function validatePayment(objForm) {
	return true;
}

function selectMemberPayment_onchange(sAction, sSelection) {
	var objAction = document.getElementById('Action');
	switch (sAction) {
		case 'SELECT_MEMBER' :
			var objSelect = document.getElementById('SelectedMember');
			break;
		case 'SELECT_INVOICE':
			objSelect = document.getElementById('SelectedInvoice');
			break;
	}
		
	if (objAction && objSelect) {
		objAction.value = sAction;
		objSelect.value = sSelection;
		
		document.MemberPayment.submit();
	}
}
function buttonUpdatePayment_onclick(sAction) {
	if (sAction == 'UPDATE_PAYMENT') {
		var alertMsg = '';
		//Validate and format Paid Date if present
		var objPaidDate = document.getElementById('PaidDate');
		var objFormattedPaidDate = document.getElementById('FormattedPaidDate');
		var objErr = document.getElementById('errPaidDate');
		if (objPaidDate && objFormattedPaidDate) {
			if (objPaidDate.value.trim() != '') {
				var validatedPaidDate = f_tcalParseDateSilent(objPaidDate.value.trim(), 'Paid Date' );
				if (validatedPaidDate == "OK") {
					objFormattedPaidDate.value = f_tcalGenerDateMySQL(objPaidDate.value.trim());
					if (objErr) {
						objErr.innerHTML = '&nbsp;';
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				} else {
					alertMsg = 'Please supply a Paid Date to update this payment.';
					objFormattedPaidDate.value = '';
					if (objErr) {
						objErr.innerHTML = '*';
						objErr.style.color = 'red';
						objErr.style.fontWeight = 'bold';
					}
				}					
			}
		} else {
				alertMsg = 'Please supply a Paid Date to update this payment.';
				objFormattedPaidDate.value = '';
				if (objErr) {
					objErr.innerHTML = '*';
					objErr.style.color = 'red';
					objErr.style.fontWeight = 'bold';
				}
		}
		if (alertMsg != '') {
			alert(alertMsg);
			return false;
		}
	}
	var objAction = document.getElementById('Action');
	if (objAction) {
		objAction.value = sAction;
		
		document.MemberPayment.submit();
	}
}

function FP_swapImg() {//v1.0
	var doc=document ,args=arguments,elm,n;
	doc.$imgSwaps=new Array();
	for(n=2; n<args.length; n+=2) { 
		elm=FP_getObjectByID(args[n]);
		if(elm) { 
			doc.$imgSwaps[doc.$imgSwaps.length]=elm;
			elm.$src=elm.src;
			elm.src=args[n+1] 
		} 
	}
}

function FP_preloadImgs() {//v1.0
	var d=document,a=arguments;
	if(!d.FP_imgs) d.FP_imgs=new Array();

	for(var i=0; i<a.length; i++) { 
 		d.FP_imgs[i]=new Image;
 		d.FP_imgs[i].src=a[i];
	}
}

function FP_getObjectByID(id,o) {//v1.0
	var c,el,els,f,m,n;
	if(!o)o=document;
	if(o.getElementById) el=o.getElementById(id);
	else if(o.layers) c=o.layers;
	else if(o.all) el=o.all[id];
	if(el) return el;
	
	if(o.id==id || o.name==id) return o;
	if(o.childNodes) c=o.childNodes;
	if(c)
		for(n=0;n<c.length;	n++) {
			el=FP_getObjectByID(id,c[n]);
			if(el) return el;
		}
	f=o.forms;
	if(f) 
	for(n=0;n<f.length;n++) { 
		els=f[n].elements;
	
		for(m=0;m<els.length;m++){ 
			el=FP_getObjectByID(id,els[n]);
			if(el) return el;
		} 
	}
	return null;
	
}

function FP_playSound(path) {//v1.0
	var b,d=document,e,es,i,se="<EMBED SRC='"+path+"' HIDDEN=TRUE LOOP=FALSE AUTOSTART=TRUE>";
		
	if(d.body)b=d.body;
	if(d.getElementsByTagName) { 
		es=d.getElementsByTagName('embed')		
		for(i=0;i<es.length;i++) { 
			e=es(i);
			if( e.src==path ) { 
				if(e.removeNode) e.removeNode();
			break;
			} 
		} 
		if(b&&b.insertAdjacentHTML) b.insertAdjacentHTML("beforeend",se);
	}
}

function select_onchange(sAction, sSelection) {
	var objAction = document.getElementById('Action');
	switch (sAction) {
		case 'SELECT_MAILSHOT' :
			var objSelect = document.getElementById('SelectedMailshot');
			break;
		case 'SELECT_NEWSITEM' :
			var objSelect = document.getElementById('SelectedNews');
			break;
		case 'SELECT_TRIP' :
			var objSelect = document.getElementById('SelectedTrip');
			break;
	}
		
	if (objAction && objSelect) {
		objAction.value = sAction;
		objSelect.value = sSelection;
		
		document.OTDCForm.submit();
	}
}
function includeAttachment_onclick(objInclude) {
	var objAttachmentList = document.getElementById('attachmentList');
	if (objAttachmentList) {
		if (objInclude.checked) {
			objAttachmentList.disabled = '';
		} else {
			objAttachmentList.disabled = 'disabled';
		}
	}
	var objAttDownload = document.getElementById('attDownload');
	if (objAttDownload ) {
		if (objInclude.checked) {
			objAttDownload .disabled = '';
		} else {
			objAttDownload .disabled = 'disabled';
		}
	}
}
function mailtoEveryone_onclick(objEveryone) {
	var objGroups = document.getElementById('mailToGroup');
	if (objGroups ) {
		if (objEveryone.checked) {
			objGroups .disabled = 'disabled';
		} else {
			objGroups .disabled = '';
		}
	}
}
function attDownload_onclick() {
	var objAttachmentList = document.getElementById('attachmentList');
	if (objAttachmentList) {
		if (objAttachmentList.value != '') {
			var dlURL = 'attdownload.php?id=' + objAttachmentList.value;
			window.open (dlURL, 'attdownload','location=1,menubar=0,status=0,scrollbars=0,width=100,height=100');
		} 
	}
}

function mailToGroup_onchange(sSelection) {
	var objMemberList = document.getElementById('mailtoMemberList');
	var objSpecialityList = document.getElementById('targetSpeciality');
	if (objMemberList) {
		if (sSelection == 1) {
			objMemberList.disabled = '';
		} else {
			objMemberList.disabled = 'disabled';
		}
	}
	if (objSpecialityList) {
		if (sSelection == 4 || sSelection == 5) {
			objSpecialityList.disabled = '';
		} else {
			objSpecialityList.disabled = 'disabled';
		}
	}
}

function validateNews(objForm) {
	// dialog message
	alertMsg = 'Please correct the following errors:\n';
	
	var l_Msg = alertMsg.length;
	
	var objFocus = null;
	var fieldRequired = Array('newsHeading','newsItem');
	var fieldDescription = Array('Heading','News Item Text');

	var newMsg = validateRequired(objForm, objFocus, fieldRequired, fieldDescription, alertMsg);
	if (alertMsg != newMsg) alertMsg = newMsg;
	
	//Validate and format Dates On and Off if present
	var objDateOn = objForm.elements['dateOn'];
	var objFormattedDateOn = objForm.elements['formattedDateOn'];
	var objErrOn = document.getElementById('errDateOn');
	var newMsg = validateDate(objFocus, objDateOn, objFormattedDateOn, objErrOn, alertMsg, 'Date On');
	if (alertMsg != newMsg) alertMsg = newMsg;

	var objDateOff = objForm.elements['dateOff'];
	var objFormattedDateOff = objForm.elements['formattedDateOff'];
	var objErrOff = document.getElementById('errDateOff');
	var newMsg = validateDate(objFocus, objDateOff, objFormattedDateOff, objErrOff, alertMsg, 'Date Off');
	if (alertMsg != newMsg) alertMsg = newMsg;

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		if (objFocus != null) 
			objFocus.focus();
		alert(alertMsg);
		return false;
	}
}

function validateTrip(objForm) {
	// dialog message
	alertMsg = 'Please correct the following errors:\n';
	
	var l_Msg = alertMsg.length;
	
	var objFocus = null;
	var fieldRequired = Array('startDate', 'tripDates','tripLocation','tripExperience');
	var fieldDescription = Array('Heading','News Item Text');

	var newMsg = validateRequired(objForm, objFocus, fieldRequired, fieldDescription, alertMsg);
	if (alertMsg != newMsg) alertMsg = newMsg;
	
	//Validate and format Dates On and Off if present
	var objDate = objForm.elements['startDate'];
	var objFormattedStartDate = objForm.elements['formattedStartDate'];
	var objErr = document.getElementById('errstartDate');
	var newMsg = validateDate(objFocus, objDate, objFormattedStartDate, objErr, alertMsg, 'Start Date');
	if (alertMsg != newMsg) alertMsg = newMsg;
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		if (objFocus != null) 
			objFocus.focus();
		alert(alertMsg);
		return false;
	}
}

function validateRequired(objForm, objFocus, fieldRequired, fieldDescription, alertMsg) {
	for (var i = 0; i < fieldRequired.length; i++){
		var objElem = objForm.elements[fieldRequired[i]];
		var objErr = document.getElementById('err' + fieldRequired[i]);
		if (objElem){
			switch(objElem.type){
			case 'select-one':
				if (objElem.selectedIndex == -1 || objElem.options[objElem.selectedIndex].text == ''){
					alertMsg += ' - ' + fieldDescription[i] + ' required\n';
					if (objFocus == null) objFocus = objElem;
					if (objErr) {
						objErr.style.color = 'red';
						objErr.style.fontWeight = 'bold';
					}
				} else {
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
				break;
			case 'select-multiple':
				if (objElem.selectedIndex == -1){
					alertMsg += ' - ' + fieldDescription[i] + ' required\n';
					if (objErr) {
						objErr.style.color = 'red';
						objErr.style.fontWeight = 'bold';
					}
				} else {
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
				break;
			case 'text':
			case 'textarea':
				if (objElem.value == '' || objElem.value == null){
					alertMsg += ' - ' + fieldDescription[i] + ' required\n';
					if (objFocus == null) objFocus = objElem;
	
					if (objErr) {
						objErr.style.color = 'red';
						objErr.style.fontWeight = 'bold';
					}
				} else {
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
				break;
			default:
			}
			if (objElem.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < objElem.length; j++){
					if (objElem[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += ' - ' + fieldDescription[i] + ' required\n';
					if (objErr) {
						objErr.style.color = 'red';
						objErr.style.fontWeight = 'bold';
					}
						
				} else {
					if (objErr) {
						objErr.style.color = '#00FFFF';
						objErr.style.fontWeight = 'normal';
					}
				}
			}
		}
	}
return alertMsg;
}
 
function validateDate(objFocus, objDate, objFormattedDate, objErr, alertMsg, strText) {
	if (objDate && objFormattedDate) {
		if (objDate.value.trim() != '') {
			var validatedDate = f_tcalParseDateSilent(objDate.value.trim(), strText );
			if (validatedDate == "OK") {
				objFormattedDate.value = f_tcalGenerDateMySQL(objDate.value.trim());
				if (objErr) {
					objErr.style.color = '#00FFFF';
					objErr.style.fontWeight = 'normal';
				}
			} else {
				objFormattedDate.value = '';
				alertMsg += ' - ' + validatedDate;
				if (objErr) {
					objErr.style.color = 'red';
					objErr.style.fontWeight = 'bold';
				}
			}					
		}
	}
	return alertMsg;
}
