active_tab = 1;
var divpos;

function HideContent(param)
{
}
function ShowBubble(params) 
{
    var dd = $('#'+params.element);
    var cloudsrc = $('#'+params.cloudsrc);
    var pos = '';
    var position = cloudsrc.position();
    //var position = dd.position();
    var y = position.top;
    var x = position.left;
  
    dd.css('top',(y-30) + "px");
    dd.css('left',(x-10) + "px");
    
    dd.css('display','block');
    
    $('#clouds_yes').attr('src',"/images/it_zat_01.gif");
    $('#clouds_no').attr('src',"/images/no_zat_01.gif");
    
    if (params.status)
    {
        if(params.status == 'yes')
        {
            $('#clouds_yes').attr('src',"/images/it_zat_02.gif");
            $('#clouds_no').attr('src',"/images/no_zat_01.gif");
        }
        else if(params.status == 'no')
        {
            $('#clouds_yes').attr('src',"/images/it_zat_01.gif");
            $('#clouds_no').attr('src',"/images/no_zat_02.gif");
        }
    }
    $('#expr_clouds').text(params.block);
    return false;
}

function SnipOn(target,snippet_type)
{
	$('#'+target).css('display','block');
}
function SnipOff(target)
{
	$('#'+target).css('display','none');
}

function ShowButtons(buttons) 
{
	HideContent('snippet_table2');
    var dd = document.getElementById('snippet_table2');
	document.getElementById('buttuns_data').innerHTML = document.getElementById(buttons).innerHTML;
	
    dd.style.top = (y-100) + "px";
	dd.style.left = (x+30) + "px";
	dd.style.display = 'block';
	window.setTimeout("HideContent('snippet_table2')", 40000);	
}


////////////////////////////////////////////////////////////////

var togglePict
function toggle(togglePict){
    

    var toggleState=document.getElementById(togglePict).alt;
    if (toggleState=='toggled')
    {
	document.getElementById(togglePict).src='/logos/untoggled.gif';
	document.getElementById(togglePict).alt='untoggled';
    }
    else 
    {
	document.getElementById(togglePict).src='/logos/untoggled.gif';
	document.getElementById(togglePict).alt='toggled';
    }
}

///////////////////////////////////////////////////////////////////////
function refineselectlast()
{
	var argv = arguments;
	var argc = argv.length;
	var url='?task_id='+argv[0]+'&eventname='+argv[1]+'&object='+argv[2].value+'&value='+argv[3];
	if(url_parameters)
	{
		url = url+'&'+url_parameters
	}
	window.location.replace(url);
	
}

///////////////////////////////////////////////////////////////////////
stab = 'snresult_clear';

function sw_tab(source,name)
{
    HideContent('bubble_refine');
	$('.tab_l').attr('src','/images/tab_01_1.gif');
	$('.tab_r').attr('src','/images/tab_01_2.gif');
	$('.tab_m').removeClass('tab_bgsel');
	$('#'+stab+'_m > a > img').attr('src','/images/'+stab+'01.gif');

	$('#'+name+'_l').attr('src','/images/tab_02_1.gif');
	$('#'+name+'_r').attr('src','/images/tab_02_2.gif');
	$('#'+name+'_m').addClass('tab_bgsel');
	$('#'+name+'_m > a > img').attr('src','/images/'+name+'02.gif');
	
	stab = name;
	active_tab = source;
}

var stab_sd = 'snresult_clear';


function sw_tab_sd(name)
{
    //HideContent('bubble_refine');
	
	$('#'+stab_sd+'_l > img').attr('src','/images/tab_01_1.gif');
	$('#'+stab_sd+'_r > img').attr('src','/images/tab_01_2.gif');
	$('#'+stab_sd+'_m').removeClass('tab_bgsel');
	$('#'+stab_sd+'_l').removeClass('tab-upleft-white');
	$('#'+stab_sd+'_r').removeClass('tab-upright-white');
	$('#'+stab_sd+'_m').addClass('tab_m');
	$('#'+stab_sd+'_l').addClass('tab-upleft-grey');
	$('#'+stab_sd+'_r').addClass('tab-upright-grey');
	$('#'+stab_sd+'_m > a > img').attr('src','/images/'+stab_sd+'01.gif');

	$('#'+name+'_l > img').attr('src','/images/tab_02_1.gif');
	$('#'+name+'_r > img').attr('src','/images/tab_02_2.gif');

	$('#'+name+'_m').removeClass('tab_m');
	$('#'+name+'_l').removeClass('tab-upleft-grey');
	$('#'+name+'_r').removeClass('tab-upright-grey');
	$('#'+name+'_m').addClass('tab_bgsel');
	$('#'+name+'_l').addClass('tab-upleft-white');
	$('#'+name+'_r').addClass('tab-upright-white');
	
	$('#'+name+'_m > a > img').attr('src','/images/'+name+'02.gif');
	
	stab_sd = name;

}
var menu_sd = 'quicksearch';

