// JavaScript Document

var skip = false;
var openedDivID = "";  // used to set the open div for blanket click close
var skillDeleteID = ""; // used when a user click a skill to delete
var employmentDeleteID = ""; // used for when a user selects a records for delete
var checkedMessages = new Array(); // used for collecting message id's in the message board
var originalAdminPersonal = ''; // used to store current admin personal info in case they decide to cancel

function init(){
	if(document.getElementById('no_travel')){
		var d = document.getElementById('distance');
		if(document.getElementById('no_travel').checked){
			d.style.border = '1px solid #eee';
			d.style.color = '#ccc';
			d.disabled = 'disabled';
		}
	}
}

function focusGreenBtn(obj){
	document.getElementById(obj.id).setAttribute('className','greenBtnFocus');
	document.getElementById(obj.id).setAttribute('class','greenBtnFocus');
}
function blurGreenBtn(obj){
	document.getElementById(obj.id).setAttribute('className','greenBtnIni');
	document.getElementById(obj.id).setAttribute('class','greenBtnIni');
}

function focusBlueBtn(obj){
	document.getElementById(obj.id).setAttribute('className','blueBtnFocus');
	document.getElementById(obj.id).setAttribute('class','blueBtnFocus');
}
function blurBlueBtn(obj){
	document.getElementById(obj.id).setAttribute('className','blueBtnIni');
	document.getElementById(obj.id).setAttribute('class','blueBtnIni');
}

function focusRedBtn(obj){
	document.getElementById(obj.id).setAttribute('className','redBtnFocus');
	document.getElementById(obj.id).setAttribute('class','redBtnFocus');
}
function blurRedBtn(obj){
	document.getElementById(obj.id).setAttribute('className','redBtnIni');
	document.getElementById(obj.id).setAttribute('class','redBtnIni');
}

function focusLogin(obj){
	document.getElementById(obj.id).style.background = '#008be8';
}
function blurLogin(obj){
	document.getElementById(obj.id).style.background = '#0099ff';
}

function openLogin(){
	if(document.getElementById('loginDiv').style.display == 'block'){
		document.getElementById('loginDiv').style.display = 'none';
	}else if(document.getElementById('loginDiv').style.display == 'none'){
		document.getElementById('loginDiv').style.display = 'block';
	}
}

function focusTxt(obj){
	document.getElementById(obj.id).style.border = '1px solid #0199fe';
	if(document.getElementById(obj.id).value == "" || document.getElementById(obj.id).value == "Please enter"){
		document.getElementById(obj.id).value = '';
		document.getElementById(obj.id).style.color = "#868686";
		document.getElementById(obj.id).style.background = "#FFF";
	}
}

function blurTxt(obj){
	document.getElementById(obj.id).style.border = '1px solid #868686';
}

function isLoginValid(){
	var username = document.getElementById('username');
	var pass = document.getElementById('password');
	var id = document.getElementById('loginID');
	if(username.value == '' || username.value == 'Please enter'){
		username.value = 'Please enter';
		username.style.border = '1px solid red';
		username.style.color = 'red';
		return false;
	}else if(pass.value == "" || pass.value == "Please enter"){
		pass.value = 'Please enter';
		pass.style.border = '1px solid red';
		pass.style.color = 'red';
		return false;
	}else{
		id.value = 421;
		document.getElementById('loginForm').submit();
	}
	
}

