
//------------------------------------------------------------------------------
// VerifyWithCaptcha
//
// Valideaza codul, verifica cu Captcha, selecteaza si updateaza subpaginile din
// purchase.html
//------------------------------------------------------------------------------

function VerifyWithCaptcha(bool_upgrade, bool_validate)
{
	var selected = 0;
	
	var error	= bool_upgrade == true ? $("#upgrade-check-error") : $("#update-check-error");
	var email	= bool_upgrade == true ? $("#upgrade-email") : $("#update-email");
	var code	= bool_upgrade == true ? $("#upgrade-code") : $("#update-code");

	if (IsValidMail(email.val(), false))
		selected++;

	if (code.val().length > 0)
		selected++;

	if (selected == 0)
		error.html("<span style=\"color: red;\">Please specify a value.</span>");

	else if (selected == 1)
	{	
		var recaptcha_holder_id	= "#tab4";
		var recaptcha_sender_id	= bool_upgrade == true ? "#tab3" : "#tab2";

		if (bool_validate == true)
		{	
			var recaptcha_data	=	"recaptcha_challenge_field=" + $("#recaptcha_challenge_field").val() +
									"&recaptcha_response_field=" + $("#recaptcha_response_field").val();

			var recaptcha_validation = $.ajax({
				type: "POST",
				url: "/script/extern/recaptcha.php",
				data: recaptcha_data,
				async: false
			}).responseText;

			if (recaptcha_validation == "CAPTCHA_VALID")
			{
				var purchase_response = GetPurchaseResponse(code.val(), bool_upgrade, true );

				if (purchase_response == "NOT_FOUND")
					PurchaseNotFound(bool_upgrade,recaptcha_sender_id)
				
				else if ((purchase_response == "VALID_HOME" || purchase_response == "VALID_PRO" || purchase_response == "VALID_ENT") && bool_upgrade == false)
					PurchaseUpdate(purchase_response, recaptcha_sender_id);

				else if ((purchase_response == "VALID_HOME" || purchase_response == "VALID_PRO" || purchase_response == "ALREADY_ENT") && bool_upgrade == true)
					PurchaseUpgrade(purchase_response, recaptcha_sender_id);
			}
			else
				Recaptcha.reload();
		}
		else
			PurchaseInsertSnOrMail(bool_upgrade, recaptcha_holder_id);
	}

	else if (selected > 1)
		error.html("<span style=\"color: red;\">Please specify only one value.</span>");
}


//------------------------------------------------------------------------------
// AutosubmitUpdateCode
//
// Functia se apeleaza cand se da click pe link-ul de cumparare update.
// Preia codul din URL, il valideaza, dupa care redirectioneaza catre subpagina
// de cumparare.
//------------------------------------------------------------------------------

function AutosubmitUpdateCode()
{
	var url	= document.location.href;
	var tab_rx	= /tab=/i;

	if (url.match( tab_rx ))
	{
		var items1	= url.split( tab_rx );
		var items2	= items1[1].split( /\&/ );
		var tab_id	= items2[0];
		var code_rx	= /code=/i;
		var email_rx = /email=/i;
		

		if (url.match( code_rx ) && tab_id == 2)
		{
			var code1		= url.split( code_rx );
			var code2		= code1[1].split( /\&/ );
			var code_val	= code2[0];

			var purchase_response = GetPurchaseResponse(code_val, false, true);
			
			if ( purchase_response == "VALID_HOME" || purchase_response == "VALID_PRO" )
			{
				var recaptcha_sender_id = "#tab2";

				$("#update-code").val(code_val);
				PurchaseUpdate(purchase_response, recaptcha_sender_id);
			}
		}
		
		else if ( url.match( email_rx ) && tab_id == 2 )
		{
			var code1		= url.split( email_rx );
			var code2		= code1[1].split( /\&/ );
			var email_val	= code2[0];

			var purchase_response = GetPurchaseResponse(email_val, false, false);

			if ( purchase_response == "VALID_HOME" || purchase_response == "VALID_PRO" )
			{
				var recaptcha_sender_id = "#tab2";

				$("#update-email").val(email_val);
				PurchaseUpdate(purchase_response, recaptcha_sender_id);
			}
		}
		
	}
}


//------------------------------------------------------------------------------
// GetPurchaseResponse
//
// bool_param = true daca code_or_email e code, false daca e email
//------------------------------------------------------------------------------

