
var IS_SANDBOX = false;
var lic;
//DISCOUNT
//var DISCOUNT = 0.25;

function onCalculateClick()
{
	lic = getLicense();
	
	if ($('#no_clients').val() < 10) {
		alert('Please enter 10 users or more.');
		return false;
	}

	if (lic == null) return false;

	//DISCOUNT
	//cost = lic.totalCost /(1 - DISCOUNT);
	//discount = lic.totalCost - cost;

	//DISCOUNT
	//document.getElementById("discount").value = discount;
	document.getElementById("total_cost").value = lic.totalCost;
	
	//$('#calculate-button').hide();
	$('#buy-button').show();

	return true;
}

function toPay() {
	if($('#total_cost').val() != '') {
		$('#roi').submit();
	} else {
		alert("Please calculate your cost.");
	}
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 }
 
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function onCalculateCloudClick()
{
	var error = false;

	var email_compression = 0.7;
	var doc_compression = 0.6;
	var pic_compression = 0.4;

	var compression = 0.7;
	if(document.getElementById("data_type").value == 'Docs') {compression = doc_compression;}
	if(document.getElementById("data_type").value == 'Graphics') {compression = pic_compression;}

	var per_user_per_month = 2;
	var per_gb_per_month = 1;

	var users = document.getElementById("no_users").value;
	var data = document.getElementById("no_gbs").value;
	var days = document.getElementById("no_days").value;
	var change = document.getElementById("no_change").value;

	if(!isInt(users) || users <= 0 || users > 1000000) {alert('Please enter a valid number of users'); document.getElementById("no_users").value = '300'; return false;}
	if(!isInt(data) || data <= 0 || data > 100000) {alert('Please enter a valid number for the average data per user'); document.getElementById("no_gbs").value = '12'; return false;}
	if(!isInt(days) || days <= 0 || days > 1000) {alert('Please enter a valid number of days for user retention'); document.getElementById("no_days").value = '30'; return false;}

	var dedupe = users * data * change * (1 - compression);

	var totalStorage = roundNumber(dedupe * days, 2);
	var totalCost = roundNumber(users * per_user_per_month + totalStorage * per_gb_per_month, 2);

	document.getElementById("total_storage").value = totalStorage;
	document.getElementById("total_cost").value = totalCost;

	return true;
}

function onBuyClick()
{
	lic = getLicense();	
	if (lic == null) return false

	document.getElementById("total_cost").value = lic.totalCost;

	return true;
}

function makePaypalPayment()
{
	var prm = "?cmd=_xclick"
				+ "&business=" + escape("sales@druva.com")
				+ "&item_name=" + escape("inSync License Model - Subscription")
				+ "&amount=" + escape(lic.totalCost)
				+ "&no_shipping=1" 
				+ "&return=" + escape("http://www.druva.com/payment_success.html")
				+ "&cancel_return=" + escape("http://www.druva.com/payment_cancel.html")
				+ "&currency_code=USD"
				+ "&custom=" + lic.toString()
				+ "&notify_url=" + escape("http://www.druva.com/paypal_notify.php")

	var url = IS_SANDBOX ? "https://www.sandbox.paypal.com/cgi-bin/webscr" : "https://www.paypal.com/cgi-bin/webscr" + prm;

	document.location = url;
}

function getLicense(servers, clients, supportType, term)
{

	MM_validateForm('no_servers','','RisNum','no_clients','','RisNum')

	if (document.MM_returnValue) {
		var lic = new inSyncLicense(servers | document.getElementById("no_servers").value,
									clients | document.getElementById("no_clients").value,
									supportType | 0,
									//supportType | document.getElementById("support_type").value,
									term | getTermValue());
		
		lic.computeLicenseCost();		

		return lic;
	}
	else
		return null;
}

function getTermValue()
{
	return $('#subscribe_term').val();
	//if (document.forms[0].subscribe_term[0].checked)
	//	return 1;
	//else if (document.forms[0].subscribe_term[1].checked)
		//alert ("1"); //return 1;
	//else
	//	return 2;
}

/* commented: 
function getIntValue(value)
{
	var n = parseInt(value);
	if (isNaN(n))
		return null;
	else
		return n;
}
*/


function inSyncLicense(servers, clients, supportType, term)
{
	this.servers = servers | 0;
	this.clients = clients | 0;
	this.supportType = supportType | 0;	
	this.subscriptionTerm = term | 0;
	this.totalCost = 0;
	this.computeLicenseCost = computeLicenseCost;
	this.toString = getToString;
}

function computeLicenseCost()
{
	var SERVER_COST = 500;
	var CLIENT_COST = 40;
	var FREE_CLIENT_LIC = 0;

	var PERPETUAL_SERVER_COST = 1500;
	var PERPETUAL_CLIENT_COST = 65;

	var supportTypeCost = 0;

	/*
	switch (this.supportType)
	{
		case 1: supportTypeCost = 20; break;
		case 2: supportTypeCost = 35; break;
		case 3: supportTypeCost = 50; break;
	}
	*/
	switch (this.subscriptionTerm)
	{
		case 1: supportTypeCost = 10; break;
		case 2: supportTypeCost = 20; break;
	}

	if (this.servers < 0) {
		this.servers = 0;
	}

	if (this.clients < this.servers * FREE_CLIENT_LIC) {
		paid_clients = 0;
	} else {
		paid_clients = this.clients - this.servers * FREE_CLIENT_LIC;
	}

	switch (this.subscriptionTerm){
	case 1:
		this.totalCost = (this.servers * (SERVER_COST)
						+ paid_clients * CLIENT_COST
						+ paid_clients * supportTypeCost);
		break;
	case 2:
		this.totalCost = (this.servers * (PERPETUAL_SERVER_COST)
						+ paid_clients * PERPETUAL_CLIENT_COST
						+ paid_clients * supportTypeCost);
		break;
	}

	/*this.totalCost = (this.servers * (this.subscriptionTerm==2 ? PERPETUAL_SERVER_COST: SERVER_COST )
						+ paid_clients * (this.subscriptionTerm==2 ? PERPETUAL_CLIENT_COST: CLIENT_COST))
						* (1 + supportTypeCost);*/
	//DISCOUNT
	//this.totalCost = this.totalCost * (1 - DISCOUNT);
	if (this.servers == 9991)
	{
		this.totalCost = 1;
	}
	return this.totalCost;
}

function getToString()
{
	return this.servers + "|" + this.clients + "|" + this.supportType;
}

