var nError;

var vMonthDay= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var	vMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

function checkRegisterForm()
{
	nError = 0;
	var strError='';
	
	strError = strError + checkMandatoryField('strForename', 'Forename');
	strError = strError + checkMandatoryField('strSurname', 'Surname');
	strError = strError + checkMandatoryField('strHomeAddress1', 'Home Address');
	strError = strError + checkMandatoryField('strHomeTown', 'Home Town');
	strError = strError + checkMandatoryField('strCompany', 'Company/Team Name');
	strError = strError + checkMandatoryField('strMembers', 'Team Members');
	strError = strError + checkMandatoryField('strIdea', 'Project Idea');

	var iEmail = document.icEditForm.strEmail;
	if(!ValidMail(iEmail.value))
	{
		nError++;
		strError = strError + "    - A valid Email must be specified.\n" ;
	}

					
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkPasswordForm()
{
	nError = 0;
	var strError='';
	
	strError = strError + checkMandatoryField('strOldPassword', 'Old Password');
	strError = strError + checkMandatoryField('strNewPassword', 'New Password');
	strError = strError + checkMandatoryField('strConfirmPassword', 'Confirm Password');
	
	if(0==nError)
	{
		// Check that the new and confirm passwords are the same
		var iNew = document.getElementById('strNewPassword');
		var iConfirm = document.getElementById('strConfirmPassword');
		
		if((null!=iNew)&(null!=iConfirm))
		{
			if(iNew.value!=iConfirm.value)
			{
				strError = strError + "    - The new password must match the confirm password.";
				nError++;
			}
		}
	}

	if(nError>0)
	{
		showErrorWindow(nError, strError, 0, '');
		return false;
	}
	
	return true;
}




function checkEnquiryForm()
{
	nError = 0;
	var strError='';
	
	strError = strError + checkMandatoryField('strName', 'Name');
	strError = strError + checkMandatoryField('strCompany', 'Company Name');

	var iEmail = document.icEditForm.strEmail;
	if(''!=iEmail.value)
	{
		if(!ValidMail(iEmail.value))
		{
			nError++;
			strError = strError + "    - A valid Email must be specified.\n" ;
		}
	}

					
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkContactForm()
{
	nError = 0;
	var strError='';

	strError = strError + checkMandatoryField('strEnquiry', 'Enquiry');
	strError = strError + checkMandatoryList('strArea', 'Area', 'NON');

	var iEmail = document.icEditForm.strEmail;
	if(''!=iEmail.value)
	{
		if(!ValidMail(iEmail.value))
		{
			nError++;
			strError = strError + "    - A valid Email must be specified.\n" ;
		}
	}
	else
	{
		nError++;
		strError = strError + "    - A value must be specifed for Email.\n" ;
	}
					
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkPatientForm()
{
	nError = 0;
	var strError='';

	strError = strError + checkMandatoryField('strInitials', 'Patient Initials');
			
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkdateformat(strFormName)
{
	
	if(''!=strFormName.value)
	{
		if(!isDate(strFormName.value,'dd/MM/yyyy'))
		{
			alert("Dates must be entered in the format DD/MM/YYYY.");	
		 	strFormName.value = "";
		}
	}	
}


function checkMandatoryField(strField, strDisplay)
{
	var iField = eval("document.icEditForm." + strField);
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nError++;
			return '    - A value must be specifed for ' + strDisplay + '.\n';
		}
	}
	return '';
}

function checkMandatoryNumericField(strField, strDisplay)
{
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nError++;
			return '<LI>A value must be specifed for ' + strDisplay + '.';
		}
		
		if(!isPositiveReal(iField.value))
		{
			nError++;
			return '<LI>' + strDisplay + ' should be a positive real number.';		
		}
	}
	return '';
}



function showErrorWindow(nError, strError)
{
	if(1==nError)
	{
		alert('Please correct the following error before resubmitting.\n\n' + strError);
	}
	else
	{
		alert('Please correct these errors before resubmitting.\n\n' + strError);
	}

	//var strPage = "icErrorPopup.asp?strError=" + strError
	//var nHeight = 105 + (nError*15);
	//showPopupPage(strPage, nHeight, 340);
}

function showPopupPage(strPage, nHeight, nWidth)
{
	window.showHelp;
	var pcdialogargs = "dialogHeight:" + nHeight + "px;dialogWidth:" + nWidth + "px;status:no;center: Yes; help: No; resizable: No;";
	var dialogWindow = showModalDialog(strPage,"",pcdialogargs );
}

function checkPostCode()
{
	var iPostcode = document.getElementById("strPostcode");
	var iCounty = document.getElementById("strCounty");
	
	if(null!=iCounty)
	{
		if(iCounty.value!='OTH')
		{
			// Northern Ireland County, therefore validate for NI Postcode
			var strPostcode = iPostcode.value.toUpperCase();
			if(strPostcode.substr(0,2)!='BT')
			{	
				nError++;
				return '<LI>An NI Postcode must begin with BT';
			}
		}
	}
	
	return ''
}

function confirmDuplicate(strMessage)
{
	var bAdd = confirm(strMessage + "\n\nAre you sure you wish to continue?\n\nPress [ok] to continue or [cancel] to cancel.");
	
	if(bAdd)
	{
		// Need to set the nIgnoreDuplicates field to 1 and submit the form
		var iField = document.getElementById("nIgnoreDuplicate")
		iField.value=1;
		var iForm = document.getElementById("icEditForm")
		iForm.submit();
	}
}

