
function set_changed(val)
{
	if (val === undefined)
	{
		val = true;
	}
	changed = val;
	if (val === false)
	{
		$(".success").hide();
	}
}

function setup_new_tab()
{
    set_changed(false);
	$("input").not('.no_track').click(function() { set_changed(true); });
	$("radio").not('.no_track').click(function() {set_changed(true);});
	$("select").not('.no_track').click(function() {set_changed(true);});
    $('.jsonForm').ajaxForm({ 
        dataType:  'json', 
        success:   processJson 
    });
    $('.jsonFormTinyMCE').ajaxForm({ 
        dataType:  'json', 
        success:   processJson
    });  
    $('.jsonFormTinyMCE input[type=submit]').click(function (){tinyMCE.triggerSave()});
   
    disable_links();
    tb_init("a.thickbox");//re init the thickbox
    $('.tooltip').tooltip(
	{
	delay: 0,
	  bodyHandler: function() { 
		return $($(this).attr("tag_id")).html(); 
		},
		showURL: false
	}
	);
	$('textarea[maxlength]').keyup(function(){ 
              var max = parseInt($(this).attr('maxlength')); 
              if($(this).val().length > max){ 
                  $(this).val($(this).val().substr(0, $(this).attr('maxlength'))); 
              } 
              //$(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining'); 
          }); 
	
}

function slide_back(box_name,previous_box)
{
	$('#'+box_name).slideUp('fast', function() {$('#'+box_name).remove();});
	$('#'+previous_box).slideDown();
}

function processJson(data) { 
    // 'data' is the json object returned from the server 
    
    if (data.full_page_redirect != undefined)
    {
		window.location = data.full_page_redirect;
    	// send them else where
    	return;
    }
    
   	if ((data.action_slide != undefined && data.action_slide == true) || 
   	(data.returning_data != undefined && data.returning_data['action_slide'] != undefined && data.returning_data['action_slide'] == "true")) // to slide up
	{ //slide the current page up and replace with new data after it
		if (data.no_back != undefined && data.no_back == true)
		{
				$('#'+data.returning_data['box_name']).slideUp('slow',function () {$('#'+data.returning_data['box_name']).remove()}).after(data.html);
		}
		else
		{
			$('#'+data.returning_data['box_name']).slideUp('slow').after(data.html);
		}
		setup_new_tab();
	}
	else if ((data.action_fade != undefined && data.action_fade == true) || 
   	(data.returning_data != undefined && data.returning_data['action_fade'] != undefined && data.returning_data['action_fade'] == "true")) // to slide up
	{ //slide the current page up and replace with new data after it
		if (data.no_back != undefined && data.no_back == true)
		{
				$('#'+data.returning_data['box_name']).fadeOut('fast',function () {
				$('#'+data.returning_data['box_name']).after(data.html);
				setup_new_tab();
				$('#'+data.returning_data['box_name']).remove();
				
				});
		}
		else
		{
			$('#'+data.returning_data['box_name']).fadeOut('fast',function () {
				$('#'+data.returning_data['box_name']).after(data.html);
				setup_new_tab();
			});
		}
		
	}
	else if (data.dialog_options != undefined)
	{
		$('#'+data.returning_data['box_name']+' .dialog').remove();
		//$('#'+data.returning_data['box_name']).append('<iframe class="dialog" src="#">'+data.html+'<div style="height:37px"></div></iframe>');
		$('#'+data.returning_data['box_name']).append('<iframe class="dialog" src="'+data.url+'"></iframe>');
		//$('#'+data.returning_data['box_name']+' .dialog').html(data.html);
		//the 37 pixels is because the ui dialog doesn't account for sizes correctly.
		$('#'+data.returning_data['box_name']+' .dialog').dialog(data.dialog_options);
	}
	else // update the same page
	{
	    if (data.status)
	    {
	    	set_changed(false); // something has been sent to server and server said ok
	    	if (data.html != undefined && data.html != '')
	    	{
			    if	(data.returning_data != undefined &&  data.returning_data['status_box'] != undefined && $('#'+data.returning_data['status_box']).length > 0)
			    {
			    	// if we got html back and we have a status box fill the status box with the html data
			    	$('#'+data.returning_data['status_box']).fadeOut().fadeIn().html(data.html);
			    	$.scrollTo($('#'+data.returning_data['status_box']), 100, {offset:{top: -50}});
			    }
			    else
			    {
			    	//could not find status box
			    	//so just pop up the returning data
			    	alert(data.html);
			    }
			 }
			 else
			 {
			 	$('#'+data.returning_data['status_box']).fadeOut().html('');
			 }
		 	if (data.returning_data != undefined && data.returning_data['onSuccess'] != undefined) //run specified on success function
			{
				eval(data.returning_data['onSuccess']);
			}
	    }
	    else
	    {
	    	// failed
	    	if	(data.returning_data != undefined &&  data.returning_data['status_box'] != undefined && $('#'+data.returning_data['status_box']).length > 0)
		    {
				//something went wrong and we have a status box
				var error_html = '';
				for (var errKey in data.errors)
				{
					//go through each error and turn it into a list item
					error_html += '<li>'+data.errors[errKey]+'</li>'
				}
				//then display it
		    	$('#'+data.returning_data['status_box']).fadeOut().fadeIn().html('<ul class="errors">'+error_html+'</ul>');
		    	//scroll to the errors
		    	$.scrollTo($('#'+data.returning_data['status_box']), 100, {offset:{top: -50}});
		    }
		    else
		    {
		    	//could not find status box
		    	for (var errKey in data.errors)
				{
					alert(data.errors[errKey]);
				}
		    }
		}
	}	
	
	if (data.go_back != undefined && data.go_back == true) // to slide up
	{
		if (data.returning_data != undefined && data.returning_data['back_name'] == undefined)
		{
			//we where told to go back but wher didn't have a back to go to
			if (data.smarty.requested_action != undefined)
			{
				//there is a requested action that we can fall back on 
				$.getJSON(data.smarty.requested_action,{render_html: true},function(json){
				  $('#'+data.returning_data['box_name']).fadeOut('normal',function () { $('#'+data.returning_data['box_name']).replaceWith(json.html).fadeIn();
				  setup_new_tab();
				  });
				  });	
			}
			
		} 
		else if (data.returning_data != undefined && $('#'+data.returning_data['back_name']).length > 0)
		{
			$('#'+data.returning_data['box_name']).fadeOut();
			$('#'+data.returning_data['back_name']).sideDown();
		}
		else
		{
			//something bad happend if this gets executed
			location.reload(true);
			//just start over
		}
	}
	if (data.javascript != undefined) // to slide up
	{
		eval(data.javascript);
	}

	//some data was returned and needs to go into a select list
	if (data.select_results != undefined)
	{
		if (data.returning_data != undefined && data.returning_data['select_id'] != undefined)
		{
			var select_html = '';
			for (selKey in data.select_results)
			{
				//go through each error and turn it into a list item
				select_html += '<option value="'+data.select_results[selKey].value+'">'+data.select_results[selKey].desc+'</option>'
			}
			$('#'+data.returning_data['select_id']).html(select_html); 
		}
	}
	tb_init("a.thickbox");//re init the thickbox
}