function GetPurchaseResponse(code_or_email, bool_upgrade, bool_param)
{
	var email = bool_upgrade == true ? $("#upgrade-email") : $("#update-email");
	var bool_email_entered = email.val() !== "" ? true : false;
	var purchase_url = bool_upgrade == true ? "/script/php/purchase-upgrade.php" : "/script/php/purchase-update.php";
	var purchase_data;
	
	if ( bool_param )
		purchase_data = bool_email_entered == true ? "email=" + email.val() : "code=" + code_or_email;
	else
		purchase_data = "email=" + code_or_email;

	purchase_data += "&check=1";
	return $.ajax({
		type: "GET",
		url: purchase_url,
		data: purchase_data,
		async: false
	}).responseText;
}


function PurchaseNotFound(bool_upgrade,recaptcha_sender_id)
{
	try
	{
		if (bool_upgrade == false)
		{
			$("#update-check").hide();
			$("#update-not-found").show();
		}
		else
		{
			$("#upgrade-check").hide();
			$("#upgrade-not-found").show();
		}

		ShowTab(null, recaptcha_sender_id);
	}
	catch (ex)
	{
		if (ex !== null)
			alert(ex)
	}
}


function PurchaseUpdate(purchase_response, recaptcha_sender_id)
{
	if (purchase_response == "VALID_HOME")
	{
		$("#update-bpp-qty").val("");
		$("#update-bpe-qty").val("");
		$("#update-bph-qty").val("1");
		$("#update-radio-1").attr("checked", true);

		DisplayTotalAndDiscount("update-home", $("#update-bph-qty").val(), $("#update-total"), $("#update-discount"));
		UpdateBuyNowButton("update-home", $("#update-bph-qty").val(), $("#update-buy-now"));

		$("#update-pro-row").hide();
		$("#update-ent-row").hide();
	}
	else if (purchase_response == "VALID_PRO")
	{
		$("#update-bph-qty").val("");
		$("#update-bpe-qty").val("");
		$("#update-bpp-qty").val("1");
		$("#update-radio-2").attr("checked", true);

		DisplayTotalAndDiscount("update-pro", $("#update-bpp-qty").val(), $("#update-total"), $("#update-discount"));
		UpdateBuyNowButton("update-pro", $("#update-bpp-qty").val(), $("#update-buy-now"));

		$("#update-home-row").hide();
		$("#update-ent-row").hide();
	}
	else
	{
		$("#update-bph-qty").val("");
		$("#update-bpp-qty").val("");
		$("#update-bpe-qty").val("1");
		$("#update-radio-3").attr("checked", true)

		DisplayTotalAndDiscount("update-ent", $("#update-bpe-qty").val(), $("#update-total"), $("#update-discount"));
		UpdateBuyNowButton("update-ent", $("#update-bpe-qty").val(), $("#update-buy-now"));

		$("#update-home-row").hide();
		$("#update-pro-row").hide();
	}
					
	try
	{
		$("#update-check").hide();
		$("#update-checked").show();

		ShowTab(null, recaptcha_sender_id);

		$(recaptcha_sender_id).height(651);
		$("#content > div.container").height(651);
	}
	catch (ex)
	{
		if (ex !== null)
			alert(ex);
	}
	
}


function PurchaseUpgrade(purchase_response, recaptcha_sender_id)
{
	$('.upgrade-bph2p').hide();
	$('.upgrade-bph2e').hide();
	$('.upgrade-bpp2e').hide();

	if (purchase_response == "VALID_HOME")
	{
		$('.upgrade-bph2p').show();
		$('.upgrade-bph2e').show();

		$("#upgrade-bph2p-qty").val("1");
		$("#upgrade-bph2e-qty").val("");
		$("#upgrade-radio-1").attr("checked", true);

		DisplayTotalAndDiscount("upgrade-h2p", $("#upgrade-bph2p-qty").val(), $("#upgrade-total"), $("#upgrade-discount"));
		UpdateBuyNowButton("upgrade-h2p", $("#upgrade-bph2p-qty").val(), $("#upgrade-buy-now"));
	}

	else if (purchase_response == "VALID_PRO")
	{
		$('.upgrade-bpp2e').show();

		DisplayTotalAndDiscount("upgrade-p2e", $("#upgrade-bpp2e-qty").val(), $("#upgrade-total"), $("#upgrade-discount"));
		UpdateBuyNowButton("upgrade-p2e", $("#upgrade-bpp2e-qty").val(), $("#upgrade-buy-now"));
	}

	try
	{
		if (purchase_response == "ALREADY_ENT")
		{
			$("#upgrade-check").hide();
			$("#upgrade-already-ent").show();
		}
		else
		{
			$("#upgrade-check").hide();
			$("#upgrade-checked").show();
		}

		ShowTab(null, recaptcha_sender_id);

		$(recaptcha_sender_id).height(712);
		$("#content > div.container").height(712);
	}
	catch (ex)
	{
		if (ex !== null)
			alert(ex);
	}
}


