var shoppingCartWindow

function TM_parseTree(object, attributeName, attributeContent, string) {
	if (object.getAttribute(attributeName) == attributeContent) {
		object.firstChild.data = string;
	}
	var child = object.childNodes;
	var childCount = child.length;
	for (var count = 0; count<childCount; count++){
		if (child[count].hasChildNodes()){
			TM_parseTree(child[count], attributeName, attributeContent, string);
		}
	}
}

function TM_isFamilly(currentObject, possibleParent) {
	var parent = currentObject;
	while (parent) {
		if (parent == possibleParent) {
			return true;
		} else {
			parent = parent.parentNode;
		}
	}
	return false;
}

function TM_initializeOptions(tabPanelsGroupId) {
	var documentOptions = new Array;
	var activeTab = TM_getActiveTab(tabPanelsGroupId);
	var documentOptions = document.getElementsByTagName("input");
	for(var scan=0; scan<documentOptions.length; scan++) {
		if(	documentOptions[scan].getAttribute("type") == "checkbox" &&
			!documentOptions[scan].disabled &&
			documentOptions[scan].checked &&
			documentOptions[scan].getAttribute("onClick") != "undefined" &&
			TM_isFamilly(documentOptions[scan],activeTab)) {
				var functionCall = documentOptions[scan].getAttribute("onClick");
				if(typeof(functionCall) == "function"){
					functionCall = functionCall.toString();
					functionCall = functionCall.slice(functionCall.indexOf("{")+1,functionCall.lastIndexOf("}"));
				}
				eval(functionCall);
		}
	}
	var documentLink = document.getElementsByTagName("a");
	for(var scan=0; scan<documentLink.length; scan++) {
		if(	documentLink[scan].getAttribute("name") == "checkout" &&
			documentLink[scan].getAttribute("onClick") != "undefined" &&
			TM_isFamilly(documentLink[scan],activeTab)) {
				var functionCall = documentLink[scan].getAttribute("onClick");
				if(typeof(functionCall) == "function"){
					functionCall = functionCall.toString();
					functionCall = functionCall.slice(functionCall.indexOf("{")+1,functionCall.lastIndexOf("}"));
				}
				eval(functionCall);
		}
	}
}

function TM_product (name, language){
	this.name = name || "ERR_NAME";
	this.language = language || "ERR_LANG";
	this.ids = new Array();
	this.prices = new Array();
	this.currencies = new Array();
	this.descriptions = new Array();
	this.type = new Array();
	this.statuses = new Array();
	
	this.addPrice = addPrice;
	this.switchStatus = switchStatus;
	this.checkOut = checkOut;
	this.displayTotal = displayTotal;
	//this.dumpList = dumpList;
}

function addPrice (id, price, currency, description, type, status){
	this.ids[this.ids.length] = id;
	this.prices[this.prices.length] = price;
	this.currencies[this.currencies.length] = currency;
	this.descriptions[this.descriptions.length] = description;
	this.type[this.type.length] = type;
	this.statuses[this.statuses.length] = status;
}

function switchStatus (tabPanelsGroupId, statusTrueId, statusFalseId){
	var documentOptions = new Array;
	var quote = "'";
	var doubleQuote = '"';
	var callerFunctionReg = new RegExp("(switchStatus)[\s]*?\\([\\s]*?["+quote+doubleQuote+"]*("+tabPanelsGroupId+")["+quote+doubleQuote+"]*[\\s]*?,[\\s]*?"+statusTrueId+"[\\s]*?,[\\s]*?"+statusFalseId+"[\\s]*?\\)");
	activeTab = TM_getActiveTab(tabPanelsGroupId);
	documentOptions = activeTab.getElementsByTagName("input");
	for(var scan=0; scan<documentOptions.length; scan++) {
		if(	documentOptions[scan].getAttribute("onClick") &&
			!documentOptions[scan].disabled &&
			callerFunctionReg.test(documentOptions[scan].getAttribute("onClick"))
			) {
				sourceStatusObject = documentOptions[scan];
			}
	}
	for(var count = 0; count < this.ids.length; count++) {
		if(sourceStatusObject.checked) {
			if(this.ids[count] == statusTrueId) {
				this.statuses[count] = true;
			} else {
				this.statuses[count] = false;
			}
		} else {
			if(this.ids[count] == statusFalseId) {
				this.statuses[count] = true;
			} else {
				this.statuses[count] = false;
			}
		}
	}
}

function dynaHost(){
	var hostIdentifierDev = /(^tmpgenc-dev\.peg)/;
	var hostIdentifierBeta = /(^gamma\.peg)/;
	if (hostIdentifierDev.test(window.location.host)) {
		return "ssl-dev";
	}
	if (hostIdentifierBeta.test(window.location.host)){
		return "betax";
	}
	return "ssl";
}