function disable_link(selector)
{
	var new_html =  '<span'+$('a'+selector).wrap("<span></span>").parent().html().slice(2).slice(0,-2)+'span>';
	$('a'+selector).wrap("<span></span>").parent().html(new_html);
}

function enable_link(selector)
{
	var new_html =  '<a'+$('span'+selector).wrap("<span></span>").parent().html().slice(5).slice(0,-5)+'a>';
	$('span'+selector).wrap("<span></span>").parent().html(new_html);
    tb_init("a.thickbox"+selector);//re init the thickbox
}

function toggle_link(selector,checkbox,use_checked)
{
	if (use_checked == undefined)
	{
		use_checked = 0;
	}
    if ($('#'+checkbox+':checked').length == use_checked)
    {
	    enable_link(selector);
	}
	else
	{
		disable_link(selector);
	}
}


function disable_links()
{
	$("a.link_disabled").click(function () {
	return false;
	});
}


function toggle_button(selector,checkbox,use_checked)
{
	if (use_checked == undefined)
	{
		use_checked = 0;
	}
    if ($('#'+checkbox+':checked').length == use_checked)
    {
	    $(selector).attr('disabled','disabled');
	}
	else
	{
		$(selector).removeAttr('disabled');
	}
}


function showregion(root)
{
	var country = $(root+' [name=country]').val();
		if (country == 'Canada')
		{
			 $('.USA .user_detail').attr("disabled","disabled");
			 $('.USA').hide();
			 $('.Canada .user_detail').removeAttr("disabled");
			 $('.Canada').show();
			 $('.prov_select').focus();
		}
		else if (country == 'USA')
		{
			 $('.Canada').hide();
   		     $('.Canada .user_detail').attr("disabled","disabled");
			 $('.USA').show();
			 $('.USA .user_detail').removeAttr("disabled");
 			 $('.state_select').focus();
		}
		else
		{
		  $('.USA .user_detail').attr("disabled","disabled");
		  $('.USA').hide();
		  $('.Canada .user_detail').attr("disabled","disabled");
		  $('.Canada').hide();	
		}
}




