function save_primary_profile( form_id, model, onSuccessFn, onErrorFn ) {
	theform = document.getElementById(form_id);
	message_div = form_id + '_message';
	if (document.getElementById( message_div )) {
		document.getElementById( message_div ).innerHTML = '<img src="/images/loading2.gif" />';
	}
	if (!onSuccessFn) {
		onSuccessFn = function (req) {
			//alert(req.responseText);
			if ( req.responseText.indexOf('<!--saved-->')==0 ) {
				//alert(req.responseText);
				needle = '<!--ide=';
				start = req.responseText.indexOf(needle) + needle.length;
				end = req.responseText.indexOf('-->',start);
				ide = req.responseText.substring(start,end);
				if (ide) {
					// get the url up to /add-new
					start = 0;
					end = location.href.indexOf('/add-new');
					if ( end > -1 ) new_href = location.href.substring(start,end); // profile page add new
					else new_href = location.href; // submit page add new
					location.href = new_href + '/' + ide;
				} else {
					document.getElementById( message_div ).innerHTML = req.responseText;
					$.scrollTo(0,1000);
				}
			} else if (document.getElementById( message_div )) {
				document.getElementById( message_div ).innerHTML = req.responseText;
				$.scrollTo(0,1000);
			}
//			window.scrollTo(0,0);
		};
	}
	if (!onErrorFn) {
		onErrorFn = function (req) {
			alert('There has been an error. Check your form action.');
		};
	}
	theform.method = 'post';
	theform.action = '/aql/save/' + model;
	AjaxRequest.submit(theform,{
		'aql_save' : location.href,
		'onSuccess' : onSuccessFn,
		'onError' : onErrorFn
	});	
}//function


function save_web_request_form( form_id, model, new_href, onSuccessFn, onErrorFn ) {
	theform = document.getElementById(form_id);
	message_div = form_id + '_message';
	if (document.getElementById( message_div )) {
		document.getElementById( message_div ).innerHTML = '<img src="/images/loading2.gif" />';
	}
	if (!onSuccessFn) {
		onSuccessFn = function (req) {
			//alert(req.responseText);
			start = new_href.length - 1;
			end = new_href.length;
			if ( new_href.substring(start,end) == '/' ) new_href = new_href.substring(0,start);
			if ( req.responseText.indexOf('<!--saved-->')==0 ) {
				needle = '<!--ide=';
				start = req.responseText.indexOf(needle) + needle.length;
				end = req.responseText.indexOf('-->',start);
//				alert(start);
//				alert(end);
//				alert(req.responseText);
				if (end > start) {
					ide = req.responseText.substring(start,end);
					if (new_href) location.href = new_href + '/' + ide;
					else location.href = location.href + '/' + ide;
				} else location.href = new_href;				
			} else if (document.getElementById( message_div )) {
				document.getElementById( message_div ).innerHTML = req.responseText;
				$.scrollTo(0,1000);
			}
			//window.scrollTo(0,0);
		};
	}
	if (!onErrorFn) {
		onErrorFn = function (req) {
			alert('There has been an error. Check your form action.');
		};
	}

	theform.method = 'post';
	theform.action = '/aql/save/' + model;
	AjaxRequest.submit(theform,{
		'aql_save' : location.href,
		'onSuccess' : onSuccessFn,
		'onError' : onErrorFn
	});	
}//function

function open_skybox_profile(title,model,ide,qs,div,refresh_uri,width,onSuccessFunction,onSuccessFunction2) {
	if ( !width ) width = 500;
//	alert( '/aql/skybox-profile/'+model+'/'+ide+'?'+qs+'&title='+title+'&div_id='+div+'&refresh_uri='+refresh_uri );
	skybox('/aql/skybox-profile/'+model+'/'+ide+'?'+qs+'&title='+title+'&div_id='+div+'&refresh_uri='+refresh_uri+'&onSuccessFn='+onSuccessFunction2,width,false,onSuccessFunction);
}

function profile_remove(model,ide,div_id,refresh_div_uri) {
	document.getElementById(div_id).innerHTML = '<img src="/images/loading2.gif" />';
	AjaxRequest.post({
		'url' : '/aql/delete/' + model,
		'ide' : ide,
		'onSuccess':function(req){
			// refresh the 1-to-m module on the profile page
			AjaxRequest.post({
				'url' : refresh_div_uri,
				'onSuccess':function(req){
					document.getElementById(div_id).innerHTML = req.responseText;
				}
			});	
		}
	});	
}

