﻿/*
Unit Manager
*/

var unitArray = new Array();

//- InitUnitManager -//
function InitUnitManager(refId, Id, minCount, maxCount, addLink, removeLink) {
	var idElement;
	var addElement;
	var removeElement;
	if (((idElement = _gel(Id)) == null) || ((addElement = _gel(addLink)) == null) || ((removeElement = _gel(removeLink)) == null)) {
		return;
	} else {
		unitArray[refId] = new Array();
		unitArray[refId]["MinItems"] = minCount;
		unitArray[refId]["MaxItems"] = maxCount;
		unitArray[refId]["CountControl"] = idElement;
		unitArray[refId]["AddLink"] = addLink;
		unitArray[refId]["RemoveLink"] = removeLink;
		
		for(x=unitArray[refId]["MinItems"]; x<=unitArray[refId]["MaxItems"];x++) {
			$("#" + refId + x).hide();
		}
		for(y=unitArray[refId]["MinItems"]; y<=unitArray[refId]["CountControl"].value;y++) {
			UnitAnimateOn("#" + refId + y);
		}
		RefreshLinkControls(refId);
	}
}

//- AddUnit -//
function AddUnit(refId) {
	if(unitArray[refId]["CountControl"].value < unitArray[refId]["MaxItems"]) {
		unitArray[refId]["CountControl"].value++;
		UnitAnimateOn("#" + refId + unitArray[refId]["CountControl"].value);
		if(unitArray[refId]["CountControl"].value == unitArray[refId]["MaxItems"]) {
			UnitAnimateOff("#" + unitArray[refId]["AddLink"]);
		}
	}
	RefreshLinkControls(refId);
	return
}

//- RemoveUnit -//
function RemoveUnit(refId) {
	if(unitArray[refId]["CountControl"].value > unitArray[refId]["MinItems"]) {
		UnitAnimateOff("#" + refId + unitArray[refId]["CountControl"].value);
		unitArray[refId]["CountControl"].value--;
		if(unitArray[refId]["CountControl"].value == unitArray[refId]["MinItems"]) {
			UnitAnimateOff("#" + unitArray[refId]["RemoveLink"]);
		}
	}
	RefreshLinkControls(refId);
	return
}

//- RefreshLinkControls -//
function RefreshLinkControls(refId) {
	if(unitArray[refId]["CountControl"].value < unitArray[refId]["MaxItems"]) {
		$("#" + unitArray[refId]["AddLink"]).show();
		$("#" + unitArray[refId]["AddLink"] + "Text").show();
	} else {
		$("#" + unitArray[refId]["AddLink"]).hide();
		$("#" + unitArray[refId]["AddLink"] + "Text").hide();
	}
	if(unitArray[refId]["CountControl"].value > unitArray[refId]["MinItems"]) {
		$("#" + unitArray[refId]["RemoveLink"]).show();
	} else {
		$("#" + unitArray[refId]["RemoveLink"]).hide();
	}
}

//- AnimateOn -//
function UnitAnimateOn(id) {
	$(id).animate({ height: 'show', opacity: 'show' }, 500);
}

//- AnimateOff -//
function UnitAnimateOff(id) {
	$(id).animate({ height: 'hide', opacity: 'hide' }, 500);
}



var formFieldCollection = new Array();
var formCollection = new Array();

function InitFormValueCollection(refId, storageLocation, storageType, isDisplay, displayLocation, displayType, isCurrency, callBack) {
	formCollection.push(refId);
	formFieldCollection[refId] = new Array();
	formFieldCollection[refId]["Storage"] = new Array();
	formFieldCollection[refId]["Storage"]["LocationId"] = storageLocation;
	formFieldCollection[refId]["Storage"]["Type"] = storageType;
	formFieldCollection[refId]["Display"] = new Array();
	formFieldCollection[refId]["Display"]["LocationId"] = (isDisplay == true) ? storageLocation : displayLocation;
	formFieldCollection[refId]["Display"]["Type"] = (isDisplay == true) ? storageType : displayType;
	formFieldCollection[refId]["Display"]["IsCurrency"] = isCurrency;
	formFieldCollection[refId]["DisplayTotal"] = 0.00;
	formFieldCollection[refId]["StorageTotal"] = 0.00;
	formFieldCollection[refId]["CallBack"] = callBack;
	UpdateForm();
	//+ bind events
	$("." + refId + "Field").bind("keyup", function(){ InputBinder($(this).attr("id"), refId, "filter"); });
	$("." + refId + "Field").bind("blur", function(){ InputBinder($(this).attr("id"), refId, "updatevalue"); });
	return;
}