function search_tab(name)
{
	// Closing popus
	close_popup('popup_loc');
	close_popup('popup_aka');
	close_popup('popup_work');
	$('#'+menu_sd+'_l > img').attr('src','/images/corner-down-left-grey.gif');
	$('#'+menu_sd+'_r > img').attr('src','/images/corner-down-right-grey.gif');	
	$('#'+menu_sd+'_m').removeClass('tabs-down-bg-w');
	$('#'+menu_sd+'_m2').removeClass('tabs-down-bg-w');
	$('#'+menu_sd+'_m').addClass('tabs-down-bg-g');
	$('#'+menu_sd+'_m2').addClass('tabs-down-bg-g');

	document.getElementById(menu_sd+'_c').style.display = "none";

	
	$('#'+name+'_l > img').attr('src','/images/corner-down-left-white.gif');
	$('#'+name+'_r > img').attr('src','/images/corner-down-right-white.gif');
	$('#'+name+'_m').removeClass('tabs-down-bg-g');
	$('#'+name+'_m2').removeClass('tabs-down-bg-g');
	$('#'+name+'_m').addClass('tabs-down-bg-w');
	$('#'+name+'_m2').addClass('tabs-down-bg-w');
	document.getElementById(name+'_c').style.display = "block";
	menu_sd = name;
	
}
function show_popup(name)
{
	document.getElementById(name).style.display = "block";
}
function close_popup(name)
{
	document.getElementById(name).style.display = "none";
}

// AKA
var aka_counter = 3
function add_aka()
{
	var container = $('#cont_aka').html();
	var new_container = container.replace(/_numb_/g, aka_counter);
	$('#one_more_aka').append(new_container);
	aka_counter++;
}

function remove_aka_el(block_numb)
{
//	 $('#aka'+block_numb).html('');
	var el = document.getElementById("aka"+block_numb);
	el.parentNode.removeChild(el);
	check_filled_aka();
}
function clear_aka ()
{
	//$('#alias2_firstname').value = '';
	document.getElementById('alias2_firstname').value = '';
	document.getElementById('alias2_middlename').value = '';
	document.getElementById('alias2_lastname').value = '';
	for(i=3;i<aka_counter;i++)
	{
		//remove_aka_el(i);
		var el = document.getElementById("aka"+i);
		if(el){
		el.parentNode.removeChild(el);
		}
		//el.remove;
	}
	aka_counter = 3;
	check_filled_aka();
}

///////////////////////////////////////////////////////////////////////

// LOCATIONS
var loc_counter = 3
function add_loc()
{
	
	var container = $('#cont_loc').html();
	var new_container = container.replace(/_numb_/g, loc_counter);
	$('#one_more_loc').append(new_container);

	loc_counter++;
}

function remove_loc_el(block_numb)
{
	 
	 var el = document.getElementById("loc"+block_numb);
	el.parentNode.removeChild(el);
	check_filled_loc();
}
function clear_loc ()
{
	//$('#alias2_firstname').value = '';
	document.getElementById('address2_City').value = '';
	document.getElementById('address2_State').value = '';
	document.getElementById('address2_Zip').value = '';
	for(i=3;i<loc_counter;i++)
	{
		//remove_loc_el(i);
		var el = document.getElementById("loc"+i);
		if(el){
		el.parentNode.removeChild(el);
		}
		//el.remove;
	}
	loc_counter = 3;
	check_filled_loc();
}

///////////////////////////////////////////////////////////////////////

// WORK
var work_counter = 3
function add_work()
{
	var container = $('#cont_work').html();
	var new_container = container.replace(/_numb_/g, work_counter);
	$('#one_more_work').append(new_container);
	work_counter++;
	check_filled_work();
}

