var CheckTypes = new Array('select', 'input', 'submit', 'textarea', 'file', 'hidden');
var SubmitTypes = new Array();//'Submit', 'Pending', 'Approve', 'Reject');
var skipValid = false;
var formEdited = false;

window.addEvent('domready', function() {
									 
	//REVEAL / HIDE SIGNIN OR FULL REG FORM
	if($('alreadyAMember') != null && $('notAlreadyAMember') != null){
		fullFormBoxEl = $($('alreadyAMember').getAttribute('rel'));
		loginFormBoxEl = $($('notAlreadyAMember').getAttribute('rel'));
		
		loginFormBoxEl.trgtHeight = 1000; //loginFormBoxEl.offsetHeight + 50;
		fullFormBoxEl.trgtHeight = 1000; //fullFormBoxEl.offsetHeight + 50;
		loginFormBoxEl.setStyle('height', 0);
		
		$('loginForm').set('action', '/index.php?class=user&action=login&view=ajax&tmpl=maincomponent');
		$('alreadyAMember').set('href', '#');
		$('alreadyAMember').addEvent('click', function(){
			$('loginForm').reset();
			$('alreadyAMemberBox').set('class', 'hidden');
			$($('notAlreadyAMember').getAttribute('rel')).set('class','');
			$($('notAlreadyAMember').getAttribute('rel')).tween('height', loginFormBoxEl.trgtHeight);
			$($('alreadyAMember').getAttribute('rel')).tween('height', 0);
			return false;
		 });
		$('notAlreadyAMember').set('href', '#');
		$('notAlreadyAMember').addEvent('click', function(){
			$('alreadyAMemberBox').set('class', '');
			$($('alreadyAMember').getAttribute('rel')).tween('height', fullFormBoxEl.trgtHeight);
			$($('notAlreadyAMember').getAttribute('rel')).tween('height', 0);
			$('loginForm').reset();
			return false;
		 });
	}
	
	//REVEAL HIDE FORGOT PASSWORD
	if($('forgotPassword') != null){
		$('forgotPasswordBox').trgtHeight = $('forgotPasswordBox').offSetHeight
		$('loginFormBoxInner').trgtHeight = $('loginFormBoxInner').offSetHeight
		$('forgotPasswordBox').setStyle('overflow', 'hidden');
		$('loginFormBoxInner').setStyle('overflow', 'hidden');
		$('forgotPasswordBox').setStyle('height', 0);
		
		$('forgotPassword').addEvent('click', function(){
			$('loginForm').reset();
			$('forgotpasswordForm').reset();
			$('forgotPasswordBox').tween('height', $('forgotPasswordBox').trgtHeight);
			$('loginFormBoxInner').tween('height', 0);
		});
		
		$('forgotPasswordCancel').addEvent('click', function(){
			$('forgotPasswordBox').tween('height', 0);
			$('loginFormBoxInner').tween('height', $('loginFormBoxInner').trgtHeight);
			$('loginForm').reset();
			$('forgotpasswordForm').reset();
		});
		
		$('forgotpasswordForm').addEvent('submit', function(e){
			e.stop();
			if( checkedForm(this) ){
				var log = $('log_res').empty().addClass('ajax-loading');
				this.set('send', {onComplete: function(response) { 
					log.removeClass('ajax-loading');
					log.set('html', response);
					$('forgotPasswordBox').tween('height', 0);
					$('loginFormBoxInner').tween('height', $('loginFormBoxInner').trgtHeight);
				}});
				this.send();
			}
		});
	}

	//LOGIN AJAX SUBMIT
	if($('loginForm') != null){
		$('loginForm').addEvent('submit', function(e){
			e.stop();
			ajaxLogin(this);
		});
	}
	setCheckForms(document);
});

function ajaxLogin(formObj){
	if( checkedForm(formObj) ){
		var log = $('log_res').empty().addClass('ajax-loading');
		formObj.set('send', {onComplete: function(response) { 
			log.removeClass('ajax-loading');
			log.set('html', response);
			if($('ajaxReturn') != null && $('ajaxReturn').getAttribute('rel') == 'true'){
				if($('signupFormBox') != null){
					fillFormFromAjax('signupForm');
					$('signupFormBox').tween('height', $('signupFormBox').trgtHeight);
					$('loginFormBox').tween('height', 0);
				}
			}
		}});
		formObj.send();
	}
}

