
/** allows for loading HTMl into the ajax box at the same location as the button **/
function loadAjaxBox(urlStr,aElem){
	afd = jQuery('#ajaxFormDiv');
	afd.css('top',$(aElem).offset().top+10);
	afd.css('left',$(aElem).offset().left+10);
	
	afd.html("<em>Loading</em>");
	afd.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
}

function loadAjaxBoxRight(urlStr,aElem){
	afd = jQuery('#ajaxFormDiv');
	afd.css('top',$(aElem).offset().top+10);
	lft = $(aElem).offset().left-400;
	if(lft < 10) lft=10;
	afd.css('left',lft);
	
	afd.html("<em>Loading</em>");
	afd.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
}

function loadSizedAjaxBoxRight(urlStr,aElem, width){
	afd = jQuery('#ajaxFormDiv');
	afd.css('top',$(aElem).offset().top+10);
	afd.css('left',$(aElem).offset().left-width+10);
	afd.css('width',width);
	afd.css('height','');
	afd.html("<em>Loading</em>");
	afd.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
}

function ajaxBoxLink(urlStr){
	afd = jQuery('#ajaxFormDiv');
	afd.html("<em>Loading</em>");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
}

function loadAjaxTextBox(theText,aElem){
		afd = jQuery('#ajaxFormDiv');
		afd.css('top',$(aElem).offset().top+10);
		afd.css('left',$(aElem).offset().left+10);
		
		afd.html("<div style='float:right;margin: 5 5 0 0;'><a class='box' href='javascript:closeAjaxForm();'>close</a></div><div class='clear'></div>");
		afd.append(theText);
		afd.show("slow");
}


/** allow for loading the form to the div via an ajax call **/
function loadAjaxForm(urlStr){
	afd = jQuery('#ajaxFormDiv');
	afd.css('left',($(window).width()-400)/2);
	afd.css('top',180);
	afd.html("<em>Loading</em>");
	afd.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
	return false;
}

function loadSizedAjaxForm(urlStr, fromTop, width){
	afd = jQuery('#ajaxFormDiv');
	afd.css('left',($(window).width()-width)/2);
	var height = ($(window).height()-(fromTop*2));
	afd.css('top',fromTop);
	afd.css('height', height);
	afd.css('overflow', 'auto');
	afd.css('width',width);
	afd.html("<em>Loading</em>");
	afd.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxFormData
			);
	return false;
}

/** form data is returned, show on screen **/
function ajaxFormData(data){
	afd = jQuery('#ajaxFormDiv');
	//load in the expForm div
	afd.html("<div style='float:right;margin: 5 5 0 0;'><a class='box' href='javascript:closeAjaxForm();'>close</a></div><div class='clear'></div>");
	opts = { beforeSubmit:  preSExpSubmit, success: returnOfFormSubmission};
	bool_useAjaxForm = true;
	//check here to see if the data is json...
	if(data == '1'){
		//reload the page.
		window.location.reload();
	}else{
		try{
			obj_json = JSON.parse(data);
			
			//initScript is assumed to be JS to run prior to setting the HTML
			if( typeof(obj_json.initScript) != "undefined" && (obj_json.initScript.length > 0) ){
				eval(obj_json.initScript);
			}
			//html is string data to add to the object
			if( typeof(obj_json.html) != "undefined" && (obj_json.html.length > 0) ){
				afd.append(obj_json.html);
			}
			//postScript is assumed to be JS to run after having the HTMl data in.
			if( typeof(obj_json.postScript) != "undefined" && (obj_json.postScript.length > 0) ){
				eval(obj_json.postScript);
			}
			
		}catch(err){
			//not JSON, just append...
			if(data[0] == '{'){afd.append('ERROR: '+err)}
			afd.append(data);
		}
	}
	//set the form to use the form class.
	
	if(bool_useAjaxForm){
		jQuery('#ajaxFormDiv form').ajaxForm(opts);
	}
}

/** Called Prior to AJAX form submission **/
function preSExpSubmit(formData, jqForm, options){
	jQuery('#ajaxFormDiv').html("<em>Saving Changes</em>");
	return true;
}

