var ErrorMSG = "";
var WindowHeight = 0;
var icnt = 0;
var FocusField = "";
var ArraySize = 18;
var IgnoreAddress = false;

var FieldList = new Array (ArraySize)
var ErrorList = new Array (ArraySize)

FieldList[icnt] = "EmailAddress";					ErrorList[icnt] = "Email Address";		icnt = icnt + 1; // 0

FieldList[icnt] = "Title";				    		ErrorList[icnt] = "Title";				icnt = icnt + 1; // 1
FieldList[icnt] = "FirstName";						ErrorList[icnt] = "First Name";			icnt = icnt + 1; // 2
FieldList[icnt] = "Surname";						ErrorList[icnt] = "Last Name";			icnt = icnt + 1; // 3
FieldList[icnt] = "KnownAs";						ErrorList[icnt] = "Known As";			icnt = icnt + 1; // 4
FieldList[icnt] = "Address1";						ErrorList[icnt] = "Address 1";			icnt = icnt + 1; // 5
FieldList[icnt] = "Address2";						ErrorList[icnt] = "Address 2";		icnt = icnt + 1; // 6
FieldList[icnt] = "Address3";						ErrorList[icnt] = "Address 3";		icnt = icnt + 1; // 7
FieldList[icnt] = "Town";				    		ErrorList[icnt] = "Town";		icnt = icnt + 1; // 8
FieldList[icnt] = "County";                 		ErrorList[icnt] = "County"; icnt = icnt + 1; // 9
FieldList[icnt] = "Postcode";						ErrorList[icnt] = "Postcode";		icnt = icnt + 1; // 10
FieldList[icnt] = "Country";						ErrorList[icnt] = "Country";		icnt = icnt + 1; // 11

FieldList[icnt] = "HomeTel";						ErrorList[icnt] = "Telephone Number";	icnt = icnt + 1; // 12
FieldList[icnt] = "MobileTel";						ErrorList[icnt] = "Mobile Number";	icnt = icnt + 1; // 13

FieldList[icnt] = "CandidateType";					ErrorList[icnt] = "Candidate Type";	icnt = icnt + 1; // 14
FieldList[icnt] = "SalesExperience";        		ErrorList[icnt] = "Sales Experience"; icnt = icnt + 1; // 15
FieldList[icnt] = "LastPosition";           		ErrorList[icnt] = "Last Pharma Position"; icnt = icnt + 1; // 16

FieldList[icnt] = "ABPIQualified"; 					ErrorList[icnt] = "ABPI Qualified"; icnt = icnt + 1; // 17
FieldList[icnt] = "ABPIDatePassed"; 				ErrorList[icnt] = "ABPI Date Passed"; icnt = icnt + 1; // 18

FieldList[icnt] = "PassportType";           		ErrorList[icnt] = "Passport Type"; icnt = icnt + 1; // 19

FieldList[icnt] = "HasDrivingLicence"; 				ErrorList[icnt] = "Driving Licence"; icnt = icnt + 1; // 20
FieldList[icnt] = "DrivingLicenceType"; 			ErrorList[icnt] = "Licence Type"; icnt = icnt + 1; // 21
FieldList[icnt] = "CountryOfLicence"; 				ErrorList[icnt] = "Country Of Licence"; icnt = icnt + 1; // 22
FieldList[icnt] = "EverDisqualifiedFromDriving"; 	ErrorList[icnt] = "Ever Disqualified From Driving"; icnt = icnt + 1; // 23
FieldList[icnt] = "AccidentWithin2Years"; 			ErrorList[icnt] = "Accident Within 2 Years"; icnt = icnt + 1; // 24
FieldList[icnt] = "PointsOnLicence"; 				ErrorList[icnt] = "Points On Licence"; icnt = icnt + 1; // 25
FieldList[icnt] = "MileageOver10000"; 				ErrorList[icnt] = "Mileage Over 10000"; icnt = icnt + 1; // 26

FieldList[icnt] = "FileUpload";             		ErrorList[icnt] = "File Upload"; icnt = icnt + 1; // 27