function checkUsername(str){
	
	if (str.length==0){
		document.getElementById("usernameText").innerHTML="";
		document.getElementById('smallCross').style.display = 'none';
		document.getElementById('smallTick').style.display = 'none';
		return;
	}
	  
	if (window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	
	xmlhttp.onreadystatechange=function(){
	  
	  if (xmlhttp.readyState==4 && xmlhttp.status==200){
		  
		document.getElementById("usernameText").innerHTML = xmlhttp.responseText;
		
		if(xmlhttp.responseText == 'Username unavailable'){
		
			document.getElementById("usernameText").style.color = '#e02309';
			document.getElementById('smallCross').style.display = 'block';
			document.getElementById('smallTick').style.display = 'none';
			
		} else if (xmlhttp.responseText.indexOf('Only alphanumeric characters allowed') != -1){
			
			document.getElementById("usernameText").style.color = '#e02309';
			document.getElementById('smallCross').style.display = 'block';
			document.getElementById('smallTick').style.display = 'none';
			
		}else if (xmlhttp.responseText.indexOf('is not allowed') != -1){
			
			document.getElementById("usernameText").style.color = '#e02309';
			document.getElementById('smallCross').style.display = 'block';
			document.getElementById('smallTick').style.display = 'none';
			
		}else {
			
			document.getElementById("usernameText").style.color = '#090';
			document.getElementById('smallCross').style.display = 'none';
			document.getElementById('smallTick').style.display = 'block';
			
		}
	  
	  }
	  
	}
	
	xmlhttp.open("GET","includes/ajax/checkUsername.php?q="+escape(str),true);
	xmlhttp.send();
}

function checkFormEmail(str){
	//alert(str);
	if (str.length==0){
	  
	  document.getElementById("emailText").innerHTML="";
	  document.getElementById('smallCross_email').style.display = 'none';
	  document.getElementById('smallTick_email').style.display = 'none';
	  return;
	  
	}
	  
	if (window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	
	xmlhttp.onreadystatechange=function(){
	  
	  if (xmlhttp.readyState==4 && xmlhttp.status==200){
		  
		document.getElementById("emailText").innerHTML = xmlhttp.responseText;
		
		// returned string is a bit odd.......
		if(xmlhttp.responseText.indexOf('Invalid email address') != -1){
			
			document.getElementById("emailText").style.color = '#e02309';
			document.getElementById('smallCross_email').style.display = 'block';
			document.getElementById('smallTick_email').style.display = 'none';
			
		} else if(xmlhttp.responseText.indexOf('Email already registered') != -1){
			
			document.getElementById("emailText").style.color = '#e02309';
			document.getElementById('smallCross_email').style.display = 'block';
			document.getElementById('smallTick_email').style.display = 'none';
		
		}else {
			
			document.getElementById("emailText").style.color = '#090';
			document.getElementById('smallCross_email').style.display = 'none';
			document.getElementById('smallTick_email').style.display = 'block';
			
		}
	  
	  }
	  
	}
	
	xmlhttp.open("GET","includes/ajax/emailVerify.php?e="+str,true);
	xmlhttp.send();
	
}

function checkThisEmail(str){
	//alert(str);
	if (str.length==0){
	  
	  document.getElementById("emailInfoTxt").innerHTML="";
	  return;
	  
	}
	  
	if (window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	
	xmlhttp.onreadystatechange=function(){
	  
	  if (xmlhttp.readyState==4 && xmlhttp.status==200){
		  
		document.getElementById("emailInfoTxt").innerHTML = xmlhttp.responseText;
		
		// returned string is a bit odd.......
		if(xmlhttp.responseText.indexOf('Invalid email address') != -1){
			
			document.getElementById("emailInfoTxt").style.color = '#e02309';
			
		} else if(xmlhttp.responseText.indexOf('Email already registered') != -1){
			
			document.getElementById("emailInfoTxt").style.color = '#e02309';
		
		}else {
			
			document.getElementById("emailInfoTxt").style.color = '#090';
			
		}
	  
	  }
	  
	}
	
	xmlhttp.open("GET","includes/ajax/emailVerify.php?e="+str,true);
	xmlhttp.send();
	
}

function checkPassword(str){
	
	var pass1 = document.getElementById('new_member_pass');
	if(str != pass1.value){
		
		document.getElementById("pass2Text").style.color = '#e02309'; // red
		document.getElementById("pass2Text").innerHTML = "Passwords do not match";
		document.getElementById('smallTick_pass2').style.display = 'none';
		document.getElementById('smallCross_pass2').style.display = 'block';
		
	}else {
		
		document.getElementById("pass2Text").style.color = '#090'; // green
		document.getElementById("pass2Text").innerHTML = "Passwords match";
		document.getElementById('smallTick_pass2').style.display = 'block';
		document.getElementById('smallCross_pass2').style.display = 'none';
		
	}
	
}

function checkCode(str){
	
	var genCode = document.getElementById('generatedCode').value;
	
	var gen = genCode;
	var inpt = str;
	
	if (str.length==0){
	  
	  document.getElementById("codeText").innerHTML="";
	  return;
	  
	}
	
	if(inpt != gen){
		
		document.getElementById("codeText").style.color = '#e02309'; // red
		document.getElementById("codeText").innerHTML = "Incorrect code";
		document.getElementById('smallTick_code').style.display = 'none';
		document.getElementById('smallCross_code').style.display = 'block';
		
	}else {
		
		document.getElementById("codeText").style.color = '#090'; // green
		document.getElementById("codeText").innerHTML = "Code confirmed";
		document.getElementById('smallTick_code').style.display = 'block';
		document.getElementById('smallCross_code').style.display = 'none';
		
	}
	
}

function isMemberRegistervalid(){
	
	var username  = document.getElementById('new_member_un');
	var email     = document.getElementById('new_member_email');
	var pass1     = document.getElementById('new_member_pass');	
	var pass2     = document.getElementById('new_member_pass2');	
	var code      = document.getElementById('generatedCode');
	var inpt_code = document.getElementById('activationcode');
	var message   = document.getElementById('regFail');
	var sq        = document.getElementById('secretQuestion');
	var sa        = document.getElementById('secretAnswer');
	var skill_sys = document.getElementById('skill_rewrite').innerHTML;
	var messageTxt = document.getElementById('alertTxt');
	var fName = document.getElementById('first_name');
	var lName = document.getElementById('last_name');
	var country = document.getElementById('location_country');
	var county = document.getElementById('county');
	
	if(fName.value == "" || fName.value == "Please enter"){
		fName.style.color = "#e02309";
		fName.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please enter your first name.</p>';
		popup('alert');
		return false;
	}else
	if(lName.value == "" || lName.value == "Please enter"){
		lName.style.color = "#e02309";
		lName.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please enter your last name.</p>';
		popup('alert');
		return false;
	}else
	if(country.value == "empty"){
		country.style.color = "#e02309";
		country.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please select a country.</p>';
		popup('alert');
		return false;
	}else
	if(county.value == "empty"){
		county.style.color = "#e02309";
		county.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please select a region.</p>';
		popup('alert');
		return false;
	}else
	// check username
	if(username.value == "" || username.value == "Please enter"){
		username.style.color = "#e02309";
		username.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please enter a username</p>';
		popup('alert');
		return false;
	}else if(document.getElementById('smallCross').style.display == "block"){
		username.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please choose a valid username</p>';
		popup('alert');
		return false;
	}else
	//check email
	if(email.value == "" || email.value == "Please enter"){
		email.style.color = "#e02309";
		email.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please enter you email address</p>';
		popup('alert');
		return false;
	}else if(document.getElementById('smallCross_email').style.display == "block"){
		email.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}else 
	// check password 1
	if(pass1.value == "" || pass1.value == "Please enter"){
		pass1.style.color = "#e02309";
		pass1.style.border = "1px solid #e02309";
		return false;
	}else 
	// check password 2
	if(pass2.value == "" || pass2.value == "Please enter"){
		pass2.style.color = "#e02309";
		pass2.style.border = "1px solid #e02309";
		return false;
	}else 
	// check passwors match
	if(document.getElementById('smallCross_pass2').style.display == 'block'){
		pass1.style.border = "1px solid #e02309";
		pass2.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please ensure passwords match</p>';
		popup('alert');
		return false;
	}else 
	// check secrets
	if(sq.value == 'empty'){
		sq.style.color = "#e02309";
		sq.style.border = "1px solid #e02309";
		messageTxt.innerHTML = '<p>Please select a secret question</p>';
		popup('alert');
		return false;
	}else 
	if(sa.value == "" || sa.value == "Please enter"){
		sa.style.color = "#e02309";
		sa.style.border = "1px solid #e02309";
		return false;
	}else 
	// check code field
	if(code.value == "" || code.value == "Please enter"){
		code.style.color = "#e02309";
		code.style.border = "1px solid #e02309";
	}else 
	// check codes match
	if(code.value != inpt_code.value){
		message.innerHTML = "Security codes do not match";
		code.style.border = "1px solid #e02309";
		return false;
	}else 
	if(skill_sys == ' ' || skill_sys == 'Please add a skill'){
		messageTxt.innerHTML = '<p style="color:red;text-align:center">Please add at least one skill.</p>';
		popup('alert');
		return false;
	}else{
		document.getElementById('memberRegID').value = 421;
		document.getElementById('memberRegisterForm').submit();
		return true;	
	}
		
}

function jumpToArticle(id){
	if(id == "empty"){
		popup('alert');
		opendDivID = 'alert';
	}else{
		location.href='newsSubCat.php?id='+id;
	}
}

function isProfileValid(){
	
	var firstname = document.getElementById('firstname');
	var lastname  = document.getElementById('lastname');
	var email     = document.getElementById('email');
	var county    = document.getElementById('address_county');
	var country   = document.getElementById('address_country');
	
	if(checkEmail(email.value) == false){
		document.getElementById('alertTxt').innerHTML = "Please enter a valid email address";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}
	
	if(firstname.value == "" || firstname.value == "Please enter"){
		firstname.style.border = "1px solid red";
		firstname.style.color = "red";
		firstname.value = "Please enter";
		document.getElementById('alertTxt').innerHTML = "Please enter your first name";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}else if(lastname.value == "" || lastname.value == "Please enter"){
		lastname.style.border = "1px solid red";
		lastname.style.color = "red";
		lastname.value = "Please enter";
		document.getElementById('alertTxt').innerHTML = "Please enter your last name";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}else if(email.value == "" || email.value == "Please enter"){
		email.style.border = "1px solid red";
		email.style.color = "red";
		email.value = "Please enter";
		document.getElementById('alertTxt').innerHTML = "Please enter your email address";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}else if(county.value == "empty" || county.value == "Please enter"){
		county.style.border = "1px solid red";
		document.getElementById('alertTxt').innerHTML = "Please select a county";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}else if(country.value == "empty" || country.value == "Please enter"){
		country.style.border = "1px solid red";
		document.getElementById('alertTxt').innerHTML = "Please select a country";
		popup('alert');
		openedDivID = 'alert';
		return false;
	}else {
		document.getElementById('submitID').value = 421;
		document.getElementById('personalDetailsForm').submit();	
	}
	
	
}

function getSkillForDelete(id){
	// create an array of elements with class name repeater (this is to loop over later and set border styles)
	// as IE is shit we have to do a work around becuse it can't manage a simple function!
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('repeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "repeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	// this is our selected div
	div = document.getElementById('repeater_'+id+'');
	
	// loop and set styles (we want to reset all divs, we'll change the selected one after this)
	for(i=0;i<arr.length;i++){
		document.getElementById((arr[i].id)).style.background = '';
	}
	
	div.style.background = '#c2e1f6';
	document.getElementById('removeBtn').setAttribute('className','blueBtnIni');
	document.getElementById('removeBtn').setAttribute('class','blueBtnIni');
	document.getElementById('removeBtn').disabled = false;
	skillDeleteID = id;
}

function getEmploymentForDelete(id){
	// create an array of elements with class name repeater (this is to loop over later and set border styles)
	// as IE is shit we have to do a work around becuse it can't manage a simple function!
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('repeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "repeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	// this is our selected div
	div = document.getElementById('repeater_employment_'+id+'');
	
	// loop and set styles (we want to reset all divs, we'll change the selected one after this)
	for(i=0;i<arr.length;i++){
		document.getElementById((arr[i].id)).style.background = '';
	}
	
	div.style.background = '#c2e1f6';
	document.getElementById('removeEmploymentBtn').setAttribute('className','blueBtnIni');
	document.getElementById('removeEmploymentBtn').setAttribute('class','blueBtnIni');
	document.getElementById('removeEmploymentBtn').disabled = false;
	document.getElementById('addEmploymentBtn').style.display = 'none';
	document.getElementById('updateEmploymentBtn').style.display = 'block';
	document.getElementById('clearBtn').style.display = 'block';
	employmentDeleteID = id;
	// now populate form
	var params = '?id='+employmentDeleteID;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText != 'FAIL'){
				// using a csv from php page, then splitting into array to populate fields
				// will return in this order:
				// title
				// company
				// from day
				// from month
				// from year
				// to day
				// to month 
				// to year
				// txt
				var obj_arr = xmlhttp.responseText.split("|");
				var title       = document.getElementById('jobTitle').value      = obj_arr[0];
				var comp        = document.getElementById('company').value       = obj_arr[1];
				var e_day       = document.getElementById('e_day').value         = obj_arr[2];
				var e_month     = document.getElementById('e_month').value       = obj_arr[3];
				var e_year      = document.getElementById('e_year').value        = obj_arr[4];
				if(obj_arr[9] != 1){
					var e_end_day   = document.getElementById('e_end_day').value     = obj_arr[5];
					var e_end_month = document.getElementById('e_end_month').value   = obj_arr[6];
					var e_end_year  = document.getElementById('e_end_year').value    = obj_arr[7];
				}else{
					// tick current_pos box and greey out end date
					document.getElementById('current_pos').checked = true;
					toggle_fields(Array('e_end_day','e_end_month','e_end_year'));
					// apply gui
					$('input').customInput();
				}
				document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML = obj_arr[8];
				
			}else{
				alert('Error fetching employment record!');	
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/getEmploymentRecord.php"+params,true);
	xmlhttp.send();
}

function resetEmployment(){
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('repeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "repeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	
	// loop and set styles (we want to reset all divs, we'll change the selected one after this)
	for(i=0;i<arr.length;i++){
		document.getElementById((arr[i].id)).style.background = '';
	}
	
	employmentDeleteID = "";
	document.getElementById('jobTitle').value            = '';
	document.getElementById('company').value             = '';
	document.getElementById('e_day').selectedIndex       = 0;
	document.getElementById('e_month').selectedIndex     = 0;
	document.getElementById('e_year').selectedIndex      = 0;
	document.getElementById('e_end_day').selectedIndex   = 0;
	document.getElementById('e_end_month').selectedIndex = 0;
	document.getElementById('e_end_year').selectedIndex  = 0;
	document.getElementById('employmentTxt').value       = '';
	document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML = '';
	document.getElementById('removeEmploymentBtn').setAttribute('className','removeBtnDisabled');
	document.getElementById('removeEmploymentBtn').setAttribute('class','removeBtnDisabled');
	document.getElementById('removeEmploymentBtn').disabled = true;
	document.getElementById('addEmploymentBtn').style.display = 'block';
	document.getElementById('updateEmploymentBtn').style.display = 'none';
	document.getElementById('clearBtn').style.display = 'none';
}

function updateEmployment(){
	var params = '?id='+employmentDeleteID;
	var title       = document.getElementById('jobTitle');
	var comp        = document.getElementById('company');
	var e_day       = document.getElementById('e_day').value;
	var e_month     = document.getElementById('e_month').value;
	var e_year      = document.getElementById('e_year').value;
	var e_end_day   = document.getElementById('e_end_day').value;
	var e_end_month = document.getElementById('e_end_month').value;
	var e_end_year  = document.getElementById('e_end_year').value;
	var current_pos = (document.getElementById('current_pos').checked) ? 1 : 0 ;
	//var txt         = document.getElementById('employmentTxt');
	var txt         = document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML;
	
	var my_arr = new Array();
	my_arr[0] = title;
	my_arr[1] = comp;
	my_arr[2] = txt;
	for(i=0;i<my_arr.length;i++){
		if(my_arr[i].value == ''){
			document.getElementById('alertTxt').innerHTML = 'Please ensure all fields are complete';
			document.getElementById(my_arr[i].id).style.border = '1px solid red';
			document.getElementById(my_arr[i].id).style.color = 'red';
			popup('alert');
			return false;
		}
	}
	
	var from = e_year+'-'+e_month+'-'+e_day;
	var to   = e_end_year+'-'+e_end_month+'-'+e_end_day;
	
	var theStartDate = new Date; 
	theStartDate.setDate(e_day);
	theStartDate.setMonth(document.getElementById('e_month').selectedIndex); // January = 0
	theStartDate.setFullYear(e_year); 
	//alert(theStartDate);return false;
	var theEndDate = new Date; 
	theEndDate.setDate(e_end_day);
	theEndDate.setMonth(document.getElementById('e_end_month').selectedIndex); // January = 0
	theEndDate.setFullYear(e_end_year); 
	
	// if the user has ticked current pos skip the validation
	if(date_checkbox_toggle == 'off'){
		if (theStartDate > theEndDate){
			alert('Error:\n\nThe \'To Date\' cannot be before the \'From Date\'\n\nPlease amend your entry.\n\n');
			return false;
			//alert(theEndDate+' is before '+theStartDate);
		}
		else 
		{
			// everything is ok, proceed
			//alert(theEndDate+' is on or after '+theStartDate);
		}
	}
	
	var params = '?id='+employmentDeleteID+'&title='+title.value+'&comp='+comp.value+'&from='+from+'&to='+to+'&txt='+txt+'&current_pos='+current_pos;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('add_employ_process').style.display = 'none';
        	document.getElementById('currentEmploymentHolder').style.display = 'block';
			document.getElementById('currentEmploymentHolder').innerHTML = xmlhttp.responseText;
			var t = setTimeout('resetEmployment()', 300);
			employmentDeleteID = '';
			var t = setTimeout('refreshPercent()',900);
		}else{
			document.getElementById('add_employ_process').style.display = 'block';
        	document.getElementById('currentEmploymentHolder').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/updateEmployment.php"+params,true);
	xmlhttp.send();	
}

function doDelete(){
	
	var params = '?id='+skillDeleteID;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('removeBtn').setAttribute('className','removeBtnDisabled');
			document.getElementById('removeBtn').setAttribute('class','removeBtnDisabled');
			document.getElementById('removeBtn').disabled = true;
			document.getElementById('add_skills_process').style.display = 'none';
        	document.getElementById('currentSkillsHolder').style.display = 'block';
			document.getElementById('currentSkillsHolder').innerHTML = xmlhttp.responseText;
			var t = setTimeout('popup(\'deleteDiv\');document.getElementById(\'blanket\').style.display=\'block\'', 800);
			var s = setTimeout('refreshPercent()',500);
		}else{
			document.getElementById('removeBtn').setAttribute('className','removeBtnDisabled');
			document.getElementById('removeBtn').setAttribute('class','removeBtnDisabled');
			document.getElementById('removeBtn').disabled = true;
			document.getElementById('add_skills_process').style.display = 'block';
        	document.getElementById('currentSkillsHolder').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/removeSkill.php"+params,true);
	xmlhttp.send();
}

function doDeleteEmployment(){
	
	var params = '?id='+employmentDeleteID;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('removeEmploymentBtn').setAttribute('className','removeBtnDisabled');
			document.getElementById('removeEmploymentBtn').setAttribute('class','removeBtnDisabled');
			document.getElementById('removeEmploymentBtn').disabled = true;
			document.getElementById('add_employ_process').style.display = 'none';
        	document.getElementById('currentEmploymentHolder').style.display = 'block';
			document.getElementById('currentEmploymentHolder').innerHTML = xmlhttp.responseText;
			var s = setTimeout('resetEmployment()', 300);
			var t = setTimeout('popup(\'deleteEmploymentDiv\');document.getElementById(\'blanket\').style.display=\'block\';', 800);
			employmentDeleteID = '';
			var u = setTimeout('refreshPercent()',900);
		}else{
			document.getElementById('removeEmploymentBtn').setAttribute('className','removeBtnDisabled');
			document.getElementById('removeEmploymentBtn').setAttribute('class','removeBtnDisabled');
			document.getElementById('removeEmploymentBtn').disabled = true;
			document.getElementById('add_employ_process').style.display = 'block';
        	document.getElementById('currentEmploymentHolder').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/removeEmployment.php"+params,true);
	xmlhttp.send();
}

// this is just checking the drop downs, once all are selected we can enable the add button
function checkNewSkill(){
	var sys = document.getElementById('sap_system');
	var skill = document.getElementById('sap_skill');
	var expo = document.getElementById('exposure');
	var cert = document.getElementById('cert');
	//var exper = document.getElementById('experience');
	if(sys.value != 'empty' && skill.value != 'empty' && expo.value != 'empty' && cert.value != 'empty'){
		document.getElementById('addBtn').disabled = false;
		document.getElementById('addBtn').setAttribute("className", "greenBtnIni");
		document.getElementById('addBtn').setAttribute('class','greenBtnIni');
	}else{
		
		document.getElementById('addBtn').setAttribute("className", "removeBtnDisabled");
		document.getElementById('addBtn').setAttribute('class','removeBtnDisabled');
		document.getElementById('addBtn').disabled = true;
	}
}

function addSkill(){
	
	var sys = document.getElementById('sap_system').value;
	var skill = document.getElementById('sap_skill').value;
	var expo = document.getElementById('exposure').value;
	var cert = document.getElementById('cert').value;
	//var exper = document.getElementById('experience').value;
	
	var params = '?sys='+escape(sys)+'&skill='+escape(skill)+'&expo='+escape(expo)+'&cert='+escape(cert);
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('add_skills_process').style.display = 'none';
        	document.getElementById('currentSkillsHolder').style.display = 'block';
			document.getElementById('currentSkillsHolder').innerHTML = xmlhttp.responseText;
			document.getElementById('addBtn').setAttribute('class','removeBtnDisabled');
			document.getElementById('addBtn').disabled = true;
			document.getElementById('sap_system').selectedIndex = 0;
			document.getElementById('sap_skill').selectedIndex = 0;
			document.getElementById('exposure').selectedIndex = 0;
			document.getElementById('cert').selectedIndex = 0;
			//document.getElementById('experience').selectedIndex = 0;
			var t = setTimeout('refreshPercent()',900);
		}else{
			document.getElementById('add_skills_process').style.display = 'block';
        	document.getElementById('currentSkillsHolder').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/addSkill.php"+params,true);
	xmlhttp.send();
	
}

function addEmployment(){
	
	var title       = document.getElementById('jobTitle');
	var comp        = document.getElementById('company');
	var e_day       = document.getElementById('e_day').value;
	var e_month     = document.getElementById('e_month').value;
	var e_year      = document.getElementById('e_year').value;
	var e_end_day   = document.getElementById('e_end_day').value;
	var e_end_month = document.getElementById('e_end_month').value;
	var e_end_year  = document.getElementById('e_end_year').value;
	var current_pos = (document.getElementById('current_pos').checked) ? 1 : 0;
	//var txt         = document.getElementById('employmentTxt');
	var txt         = document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML;
	//alert(txt);return false;
	var from = e_year+'-'+e_month+'-'+e_day;
	var to   = e_end_year+'-'+e_end_month+'-'+e_end_day;
	
	var theStartDate = new Date; 
	theStartDate.setDate(e_day);
	theStartDate.setMonth(document.getElementById('e_month').selectedIndex); // January = 0
	theStartDate.setFullYear(e_year); 
	//alert(theStartDate);return false;
	var theEndDate = new Date; 
	theEndDate.setDate(e_end_day);
	theEndDate.setMonth(document.getElementById('e_end_month').selectedIndex); // January = 0
	theEndDate.setFullYear(e_end_year); 
	
	// if the user has ticked current possition then skip this validation
	if(date_checkbox_toggle == 'off'){
		if (theStartDate > theEndDate){
			alert('Error:\n\nThe \'To Date\' cannot be before the \'From Date\'\n\nPlease amend your entry.\n\n');
			return false;
			//alert(theEndDate+' is before '+theStartDate);
		}
		else 
		{
			// everything is ok, proceed
			//alert(theEndDate+' is on or after '+theStartDate);
		}
	}
	
	var my_arr = new Array();
	my_arr[0] = title;
	my_arr[1] = comp;
	//my_arr[2] = txt;
	for(i=0;i<my_arr.length;i++){
		if(my_arr[i].value == ''){
			document.getElementById('alertTxt').innerHTML = 'Please ensure all fields are complete';
			document.getElementById(my_arr[i].id).style.border = '1px solid red';
			document.getElementById(my_arr[i].id).style.color = 'red';
			popup('alert');
			document.getElementById('blanket').style.display = 'block';
			return false;
		}
	}
	//alert(document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML.length);return false;
	if(document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML.length == 14){
		document.getElementById('alertTxt').innerHTML = 'Please describe key achievements and duties';
		popup('alert');
		document.getElementById('blanket').style.display = 'block';
		return false;
	}
	
	var params = '?title='+title.value+'&comp='+comp.value+'&from='+from+'&to='+to+'&txt='+txt+'&current_pos='+current_pos;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('add_employ_process').style.display = 'none';
        	document.getElementById('currentEmploymentHolder').style.display = 'block';
			document.getElementById('currentEmploymentHolder').innerHTML = xmlhttp.responseText;
			document.getElementById('e_day').selectedIndex   = 0;
			document.getElementById('e_month').selectedIndex = 0;
			document.getElementById('e_year').selectedIndex  = 0;
			document.getElementById('e_end_day').selectedIndex   = 0;
			document.getElementById('e_end_month').selectedIndex = 0;
			document.getElementById('e_end_year').selectedIndex  = 0;
			document.getElementById('jobTitle').value = '';
			document.getElementById('company').value = '';
			document.getElementById('current_pos').checked = false;
			// apply gui
			$('input').customInput();
			// reset date boxes
			reset_dropDowns(Array('e_end_day','e_end_month','e_end_year'));
			document.getElementById('employmentTxt').value = '';
			document.getElementById('employmentTxtWidgIframe').contentWindow.document.body.innerHTML = '';
			var s = setTimeout('refreshPercent()', 900);
		}else{
			document.getElementById('add_employ_process').style.display = 'block';
        	document.getElementById('currentEmploymentHolder').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/addEmployment.php"+params,true);
	xmlhttp.send();
	
}

function refreshPercent(){
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText != "FAIL"){
				document.getElementById('percentHolder').innerHTML = xmlhttp.responseText;
			}else{
				alert('Error fetching percent!');	
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/getPercent.php",true);
	xmlhttp.send();	
}

function getEditForDelete(id){
	// create an array of elements with class name repeater (this is to loop over later and set border styles)
	// as IE is shit we have to do a work around becuse it can't manage a simple function!
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('repeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "repeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	// this is our selected div
	div = document.getElementById('repeater_employment_'+id+'');
	
	// loop and set styles (we want to reset all divs, we'll change the selected one after this)
	for(i=0;i<arr.length;i++){
		document.getElementById((arr[i].id)).style.background = '';
	}
	
	div.style.background = '#c2e1f6';
	document.getElementById('removeEmploymentBtn').setAttribute('className','blueBtnIni'); // for IE7
	document.getElementById('removeEmploymentBtn').setAttribute('class','blueBtnIni');
	document.getElementById('removeEmploymentBtn').disabled = false;
	employmentDeleteID = id;
}

function toggleDrop(toggleID){

	var toggle = document.getElementById(toggleID);
	
	if(toggle.style.display == 'none'){
		//toggle.style.dispaly = 'block';
		//toggle.setAttribute('display','block');
	}else{
		//toggle.style.dispaly = 'none';
		//toggle.setAttribute('display','none');
	}
	//updateToggle = checkbox.checked ? toggle.disabled = true : toggle.disabled = false;
	
}

function getCharCount(str){
	if(str.length < 250){
		var remaining = 250 - str.length;
		document.getElementById('characterCount').innerHTML = 'Recommend another <strong style="color:#000">'+remaining+'</strong> characters';
	}else{
		document.getElementById('characterCount').innerHTML = '';
	}
}

// called on keyup
function validateEmail(str){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
	
	if(emailPattern.test(str) == false){
		document.getElementById('emailImg').innerHTML = '<img src="imgs/small_cross.png" height="13" border="0" alt="" />';
	}else{
		document.getElementById('emailImg').innerHTML = '<img src="imgs/small_tick.png" height="13" border="0" alt="" />';
	}
} 

// called from form submit validation (isProfileValid())
function checkEmail(str){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
	return emailPattern.test(str);
}

function jumpToFAQ(){
	var page = document.getElementById('faqFilter').value;
	location.href='faqs.php?cat='+page;	
}

var popupTimerHandle = null;

function showBox(div) {
	
	if (popupTimerHandle != null) {
		
		clearTimeout(popupTimerHandle);
		popupTimerHandle = null;
	
	}
		
	divObj = document.getElementById(div);
	divObj.style.display = 'block';
	
}
		
function hideBox(div) {
	
	popupTimerHandle = setTimeout("reallyHideBox('" + div + "');", 350);

}
		
function reallyHideBox(div) {
	
	divObj = document.getElementById(div);
	divObj.style.display = 'none';

}

function isForgotValid(){
	var q = document.getElementById('secretQuestion');
	var a = document.getElementById('secretAnswer');
	var e = document.getElementById('email');
	if(q.value == 'empty'){
		document.getElementById('alertTxt').innerHTML = 'Please select a secret question';
		popup('alert');	
		return false;
	}else 
	if(a.value == '' || a.value == 'Please enter'){
		document.getElementById('alertTxt').innerHTML = 'Please answer the secret question';
		a.value = 'Please enter';
		a.style.border = '1px solid red';
		popup('alert');	
		return false;
	}if(e.value == '' || e.value == 'Please enter'){
		document.getElementById('alertTxt').innerHTML = 'Please enter you email address';
		e.value = 'Please enter';
		e.style.border = '1px solid red';
		popup('alert');	
		return false;
	}else{
		document.getElementById('submitID').value = 421;	
	}
}

function showPasswordRequestSuccess(email){
	document.getElementById('pageInfo').innerHTML = '<div class="thirdsTitle">Email sent.</div><div class="thirds_center" style="float:none;width:600px;margin:30px auto;"><div class="innerThirds" style="width:400px;margin:10px auto;"><div class="forgotElementHolder" style="font-size:18px;line-height:25px">An email has been sent to '+email+'.</div><div class="forgotElementHolder">Please check your inbox and follow the link provided to reset your password.</div></div></div></div>';
}

function showPasswordRequestFail(){
	document.getElementById('pageInfo').innerHTML = '<div class="thirdsTitle" style="color:red;">Error.</div><div class="thirds_center" style="float:none;width:600px;margin:30px auto;"><div class="innerThirds" style="width:400px;margin:10px auto;"><div class="forgotElementHolder" style="font-size:18px;line-height:20px;">Your secret question and answer did not match any records in our database.</div><div class="forgotElementHolder">Please check your question and answer and try again.</div><div class="forgotElementHolder"><input type="button" value="Try again" class="greenBtnIni" id="resetBtn" name="resetBtn" onmouseover="focusGreenBtn(this)" onmouseout="blurGreenBtn(this)" onclick="location.href=\'forgot_password.php\'" /></div><!-- end of forgotElementHolder --></div></div></div>';
}

function isPasswordValid(){
	var p1 = document.getElementById('newPass');	
	var p2 = document.getElementById('newPass2');
	if(p1.value == ""){
		document.getElementById('alertTxt').innerHTML = 'Please enter a new password';
		p1.style.border = '1px solid red';
		popup('alert');	
		return false;
	}
	if(p2.value == ""){
		document.getElementById('alertTxt').innerHTML = 'Please confirm your new password';
		p2.style.border = '1px solid red';
		popup('alert');	
		return false;
	}
	document.getElementById('submitID').value = 421;
}

function checkChangedPassword(str){
	var p1 = document.getElementById('newPass');
	if(str != p1.value){
		document.getElementById('pass2Text').innerHTML            = 'Passwords do not match';
		document.getElementById('pass2Text').style.color          = 'red';
		document.getElementById('smallCross_pass2').style.display = 'inline';
		document.getElementById('smallTick_pass2').style.display  = 'none';
	}else{
		document.getElementById('pass2Text').innerHTML            = 'Passwords match';
		document.getElementById('pass2Text').style.color          = 'green';
		document.getElementById('smallCross_pass2').style.display = 'none';
		document.getElementById('smallTick_pass2').style.display  = 'inline';
	}
}

function isPaperSearchValid(){
	if(document.getElementById('paperSearchTxt').value == "" || document.getElementById('paperSearchTxt').value == "Please enter"){
		document.getElementById('alertTxt').innerHTML = 'Please enter a valid search';
		document.getElementById('paperSearchTxt').value = "Please enter";
		document.getElementById('paperSearchTxt').style.border = '1px solid red';
		document.getElementById('paperSearchTxt').style.color = 'red';
		popup('alert');
		return false;
	}
}

function locateToIndex(){
	location.href = 'index.php';	
}

function isContactValid(){
	var name = document.getElementById('f_name');	
	var email = document.getElementById('email');
	var mes = document.getElementById('message');
	var alt = document.getElementById('alertTxt');	
	var dept = document.getElementById('dept');
	if(dept.value == 'empty'){
		dept.style.border = '1px solid red';
		alt.innerHTML = 'Please select a department to contact';
		popup('alert');
		return false;
	}else 
	if(name.value == "" || name.value == "Please enter"){
		name.style.color = 'red';
		name.style.border = '1px solid red';
		alt.innerHTML = 'Please enter your name';
		popup('alert');
		return false;
	}
	else if(email.value == "" || email.value == "Please enter"){
		email.style.color = 'red';
		email.style.border = '1px solid red';
		alt.innerHTML = 'Please enter your email address';
		popup('alert');
		return false;
	}
	else if(mes.value == "" || mes.value == "Please enter"){
		mes.style.color = 'red';
		mes.style.border = '1px solid red';
		alt.innerHTML = 'Please enter your message';
		popup('alert');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
	}
}

function toggleStartupPopup(obj){
	
	if(document.getElementById(obj.id).checked == true){
		var params = '?value=1';
	}else{
		var params = '?value=0';
	}
	
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText != ""){
				location.href = 'profile.php';
			}else{
				alert('Error setting popup!');	
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/disablePopup.php"+params,true);
	xmlhttp.send();
}

function isAgencyValid(){
	
	var agencyName  = document.getElementById('agencyName');
	var phone      = document.getElementById('phone');
	var vat        = document.getElementById('vat');	
	var comp       = document.getElementById('comp');	
	var house      = document.getElementById('house');
	var ad1        = document.getElementById('ad1');
	var ad2        = document.getElementById('ad2');
	var town       = document.getElementById('town');
	var county     = document.getElementById('county');
	var pcode      = document.getElementById('pcode');
	var fName      = document.getElementById('fName');
	var lName      = document.getElementById('lName');
	var adminPhone = document.getElementById('adminPhone');
	//var payment    = document.getElementById('payment');
	var how        = document.getElementById('how');
	
	var agency_username   = document.getElementById('agency_username');
	var agents     = document.getElementById('agents');
	var email      = document.getElementById('email');
	var pass1      = document.getElementById('new_member_pass');
	var pass2      = document.getElementById('new_member_pass2');
	var codeInput  = document.getElementById('activationcode');
	var codeGen    = document.getElementById('generatedCode');
	
	var secretQ    = document.getElementById('secretQuestion');
	var secretA    = document.getElementById('secretAnswer');
	
	if(document.getElementById('termsCheck').checked == true){
		
	}else{
		document.getElementById('alertTxt').innerHTML = '<p style="color:#333">Please read and agree to the terms of use before proceeding</p><p align="center"><img src="imgs/okBtn.png" border="0" onclick="popup(\'alert\')" style="cursor:pointer" /></p>';
		popup('alert');
		return false;	
	}
	
	var arr = new Array(agencyName,phone,vat,comp,house,ad1,town,county,pcode,fName,lName,adminPhone,how,secretQ,secretA);
	for(i=0;i<arr.length;i++){
		if(arr[i].value == '' || arr[i].value == 'Please enter' || arr[i].value == 'empty'){
			arr[i].style.border = '1px solid red';
			document.getElementById('errorTxt').innerHTML = 'Please ensure all fields are completed';
			return false;	
		}
	}
	var lowerCode = codeInput.value.toLowerCase();
	var lowerGen = codeGen.value.toLowerCase()
	/*if(testNumbers(vat, 'vatError') == false){
		vat.style.border = '1px solid #e02309';
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid VAT number';
		return false;
	}else 
	if(testNumbers(comp, 'compError') == false){
		comp.style.border = '1px solid #C00';
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid company number';
		return false;
	}else 
	if(testNumbers(phone, 'phoneError') == false){
		phone.style.border = '1px solid #C00';
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid phone number';
		return false;
	}else 
	if(testNumbers(adminPhone, 'adminPhoneError') == false){
		adminPhone.style.border = '1px solid #C00';
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid admin phone number';
		return false;
	}else */
	
	// check username
	if(agency_username.value == ""){
		agency_username.style.color = "#e02309";
		agency_username.style.border = "1px solid #C00";
		alert('Please enter a username');
		return false;
	}else if(document.getElementById('smallCross').style.display == "block"){
		username.style.border = "1px solid #C00";
		document.getElementById('errorTxt').innerHTML = 'Please choose a valid username';
		alert('Please enter a valid username');
		return false;
	}else
	//check email
	if(email.value == "" || email.value == "Please enter"){
		email.style.color = "#e02309";
		email.style.border = "1px solid #C00";
		email.value = "Please enter";
		alert('Please enter an email address');
		return false;
	}else if(document.getElementById('smallCross_email').style.display == "block"){
		email.style.border = "1px solid #C00";
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid email address';
		alert('Please enter a valid email address');
		return false;
	}else 
	// check password 1
	if(pass1.value == "" || pass1.value == "Please enter"){
		pass1.style.color = "#e02309";
		pass1.style.border = "1px solid #C00";
		pass1.value = "Please enter";
		alert('Please enter a password');
		return false;
	}else 
	// check password 2
	if(pass2.value == "" || pass2.value == "Please enter"){
		pass2.style.color = "#e02309";
		pass2.style.border = "1px solid #C00";
		pass2.value = "Please enter";
		alert('Please enter a password');
		return false;
	}else 
	// check passwors match
	if(pass2.value != pass1.value){
		pass1.style.border = "1px solid #C00";
		pass2.style.border = "1px solid #C00";
		message.innerHTML = 'Please ensure password match';
		alert('Passwords do not match');
		return false;
	}else 
	if(document.getElementById('smallCross_pass2').style.display == 'block'){
		pass1.style.border = "1px solid #C00";
		pass2.style.border = "1px solid #C00";
		document.getElementById('errorTxt').innerHTML = 'Please ensure password match';
		alert('Passwords do not match');
		return false;
	}else 
	// check agents
	/*if(isNaN(agents.value) == true || agents.value == ''){
		agents.style.border = "1px solid #C00";
		document.getElementById('errorTxt').innerHTML = 'Please enter a valid number of agents required';
		alert('Please enter agents required');
		return false;
	}else */
	// check code field
	if(codeInput.value == ""){
		codeInput.style.color = "#C00";
		codeInput.style.border = "1px solid #e02309";
		alert('Please enter the code');
		return false;
	}else 
	// check codes match
	if(lowerCode != lowerGen){
		document.getElementById('errorTxt').innerHTML = "Security codes do not match";
		codeInput.style.border = "1px solid #C00";
		alert('Please ensure codes match');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
		//document.getElementById('isAgencyValid').submit();
		return true;	
	}
		
}

function checkVAT(obj){
	var message = document.getElementById('errorTxt');
	if(obj.value.length > 9){
		message.innerHTML = (9 - obj.value.length) + ' characters remaining';
	}
}

function testNumbers(obj, divID) {
	
	if (document.getElementById(obj.id).value.match(/^[0-9 ]*$/ )){
		document.getElementById(divID).innerHTML = '';
		obj.style.border = '1px solid #0199fe';
		return true;
	}else{
		obj.style.border = '1px solid #C00';
		document.getElementById(divID).innerHTML = '<span style="color: #C00;padding-left: 5px;font-size:11px"><strong>Not a number</strong></span>';
		return false;
	}

}

function test_phone_number(obj, divID){
	var pattern = /^[\(\) ][ ][0-9 ]*$/;
	if(obj.value.search(pattern) == -1) //if match failed
	{
		obj.style.border = '1px solid #C00';
		document.getElementById(divID).innerHTML = '<span style="color: #C00;padding-left: 5px;font-size:11px"><strong>Invalid phone number</strong></span>';
		return false;
		
	}else{
		
		document.getElementById(divID).innerHTML = '';
		obj.style.border = '1px solid #0199fe';
		return true;
		
	}
}


function onlyNumbers(evt){
   
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    return (charCode > 31 && (charCode < 48 || charCode > 57)) ? false : true;

}

// agency functions
function populateJobOptions(jobType){
	var params = '?type='+jobType;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('rewrite').innerHTML = xmlhttp.responseText;
			document.getElementById('options_process').style.display = 'none';
			apply_gui();
		}else{
			document.getElementById('rewrite').innerHTML = '';
			document.getElementById('options_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/getOptions.php"+params,true);
	xmlhttp.send();
}

function checkAgencySkill(){
	var sys   = document.getElementById('sap_system');
	var skill = document.getElementById('sap_skill');
	sys.style.border = '1px solid #cccccc';
	skill.style.border = '1px solid #cccccc';
	
	if(sys.value == 'empty' || skill.value == 'empty'){
		return false;
	}else{
		var current_content = document.getElementById('skill_rewrite').innerHTML;
		var params = '?skill='+escape(skill.value)+'&sys='+escape(sys.value);
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('No') == -1){
					document.getElementById('skill_rewrite').innerHTML = xmlhttp.responseText;
					document.getElementById('systems_process').style.display = 'none';
					sys.selectedIndex   = 0;
					skill.selectedIndex = 0;
				}else{
					document.getElementById('systems_process').style.display = 'none';
					document.getElementById('skill_rewrite').innerHTML = current_content;
					document.getElementById('alertTxt').innerHTML = '<p style="color:#C00;">No more skill sets allowed</p><p style="text-align:center;font-weight:normal;color:#666;">You may only add two skill sets per job</p>';
					popup('alert');
				}
			}else{
				document.getElementById('skill_rewrite').innerHTML = '';
				document.getElementById('systems_process').style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/addAgencySkiil_and_sys.php"+params,true);
		xmlhttp.send();
	}
	
}

function removeSkill(id){
	var sys   = document.getElementById('sap_system');
	var skill = document.getElementById('sap_skill');
	var params = '?id='+id;
	var div = 'skill_id_'+id;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('skill_rewrite').innerHTML = xmlhttp.responseText;
			document.getElementById('systems_process').style.display = 'none';
			sys.selectedIndex   = 0;
			skill.selectedIndex = 0;
			//fadeOut(div, 800);
		}else{
			document.getElementById('skill_rewrite').innerHTML = '';
			document.getElementById('systems_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/removeAgencySkiil_and_sys.php"+params,true);
	xmlhttp.send();
}

function validate_the_date(startDate, endDate){
	
	return (startDate > endDate) ? false : true ;
	
}

function isNewJobValid(today){
	
	var jobType  = document.getElementById('jobType');
	var travel   = document.getElementById('travel');
	var county   = document.getElementById('county');
	var country   = document.getElementById('country');
	var hasSkill = document.getElementById('skill_rewrite');
	var skill    = document.getElementById('sap_skill');
	var sys      = document.getElementById('sap_system');
	var day      = document.getElementById('day');
	var month    = document.getElementById('month');
	var year     = document.getElementById('year');
	var payRate  = document.getElementById('payRate');
	var txt      = document.getElementById('jobTxt');
	var title    = document.getElementById('jobTitleTxt');
	var ref      = document.getElementById('jobRef');
	var status   = document.getElementById('status');
	var message  = document.getElementById('alertTxt');
	
	if(jobType.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please select a job type</p>';
		jobType.style.border = '1px solid red';
		popup('alert');
		return false;
	}
	// check to see if the date is set or if it's asap

	
	
	var skill_sys = document.getElementById('skill_rewrite').innerHTML;
	
	if(checkbox_toggle == 'off'){
		if(payRate.value == 0 || payRate.value == '0'){
			message.innerHTML = '<p style="color:red;text-align:center">Please specify a pay rate</p>';
			payRate.style.border = '1px solid red';
			popup('alert');
			return false;
		}
	}
	
	if(skill_sys == ' ' || skill_sys == 'Please add a skill'){
		message.innerHTML = '<p style="color:red;text-align:center">Please add at least one skill and system</p>';
		popup('alert');
		return false;
	}
	

	if(payRate.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please specify a pay rate</p>';
		payRate.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(travel.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please specify if the candidate has to travel</p>';
		travel.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(county.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please specify the job location</p>';
		county.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(country.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please specify the job location</p>';
		country.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(hasSkill.innerHTML == ''){
		message.innerHTML = '<p style="color:red;text-align:center">Please add at least one skill set</p>';
		skill.style.border = '1px solid red';
		sys.style.border   = '1px solid red';
		popup('alert');
		return false;
	}
	else 
	if(title.value == ''){
		message.innerHTML = '<p style="color:red;text-align:center">Please enter a job title</p>';
		title.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(ref.value == ''){
		message.innerHTML = '<p style="color:red;text-align:center">Please enter a job reference</p>';
		ref.style.border = '1px solid red';
		popup('alert');
		return false;
	}else 
	if(txt.value == ''){
		message.innerHTML = '<p style="color:red;text-align:center">Please add a job description</p>';
		popup('alert');
		return false;
	}else 
	if(status.value == 'empty'){
		message.innerHTML = '<p style="color:red;text-align:center">Please select a job status</p>';
		status.style.border = '1px solid red';
		popup('alert');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
	}
	
}

function isBlogValid(){
	
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	var title         = document.getElementById('blogTitle');
	var txt           = document.getElementById('blogTxt');
	
	if(title.value == ""){
		
		messageHeader.innerHTML = 'Error!';
		message.innerHTML = 'Please enter a title for your blog';
		title.style.border = '1px solid red';
		popup('alert');
		return false;
		
	}else if (txt.value == ""){
		
		messageHeader.innerHTML = 'Error!';
		message.innerHTML = 'Please enter some text for your blog';
		popup('alert');
		return false;
		
	}else{
		
		document.getElementById('submitID')	.value = 421;
		return true;
		
	}
	
}

function doDeleteBlog(id){
	var params = '?delete_id='+id;
	location.href='agency_edit_blog.php'+params;
}

function isEditJobValid(today){
	
	var travel        = document.getElementById('travel');
	var country       = document.getElementById('country');
	var county        = document.getElementById('county');
	var day           = document.getElementById('day');
	var month         = document.getElementById('month');
	var year          = document.getElementById('year');
	var payRate       = document.getElementById('payRate');
	var txt           = document.getElementById('jobTxt');
	var status        = document.getElementById('status');
	var title_txt     = document.getElementById('jobTitleTxt');
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	messageHeader.innerHTML = 'Error!';
	//var e_day         = document.getElementById('e_day');
	//var e_month       = document.getElementById('e_month');
	//var e_year        = document.getElementById('e_year');
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(status.value == 'live'){
		
		var passed_month = (month.selectedIndex +1);
		//passed_month = (passed_month < 10) ? '0'+passed_month : passed_month ;
		var passed_day = (day.value < 10) ? '0' + day.value : day.value;
		
		var passed_in_date = passed_month+'-'+passed_day+'-'+year.value;
		
		// if the job is live and the date is set then run date check
		if(date_checkbox_toggle == 'off'){
			
			if(passed_in_date >= today){
				//alert('proceed');	
			}else{
				//alert('this would be an error');
				message.innerHTML = '<p style="color:red;text-align:center">Start date cannot be in the past</p>';
				day.style.border = '1px solid red';
				month.style.border = '1px solid red';
				year.style.border = '1px solid red';
				popup('alert');
				return false;
			}
			
		}
	
	}
	
	
	if(status.value == 'live' || status.value == 'draft'){
		
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if(checkbox_toggle == 'off'){
			if(payRate.value == 0 || payRate.value == '0'){
				message.innerHTML = '<p style="color:red;text-align:center">Please specify a pay rate</p>';
				payRate.style.border = '1px solid red';
				popup('alert');
				return false;
			}
		}
		
		if(day.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please select a start day</p>';
			day.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(month.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please select a start month</p>';
			month.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(year.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please select a start year</p>';
			year.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(payRate.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please specify a pay rate</p>';
			payRate.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(travel.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please specify if the candidate has to travel</p>';
			travel.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(county.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please specify the job location</p>';
			county.style.border = '1px solid red';
			popup('alert');
			return false;
		}else  
		if(country.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please specify the job location</p>';
			country.style.border = '1px solid red';
			popup('alert');
			return false;
		}else  
		if(title_txt.value == ""){
			message.innerHTML = '<p style="color:red;text-align:center">Please enter a job title</p>';
			title_txt.style.border = '1px solid red';
			popup('alert');
			return false;
		}else 
		if(txt.value == ''){
			message.innerHTML = '<p style="color:red;text-align:center">Please add a job description</p>';
			popup('alert');
			return false;
		}else 
		if(status.value == 'empty'){
			message.innerHTML = '<p style="color:red;text-align:center">Please select a job status</p>';
			status.style.border = '1px solid red';
			popup('alert');
			return false;
		}else{
			document.getElementById('submitID').value = 421;
			return true;
		}
	
	}else if(status.value == 'closed'){
		
		document.getElementById('submitID').value = 421;
		return true;
		
	}
	
}

function showTxtCount(str){
	var remaining = 600 - str.length;
	remaining = (remaining <= 0) ? 0 : remaining ;
	document.getElementById('remainingTxt').innerHTML = remaining + ' characters remaining';
}

function isAgencyProfileValid(){
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	var about         = document.getElementById('aboutTxt');
	if(about.value.length > 600){
		messageHeader.innerHTML = '<p>Warning</p>';
		message.innerHTML       = '<p>You may not have more than 600 characters in your about text.</p><p>You currently have <strong>'+about.value.length+'</strong> characters.</p><p>Please ensure you only have 600 characters</p>';
		popup('alert');
		return false;
	}else if(about.value.length < 600){
		document.getElementById('submitID').value = 421;
	}
	
}

function getAgencyImg(){
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('agencyProfileImage').innerHTML = xmlhttp.responseText;
				document.getElementById('f1_upload_form').innerHTML = '<input type="file" name="imgFile" id="imgFile" size="40" /><div id="uploadBtnHolder"><input type="submit" id="uploadBtn" name="uploadBtn" value="Upload" class="greenBtnIni" onmouseover="focusGreenBtn(this)" onmouseout="blurGreenBtn(this)" /></div>';
          		document.getElementById('f1_upload_form').style.display = 'block';  
			}else{
				alert('Error fetching image');
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/get_agency_image.php",true);
	xmlhttp.send();
}

function isOfficesValid(){
	var house         = document.getElementById('house');
	var ad1           = document.getElementById('ad1');
	var ad2           = document.getElementById('ad2');
	var town          = document.getElementById('town');
	var county        = document.getElementById('county');
	var pcode         = document.getElementById('pcode');
	var phone         = document.getElementById('phone');
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	
	if (house.value == "" || house.value == "Please enter"){
		house.style.border = '1px solid red';
		house.style.color = 'red';
		house.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid building number / name</p>';
		popup('alert');
		return false;
	}else if (ad1.value == "" || ad1.value == "Please enter"){
		ad1.style.border = '1px solid red';
		ad1.style.color = 'red';
		ad1.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter address line 1</p>';
		popup('alert');
		return false;
	}else if (town.value == "" || town.value == "Please enter"){
		town.style.border = '1px solid red';
		town.style.color = 'red';
		town.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid town</p>';
		popup('alert');
		return false;
	}else if (county.value == "" || county.value == "Please enter"){
		county.style.border = '1px solid red';
		county.style.color = 'red';
		county.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid county</p>';
		popup('alert');
		return false;
	}else if (pcode.value == "" || pcode.value == "Please enter"){
		pcode.style.border = '1px solid red';
		pcode.style.color = 'red';
		pcode.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid post code</p>';
		popup('alert');
		return false;
	}else if (phone.value == "" || phone.value == "Please enter"){
		phone.style.border = '1px solid red';
		phone.style.color = 'red';
		phone.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid phone number</p>';
		popup('alert');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
		return true;	
	}
	
	
}

var originalHQ = '';

// get function for edit hq
function repopulateHQDiv(){
	originalHQ = document.getElementById('hqDiv').innerHTML;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('hqDiv').innerHTML = xmlhttp.responseText;
				document.getElementById('systems_process').style.display = 'none';
			}else{
				document.getElementById('systems_process').style.display = 'none';
			}
		}else{
			document.getElementById('hqDiv').innerHTML = '';
			document.getElementById('systems_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_hq_form.php",true);
	xmlhttp.send();
	
}
// save function for hq address
function saveHQDetails(){
	var house  = document.getElementById('hq_house');
	var ad1    = document.getElementById('hq_ad1');
	var ad2    = document.getElementById('hq_ad2');
	var town   = document.getElementById('hq_town');
	var county = document.getElementById('hq_county');
	var pcode  = document.getElementById('hq_pcode');
	var phone  = document.getElementById('hq_phone');
	
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	
	if (house.value == "" || house.value == "Please enter"){
		house.style.border = '1px solid red';
		house.style.color = 'red';
		house.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid building number / name</p>';
		popup('alert');
		return false;
	}else if (ad1.value == "" || ad1.value == "Please enter"){
		ad1.style.border = '1px solid red';
		ad1.style.color = 'red';
		ad1.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter address line 1</p>';
		popup('alert');
		return false;
	}else if (town.value == "" || town.value == "Please enter"){
		town.style.border = '1px solid red';
		town.style.color = 'red';
		town.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid town</p>';
		popup('alert');
		return false;
	}else if (county.value == "" || county.value == "Please enter"){
		county.style.border = '1px solid red';
		county.style.color = 'red';
		county.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid county</p>';
		popup('alert');
		return false;
	}else if (pcode.value == "" || pcode.value == "Please enter"){
		pcode.style.border = '1px solid red';
		pcode.style.color = 'red';
		pcode.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid post code</p>';
		popup('alert');
		return false;
	}else if (phone.value == "" || phone.value == "Please enter"){
		phone.style.border = '1px solid red';
		phone.style.color = 'red';
		phone.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid phone number</p>';
		popup('alert');
		return false;
	}else{
	
		var params = '?house='+escape(house.value)+'&ad1='+escape(ad1.value)+'&ad2='+escape(ad2.value)+'&town='+escape(town.value)+'&county='+escape(county.value)+'&pcode='+escape(pcode.value)+'&phone='+escape(phone.value);
		
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){
					document.getElementById('hqDiv').innerHTML = xmlhttp.responseText;
					document.getElementById('systems_process').style.display = 'none';
				}else{
					document.getElementById('systems_process').style.display = 'none';
					document.getElementById('hqDiv').innerHTML = originalHQ; // just set the address back to normal as no changes were made and error handling would be messy
				}
			}else{
				document.getElementById('hqDiv').innerHTML = '';
				document.getElementById('systems_process').style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/save_hq_form.php"+params,true);
		xmlhttp.send();
	
	}
}

function filterResults(){
	// get all values and build an array to pass to ajax page
	// start the params string
	var page = document.getElementById('thisPage').value;
	var params = '?page='+page;
	
	var type_arr = new Array();
	var types = document.filterForm.jobType.length;
	for (i = 0; i < types; i++) {
		if (document.filterForm.jobType[i].checked) {
			type_arr.push(document.filterForm.jobType[i].value);
		}
	}
	if(type_arr.length > 0){
		params = params + '&type_arr=' + type_arr;
	}
	
	if(document.getElementById('filterPayFromContract').value != ""){
		var rangeFromContract = document.getElementById('filterPayFromContract').value;
		params = params + '&rangeFromContract='+rangeFromContract;
	}
	if(document.getElementById('filterPayToContract').value != ""){
		var rangeToContract = document.getElementById('filterPayToContract').value;
		params = params + '&rangeToContract='+rangeToContract;
	}
	if(document.getElementById('filterPayFromPermanent').value != ""){
		var rangeFromPermanent = document.getElementById('filterPayFromPermanent').value;
		params = params + '&rangeFromPermanent='+rangeFromPermanent;
	}
	if(document.getElementById('filterPayToPermanent').value != ""){
		var rangeToPermanent = document.getElementById('filterPayToPermanent').value;
		params = params + '&rangeToPermanent='+rangeToPermanent;
	}
	
	if(document.getElementById('filterLocationDropCountry').value != "empty"){
		var country = document.getElementById('filterLocationDropCountry').value;
		params = params + '&country='+escape(country);
	}
	if(document.getElementById('filterLocationDrop').value != "empty"){
		var county = document.getElementById('filterLocationDrop').value;
		params = params + '&county='+escape(county);
	}
	
	/*if(document.getElementById('filterTravelDrop').value != "empty"){
		var travel = document.getElementById('filterTravelDrop').value;
		params = params + '&travel='+travel;
	}*/
	if(document.getElementById('filterDate').value != "all"){
		var dates = document.getElementById('filterDate').value;
		params = params + '&dateRange='+escape(dates);
	}
	
	var o_options = document.filterForm.options_options.length;
	var options_arr = new Array();
	for (i = 0; i < o_options; i++) {
		if (document.filterForm.options_options[i].checked) {
			options_arr.push(document.filterForm.options_options[i].value);
		}
	}
	if(options_arr.length > 0){
		params = params + '&options_arr=' + options_arr;
	}
	location.href = 'jobs.php'+params;
}


var originalAddress = ''; 
// other offices
function repopulateAddressDiv(id){
	// id being passed is the record id from sstbl_agency_offices which is used for div id too
	originalAddress = document.getElementById('rewite_'+id+'').innerHTML;
	var divID = 'rewite_'+id;
	var processDivID = 'rewite_'+id+'_process';
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById(divID).innerHTML = xmlhttp.responseText;
				document.getElementById(processDivID).style.display = 'none';
			}else{
				document.getElementById(processDivID).style.display = 'none';
			}
		}else{
			document.getElementById(divID).innerHTML = '';
			document.getElementById(processDivID).style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_address_form_by_id.php?id="+id,true);
	xmlhttp.send();
}

function repopulatePhone(id){
	var divID = 'profilePhone';
	var processDivID = 'phone_process';
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById(divID).innerHTML = xmlhttp.responseText;
				document.getElementById(processDivID).style.display = 'none';
			}else{
				document.getElementById(processDivID).style.display = 'none';
			}
		}else{
			document.getElementById(divID).innerHTML = '';
			document.getElementById(processDivID).style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_admin_phone.php?id="+id,true);
	xmlhttp.send();
}

function saveAdminPhone(id){
	var number = document.getElementById('admin_phone').value;
	var params = '?id='+id+'&number='+number;
	var divID = 'profilePhone';
	var processDivID = 'phone_process';
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){
					document.getElementById(divID).innerHTML = xmlhttp.responseText;
					document.getElementById(processDivID).style.display = 'none';
				}else{
					document.getElementById(processDivID).style.display = 'none';
					alert('Failed to update');
				}
			}else{
				document.getElementById(divID).innerHTML = '';
				document.getElementById(processDivID).style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/save_admin_phone.php"+params,true);
		xmlhttp.send();
}

function saveOfficeDetails(id){
	var house  = document.getElementById('office_house_'+id+'');
	var ad1    = document.getElementById('office_ad1_'+id+'');
	var ad2    = document.getElementById('office_ad2_'+id+'');
	var town   = document.getElementById('office_town_'+id+'');
	var county = document.getElementById('office_county_'+id+'');
	var pcode  = document.getElementById('office_pcode_'+id+'');
	var phone  = document.getElementById('office_phone_'+id+'');
	var office_name = document.getElementById('office_name_'+id+'');
	
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	
	if(office_name.value == "" || office_name.value == 'Please enter'){
		office_name.style.border = '1px solid red';
		office_name.style.color = 'red';
		office_name.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter an office name</p>';
		popup('alert');
		return false;
	}else 
	
	if (house.value == "" || house.value == "Please enter"){
		house.style.border = '1px solid red';
		house.style.color = 'red';
		house.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid building number / name</p>';
		popup('alert');
		return false;
	}else if (ad1.value == "" || ad1.value == "Please enter"){
		ad1.style.border = '1px solid red';
		ad1.style.color = 'red';
		ad1.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter address line 1</p>';
		popup('alert');
		return false;
	}else if (town.value == "" || town.value == "Please enter"){
		town.style.border = '1px solid red';
		town.style.color = 'red';
		town.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid town</p>';
		popup('alert');
		return false;
	}else if (county.value == "" || county.value == "Please enter"){
		county.style.border = '1px solid red';
		county.style.color = 'red';
		county.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid county</p>';
		popup('alert');
		return false;
	}else if (pcode.value == "" || pcode.value == "Please enter"){
		pcode.style.border = '1px solid red';
		pcode.style.color = 'red';
		pcode.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid post code</p>';
		popup('alert');
		return false;
	}else if (phone.value == "" || phone.value == "Please enter"){
		phone.style.border = '1px solid red';
		phone.style.color = 'red';
		phone.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid phone number</p>';
		popup('alert');
		return false;
	}else{
		var divID = 'rewite_'+id;
		var processDivID = 'rewite_'+id+'_process';
		var params = '?id='+id+'&house='+escape(house.value)
		+'&ad1='+escape(ad1.value)+'&ad2='+escape(ad2.value)
		+'&town='+escape(town.value)+'&county='+escape(county.value)
		+'&pcode='+escape(pcode.value)+'&phone='+phone.value+'&office_name='+escape(office_name.value);
		
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){
					document.getElementById(divID).innerHTML = xmlhttp.responseText;
					document.getElementById(processDivID).style.display = 'none';
				}else{
					document.getElementById(processDivID).style.display = 'none';
					document.getElementById(divID).innerHTML = originalAddress; // just set the address back to normal as no changes were made and error handling would be messy
				}
			}else{
				document.getElementById(divID).innerHTML = '';
				document.getElementById(processDivID).style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/save_office_form.php"+params,true);
		xmlhttp.send();
	
	}
}

function repopulateAdminPersonalDetails(){
	originalAdminPersonal = document.getElementById('personal_rewrite').innerHTML;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('personal_rewrite').innerHTML = xmlhttp.responseText;
				document.getElementById('personal_process').style.display = 'none';
			}else{
				document.getElementById('personal_process').style.display = 'none';
			}
		}else{
			document.getElementById('personal_rewrite').innerHTML = '';
			document.getElementById('personal_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_admin_personal.php",true);
	xmlhttp.send();
}

function saveAdminDetails(){
	var fname = document.getElementById('admin_fname');
	var lname = document.getElementById('admin_lname');
	var email = document.getElementById('admin_email');
	var phone = document.getElementById('admin_phone');
	var secretA = document.getElementById('secretAnswer');
	var secretQ = document.getElementById('secretQuestion');
	var emailError = document.getElementById('emailInfoTxt');
	
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	
	var params = '?fname='+escape(fname.value)
	+'&lname='+escape(lname.value)
	+'&email='+escape(email.value)
	+'&phone='+escape(phone.value)
	+'&secretQ='+escape(secretQ.value)
	+'&secretA='+escape(secretA.value);
	
	if(emailError.innerHTML.indexOf('Email already registered') != -1 || emailError.innerHTML.indexOf('Invalid email address') != -1){ // -1 = not found
		email.style.border = '1px solid red';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}else 
	
	if (fname.value == "" || fname.value == "Please enter"){
		fname.style.border = '1px solid red';
		fname.style.color = 'red';
		fname.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your first name</p>';
		popup('alert');
		return false;
	}else if(lname.value == "" || lname.value == "Please enter"){
		lname.style.border = '1px solid red';
		lname.style.color = 'red';
		lname.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your last name</p>';
		popup('alert');
		return false;
	}else if(email.value == "" || email.value == "Please enter"){
		email.style.border = '1px solid red';
		email.style.color = 'red';
		email.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your email</p>';
		popup('alert');
		return false;
	}else if(phone.value == "" || phone.value == "Please enter"){
		phone.style.border = '1px solid red';
		phone.style.color = 'red';
		email.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your phone number</p>';
		popup('alert');
		return false;
	}
	else {
		
		var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ;
		
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){
					document.getElementById('personal_rewrite').innerHTML = xmlhttp.responseText;
					document.getElementById('personal_process').style.display = 'none';
				}else{
					document.getElementById('personal_process').style.display = 'none';
				}
			}else{
				document.getElementById('personal_rewrite').innerHTML = '';
				document.getElementById('personal_process').style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/save_admin_personal.php"+params,true);
		xmlhttp.send();
		
	}
	
}

function cancelSaveAdminDetails(){
	
	document.getElementById('personal_rewrite').innerHTML = originalAdminPersonal;
	
}

// agent calls
function repopulateAgentPersonalDetails(){
	originalAdminPersonal = document.getElementById('personal_rewrite').innerHTML;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('personal_rewrite').innerHTML = xmlhttp.responseText;
				document.getElementById('personal_process').style.display = 'none';
			}else{
				document.getElementById('personal_process').style.display = 'none';
			}
		}else{
			document.getElementById('personal_rewrite').innerHTML = '';
			document.getElementById('personal_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_agent_personal.php",true);
	xmlhttp.send();
}

function saveAgentDetails() {
	var fname = document.getElementById('agent_fname');
	var lname = document.getElementById('agent_lname');
	var email = document.getElementById('agent_email');
	var phone = document.getElementById('agent_phone_number');
	var sq = document.getElementById('secretQuestion');
	var sa = document.getElementById('secretAnswer');
	
	var messageHeader = document.getElementById('alertHeader');
	var message       = document.getElementById('alertTxt');
	
	var params = '?fname='+escape(fname.value)+'&lname='+escape(lname.value)+'&email='+escape(email.value)+'&phone='+escape(phone.value)+'&sq='+escape(sq.value)+'&sa='+sa.value;
	
	if (fname.value == "" || fname.value == "Please enter"){
		fname.style.border = '1px solid red';
		fname.style.color = 'red';
		fname.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your first name</p>';
		popup('alert');
		return false;
	}else if(lname.value == "" || lname.value == "Please enter"){
		lname.style.border = '1px solid red';
		lname.style.color = 'red';
		lname.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your last name</p>';
		popup('alert');
		return false;
	}else if(email.value == "" || email.value == "Please enter"){
		email.style.border = '1px solid red';
		email.style.color = 'red';
		email.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your email</p>';
		popup('alert');
		return false;
	}else if(phone.value == "" || phone.value == "Please enter"){
		phone.style.border = '1px solid red';
		phone.style.color = 'red';
		phone.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your phone number</p>';
		popup('alert');
		return false;
	}else if(sq.value == "" || sq.value == "Please enter"){
		sq.style.border = '1px solid red';
		sq.style.color = 'red';
		sq.value = 'Please enter';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter your secret question</p>';
		popup('alert');
		return false;
	}
	else {
	
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){
					document.getElementById('personal_rewrite').innerHTML = xmlhttp.responseText;
					document.getElementById('personal_process').style.display = 'none';
				}else{
					document.getElementById('personal_process').style.display = 'none';
				}
			}else{
				document.getElementById('personal_rewrite').innerHTML = '';
				document.getElementById('personal_process').style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/save_agent_personal.php"+params,true);
		xmlhttp.send();
		
	}
	
}

function editAgent(id){
	var params = '?id='+id;
	document.getElementById('alertHeader').innerHTML = '';
	document.getElementById('alertTxt').innerHTML = '<div id="agent_rewrite"></div><!-- end of aget rewrite --><div id="edit_process" class="process_loader" style="display:none;text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div><!-- end of systems_process -->';
	popup('alert');
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('agent_rewrite').innerHTML = xmlhttp.responseText;
				document.getElementById('edit_process').style.display = 'none';
				$(
					function(){
						$('input').customInput();
					}
				);
			}else{
				document.getElementById('edit_process').style.display = 'none';
			}
		}else{
			document.getElementById('agent_rewrite').innerHTML = '';
			document.getElementById('edit_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/get_agent_options.php"+params,true);
	xmlhttp.send();
}


function updateAgentPermissions(id){
	
	if(document.getElementById('edit_permission_admin').checked == true){
		var admin = 1;
	}else{
		var admin = 0
	}
	var buy = 1;
	if(document.getElementById('edit_permission_post').checked == true){
		var post = 1;
	}else{
		var post = 0;
	}
	
	var params = '?id='+id+'&admin='+admin+'&buy='+buy+'&post='+post;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('agent_rewrite').innerHTML = '<p style="color:green">Agent updated</span>';
				document.getElementById('edit_process').style.display = 'none';
				var t = setTimeout('popup(\'alert\');', 1800);
			}else{
				document.getElementById('edit_process').style.display = 'none';
				document.getElementById('agent_rewrite').innerHTML = 'Error updating';
			}
		}else{
			document.getElementById('agent_rewrite').innerHTML = '';
			document.getElementById('edit_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/save_agent_options.php"+params,true);
	xmlhttp.send();
}

function deleteAgent(id){
	document.getElementById('alertHeader').innerHTML = '<p>Warning</p>';
	document.getElementById('alertTxt').innerHTML = '<p style="padding:20px 0">You are about to delete this agent</p><div style="text-align:center"><input type="button" name="deleteAgentBtn" id="deleteAgentBtn" value="Yes, remove agent" class="greenBtnIni" onmouseover="focusGreenBtn(this)" onmouseout="blurGreenBtn(this)" onclick="doDeleteAgent('+id+')" /> <span style="padding:5px 10px 0 10px;margin-left:20px;font-weight:bold;cursor:pointer" onclick="popup(\'alert\')">Cancel</span></div>';
	popup('alert');
}

function doDeleteAgent(id){
	popup('alert');
	var params = '?id='+id; // passed in from function above
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('delete_process').style.display = 'none';
				document.getElementById('currentAgentsHolder').innerHTML = xmlhttp.responseText;
			}else{
				document.getElementById('delete_process').style.display = 'none';
				document.getElementById('currentAgentsHolder').innerHTML = '<div class="none" style="padding:40px 0 20px 0;">Whoops, something went wrong</div>';
			}
		}else{
			document.getElementById('currentAgentsHolder').innerHTML = '<div id="delete_process" class="process_loader" style="display:none;text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div><!-- end of systems_process -->';
			document.getElementById('delete_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/delete_agent.php"+params,true);
	xmlhttp.send();

}

function isNewAgentValid(){
	
	var username = document.getElementById('agentUsername');
	var usernameCheck = document.getElementById('smallCross');
	var passwordCheck = document.getElementById('smallCross_pass2');
	var password1 = document.getElementById('new_member_pass');
	var password2 = document.getElementById('new_member_pass2');
	var email = document.getElementById('email');
	var emailCheck = document.getElementById('smallCross_email');
	var messageHeader = document.getElementById('alertHeader');
	var message = document.getElementById('alertTxt');
	
	if(username.value == "" || username.value == "Please enter"){
		username.value = 'Please enter';
		username.style.border = '1px solid red';
		username.style.color = 'red';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid username</p>';
		popup('alert');
		return false;
	}
	else if(usernameCheck.style.display == 'block'){
		username.value = 'Please enter';
		username.style.border = '1px solid red';
		username.style.color = 'red';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Username already taken</p><p>Please enter a valid username</p>';
		popup('alert');
		return false;
	}
	else if(passwordCheck.style.display == 'block'){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Passwords do not match</p>';
		popup('alert');
		return false;
	}
	else if(password1.value != password2.value){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Passwords do not match</p>';
		popup('alert');
		return false;
	}
	else if(password1.value == "" || password2.value == ""){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please confirm your password</p>';
		popup('alert');
		return false;
	}
	else if(email.value == "" || email.value == "Please enter"){
		email.value = 'Please enter';
		email.style.border = '1px solid red';
		email.style.color = 'red';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}
	else if(emailCheck.style.display == 'block'){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}
	else{
	
		document.getElementById('submitID').value = 421;
		
	}
	
}

function isEditAgentValid(){
	
	var usernameCheck = document.getElementById('smallCross');
	var passwordCheck = document.getElementById('smallCross_pass2');
	var email = document.getElementById('email');
	var emailCheck = document.getElementById('smallCross_email');
	var messageHeader = document.getElementById('alertHeader');
	var message = document.getElementById('alertTxt');
	
	if(passwordCheck.style.display == 'block'){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Passwords do not match</p>';
		popup('alert');
		return false;
	}
	else if(email.value == "" || email.value == "Please enter"){
		email.value = 'Please enter';
		email.style.border = '1px solid red';
		email.style.color = 'red';
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}
	else if(emailCheck.style.display == 'block'){
		messageHeader.innerHTML = '<p>Form Error</p>';
		message.innerHTML = '<p>Please enter a valid email address</p>';
		popup('alert');
		return false;
	}
	else{
	
		document.getElementById('submitID').value = 421;
		
	}
	
}



function sendMessage(userid, url){
	//var message = document.getElementById('messageTxt_'+userid+'WidgIframe').contentWindow.document.body.innerHTML;
	var message = document.getElementById('messageTxt_'+userid+'').value;
	var user_email = document.getElementById('user_email_'+userid+'').value;
	var agency = document.getElementById('agencyName').value;
	var jobID = document.getElementById('jobID').value;		
	var jobType = document.getElementById('jobType').value;	
	var candidateName = document.getElementById('candidateName_'+userid+'').value;	
	var agentID = document.getElementById('agentID').value;
	var subject = document.getElementById('subject').value;	
	var encrypted_id = document.getElementById('encrypted_JobID').value;
	var job_ref = document.getElementById('the_job_ref').value;					
	
	var params = '?userID='+userid
	+'&agentID='+agentID
	+'&candidateID='+userid
	+'&candidate='
	+escape(candidateName)
	+'&message='+escape(message)
	+'&user_email='+escape(user_email)
	+'&agency='+escape(agency)
	+'&jobID='+escape(jobID)
	+'&jobType='+escape(jobType)
	+'&url='+escape(url)
	+'&subject='+escape(subject)
	+'&encrypted_id='+encrypted_id
	+'&job_ref='+job_ref;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('messageForm_'+userid+'').innerHTML = xmlhttp.responseText;
				document.getElementById('message_process_'+userid+'').style.display = 'none';
				document.getElementById('messageTitle_'+userid+'').innerHTML = 'Success';
			}else{
				document.getElementById('message_process_'+userid+'').style.display = 'none';
				document.getElementById('messageForm_'+userid+'').innerHTML = '<p>Whoops, something went wrong</p>';
			}
		}else{
			document.getElementById('messageForm_'+userid+'').innerHTML = '';
			document.getElementById('message_process_'+userid+'').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/send_approach_message.php"+params,true);
	xmlhttp.send();
	
}

function sendEnquireMessage(){
	var message = document.getElementById('enquireTxtWidgIframe').contentWindow.document.body.innerHTML;
	var user_email = document.getElementById('enquire_user_email').value;
	var agency = document.getElementById('enquire_agencyName').value;
	var jobID = document.getElementById('enquire_jobID').value;		
	var candidateName = document.getElementById('enquire_candidateName').value;	
	var agencyID = document.getElementById('enquire_agencyID').value;
	var jobTitle = document.getElementById('enquire_theJobTitle').value;
	var user_id = document.getElementById('enquire_userID').value;	
	var subject = document.getElementById('enquire_subject').value;
	var ref = document.getElementById('enquire_job_ref').value;
	var ownerID = document.getElementById('enquire_jobOwnerID').value;
	var params = '?agencyID='+agencyID+
	'&candidate='+escape(candidateName)+
	'&message='+escape(message)+
	'&user_email='+escape(user_email)+
	'&agency='+escape(agency)+
	'&jobID='+escape(jobID)+
	'&jobTitle='+escape(jobTitle)+
	'&userID='+user_id+
	'&subject='+escape(subject)+
	'&ref='+escape(ref)+
	'&ownerID='+ownerID;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('enquire_messageForm').innerHTML = xmlhttp.responseText;
				document.getElementById('enquire_message_process').style.display = 'none';
				document.getElementById('enquire_messageTitle').innerHTML = 'Success';
			}else{
				document.getElementById('enquire_message_process').style.display = 'none';
				document.getElementById('enquire_messageForm').innerHTML = '<p>Whoops, something went wrong</p>';
			}
		}else{
			document.getElementById('enquire_messageForm').innerHTML = '';
			document.getElementById('enquire_message_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/send_enquire_message.php"+params,true);
	xmlhttp.send();
}

function sendApplyMessage(owner_id, allow_apply){
	
	if(allow_apply == 0){
		alert('Your Online SAP profile CV is not complete.  \n\nBefore you can apply for this job you should complete your profile so the employer can review your CV properly');
		return false;	
	}
	
	var message = document.getElementById('messageTxtWidgIframe').contentWindow.document.body.innerHTML;
	var user_email = document.getElementById('user_email').value;
	var agency = document.getElementById('agencyName').value;
	var jobID = document.getElementById('jobID').value;		
	var candidateName = document.getElementById('candidateName').value;	
	var agencyID = document.getElementById('agencyID').value;
	var jobTitle = document.getElementById('theJobTitle').value;
	var user_id = document.getElementById('userID').value;	
	var subject = document.getElementById('subject').value;
	
	var params = '?agencyID='+agencyID+
	'&ownerID='+owner_id+
	'&candidate='+escape(candidateName)+
	'&message='+escape(message)+
	'&user_email='+escape(user_email)+
	'&agency='+escape(agency)+
	'&jobID='+escape(jobID)+
	'&jobTitle='+escape(jobTitle)+
	'&userID='+user_id+
	'&subject='+escape(subject);
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('messageForm').innerHTML = xmlhttp.responseText;
				document.getElementById('message_process').style.display = 'none';
				document.getElementById('messageTitle').innerHTML = 'Success';
				document.getElementById('buttons').innerHTML = '<div class="applied">You have already applied for this job </div>'
			}else{
				document.getElementById('message_process').style.display = 'none';
				document.getElementById('messageForm').innerHTML = '<p>Whoops, something went wrong</p>';
			}
		}else{
			document.getElementById('messageForm').innerHTML = '';
			document.getElementById('message_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/send_apply_message.php"+params,true);
	xmlhttp.send();
	
}

function sendOfferMessage(job_id){
	alert('function only half built');
	return false;
	var message = document.getElementById('messageTxt_'+job_id+'').value;
	var candidateName = document.getElementById('candidateName').value;	
	var agencyID = document.getElementById('agencyID').value;						
	
	var params = '?agencyID='+agencyID+'&candidateID='+userid+'&candidate='+escape(candidateName)+'&message='+escape(message)+'&user_email='+escape(user_email)+'&agency='+escape(agency)+'&jobID='+escape(jobID)+'&jobType='+escape(jobType);
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('messageForm_'+userid+'').innerHTML = xmlhttp.responseText;
				document.getElementById('message_process_'+userid+'').style.display = 'none';
				document.getElementById('messageTitle_'+userid+'').innerHTML = 'Success';
			}else{
				document.getElementById('message_process_'+userid+'').style.display = 'none';
				document.getElementById('messageForm_'+userid+'').innerHTML = '<p>Whoops, something went wrong</p>';
			}
		}else{
			document.getElementById('messageForm_'+userid+'').innerHTML = '';
			document.getElementById('message_process_'+userid+'').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/send_offer_message.php"+params,true);
	xmlhttp.send();
	
}

function isMessageValid(){
	var txt = document.getElementById('messageTxt').value;
	if(txt == '' || txt == 'Post a new message...'){
		document.getElementById('alertTxt').innerHTML = '<p style="color:red:text-align:center">Please enter a message</p>';
		popup('alert');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
		return true;
	}
}

function markMessages(){
	// all we do here is collect data, no actions are taken other than GUI
	// clear any old id's from the global scope
	checkedMessages = [];
	// first active options
	document.getElementById('unreadBtn').disabled = false;
	document.getElementById('archiveBtn').disabled = false;
	
	// for ie
	document.getElementById('unreadBtn').setAttribute('className', 'blueBtnIni');
	document.getElementById('archiveBtn').setAttribute('className', 'blueBtnIni');
	
	// for all other compitent browsers
	document.getElementById('unreadBtn').setAttribute('class', 'blueBtnIni');
	document.getElementById('archiveBtn').setAttribute('class', 'blueBtnIni');
	
	
	// now loop checkboxes
	var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;
	for (var i = 0; i < inputs.length; i++) {
	  if (inputs[i].type == "checkbox") {
		if (inputs[i].checked) {
		  checkedMessages.push(inputs[i].value);
		}
	  }
	}
	var nbChecked = checkedMessages.length; //number of checked checkboxes
	//alert(nbChecked);
	// if none are selected then deactivate buttons
	if(nbChecked == 0){
		document.getElementById('unreadBtn').disabled = true;
		document.getElementById('archiveBtn').disabled = true;
		// for ie
		document.getElementById('unreadBtn').setAttribute('className', 'removeBtnDisabled');
		document.getElementById('archiveBtn').setAttribute('className', 'removeBtnDisabled');
		// for all other compitent browsers
		document.getElementById('unreadBtn').setAttribute('class', 'removeBtnDisabled');
		document.getElementById('archiveBtn').setAttribute('class', 'removeBtnDisabled');
	}
}

function toggleCheckBoxes(){
	var inputs = document.getElementsByTagName("input"); 
	for (var i = 0; i < inputs.length; i++) {  
		if (inputs[i].type == "checkbox") {  
			if(inputs[i].checked == true){
				uncheckAll();
				markMessages();
				//document.getElementById('unreadBtn').addEventListener('onmouseover', function (e){focusBlueBtn(document.getElementById('unreadBtn'))}, true);
				//document.getElementById('archiveBtn').addEventListener('onmouseover', function (e){focusBlueBtn(document.getElementById('archiveBtn'))}, true);
			}else{
				checkAll();	
				markMessages();
			}
		} 
	}
	
}

function markUnreadMessages(){
	// so we should have all our id's in the global scope checkedMessages at this point.
	// we're going to create a form then submit the page back to itself were it will preform the action
	// same happens below when we archive
	//alert('message id\'s to mark as unread: '+checkedMessages);
	var submitForm = document.createElement("form");
	document.body.appendChild(submitForm);
	submitForm.setAttribute('method',"POST");
	submitForm.setAttribute('id',"unreadForm");
	for(i=0;i<checkedMessages.length;i++){
		var formFieldName = "toUnread_"+checkedMessages[i];
		//alert(formFieldName);
		var newElement = document.createElement('input');
		newElement.setAttribute('type', 'hidden');
		newElement.setAttribute('name', formFieldName);
		newElement.setAttribute('value', checkedMessages[i]);
		submitForm.appendChild(newElement);
		//createNewFormElement(submitForm, formFieldName, checkedMessages[i]); // field name | field value
		// alert('created element in loop');
	}
	var newElement = document.createElement("input");
	newElement.setAttribute('type', 'hidden');
	newElement.setAttribute('name', 'unreadSubmitID');
	newElement.setAttribute('value', 421);
	submitForm.appendChild(newElement);
	//createNewFormElement(submitForm, "unreadSubmitID", 421);
	submitForm.setAttribute('action', "message_manager.php");
	submitForm.submit();
	
}
function archiveMessages(){
	var submitForm = document.createElement("form");
	document.body.appendChild(submitForm);
	submitForm.setAttribute('method',"POST");
	submitForm.setAttribute('id',"archiveForm");
	for(i=0;i<checkedMessages.length;i++){
		var formFieldName = "toArchive_"+checkedMessages[i];
		//alert(formFieldName);
		var newElement = document.createElement('input');
		newElement.setAttribute('type', 'hidden');
		newElement.setAttribute('name', formFieldName);
		newElement.setAttribute('value', checkedMessages[i]);
		submitForm.appendChild(newElement);
		//createNewFormElement(submitForm, formFieldName, checkedMessages[i]); // field name | field value
		// alert('created element in loop');
	}
	var newElement = document.createElement("input");
	newElement.setAttribute('type', 'hidden');
	newElement.setAttribute('name', 'archiveSubmitID');
	newElement.setAttribute('value', 421);
	submitForm.appendChild(newElement);
	//createNewFormElement(submitForm, "unreadSubmitID", 421);
	submitForm.setAttribute('action', "message_manager.php");
	submitForm.submit();
}

function manageThisJob(user_id,job_id,status,agency_id, urlFrom){				
	
	var params = '?user_id='+user_id+'&job_id='+job_id+'&status='+escape(status)+'&agency_id='+agency_id;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			location.href = 'view_job.php?id='+job_id+'&urlFrom='+urlFrom;
		}
	}
	xmlhttp.open("GET","includes/ajax/manageJob.php"+params,true);
	xmlhttp.send();
}

function manageThisJobFromAgency(user_id,job_id,encrypted_job_id,status,agency_id){				
	
	var params = '?user_id='+user_id+'&job_id='+job_id+'&status='+escape(status)+'&agency_id='+agency_id;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			location.href = 'agency_candidates.php?id='+encrypted_job_id;
		}
	}
	xmlhttp.open("GET","includes/ajax/manageJob.php"+params,true);
	xmlhttp.send();
}


function openProfile_and_shortlist(user_id, agency_id, agent_id, job_id,url){
	var params = '?user_id='+user_id+'&job_id='+job_id+'&agency_id='+agency_id+'&agent_id='+agent_id;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				//location.hef = 'view_job.php?id='+job_id+'&urlFrom='+url;
			}else{
				//location.hef = 'view_job.php?id='+job_id+'&urlFrom='+url;;
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/open_and_list.php"+params,true);
	xmlhttp.send();
}

function changeJobView(str){
	location.href = 'jobs_manager.php?view='+str;
}
function filterNetwork(page){
	
	var skill = document.getElementById('skill').value;
	var job_type = document.getElementById('job_type').value;
	if(document.getElementById('filterName')){
		var name = document.getElementById('filterName').value;
	}else{
		name = '';	
	}
	
	var params = '?name='+escape(name)+'&job_type='+escape(job_type)+'&skill='+escape(skill)+'&page='+page;
	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('networkList').innerHTML = xmlhttp.responseText;
				appplyUI();
			}else{
				document.getElementById('networkList').innerHTML = '<div class="process_loader" style="text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/network.filter.php"+params,true);
	xmlhttp.send();
	
}

function sendMassMessage(){
	
	// check message text is not empty
	var txt = document.getElementById('messageTxtMassWidgIframe').contentWindow.document.body.innerHTML;
	if(txt == '' || txt.length <= 14){
		document.getElementById('messageErrorTxt').innerHTML = 'Please enter some text';
		return false;
	}
	// grab all selected members
	var inputs = document.getElementsByTagName("input"); 
	var checked_arr = []; //will contain all checked checkboxes
	for (var i = 0; i < inputs.length; i++) {
	  if (inputs[i].type == "checkbox" && inputs[i].name == 'selectedNetwork') {
		if (inputs[i].checked) {
		  checked_arr.push(inputs[i].id);
		}
	  }
	}
	// check at least one member has been selected
	if(checked_arr.length == 0){
		document.getElementById('messageErrorTxt').innerHTML = 'Please select at least one recipient';
		document.getElementById('networkList').style.border = '1px solid red';
		return false;
	}
	
	if(document.getElementById('messageSubject').value == ''){
		document.getElementById('messageErrorTxt').innerHTML = 'Please enter a message subject';
		document.getElementById('messageSubject').style.border = '1px solid red';
		return false;
	}
	
	// fire ajax
	var params = '?message='+escape(txt)+'&checked_arr='+checked_arr+'&subject='+escape(document.getElementById('messageSubject').value);	
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('messageForm').innerHTML = '<div style="text-align:center;padding:20px 0"><p>Message Sent</p><p>Please wait while you are redirected...</p></div>';
				var t = setTimeout("location.href='message_manager.php'", 1500);
			}else{
				document.getElementById('messageForm').innerHTML = '<p>Opps, something went wrong...</p><p>Click <span style="cursor:pointer" onclick="resetMassMessage()">here</span> to try again</p>';
			}
		}else{
			document.getElementById('messageForm').innerHTML = '<div class="process_loader" style="text-align:center"><div>Sending</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';	
		}
	}
	xmlhttp.open("GET","includes/ajax/message.send.php"+params,true);
	xmlhttp.send();
	
}

function resetMassMessage(){
	document.getElementById('messageForm').innerHTML = '' 
	+ '<div class="popInfoTxt">'
	+ '<label><img src="imgs/smallBlueArrow.png" width="7" height="9" border="0" alt="" /> Message:</label><br/>'
	+ '<textarea id="messageTxt" name="messageTxt" style="width:730px; height:150px" onfocus="focusTxt(this)" onblur="blurTxt(this)"></textarea>'
	+ '</div><!-- end of popInfoTxt -->'
	
	+ '<div align="right" style="padding: 10px 0">'
	+ '<div class="right">'
    + '<input type="image" id="sendBtn" name="sendBtn" src="imgs/sendMessageBtn.png" value="Send message" onclick="sendMassMessage();return false;" />'
	+ '</div>'
    + '<div class="right" id="messageErrorTxt" style="color:#C00;font-weight:bold;padding: 10px 20px 0 0"></div></div>';
}

//////////////////////////////////////////////////  Fade Up Functions  ////////////////////////////////////////////////////////////////////
function fadeOut(thisId, speed) {
    var thisE = document.getElementById(thisId);
    thisE.style.zoom = 1; //needed for IE
    speed = speed/20;
    var i = 100;
    var intervalId = setInterval(function() {
        if(i>=0) {
            thisE.style.opacity = i/100;
            thisE.style.filter = 'alpha(opacity='+i+')';
            i -= 5;
        } else {
            setTimeout(function() {thisE.style.display = "none";}, speed);
            clearInterval(intervalId);
            return false;
        }
    }, speed);
}
function fadeIn(thisId, speed) {
    var thisE = document.getElementById(thisId);
    thisE.style.display = "block";
    thisE.style.zoom = 1; //needed for IE
    thisE.style.opacity = 0;
    thisE.style.filter = "alpha(opacity = 0)";
    speed = speed/20;
    var i = 0;
    var intervalId = setInterval(function() {
        if(i <= 100) {
            thisE.style.opacity = i/100;
            thisE.style.filter = 'alpha(opacity='+i+')';
            i += 5;
        } else {
            clearInterval(intervalId);
            return false;
        }
    }, speed);
}
function openFadeUp(divID){
	if(typeof window.innerWidth!='undefined'){
		viewportwidth=window.innerHeight;
	}
	else{
		viewportwidth=document.documentElement.clientHeight;
	}
	if((viewportwidth>document.body.parentNode.scrollWidth)&&(viewportwidth>document.body.parentNode.clientWidth)){
		window_width=viewportwidth;
	}
	else{
		if(document.body.parentNode.clientWidth>document.body.parentNode.scrollWidth){
			window_width=document.body.parentNode.clientWidth;
		}
		else{
			window_width=document.body.parentNode.scrollWidth;
		}
	}
	
	
	var fadeDiv=document.getElementById(divID);
	window_width = window_width/2-200;
	fadeDiv.style.left = window_width+'px';
	
	fadeIn(divID, 500);
	var f = setTimeout("fadeOut('"+divID+"', 500)", 4000);
}
//////////////////////////////////////////////////  popup functions  //////////////////////////////////////////////////////////////////////
function toggle(div_id){
	var el=document.getElementById(div_id);
	if(el.style.display=='none'){
		el.style.display='block';
	}
	else{el.style.display='none';}
}
function blanket_size(popUpDivVar){
	/*
	if(typeof window.innerWidth!='undefined'){
		var viewportheight=window.innerHeight;
	}
	else{
		var viewportheight=document.documentElement.clientHeight;
	}
	if((viewportheight>document.body.parentNode.scrollHeight)&&(viewportheight>document.body.parentNode.clientHeight)){
		var blanket_height=viewportheight;
	}
	else{
		if(document.body.parentNode.clientHeight>document.body.parentNode.scrollHeight){
			blanket_height=document.body.parentNode.clientHeight;
		}
		else{
			blanket_height=document.body.parentNode.scrollHeight;
		}
	}*/
	
	var _docHeight = xDocSize();
//alert(_docHeight);
	blanket_height=_docHeight;//$(document).height();
	var blanket=document.getElementById('blanket');
	blanket.style.height=blanket_height+'px';
	var popUpDiv=document.getElementById(popUpDivVar);
	
	// 100 is half the height of the div
	popUpDiv_height=document.body.parentNode.clientHeight/2-50;
	popUpDiv.style.top=popUpDiv_height+'px';
	
}
function xDocSize()
{
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
  //return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
  return Math.max(esh,eoh,bsh,boh);
}

// general pop up function
function window_pos(popUpDivVar){
	if(typeof window.innerWidth!='undefined'){
		var viewportwidth=window.innerHeight;
	}
	else{
		var viewportwidth=document.documentElement.clientHeight;
	}
	if((viewportwidth>document.body.parentNode.scrollWidth)&&(viewportwidth>document.body.parentNode.clientWidth)){
		var window_width=viewportwidth;
	}
	else{
		if(document.body.parentNode.clientWidth>document.body.parentNode.scrollWidth){
			var window_width=document.body.parentNode.clientWidth;
		}
		else{
			var window_width=document.body.parentNode.scrollWidth;
		}
	}
	//200 is half popup's width (see below)
	var popUpDiv=document.getElementById(popUpDivVar);
	window_width=window_width/2-200;
	popUpDiv.style.left=window_width+'px';
	if(popUpDiv.id == 'memberRegisterDiv'){
		popUpDiv.style.top = '30px';
	}
	popUpDiv.style.zIndex = '2147483647';
	
}
// info pop up function
function window_pos_info(popUpDivVar){
	//alert('window.innerWidth: '+window.innerWidth);
	var window_width = 0;
	if(typeof window.innerWidth!='undefined'){
		var viewportwidth=window.innerWidth;
		//alert('viewportwidth first: '+viewportwidth);
	}
	else{
		var viewportwidth=document.documentElement.clientWidth;
		//alert('viewportwidth second: '+viewportwidth);
	}
	
	if((viewportwidth>document.body.parentNode.scrollWidth)&&(viewportwidth>document.body.parentNode.clientWidth)){
		window_width=viewportwidth;
		//alert('window_width first setting: '+viewportwidth);
	}else{
		if(document.body.parentNode.clientWidth>document.body.parentNode.scrollWidth){
			window_width=document.body.parentNode.clientWidth;
			//alert('window_width second setting: '+window_width);
		}
		else{
			window_width=document.documentElement.clientWidth;
			//document.body.parentNode.scrollWidth = document.documentElement.clientWidth;
			//alert('window_width third setting: '+window_width);
		}
	}
	//200 is half popup's width (see below)
	var popUpDiv=document.getElementById(popUpDivVar);
	var left_spacer = 0;
	left_spacer=((window_width/2)-375);
	//alert('Window width: '+left_spacer);
	popUpDiv.style.left=left_spacer+'px';
	popUpDiv.style.top='50px';
	popUpDiv.style.zIndex = '2147483647';
	window_width = 0;
	left_spacer = 0;
}
// info pop up function
function window_pos_terms(popUpDivVar){
	if(typeof window.innerWidth!='undefined'){
		var viewportwidth=window.innerHeight;
	}
	else{
		var viewportwidth=document.documentElement.clientHeight;
	}
	if((viewportwidth>document.body.parentNode.scrollWidth)&&(viewportwidth>document.body.parentNode.clientWidth)){
		var window_width=viewportwidth;
	}
	else{
		if(document.body.parentNode.clientWidth>document.body.parentNode.scrollWidth){
			var window_width=document.body.parentNode.clientWidth;
		}
		else{
			var window_width=document.body.parentNode.scrollWidth;
		}
	}
	//200 is half popup's width (see below)
	var popUpDiv=document.getElementById(popUpDivVar);
	window_width=window_width/2-375;
	popUpDiv.style.left=window_width+'px';
	popUpDiv.style.top='50px';
	
}
function popup(windowname){
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);
}
function popupInfo(windowname){
	blanket_size(windowname);
	window_pos_info(windowname);
	toggle('blanket');
	toggle(windowname);
}
function popupTerms(windowname){
	blanket_size(windowname);
	window_pos_terms(windowname);
	toggle('blanket');
	toggle(windowname);
}

function changeTxt(selectedBox){
	var header = document.getElementById('infoHeader');
	var txt    = document.getElementById('infoTxt');
	switch(selectedBox){
		case 'typeInfo':
		header.innerHTML = 'Job Type';
		txt.innerHTML = 'Specify the job type.';
		break;
		
		case 'travelInfo':
		header.innerHTML = 'Travel Options';
		txt.innerHTML = 'Is the candidate required to travel in this position?';
		break;
	}
}

function popupAgency(agency_id){
	document.getElementById('ajax_rewrite').innerHTML = '';
	document.getElementById('closeBtn').style.display = 'block';
	document.getElementById('loading_process').style.display = 'block';
	popupInfo('ajaxPopup');
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('loading_process').style.display = 'none';
				document.getElementById('closeBtn').style.display = 'none';
				document.getElementById('ajax_rewrite').innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/profile.agency.get.php?id="+agency_id,true);
	xmlhttp.send();
}

function prepShortlist(job_id){
	document.getElementById('ajax_rewrite').innerHTML = '';
	document.getElementById('closeBtn').style.display = 'block';
	document.getElementById('loading_process').style.display = 'block';
	var params = '?job_id='+job_id;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				document.getElementById('loading_process').style.display = 'none';
				document.getElementById('closeBtn').style.display = 'none';
				document.getElementById('ajax_rewrite').innerHTML = xmlhttp.responseText;
				applyTips();
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/shortlist.prep.php"+params,true);
	xmlhttp.send();
}

function prepShortlistEmail(job_id, user_id, encrypted_id){
	var inputs = document.getElementsByTagName("input"); 
	var checked_arr = []; //will contain all checked checkboxes
	for (var i = 0; i < inputs.length; i++) {
	  if (inputs[i].type == "checkbox") {
		if (inputs[i].checked) {
		  checked_arr.push(inputs[i].id);
		}
	  }
	}
	if(checked_arr.length >= 1){
		var count = (checked_arr.length == 1) ? '1 PDF' : checked_arr.length+' PDF\'s';
		var holder = '<div style="padding:10px;text-align:center"><p>Generating ' + count + '</p><p>This may take a few moments, please be patient.</p></div>';
	}else{
		var holder = '';	
	}
	document.getElementById('ajax_rewrite').innerHTML = holder;
	document.getElementById('loading_process').style.display = 'block';
	popupInfo('ajaxPopup');
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('closeBtn').style.display = 'none';
			document.getElementById('ajax_rewrite').innerHTML = xmlhttp.responseText;
			document.getElementById('loading_process').style.display = 'none';
		}
	}
	xmlhttp.open("GET","includes/ajax/shortlist.email.prep.php?job_id="+job_id+'&checked_arr='+checked_arr+'&user_id='+user_id+'&encrypted_id='+encrypted_id,true);
	xmlhttp.send();
}