function PurchaseInsertSnOrMail(bool_upgrade, recaptcha_holder_id)
{
	try
	{
		$("#recaptcha-update-btn").hide();
		$("#recaptcha-upgrade-btn").hide();

		if (bool_upgrade == false)
			$("#recaptcha-update-btn").show();
		else
			$("#recaptcha-upgrade-btn").show();

		ShowTab(null, recaptcha_holder_id);
		Recaptcha.reload(); 
	}
	catch (ex)
	{
		if (ex !== null)
			alert(ex);
	}
}


function GetReference()
{
	var url	= document.location.href;
	var ref = "";

	if (url.match(/\?/))
	{
		url = url.split(/\?/);
		var params = url[1].split(/\&/);

		$.each(params, function(i)
		{
			var s = params[i].split(/\=/);
			if (s[0] == "s")
				ref = s[1];
		});
	}

	ref_clean = ref.split(/\[/)[0];
	ref = "&REF=" + ref_clean;

	/*if (ref != "")
	{
		switch (ref_clean)
		{
			case "cnet":			ref = "&REF=CNet"; break;
			case "cnetp":			ref = "&REF=CNet-Pro"; break;
			case "photoconverter":	ref = "&REF=PhotoConverter.net"; break;
			case "convertjpgtopdf":	ref = "&REF=ConvertJpgToPdf.com"; break;
			case "jpgconverter":	ref = "&REF=JpgConverter.net"; break;
			case "converttiftojpg":	ref = "&REF=ConvertTifToJpg.com"; break;
			case "resizepicture":	ref = "&REF=ResizePicture.net"; break;
			case "resizepics":		ref = "&REF=ResizePics.net"; break;
		}
	}*/

	return ref;
}


function GetItemPrice(item)
{
	var new_home	= new Array();
	var update_home	= new Array();
	var upgrade_h2p	= new Array();
	var upgrade_h2e = new Array();
	var new_pro		= new Array();
	var update_pro	= new Array();
	var upgrade_p2e = new Array();
	var new_ent		= new Array();
	var update_ent	= new Array();

	new_home[0]	= 34.95; // #1
	new_home[1]	= 27.95; // #2-9	(20%)
	new_home[2]	= 24.50; // #10-24	(30%)
	new_home[3]	= 20.95; // #25+	(40%)

	new_pro[0] = 59.95; // #1
	new_pro[1] = 47.95; // #2-9		(20%)
	new_pro[2] = 41.95; // #10-24	(30%)
	new_pro[3] = 35.95; // #25+		(40%)

	new_ent[0] = 149.95; // #1
	new_ent[1] = 119.95; // #2-9	(20%)
	new_ent[2] = 104.95; // #10-24	(30%)
	new_ent[3] = 89.95;	 // #25+	(40%)

	update_home[0] = 17.50; // #1
	update_home[1] = 14.00; // #2-9		(20%)
	update_home[2] = 12.25; // #10-24	(30%)
	update_home[3] = 10.50; // #25+		(40%)

	update_pro[0] = 29.95; // #1
	update_pro[1] = 24.00; // #2-9		(20%)
	update_pro[2] = 21.00; // #10-24	(30%)
	update_pro[3] = 18.00; // #25+		(40%)

	update_ent[0] = 74.95; // #1
	update_ent[1] = 59.95; // #2-9		(20%)
	update_ent[2] = 52.50; // #10-24	(30%)
	update_ent[3] = 44.95; // #25+		(40%)

	upgrade_h2p[0] = 25.00; // #1
	upgrade_h2p[1] = 20.00; // #2-9		(20%)
	upgrade_h2p[2] = 17.50; // #10-24	(30%)
	upgrade_h2p[3] = 15.00; // #25+		(40%)

	upgrade_h2e[0] = 115.00; // #1
	upgrade_h2e[1] = 92.00;  // #2-9	(20%)
	upgrade_h2e[2] = 80.50;  // #10-24	(30%)
	upgrade_h2e[3] = 69.00;  // #25+	(40%)

	upgrade_p2e[0] = 90.00;  // #1
	upgrade_p2e[1] = 72.00;  // #2-9	(20%)
	upgrade_p2e[2] = 63.00;  // #10-24	(30%)
	upgrade_p2e[3] = 54.00;  // #25+	(40%)

	switch (item)
	{
		case "buy-home":return new_home;
		case "buy-pro":return new_pro;
		case "buy-ent":return new_ent;
		case "update-home":return update_home;
		case "update-pro":return update_pro;
		case "update-ent":return update_ent;
		case "upgrade-h2p":return upgrade_h2p;
		case "upgrade-h2e":return upgrade_h2e;
		case "upgrade-p2e":return upgrade_p2e;
		default:return false;
	}
}


function CalculateTotal(product, quantity)
{
	var total = 0;
	var prices = GetItemPrice(product);

	quantity = parseInt(quantity);

	if (quantity == 1)
		total = quantity * prices[0];

	else if (quantity >= 2 && quantity <= 9)
		total = quantity * prices[1];

	else if (quantity >= 10 && quantity <= 24)
		total = quantity * prices[2];

	else if (quantity >= 25)
		total = quantity * prices[3];

	total = total.toFixed(2);
	return total.toString();
}


function CalculateDiscount(product, quantity, total)
{
	quantity = parseInt(quantity);

	var price				= GetItemPrice(product)[0];
	var total_no_discount	= (quantity * price);
	var discount			= total_no_discount - total;

	discount = discount.toFixed(2);
	if (discount.toString() == "NaN")
		return "0";
	
	return discount.toString();
}


function DisplayTotalAndDiscount(product, quantity, total_element, discount_element)
{
	var result = CalculateTotal(product, quantity);
	total_element.val(result);
	discount_element.val(CalculateDiscount(product, quantity, result))
}


function GetShownPrice(product, quantity)
{
	var prices_array = GetItemPrice(product);

	if (quantity == 1)
		return prices_array[0].toString();

	else if (quantity >= 2 && quantity <= 9)
		return prices_array[1].toString();

	else if (quantity >= 10 && quantity <= 24)
		return prices_array[2].toString();

	else if (quantity >= 25)
		return prices_array[3].toString();

	return prices_array[0].toString();
}


function UpdateBuyNowButton(product, quantity, button)
{
	var product_code;
	var link = "https://secure.avangate.com/order/checkout.php?PRODS=";

	if ( quantity !== "" )
		quantity = parseInt(quantity)
	else
		quantity = 1;

	var is_new = false;
	switch (product)
	{
		case "buy-home":product_code = 748733;is_new = true;break;
		case "buy-pro":product_code = 748732;is_new = true;break;
		case "buy-ent":product_code = 4553743;is_new = true;break;
		case "update-home":product_code = 2981053;break;
		case "update-pro":product_code = 2981192;break;
		case "update-ent":product_code = 4553763;break;
		case "upgrade-h2p":product_code = 2981248;break;
		case "upgrade-h2e":product_code = 4553765;break;
		case "upgrade-p2e":product_code = 4553764;break;
		default:product_code = 748733;is_new = true;break;
	}

	var url = "";
	var link_direct = link + product_code + "&QTY=" + quantity + "&CART=1" + GetReference();

	if (is_new)
	{
		link_temp = link_direct;
		var link_checker = "http://content.avangate.com/check_affiliate/?account=l7XGmw==&avgurl=";
		link_temp = link_temp.replace(/:/g, "%3A");
		link_temp = link_temp.replace(/\?/g, "%3F");
		link_temp = link_temp.replace(/=/g, "%3D");
		link_temp = link_temp.replace(/&/g, "%26");
		url = link_checker + link_temp + "&otherurl=" + link_temp + "%26AFFILIATE%3D11353";
	}
	else
		url = link_direct;

	button.attr("href", url );
}


function ToggleOrderItems()
{
	ToggleNewProduct();
	ToggleUpdateProduct();
	ToggleUpgradeProduct();
}


function ToggleNewProduct()
{
	$.new_radio_1 = $("#new-radio-1");
	$.new_radio_2 = $("#new-radio-2");
	$.new_radio_3 = $("#new-radio-3");

	$.new_home_product_price	= $("#bph-price");
	$.new_home_product_quantity	= $("#bph-qty");

	$.new_pro_product_price		= $("#bpp-price");
	$.new_pro_product_quantity	= $("#bpp-qty");

	$.new_ent_product_price	= $("#bpe-price");
	$.new_ent_product_quantity	= $("#bpe-qty");

	$.new_total		= $("#total-new");
	$.new_discount	= $("#discount-new");
	$.new_buynow	= $("#new-buy-now");
	$.new_reset		= $("#reset-new");

	$.new_home_txt	= $("#new-home-text");
	$.new_pro_txt	= $("#new-pro-text");
	$.new_ent_txt	= $("#new-ent-text");

	$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
	$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());
	$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());

	// Setarea initiala pt link-ul Buy Now
	UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);

	if ($.new_radio_2.is(":checked"))
	{
		DisplayTotalAndDiscount("buy-pro", $.new_pro_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);
	}

	$.new_reset.click(function(){
		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());

		$.new_home_product_quantity.val("");
		$.new_pro_product_quantity.val("1");
		$.new_ent_product_quantity.val("");

		$.new_radio_2.attr("checked", true);
		DisplayTotalAndDiscount("buy-pro", $.new_pro_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);
	});

	$.new_radio_1.click(function(){
		$.new_home_product_quantity.val("1");
		$.new_pro_product_quantity.val("");
		$.new_ent_product_quantity.val("");
		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());

		DisplayTotalAndDiscount("buy-home", $.new_home_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-home", $.new_home_product_quantity.val(), $.new_buynow);
	});

	$.new_radio_2.click(function(){
		$.new_home_product_quantity.val("");
		$.new_pro_product_quantity.val("1");
		$.new_ent_product_quantity.val("");
		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());

		DisplayTotalAndDiscount("buy-pro", $.new_pro_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);
	});

	$.new_radio_3.click(function(){
		$.new_home_product_quantity.val("");
		$.new_pro_product_quantity.val("");
		$.new_ent_product_quantity.val("1");
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());

		DisplayTotalAndDiscount("buy-ent", $.new_ent_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-ent", $.new_ent_product_quantity.val(), $.new_buynow);
	});

	$.new_home_product_quantity.click(function(){
	});

	$.new_home_product_quantity.keyup(function(){
		$.new_pro_product_quantity.val("");
		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());
		$.new_ent_product_quantity.val("");
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());

		$.new_radio_1.attr("checked", true);
		$.new_home_product_price.val(GetShownPrice("buy-home", $.new_home_product_quantity.val()));
		
		DisplayTotalAndDiscount("buy-home", $.new_home_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-home", $.new_home_product_quantity.val(), $.new_buynow);
	});

	$.new_home_product_quantity.blur(function(){
		if ($.new_home_product_quantity.val() == "")
			$.new_radio_1.attr("checked", false);
	});

	$.new_pro_product_quantity.click(function(){
	});

	$.new_pro_product_quantity.keyup(function(){
		$.new_home_product_quantity.val("");
		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
		$.new_ent_product_quantity.val("");
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());

		$.new_radio_2.attr("checked", true);
		$.new_pro_product_price.val(GetShownPrice("buy-pro", $.new_pro_product_quantity.val()));

		DisplayTotalAndDiscount("buy-pro", $.new_pro_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);
	});

	$.new_pro_product_quantity.blur(function(){
		if ($.new_pro_product_quantity.val() == "")
			$.new_radio_2.attr("checked", false);
	});

	$.new_ent_product_quantity.click(function(){
	});

	$.new_ent_product_quantity.keyup(function(){
		$.new_home_product_quantity.val("");
		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
		$.new_pro_product_quantity.val("");
		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());

		$.new_radio_3.attr("checked", true);
		$.new_ent_product_price.val(GetShownPrice("buy-ent", $.new_ent_product_quantity.val()));

		DisplayTotalAndDiscount("buy-ent", $.new_ent_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-ent", $.new_ent_product_quantity.val(), $.new_buynow);
	});

	$.new_ent_product_quantity.blur(function(){
		if ($.new_ent_product_quantity.val() == "")
			$.new_radio_3.attr("checked", false);
	});

	$.new_home_txt.click(function(){
		$.new_pro_product_quantity.val("");
		$.new_ent_product_quantity.val("");
		
		$.new_home_product_quantity.val("1");
		$.new_radio_1.attr("checked", true);

		DisplayTotalAndDiscount("buy-home", $.new_home_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-home", $.new_home_product_quantity.val(), $.new_buynow);

		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());
	});

	$.new_pro_txt.click(function(){
		$.new_home_product_quantity.val("");
		$.new_ent_product_quantity.val("");
		
		$.new_pro_product_quantity.val("1");

		DisplayTotalAndDiscount("buy-pro", $.new_pro_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-pro", $.new_pro_product_quantity.val(), $.new_buynow);

		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
		$.new_ent_product_price.val(GetItemPrice("buy-ent")[0].toString());
		$.new_radio_2.attr("checked", true);
	});

	$.new_ent_txt.click(function(){
		$.new_home_product_quantity.val("");
		$.new_pro_product_quantity.val("");

		$.new_ent_product_quantity.val("1");

		DisplayTotalAndDiscount("buy-ent", $.new_ent_product_quantity.val(), $.new_total, $.new_discount);
		UpdateBuyNowButton("buy-ent", $.new_ent_product_quantity.val(), $.new_buynow);

		$.new_home_product_price.val(GetItemPrice("buy-home")[0].toString());
		$.new_pro_product_price.val(GetItemPrice("buy-pro")[0].toString());
		$.new_radio_3.attr("checked", true);
	});
}

