
function selectedConfiguration()
{
	try
	{		
		this.selectedComponents = new Array();
		this.selectedAccessories = new Array();	
		
		/// used to dynamically update pulldown menus
		this.options = new Array();
	} 
	catch (e) 
	{
		alert(e);
	}	
}		

selectedConfiguration.prototype.checkForNullSelection = function(form,pageid){

	var toReturn = true;

	try
	{
		
		var sel = document.getElementsByTagName("select");
	
		for(i = 0;i< sel.length;i++){
			
			if(sel[i].value == "null"){
			
				alert("You must make a selection for each option before you can go to the next step.")
				return false;
					
			}

		}
		
		if(pageid == 1)
		{
		
			for(i = 0;i < document.forms[0].elements["section1value:2"].length;i++)
			{	
				if(document.forms[0].elements["section1value:2"][i].checked)
				{	
					return true;	
				}
			}
		
			alert("You must select a cabinet size before you can go to the next step.");
				
			return false;
		}	
	} 
	catch (e) 
	{
		alert(e);
	}
		
	return true;	
}

/**
* responsible for initializing an Accessory object for each passed in accessory
**/
selectedConfiguration.prototype.addAccessory = function(index,cost,quantity)
{
	try 
	{
		this.selectedAccessories[index] = new Accessory(cost,quantity);
	} 
	catch (e) 
	{
		alert(e);
	}
}



selectedConfiguration.prototype.addComponent = function(index,smart_number_value,cost,quantity){

	
	try {
		
	
		this.selectedComponents[index] = new Component(smart_number_value,cost,quantity);
		
	
		
	} catch (e) {
	
		alert(e);
	}
}

selectedConfiguration.prototype.updateImage = function(){

	document.getElementById("configuration_image").src = this.getImageName();
}


/**
* Responsible for updating the accessoryPrice span tag with the current accessory price total
**/
selectedConfiguration.prototype.updateAccessory = function(id,cost,qty,form_field)
{	
	try
	{
	
		if(!parseInt(qty)){
			form_field.value = "";
			qty = 0;
		} else {
			var i = parseInt(qty);
			form_field.value = i;
			qty = i;
		
		}
		this.selectedAccessories[id] = new Accessory(cost,qty);
		document.getElementById("accessoryPrice").innerHTML = this.getAccessoryCost();

	} catch (e){
		alert(e);
	}
}

selectedConfiguration.prototype.updateConfiguration = function(smart_number_index,value,form,size){
	
	
	if(size != null && smart_number_index == 1){
		
		if(size == "23"){
			document.forms[0].elements["section1value:1"][1].checked = true;
			// update component
			this.addComponent(8,"23",0,null);
			
		} else if (size == "19"){
			document.forms[0].elements["section1value:1"][0].checked = true;
			this.addComponent(8,"19",0,null);
		}
		
		
	}

	if(smart_number_index == "9" && this.options.length < 1){
		
		var frm = document.forms[0].elements["section1value:2"].options;
		
		
		for(x = 0; x < frm.length; x++){
		
			var t = frm[x].text;
			var v = frm[x].value;
			
			this.options[x] = {text:t,value:v};
		
		}	
		
		//this.options = document.forms[0].elements["value:2"].options;
	}
		
	this.enforceDependencyRule(smart_number_index,value,form);
	
	if(value != "null"){
	
		var params = value.split(":");	
		this.addComponent(smart_number_index,params[1],params[3],params[2]);
		document.getElementById("componentPrice").innerHTML = this.getConfigurationCost();
		document.getElementById("smartNumber").innerHTML = this.getSmartNumber();
		document.getElementById("configuration_image").src = this.getImageName();

	}
}


