function getFocus(candregistrationform){
	//When the page loads focus on first name field
	document.candregistrationform[getLocalValue('CAND_EMAIL')].focus();
}

// Post the form to an iframe (to avoid refresh) to parse the resume
function parseResume() {	
	// check to make sure a resume has been selected
	var fldupload = getFormElement('candregistrationform', 'UPLOAD');
	if ( fldupload.value == '' ) {
		alert(getLocalValue('MSG_CAND_REG_NORESUMESELECTED'));
		return;
	}
	
	// disable the autopopulate button
	showStatus(true, getLocalValue('MSG_CAND_REG_WAITMSG'));
	var autopopbtn = getFormElement('candregistrationform', 'autopopulate');
	autopopbtn.disabled = true;
	
	// post the form to the iframe to upload the file to the parser
	var formname = 'candregistrationform';
	var formobj = document.getElementById(formname);
	formobj.encoding = "multipart/form-data";
	formobj.method = "post";
	formobj.target = "upload_target";
	formobj.action = getGlobalValue('SITE_CONTEXT') + '/ajaxbridge/parseresume.jsp?type=html';
	formobj.submit();
	formobj.target = "";
	formobj.action = "";
}

// browser independent method to get the value of a node using an xpath
function getHRXMLValue(xmlDoc, xpath) {
	var result = null, node = null;
	
	// ie method
	if ( window.ActiveXObject ) {
		result = xmlDoc.selectNodes(xpath);
		if ( (result != null) && (result.length > 0) )
			node = result[0];
	}
	// other browsers
	else {
		result = document.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);	
		if ( result != null )
			node = result.iterateNext(); 
	}
	
	// got the node, so get the node's value
	if ( node != null ) {
		var enodes = node.childNodes;
		for ( var n = 0; n < enodes.length; n++ ) {
			var enode = enodes[n];
			if ( enode.nodeType == 3 ) {
				// this is the text node
				return (enode.data == null)?"":enode.data;
			}
		}
	}
	
	return "";
}

// set the value of a form field given the xpath and xmldom object to retrieve the data from
function setFieldValue(xmlDoc, xpath, fname) {
	// get the actual form field name (fname is the "alias")
	var fldname = getLocalValue(fname);
	var fld = getFormElement('candregistrationform', fldname);
	if ( fld != null ) {
		fld.value = getHRXMLValue(xmlDoc, xpath);
	}
}

// given a state abbreviation, get the Adapt code id
function getStateCodeID(abbrev) {
	var states = getFormElement('candregistrationform', 'statecodes').value.split('|');
	var state;
	for ( var i = 0; i < states.length; i++ ) {
		state = states[i].split('~');
		if ( abbrev == state[1] )
			return state[0];
	}
		
	return "";
}

// given the selection basket form field names and the resume text, interpret the values
function doInterpretation(resumetext, sourceName, destName, hiddenName, maxAdd) {
	if ( resumetext.trim().length == 0 )
		return;
		
	var value;
	var found = 0;
	// parse the source basket values and search the resume text for each phrase
	var selobj = getFormElement('candregistrationform', sourceName)
	for ( i = 0; i < selobj.options.length; i ++ ) {
		value = selobj.options[i].text.toLowerCase();
		if ( resumetext.indexOf(value) >= 0 ) {
			// it's in there, so select it in the source basket
			selobj.options[i].selected = true;
			found++;
			if ( found >= maxAdd )
				break;
		}
	}
	// move all selections to the destination basket
	moveBasketSelection('candregistrationform', sourceName, destName, hiddenName, maxAdd);
}

