function ajaxCheckIDNumber(str, id, entityID, counter){
	var ajax_holder = $('#ajax_'+id+'_holder_'+counter);
	
	ajax_holder.addClass('loader');
	ajax_holder.html('');
	var url = base + 'search/ajax2/checkID/';
	$.ajax({
		type: "POST",
		data: "idNum="+str+"&entityID="+entityID,
		url: url,
		success: function(response){
			if(response == 200){
				ajax_holder.removeClass('loader');	
				
				if(!isIdNumber(str)){
					ajax_holder.addClass('error');
					ajax_holder.html('Please fill in a correct ID Number');
					toggleEntitySaveButtons( true );
				}else{
					toggleEntitySaveButtons( false );
				}
		  }
		  if(response == 404){
				toggleEntitySaveButtons( true );
				ajax_holder.removeClass('loader');
				ajax_holder.addClass('error');
				ajax_holder.html( 'There is already a person with this ID Number' );
				
				return false;
		  }
		}
	});
}

function toggleEntitySaveButtons(disable){
	if( disable == null ){ disable = true;
	}
	$('#update_properties').attr("disabled", disable);
	$('#save_properties').attr("disabled", disable);
}

/*13 digit number only*/
function isIdNumber(str) {
	str = str.replace(/^\s+|\s+$/g, '');
	return /^\d{13,13}$/.test(str);
}



/*checks if correct email address is added*/
function isEmail(str) {
	if(str == ''){ return true;}
	return /^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,4})(\.[a-z]{2})?$/i.test(str);
}

/*date format like YYYYmmdd*/
function isDate(str) {
	return /((19|20)\d\d)(0[1-9]|1[012])(0[1-9]|1[0-9]|2[0-9]|3[01])$/i.test(str);
}
/*make text read if incorect*/
function checkAndChangeColor(check, changeColor){
	if(check){
		changeColor.style.color = "#000000";
		return true;
	}else{
		changeColor.style.color = "#FF0000";
		return false;
	}
}
function setRedirectProperty(propertyID , url){
	document.getElementById('e_prop_redirect[' + propertyID + ']').value =  url ;
	document.getElementById('e_prop_redirect[' + propertyID + ']').form.submit();
}

var lastID = null;

function saveProperty(propertyID, entityID, counter){
	$('#ajax_' + propertyID  + '_save_' + counter).html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
	$('#ajax_'+ propertyID +'_save_' + counter).addClass('loader');
	
	var value = document.getElementById('e_prop[' + propertyID + ']').value;
	
	
	//Tick Box Fix
	
	if($('#e_prop[' + propertyID + ']')[0]){
		// Condition added to fix and check element existance
		if( $('#e_prop[' + propertyID + ']')[0].type ==  'checkbox' ){
			if($('#e_prop[' + propertyID + ']')[0].checked == false){
				value = 0;
			}
		}
	}
	
	var url = base + 'search/ajax/editentity/updateProperty/' + propertyID + '/' + entityID;
	$.ajax({
		type: "POST",
		url: url,
		data: 'value=' + value,
		success: function(response){
			if(response != 200){
				alert('There was a problem saving this field');
			}
			
			$('#ajax_'+propertyID+'_save_' + counter).removeClass('loader');
			
		}
	});
	
}

$(document).ready(function() {
	$('select.redirectThis').change(function() {
		id = $(this).attr('id');
		id = id.replace('e_prop','e_prop_redirect');
		doc = document.getElementById(id);
		doc.value = here2;
		doc.form.submit();
	});
	
});



