//////////////////////////////////////////////////////
// File: vector.js
//
// Author: Jason Geissler
// 
// Date: Sept 3, 2003
//
// Purpose: To have a dynamic collection instead
//          of using arrays when the total quantity
//          is unknown
//////////////////////////////////////////////////////
var page;
// Vector Constructor -- constructs the object
function Vector(inc) {
	if (inc == 0) {
		inc = 100;
	}
	
	/* Properties */
	this.data = new Array(inc);
	this.increment = inc;
	this.size = 0;
	
	/* Methods */
	this.getCapacity = getCapacity;
	this.getSize = getSize;
	this.isEmpty = isEmpty;
	this.getLastElement = getLastElement;
	this.getFirstElement = getFirstElement;
	this.getElementAt = getElementAt;
	this.addElement = addElement;
	this.insertElementAt = insertElementAt;
	this.removeElementAt = removeElementAt;
	this.removeAllElements = removeAllElements;
	this.indexOf = indexOf;
	this.contains = contains
	this.resize = resize;
	this.toString = toString;
	this.sort = sort;
	this.trimToSize = trimToSize;
	this.clone = clone;
	this.overwriteElementAt;
}

// getCapacity() -- returns the number of elements the vector can hold
function getCapacity() {
	return this.data.length;
}

// getSize() -- returns the current size of the vector
function getSize() {
	return this.size;
}

// isEmpty() -- checks to see if the Vector has any elements
function isEmpty() {
	return this.getSize() == 0;
}

// getLastElement() -- returns the last element
function getLastElement() {
	if (this.data[this.getSize() - 1] != null) {
		return this.data[this.getSize() - 1];
	}
}

// getFirstElement() -- returns the first element
function getFirstElement() {
	if (this.data[0] != null) {
		return this.data[0];
	}
}

// getElementAt() -- returns an element at a specified index
function getElementAt(i) {
	try {
		return this.data[i];
	} 
	catch (e) {
		return "Exception " + e + " occured when accessing " + i;	
	}	
}

// addElement() -- adds a element at the end of the Vector
function addElement(obj) {
	obj = (obj+"").toUpperCase();
	if(this.getSize() == this.data.length) {
		this.resize();
	}
	this.data[this.size++] = obj;
}

// insertElementAt() -- inserts an element at a given position
function insertElementAt(obj, index) {
obj = (obj+"").toUpperCase();
	try {
		if (this.size == this.capacity) {
			this.resize();
		}
		
		for (var i=this.getSize(); i > index; i--) {
			this.data[i] = this.data[i-1];
		}
		this.data[index] = obj;
		this.size++;
	}
	catch (e) {
		return "Invalid index " + i;
	}
}

// removeElementAt() -- removes an element at a specific index
function removeElementAt(index) {
	try {
		var element = this.data[index];
		
		for(var i=index; i<(this.getSize()-1); i++) {
			this.data[i] = this.data[i+1];
		}
		
		this.data[getSize()-1] = null;
		this.size--;
		return element;
	}
	catch(e) {
		return "Invalid index " + index;
	}
} 

// removeAllElements() -- removes all elements in the Vector
function removeAllElements() {
	this.size = 0;
	
	for (var i=0; i<this.data.length; i++) {
		this.data[i] = null;
	}
}

// indexOf() -- returns the index of a searched element
function indexOf(obj) {
obj = (obj+"").toUpperCase();
	for (var i=0; i<this.getSize(); i++) {
		if (this.data[i] == obj) {
			return i;
		}
	}
	return -1;
}

// contains() -- returns true if the element is in the Vector, otherwise false
function contains(obj) {
obj = (obj+"").toUpperCase();
	for (var i=0; i<this.getSize(); i++) {
		if (this.data[i] == obj) {
			return true;
		}
	}
	return false;
}

// resize() -- increases the size of the Vector
function resize() {
	newData = new Array(this.data.length + this.increment);
	
	for	(var i=0; i< this.data.length; i++) {
		newData[i] = this.data[i];
	}
	
	this.data = newData;
}


// trimToSize() -- trims the vector down to it's size
function trimToSize() {
	var temp = new Array(this.getSize());
	
	for (var i = 0; i < this.getSize(); i++) {
		temp[i] = this.data[i];
	}
	this.size = temp.length - 1;
	this.data = temp;
} 

