	
var IS_SANDBOX = false;
var lic;
//DISCOUNT
//var DISCOUNT = 0.25;

function onCalculateClick()
{
	lic = getLicense();
	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;
	
	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 | document.getElementById("support_type").value,
									term | getTermValue());
		lic.computeLicenseCost();		
		
		return lic;
	}
	else
		return null;
}

function getTermValue()
{
	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 = 400;
	var CLIENT_COST = 45;
	var FREE_CLIENT_LIC = 0;

	var PERPETUAL_SERVER_COST = 750;
	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;
	}

	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;
}