function ajaxRemoveItem(targetObj, action, check){
	var log = targetObj;
	proceed = (check) ? confirm("Are you sure you want to delete this item perminently?") : true;

	if(proceed){
		new Request({
		   url: action,
		   method: 'post',
		   onFailure: function(instance){
				// ...
		   },
		   onSuccess: function(response){
				log.removeClass('ajax-loading');		
				if(response != ''){
					if(response.indexOf('<root>') != -1){
						message = tagContents('message', response); 
						status = tagContents('status', response); 
						if(status == true || status == 'true' || parseInt(status) > 0 || message.indexOf('success') != -1){
							log.parentNode.removeChild(log);
						}else{
							alert("Sorry, action failed ("+status+"): "+message);
						}
					}
				}else{
					log.set('html', 'Unknown Failure');
				}
		   }
		}).send();
	}
}

//ajaxLoadHtml(Form (as obj), Target load area (as obj), Action to post to, Set true to not go to page top, Set false to prevent adding content to load area)
function ajaxLoadHtml(formObj, targetObj, action, stayput, addcontent, check, postProcess){
	proceed = (check) ? confirm("Are you sure you want to continue?") : true;
	
	if(formObj && formObj != null){
		formData = formObj.toQueryString();
		var formBox = formObj.name + 'Area';
		if(!action){
			action = window.location.search.substring(0);
		}
	}else{
		formData = '';
	}
	
	if(!addcontent){
		var log = targetObj.empty().removeClass('hidden').addClass('ajax-loading');
	}else{
		var log = targetObj;
	}
	
	new Request({
	   url: action,
	   method: 'post',
	   data: formData,
	   evalScripts: true,
	   onFailure: function(instance){
			// ...
	   },
	   onSuccess: function(response){
			log.removeClass('ajax-loading');		
			if(response != ''){
				if(response.indexOf('<root>') != -1){
					message = tagContents('message', response); 
					status = tagContents('status', response); 
					if(log.id.indexOf('Ajax') == -1){
						log.set('html', log.get('html') + message);
					}else{
						log.empty();
						showMissingInfoError(message);
					}
					if(status == true && $(formBox)){
						$(formBox).addClass('hidden');
					}
				}else{
					log.set('html', log.get('html') + response);
					MOOdalBox.scanAnchors();
				}
			}else{
				log.set('html', 'Unknown Failure - Are you still connected to the internet?');
			}
			if(!stayput){
				window.location = String(window.location).replace(/\#.*$/, "") + "#thetop";
			}		
			if( postProcess && postProcess!=''){
				status = (status) ? true : false;
				eval(postProcess+'('+status+')');
			}
	   }
	}).send();
}

//REGISTRATION FORM AUTO CHECKER
//document.setCheckForms = function(baseObj){
function setCheckForms(baseObj, postProcess){
	docForms = baseObj.getElementsByTagName('form'); //$$('#wrapper form');
	for(f=0;f<docForms.length;f++){
		setForm = $(docForms[f].id);
		if(setForm != null){
			if(setForm.getAttribute('rel') != null && setForm.getAttribute('rel') == 'checkForm'){
				for(i=0;i<SubmitTypes.length;i++){
					if( $(setForm.id + SubmitTypes[i]) != null && $(setForm.id + SubmitTypes[i]).getAttribute('rel') != null ){
						rel = $(setForm.id + SubmitTypes[i]).getAttribute('rel');
						$(setForm.id + SubmitTypes[i]).parentNode.innerHTML = '<div id="' + setForm.id + SubmitTypes[i]+ '" class="'+rel+'Button"><div class="' + rel + 'ButtonInner">&nbsp;</div></div>';
						$(setForm.id + SubmitTypes[i]).stype = SubmitTypes[i];
						$(setForm.id + SubmitTypes[i]).addEvent('click', function(){
							checkForm = $(this.id.split(this.stype).join(''));
							if(checkForm.name.indexOf('login') != -1 && checkForm.action.indexOf('ajax') != -1 ){
								ajaxLogin(checkForm);
							}else {
								if( checkedForm( checkForm ) ) {
									if(checkForm['application[submit_status]']){
										checkForm['application[submit_status]'].value= this.stype.toLowerCase();
									}
									checkForm.submit();
								}
							}
						})
					}
				}
							
				setForm.addEvent('submit', function(e){
					e.stop();
					if( checkedForm(this) ){
						if( this.getAttribute('action').indexOf('ajax') == -1 ){
							this.submit();
						}else{
							//if(this.name.toLowerCase().indexOf('gpform') != -1){
								if(postProcess && postProcess!=''){
									ajaxLoadHtml(this, $('ajaxError'), this.action + '&tmpl=maincomponent', false, false, false, postProcess);
								}else{
									ajaxLoadHtml( this, $('ajaxError'), this.action + '&tmpl=maincomponent');
								}
								//MOOdalBox.close();
							//}
							
						}
					}
				});
				
				setCheckThis(setForm);
				
			}
		}
	}
}

function setCheckThis(setForm){
	
	for(i=0;i<CheckTypes.length;i++){
		formInputs = $$('#' + setForm.id + ' ' + CheckTypes[i]); 
		//formInputs = setForm.getElementsByTagName(CheckTypes[i]);
		for(j=0;j<formInputs.length;j++){
			if( !formInputs[j].hasClass('done') ){
				if(formInputs[j].name.indexOf('patientnote') == -1){
					formInputs[j].addEvent('change', function (){ formEdited = true; });
				}
				formInputs[j].addClass('done');
				rel = (formInputs[j].getAttribute('rel') != null) ? formInputs[j].getAttribute('rel') : '';
				if( (rel.indexOf('c') != -1 || rel.indexOf('if') != -1) && (rel.length < 4 || rel.substr(0, 4) == 'cpc.') ){
					errorImg = formInputs[j].name.split('[').join('_').split(']').join('') + 'Status';
					
					if(formInputs[j].type == 'file'){
						addImageUploader(formInputs[j]);
					}else if(formInputs[j].type == 'checkbox'){
						formInputs[j].parentNode.parentNode.innerHTML += '<div id="'+errorImg+'" class="formStatus"></div>';
					}else if(formInputs[j].type != 'hidden'){
						if(formInputs[j].parentNode){
							formInputs[j].parentNode.innerHTML += '<div id="'+errorImg+'" class="formStatus"></div>';
						}
					}
					formInputs[j] = $$('#' + setForm.id + ' ' + CheckTypes[i])[j];
					if(formInputs[j]!= null){
						if(rel == 'cd' || rel=='cdb'){
							formInputs[j].addEvent('click', function(){	
								scwNextAction=checkThisFormItem.runsAfterSCW(this,this);
								scwShow(this,this,this);
							 });
						}
						formInputs[j].addEvent('blur', function(){
							checkThisFormItem(this);
						});
					}
				}else{
					if(rel == 'd'){
						formInputs[j].addEvent('click', function(){	
							scwNextAction=checkThisFormItem.runsAfterSCW(this,this);
							scwShow(this,this,this);
						 });
					}
				}
			}
		}
	}
	loadPCMSHelpers(setForm, '.formLabel');
}

function checkThisFormItem(formItem){ //pass Obj
	rel = formItem.getAttribute('rel');
	name = formItem.name;

	if(skipValid){
		if( name.indexOf('firstname') == -1 && 
			name.indexOf('lastname') == -1 && 
			name.indexOf('address1') == -1 && 
			name.indexOf('city') == -1 && 
			name.indexOf('county') == -1 && 
			name.indexOf('postcode') == -1 && 
			name.indexOf('email') == -1 && 
			name.indexOf('telephone') == -1 &&
			name.indexOf('[user_id]') == -1
			){
			return skipValid;
		}
	}
	errorImg = (rel.indexOf('fc') != -1) ? formItem.name.split('[')[1].split(']').join('') + 'Status' : formItem.name.split('[').join('_').split(']').join('') + 'Status';
	val = false;
	vLC = (rel == 'cp' || rel == 'ce' || rel == 'cec' || rel=="ct") ? 5 : 1;
	vLC = (rel == 'cd' || rel == 'cdb') ? 9 : vLC;
	vLC = (rel == 'cn') ? 0 : vLC;
	if(formItem.type == 'text'){
		val = (formItem.value.length > vLC) ? true : false;
		if(rel == 'ce' || rel == 'cec'){
			val = ( formItem.value.indexOf(".") > 0 && formItem.value.indexOf("@") != -1 ) ? val : false;
		}
		if(rel == 'cd'){
			if(formItem.value != ''){
				val = (formItem.value.split("/").length > 2) ? val : false;
			}else{
				val = false; //'i';
			}
		}
		if(rel == 'cdb'){
			dob = formItem.value;
			if(dob.indexOf('/')!= -1){
				var diff = new Date() - new Date(dob);
				age = parseInt(diff/60/60/24/365.25/1000) + 1;
				
				for(i=0;i<fieldList.length;i++){
					drop = $(fieldList[i]);
					for(k=0;k<drop.options.length;k++){
						drop.options[k].disabled = ( parseInt(drop.options[k].value) > parseInt(age) ) ? 'disabled' : '';
					}
				}
			}else{
				age = 0;
			}
			if(dob != '' && age < 18 ){
				formItem.value = '';
				val=false;
				alert("According to your date of birth, you are under 18. If this is correct, please contact our offices. Otherwise, please re-enter your date of birth.");
			}
		}
		val = ( rel == 'cn' && 	parseFloat(formItem.value) == 0 ) ? false : val;
		phoneCheck = /^(\+\d{0,12})*\s*(\(\d{0,12}\)\s*)*\d{0,12}(-{0,12}|\s{0,12})\d{0,12}(-{0,12}|\s{0,12})\d{0,12}(-{0,12}|\s{0,12})\d{0,12}(-{0,12}|\s{0,12})\d{0,12}$/;
		val = ( rel == 'ct' && !formItem.value.match(phoneCheck)) ? false : val;
	}else if(formItem.type == 'password'){
		val = (formItem.value.length > vLC) ? true : false;
	}else if(formItem.type == 'textarea'){
		val = (formItem.value.length > vLC) ? true : false;
	}else if(formItem.type == 'checkbox'){
		val = (formItem.checked) ? true : false;
	}else if( formItem.type.indexOf('select')!= -1 ){
		if(formItem.options.length > 0){
			val = (formItem.selectedIndex == -1 || formItem.options[formItem.selectedIndex].value != '') ? true : false;	
		}else{
			val = false;
		}
	}else if(formItem.type == 'hidden' && rel.indexOf('c') > 0){
		val = (formItem.value.length > 0) ? true : false;
	}
	//if($(errorImg)!=null){
	//if(formItem.parentNode.getElementById(errorImg)!= null){	
	if(formItem.parentNode.getElementsByTagName('div')[0] != null && formItem.parentNode.getElementsByTagName('div')[0].className.indexOf('Status') != -1){
		if(val != 'i'){
			if(val == true){
				formItem.parentNode.getElementsByTagName('div')[0].className = 'formStatusTick';
				//$(errorImg).set('class', 'formStatusTick');
			}else{
				formItem.parentNode.getElementsByTagName('div')[0].className = 'formStatusCross';
				//$(errorImg).set('class', 'formStatusCross');
			}
		}else{
			val = false;
		}
	}
	return val;
}

function fillFormFromAjax(fillForm){ //pass Obj Name
	if($('ajaxData')!=null){
		//ajaxData = $('ajaxData').getElementsByTagName('div');
		ajaxDataEls = $$('#ajaxData div');
		ajaxDataComplete = parseInt($('ajaxData').getAttribute('rel'));
		ajaxArray = new Array();
		for(i=0;i< ajaxDataEls.length;i++){
			ajaxArray[ajaxDataEls[i].id] = ajaxDataEls[i].get('html');
		}
		for(i=0;i<CheckTypes.length;i++){
			//formInputs = $(fillForm).getElementsByTagName(CheckTypes[i]);
			formInputs = $$('#' + fillForm + ' ' + CheckTypes[i]);
			for(j=0;j<formInputs.length;j++){
				chkName = formInputs[j].name.split('[').join('_').split(']').join('');
				if(ajaxArray[chkName]!=null){
					if(formInputs[j].type == 'text' || formInputs[j].type == 'password' || formInputs[j].type == 'hidden'){
						formInputs[j].value = ajaxArray[chkName];
					}else if(formInputs[j].type == 'checkbox'){
						formInputs[j].checked = (parseInt(ajaxArray[chkName]) == 1) ? true : false;
					}else if(formInputs[j].type.indexOf('select')!=-1){
						formInputs[j].selectedIndex = 0;
						for(k=0;k<formInputs[j].options.length;k++){
							formInputs[j].selectedIndex = (formInputs[j].options[k].value == ajaxArray[chkName]) ? k : formInputs[j].selectedIndex;
						}	
					}
				}
			}
		}
		if(ajaxDataComplete && checkedForm( $(fillForm) ) ){
			$(fillForm).submit();
		}
	}
}

function checkedForm(checkForm){ //pass Obj
	vOkToSend = true;
	for(i=0;i<CheckTypes.length;i++){
		//formInputs = checkForm.getElementsByTagName(CheckTypes[i]);
		formInputs = $$('#' + checkForm.id + ' ' + CheckTypes[i]);
		for(j=0;j<formInputs.length;j++){
			rel = (formInputs[j].getAttribute('rel') != null) ? formInputs[j].getAttribute('rel') : ''
			if(rel.indexOf('c') != -1){
				if(!checkThisFormItem(formInputs[j])){
					vOkToSend = false;
				}
			}
		}
	}
	if(!vOkToSend){		
		if($(checkForm.id + 'Status') != null){
			$(checkForm.id + 'Status').set('class', 'formStatusCross');
		}
		window.location = String(window.location).replace(/\#.*$/, "") + "#top";
		return false;
	}else{
		return true;
	}
}

//CONVERT FILE UPLOAD TO AJAX LOADER
function addImageUploader(fileId){ //pass Obj

	//Replace the file dialog
	fileForm = $(fileId.id);
	fileFormName = fileForm.name;
	fileFormStored = ( $(fileId.id+'stored') != null ) ? $(fileId.id+'stored').value : '';
	newFormElm =  new Element('div');
	newFormElm.addClass('formField');
	newFormElm.set('html','<a id="'+fileId.id+'" href="#">Upload new</a> <span class="redCopy" id="'+fileId.id+'Notice"></span>'
						 +'<input type="hidden" id="'+fileId.id+'Input" name="'+fileFormName+'" value="'+fileFormStored+'" />');
	fileForm.parentNode.grab(newFormElm);
	fileForm.destroy();
	
	var link = $(fileForm.id);
	var input = $(fileForm.id+'Input');
	var log =  $(fileForm.id+'Notice');
	var linkIdle = link.get('html');
 
	function linkUpdate() {
		if (!swf.uploading) return;
		var size = Swiff.Uploader.formatUnit(swf.size, 'b');
		link.set('html', '<span class="small">' + swf.percentLoaded + '% of ' + size + '</span>');
	}
 
	// Uploader instance
	var swf = new Swiff.Uploader({
		path: '/script/fancyupload/Swiff.Uploader.swf',
		url: '/index.php?class=patient&action=fileupload&view=ajax&tmpl=maincomponent',
		verbose: true,
		queued: false,
		multiple: false,
		target: link,
		instantStart: true,
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png, *.pdf)': '*.jpg; *.jpeg; *.gif; *.png; *.pdf;'
		},
		fileSizeMax: 10 * 1024 * 1024,
		onSelectSuccess: function(files) {
			if (Browser.Platform.linux) window.alert('Warning: Due to a misbehaviour of Adobe Flash Player on Linux,\nthe browser will probably freeze during the upload process.\nSince you are prepared now, the upload will start right away ...');
			log.set('html', 'Uploading'); //, 'Uploading <em>' + files[0].name + '</em> (' + Swiff.Uploader.formatUnit(files[0].size, 'b') + ')');
			this.setEnabled(false);
		},
		onSelectFail: function(files) {
			//log.alert('<em>' + files[0].name + '</em> was not added!', 'Please select an image smaller than 2 Mb. (Error: #' + files[0].validationError + ')');
			log.set('html', 'Error: File was not added');
		},
		appendCookieData: true,
		onQueue: linkUpdate,
		onFileComplete: function(file) {
			// We *don't* save the uploaded images, we only take the md5 value and create a monsterid ;)
			if (file.response.error) {
				//log.alert('Failed Upload', 'Uploading <em>' + this.fileList[0].name + '</em> failed, please try again. (Error: #' + this.fileList[0].response.code + ' ' + this.fileList[0].response.error + ')');
				log.set('html', 'Failed Upload...');
			} else {
				var responseInfo = JSON.decode(file.response.text, true); // secure decode
				if( !responseInfo.status ){
					log.set('html', responseInfo.error);
				}else{
					imageName = responseInfo.name + '.jpg'
					imagePath = responseInfo.stored; 
					log.set('html', 'File saved');
					input.value = imagePath;
					if( $('image_'+link.id) != null){
						if( $('image_'+link.id).tagName.toLowerCase() == 'img' ){
							loadSizedImage($('image_'+link.id), imagePath,$('image_'+link.id).width,$('image_'+link.id).height);
						}else{
							loadSizedImage($('image_'+link.id), imagePath,$('image_'+link.id).getStyle("width").replace('px',''),$('image_'+link.id).getStyle("height").replace('px',''));
						}
					}
					if( $('link_'+link.id) != null){
						$('link_'+link.id).href=imagePath;
						$('link_'+link.id).removeClass('hidden');
					}
				}
			}
			file.remove();
			this.setEnabled(true);
		},
		onComplete: function() {
			link.set('html', linkIdle);
		}
	});
 
	// Button state
	link.addEvents({
		click: function() { 	return false; 	},
		mouseenter: function() { 	this.addClass('hover'); swf.reposition(); 	},
		mouseleave: function() { 	this.removeClass('hover'); this.blur();	},
		mousedown: function() { 	this.focus(); 	}
	});


}

