/* 
	BASE.JS
	* This file serves as the base class for all javascripts.  The functions
	* created here will be available to all pages of the site.
	* 
	* This script needs to be included AFTER all jQuery scripts
*/


// TOGGLE
function toggle(bean, flag, id, status,object){
	$(object).attr('disabled', true);
	$.getJSON('/coldboxproxy.cfc?method=toggle',{bean:bean,flag:flag,id:id,status:status},processResult);
	// Process the result
	function processResult(data){
		if(data.SUCCESS == true){
			$(object).attr('disabled', false);
		}else{
			alert(data.MESSAGE);
			$(object).attr('disabled', false);
		}
	}
}

function radioSelect(bean, flag, id) {
	$.getJSON('/coldboxproxy.cfc?method=radioSelect',{bean:bean,flag:flag,id:id},processResult);
	//process the result
	function processResult(data){
		if(data.SUCCESS == true){
			displayMessage('Record has been updated.','cbox_messagebox_info');
		}else{
			displayMessage(data.MESSAGE, 'cbox_messagebox_error');
		}
	}
}

function addToNeedzone(userid, description, categoryid, item) {
	$.getJSON('/coldboxproxy.cfc?method=doAddNeedzoneAjax',{UserID:userid,Description:description,CategoryID:categoryid,Item:item},processResult);
	// Process the result
	function processResult(data){
		if(data.SUCCESS == true){
			displayMessage('Your needzone request has successfully been added.','cbox_messagebox_info');
		}else{
			displayMessage(data.MESSAGE, 'cbox_messagebox_error');
		}
	}
}

function displayMessage(msg, className){

	if( $("#messagebox").length > 0 )
	{
		$("#messagebox").html('<div class="'+className+'"><p class="cbox_messagebox">'+msg+'</p></div>');
		$("#messagebox").slideDown();
		setTimeout(function(){$("#messagebox").slideUp();}, 2000)
	}
	
}

function toggleDisplay(el, callback){
	$("."+el).toggle();
	
	if(callback){
		callback();
	}
}

function togglefolder(obj){
	if( $('#'+obj).hasClass('folder-closed') )
	{
		$('#'+obj).removeClass('folder-closed').addClass('folder-open');
	}else{
		$('#'+obj).removeClass('folder-open').addClass('folder-closed');
	}	

}
function deleteRecord(bean, id, visID){
		
		// Sets the content of the dialog box
		$("#dialog2").html('<p>Are you sure you want to delete this record?  This action is irreversable!');
		// Creates the dialog box
		$("#dialog2").dialog({
			bgiframe: true,
			height:200,
			title: 'Confirm Delete',
			width: 450,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {'Delete': processDelete,	Cancel: function() {$(this).dialog('close');}
			},
			close: function(){$(this).dialog('destroy')}
		});
		
		// Ajax request
		function processDelete(){
			$.getJSON('/coldboxproxy.cfc?method=deleteRecord',{bean:bean,id:id},processResult);
		}
		
		// Process Ajax Response
		function processResult(data){
			if(data.SUCCESS == true){
				// Delete successful.  Display success message
				displayMessage(data.MESSAGE,'cbox_messagebox_info');
				// If a visual element id was passed in - Fade it out, and then remove it from the DOM
				if(visID != 'undefined' && $("#"+visID).length > 0){
					$("#"+visID).fadeOut(500, 
						function(){
							$("#dialog2").dialog('close');
							$("#"+visID).remove();
						}
					);
				}
			}else{
				displayMessage(data.MESSAGE, 'cbox_messagebox_error');
			}
		}
}

function submitForm(form, containerID, overlaydiv, callback){
	// If tinyMCE exists, save it's contents
	if(typeof(tinyMCE) != 'undefined')
	{
		tinyMCE.triggerSave();
	}
	// Serialize the data
	var data = $(form).serialize();
	// Process the ajax request and fill the container with the response
	if(containerID != 'undefined'){
		if(overlaydiv != 'undefined'){$('#'+overlaydiv+' div.ui-widget-overlay').removeClass('hidden');}
		$('#'+containerID).load(form.action, data, function(){processResult(callback)});		
	}else{
	// No container id passed in.  Just process the ajax request.	
		if(overlaydiv != 'undefined'){$('#'+overlaydiv+' div.ui-widget-overlay').removeClass('hidden');}
		$.post(form.action, data, processResult);
	}
	
	function processResult(cb){
		if(overlaydiv != 'undefined'){$('#'+overlaydiv+' div.ui-widget-overlay').addClass('hidden');}
		if(typeof(cb) != 'undefined'){
			console.log(cb);
			setTimeout(cb, 500);
		}	
	}
	return false;
}
	
function dollarformat(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
  }
function dialog(target,url,title){
		$('#'+target).load(url);
		$('#'+target).dialog({
				modal: true,
				title: title,
				closeOnEscape: false,
				width: 850,
				position: 'top',
				close: function(){ $(this).dialog('destroy');}
		});		
}