function CheckDate(myDate)
{
    var date_array = myDate.split('-');
    var strDay = date_array[0];

    // Attention! Javascript consider months in the range 0 - 11
    var strMonth = date_array[1] - 1;
    var strYear = date_array[2];

    var day = parseInt(strDay);
    var month = parseInt(strMonth);
    var year = parseInt(strYear);
    
    var dt = new Date(year, month, day, 0, 0, 0, 0);
   
    if (dt.getDate() != day) {
        return (false);
    }
    else if (dt.getMonth() != month) {
        //this is for the purpose JavaScript starts the month from 0
        return (false);
    }
    else if (dt.getFullYear() != year) {
        return (false);
    }
    return true;
}

function VerifyRegistration()
{
	// *** FormCheck only works on <input> tags, does not work with <select> ***

	document.getElementById("message1").innerHTML  = "";

	ErrorMSG = "";
	WindowHeight = 0;
	FocusField = "";

    // check email address
	FormCheck(0, 0, 'frmRegistration1');

	// check firstname surname
	FormCheck(2, 3, 'frmRegistration1');
	
	// check address 1
	FormCheck(5, 5, 'frmRegistration1');
	
	// check town, county, postcode, country
	FormCheck(8, 11, 'frmRegistration1');

	// check file upload
	FormCheck(27, 27, 'frmRegistration1');
	
    // check ABPI Pass date
	if (document.frmRegistration1.ABPIQualified.options[document.frmRegistration1.ABPIQualified.selectedIndex].value == 1) {
	    FormCheck(18, 18, 'frmRegistration1');
	}

	if (document.frmRegistration1.ABPIDatePassed.value != "")
	{
		if (CheckDate(document.frmRegistration1.ABPIDatePassed.value) == true)
		{
			document.getElementById("LabelABPIDatePassed").className = "labelnormal";			
		}
		else
		{
			ErrorMSG = ErrorMSG + "\nABPI Date Passed is invalid";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelABPIDatePassed").className = "labelinvalid";		
		}
	}
	
	if (document.frmRegistration1.CandidateType.options[document.frmRegistration1.CandidateType.selectedIndex].value == -1) {

	    ErrorMSG = ErrorMSG + "\nPlease select your Candidate Type";
	    WindowHeight = WindowHeight + 20;
	    document.getElementById("LabelCandidateType").className = "labelinvalid";
	}
	else {
	    document.getElementById("LabelCandidateType").className = "labelnormal";
	}

    // check Sales Experiance
	if (document.frmRegistration1.SalesExperience.options[document.frmRegistration1.SalesExperience.selectedIndex].value == -1) {
	    ErrorMSG = ErrorMSG + "\nPlease select your Sales Experience";
	    WindowHeight = WindowHeight + 20;
	    document.getElementById("LabelSalesExperience").className = "labelinvalid";
	}
	else {
	    document.getElementById("LabelSalesExperience").className = "labelnormal";
	}
	
	// check Last Position
	if (document.frmRegistration1.LastPosition.options[document.frmRegistration1.LastPosition.selectedIndex].value == -1) {
	    ErrorMSG = ErrorMSG + "\nPlease select your Last Pharma Position";
	    WindowHeight = WindowHeight + 20;
	    document.getElementById("LabelLastPosition").className = "labelinvalid";
	}
	else {
	    document.getElementById("LabelLastPosition").className = "labelnormal";
	}

	// check ABPI
	if (document.frmRegistration1.ABPIQualified.options[document.frmRegistration1.ABPIQualified.selectedIndex].value == -1) {

	    ErrorMSG = ErrorMSG + "\nAre you ABPI Qualified";
	    WindowHeight = WindowHeight + 20;
	    document.getElementById("LabelABPIQualified").className = "labelinvalid";
	}
	else {
	    document.getElementById("LabelABPIQualified").className = "labelnormal";
	}

	// check Driving Licence
	if (document.frmRegistration1.HasDrivingLicence.options[document.frmRegistration1.HasDrivingLicence.selectedIndex].value == -1) {

	    ErrorMSG = ErrorMSG + "\nDo you have a Driving Licence";
	    WindowHeight = WindowHeight + 20;
	    document.getElementById("LabelDrivingLicence").className = "labelinvalid";
	}
	else {
	    document.getElementById("LabelDrivingLicence").className = "labelnormal";
	}

	// check Driving Licence Details
	if (document.frmRegistration1.HasDrivingLicence.options[document.frmRegistration1.HasDrivingLicence.selectedIndex].value == 1) {
		// licence type
		if (document.frmRegistration1.DrivingLicenceType.options[document.frmRegistration1.DrivingLicenceType.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select your Licence Type";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelLicenceType").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelLicenceType").className = "labelnormal";
		}
		
		// country of licence
		if (document.frmRegistration1.CountryOfLicence.options[document.frmRegistration1.CountryOfLicence.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select Country Of Licence";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelCountryOfLicence").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelCountryOfLicence").className = "labelnormal";
		}
		
		// ever disqualified from driving
		if (document.frmRegistration1.EverDisqualifiedFromDriving.options[document.frmRegistration1.EverDisqualifiedFromDriving.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select Ever Disqualified From Driving";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelEverDisqualifiedFromDriving").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelEverDisqualifiedFromDriving").className = "labelnormal";
		}
		
		// Accident within 2 years
		if (document.frmRegistration1.AccidentWithin2Years.options[document.frmRegistration1.AccidentWithin2Years.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select Accident within 2 years";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelAccidentWithin2Years").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelAccidentWithin2Years").className = "labelnormal";
		}
		
		// Points on Licence
		if (document.frmRegistration1.PointsOnLicence.options[document.frmRegistration1.PointsOnLicence.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select Points on Licence";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelPointsOnLicence").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelPointsOnLicence").className = "labelnormal";
		}
		
		// Mileage over 10,000PA
		if (document.frmRegistration1.MileageOver10000.options[document.frmRegistration1.MileageOver10000.selectedIndex].value == -1) {

			ErrorMSG = ErrorMSG + "\nPlease select Mileage Over 10,000";
			WindowHeight = WindowHeight + 20;
			document.getElementById("LabelMileageOver10000").className = "labelinvalid";
		}
		else {
			document.getElementById("LabelMileageOver10000").className = "labelnormal";
		}
		
	}
	
	
	if (ErrorMSG == "")
	{

		return true;
	}
	else
	{
		DisplayError(WindowHeight, "Please complete the following fields:" + ErrorMSG );
		if (FocusField != "")
		{
			eval("frmRegistration1."+FocusField+".focus()");
		}
		return false;
	}
}