function ToggleUpdateProduct()
{
	$.update_radio_1 = $("#update-radio-1");
	$.update_radio_2 = $("#update-radio-2");
	$.update_radio_3 = $("#update-radio-3");

	$.update_home_price		= $("#update-bph-price");
	$.update_home_quantity	= $("#update-bph-qty");

	$.update_pro_price		= $("#update-bpp-price");
	$.update_pro_quantity	= $("#update-bpp-qty");

	$.update_ent_price		= $("#update-bpe-price");
	$.update_ent_quantity	= $("#update-bpe-qty");

	$.update_home_price.val(GetItemPrice("update-home")[0].toString());
	$.update_pro_price.val(GetItemPrice("update-pro")[0].toString());
	$.update_ent_price.val(GetItemPrice("update-ent")[0].toString());

	$.update_home_txt	= $("#update-home-text");
	$.update_pro_txt	= $("#update-pro-text");
	$.update_ent_txt	= $("#update-ent-text");

	$.update_total		= $("#update-total");
	$.update_discount	= $("#update-discount");
	$.update_buynow		= $("#update-buy-now");
	$.update_reset		= $("#update-reset");	

	$.update_home_quantity.click(function(){
		$.update_pro_quantity.val("");
		$.update_ent_quantity.val("");
		
		DisplayTotalAndDiscount("update-home", $.update_home_quantity.val(), $.update_total, $.update_discount);
		$.update_pro_price.val(GetItemPrice("update-pro")[0].toString());
		$.update_ent_price.val(GetItemPrice("update-ent")[0].toString());
	});

	$.update_home_quantity.blur(function(){
		if ($.update_home_quantity.val() == "")
		{
			$.update_radio_1.attr("checked", false);
			UpdateBuyNowButton("update-home", $.update_home_quantity.val(), $.update_buynow);
		}
	});

	$.update_home_quantity.keyup(function(){
		$.update_radio_1.attr("checked", true);
		$.update_home_price.val(GetShownPrice("update-home", $.update_home_quantity.val()));
		DisplayTotalAndDiscount("update-home", $.update_home_quantity.val(), $.update_total, $.update_discount);
		UpdateBuyNowButton("update-home", $.update_home_quantity.val(), $.update_buynow);
	});

	$.update_pro_quantity.click(function(){
		$.update_home_quantity.val("");
		$.update_ent_quantity.val("");

		DisplayTotalAndDiscount("update-pro", $.update_pro_quantity.val(), $.update_total, $.update_discount);
		$.update_home_price.val(GetItemPrice("update-home")[0].toString());
		$.update_ent_price.val(GetItemPrice("update-ent")[0].toString());
	});

	$.update_pro_quantity.blur(function(){
		if ($.update_pro_quantity.val() == "")
		{
			$.update_radio_2.attr("checked", false);
			UpdateBuyNowButton("update-pro", $.update_pro_quantity.val(), $.update_buynow);
		}
	});
	
	$.update_pro_quantity.keyup(function(){
		$.update_radio_2.attr("checked", true);
		$.update_pro_price.val(GetShownPrice("update-pro", $.update_pro_quantity.val()));
		DisplayTotalAndDiscount("update-pro", $.update_pro_quantity.val(), $.update_total, $.update_discount);
		UpdateBuyNowButton("update-pro", $.update_pro_quantity.val(), $.update_buynow);
	});

	$.update_ent_quantity.click(function(){
		$.update_home_quantity.val("");
		$.update_pro_quantity.val("");

		DisplayTotalAndDiscount("update-ent", $.update_ent_quantity.val(), $.update_total, $.update_discount);
		$.update_home_price.val(GetItemPrice("update-home")[0].toString());
		$.update_pro_price.val(GetItemPrice("update-pro")[0].toString());
	});

	$.update_ent_quantity.blur(function(){
		if ($.update_ent_quantity.val() == "")
		{
			$.update_radio_3.attr("checked", false);
			UpdateBuyNowButton("update-ent", $.update_ent_quantity.val(), $.update_buynow);
		}
	});

	$.update_ent_quantity.keyup(function(){
		$.update_radio_3.attr("checked", true);
		$.update_ent_price.val(GetShownPrice("update-ent", $.update_ent_quantity.val()));
		DisplayTotalAndDiscount("update-ent", $.update_ent_quantity.val(), $.update_total, $.update_discount);
		UpdateBuyNowButton("update-ent", $.update_ent_quantity.val(), $.update_buynow);
	});

	$.update_reset.click(function(){
		$.update_radio_1.attr("checked", true);
		$.update_pro_quantity.val("");
		$.update_ent_quantity.val("");
		$.update_home_quantity.val("1");

		DisplayTotalAndDiscount("update-home", $.update_home_quantity.val(), $.update_total, $.update_discount);
		UpdateBuyNowButton("update-home", $.update_home_quantity.val(), $.update_buynow);

		$.update_pro_price.val(GetItemPrice("update-pro")[0].toString());
		$.update_ent_price.val(GetItemPrice("update-ent")[0].toString());
	});
}


