function countArray(B) {
	var C = 0;
	for ( var A in B) {
		C++
	}
	return C
}
var oBuynow = function() {
	var licensesPrices = new Array();
	var bundlesPrices = new Array();
	var offersPrices = new Array();
	var bundledProductsPrices = new Array();
	var licenses = new Array();
	var currencies2Operators = new Array();
	var currency = "USD";
	var language = "en";
	var productVersion = "0";
	var productId = 0;
	var defaultOperatorId = 0;
	var currentOperatorId = 0;
	var selected = new Array();
	selected.licenses = new Array();
	selected.bundles = new Array();
	selected.offers = new Array();
	var isCouponValid = false;
	var nameInput = new Array();
	var emailInput = new Array();
	var total = 0;
	var totalOffers = 0;
	var supprices = new Array();
	var CurrenciesArray = new Array();
	var CuponsLinkArray = new Array();
	var CuponsValueArray = new Array();

	var publicObject = {
		setProductId : function(_productId) {
			productId = _productId
		},
		setLanguage : function(_language) {
			language = _language
		},
		setLicensesPrices : function(_licensesPrices) {
			licensesPrices = _licensesPrices
		},
		setBundlesPrices : function(_bundlesPrices) {
			bundlesPrices = _bundlesPrices
		},
		setOffersPrices : function(_offersPrices) {
			offersPrices = _offersPrices
		},
		setBundledProductsPrices : function(_bundledProductsPrices) {
			bundledProductsPrices = _bundledProductsPrices
		},
		setLicenses : function(_licenses) {
			licenses = _licenses
		},
		setProductVersion : function(_productVersion) {
			productVersion = _productVersion
		},
		setLicenseQuantity : function(_licenseId, _quantity) {
			$("#lqn_" + _licenseId).val(_quantity);
			this.calculateTotal(_licenseId)
		},
		setDefaultOperatorId : function(_defaultOperatorId) {
			defaultOperatorId = _defaultOperatorId;
//			currentOperatorId = _defaultOperatorId
		},
		setCurrentOperatorId : function(_currentOperatorId) {
			currentOperatorId = _currentOperatorId
		},
		setCurrencies2Operators : function(_currencies2Operators) {
			currencies2Operators = _currencies2Operators
		},
		setCurrenciesArray : function(_CurrenciesArray) {
			CurrenciesArray = _CurrenciesArray
		},	
		setCuponsArray : function( key, links, val) {
			CuponsLinkArray[key] = links;
			CuponsValueArray[key] = val;
		},	
	
		setCurrency : function(_currency) {
			var discont =1;
			var d;
			for ( var i in licenses) {
				if (licensesPrices != undefined
						&& licensesPrices[i] != undefined
						&& licensesPrices[i][_currency] != undefined) {
					
					if (licensesPrices[i][currentOperatorId]["contractIds"][_currency]!=undefined)
					  var price_id = parseInt(licensesPrices[i][currentOperatorId]["contractIds"][_currency]);
					 else
					  var price_id = parseInt(licensesPrices[i][currentOperatorId]["contractIds"]["default"]);

					discont = (CuponsValueArray[price_id]!=undefined) ? (100-parseInt(CuponsValueArray[price_id]))*0.01 : 1 ;
					
					$("#price_" + i).html(
							((_currency=='RUB')? '' : ' '+CurrenciesArray[_currency])
									+ ((discont != 1) ? "<span style='text-decoration:line-through'>" : "")+parseInt(licensesPrices[i][_currency]["price"])+((discont != 1) ? "</span>&nbsp;<span style='color:#a40000'>" +parseInt(licensesPrices[i][_currency]["price"] * discont)+"</span>": "")+((_currency=='RUB') ?  ' '+CurrenciesArray[_currency]:''));
/*					if (parseInt(licenses[i]["usernumber"])>0)
					{
					var per_node = Math.round(licensesPrices[i][_currency]["price"] * discont / licenses[i]["usernumber"] * 100) / 100;
					$("#price_just_" + i).html( $('#just_per_node').html()+' '+
							((_currency=='RUB')? '' : ' '+CurrenciesArray[_currency])
									+ per_node+((_currency=='RUB')? ' '+CurrenciesArray[_currency] : '')+' '+$('#node').html());
			
					}*/
					if (licenses[i]['type']!='C')
					{
						var buynow_link = this.build_link(_currency,currentOperatorId,i);
						$('#buynow'+i).attr('href',buynow_link);
					}
				}
			}

			$('#currencies a').removeClass('curr');
			$('#currencies a#cur_'+_currency).addClass('curr');
		//	$('#operators a').removeClass('curoper');
		//	$('#operators a#oper_'+currentOperatorId).addClass('curoper');
			currency = _currency;
			this.setCookie("currency", _currency);
			return false;
		},
		build_link : function(cr, platf, nds)
			{
				if (licensesPrices[nds][platf]["contractIds"][cr]!=undefined)
					  var price_id = licensesPrices[nds][platf]["contractIds"][cr];
					 else
					  var price_id = licensesPrices[nds][platf]["contractIds"]["default"];
		
				if (platf == 2)
					{
					 var link = 'http://www.softkey.ru/catalog/basket.php?act=buy&id='+price_id;
					}
				else if (platf == 4)
					{
					 var link = 'https://secure.shareit.com/shareit/checkout.html?PRODUCT['+price_id+']=1'+((CuponsLinkArray[price_id]!=undefined) ? '&'+CuponsLinkArray[price_id] : '')+'&currencies='+cr;
					}
				else 
					{
	    			var link = 'https://www.plimus.com/jsp/buynow.jsp?contractId='+price_id+((CuponsLinkArray[price_id]!=undefined) ? '&'+CuponsLinkArray[price_id] : '')+'&currency='+cr;
					}
					
				return link;
		},
		setCookie : function(cookieName, value) {
			var exdate = new Date();
			exdate.setDate(365);
			document.cookie = cookieName + "=" + escape(value)
					+ ";path=/;expires=" + exdate + ";domain=" + location.host
		},
		getCookie : function(cookieName) {
			if (document.cookie.length > 0) {
				start = document.cookie.indexOf(cookieName + "=");
				if (start != -1) {
					start = start + cookieName.length + 1;
					end = document.cookie.indexOf(";", start);
					if (end == -1) {
						end = document.cookie.length
					}
					return unescape(document.cookie.substring(start, end))
				}
			}
			return null
		},
		changeplatform:function(_operatorid){
			currentOperatorId = _operatorid;
			for ( var i in licenses) {
				
				if (licenses[i] != undefined)
				{
					if (licenses[i]['type']!='C')
					{
						var buynow_link = this.build_link(currency,currentOperatorId,i);
							$('#buynow'+i).attr('href',buynow_link);
							if (currentOperatorId==3 && language=='en')
							{
								$('#buynow'+i).bind('click', function(){
											if (window.pageTracker !== undefined)
											{
												pageTracker._link(this.href);
												return false;
											}
								});
							}
							else
							{
							//	$('#buynow'+i).bind('click', function(){ return true;});
//								$('#buynow'+i).unbind('click');							
							//	$('#buynow'+i).bind('click', function(){ document.location = $(this).attr('href'); return true;});	
							}
					}	
					
					
				}
			}
			$('#operators a').removeClass('curoper');
			$('#operators a#oper_'+_operatorid).addClass('curoper');
			this.setCookie("operatorid", _operatorid);
			//alert(this.getCookie("operatorid"));
			//return false;
		}
	};

	$(document).ready( function()
	{

	/*			$('#operators a').attr('href','#');

				$(".e-blue, .e-blue2").colorbox({iframe:true, innerWidth:'600px', innerHeight:'640px',opacity:'0.90'});

				$("a[rel=iframe]").colorbox({iframe:true, innerWidth:'600px', innerHeight:'640px',opacity:'0.90'});
				$("a#polink").colorbox({iframe:true, innerWidth:'555px', innerHeight:'640px',opacity:'0.90'});
				$("a#extendlicense").colorbox({iframe:true, innerWidth:'600px', innerHeight:'540px',opacity:'0.90'});
				$("a#extenddemo").colorbox({iframe:true, innerWidth:'600px', innerHeight:'640px',opacity:'0.90'});
				$("a#formalquote").colorbox({iframe:true, innerWidth:'600px', innerHeight:'600px',opacity:'0.90'});
				*/
				
/*				var userOperatorId = publicObject.getCookie("operatorid");

				if (userOperatorId != null && currentOperatorId != 2)
				{
					currentOperatorId = userOperatorId;
				}
				if (currentOperatorId==2)
					var userCurrency="RUB";
				else
					var userCurrency = publicObject.getCookie("currency");

				publicObject.changeplatform(currentOperatorId)*/

/*		 if(String(window.location).indexOf("overlay=")!=-1)
		{
			var len = String(window.location).length;
			var pos = String(window.location).indexOf("overlay=");
			var anhor = 'a#'+String(window.location).substr(pos+8, len - pos - 8);
			$(anhor).click();
		}*/
				
								
	});
	return publicObject
}();


