var last_menu           = 'home';
var global_ajax_handler = 'lib/global_ajax_handler.php';
function flip_tab(obj)
{
	if(last_menu=='') last_menu='home';
	if(document.getElementById(last_menu) && obj.id != last_menu)
	{	
    e        = document.getElementById(last_menu);
		e_sub    = document.getElementById(e.id+'_sub');
		crt_sub  = document.getElementById(obj.id+'_sub');
				
		last_menu     = obj.id;
		e.className   = 'tab';
		obj.className = 'tab_selected';
		
		e_sub.style.display   = 'none';
		crt_sub.style.display = 'block';
		//submenu.style.display = 'none';
	}else
  {
    crt_sub  = document.getElementById(obj.id+'_sub');
    obj.className = 'tab_selected';
    crt_sub.style.display = 'block';
    last_menu = obj.id;
  }
}

function validate_register()
{
	f = document.getElementById('register_frm');
	var msg = '';
	/*
  if(f.rpassword.value.indexOf(' ')!=-1 || f.rpasswordc.value.indexOf(' ')!=-1)
  {
    alert('Password field can\'t contain whitespaces');
    return false;
  }*/
  if(f.rpassword.value.length<4)
  {
    alert('Password must have at least 4 characters');
    return false;
  }
	if(empty_string(f.remail.value))
	{
		msg += '\nEmail';
	}
	if(empty_string(f.rpassword.value))
	{
	
		msg += '\nPassword';
	}
	if(empty_string(f.rpasswordc.value))
	{
		msg +='\nConfirm password';
	}
	
	if(f.rpasswordc.value!=f.rpassword.value)
	{
		alert('The passwords do not match');
		return false;
	}
	if(!f.tos.checked)
	{
		alert('You must agree to our TOU');
		return false;
	}
	if(msg.length!=0)
	{
		alert('The following fields are required:\n'+msg);
		return false;
	}
	return true;
}

/*INSERT HTML TO NEWSLETTER*/

function doAddContent()
{
	ref       = showDialog(400,350);
	dlg       = ref[1];
	modal_obj = ref[0];
	p_obj     = show_progress(modal_obj);
	new Ajax.Request(ajax_handler,
    {
        method   : 'post',
	    postBody : 'action=add_content&type='+$('element_type').value,
				 
		onLoading: function()
		{
		},
		
		onComplete: function(t)
		{
			var response   = t.responseText || "no response text";
			destroy_progress(p_obj);
			setDialogHTML(get_response_tag('elements_html',response));
		},
		
		onFailure: function()
		{ 
			alert('Something went wrong...') ;
		}
	
    });
}

function add_editor_content(type,id)
{
	// add content to current cursor position
	contentHTML = $('snippet_'+id).innerHTML;
	switch(type)
	{
		case 'link'    :
			var link_title = prompt("Please enter link title");
			if(empty_string(link_title))
			{
				alert('You must enter a Title for your link');
				return;
			}
			
			contentHTML = '<a href="{!#SITEURL#!}index.php?section=track_link&nltid={!#n_id#!}&lnkid='+id+'&sid={!#s_id#!}&t={!#TIME#!}&follow='+contentHTML+'">'+link_title+'</a>';
			
		break;
		
		case 'field'   : contentHTML = '{!#'+contentHTML+'#!}';
		break;
	}
	tinyMCE.execCommand('mceInsertContent',false,contentHTML);
	closeDialog();
	//return false;
}


function snippet_ovr(obj)
{
	obj.className = 'snippet_ovr';
	$('opt_'+obj.id).style.visibility = 'visible';
	
}
function snippet_out(obj)
{
	obj.className='snippet';
	$('opt_'+obj.id).style.visibility = 'hidden';
}

function add_user_setting(setting,value)
{
	new Ajax.Request(global_ajax_handler,
    {
        method   : 'post',
	    postBody : 'action=add_user_setting&setting='+setting+'&value='+value,		 
		onLoading: function()
		{
		},
		onComplete: function(t)
		{
			var response   = t.responseText || "no response text";
			if(setting =='from_email' || setting=='reply_email')
			{
				alert("An email has been sent to `"+value+"`\nPlease check your mail and click the confirmation link.\nThank you!");
			}
		},
		onFailure: function()
		{ 
			alert('Something went wrong...') ;
		}
    });	
}
function focus_subject(obj,target_response)
{
  if(empty_string(obj.value)){
  obj_target = $(target_response);
	str = "<div style='border:1px solid #ccc; background:#ffffcc'>"
       + "<img src='media/img/icons/tag_green.png' style='vertical-align:middle'>&nbsp;"
	     + "Please try not to enter spam terms in your subject."
       + "</div>";
   obj_target.innerHTML = str;
   }
}
function parse_subject(obj,target_response)
{
   obj_target = $(target_response);
   if(empty_string(obj.value))
	 {
	 	obj_target.innerHTML ='';
	 	return;
	 }
   //alert(obj_target.id);
	 new Ajax.Request(global_ajax_handler,
	    {
	      method   : 'post',
		    postBody : 'action=parse_subject&subject='+obj.value,
			
			  onComplete: function(t)
			  {
				  var response         = t.responseText || "no response text";
				  obj_target.innerHTML = get_response_tag('html',response);
			 	 // alert(response);
			  },
			
			  onFailure: function()
			  { 
		   		//alert('Something went wrong...') ;
			  }
		
	    });
}