function UpdateForm() {
	for(x=0;x<=formCollection.length - 1;x++) {
		UpdateFormTotals(formCollection[x]);
	}
}
function InputBinder(elementId, refId, action) {
	if(action == "filter") {
		var element;
		if((element = _gel(elementId)) == null) {
			return;
		}
		FilterInput(element);
	} else if (action == "updatevalue") {
		UpdateFormValue("#" + elementId, refId)
		UpdateForm();
	} else {
		return;
	}
	return;
}

function UpdateFormValue(fieldId, refId) {
	$(fieldId).val(FormatNumber($(fieldId).val(),false));
	return;
}

function UpdateFormTotals(refId) {
	var tempTotal = 0.00;
	$("." + refId + "Field").each(function(i){ tempTotal += (isNaN(RawNumber(this.value)))? 0.00 : parseFloat(RawNumber(this.value))  });
		
	//+ Storage
	if(formFieldCollection[refId]["CallBack"] != null) {
		formFieldCollection[refId]["StorageTotal"] = eval(formFieldCollection[refId]["CallBack"] + "(formFieldCollection, tempTotal, 'storage')");
	} else {
		formFieldCollection[refId]["StorageTotal"] = tempTotal;
	}
	if(formFieldCollection[refId]["Storage"]["Type"] == "field") {
		$("#" + formFieldCollection[refId]["Storage"]["LocationId"]).val(FormatNumber(formFieldCollection[refId]["StorageTotal"], false));
	} else if(formFieldCollection[refId]["Storage"]["Type"] == "span") {
		$("#" + formFieldCollection[refId]["Storage"]["LocationId"]).text(FormatNumber(formFieldCollection[refId]["StorageTotal"], false));
	}
	
	//+ Display
	if(formFieldCollection[refId]["CallBack"] != null) {
		formFieldCollection[refId]["DisplayTotal"] = eval(formFieldCollection[refId]["CallBack"] + "(formFieldCollection, tempTotal, 'display')");
	} else {
		formFieldCollection[refId]["DisplayTotal"] = tempTotal;
	}
	if(formFieldCollection[refId]["Display"]["Type"] == "field") {
		$("#" + formFieldCollection[refId]["Display"]["LocationId"]).val(FormatNumber(formFieldCollection[refId]["DisplayTotal"], formFieldCollection[refId]["Display"]["IsCurrency"]));
	} else if(formFieldCollection[refId]["Display"]["Type"] == "span") {
		$("#" + formFieldCollection[refId]["Display"]["LocationId"]).text(FormatNumber(formFieldCollection[refId]["DisplayTotal"], formFieldCollection[refId]["Display"]["IsCurrency"]));
	}
	return;
}

/*
Common Functions
*/

//- RawNumber -//
function RawNumber(num) {
	var num;
	var sign;
	var cents
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0.00";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

//- FormatNumber -//
function FormatNumber(num, isCurrency) {
	var num;
	var sign;
	var cents
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0.00";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	var curSym = '';
	if(isCurrency == true) curSym = '$'
	return (((sign)?'':'-') + curSym + num + '.' + cents);
}

//- CalcKeyCode -//
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

//- FilterInput -//
function FilterInput(el) {
  var strPass = el.value;
  var strLength = strPass.length;
  var lchar = el.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if ((cCode < 48 || cCode > 57) && ((cCode != 46) && (cCode != 44))) {
    var myNumber = el.value.substring(0, (strLength) - 1);
    el.value = myNumber;
  }
  return false;
}