var gCountryCode="";
var gCountryName = "";
var gDevise="";
var gGlobalReduction=0;
//var gDefaultKeyEnvironment="dfd58e55fz";

/** to select the appropriate devise **/
var gCountryForEuro = new Array("AT","BE","CY","CZ","DK","EE","FI","FR","DE","EL","HU","IE","IT","LV","LT","LU","MT","NL","PL","PT","SK","SI","ES","SE");
var gCountryForPound = new Array("GB");

if (window.XMLHttpRequest) { 
	var vHttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
	var vHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

/* OBJECTS */
Soft = function(inId, inSoftName, inHtmlDetail, inDetail, inDollar, inEuro, inPound) {
	this.id=inId;
	this.softName=inSoftName;
	this.htmlDetail=inHtmlDetail;
	this.detail=inDetail;
	this.dollar=inDollar*1.00;
	this.euro=inEuro*1.00;
	this.pound=inPound*1.00;
}

Rules = function(inQuantity, inReduction) {
	this.quantity = inQuantity;
	this.reduction = inReduction;
}

var gSoftList = new Array();
var gQuantityRules = new Array();

// INIT 

function revealEnvironment() {
	initEnvironment("REVEAL");
}

function initEnvironment(inKey) {
	
	initSoft(inKey);
	//initQuantityRules(inKey);
	initCountryCode();
	updateCurrency(inKey);
	initCurrency(inKey);
	
	checkGlobalReduction(inKey);
}

function initQuantityRules(inKey) {
	// get all the quantity rules
	vRequest = "script/cart/initEnvironment.php?key="+inKey+"&type=getQuantityRules";
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	
	var vResponse = vHttp.responseText;
	if( vResponse = "KO" ) {
		// do nothing...
	} else {
		vResponse=vResponse.split("|");
		for( var i=0;i<vResponse.length;i++ ) {
			var vRules = vResponse[i].split(":");
			gQuantityRules.push(new Rules(vRules[0],vRules[1]));
		}
	}
}

function initSoft(inKey) {
	// get all the softs from database:
	var vRequest = "script/cart/initEnvironment.php?key="+inKey+"&type=getSoft";
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	var vResponse = vHttp.responseText.split("|");	
	
	for( var i=0;i<vResponse.length;i++ ) {
		var vSoft = vResponse[i].split(":");
		// soft details:
		gSoftList.push(new Soft(vSoft[0],vSoft[1],vSoft[2],vSoft[3],vSoft[4],vSoft[5],vSoft[6]));
	}
}

function checkGlobalReduction(inKey) {
	var vRequest = "script/cart/initEnvironment.php?type=getGlobalReduction&value="+inKey;
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	var vResponse = vHttp.responseText.split("|");
	if( vResponse[0] == 'OK' ) {
		gGlobalReduction = vResponse[1];
	} else {
		gGlobalReduction = 0;
		return 0;
	}
}

function initCurrency(inKey) {
	var vRequest = "script/cart/initEnvironment.php?key="+inKey+"&type=getCurrency";
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	var vResponse = vHttp.responseText.split("|");
	if( vResponse[0] == 'OK' ) {
		gDevise = vResponse[1];	
	} else {
		// vResponse[0] == 'ER'
		gDevise = 'dollar'
	}
}

function initCountryCode() {
	if( !gCountryCode ) {
		var vRequest = "script/cart/ip_toc.php";
		vHttp.open("GET", vRequest, false);
		vHttp.send(null);
		var vResponse = vHttp.responseText;
		var vArr=vResponse.split("|");
		if( vArr.length > 0 ) {
			gCountryCode = vArr[0];
			gCountryName = vArr[1];
		} else {
			gCountryCode = "";
			gCountryName = "";
		}
	}		
}

function updateCurrency(inKey) {
	var found=false;
	if( gCountryCode == "GB" ) {
		found=true;
		gDevise = "pound";
	} else {
		for( var index in gCountryForEuro ) {
			if( gCountryCode == gCountryForEuro[index] ) {
				gDevise="euro";
				found=true;
				break;
			} 
		
		}
	}
	
	if( found == false ) {
		gDevise = "dollar";
	}

	var vRequest = "script/cart/initEnvironment.php?key="+inKey+"&type=setCurrency&value="+gDevise;
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	var vResponse = vHttp.responseText;
	
}
