var objSeeker = new JobSeeker();
function JobSeeker ()
{	
	
	this.validateDetails = function()
	{
		$('ValidateMsg').update('');
		$('strCommsMsg').update('');
		$('strDobMsg').update('');

		if (isDefined(IS_DEV) && IS_DEV && !$(KEY_INT_ID).value && 1)
		{
			$(KEY_FIRST_NAME).value		='Test';
			$(KEY_LAST_NAME).value		='Applicatn';
			$(KEY_ADDR_1).value			='Addr1';
			$(KEY_POSTCODE).value		='5020';
			$(KEY_PHONE).value			='81000000';			
			$(KEY_SUBURB).value			='Adelaide';		
			if (!$(KEY_UNAME).value)$(KEY_UNAME).value		= 'username';
			if (!$(KEY_MOBILE).value)$(KEY_MOBILE).value	= '0423031060';
				
			$(KEY_STR_EMAIL).value				='paul@sappio.com';
			if ($(KEY_STR_PWORD))$(KEY_STR_PWORD).value				='pword';
			if ($(KEY_STR_PWORD))$('strPwordConfirm').value			='pword';
			$('intComms1').checked 				= true;
			$(KEY_GENDER).selectedIndex 		= 1;
			$(KEY_FLUENT_ENGLISH).selectedIndex = 1;
			$(KEY_DISABLED).selectedIndex = 1;		
			$(KEY_DOB).value = '01-01-1980';
			$(KEY_INDIGENOUS).selectedIndex = 2;			
		}

		boolValid 				= true;
		if (!validateText(5, 'Username', KEY_UNAME, 'strUserNameMsg'))
		{
			boolValid = false;
		}
		if ($(KEY_STR_PWORD) && !validatePassword(5, KEY_STR_PWORD, 'PwordMsg'))
		{
			boolValid = false;
		}
		else if ($(KEY_STR_PWORD) && !confirmPassword('strPwordConfirm', KEY_STR_PWORD, 'ConfMsg'))
		{
			boolReturn = false;
		}
		if (!validateText(2, 'First Name', KEY_FIRST_NAME, 'strFirstNameMsg'))
		{
			boolValid = false;
		}
		if (!validateText(2, 'Last Name', KEY_LAST_NAME, 'strLastNameMsg'))
		{
			boolValid = false;
		}
		if ($(KEY_GENDER).selectedIndex==0)
		{
			$('strGenderMsg').update('<div style="color:red;display:inline">Invalid Gender</div>');
			boolValid = false;
		}
		$(KEY_DOB).value = $(KEY_DAY).value+'-'+$(KEY_MONTH).value+'-'+$(KEY_YEAR).value;
		if (!validateText(2, 'Address Line 1', KEY_ADDR_1, 'strAddr1Msg'))
		{
			boolValid = false;
		}
		if (!validateText(2, 'Suburb', KEY_SUBURB, 'strSuburbMsg'))
		{
			boolValid = false;
		}
		if ($(KEY_POSTCODE).value.length !=4 || !isNumber($(KEY_POSTCODE), false))
		{
			$('strPostCodeMsg').update('<div style="color:red;display:inline">Invalid Postcode</div>');
			boolValid = false;
		}

		[1,2,3].each(function(intId)
		{
			strField = 'intComms'+intId;
			if ($(strField).checked)
			{
				$(KEY_COMMS).value = $(strField).value;
			}
		});
		if (!$(KEY_COMMS).value)
		{
			$('strCommsMsg').update('<div style="color:red;display:inline">You must select a communications option</div>');
			boolValid = false;
		}
		
		[KEY_GENDER, KEY_DISABLED, KEY_FLUENT_ENGLISH, KEY_INDIGENOUS].each(function(strField)
		{
			$(strField+'Msg').update('');
			if ($(strField).selectedIndex==0)
			{
				$(strField+'Msg').update('<div style="color:red;display:inline">Required</div>');
				boolValid = false;				
			}
		});
		

	
		//must have either an email address or mobile
		boolValidLogin = false;
		if (isNumber($(KEY_MOBILE), false))
		{
			if ($(KEY_MOBILE).value.length>=10 && $(KEY_MOBILE).value.length<=10)
			{
				boolValidLogin = true;
			}
		}
		if (validateEmail($(KEY_STR_EMAIL)))
		{
			boolValidLogin = true;			
		}
		
		
		if (boolValid && boolValidLogin)
		{	
			this.showQualDiv();			
		}
		else if(!boolValidLogin)
		{
			$('ValidateMsg').update('You must enter either an email address or mobile number to create your account');
		}
		
	}
	
	
	this.validateQualifications = function()
	{		
		if (isDefined(IS_DEV) && IS_DEV && !$(KEY_INT_ID) && 1)
		{
			$(KEY_VOC_ID).value = 1;
			if ($(KEY_TYPE_ID).value == VAL_GTASA_ID)
			{
				$(KEY_VOC_YEAR).value = 1;
				$(KEY_TRAIN_YEAR).value = 1;
			}
			else
			{
				$(KEY_INDIGENOUS).selectedIndex = 1;
				$(KEY_RESUME).selectedIndex = 1;
				$(KEY_VOC_TYPE_ID).selectedIndex = 1;
				$(KEY_WHITE_CARD).selectedIndex = 1;
				$(KEY_VOC_COURSE_ID).selectedIndex = 1;
			}
		}
		$(KEY_VOC_ID+'Msg').update('');	
		if ($(KEY_TYPE_ID).value != VAL_GTASA_ID)
		{
			$(KEY_RESUME+'Msg').update('');
		}
		boolValid = true;
		if($(KEY_VOC_ID).value == 0)
		{
			boolValid = false;
			$(KEY_VOC_ID+'Msg').update('<div style="color:red;display:inline">Invalid Vocation</div>');			
		}
		
		if($(KEY_TYPE_ID).value == VAL_GTASA_ID)
		{		
			if($(KEY_VOC_YEAR).value == 0)
			{
				boolValid = false;
				$(KEY_VOC_YEAR+'Msg').update('<div style="color:red;display:inline">Invalid Year</div>');		
			}	
			if($(KEY_TRAIN_YEAR).value == 0)
			{
				boolValid = false;
				$(KEY_TRAIN_YEAR+'Msg').update('<div style="color:red;display:inline">Invalid Level</div>');		
			}
		}
		else
		{
			for (intOptId=1; intOptId<=3; intOptId++)
			{
				if ($('VocType['+intOptId+']') && $('VocType['+intOptId+']').checked==true)
				{
					$(KEY_VOC_TYPE_ID).value += intOptId+',';					
				}
			}
			[KEY_RESUME, KEY_WHITE_CARD, KEY_VOC_ID].each(function(strField)
			{
				if($(strField))
				{
					$(strField+'Msg').update('');
					if ($(strField).selectedIndex==0)
					{
						$(strField+'Msg').update('<div style="color:red;display:inline">Required</div>');
						boolValid = false;				
					}
				}
			});
		}	

		if (isDefined(IS_DEV) && IS_DEV && 1)
		{
			boolValid = true;
		}
		
		if (boolValid)
		{
			this.showLocDiv();	
		}
		else
		{
			$('DetailsValidateMsg').update('Some fields are invalid');
		}
	}
	
	
	this.validateLocation = function()
	{
		strWorkRegions 	= '';
		if (isDefined(IS_DEV) && IS_DEV && !$(KEY_INT_ID) && 1)
		{
			strWorkRegions = '1';
			$(KEY_TRANSPORT).selectedIndex=1;
			$(KEY_TRAVEL_FAR).selectedIndex=1;
			$(KEY_RELOCATE).selectedIndex=2;
		}

		boolValid 				= true;
		[KEY_TRANSPORT, KEY_TRAVEL_FAR, KEY_RELOCATE].each(function(strField)
		{
			if($(strField).selectedIndex == 0)
			{
				boolValid = false;
				$(strField+'Msg').update('<div style="color:red;display:inline">Required</div>');	
			}
			else
			{
				$(strField+'Msg').update('');
			}
		});
		
		intOptId 		= 1;
		while (intOptId)
		{
			strElCheck = 'WorkRegions['+intOptId+']';
			if ($(strElCheck))
			{
				if ($(strElCheck).checked==true)
				{
					strWorkRegions += intOptId+',';
				}
				intOptId++;
			}
			else
			{
				intOptId = null;
			}
		}
		$('strWorkRegionMsg').update('');
		if (!strWorkRegions)
		{
			boolValid = false;
			$('strWorkRegionMsg').update('<div style="color:red;display:inline">At least one region must be selected</div>');				
		}
		intOptId = 1;
		strRelocateRegions	= '';
		while (intOptId)
		{
			strElCheck = 'RelocateRegions['+intOptId+']';
			if ($(strElCheck))
			{
				if ($(strElCheck).checked==true)
				{
					strRelocateRegions += intOptId+',';
				}
				intOptId++;
			}
			else
			{
				intOptId = null;
			}
		}
		$('strRelocateRegionMsg').update('');
		if ($(KEY_RELOCATE).value==1 && !strRelocateRegions.length)
		{
			boolValid = false;
			$('strRelocateRegionMsg').update('<div style="color:red;display:inline">At least one region must be selected</div>');				
		}
		$(KEY_WORK_REGION).value		= strWorkRegions;
		$(KEY_RELOCATE_REGION).value	= strRelocateRegions;


		if (boolValid)
		{	
			this.showDiscDiv();					
		}
		else
		{
			$('DetailsValidateMsg').update('Some fields are invalid');
		}
	}
	

	this.populateDetails = function(objSeeker)
	{
		$(KEY_STR_EMAIL).value				= objSeeker.strEmail;
		$(KEY_UNAME).value					= objSeeker.strUserName;
		$(KEY_FIRST_NAME).value				= objSeeker.strFirstName;
		$(KEY_LAST_NAME).value				= objSeeker.strLastName;	
		$(KEY_ADDR_1).value					= objSeeker.strAddr1;
		$(KEY_ADDR_2).value					= objSeeker.strAddr2;
		$(KEY_POSTCODE).value				= objSeeker.strPostCode;
		$(KEY_PHONE).value					= objSeeker.strPhone;
		$(KEY_MOBILE).value					= objSeeker.strMobile;
		$(KEY_SUBURB).value					= objSeeker.strSuburb;
		$(KEY_DOB).value					= objSeeker.strDob;
		$(KEY_LICENSE).value 				= objSeeker.strLicense;
		$(KEY_DISABLED).selectedIndex 		= objSeeker.boolDisabled == 1 ? 2 : 1;
		$(KEY_INDIGENOUS).selectedIndex 	= objSeeker.boolIndigenous == 1 ? 1 : 2;
		$(KEY_FLUENT_ENGLISH).selectedIndex	= objSeeker.boolFluentEnglish == 1 ? 1 : 2;
		$(KEY_GENDER).selectedIndex 		= objSeeker.strGender == 'F' ? 2 : 1;
		
		//Contact Prefs
		strCommsVar = KEY_COMMS+objSeeker.intComms;
		$(strCommsVar).checked				= true;	
		//Date of Birth conversion
		if (objSeeker.strDob.indexOf('-'))
		{
			arrDate = objSeeker.strDob.split('-');
			$(KEY_DOB).value = arrDate[0]+'-'+arrDate[1]+'-'+arrDate[2];
		}
		
		if ($(KEY_TYPE_ID).value == VAL_GTASA_ID)
		{
			//var strVocTypeIds = objSeeker.strVocTypeIds;
			if (typeof(objSeeker.strVocTypeIds) == 'number')
			{
				var arrVocTypeIds = new Array(objSeeker.strVocTypeIds);
			}
			else if (objSeeker.strVocTypeIds.include(','))
			{
				var arrVocTypeIds = objSeeker.strVocTypeIds.toArray(',');
			}
			for (var intVocTypeId in arrVocTypeIds)
			{
				strVar =KEY_VOC_TYPE_ID+'['+intVocTypeId+']';
				if ($(strVar))
					$(strVar).checked	= true;		
			}
		}
		
		var arrWorkRegionIds = objSeeker.strWorkRegionIds.split(',');
		arrWorkRegionIds.each(function(intWorkRegionId)
		{
			if ($('element_42['+intWorkRegionId+']'))$('element_42['+intWorkRegionId+']').checked	= true;			
		});
		
		var arrRelocateRegionIds = objSeeker.strRelocateRegionIds.split(',');
		arrRelocateRegionIds.each(function(intRelocateRegionId)
		{
			if ($('element_47['+intRelocateRegionId+']'))$('element_47['+intRelocateRegionId+']').checked	= true;		
		});
		
		//What year are you in? 
		if (objSeeker.intVocationYear && $('element_32'))
		{
			for(intIndex=0;intIndex<$('element_32').length;intIndex++) 
			{
			   if($('element_32').options[intIndex].value == objSeeker.intVocationYear)
			   {
				   $('element_32').selectedIndex = intIndex;
			   }
			}
		}
		
		//What level of off-the-job / trade school are you undertaking? * 
		if (objSeeker.intTrainYear && $('element_33'))
		{
			for(intIndex=0;intIndex<$('element_33').length;intIndex++) 
			{
			   if($('element_33').options[intIndex].value == objSeeker.intTrainYear)
			   {
				   $('element_33').selectedIndex = intIndex;
			   }
			}
		}
		
		if (objSeeker.intVocCourseId && $('element_62'))
		{
			for(intIndex=0;intIndex<$('element_62').length;intIndex++) 
			{
			   if($('element_62').options[intIndex].value == objSeeker.intVocCourseId)
			   {
				   $('element_62').selectedIndex = intIndex;
			   }
			}
		}
		
		if ($('element_64'))$('element_64').selectedIndex = objSeeker.boolWhiteCard ? 1 : 2;
		if ($('element_43'))$('element_43').selectedIndex = objSeeker.boolLicense ? 1 : 2;
		if ($('element_44'))$('element_44').selectedIndex = objSeeker.boolTransport ? 1 : 2;
		if ($('element_45'))$('element_45').selectedIndex = objSeeker.boolTravelFar ? 1 : 2;
		if ($('element_46'))$('element_46').selectedIndex = objSeeker.boolRelocate ? 1 : 2;
		
		
		if ($('strIndigenousField'))$('strIndigenousField').selectedIndex	= objSeeker.boolIndigenous==1 ? 1 : 2;
		if ($('strResumeField'))$('strResumeField').selectedIndex		= objSeeker.boolResume==1 ? 1 : 2;
		

		this.updateVocations($(KEY_INDUSTRY_ID).options[$(KEY_INDUSTRY_ID).selectedIndex].id, objSeeker.intVocationId);
		
		//dbe(objSeeker.strAddr1+' - '+objSeeker.strAddr2+': '+$('element_3').value );
	}
	this.submitSeeker = function()
	{
		$('ValidateMsg').update('');
		$('messages').update('');
		objCallBack = function(strResponse)
		{			
			//dbe(strResponse);
			try
			{
				objResp = strResponse.evalJSON();					
			}
			catch (ex)
			{
				$('messages').update('Json Error on Json: '+strResponse+'.\n Description: '+ex.description);
			}

			$('ValidateMsg').update(objResp.strMessage);	
			if (objResp.intStatus == 1)
			{
				$('DetailsDiv').hide();
				$('LocDiv').hide();
				$('QualDiv').hide();	

				$('DisclaimerDiv').hide();	
				$('HomeDiv').show();	
				
				if (isDefined(IS_DEV) && IS_DEV && 1)					
				{
					$('DisclaimerDiv').show();				
				}
			}
			else
			{
				$('DetailsDiv').show();
				$('LocDiv').hide();
				$('QualDiv').hide();	
				$('DisclaimerDiv').hide();	
				$('HomeDiv').hide();					
				if (isDefined(IS_DEV) && IS_DEV && 1)					
				{
					$('DisclaimerDiv').show();				
				}				
			}
		}
		objRmi = new RMICaller();
		objRmi.sendForm('SeekerSignup', 'ElWait', IMG_ICON_WAIT, IMG_BTN_TICK, IMG_BTN_CROSS, objCallBack);
	
		$('ElWait').src='images/icons/dot.jpg';
	}
	
	
	this.validate = function(arrValFieldMap)
	{		
		//iterate through the field map, add the values to corresponding hidden fields
		boolValid = true;
		for (var intIndex in arrValFieldMap)
		{
			strInputName 	= arrValFieldMap[intIndex];
			strElement 		= 'element_'+intIndex;
			if ($(strElement))
			{
				//dbe('Validating '+strElement);
				$(strElement).style.background="#ffffff";
				strErr 		= 'err_'+intIndex;
				if ($(strElement).getAttribute('boolRequired')==1 && !$(strElement).value)
				{
					boolValid = false;
					$(strElement).style.background="#FFC4C4";	
				}
			

				if (intIndex == 47 || intIndex == 42)
				{
				}
				else
				{
					//dbe('Moving '+strElement+' to '+strInputName);
					objEl 		= $(strInputName);
					objEl.value = $(strElement).value;
				}
			}
			else if (document.getElementsByName(strElement))
			{
				arrEl = document.getElementsByName(strElement);
				intElements = arrEl.length;
				strMergedValue = '';
				for (var i=0;i<intElements; i++)
				{
					elElement = arrEl[i];
					if (elElement)
					{
						if ($(elElement).type=='radio' && $(elElement).checked==true)
						{
							strMergedValue = elElement.value;
						}
						else if ($(elElement).type=='checkbox')
						{
							if ($(elElement).checked==true)
							{
								strMergedValue += (i+1)+',';
							}
						}								
					}
				}
				//dbe(strElement+': '+strMergedValue+'!!');
				objEl 		= $(strInputName);
				if (objEl)
				{
					objEl.value = strMergedValue;
					//dbe(strInputName+': '+strElement+'(Size '+intElements+') - '+objEl.value+' ...');
				}
				else
				{
					dbe('Bad '+strElement+' = '+strInputName);
				}
			}
		}
		return boolValid;
	}
		
		
	this.updateVocations = function(intIndustryId, intVocationId)
	{
		//dbe(intIndustryId+' - '+intVocationId);
		objCallBack = function(strVocations)
		{	
			$(KEY_VOC_ID).options.length = 1;
			arrVocations = strVocations.evalJSON();

			boolVocations = false;
			//dbe(arrVocations.length+'!!');
			//arrVocations.each(function(strName)
			for (var intIndex in arrVocations)
			{
				strName = arrVocations[intIndex];
				//dbe(strName+'!!');
				if (strName.length)
				{
					boolVocations = true;
					var elOptNew 	= document.createElement('option');
					elOptNew.text 	= strName;
					elOptNew.value 	= intIndex;
					try 
					{
					  $(KEY_VOC_ID).add(elOptNew, null); //standards compliant; doesn't work in IE
					}
					catch(ex) 
					{
					  $(KEY_VOC_ID).add(elOptNew); //IE only
					}
				}

			}//);
			if (!boolVocations)
			{
				$('ValidateMsg').update('No Vocations Found');
			}	
			if (intVocationId)
			{			
				for(intIndex=0;intIndex<$(KEY_VOC_ID).length;intIndex++) 
				{

				   if($(KEY_VOC_ID).options[intIndex].value == intVocationId)
				   {
					   $(KEY_VOC_ID).selectedIndex = intIndex;
				   }
				}
			}
		}	

		objRmi = new RMICaller();
		objRmi.call('JobMgr', 'loadIndustryVocations', '{"'+KEY_INDUSTRY_ID+'":'+intIndustryId+'}', objCallBack);
		
	}


	this.showDetailsDiv = function()
	{
		$('DetailsDiv').show();
		$('LocDiv').hide();
		$('QualDiv').hide();	
	}
	this.showQualDiv = function()
	{
		$('DetailsDiv').hide();
		$('LocDiv').hide();
		$('QualDiv').show();		
	}
	this.showLocDiv = function()
	{
		$('DetailsDiv').hide();
		$('LocDiv').show();
		$('QualDiv').hide();		
	}
	
	this.showEmpDiv = function()
	{
		$('DetailsDiv').hide();
		$('LocDiv').hide();
		$('QualDiv').hide();	
	}
	
	this.showDiscDiv = function()
	{
		$('DetailsDiv').hide();
		$('LocDiv').hide();
		$('QualDiv').hide();	
		$('DisclaimerDiv').show();	
	}
}