// JavaScript Document

$(document).ready(
	function() {
 
       $(".block h2 .a1").click(function(){
			$(".f1").animate({ opacity: "toggle" }, "slow");
			$(this).text("show search");
		});
	    $(".block h2 .a1").toggle(function(){
			$(this).text("show search");
			$(this).addClass("a");
			},function(){
			$(this).text("hide search");
			$(this).removeClass("a");
		});
	   
	   $(".block h2 .a2").click(function(){
			$(".f2").animate({ opacity: "toggle" }, "slow");
		});
	   $(".block h2 .a2").toggle(function(){
			$(this).text("show options");
			$(this).addClass("a");
			},function(){
			$(this).text("hide options");
			$(this).removeClass("a");
		});
		
		$("#promo > ul").tabs({
			fxFade: true,
			fxSpeed: 'slow'
		});


		$(".sources_on").click(function(){
			$(".sources").find("input:checkbox").attr('checked',true);
			return false;
		});

		$(".sources_off").click(function(){
			$(".sources").find("input:checkbox").attr('checked',false);
			return false;
		});

		$(".limit_on").click(function(){
			$(".limit_tld").find("input:checkbox").attr('checked',true);
			return false;
		});

		$(".limit_off").click(function(){
			$(".limit_tld").find("input:checkbox").attr('checked',false);
			return false;
		});

		$(".result_checkbox_toggle").live('click',function(){
			$("table.results").find("input:checkbox").attr('checked',$(this).attr('checked'));
			$("input.domain_record:checkbox").attr('checked',$(this).attr('checked'));
		});

	});


function copySelected()
{
	var _txt = "";
	$("input.domain_record:checkbox").each(function(){
		if($(this).attr('checked'))
			if(_txt.length == 0)
				_txt = $(this).val();
			else
				_txt = _txt + ", " + $(this).val();
	});
	if(_txt.length > 0)
	{
		if(_clipboard_available)
		{
			$.clipboard(_txt);
		}
		else
		{
			$('#clipboard_data').val(_txt);
			tb_show('Clipboard','#TB_inline?height=350&width=400&inlineId=clipboard_content',null);
		}
	}
}

function load_keywords(_link){
	$(".list_keywords").load(_link);
}

function domain_action(_action,_id) {
	if(_action.length == 0 || _action == "empty") return false;

	if(_action == "renew")
	{
		// take first checked checkbox
		var _id = $(".domain_id:checked").val();

		_link = $(".registarURL_"+_id).attr('href');
		if(_link.length > 0)
			popup = window.open(_link,'Renew');
			popup.focus();
	}
	else
	{
		var _arr = new Array();
		$(".domain_id:checked").each(function(){_arr.push($(this).val())});

		if(_arr.length > 0)
		{
			if(_action == "delete")
			{
				//confirmation
				tb_show("Delete Confirmation", "/invest/portfolio.php?cmd=delete_confirmation&id="+_arr, null);
			}
			else
			{
				$.post('callback/domains_process.php',
					{'action': _action, 'domains[]': _arr, 'empty': 'yes'},
					function(){
						// reload page
						window.location.href=window.location.href;
					});
			}
		}
		else
			return false;
	}

}

function limitNumber(element,e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);

	var _t = $(element).val();
	_t = _t + keychar;

	var _v = extParseFloat(_t);

	return _v <= 10e9;
}

function updateCost() {
	var total_cost = 0;

	var auction_cost;
	var acquisition_cost;
	var paypal_fee;
	var bank_fee;
	var escrow_fee;
	var auction_fee;
	var listing_fee;

	auction_cost = extParseFloat($("#auction_cost").val());
	acquisition_cost = extParseFloat($("#acquisition_cost").val());
	paypal_fee = extParseFloat($("#paypal_fee").val());
	bank_fee = extParseFloat($("#bank_fee").val());
	escrow_fee = extParseFloat($("#escrow_fee").val());
	auction_fee = extParseFloat($("#auction_fee").val());
	listing_fee = extParseFloat($("#listing_fee").val());

	if(auction_cost > 0)
		total_cost += auction_cost;
	if(acquisition_cost > 0)
		total_cost += acquisition_cost;

	if(paypal_fee > 0)
		total_cost -= paypal_fee;
	if(bank_fee > 0)
		total_cost -= bank_fee;
	if(escrow_fee > 0)
		total_cost -= escrow_fee;
	if(auction_fee > 0)
		total_cost -= auction_fee;
	if(listing_fee > 0)
		total_cost -= listing_fee;

	$(".total_cost").html(custRound(total_cost,2));

}

function updateCost2() {
	var total_cost = 0;

	var auction_cost;
	var acquisition_cost;
	var registration_cost;
	var privacy_cost;
	var paypal_fee;
	var bank_fee;
	var escrow_fee;
	var auction_fee;
	var listing_fee;

	auction_cost = extParseFloat($("#auction_cost").val());
	acquisition_cost = extParseFloat($("#acquisition_cost").val());
	registration_cost = extParseFloat($("#registration_cost").val());
	privacy_cost = extParseFloat($("#privacy_cost").val());
	paypal_fee = extParseFloat($("#paypal_fee").val());
	bank_fee = extParseFloat($("#bank_fee").val());
	escrow_fee = extParseFloat($("#escrow_fee").val());
	auction_fee = extParseFloat($("#auction_fee").val());
	listing_fee = extParseFloat($("#listing_fee").val());

	if(auction_cost > 0)
		total_cost += auction_cost;
	if(acquisition_cost > 0)
		total_cost += acquisition_cost;
	if(registration_cost > 0)
		total_cost += registration_cost;
	if(privacy_cost > 0)
		total_cost += privacy_cost;
	if(paypal_fee > 0)
		total_cost += paypal_fee;
	if(bank_fee > 0)
		total_cost += bank_fee;
	if(escrow_fee > 0)
		total_cost += escrow_fee;
	if(auction_fee > 0)
		total_cost += auction_fee;
	if(listing_fee > 0)
		total_cost += listing_fee;


	$(".total_cost").html(custRound(total_cost,2));

}

function extParseFloat(val)
{
	var result_float = 0;
	if(val.length > 0)
	{
		val = val.replace(/,/g,'');
		val = val.replace(/\$/g,'');
		if(val.length > 0)
		{
			result_float = parseFloat(val);
		}
	}

	if(result_float != result_float) result_float = 0;

	return result_float;
}

function custRound(x,places) {
	return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