function purgePDF(csv){
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			// all good
		}
	}
	xmlhttp.open("GET","includes/ajax/pergePDF.php?csv="+csv,true);
	xmlhttp.send();
}

function addCandidatesToShortlist(job_id, encrypted_id){
	
	var inputs = document.getElementsByTagName("input"); 
	var checked_arr = []; //will contain all checked checkboxes
	for (var i = 0; i < inputs.length; i++) {
	  if (inputs[i].type == "checkbox") {
		if (inputs[i].checked) {
		  checked_arr.push(inputs[i].id);
		}
	  }
	}
	document.getElementById('alertTxt').innerHTML = '<div style="padding:10px;font-size:12px;color:#666;text-align:center">Working</div><div><img src="imgs/loading19.gif" width="16" height="16" border="0" alt="" /></div>';
	document.getElementById('closeBtn').style.display = 'none';
	popup('alert');
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				// success
				var t = setTimeout('locate_to(\'agency_candidates.php?id='+encrypted_id+'\')',1500);
			}else{
				
				document.getElementById('alertTxt').innerHTML = '<div style="padding:10px;font-size:12px;color:#666;text-align:center">No action taken</div><div></div>';
				document.getElementById('closeBtn').style.display = 'block';
		
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/shortlist.manage.php?id_csv="+checked_arr+'&job_id='+job_id,true);
	xmlhttp.send();	
		
}
function getRecordForDelete(id){
	document.getElementById('closeBtn').style.display = 'none';
	document.getElementById('alertTxt').innerHTML = ''+
	'<div style="padding:10px;font-size:12px;color:#666;text-align:center">Are you sure you want to remove this office?</div>'+
	'<div style="text-align:center">'+
		'<input type="image" src="imgs/cancelBtn.png" style="cursor:pointer" onclick="popup(\'alert\')" hspace="10" />'+
		'<input type="image" src="imgs/prceedBtn.png" style="cursor:pointer" onclick="doDeleteOffice('+id+')" hspace="10" />'+
	'</div>';
	popup('alert');
}
function doDeleteOffice(id){
	document.getElementById('alertTxt').innerHTML = '<div style="padding:10px;font-size:12px;color:#666;text-align:center">Working</div><div><img src="imgs/loading19.gif" width="16" height="16" border="0" alt="" /></div>';
	document.getElementById('closeBtn').style.display = 'none';
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				// success
				var t = setTimeout('locate_to(\'agencyDashboard.php\')',1000);
			}else{
				document.getElementById('alertTxt').innerHTML = ''+
				'<div style="padding:10px;font-size:12px;color:#666;text-align:center">There has been an error whilst trying to remove this office.</div>'+
				'<div style="padding:10px;font-size:12px;color:#666;text-align:center">Please reload this page and try again.</div>';
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/office.manage.php?id="+id,true);
	xmlhttp.send();	
}
function checkAll(){
	
	var inputs = document.getElementsByTagName("input");  
	var cbs = []; //will contain all checkboxes  
	var checked = []; //will contain all checked checkboxes  
	for (var i = 0; i < inputs.length; i++) {  
		if (inputs[i].type == "checkbox") {  
			inputs[i].checked = true; 
		}  
	}
	$('input').customInput();  

}
function uncheckAll(){
	
	var inputs = document.getElementsByTagName("input"); 
	var cbs = []; //will contain all checkboxes  
	var checked = []; //will contain all checked checkboxes  
	for (var i = 0; i < inputs.length; i++) {  
		if (inputs[i].type == "checkbox") {  
			inputs[i].checked = false; 
		}  
	}
	$('input').customInput();  
	
}