function remove_work_el(block_numb)
{
	 
	 var el = document.getElementById("work"+block_numb);
	el.parentNode.removeChild(el);
	check_filled_work();
}
function clear_work ()
{
	//$('#alias2_firstname').value = '';
	document.getElementById('company2_companyname').value = '';
	document.getElementById('company2_title').value = '';
	document.getElementById('company2_City').value = '';
	document.getElementById('company2_State').value = '';
	document.getElementById('company2_Zip').value = '';
	
	for(i=3;i<work_counter;i++)
	{
		//remove_work_el(i);
		var el = document.getElementById("work"+i);
		if(el){
		el.parentNode.removeChild(el);
		}
		//el.remove;
	}
	work_counter = 3;
	check_filled_work();
}

///////////////////////////////////////////////////////////////////////

function sw_paypal(show_button)
{
	if(show_button == 'reg_button')
	{
		if(document.getElementById('reg_button')){
		document.getElementById('reg_button').style.display = "block";
		}
		if(document.getElementById('unreg_button'))
		{
		document.getElementById('unreg_button').style.display = "none";
		}
	}
	else
	{
		if(document.getElementById('unreg_button'))
		{
		document.getElementById('unreg_button').style.display = "block";
		}
		if(document.getElementById('reg_button'))
		{
		document.getElementById('reg_button').style.display = "none";
		}
	}
}
function check_filled_quicksearch(div_id)
{
	var counter_filled = 0;
	$('input, textarea, select', '#'+div_id).each(function(){if($(this).val() != ''){if($(this).attr('name') != 'search_session'){counter_filled++;}}});
	if(counter_filled)
	{
		$('#tick_quicksearch').attr('src', '/images/tick.gif');
	}
	else
	{
		
		$('#tick_quicksearch').attr('src', '/images/tick-no.gif');
	}	
}
function check_filled_aka()
{
	var counter_filled = 0;
	var tabs_counter_filled = 0;
	var div_id
	for (i=1;i <= aka_counter;i++)
	{
		
		if(i == 1)
		{
			div_id = 'akasearch_c';
		}
		else{
			div_id = 'akasearch'+i;
		}
		
		$('input, textarea, select', '#'+div_id).each(function(){ if($(this).val() != ''){
			counter_filled++;
			if(i>1){tabs_counter_filled++;}
		}});
	}
	if(counter_filled)
	{
		$('#tick_aka').attr('src', '/images/tick.gif');
	}
	else
	{
		
		$('#tick_aka').attr('src', '/images/tick-no.gif');
	}
	if(tabs_counter_filled > 0)
	{
		$('#aka_filled').css('display','inline');
	}else
	{
		$('#aka_filled').css('display','none');
	}
}

function check_filled_loc()
{
	var counter_filled = 0;
	var tabs_counter_filled = 0;
	var div_id
	for (i=1;i <= loc_counter;i++)
	{
		
		if(i == 1)
		{
			div_id = 'locationsearch_c';
		}
		else{
			div_id = 'locationsearch'+i;
		}
		
		$('input, textarea, select', '#'+div_id).each(function(){ if($(this).val() != '' && $(this).val() != null ){counter_filled++;
			if(i>1){tabs_counter_filled++;}
		}});
	}
	if(counter_filled)
	{
		$('#tick_loc').attr('src', '/images/tick.gif');
	}
	else
	{
		
		$('#tick_loc').attr('src', '/images/tick-no.gif');
	}
	if(tabs_counter_filled > 0)
	{
		$('#loc_filled').css('display','inline');
	}else
	{
		$('#loc_filled').css('display','none');
	}
}