// extract the xml from the iframe that was returned by the parser and populate the registration form
function autoPopulateForm() {
	var autopopbtn = getFormElement('candregistrationform', 'autopopulate');
	var ie = false;
	
	// get the hrxml from the iframe content
	var hrxml = '';
		
	var framedoc = document.getElementById('upload_target').contentDocument;
	
	if ( framedoc == null ) {
		// this must be IE, so get the content object the IE way
		framedoc = document.getElementById('upload_target').contentWindow.document;
		ie = true;
	}
	
	// get the content
	hrxml = framedoc.body.innerHTML.trim();	
	
	// check for empty xml or an error returned by the parser
	var e = hrxml.indexOf('Error');
	if ( hrxml == '' || ((e >= 0) && (e < 10)) ) {		
		autopopbtn.disabled = false;
		return;
	}
	
	// get a lowercase version of the hrxml for case insensitive search to
	// extract the resume text from the hrxml
	var hrxmllower = hrxml.toLowerCase();
	// find the beginning of the resume text
	var n = hrxmllower.indexOf("<nonxmlresume>");
	// extract the resume text (lower case for easy searching) for skills/category interpretation
	var resumetext = hrxmllower.substring(n);
	// piece the hrxml back together without the resume text
	hrxml = hrxml.substring(0, n);
	// ie likes it uppercase, but other browsers like it lowercase
	hrxml += ( ie == true )?"</RESUME>":"</resume>";
	
	// create the XMLDOM object
	var xmlDoc = getXMLDocument(hrxml);	
	
	// populate the form
	
	// name
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/PERSONNAME/GIVENNAME', 'CAND_FIRSTNAME');
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/PERSONNAME/MIDDLENAME', 'CAND_MIDDLENAME');
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/PERSONNAME/FAMILYNAME', 'CAND_LASTNAME');
	
	// street
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//POSTALADDRESS[@type="main"]/DELIVERYADDRESS/ADDRESSLINE', 'CAND_ADDRESS1');
	
	// city
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//POSTALADDRESS[@type="main"]/MUNICIPALITY', 'CAND_MUNICIPALITY');
	
	// state
	// for the state, we need to get the value, which will be the state abbreviate.  Then we look up the code id
	// using the getStateCodeID() method, which uses the "statecodes" hidden field that contains the state abbrevs and ids
	/*-- took out country interpretation --*/
	//var selobj = getFormElement('candregistrationform', getLocalValue('CAND_COUNTRY'));
	//var value = getHRXMLValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//POSTALADDRESS[@type="main"]/CountryCode');
	//value = getStateCodeID(value);
	// got the id, now select the state in the drop down
	//selectOption(selobj, value, true);

	
	// zip code
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//POSTALADDRESS[@type="main"]/POSTALCODE', 'CAND_POSTCODE');
	
	// home phone
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//TELEPHONE[@type="home"]/SUBSCRIBERNUMBER', 'CAND_PHONE_HOME');
	// work phone
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//TELEPHONE[@type="work"]/SUBSCRIBERNUMBER', 'CAND_PHONE_WORK');
	// mobile phone
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD//Mobile[@type="mob"]/SUBSCRIBERNUMBER', 'CAND_PHONE_CELL');
	// email address
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD/INTERNETEMAILADDRESS', 'CAND_EMAIL');	
	//Login
	setFieldValue(xmlDoc, '/RESUME/STRUCTUREDXMLRESUME/CONTACTINFO/CONTACTMETHOD/INTERNETEMAILADDRESS', 'WEB_LOGIN_LOGIN');
	// interpret the job certifications
	//doInterpretation(resumetext, 'certs_from', 'certs_to', 'selected_certs', getLocalValue('MAX_CERTS'));
	
	// interpret the skills
	doInterpretation(resumetext, 'skills_from', 'skills_to', 'selected_skills', getLocalValue('MAX_SKILLS'));	
	
	// re-enable the autopopulate button
	showStatus(false);
	autopopbtn.disabled = false;
}