function checkAllNetwork(){
	
	var inputs = document.getElementsByTagName("input");  
	var cbs = []; //will contain all checkboxes  
	var checked = []; //will contain all checked checkboxes  
	for (var i = 0; i < inputs.length; i++) {  
		if (inputs[i].type == "checkbox" && inputs[i].name == 'selectedNetwork') {  
			inputs[i].checked = true; 
		}  
	}
	$('input').customInput();  

}
function uncheckAllNetwork(){
	
	var inputs = document.getElementsByTagName("input"); 
	var cbs = []; //will contain all checkboxes  
	var checked = []; //will contain all checked checkboxes  
	for (var i = 0; i < inputs.length; i++) {  
		if (inputs[i].type == "checkbox" && inputs[i].name == 'selectedNetwork') {  
			inputs[i].checked = false; 
		}  
	}
	$('input').customInput();  
	
}

function locate_to(pageName){
	location.href=pageName;	
}

function removeCandidate(id,job_id, encrypted_job_id){
	//document.getElementById('ajax_rewrite').innerHTML = '';
	//document.getElementById('closeBtn').style.display = 'block';
	//document.getElementById('loading_process').style.display = 'block';
	document.getElementById('alertTxt').innerHTML = '<div style="padding:10px;font-size:12px;color:#666;text-align:center">Working</div><div><img src="imgs/loading19.gif" width="16" height="16" border="0" alt="" /></div>';
	document.getElementById('closeBtn').style.display = 'none';
	popup('alert');
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				// success
				// fire another ajax call to get the list again
				//prepShortlist(job_id);
				//document.getElementById('shortlistCount_'+job_id+'').innerHTML = 'Review shortlist ('+xmlhttp.responseText+')';
				location.href='viewShortlist.php?job_id='+encrypted_job_id;
			}
		}
	}
	xmlhttp.open("GET","includes/ajax/shortlist.delete.php?job_id="+job_id+'&member_id='+id,true);
	xmlhttp.send();	
}