function checkOut (){
	if (checkOut.arguments.length > 0){
		switch(typeof(checkOut.arguments[0])) {
			case 'number':
				var documentCheckOut = new Array();
				documentCheckOut = document.getElementsByName("checkout");
				for(var scan=0; scan<documentCheckOut.length; scan++) {
					var functionCall = documentCheckOut[scan].getAttribute("onClick");
					if (functionCall){
						if(typeof(functionCall) == "function"){
							functionCall = functionCall.toString();
							functionCall = functionCall.slice(functionCall.indexOf("{")+1,functionCall.lastIndexOf("}"));
						}
						if(functionCall.indexOf(".checkOut("+checkOut.arguments[0]+")") > -1) {
								
								if(!shoppingCartWindow || shoppingCartWindow.closed){
									shoppingCartWindow = window.open(
										'https://'+dynaHost()+'.pegasys-inc.com/webapp/cart/add/lang/en/id/'+checkOut.arguments[0],
										'shopCart',
										'width=578,height=311, scrollbars=yes, resizable=yes, status=no, toolbar=no, dependent=yes'
									);
								} else {
									shoppingCartWindow.location.href = 'https://'+dynaHost()+'.pegasys-inc.com/webapp/cart/add/lang/en/id/'+this.ids[count];
									shoppingCartWindow.focus();
								}
								
							documentCheckOut[scan].href = "https://"+dynaHost()+".pegasys-inc.com/webapp/cart/add/lang/en/id/"+checkOut.arguments[0];
							documentCheckOut[scan].setAttribute("target", "shopCart");
						}
					}
				}
				break;
			case 'object':
				var shoppingCallArg = "";
				for(var count = 0; count < this.ids.length; count++) {
					if(checkOut.arguments[0].name == "checkout"){
						if(this.statuses[count]) {
							if(isNaN(this.ids[count])) {
								checkOut.arguments[0].href = this.ids[count];
								return;
							} else {
								
								if(!shoppingCartWindow || shoppingCartWindow.closed){
									shoppingCartWindow = window.open(
										'https://'+dynaHost()+'.pegasys-inc.com/webapp/cart/add/lang/en/id/'+this.ids[count],
										'shopCart',
										'width=578,height=311, scrollbars=yes, resizable=yes, status=no, toolbar=no, dependent=yes'
									);
								} else {
									shoppingCartWindow.location.href = 'https://'+dynaHost()+'.pegasys-inc.com/webapp/cart/add/lang/en/id/'+this.ids[count];
									shoppingCartWindow.focus();
								}
								
								checkOut.arguments[0].href = "https://"+dynaHost()+".pegasys-inc.com/webapp/cart/add/lang/en/id/"+this.ids[count];
								checkOut.arguments[0].setAttribute("target", "shopCart");
							}
							return;
						}
					}
				}
				break;
			default:
				return;
		}
	} else {
		return;
	}
}

function displayTotal(currentPanelGroupId) {
	if (displayTotal.arguments.length > 1){
		currentPanelGroupId = displayTotal.arguments[0];
		totalDisplayId = displayTotal.arguments[1];
	} else {
		currentPanelGroupId = displayTotal.arguments[0];
		totalDisplayId = "total";
	}
	for(var count = 0; count < this.ids.length; count++) {
		if(this.statuses[count]) {
			var priceString = this.currencies[count] + this.prices[count];
			if(priceString.length - priceString.lastIndexOf(".") < 3){
				priceString += "0";
			}
			if(priceString.lastIndexOf(".") < 0){
				priceString += ".00";
			}
			if(this.descriptions[count].search(/ CD/) > -1) {
				swiftCDPriceString = this.currencies[count] + "12.53";
			} else {
				swiftCDPriceString = "";
			}
			TM_parseTree(TM_getActiveTab(currentPanelGroupId), "display_id", totalDisplayId, priceString);
			TM_parseTree(TM_getActiveTab(currentPanelGroupId), "display_id", "swiftCD", swiftCDPriceString);
			var activeTab = TM_getActiveTab(currentPanelGroupId);
			var documentCheckOut = new Array();
			documentCheckOut = document.getElementsByName("checkout");
			for(var scan=0; scan<documentCheckOut.length; scan++) {
				if(TM_isFamilly(documentCheckOut[scan], activeTab)) {
					documentCheckOut[scan].setAttribute("target", "shopCart");
					documentCheckOut[scan].href = "https://"+dynaHost()+".pegasys-inc.com/webapp/cart/add/lang/en/id/"+this.ids[count];
				}
			}
		}
	}
}

function dumpList (){
	var dumpListString = "";
	for(var count = 0; count < this.ids.length; count++) {
		dumpListString +=	"id: " + this.ids[count] + " " +
							"price: " + this.prices[count] + " " +
							"currency: " + this.currencies[count] + " " +
							"description: " + this.descriptions[count] + " " +
							"status: " + this.statuses[count] + "\n";
	}
	alert(dumpListString);
}

//Button initialization on page load (uses jquery)
$(window).load(function(){

	//View cart button
	$('.viewCart').click(
		function(){
			if(!shoppingCartWindow || shoppingCartWindow.closed){
				shoppingCartWindow = window.open(
					'https://'+dynaHost()+'.pegasys-inc.com/webapp/cart/index/lang/en',
					'shopCart',
					'width=578,height=311, scrollbars=yes, resizable=yes, status=no, toolbar=no, dependent=yes'
				);
			} else {
				shoppingCartWindow.focus();
			}
		}
	);

});

