//--- Constants ---

//var mcWhatever = 0;

//--- Procedures ---

//Validation
//Login
//Save
//Dates
//File
//Images
//New Records
//Photos
//Ticker

//Validation.

function mValidate (sEntity, iID) {
	switch (sEntity) {
		case 'Company':
			if (!gIsPopulated('txtLegalName', 'Legal Name')) {return false;}
			if (!gIsDate('txtCallBackDate', 'Call Back Date')) {return false;}
			if (!gIsNumeric('txtAmount', 'Value', false, true)) {return false;}
			if (!gIsNumeric('txtContractMonths', 'Contract Months', true, false)) {return false;}
			if (iID == 0) {if (!mValidate('Person')) {return false;}} else {if (!mValidateList('Person')) {return false;}}
			break;
		default: break;
	}
	
	return true;
}

function mValidateList(sEntity) {
	var iID;
	var iCount = eval('frmForm.hid' + sEntity + 'Count').value;

	for (var i=1; i<=iCount; i++) {
		iID = eval('frmForm.hid' + sEntity + 'ID' + i).value;
		if (eval('frmForm.hidIsDirty' + sEntity + iID).value != '') {
			switch (sEntity) {
				case 'Person': if (!gIsPopulated('txtFirstName' + iID, 'First Name')) {return false;}; break;
				default: break;
			}
		}
	}
		
	for (var i=1; i<=mcNewItems; i++) {
		iID = 'New' + i;
		if (eval('frmForm.hidIsDirty' + sEntity + iID).value != '') {
			switch (sEntity) {
				case 'Person': if (!gIsPopulated('txtFirstName' + iID, 'First Name')) {return false;} break;
				default: break;
			}
		}
	}
			
	return true;
}

//Login.

function mLogIn() {
	if (!gIsSelected('cboPerson', 'Name')) {return false;}
	if (!gIsPopulated('txtPassword', 'Password')) {return false;}
	gSubmit('LogIn');
}

//Save.

function mSave(sEntity) {
	var iID = eval('frmForm.hid' + sEntity + 'ID').value;
	if (!mValidate(sEntity, iID)) {return false;}
	gSubmit('Save');
}

//Dates.

function mDateClick(sTextBox, sIsDirty) {
	frmCalendar.select(eval('document.frmForm.' + sTextBox), sTextBox, 'dd/MM/yyyy');
	if (sIsDirty != '') {gIsDirty(sIsDirty)}

//	function z_mDateClick(s) {
//		frmCalendar.select(eval('document.frmForm.' + s), s, 'dd/MM/yyyy');
//	}
}

//File.

function uploadValidate() {
	//Copy the file name to the text box for the audit's scanned file.
	//if (confirm('Do you want to make this the scanned file for this person\'s most recent (or new) audit?\n\r\n(Cancel if you just want to upload the file)')) {
		if (!uploadChange()) {return false;}
	//}	
	
	//Upload the file.
	//if (confirm('Upload this file?\n\r\n\(Cancel if the file is already on the server)')) {frmUpload.submit();}
	frmUpload.submit();
	
	//Save the person's details.
	/*
	var s;
	if (frmForm.hidPersonID.value == 0) {s = 'add';} else {s = 'change the details for';}
	if (confirm('Would you like to ' + s + ' this person?')) {
		if (personValidate()) {
			frmForm.hidPageMode.value = 'PersonSave';
			frmForm.submit();
		}
	}
	*/
}

function uploadChange() {
	var iAuditID = -1;
	var sFile;

	//Get the ID of the topmost audit being displayed (either a new audit or the most recent existing audit).
	if (divAudit0.style.display == '') {
		iAuditID = 0;
	}
	else {
		if (frmForm.hidAudits.value == 0) {
			alert('This person currently has no audits. Please click \'Add New Audit\'.');
			return false;
		}
		else {
			iAuditID = frmForm.hidAuditID1.value;
		}
	}
	
	sFile = frmUpload.filUpload.value;
	
	browseChange(iAuditID, sFile);
	
	return true;
}

function browseChange(iAuditID, sFile) {
	var iSlash = sFile.lastIndexOf('\\');
	if (iSlash > -1) {sFile = sFile.substring(iSlash + 1, sFile.length);}

	eval('frmForm.txtScan' + iAuditID).value = sFile;
	auditIsDirty(iAuditID);
}

//Images.

if (document.images)
{
  pic1 = new Image(840,427); pic1.src = 'img/map1.gif'; 
  pic2 = new Image(840,427); pic2.src = 'img/map2.gif'; 
}
function mMap(i) {
	tbl.style.backgroundImage = 'url(img/map' + i + '.gif)';
}

//New Records.

function mAdd(sEntity) {
	//Spring.
	var lnk = eval('lnkAdd' + sEntity);
	var bShow = (lnk.innerHTML != 'Cancel');
	if (bShow) {lnk.innerHTML = 'Cancel'; eval('frmForm.hidAdd' + sEntity).value = 1} else {lnk.innerHTML = 'Add More...'; eval('frmForm.hidAdd' + sEntity).value = ''}
	
	for (var i=1; i<=mcNewItems; i++) {
		gShowElement('tr' + sEntity + i, bShow);
	}

/*	City Index.
	var lnk = eval('lnk' + s);
	var div = eval('div' + s);

	if (lnk.innerHTML == 'Cancel') {
		lnk.innerHTML = 'Add more...';
		div.style.display = 'none';
	} else {
		lnk.innerHTML = 'Cancel';
		div.style.display = '';
	}*/
}

//Photos.

function mPersonPhoto(sDirection, sPerson) {
	try {
		var iPhotos = document.frmForm.hidPhotos.value;
		
		if (sDirection == 'next') {
			if (document.frmForm.hidPhoto.value != iPhotos) {
				document.frmForm.hidPhoto.value = parseInt(document.frmForm.hidPhoto.value) + 1;
			} else {
				document.frmForm.hidPhoto.value = 1;
			}
		} else {
			if (document.frmForm.hidPhoto.value != 1) {
				document.frmForm.hidPhoto.value = parseInt(document.frmForm.hidPhoto.value) - 1;
			} else {
				document.frmForm.hidPhoto.value = iPhotos;
			}
		}
		
		document.imgPhoto.src = 'img/photo/' + sPerson + '_' + document.frmForm.hidPhoto.value + '.jpg';
	}
	catch (err) {alert("Error: move\n" + err.description);return false;}
}

//Ticker.

var iTicker;
var sTicker;
var action;	
function mTickerStart() {
	iTicker = 0;
	sTicker = 'welcome to the preds website. not quite finished ... but still ace.';
	action = window.setInterval("mTicker()",20);
}
function mTicker() {
	iTicker++;
	if (iTicker <= sTicker.length) {if (iTicker < sTicker.length - 1) {tdWelcome.innerHTML = sTicker.substr(0, iTicker) + '_'} else {tdWelcome.innerHTML = sTicker.substr(0, iTicker)}};
	if (iTicker == sTicker.length) {window.clearInterval(action)};
}