function FormCheck(varStartPointer, varEndPointer, varForm)
{
	

	for (ArrayPointer = varStartPointer; ArrayPointer <= varEndPointer; ArrayPointer++)
	{
	    //alert(FieldList[ArrayPointer])
		if (eval(varForm + "." + FieldList[ArrayPointer] + ".value == ''") || eval(varForm + "." + FieldList[ArrayPointer] + ".value == ' '"))
		{
			ErrorMSG = ErrorMSG + "\n" + ErrorList[ArrayPointer]
			WindowHeight = WindowHeight + 20
			if (FocusField == "")
			{
				FocusField = FieldList[ArrayPointer]
				//varForm + "." + FieldList[ArrayPointer] + ".className = 'invalid'";

			}

	        switch (FieldList[ArrayPointer])
					{
					case "PassportDay":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelPassport").className = "labelinvalid";
					    break;
					case "PassportMonth":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelPassport").className = "labelinvalid";
					    break;
					case "PassportYear":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelPassport").className = "labelinvalid";
					    break;
					case "BirthDay":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelDateOfBirth").className = "labelinvalid";
					    break;
					case "BirthMonth":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelDateOfBirth").className = "labelinvalid";
					    break;
					case "BirthYear":

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("LabelDateOfBirth").className = "labelinvalid";
					    break;

	                default:

			            document.getElementById(FieldList[ArrayPointer]).className = "invalid";
			            document.getElementById("Label" + FieldList[ArrayPointer]).className = "labelinvalid";
			        }
		} else {

				switch (FieldList[ArrayPointer])
					{
					case "PassportDay":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelPassport").className = "labelnormal";
					    break;
					case "PassportMonth":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelPassport").className = "labelnormal";
					    break;
					case "PassportYear":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelPassport").className = "labelnormal";
					    break;
					case "BirthDay":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelDateOfBirth").className = "labelnormal";
					    break;
					case "BirthMonth":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelDateOfBirth").className = "labelnormal";
					    break;
					case "BirthYear":
			            document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("LabelDateOfBirth").className = "labelnormal";
					    break;

        	        default:
		                document.getElementById(FieldList[ArrayPointer]).className = "normal";
			            document.getElementById("Label" + FieldList[ArrayPointer]).className = "labelnormal";
                    }
		}
	}
}