function sendContactMessage(){
	document.getElementById('messageSubmitID').value = 421;
	//document.getElementById('messageform').submit();
}
function selectMessage(message_id, user_id, box, time){
	// create an array of elements with class name box (this is to loop over later and set border styles)
	// as IE is shit we have to do a work around becuse it can't manage a simple function!
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('emailPreviewRepeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "emailPreviewRepeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	// loop and set styles
	for(i=0;i<arr.length;i++){
		if(arr[i].id != 'emailDiv_'+message_id){
			if(document.getElementById(arr[i].id).style.background != 'none repeat scroll 0% 0% rgb(212, 226, 251)'){ // meaning is unread
				document.getElementById(arr[i].id).style.background = '#FFF';	
			}
		}
	}
	document.getElementById('emailDiv_'+message_id+'').style.background = '#bed5fe';
	if(document.getElementById('unread_'+message_id+'')){
		document.getElementById('unread_'+message_id+'').src = 'imgs/envelopeOpen.png';
	}
	// now fire ajax to populate reading pane
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('ajax_rewrite').innerHTML = xmlhttp.responseText;
		}else{
			document.getElementById('ajax_rewrite').innerHTML = '<div class="process_loader" style="text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
		}
	}
	xmlhttp.open("GET","includes/ajax/email.read.php?id="+message_id+'&user_id='+user_id+'&box='+box+'&time='+time,true);
	xmlhttp.send();
}