function submitregistration() {
	showStatus(true);
	var formname = 'candregistrationform';
	var formobj = document.getElementById(formname);
	var pdelim = formobj.PDELIM.value;
	var vdelim = formobj.VDELIM.value;
	var validatorlist = formobj.VALIDATION_PARAMS.value;
	var validatedfields = validatorlist.split(vdelim);
	
	//first check doc type
	var upld = formobj.UPLOAD.value
	if( upld != '' ){
		upld = upld.toLowerCase();
		upld = upld.substring(upld.length-4,upld.length)
		if( upld.indexOf('.') > -1 ){
			upld= upld.substring(upld.length-3,upld.length)
			if(upld != 'tif' && upld != 'jpg' && upld != 'doc' && upld != 'pdf'){
				alert(getLocalValue('MSG_CAND_REG_INVALID_DOC_TYPE'));
				showStatus(false);
				return;
			}
		}else{
			if( upld == 'docx'){
				alert(getLocalValue('MSG_CAND_REG_INVALID_DOC_TYPE'));
				showStatus(false);
				return;
			}
		}
	}
	
	//validate the rest of the required fields
	for ( var i = 0; i < validatedfields.length; i++ ) {
		var params = validatedfields[i].split(pdelim);		
		var obj = getFormElement(formname, params[0]);
		if ( obj != null ) {
			if ( vAlert(params[3], obj.value, params[1], params[2], 0) == false ) {
				showStatus(false);
				return;
			}
			
			if ( params[0] == getLocalValue('WEB_LOGIN_PASSWORD') ) {
				var confirmpwd = formobj.CONFIRMPASSWORD.value;
				if ( obj.value != confirmpwd ) {
					alert(getLocalValue('MSG_SECURITY_PWD_NO_MATCH'));
					showStatus(false);
					return;
				}
			}
		}
	}
	if(formobj.verification.checked != true){
		alert(getLocalValue('MSG_CAND_REG_TERMSAGREE'));
		showStatus(false);
		return;
	}
	
	if ( validateUser() ) {
		// get the job category
		try{
			if ( formobj.selected_jobcat.value != '' ) {
				var certname = getLocalValue('CAND_JOBCATEGORY');
				var certarray = formobj.selected_jobcat.value.split(',');
				for ( var i = 0; i < certarray.length; i++ ) {
					var cert = certarray[i];
					var cname = certname.replace('$$', '$' + (i+1) + '$');
					var cobj = getFormElement(formname, cname);
					if ( cobj != null ) {
						cobj.value = cert;
					}
				}
			}
		}catch(err){
		}

		// get the cert
		try{
			if ( formobj.selected_certs.value != '' ) {
				var certname = getLocalValue('CAND_CERT');
				var certarray = formobj.selected_certs.value.split(',');
				for ( var i = 0; i < certarray.length; i++ ) {
					var cert = certarray[i];
					var cname = certname.replace('$$', '$' + (i+1) + '$');
					var cobj = getFormElement(formname, cname);
					if ( cobj != null ) {
									cobj.value = cert;
					}
				}
			}
		}catch(err){
		}


		// get the skills
		try{
			if ( formobj.selected_skills.value != '' ) {
				var skillname = getLocalValue('CAND_SKILL');
				var skillarray = formobj.selected_skills.value.split(',');
				for ( var i = 0; i < skillarray.length; i++ ) {
					var skill = skillarray[i];
					var sname = skillname.replace('$$', '$' + (i+1) + '$');
					var sobj = getFormElement(formname, sname);
					if ( sobj != null )
						sobj.value = skill;				
				}
			}
		}catch(err){
		}
		
		
		//Set other owned transport
		try{		
			if ( formobj.ownedtransport.value != '' ) {		
				var fname = getLocalValue('CAND_OWNED_TRANSPORT');
				var cnt = 1;
				for(var i=0;i<formobj.ownedtransport.options.length;i++) {
					if ( formobj.ownedtransport.options[i].selected) {
						var value = formobj.ownedtransport.options[i].value;
						var cname = fname.replace('$$', '$' + (cnt) + '$');
						cnt++;
						var fobj = getFormElement(formname, cname);
						if ( fobj != null ) {
							//alert(value)
							fobj.value = value;
						}
					}
				}
			}
		}catch(err){
		}
		
		//Set Geographical Locations
		try{		
			if ( formobj.geolocations.value != '' ) {		
				var fname = getLocalValue('CAND_GEOLOCATION');
				var cnt = 1;
				for(var i=0;i<formobj.geolocations.options.length;i++) {
					if ( formobj.geolocations.options[i].selected) {
						var value = formobj.geolocations.options[i].value;
						var cname = fname.replace('$$', '$' + (cnt) + '$');
						cnt++;
						var fobj = getFormElement(formname, cname);
						if ( fobj != null ) {
							fobj.value = value;
						}
					}
				}
			}
		}catch(err){
		}
		
		
		// create the full name:
		var fullname = getFormElement(formname, getLocalValue('CAND_FULLNAME'));
		var firstname = getFormElement(formname, getLocalValue('CAND_FIRSTNAME'));
		var middlename = getFormElement(formname, getLocalValue('CAND_MIDDLENAME'));
		var lastname = getFormElement(formname, getLocalValue('CAND_LASTNAME'));	

		if(middlename != null && middlename != 'undefined'){
			fullname.value = firstname.value + ' ' + middlename.value + ' ' + lastname.value;
		}else{//Short form
			fullname.value = firstname.value + ' ' + lastname.value;
		}
		
		// submit the form
		formobj.encoding = "multipart/form-data";
		formobj.method = "post";
		formobj.action = getGlobalValue('SITE_CONTEXT') + '/login/candregistration_post_view.jsp';
		formobj.submit();
	}	
	
	showStatus(false);
}
function validateEmail(){
	var formname = 'candregistrationform';
	var prefix = '&' + getGlobalValue('ADAPT_PREFIX');
	var searchname = '&' + getGlobalValue('SEARCHFORM_NAME');
	var countonly = '&' + getGlobalValue('SEARCHFORM_COUNTONLY') + '=yes';
	var obj, email1 = '', email2 = '', login = '';
	obj = getFormElement(formname, getLocalValue('CAND_EMAIL'));
	if ( obj != null )
		email1 = prefix + 'Email=' + obj.value;
	var params = searchname + '=GeneralCandidateMatch' + email1 + countonly;
	var count = runSearch( params );

	if ( Number(count) > 0 ) {
		alert(getLocalValue('MSG_CAND_REG_USER_EXISTS'));
		obj.value="";
		obj.focus();
		return false;
	}
}