function loadSizedImage(object, path, x, y){
	var loadObject = object;
	var imgReq = new Request({ //.HTML
			url: "/index.php?class=media&action=format&view=" + escape("link["+x+"|"+y+"|1]") + "&tmpl=maincomponent&data=" + escape(path),
			onSuccess: function(filename) {
				if(object.tagName.toLowerCase() == 'img'){
					object.src = filename;
					object.removeClass('hidden');
				}else{
					var newImg = new Element('img')
					//newImg.setProperties({src:filename,width:x,height:y});  //IE wont work
					newImg.src = filename;
					newImg.width = x
					newImg.height = y;
					object.empty();
					object.grab(newImg);
				}
			},
			onFailure: function() {
				alert('Request failed.');
			}
		});
	imgReq.send();
}

function showHideRel(obj, id){
	if(obj.type == 'checkbox'){
		switcher = (obj.checked == true) ? true : false;
	}
	if(obj.type.indexOf('select')!= -1){
		if( id == 'menopause' ) { 
			switcher = (obj.options[obj.options.selectedIndex].value == 1 ) ? false : true;
		}else{
			switcher = (obj.options[obj.options.selectedIndex].value > 0) ? false : true;

		}
	}
	if( !switcher ){
		for(i=0;i<5;i++){
			setter = (i==0) ? '' : i;
			if( $(id + 'Container' + setter) != null ){
				$(id + 'Container' + setter).removeClass('hidden');
			}
			if( $(id + 'Field' + setter) != null ){
				if(id == 'children'){
					setFieldTo(id + 'Field' + setter, 12);
				}else{
					setFieldTo(id + 'Field' + setter, '');
				}
			}
		}
	}else{
		for(i=0;i<5;i++){
			setter = (i==0) ? '' : i;
			if( $(id + 'Container' + setter) != null ){
				$(id + 'Container' + setter).addClass('hidden');
			}
			if( $(id + 'Field' + setter) != null ){
				setFieldTo(id + 'Field' + setter, 0);
			}
		}
	}
}