// sort() - sorts the collection based on a field name - f
function sort(f) {
	var i, j;
	var currentValue;
	var currentObj;
	var compareObj;
	var compareValue;
	
	for(i=1; i<this.getSize();i++) {
		currentObj = this.data[i];
		currentValue = currentObj[f];
		
		j= i-1;
		compareObj = this.data[j];
		compareValue = compareObj[f];
		
		while(j >=0 && compareValue > currentValue) {
			this.data[j+1] = this.data[j];
			j--;
			if (j >=0) {
				compareObj = this.data[j];
				compareValue = compareObj[f];
			}				
		}	
		this.data[j+1] = currentObj;
	}
}

// clone() -- copies the contents of a Vector to another Vector returning the new Vector.
function clone() {
	var newVector = new Vector(this.size);
	
	for (var i=0; i<this.size; i++) {
		newVector.addElement(this.data[i]);
	}
	
	return newVector;
}

// toString() -- returns a string rep. of the Vector
function toString() {
	var str = "Vector Object properties:\n" +
	          "Increment: " + this.increment + "\n" +
	          "Size: " + this.size + "\n" +
	          "Elements:\n";
	
	for (var i=0; i<getSize(); i++) {
		for (var prop in this.data[i]) {
			var obj = this.data[i];
			str += "\tObject." + prop + " = " + obj[prop] + "\n";
		}
	}
	return str;	
}

// overwriteElementAt() - overwrites the element with an object at the specific index.
function overwriteElementAt(obj, index) {
	this.data[index] = obj;
}


var cItem = new Vector(0);
var cItemList = new Vector(0);
var itemIds = new Vector(0);

function addItemId(id)
{
	id = (id+"").toUpperCase();
	eval("v"+id+" = new Vector(0);");
	itemIds.addElement(id+"");
}
function getItemPosition(id)
{
	id = (id+"").toUpperCase();
	for(var b=0;b<itemIds.getSize();b++)
	{
		if((id+"") == (itemIds.getElementAt(b)+""))
		{
			return (b*1+1);
		}
	}
	return -1;
}
function getItemId(id)
{
	id = (id+"").toUpperCase();
	return getItemPosition(id);
}

var stringArray = new Vector(0);

function addBothToList(mainItem, conflictingItem)
{
	mainItem = (mainItem+"").toUpperCase();
	conflictingItem = (conflictingItem+"").toUpperCase();
	stringArray.addElement(mainItem+" "+conflictingItem);
	
}
function addAllToLists()
{
	for(var c=0;c<stringArray.getSize();c++)
	{
		var strLine = stringArray.getElementAt(c)+"";
		var main = strLine.substring(0,strLine.indexOf(" "));
		var conflict = strLine.substring(strLine.indexOf(" ")+1);
		addToItemList(main,conflict);
		addToItemList(conflict,main);
	}
}
function addToItemList(mainItem, conflictingItem)
{
	var id = (mainItem+"").toUpperCase();
	var temp1;
	eval("temp1 = v"+id+";");
	temp1.addElement(conflictingItem+"");
}
function colorSwitch(id)
{
    var tableCell = document.getElementById(id);
	var obj = "objvwquantity" + id;
    if(document.getElementById(obj).checked)
       tableCell.className= "selected";
    else
       tableCell.className= "unsel";
}
function colorSwitch(id,bool)
{
	for(a=lastOptional;a<lastPlan;a++)
	{
		if(a!=id)
			if(document.getElementById("objvwquantity"+a).checked)
			{
				document.getElementById("objvwquantity"+a).checked = false; 
				var tableCell = document.getElementById(a);   
				tableCell.className= "unsel";     
			}
	}
	var tableCell = document.getElementById(id);
	var obj = "objvwquantity" + id;
	if(document.getElementById(obj).checked)
		tableCell.className= "selected";
	else
		tableCell.className= "unsel";
}
function itemChecked(id)
{
resetWarnings();
	var pos = getItemId(id);
	colorSwitch2(pos);

	id = (id+"").toUpperCase();
	var temp;
	eval("temp = v"+id+";");
	var boolme = true;
	for(var a=0;a<temp.getSize();a++)
	{
		if(boolme)
		{
			var warning = "Items highlighted yellow conflict with this accessory";
			setSpanHTML('s'+id,warning);
			boolme=false;
			warn =0;
		}
		var itemId = temp.getElementAt(a);
		var position = getItemId(itemId);
		var tableCell = document.getElementById(position);
		
		if(isChecked(position))
		{
			unselectRow(position);
		}
		tableCell.className="yellow";
	}


	
}
function setSpanHTML(spanID,text)
{
	var span = document.getElementById(spanID);
	span.innerHTML = text;
}
var warn =0;
function resetWarnings()
{
	if(warn>=1)
	if(page=='item'){
		for(var x=0;x<itemIds.getSize();x++)
		{
			var id1 = itemIds.getElementAt(x);
			setSpanHTML('s'+id1,"");
			var pos = getItemId(id1);
			
			var tableCell = document.getElementById(pos);
			if(tableCell.className=="yellow")
			{
				tableCell.className="unsel";
			}
		}
		warn =0;
	}
	warn++;
}
function colorSwitch2(idPos)
{
	var id = idPos;
	var tableCell = document.getElementById(id);
	var obj = "objvwquantity" + id;
     if(document.getElementById(obj).checked)
       tableCell.className= "selected";
    else
       tableCell.className= "unsel";
	
}