function postMessage(){
	var message = document.getElementById('messageTxt').value;
	var job_id  = document.getElementById('jobID').value;
	var agent_id = document.getElementById('agentID').value;
	var user_id = document.getElementById('userID').value;
	var user_type = document.getElementById('user_type').value;
	var subject = document.getElementById('subject').value;
	
	if(message == ''){
		document.getElementById('alertTxt').innerHTML = 'Please enter a message';
		popup('alert');
		return false;
	}
	//var currentDate = new Date();
	//var milli_time = currentDate.getTime();
	//var time = Math.round((milli_time / 1000));
	//alert(time);
	var params = 'message='+escape(message)+'&job_id='+job_id+'&agent_id='+agent_id+'&user_id='+user_id+'&user_type='+user_type+'&subject='+subject;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			//alert(xmlhttp.responseText);
			//selectMessage(job_id,user_id,'inbox',xmlhttp.responseText);
			location.href = 'message_manager.php';
		}else{
			document.getElementById('ajax_rewrite').innerHTML = '<div class="process_loader" style="text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
		}
	}
	xmlhttp.open("GET","includes/ajax/message.post.php?"+params,true);
	xmlhttp.send();
}

function isClientValid(){
	var company        = document.getElementById('client_company');	
	var contact_name   = document.getElementById('client_contact_name');
	var address_1      = document.getElementById('client_address_1');
	var address_2      = document.getElementById('client_address_2');
	var town           = document.getElementById('client_town');
	var county         = document.getElementById('client_county');
	var post_code      = document.getElementById('client_post_code');
	var phone_number   = document.getElementById('client_phone_number');
	var email          = document.getElementById('client_email');
	var account_number = document.getElementById('client_acc_number');
	
	var arr = Array(company,contact_name,address_1,address_2,town,county,post_code,phone_number,email,account_number);
	var isValid = false;
	
	for(var i = 0; i < arr.length; i++){
		if(arr[i].value == '' && (arr[i].id == 'client_company' || arr[i].id == 'client_email' || arr[i].id == 'client_contact_name')){
			document.getElementById('errorMessage').innerHTML = 'Please enter ' + str_replace(arr[i].id, '_', ' ' );
			arr[i].style.border = '1px solid red';
			return false;
		}else{
			isValid = true;
			document.getElementById('errorMessage').innerHTML = '';
		}
	}
	
	if(isValid == true){
		document.getElementById('clientSubmitID').value = 421;
		return true;
	}
	
}