function FormCheckDefault(varStartPointer, varEndPointer, varForm)
{
	for (ArrayPointer = varStartPointer; ArrayPointer <= varEndPointer; ArrayPointer++)
	{
	    //alert(FieldList[ArrayPointer])
		if (eval(varForm + "." + FieldList[ArrayPointer] + ".value == ''") || eval(varForm + "." + FieldList[ArrayPointer] + ".value == ' '"))
		{
			ErrorMSG = ErrorMSG + "<BR>" + ErrorList[ArrayPointer]
			WindowHeight = WindowHeight + 20
			if (FocusField == "")
			{
				FocusField = FieldList[ArrayPointer]
				//varForm + "." + FieldList[ArrayPointer] + ".className = 'invalid'";

			}

        }
     }
}





function DisplayError(varHeight, varMsg) // Displays an error message. Height and message are passed in
{

	var TempHeight = 70
	TempHeight=TempHeight+varHeight
	if (TempHeight < 100)
	{
		TempHeight = 100
	}


if (1==2) {
	self.focus();
	document.getElementById("jsNotice").innerHTML  = "<table bgcolor=#ffffff class=standardAlert width=300><tr height='+TempHeight+'><td align=middle>" + varMsg + "</td></tr><tr><td align=right><a  class=menulinks2 href='javascript:hidejsNotice();'>CLOSE</a></td></tr></table>";
	document.getElementById("jsNotice").style.display = '';
	setTimeout(function() { document.getElementById("jsNotice").style.display = "none"; }, 16*60*1000); // 16 min (20x60 second delay
} else
{
	//document.getElementById("message1").innerHTML  = "<table bgcolor='#ffffff' class=standardiMessage width='50%' align='center'><tr height='+TempHeight+'><td align=middle>" + varMsg + "</td></tr><tr><td align=right><a  class=menulinks2 href='javascript:hidejsNotice();'><small>CLOSE</small></a></td></tr></table>";
    alert(varMsg);


}


	//window.open('Error.asp?Height='+TempHeight+'&ErrorMSG='+varMsg,'Error','width=400, height='+TempHeight+',location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no, left='+(window.screen.width-400)/2+', top='+(window.screen.height-TempHeight)/2)
}


function DisplayMessageError()
{

	self.focus();
	document.getElementById("jsalert").innerHTML  = "<table bgcolor=#E3E4E5 class=standardAlert width=400><tr height=140><td align=middle>" + message + "</td></tr><tr><td align=right><a  class=menulinks2 href='javascript:hidejsalert();'>CLOSE</a></td></tr></table>";
	document.getElementById("jsalert").style.display = '';
	setTimeout(function() { document.getElementById("jsalert").style.display = "none"; }, 16*60*1000); // 16 min (20x60 second delay
}


function DisplayMessageError(varHeight, varMsg) // Displays an error message. Height and message are passed in
{

	var TempHeight = 70
	TempHeight=TempHeight+varHeight
	if (TempHeight < 100)
	{
		TempHeight = 100
	}



	document.getElementById("message1").innerHTML  = "<table bgcolor='#ffffff' class=standardiMessage width='50%' align='center'><tr height='+TempHeight+'><td align=middle>" + varMsg + "</td></tr><tr><td align=right><a  class=menulinks2 href='javascript:hidejsNotice();'><small>CLOSE</small></a></td></tr></table>";


}