function isChecked(idPos)
{
	try{		
		var itemCheck = document.getElementById("objvwquantity" + idPos);
		return itemCheck.checked;
	}catch(e){return false;}
}
function colorSwitchFinisher(id)
{
	for(a=firstFinisher;a<lastFinisher;a++){
	    if(a!=id){
			if(document.getElementById("objvwquantity"+a).checked)
			{
				document.getElementById("objvwquantity"+a).checked = false;
				document.getElementById("finisherSpan").innerHTML = 'Only one finisher can be installed in a machine.' 
				var tableCell = document.getElementById(a);
				tableCell.className= "unsel";
			}
		}
	}
	
	var tableCell = document.getElementById(id);
	var obj = "objvwquantity" + id;
	if(document.getElementById(obj).checked){
		tableCell.className= "selected";
	}
	else{
		tableCell.className= "unsel";
	}
}

//print selection/unselection control
var printShown = false;

function colorSwitchPrint(id)
{
	for(x in printArray)
	{
		if(id == printArray[x])
		{
			for(z in printArray)
			{
				var y = printArray[z];
				if(y!=id)
				{
					if(document.getElementById("objvwquantity"+y).checked)
					{
						
						if(!printShown)
						{
							printShown = true;
							var stars = document.getElementsByName('redstar');
							for(p in stars)
							{
								var span = stars[p];
								span.innerHTML = '*';
							}
						}
						document.getElementById('printSpan').innerHTML = '*Certain accessories are limited to one option per unit.';
						unselectRow(y);
					}
				}
			}
			break;
		}
	}
	var tableCell = document.getElementById(id);
	var obj = "objvwquantity" + id;
	if(document.getElementById(obj).checked){
		tableCell.className= "selected";
	}
	else{
		tableCell.className= "unsel";
	}
}
function selectRow(id)
{
	var tableCell = document.getElementById(id);
	document.getElementById("objvwquantity"+id).checked = true;
	tableCell.className= "selected";
}
function unselectRow(id)
{
	var tableCell = document.getElementById(id);
	document.getElementById("objvwquantity"+id).checked = false;
	tableCell.className= "unsel";
}

function openMe(url)
{
	var loc = window.location+"";
	var lastInd = loc.lastIndexOf('/');
	var urlNew = loc.substring(0,lastInd)+'/'+url;
	var newwindow = window.open(urlNew,"","height=500,width=500,scrollbars=1");
	if (!newwindow.opener) 
		newwindow.opener = self;

}

var noConfig = false;
function orderButton()
{
	var loc = window.location+"";
	var ret = true;

	if(noConfig)
	   return true;
	   
	if(loc.indexOf("theID=")>=0)
	{
		ret = false;
		var lent = loc.indexOf("theID=")+6;
		var oid = loc.substring(lent).valueOf();
		window.opener.selectRow(oid);
		window.close();
	}

	return ret;
}

function addCart()
{
	noConfig=true;
	document.getElementById('frmOrder').submit();
}
function writeButton()
{
	var configButton = "/lib/yhst-68413443815062/configblue.gif";
	var cartBut= "/lib/yhst-68413443815062/cartblue.gif";
	var buttonCode = "<input style=\"display:inline;\" type=\"image\" src=";
	var loc = window.location+"";
	
	if(loc.indexOf("theID=")>=0)
	{
		buttonCode+=configButton+"><a href=\"javascript:addCart();\"><img border=0 src="+cartBut+"></a>";
	}
	else
	{
		buttonCode+=cartBut+">";
	}

	document.write(buttonCode);

}

function product(id)
{
	id = "ii"+id;
	if(document.getElementById(id)!=null)
	{
		var text = document.getElementById(id).value;
		openMe(text);
	}

}