function str_replace(string, seperator, replacement) {
	var temp = string.split(seperator);
	return temp.join(replacement);
}

function getClient(id,agency_ref){
	
	if(id != 'empty'){
		
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			var data = xmlhttp.responseText;
			var dataObj = eval('(' + data + ')');
			
			for( var i in dataObj ){
				
				for( var key in dataObj[i] ){
					
					if (dataObj[i].hasOwnProperty(key)) {
						
					    if(document.getElementById('client_'+key)){
							
							document.getElementById('client_'+key).value = str_replace(dataObj[i][key], '|', '"');
							
						}
						
					}
					
				}
				
			}
			//document.getElementById('workingTxt').style.visibility = 'hidden';
			document.getElementById('client_id').value = id;
			
		}else{
			//document.getElementById('workingTxt').style.visibility = 'visible';
		}
	}
	xmlhttp.open("GET","includes/ajax/client.get.php?id="+id+'&agency_ref='+agency_ref,true);
	xmlhttp.send();
		
	}else{
		return false;	
	}
	
}

function prepRemoveClient(id, client_name, url){
	
	var header = document.getElementById('alertHeader').innerHTML = '<p style="color:#C))">Confirm Delete</p>';
	var txt = document.getElementById('alertTxt').innerHTML = ''+
	'<p style="color:#333">Are you sure you want to delete <strong>'+client_name+'</strong>?</p>'+
	'<div style="text-align:center">'+
	'<input type="image" src="imgs/prceedBtn.png" value="Proceed" onclick="doDeleteClient('+id+',\''+url+'\')" style="cursor:pointer;margin-right:10px" />'+
	'<input type="image" src="imgs/cancelBtn.png" value="Cancel" onclick="popup(\'alert\');" style="cursor:pointer;margin-left:10px" />'+
	'</div>';
	popup('alert');
	
}
function doDeleteClient(id,url){
	location.href='includes/actions/client.remove.php?id='+id+'&url='+url;
}