function ToggleUpgradeProduct()
{
	$.upgrade_radio_1 = $("#upgrade-radio-1"); // h2p
	$.upgrade_radio_2 = $("#upgrade-radio-2"); // h2e
	$.upgrade_radio_3 = $("#upgrade-radio-3"); // p2e

	$.upgrade_h2p_price		= $("#upgrade-bph2p-price");
	$.upgrade_h2p_quantity	= $("#upgrade-bph2p-qty");
	$.upgrade_h2p_price.val(GetItemPrice("upgrade-h2p")[0].toString());

	$.upgrade_h2e_price		= $("#upgrade-bph2e-price");
	$.upgrade_h2e_quantity	= $("#upgrade-bph2e-qty");
	$.upgrade_h2e_price.val(GetItemPrice("upgrade-h2e")[0].toString());

	$.upgrade_p2e_price		= $("#upgrade-bpp2e-price");
	$.upgrade_p2e_quantity	= $("#upgrade-bpp2e-qty");
	$.upgrade_p2e_price.val(GetItemPrice("upgrade-p2e")[0].toString());

	$.upgrade_total		= $("#upgrade-total");
	$.upgrade_discount	= $("#upgrade-discount");
	$.upgrade_buynow	= $("#upgrade-buy-now");
	$.upgrade_reset_btn = $("#upgrade-reset");

	// Setarea initiala pt link-ul Buy Now
	UpdateBuyNowButton("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_buynow);

	// h2p
	
	$.upgrade_h2p_quantity.keyup(function(){
		$.upgrade_h2e_price.val(GetShownPrice("upgrade-h2e", $.upgrade_h2e_quantity.val()));
		$.upgrade_h2e_quantity.val("");

		$.upgrade_radio_1.attr("checked", true);
		$.upgrade_h2p_price.val(GetShownPrice("upgrade-h2p", $.upgrade_h2p_quantity.val()));
			
		DisplayTotalAndDiscount("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_total, $.upgrade_discount);
		UpdateBuyNowButton("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_buynow);
	});

	$.upgrade_radio_1.click(function(){
		$.upgrade_h2p_quantity.val("1");
		$.upgrade_h2e_quantity.val("");

		$.upgrade_h2p_price.val(GetItemPrice("upgrade-h2p")[0].toString());

		DisplayTotalAndDiscount("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_total, $.upgrade_discount);
		UpdateBuyNowButton("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_buynow);
	});

	// h2e

	$.upgrade_h2e_quantity.keyup(function(){
		$.upgrade_h2p_price.val(GetShownPrice("upgrade-h2p", $.upgrade_h2p_quantity.val()));
		$.upgrade_h2p_quantity.val("");

		$.upgrade_radio_2.attr("checked", true);
		$.upgrade_h2e_price.val(GetShownPrice("upgrade-h2e", $.upgrade_h2e_quantity.val()));

		DisplayTotalAndDiscount("upgrade-h2e", $.upgrade_h2e_quantity.val(), $.upgrade_total, $.upgrade_discount);
		UpdateBuyNowButton("upgrade-h2e", $.upgrade_h2e_quantity.val(), $.upgrade_buynow);
	});

	$.upgrade_radio_2.click(function(){
		$.upgrade_h2p_quantity.val("");
		$.upgrade_h2e_quantity.val("1");

		$.upgrade_h2e_price.val(GetItemPrice("upgrade-h2e")[0].toString());

		DisplayTotalAndDiscount("upgrade-h2e", $.upgrade_h2e_quantity.val(), $.upgrade_total, $.upgrade_discount);
		UpdateBuyNowButton("upgrade-h2e", $.upgrade_h2e_quantity.val(), $.upgrade_buynow);
	});

	// p2e

	$.upgrade_p2e_quantity.keyup(function(){
		$.upgrade_p2e_price.val(GetShownPrice("upgrade-p2e", $.upgrade_p2e_quantity.val()));

		DisplayTotalAndDiscount("upgrade-p2e", $.upgrade_p2e_quantity.val(), $.upgrade_total, $.upgrade_discount);
		UpdateBuyNowButton("upgrade-p2e", $.upgrade_p2e_quantity.val(), $.upgrade_buynow);
	});


	$.upgrade_p2e_quantity.click(function(){
		DisplayTotalAndDiscount("upgrade-p2e", $.upgrade_p2e_quantity.val(), $.upgrade_total, $.upgrade_discount);
	});

	// reset

	$.upgrade_reset_btn.click(function(){
		// Vizibila linia tabelului cu p2e
		if( $('.upgrade-bpp2e').is(":visible") )
		{
			$.upgrade_p2e_price.val(GetItemPrice("upgrade-p2e")[0].toString());
			$.upgrade_p2e_quantity.val("1");

			DisplayTotalAndDiscount("upgrade-p2e", $.upgrade_p2e_quantity.val(), $.upgrade_total, $.upgrade_discount);
			UpdateBuyNowButton("upgrade-p2e", $.upgrade_p2e_quantity.val(), $.upgrade_buynow);
		}
		// Vizibile cele 2 linii ale tabelului cu h2p, h2e
		else
		{
			$.upgrade_h2p_price.val(GetItemPrice("upgrade-h2p")[0].toString());
			$.upgrade_h2p_quantity.val("1");
			$.upgrade_h2e_price.val(GetItemPrice("upgrade-h2e")[0].toString());
			$.upgrade_h2e_quantity.val("");

			DisplayTotalAndDiscount("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_total, $.upgrade_discount);
			UpdateBuyNowButton("upgrade-h2p", $.upgrade_h2p_quantity.val(), $.upgrade_buynow);
		}
	});
}


function Check()
{
	var vId = 'l7XGmw==';
	var scriptSRC = '/check_affiliate_v2.js';

	var protocol = window.location.protocol;
	if (protocol.indexOf("https") === 0) document.write(unescape("%3Cscript src='https://secure.avangate.com/content" + scriptSRC + "' type='text/javascript'%3E%3C/script%3E"));
	else document.write(unescape("%3Cscript src='http://content.avangate.com" + scriptSRC + "' type='text/javascript'%3E%3C/script%3E"));

	var avgParams = _checkAvgParams();
	var alreadyChecked = false;
	if (avgParams != null) {
		_AVGSetCookie('_avgCheck', avgParams);
		alreadyChecked = true;
	}
	var avgProds = _avgProds(_AVGGetCookie('_avgCheck'), alreadyChecked, vId); //redirect
	var AVG_AFF = false;
	var AVG_PRODS = new Array();
	if (avgProds != "-") {
		AVG_AFF = true;
		if(avgProds != 'all') {AVG_PRODS = avgProds.split(',');}
	}
}