function save_skybox_profile( form_id, model, underlying_div, refresh_uri, onSuccessFn, onErrorFn, onSuccessFn2 ) {
	theform = document.getElementById(form_id);
	message_div = form_id + '_message';
	if (document.getElementById( message_div )) {
		document.getElementById( message_div ).innerHTML = '<img src="/images/loading2.gif" />';
	}
	if (!onSuccessFn) {
		onSuccessFn = function (req) {
			if ( trimString(req.responseText).indexOf('<!--saved-->') != 0 ) {
				document.getElementById( message_div ).innerHTML = req.responseText;
			} else { 
				document.getElementById( 'skybox' ).innerHTML = req.responseText;
				refresh_divFn = function () {
					AjaxRequest.post({
						'url' : decodeURIComponent(refresh_uri),
						'onSuccess':function(req){
							document.getElementById(underlying_div).innerHTML = req.responseText;
							if (onSuccessFn2) onSuccessFn2();
							history.back();
						}
					});
				}
				setTimeout(refresh_divFn,750);
			}//if
		};
	}
	if (!onErrorFn) {
		onErrorFn = function (req) {
			alert('There has been an error. Check your form action.');
		};
	}

	theform.method = 'post';
	theform.action = '/aql/save/' + model;
	AjaxRequest.submit(theform,{
		'aql_save' : location.href,
		'onSuccess' : onSuccessFn,
		'onError' : onErrorFn
	});	
}//function

function delete_skybox_profile( form_id, model, underlying_div, refresh_uri, onSuccessFn, onErrorFn ) {
	theform = document.getElementById(form_id);
	message_div = form_id + '_message';
	if (document.getElementById( message_div )) {
		document.getElementById( message_div ).innerHTML = '<img src="/images/loading2.gif" />';
	}
	if (!onSuccessFn) {
		onSuccessFn = function (req) {
			//alert(req.responseText);
			AjaxRequest.post({
				'url' : refresh_uri,
				'onSuccess':function(req){
					document.getElementById(underlying_div).innerHTML = req.responseText;
					history.back();
				}
			});
		};
	}
	if (!onErrorFn) {
		onErrorFn = function (req) {
			alert('There has been an error. Check your form action.');
		};
	}

	theform.method = 'post';
	theform.action = '/aql/delete/' + model;
	AjaxRequest.submit(theform,{
		'aql_save' : location.href,
		'onSuccess' : onSuccessFn,
		'onError' : onErrorFn
	});	
}//function


function aql_grid(grid_id,param) {
	eval("json_e = grid_e_"+grid_id+";");
	AjaxRequest.post({
		'url' : '/aql/grid',
		'grid' : json_e,
		'onSuccess':function(req){
			document.getElementById("grid_"+grid_id).innerHTML = req.responseText;
		}
	});
}//function aql_grid()


function save_grid_row( form_id, model, onSuccessFn, onErrorFn ) {
	theform = document.getElementById(form_id);
	message_div = form_id + '_message';
	if (document.getElementById( message_div )) {
		document.getElementById( message_div ).innerHTML = '<img src="/images/loading2.gif" />';
	}
	if (!onSuccessFn) {
		onSuccessFn = function (req) {
			//alert(req.responseText);
			document.getElementById( message_div ).innerHTML = req.responseText;
		};
	}
	if (!onErrorFn) {
		onErrorFn = function (req) {
			alert('There has been an error. Check your form action.');
		};
	}
	theform.method = 'post';
	theform.action = '/aql/save/' + model;
	AjaxRequest.submit(theform,{
		'aql_save' : location.href,
		'onSuccess' : onSuccessFn,
		'onError' : onErrorFn
	});	
}//function

function aql_archive(ide,model) {
	AjaxRequest.post({
		'url' : '/aql/archive/'+model,
		'ide' : ide,
		'onSuccess':function(req){
			document.getElementById('aql_archive_'+ide).innerHTML = req.responseText;
		}
	});
}//function

function aql_delete(ide,model) {
	AjaxRequest.post({
		'url' : '/aql/delete/'+model,
		'ide' : ide,
		'onSuccess':function(req){
			document.getElementById('aql_delete_'+ide).innerHTML = req.responseText;
		}
	});
}//function