selectedConfiguration.prototype.enforceDependencyRule = function(smart_number_index,value,form){
	
	
	if(smart_number_index == "9"){

	
		var params = value.split(":");
		
		document.forms[0].elements["section1value:2"].options.length = 0;
		document.forms[0].elements["section1value:2"].options[0] = new Option(this.options[0].text,this.options[0].value);
		document.forms[0].elements["section1value:2"].options[1] = new Option(this.options[1].text,this.options[1].value);
		
		if(params[1] == "Z"){		
				
			document.forms[0].elements["section1value:2"].options[2] = new Option(this.options[2].text,this.options[2].value,"",true);
					
		} else {
			
			document.forms[0].elements["section1value:2"].options[2] = new Option(this.options[3].text,this.options[3].value);
			document.forms[0].elements["section1value:2"].options[3] = new Option(this.options[4].text,this.options[4].value);
			document.forms[0].elements["section1value:2"].options[4] = new Option(this.options[5].text,this.options[5].value);
		
		} 
	}
	
}

selectedConfiguration.prototype.getCurrency = function(i){

	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	
	if(s.indexOf('.') < 0) { 
			s += '.00'; 
	}
	if(s.indexOf('.') == (s.length - 2)) { 
		s += '0'; 
	}
	
	return s;

}

/**
*	Responsible for generating the sum of all entered accessories
**/
selectedConfiguration.prototype.getAccessoryCost = function()
{
	var toReturn = parseInt(0);
	
	for(accessory in this.selectedAccessories)
	{
		toReturn = toReturn + parseFloat(this.selectedAccessories[accessory].Cost * this.selectedAccessories[accessory].Quantity);	
	}

	return this.getCurrency(toReturn);
}

/**
*	Responsible for generating the sum of all selected cabinet configuration components
**/
selectedConfiguration.prototype.getConfigurationCost = function()
{
	var toReturn = parseInt(0);
	
	for(i=0;i<12;i++){
		if(this.selectedComponents[i] != null){
			toReturn = toReturn + parseFloat(this.selectedComponents[i].Cost);	
		}
	}
	
	/**
	for(a in this.selectedAccessories){
		toReturn = toReturn + parseFloat(this.selectedAccessories[a].Cost * this.selectedAccessories[a].Quantity);	
	}
	**/
	
	return this.getCurrency(toReturn);
}

selectedConfiguration.prototype.getImageName = function()
{	
	var toReturn = "images/";	
	var _qs = "size=thumb&";
		
	for(i=2;i<12;i++)
	{
		if(this.selectedComponents[i] != null){
			_qs += "smi_" + i + "=" + this.selectedComponents[i].SmartNumber;	
		} else {
			_qs += "smi_" + i + "=" + "Z";
		}
		if(i < 11){
		
			_qs += "&";	
		}
	}
	toReturn += "_th.png";

	return "configuration_image.aspx?" + _qs;
}


selectedConfiguration.prototype.getSmartNumber = function(){
	
	var toReturn = "P";
		
	for(i=1;i<12;i++){
		if(this.selectedComponents[i] != null){
			toReturn += this.selectedComponents[i].SmartNumber;	
		} else {
			
			if(i == 10){
				toReturn += "0";
			} else {
				toReturn += "Z";
			}
		}
	}
	return toReturn;

}

function Component(smart_number_id,cost,quantity)
{	
	this.SmartNumber = smart_number_id;
	this.Cost = cost;
	this.Quantity = quantity;

}

function Accessory(cost,quantity)
{	
	this.Cost = cost;
	this.Quantity = quantity;
}

var userConfiguration = new selectedConfiguration();


function moveDialog(){

		// determine width of browser in px
	this.base_x = function(){
		var x;
		if(document.all){
			x = document.body.clientWidth;
		} else {
			x = window.innerWidth;
		}	
		return x;
	}
	
	document.getElementById("ConfiguratorDialog").style.left = (this.base_x() - 732) / 2 ;

}

function showConfiguratorDialog(label,id){
	
	document.getElementById("ConfiguratorDialogLabel").innerHTML = label;
	document.getElementById("ConfiguratorDialogFrame").src = "dialog_box.aspx?id=" + id;
	moveDialog();
	document.getElementById("ConfiguratorDialog").style.visibility = "visible";
	
}



function getCurrency(i){

	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	
	if(s.indexOf('.') < 0) { 
			s += '.00'; 
	}
	if(s.indexOf('.') == (s.length - 2)) { 
		s += '0'; 
	}
	
	return s;

}