function check_filled_work()
{
	var counter_filled = 0;
	var tabs_counter_filled = 0;
	var div_id
	for (i=1;i <= work_counter;i++)
	{
		
		if(i == 1)
		{
			div_id = 'worksearch_c';
		}
		else{
			div_id = 'worksearch'+i;
		}
		
		$('input, textarea, select', '#'+div_id).each(function(){ if($(this).val() != '' && $(this).val() != null){ counter_filled = counter_filled+1;
		if(i>1){tabs_counter_filled++;}
		}});
		
	}
	if(counter_filled)
	{
		$('#tick_work').attr('src', '/images/tick.gif');
	}
	else
	{
		
		$('#tick_work').attr('src', '/images/tick-no.gif');
	}
	if(tabs_counter_filled > 0)
	{
		$('#work_filled').css('display','inline');
	}else
	{
		$('#work_filled').css('display','none');
	}
	
}
function reset_form()
{
	$('input, textarea,', '#search_form').each(function(){
		$(this).val('');
	});
	$('select', '#search_form').each(function(){
		$('option:first-child', this).attr("selected","selected");
		
	});
	reset_upload();
	check_filled_quicksearch('quicksearch_c');
	check_filled_aka();
	check_filled_loc();
	check_filled_work();
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function clear_message()
{
	$('#account_message').html('');
}

function add2watchlist(task_id) {
    var search_id = $('#search_id').val(); 
	var interval = $('#interval').val(); 
	
		$('#watchlist_message').html('Sending request to server');
		$.getJSON('/watcher_add?interval='+interval+'&search_id='+search_id, function(data){
			$('#watchlist_message').html(data.resp);
		});
	
	return true;
}

function watchlist_buffer(search_id,interval_id,status_id,interval,status) {
	
	$('#search_id').val(search_id); 
	$('#interval_id').val(interval_id); 
	$('#status_id').val(status_id); 
	$('#interval_b').val(interval); 
	$('#status_b').val(status); 
	return true;
}

function editwatchlist() {
    var search_id = $('#search_id').val(); 
	var interval_id = $('#interval_id').val(); 
	var status_id = $('#status_id').val(); 
	var interval_b = {1:'Hourly',2:'Daily',3:'Weekly',4:'Monthly'};
	var status_b = {1:'Active',2:'Stoped'};
		$('#watchlist_edit_message').html('Sending request to server');
		$.getJSON('/watcher_edit?interval='+interval_id+'&search_id='+search_id+'&status='+status_id, function(data){
			$('#watchlist_edit_message').html(data.resp);
			if(data.resp == 'Task changed')
			{
				$('#interval'+search_id).html(interval_b[interval_id]);
				$('#statusb'+search_id).html(status_b[status_id]);
			}
		});
	
	return true;
}

function  change_passwd ()
{
	var new_passwd = $('#newpasswd').val(); 
	var old_password = $('#old_password').val(); 
	var confirm_newpasswd = $('#confirm_newpasswd').val();
	if(confirm_newpasswd == new_passwd)
	{
		$('#account_message').html('Sending request to server');
		$.getJSON('/auth/change_passwd?passwd='+new_passwd+'&old_password='+old_password, function(data){
			$('#account_message').html(data.resp);
		});
	}
	else
	{
		$('#account_message').html('Your New passwords did not match');
	}
	return true;
}

function  cancel_recurring ()
{
	
		$('#account_message').html('Sending request to server');
		$.getJSON('/auth/cancel_payment', function(data){
			$('#account_message').html(data.resp);
		});
	
	return true;
}

function rsign()
{
   $.blockUI.defaults.css = {
                padding:        0,
                margin:         0,
                width:          '100%', 
                top:            '30%', 
                left:           '0%', 
                textAlign:      'center', 
                horizontalAlign: 'center',
                color:          '#000', 
                border:         '2px solid #aaa',
                backgroundColor:'#eeeeee',
                cursor:         'wait'
            };
     $.blockUI({ message: $('#signup_widget') });
}

function addtowatchlist(taskid){
	$.get('/watcher/add_from_search_'+taskid, function(data){
	$('#watchbtn').remove();
	});
	return false;
}

  $(document).ready(function() { 
      $('.rsign').click(function() { 
            $.blockUI.defaults.css = {
                padding:        0,
                margin:         0,
                width:          '100%', 
                top:            '30%', 
                left:           '0%', 
                textAlign:      'center', 
                horizontalAlign: 'center',
                color:          '#000', 
                border:         '2px solid #aaa',
                backgroundColor:'#eeeeee',
                cursor:         'wait'
            };
            $.blockUI({ message: $('#signup_widget') }); 
        }); 
        $('.rsignin').click(function() { 
            $.blockUI.defaults.css = {
                padding:        0,
                margin:         0,
                width:          '30%', 
                left:           '35%', 
                top:            '30%', 
                textAlign:      'center', 
                color:          '#000', 
                border:         '2px solid #aaa',
                backgroundColor:'#fff',
                cursor:         'wait'
            };
            $.blockUI({ message: $('#signin_widget') }); 
        }); 
  
  });

