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 checkUserForm(bEdit)
{
	nError = 0;
	var strError='';
	
	if(!bEdit)
	{
		strError = strError + checkMandatoryField('strUsername', 'Username');
	}
	strError = strError + checkMandatoryField('strPassword', 'Password');
	strError = strError + checkMandatoryField('strForename', 'Forename');
	strError = strError + checkMandatoryField('strSurname', 'Surname');
	strError = strError + checkMandatoryField('strAddress1_1', 'Address 1');
	strError = strError + checkMandatoryField('strTown1', 'Town');
	strError = strError + checkMandatoryField('strCompany', 'Company');

	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='';
	
	if((''==document.icEditForm.strUsername.value) && (''==document.icEditForm.strEmail.value))
	{
		nError++;
		strError = "    - Either a username or email must be specified.\n" ;  
	}
	
	if(nError>0)
	{
		showErrorWindow(nError, strError);
		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('strName', 'Name');
	strError = strError + checkMandatoryField('strEnquiry', 'Enquiry');

	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 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('strForename', 'Forename');
	strError = strError + checkMandatoryField('strSurname', 'Surname');
	strError = strError + checkMandatoryField('strAddress1', 'Address 1');
	strError = strError + checkMandatoryField('strTown', 'Town');

	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;
	}
}
