// JavaScript Document

function arabianProductObject(inputCtrl, maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, InternalId){
	this.inheritFrom = customProductObject;	
	this.inheritFrom(inputCtrl, maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, InternalId);
	this.name = "arabianProductObject";
	this.strrev = function(str) {
	   if (!str) return '';
	   var revstr='';
	   for (i = str.length-1; i>=0; i--)
		   revstr+=str.charAt(i)
	   return revstr;
	}
	
	this.writeResults = function(){
		var retVal = "";
		if (this.professionalResultDiv ) {
			if (this.getLicenceNumber() > 0) {
				var tmpHtml = "";
				var currency = this.getCurrency();
				var licNo = this.getLicenceNumber() ;
				if (this.unlimitedFrom > 0 && licNo >= this.unlimitedFrom) licNo = this.textUnlimited;
				if (this.productRenewal ) {
					if (licNo == this.textUnlimited){
						tmpHtml =  " -" + this.productRenewal + "% "+this.lblRenewal+" " + licNo  ;
					} else {
						tmpHtml = licNo  + " x " + this.makeDecimals(this.getPricePerLicence())+ " " + currency +   " -" + this.productRenewal + "% "+this.lblRenewal+" ";
					}
				} else {
					if (licNo == this.textUnlimited){
						tmpHtml = licNo  ;
					} else {
						tmpHtml = currency + " " +this.makeDecimals(this.getPricePerLicence())+ " x "+ licNo   ;
					}
				}
				if (this.productDiscount > 0)
					retVal = currency+" "+this.makeDecimals(this.finalPrice)+" = ("+tmpHtml + ") -" +this.productDiscount+"% "+this.productDiscountDescription;
				else
					retVal = currency +" "+ this.makeDecimals(this.finalPrice) + " =  "+ tmpHtml ;

			} else {
				retVal  = "";
			}
			//retVal = this.strrev(retVal);
			this.professionalResultDiv.innerHTML = retVal;
		}
	}
	
}
 
function arabianCalculatorObject(controls, targetDivId){
	this.inheritFrom = customCalculatorObject;	
	this.inheritFrom(controls, targetDivId);
	this.objectName = "arabianCalculatorObject";
	function arabianCalculatorObject(controls, targetDivId){
		this.lblSolutionSelectedConf = "<h2>Solution for selected configuration</h2>";
		this.lblSolutionRecomended = "<h2>Our recommendation - you will save <span style='color:red;'>{0} %<span></h2>";
		this.lblSolutionOther = "<h2>Other solutions </h2>";
		this.lblCurrentCutomerLicenses= "<h2>Current customer licenses: </h2>";
		this.lblProduct	= "Product";
		this.lblRenewal = "Renewal";
	}
	this.getName = function(internalId){
		alert(this.internalId);
		var controls = this.contorlObj.getElementsByTagName('control');
		for(var iControl=0;iControl<controls.length;iControl++){
			var control = 	controls.item(iControl);
			alert("Id"+control.getAttribute('id'));
		}
		return "ss";
	}
	this.getProductObject = function(inp, internalId){
		var product = new arabianProductObject(this.ctrlInputList[inp], this.maintenanceCtrl, this.currencyCtrl, this.customerTypeCtrl, this.renewalCtrl, this.discountCtrl, internalId);
		product.calculator = this;
		return product;
	}
	
}



function arabianPricelistObject(pricelist,  calculator ){
	this.inheritFrom = pricelistObject;	
	this.inheritFrom(pricelist,  calculator);
	this.internalIdIndex = new Array();
	this.internalIdName = new Array();

	this.indexOf = function(arr, variable){
		for(var i=0; i<arr.length; i++){
			if(arr[i]==variable){
				return i;
			}
		}
		return -1;
	}

	this.getName = function(InternalId){
		var retVal = "";
		
		this.ctrl;
		if (this.xmlPricelist) {
			elProducts = this.xmlPricelist.getElementsByTagName("family");
			for(var k=0;k<elProducts.length;k++){
				if (elProducts[k].getAttribute('InternalId')  == InternalId) {
					retVal  = elProducts[k].getAttribute('product-family-name');
					break;
				}	
			}
		retVal = this.getProductNameByInternalId(InternalId, retVal);

		} else this.errors.push("[getName] Pricelist is not ready,  wait for while ... ");
		return retVal;
	}
	
	this.getProductNameByInternalId = function(internalId, productName){
		if (this.internalIdIndex.length == 0) {
			//alert(this.calculator);
			var controls = this.calculator.contorlObj.getElementsByTagName("control");
			for(var i=0;i<controls.length;i++){
				this.ctrl = controls[i];
				this.internalIdIndex[i] = this.ctrl.getAttribute('id');
				this.internalIdName[i] = this.ctrl.getAttribute('label');
			}
		}
		if (this.indexOf(this.internalIdIndex, internalId) > 0) {
			productName = this.internalIdName[this.indexOf(this.internalIdIndex, internalId)]	
		}
		return productName;
	}
}
