
function __CreateCookie(name, value, days) {
    var expires = "";
    if(days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + ";";
}

function __ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while(c.charAt(0) == ' ') c = c.substring(1, c.length);
        if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function __EraseCookie(name) {
    __CreateCookie(name, "", -1);
}


/* Lokad Common (JV, probably deprecated because of jQuery) */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function dhtmlLoadScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

function lokadTracker(path)
{
   urchinTracker(path);

   google_conversion_id = 1068324425;
   google_conversion_language = "en_US";
   google_conversion_format = "1";
   google_conversion_color = "FFFFFF";
   
   if (1) {
       google_conversion_value = 1;
   }

   google_conversion_label = "pageview";
   
   dhtmlLoadScript('http://www.googleadservices.com/pagead/conversion.js');
}


/* Products page */

$(document).ready(function() {

	$(".pnav a").click(function()
	{
		var linkTitle = $(this).attr("title");
		var parentBox = $(this).parents('.productbox');

		//check the panel's state and return if it's already shown
		if( $(this).parent().attr('class') == 'active' )
			return;
	   
		// switch the panels
		$( ".pcontainer", parentBox ).css('display', 'none');//slideUp('fast');
		$("#"+linkTitle).css('display', 'block');//slideDown('fast');
		
		//highlight active tab
		$(".pnav li", parentBox).removeClass('active');
		$(this).parent().addClass('active');
	}
	
);
});

/* Pricing page */

// Lokad.com, Copyright 2007-2009

var EurInAud = 1.7;
var EurInCad = 1.5;
var EurInChf = 1.6;
var EurInGbp = 0.8;
var EurInJpy = 160.0;
var EurInUsd = 1.5;
var EurInMxn = 17.5;

var CostPerForecast = 0.15; // USD
var Power = 2.0 / 3.0;

function CalculateMonthlyForecastsForSales(numberOfSkus, numberOfRefreshs, numberOfForecastPoints) {
	var forecastsPerMonth = numberOfSkus * numberOfRefreshs * numberOfForecastPoints;
	
	return forecastsPerMonth;
}

function CalculateMonthlyForecastsForCalls(numberOfQueues, numberOfRefreshs, numberOfWeeks, quarter) {
	var forecastsPerMonth = numberOfQueues * 7 * 24 * numberOfRefreshs * numberOfWeeks * (quarter ? 4 : 1);
	
	return forecastsPerMonth;
}

// returns price in USD
function CalculateMonthlyCost(numberOfForecasts) {
	return RoundPrice(Math.pow(numberOfForecasts, Power) * CostPerForecast);
}

function RoundPrice(value) {
	return Math.round(value * 100.0) / 100.0
}

function ConvertCurrency(priceInUsd, currency) {
	var priceInEur = priceInUsd / EurInUsd;

	switch(currency.toLowerCase()) {
		case 'usd': return RoundPrice(priceInUsd);
		case 'eur': return RoundPrice(priceInEur);
		case 'aud': return RoundPrice(priceInEur * EurInAud);
		case 'cad': return RoundPrice(priceInEur * EurInCad);
		case 'chf': return RoundPrice(priceInEur * EurInChf);
		case 'gbp': return RoundPrice(priceInEur * EurInGbp);
		case 'jpy': return RoundPrice(priceInEur * EurInJpy);
                case 'mxn': return RoundPrice(priceInEur * EurInMxn);
		default: return 0;
	}
}

function CalcSells(currency) {
	if(typeof currency == "undefined") currency = 'usd';

	var noForecasts = CalculateMonthlyForecastsForSales(
		document.getElementById("sells_skuCount").value,
		document.getElementById("sells_refreshCount").value,
		document.getElementById("sells_forecastPointCount").value);
	var price = CalculateMonthlyCost(noForecasts);
	price = ConvertCurrency(price, currency);
	
	$("#sells_price").html(price);
	$("#sells_forecasts").html(noForecasts);
}

function CalcCalls(currency) {
	if(typeof currency == "undefined") currency = 'usd';

	var noForecasts = CalculateMonthlyForecastsForCalls(
		document.getElementById("calls_queueCount").value,
		document.getElementById("calls_refreshCount").value,
		document.getElementById("calls_weekCount").value,
		document.getElementById("calls_quarter").checked == true);
	var price = CalculateMonthlyCost(noForecasts);
	price = ConvertCurrency(price, currency);
	
	$("#calls_price").html(price);
	$("#calls_forecasts").html(noForecasts);
}

function CalculatePriceGeneral() {
	var currency = $("#general_currency").get(0).value;
	var noForecasts = document.getElementById("general_forecastCount").value;
	
	var price = CalculateMonthlyCost(noForecasts);
	var outputPrice = ConvertCurrency(price, currency);
	
	$("#general_result").html(outputPrice);
	
	return false;
}