function setMemoCheckField(iEl)
{
	var iField = eval("document.all." + iEl.icTextarea);
	if(null!=iField)
	{
		if(iEl.checked)
		{
			iField.style.display = '';
		}
		else
		{
			iField.style.display = 'none';
		}
	}
}

function submitListMoveForm(nPage)
{
		
	iForm = document.icListMoveForm;
	iPage = document.icListMoveForm.nPage;
	
	if((null!=iForm)&&(null!=iPage))
	{
		iPage.value = nPage;
		iForm.submit();
	}
}

function setEventStatus(nId, strGUID)
{
	iForm = document.icEventForm;
	
	if(null!=iForm)
	{
		iForm.id.value = strGUID;
		iForm.uid.value = nId;
		iForm.submit();
	}
}

function checkEntrepreneurForm(bEdit)
{
	nError = 0;
	var strError='';
	
	strError = strError + checkMandatoryField('strName', 'Name');
	strError = strError + checkMandatoryField('nEquity', 'Equity');
	strError = strError + checkMandatoryField('strAbstract', 'Abstract');
	strError = strError + checkMandatoryField('strIdea', 'Business Idea');
	
	var iEquity = document.icEditForm.nEquity.value;
	if(iEquity < 0 || iEquity > 100)
	{
		nError++;
		strError = strError + "    - A valid percentage must be specified for Equity.\n" ;
	}

	//var iEmail = document.icEditForm.strEmail;
	//if(!ValidMail(iEmail.value))
	//{
	//	nError++;
	//	strError = strError + "    - A valid Email must be specified.\n" ;
	//}
					
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkMessage(strMessage)
{
	icLoginForm.strUsername.focus();
	if ((null!=strMessage) && (''!=strMessage))
	{
		alert(strMessage);
	}
}

function clearIfFirst(iEl)
{
	if (1==iEl.ICFIRST)
	{
		iEl.value="";
		iEl.ICFIRST=0;
	}
}

/*Sean Hasson
added 26/02/03
allows only numeric keys 
Field as Param
*/
function checkNumericKey() 
{

var Key = window.event.keyCode;

if(Key==9 || Key==8){return;}

	if (Key<48 || Key>57)
	{
	
	//event.cancelBubble=true;
	event.returnValue=false;

	event.keyCode=0;

	}

}

function checkEmailPreview()
{
	nError = 0;
	var strError='';

	strError = strError + checkMandatoryField('strSubject', 'Subject');
	strError = strError + checkMandatoryField('strEmailBody', 'Email Body');
	
	var nAudience1 = document.getElementById("nAudience1");
	if(null!=nAudience1)
	{
		if(!nAudience1.checked)
		{
			var nAudience2 = document.getElementById("nAudience2");
			if(null!=nAudience2)
			{
				if(!nAudience2.checked)
				{
					strError = strError + "    - A value must be specified for Audience.";
					nError++;
				}	
			}	
		}
	}

					
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function checkMandatoryList(strField, strDisplay, strNullValue)
{
	var iField = document.getElementById(strField);
	
	if(null!=iField)
	{
		if(0==iField.options.length )
		{
			nError++;
			return '    - A value must be selected for ' + strDisplay + '.\n';
		}
		else
		{
			if(iField.value==strNullValue || iField.value=='')
			{
				nError++;
				return '    - A value must be selected for ' + strDisplay + '.\n';
			}
			
		}
		
	}
	return '';
}

function checkDiscussForm()
{
	nError = 0;
	var strError='';

	
	strError = strError + checkMandatoryField('topic_name', 'Topic');
	strError = strError + checkMandatoryField('forum_name', 'Name');
	strError = strError + checkMandatoryField('forum_desc', 'Description');
	
	var iMessageArea = document.getElementById('topic_message');
	if(null!=iMessageArea)
	{	
		if(''==iMessageArea.value)
		{
			nError++;
			strError = strError + '    - A value must be selected for Message.';		
		}
	}
	
			
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}
	return true;
}

function redirectToPVM(nId)
{
	window.location="editPVM.asp?nId=" + nId
}

function RedirectToEditVisits(strPatientId, strProjectId)
{
	var url ="editVisits.asp";
	url = url + "?nPatientId=" + strPatientId;
	url = url + "&nProjectId=" + strProjectId;
	alert( url );
	window.location= url;	
}

function setActionID( nAction, nPatientId, nProjectId )
{

	var iActionType = document.getElementById('nActionId');	
	var iPatientId = document.getElementById('nPatientId');
	var iProjectId = document.getElementById('nProjectId');
	
	if( null != iActionType )
	{
		iActionType.value = nAction;
		iPatientId.value = nPatientId;
		iProjectId.value = nProjectId;
	}
	else
	{
		return false;
	}
}


function checkVisitDetails()
{
	nError = 0;
	var strError='';
	
	var iVisitDate = document.getElementById('visit_date');
	if(null!=iVisitDate )
	{	
		if(''==iVisitDate.value)
		{
			nError++;
			strError = strError + '    - A date must be selected for the Visit Date.';		
		}
	}
	
	
	var iVisitReason = document.getElementById('visit_reason');
	if(null!=iVisitReason )
	{	
		if(''==iVisitReason.value)
		{
			nError++;
			strError = strError + '    - A reason for the visit must be given.';		
		}
		else if( iVisitReason.value.length > 150 )
		{
			nError++;
			strError = strError + '    - The visit description cannot exceed 150 characters. Please reduce the text length.';	
		}			
	}
	

	if(nError>0)
	{
		showErrorWindow(nError, strError);
		return false;
	}

	return true;
	
}