function setFieldTo(id, value){
	if( $(id).type.indexOf('select') == -1 ){
		$(id).value = value;
	}else{
		$(id).selectedIndex = value;
	}
}

function checkFax(id, type, field){
	
	if($(id).value != ''){
		var logBox = $$('#pageError .errorMessage')[0];
		logBox.set('text','Sending test...');
		var sendFax = new Request({
						   url: "index.php?class=email&action=testfax&view=ajax&tmpl=maincomponent",
						   method: 'post',
						   data: "testfax[to_id]="+parseInt($('id').value)+"&testfax[to_type]="+escape(type)+"&testfax[to_field]="+escape(field)+"&testfax[to_number]="+escape($(id).value),
						   onFailure: function(instance){
								// ...
						   },
						   onSuccess: function(response){
							   //alert(response);
								logBox.removeClass('ajax-loading');
								if(response != ''){
									if(response.indexOf('<root>') != -1){
										message = tagContents('message', response); 
										status = tagContents('status', response); 
										if(status == true || status == 'true' || parseInt(status) > 0 || message.indexOf('success') != -1){
											logBox.set('text', message);
										}else{
											alert("Sorry, action failed ("+status+"): "+message);
										}
									}
								}else{
									logBox.set('text', 'Unknown Failure');
								}
						   }
						}).send();
	}else{
		alert("Please enter a fax number to test.");
	}	
}

function updateHospitals(obj, target){
	var tgDrop = target;
	new Request({
			   url: "index.php?class=user&action=hospitallist&view=form.options&tmpl=maincomponent&user_id=" + obj.options[obj.options.selectedIndex].value,
			   onFailure: function(instance){
					// ...
			   },
			   onSuccess: function(response){
					tgDrop.set('html', response);
			   }
			}).send();
	
}

function updatePatients(obj, target){
	var tgDrop = target;
	new Request({
			   url: "index.php?class=patient&action=display&view=user.patient.list&tmpl=maincomponent&user_id=" + obj.options[obj.options.selectedIndex].value,
			   onFailure: function(instance){
					// ...
			   },
			   onSuccess: function(response){
					tgDrop.set('html', response);
			   }
			}).send();
}