/** See if the returned value is a single digit, if not, assume error **/
function returnOfFormSubmission(responseText, statusText){
	if(responseText == '1'){
		jQuery('#ajaxFormDiv').hide("slow");
		//reload the page.
		window.location.reload();
	}else{
		//alert("Your edits contained errors.");
		ajaxFormData(responseText);
	}
}

/** Simple Div hider **/
function closeAjaxForm(){
	jQuery('#ajaxFormDiv').hide("slow");
}

/** Inline box functions **/
var inlineJQ;
function loadInlineBox(urlStr,boxDiv){
	inlineJQ = jQuery('#'+boxDiv);
	inlineJQ.html("<em>Loading</em>");
	inlineJQ.show("slow");
	jQuery.get(urlStr,
				null,
				ajaxInlineData
			);
	return false;
}

function ajaxInlineData(data){
	//load in the expForm div
	inlineJQ.html("<div style='float:right;margin: 5 5 0 0;'><a class='box' href='#' onClick=\"inlineJQ.hide('slow');return false;\">close</a></div><div class='clear'></div>");
	opts = { beforeSubmit:  function(formData, jqForm, options){inlineJQ.html("<em>Saving Changes</em>");},
			 success: function(responseText, statusText){if(responseText == '1'){inlineJQ.hide("slow");window.location.reload(); }else{ajaxInlineData(responseText);}}
		   };
	bool_useAjaxForm = true;
	//check here to see if the data is json...
	try{
		obj_json = JSON.parse(data);
		
		//initScript is assumed to be JS to run prior to setting the HTML
		if( typeof(obj_json.initScript) != "undefined" && (obj_json.initScript.length > 0) ){
			eval(obj_json.initScript);
		}
		//html is string data to add to the object
		if( typeof(obj_json.html) != "undefined" && (obj_json.html.length > 0) ){
			inlineJQ.append(obj_json.html);
		}
		//postScript is assumed to be JS to run after having the HTMl data in.
		if( typeof(obj_json.postScript) != "undefined" && (obj_json.postScript.length > 0) ){
			eval(obj_json.postScript);
		}
		
	}catch(err){
		//not JSON, just append...
		if(data[0] == '{'){inlineJQ.append('ERROR: '+err)}
		inlineJQ.append(data);
	}
	//set the form to use the form class.
	
	if(bool_useAjaxForm){
		jQuery('#'+inlineJQ[0].id+' form').ajaxForm(opts);
	}
}



/** Define a function to handle memo adding/editing **/
var currentMemoName = '';
function editMemo(memoFieldName,aElem){
	currentMemoName = memoFieldName;
	currentVal = jQuery('#'+memoFieldName).val();
	
	afd = jQuery('#memoDiv');
	afd.css('top',$(aElem).offset().top+10);
	afd.css('left',$(aElem).offset().left+10);
	
	//set the textarea val to the currentValue
	jQuery('#memoDiv_textArea').val(currentVal);
	afd.show("slow");
}

/** Define a function for handling saves of the memo **/
function saveMemoEdit(){
	newVal = jQuery('#memoDiv_textArea').val();
	jQuery('#'+currentMemoName).val(newVal);
	jQuery('#memo_'+currentMemoName).append('<strong style="color: #009900;">!</strong>');
	//close the memo box
	jQuery('#memoDiv').hide('slow');
	currentMemoName = '';
}

function closeMemoForm(){
	jQuery('#memoDiv').hide("slow");
}

function loadMemos(memoType,aElem){
	afd = jQuery('#memoShowDiv');
	afd.css('top',$(aElem).offset().top+10);
	afd.css('left',$(aElem).offset().left+10);
	
	afd.html("<em>Loading</em>");
	afd.show("slow");
	urlStr = "ajax/memo.php?tableName="+memoTableName+"&foreignID="+memoForeignID+"&type="+memoType;
	jQuery.get(urlStr,
				null,
				ajaxMemoData
			);
	return false;
}

function ajaxMemoData(data){
	afd = jQuery('#memoShowDiv');
	afd.html("<div style='float:right;margin: 5 5 0 0;'><a class='box' href='javascript:closeMemoData();'>close</a></div><div class='clear'></div>");
	afd.append(data);
}

function closeMemoData(){
	jQuery('#memoShowDiv').hide('slow');
}