function validateUser() {
	var formname = 'candregistrationform';
	var prefix = '&' + getGlobalValue('ADAPT_PREFIX');
	var searchname = '&' + getGlobalValue('SEARCHFORM_NAME');
	var countonly = '&' + getGlobalValue('SEARCHFORM_COUNTONLY') + '=yes';
	var obj, email1 = '', email2 = '', login = '';
	
	obj = getFormElement(formname, getLocalValue('CAND_EMAIL'));
	if ( obj != null )
		email1 = prefix + 'Email=' + obj.value;
				
	obj = getFormElement(formname, getLocalValue('WEB_LOGIN_LOGIN'));
	if ( obj != null )
		login = prefix + 'LoginName=' + obj.value;		
		
	// check the first email address
	var params = searchname + '=GeneralCandidateMatch' + email1 + countonly;
	var count = runSearch( params );

	if ( Number(count) > 0 ) {
		alert(getLocalValue('MSG_CAND_REG_USER_EXISTS'));
		return false;
	}
	
	// now check the login
	params = searchname + '=WR_FindLogin' + login + countonly;
	var count = runSearch( params );
	if ( Number(count) > 0 ) {
		alert(getLocalValue('MSG_CAND_REG_LOGIN_EXISTS'));
		return false;
	}
	
	return true;
}

//Get branches based on GeoLocation and JobCat
function getBranches(country){
	var formname = 'candregistrationform';
	var prefix = '&' + getGlobalValue('ADAPT_PREFIX');
	var prefix2 = getGlobalValue('ADAPT_PREFIX');
	var searchname = '&' + getGlobalValue('SEARCHFORM_NAME');
	var getList = '&' + getGlobalValue('SEARCHFORM_GETLISTONLY') + '=yes';
	var obj, jobcat, area;
	obj = getFormElement(formname, getLocalValue('CAND_OWNOFFICE'));
	while(obj.options.length > 1){obj.remove(1);}	
	jobcat  = getFormElement(formname, getLocalValue('CAND_JOBCATEGORYPREF'));
	area = getFormElement(formname, 'geolocations');
	var text = prefix + 'COUNTRY=' + country;
	text += prefix + 'JOBCAT=' + jobcat.options[jobcat.selectedIndex].value;
	var count = 1;
	for(i=0;i<area.length;i++) {
		if(area.options[i].selected) {
			text += prefix + 'AREA_' + count + '=' + area.options[i].value;
			count++;
		}
	}
	var params = searchname + '=All_OfficesWeb' + text + getList;
	var list = runSearch( params ).trim();	
	if(list.length > 0) {
	while(obj.options.length > 1){obj.remove(1);}
			list = list.substring(0,list.length-1);
			var items = list.split(";");
			for(i=0;i<items.length;i++) {
			var theString = prefix2 + items[i].trim();
			
				if( getLocalValue(theString) != "" ) {
					/*
					var objOption = document.createElement("option");
					objOption.value = items[i];
					objOption.text = getLocalValue(theString);
					obj.add(objOption);
					*/
					//done this way for cross browser compatibility
					obj.options[obj.options.length] = new Option(getLocalValue(theString), items[i]);
				}
			}
	}
}

function setRelocate(chkbox) {
	var formname = 'candregistrationform';
	var formobj = document.getElementById(formname);
	var prefix = getGlobalValue('ADAPT_PREFIX');
	with ( formobj ) {
		var relocFldName = getLocalValue('CAND_RELOCATE');
		var relocFld = getFormElement(formname, relocFldName);
		if ( relocFld != null )
			relocFld.value = (chkbox.checked == true)?'Y':'N';
	}
}

function warnOnCancel(msg, url) {
	if (confirm(msg))
		window.location.href = url;
}