function hidejsNotice()
{
    if (1==2) {
	    document.getElementById("jsNotice").style.display = "none";
    } else
    {
	    document.getElementById("message1").innerHTML  = "";
    }
}
function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;

}



function CheckEmail(varFormName, varFieldName)
{
	ok=true;
	eval("mail=" + varFormName + "." + varFieldName + ".value")
	if(mail!='')
	{
		at_pos=mail.indexOf("@");
		dot_pos=mail.indexOf(".");
		if(at_pos<1 || dot_pos<1)
		{
			ok=false;
		}
		else
		{
			condition="yes";
			var at_count=0;
			var dot_count=0;
			var pos=0;
			var temp=0;
			for(var i=0;i<mail.length;i++)
			{
				if((mail.charCodeAt(i)>0 && mail.charCodeAt(i)<48)||(mail.charCodeAt(i)>57 && mail.charCodeAt(i)<65)||(mail.charCodeAt(i)>91 && mail.charCodeAt(i)<97)||mail.charCodeAt(i)>122)
				{
					if(mail.charAt(i)=="@"||mail.charAt(i)==".")
					{
						if(mail.charAt(i)=="@"){at_count++}else{dot_count++}
							if(dot_count>=1)
							{
								dot_pos=i;
								if((dot_pos>at_pos) && temp==0)
								{
									pos=dot_pos-at_pos;
									temp++;
								}
							}
					}
					else
					{
						if((mail.charAt(i)=="_")||(mail.charAt(i)=="-"))
						{
							condition="yes";
						}
						else
						{
							condition="no";
							i=mail.length;
						}
					}
				}
			}
			if(condition=="no")
			{
				ok=false;
			}
			else
			{
				if(at_count>1)
				{
					ok=false;
				}
					else
				{
					if(pos<2)
					{
						ok=false;
						i=mail.length;
					}
					else
					{
						count=dot_pos+1;
						domain="";
						for(count;count<mail.length;count++)
						{
							domain=domain+mail.charAt(count);
						}
						dom=new Array("au","com","net","org","edu","in","mil","gov","arpa","biz","aero","name","coop","info","pro","museum");
						error="yes";
						for(var k=0;k<dom.length;k++)
						{
							if(domain==dom[k])
							{
								k=dom.length;
								error="no";
							}
						}
						if((error=="yes" && (domain.length>2)) || (domain.length<2))
						{
							ok=false;
						}
						}
				}
			}
		}
	}
	if (ok)
	{
		return true;
	}
	else
	{
		eval(varFormName + "." + varFieldName + ".focus()");
		DisplayError(120, "Please supply a valid email address.<BR>Thank You.");
		return false;
	}
}

function LimitText(varFormName, varFieldName, varFieldSize) // A form name, object name and size restriction is passed to compare string length
{
	if (eval(varFormName+"."+varFieldName+".value.length") > varFieldSize)
	{
		DisplayError(20, "This field can only contain "+varFieldSize+" characters.");
		return false;
	}
	return true;
}

function fnExpandTextArea(text_area,minSize)
{
	if (text_area.scrollHeight > minSize)
	{
		text_area.style.height = 0;
		text_area.style.height = text_area.scrollHeight + 20 + "px";

	}

}
function NumberValidator(varLocation)
{

	var varString = eval(varLocation + ".value");
	if (varString != "")
	{

		if (isNaN(varString))
		{
			eval(varLocation + ".value = '" + varString.substring(0, varString.length - 1) + "'")
			DisplayError(20, "Sorry, but only numbers are allowed in this field")
		}
	}
}

function fnw3CheckBoxFix(fromThis,ToThis)
{

	if (fromThis.checked == true)
	{	
		ToThis.value = fromThis.value;
	} else
	{	
		ToThis.value = "";
	}
}