function prepRemoveAllCompany(user_id,company_name,url){
	var header = document.getElementById('alertHeader').innerHTML = '<p style="color:#C))">Confirm Delete</p>';
	var txt = document.getElementById('alertTxt').innerHTML = ''+
	'<p style="color:#333">Are you sure you want to delete <strong>'+company_name+'</strong>?</p>'+
	'<p align="center">This will completely remove all contacts for this company.  This cannot be un-done.</p>'+
	'<div style="text-align:center;padding:10px 0;">'+
	'<input type="image" src="imgs/prceedBtn.png" value="Proceed" onclick="doDeleteCompany('+user_id+',\''+company_name+'\',\''+url+'\')" style="cursor:pointer;margin-right:10px" />'+
	'<input type="image" src="imgs/cancelBtn.png" value="Cancel" onclick="popup(\'alert\');" style="cursor:pointer;margin-left:10px" />'+
	'</div>';
	popup('alert');
}
function doDeleteCompany(user_id,company_name,url){
	location.href='includes/actions/client.company.remove.php?user_id='+user_id+'&url='+url+'&comp='+escape(company_name);	
}

function composeMail(){
	
	popupInfo('newMail');
	
}
function sendShortlistEmail(arr,agency_name, job_id, checked_arr, encrypted_id){
	
	var receiverAddress = document.getElementById('client_email');
	var message = document.getElementById('messageTxt');
	var client_namme = document.getElementById('clientList').value;
	
	if(receiverAddress.value == ''){
		document.getElementById('messageErrorTxt').innerHTML = 'Please provide a recipient email address';
		receiverAddress.style.border = '1px solid red';	
		return false;
	}else if(message.value == ""){
		document.getElementById('messageErrorTxt').innerHTML = 'Please add a message';
		message.style.border = '1px solid red';	
		return false;
	}else{
		var params = 'message='+escape(message.value)+
		'&csv='+escape(arr)+
		'&receiverAddress='+
		escape(receiverAddress.value)+
		'&agency_name='+agency_name+
		'&job_id='+job_id+
		'&checked_arr='+checked_arr;
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById('message_ajax_rewrite').innerHTML = ''+
				'<div style="padding:40px 0;text-align:center">'+xmlhttp.responseText+'</div>'+
				'<div align="center" style="padding:20px 0"><img src="../../imgs/closeBoxBtn.png" width="84" height="28" alt="Close" border="0" onclick="refreshShortlist(\''+arr+'\', \''+encrypted_id+'\')" style="cursor:pointer" /></div>';
			
			}else{
				document.getElementById('message_ajax_rewrite').innerHTML = '<div class="process_loader" style="text-align:center"><div>Loading</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
			}
		}
		xmlhttp.open("GET","includes/ajax/shortlist.email.send.php?"+params,true);
		xmlhttp.send();
	}

}

function refreshShortlist(arr, encrypted_id){
	document.getElementById('message_ajax_rewrite').innerHTML = ''+
	'<div style="padding:40px 0;text-align:center"><p>Clearing temporary pdf files</p> <p>Please wait...</p></div>';
	purgePDF(''+arr+'');
	var c = setTimeout("redirect_to_shortlist('"+encrypted_id+"')", 1500);
}

function redirect_to_shortlist(encrypted_id){
	location.href='viewShortlist.php?job_id='+encodeURIComponent(encrypted_id);
}

function getClients(value, user_id){
	if(value == 'empty'){
		return false;
	}else{
		var params = 'company='+value+'&user_id='+user_id;
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById('clientsDropDown').innerHTML = xmlhttp.responseText;
			}else{
				document.getElementById('clientsDropDown').innerHTML = '<div style="text-align:center;padding:5px"><div>Fetching...</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
			}
		}
		xmlhttp.open("GET","includes/ajax/clients.get.dropdown.php?"+params,true);
		xmlhttp.send();
	}
}
function populateClientInfo(value){
	if(value == 'empty'){
		return false;
	}else{
		var params = 'id='+value;
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById('clientEmailInfo').innerHTML = xmlhttp.responseText;
			}else{
				document.getElementById('clientEmailInfo').innerHTML = '<div style="text-align:center;padding:5px"><div>Fetching...</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
			}
		}
		xmlhttp.open("GET","includes/ajax/clients.get.dropdown2.php?"+params,true);
		xmlhttp.send();
	}
	
}

function isCommentValid(){
	if(document.getElementById('comment_message').value == ''){
		alert('Please enter a comment');
		return false;
	}else{
		document.getElementById('submitID').value = 421;
		return true;
	}
}

function get_sub_area(value){
	// value will have underscore instead of space 
	if(value == 'empty'){
		return false;
	}else{
		var params = 'parent='+value;
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById('areaHolder').innerHTML = xmlhttp.responseText;
			}else{
				document.getElementById('areaHolder').innerHTML = '<div style="text-align:center;padding:5px"><div>Fetching...</div><div><img src="imgs/spinner.gif" width="16" height="16" border="0" alt="" /></div></div>';
			}
		}
		xmlhttp.open("GET","includes/ajax/county.get.php?"+params,true);
		xmlhttp.send();
	}
	
}

var checkbox_toggle = 'off';
function blankFields(field_id){
	
	if(checkbox_toggle == 'on'){
		document.getElementById(field_id).style.background = '#FFF';
		document.getElementById(field_id).style.border = '1px solid #ccc';
		document.getElementById(field_id).style.color = '#666';
		document.getElementById(field_id).disabled = '';
		checkbox_toggle = 'off';
		//alert('toggle off\n\n field should be active');
	}else{
		document.getElementById(field_id).style.border = '1px solid #ccc';
		document.getElementById(field_id).style.background = '#CCC';
		document.getElementById(field_id).style.color = '#999';
		document.getElementById(field_id).disabled = 'disabled';
		checkbox_toggle = 'on';
		//alert('toggle on, filed inactive');
	}
	
	
}

var date_checkbox_toggle = 'off';
function toggle_fields(arr){
	
	if(date_checkbox_toggle == 'on'){
		for(i=0;i<arr.length;i++){
			document.getElementById(arr[i]).style.background = '#FFF';
			document.getElementById(arr[i]).style.border = '1px solid #ccc';
			document.getElementById(arr[i]).style.color = '#666';
			document.getElementById(arr[i]).disabled = '';
		}
		date_checkbox_toggle = 'off';
		//alert('toggle off\n\n field should be active');
	}else{
		for(i=0;i<arr.length;i++){
			document.getElementById(arr[i]).style.border = '1px solid #ccc';
			document.getElementById(arr[i]).style.background = '#CCC';
			document.getElementById(arr[i]).style.color = '#999';
			document.getElementById(arr[i]).disabled = 'disabled';
		}
		date_checkbox_toggle = 'on';
		//alert('toggle on, filed inactive');
	}
	
}

function reset_dropDowns(arr){
	for(i=0;i<arr.length;i++){
		document.getElementById(arr[i]).style.background = '#FFF';
		document.getElementById(arr[i]).style.border = '1px solid #ccc';
		document.getElementById(arr[i]).style.color = '#666';
		document.getElementById(arr[i]).disabled = '';
	}
	date_checkbox_toggle = 'off';
}

function checkMemberSkill(){
	var sys   = document.getElementById('sap_system');
	var skill = document.getElementById('sap_skill');
	var expo   = document.getElementById('exposure');
	var cert = document.getElementById('cert');
	sys.style.border = '1px solid #cccccc';
	skill.style.border = '1px solid #cccccc';
	expo.style.border = '1px solid #cccccc';
	cert.style.border = '1px solid #cccccc';
	
	if(sys.value == 'empty' || skill.value == 'empty' || expo.value == 'empty' || cert.value == 'empty'){
		return false;
	}else{
		var current_content = document.getElementById('skill_rewrite').innerHTML;
		var params = '?skill='+escape(skill.value)+'&sys='+escape(sys.value)+'&expo='+escape(expo.value)+'&cert='+escape(cert.value);
		if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
			xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById('skill_rewrite').innerHTML = xmlhttp.responseText;
				document.getElementById('systems_process').style.display = 'none';
				sys.selectedIndex   = 0;
				skill.selectedIndex = 0;
				expo.selectedIndex = 0;
				cert.selectedIndex = 0;
			}else{
				document.getElementById('skill_rewrite').innerHTML = '';
				document.getElementById('systems_process').style.display = 'block';
			}
		}
		xmlhttp.open("GET","includes/ajax/addMemberRegisterSkiil_and_sys.php"+params,true);
		xmlhttp.send();
	}
	
}

function removeMemberSkill(id){
	var sys   = document.getElementById('sap_system');
	var skill = document.getElementById('sap_skill');
	var params = '?id='+id;
	var div = 'skill_id_'+id;
	if (window.XMLHttpRequest){var xmlhttp = new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById('skill_rewrite').innerHTML = xmlhttp.responseText;
			document.getElementById('systems_process').style.display = 'none';
			sys.selectedIndex   = 0;
			skill.selectedIndex = 0;
			//fadeOut(div, 800);
		}else{
			document.getElementById('skill_rewrite').innerHTML = '';
			document.getElementById('systems_process').style.display = 'block';
		}
	}
	xmlhttp.open("GET","includes/ajax/removeMemberSkiil_and_sys.php"+params,true);
	xmlhttp.send();
}

