var oCol = new Array();
var oMatch = new Array();
var mbLoading = true;
var mbConsoldatedProduct = false;
var mbSubmitted = false;
var mlThisAttachmentID = 0 ;
									
function addArrayItem(oCol, lProductID, lValueID, lProductAttributeID, lAttachmentID){
	var iIndex = oCol.length;
	oCol[iIndex] = new Object();
	oCol[iIndex].lProductID = lProductID;
	oCol[iIndex].lValueID = lValueID;
	oCol[iIndex].lProductAttributeID = lProductAttributeID;
	oCol[iIndex].lAttachmentID = lAttachmentID;
							
}
						
function selectOption(){

	if (mbLoading){
		return;
		}

	if (mbConsoldatedProduct==false){
		return true;
	}
		
	var lProductID = findProductID();
	if (lProductID==0){
		alert('Sorry,  this combination is not available.');
		showPriceDiv('divprice_notavailable');
		//showStockDiv('divstock_notavailable');
		return false;
	}else{
		changeImage(lProductID, mnInstallationID);
		showPriceDiv('divprice_' + lProductID);
		//showStockDiv('divstock_' + lProductID);
		return true;
														
	}
}
						
function showPriceDiv(sName){
								
	hideElements('div','divprice');
							
	if (!document.getElementById){ return };
	var oEle = document.getElementById(sName);
	var oStyle = oEle.style;
							
	if (oStyle.display =='block'){
			oStyle.display='none';
	}else{
		oStyle.display='block';
	}
}
						
function showStockDiv(sName){

	hideElements('div','divstock');

	if (!document.getElementById){ return };
	var oEle = document.getElementById(sName);
	var oStyle = oEle.style;
							
	if (oStyle.display =='block'){
			oStyle.display='none';
	}else{
		oStyle.display='block';
	}
}
						
function hideElements(sTagName, sPrefix){
	var colEle = document.getElementsByTagName(sTagName);
	var i=0;
	var sID = "";
	for(i=0;i<colEle.length;i+=1){
		//check the prefix
		sID = colEle[i].id;
		if (sID.indexOf(sPrefix)==0){
				hideDiv(sID);
			}
	}
}
						
function hideDiv(sName){
	if (!document.getElementById){ return };
							
	var oEle = document.getElementById(sName);
	var oStyle = oEle.style;
				
	oStyle.display='none';
				
}
		
function findProductID(){
	var j=0;
	var nOptionCount = 0 ;
	oMatch = new Array();
														
	for (j=0;j<document.forms["frmAdd"].elements.length;j++) {
        if (document.forms["frmAdd"].elements[j].tagName == "SELECT") {
			var oEle = document.forms["frmAdd"].elements[j];
			var lAttID = 0;
			var lOptionID = 0;
			var sName = oEle.name;
			var lAttID = sName.substring(14,sName.length);
			lOptionID = oEle[oEle.selectedIndex].value;
			nOptionCount +=1;
			//Search for the productID 
			var n=0;
			if(oCol){
				for (n=0;n<oCol.length;n++) {
					//Find a match for all records
					var s = oCol[n].lProductID + oCol[n].lValueID + oCol[n].lProductAttributeID;
					var lProductID = 0;
					if (oCol[n].lValueID==lOptionID&&oCol[n].lProductAttributeID==lAttID){
						lProductID = oCol[n].lProductID;
						addCount(lProductID);
						//alert('found');
					}	
				}
			}
		}
    }
	lProductID = getProductWithMax(nOptionCount);
	//Get the attachment ID from the combination array
	mlThisAttachmentID = getAttachmentID(lProductID);						
	return lProductID;
}
						
function getAttachmentID(lProductID){
						
	var n = 0;
	for (n=0;n<oCol.length;n++) {
		if(oCol[n].lProductID==lProductID){
			return oCol[n].lAttachmentID;
		}
	}
	return 0;
							
}
function getProductWithMax(nOptionCount){
	//The one with the highest number of hits is the product to select
	//-Thats because it must the the only one that matches ALL of the select options currently 
	//- on the page (ie the combination that the user has selected.
	var n = 0;
	var bFound = false;
	for (n=0;n<oMatch.length;n++) {
		//alert(oMatch[n].lProductID + ', ' + oMatch[n].lCount +', ' + nOptionCount);
		if(oMatch[n].lCount==nOptionCount){
			bFound = true;
			return oMatch[n].lProductID;
		}
	}
	if (!bFound){
		return 0;
	}
}
						
function addCount(lProductID){
	//Check if it exists
	var n = 0;
	var bFound = false;
	for (n=0;n<oMatch.length;n++) {
		if (oMatch[n].lProductID==lProductID){
			oMatch[n].lCount += 1;
			bFound = true;
		}
	}
	//Add it if not
	if (!bFound){
		var iIndex = oMatch.length;
		oMatch[iIndex] = new Object();
		oMatch[iIndex].lProductID = lProductID;
		oMatch[iIndex].lCount = 1;
	}
}
						
function goPopup(lLinkID){
	
	var sFeatures = "scrollbars=yes,toolbar=no,titlebar=no,resizable=yes";
	var sURL="/default.aspx?page=isellinfo&linkid=" + lLinkID + "&installation=" + mnInstallationID;
							
	//sURL = 'downloadhandler.axd?type=2&id=6&ins=5';
	window.open(sURL, null, sFeatures)

}					
							
function goAddEnquiry(){
									
			/*
			if (selectOption()==false){
				return;
			}
			*/
			if (document.frmAdd.txtOptions.value==''){
				alert('Please advise when this item is required in the box provided');
				return;
			}									
			document.frmAdd.action = "/default.aspx?page=iselladdtoenquiry";
			document.frmAdd.submit();
						
	}
							
function goAddOrder(){

			if (mbSubmitted==true){
				alert('Page already submitted.');
				return;	
			}								
									
			if (selectOption()==false){
				return;
			}
									
			if (checkOptions()==false){
				return;
			}
																		
			if (document.frmAdd.txtQuantity.value==''){
				alert('Please enter the quantity required.');
				return;
			}
			//document.frmAdd.action = "/default.aspx?page=iselladdtoenquiry";
			mbSubmitted=true;
			document.frmAdd.submit();
						
	}

	var mlChildProductID = 0;
							
	function setLists(){
							
		if (mlChildProductID==0){
			return;
		};
		var n=0;
		for (n=0;n<oCol.length;n++) {
			if (oCol[n].lProductID == mlChildProductID){
				var sElementName = "cbo_attribute_" + oCol[n].lProductAttributeID;
				document.getElementsByName(sElementName)[0].value = oCol[n].lValueID
			}
		}
									
		selectOption();
	}

	function zoom() {
	    var lProductID = 0;
	    var sURL = "";
	    //Determine if consolidated Product
	    if (oCol.length == 0) {
	        //Not consol product 
	        lProductID = document.forms["frmAdd"].elements["product"].value;
	    } else {
	        lProductID = findProductID();
	    }

	    var sFeatures = "status=no,unadorned=yes,center=yes,resizable=yes,scrollbars=yes";

	    sURL = "/default.aspx?page=isellviewimage&amp;installation=" + mnInstallationID + "&amp;product=" + lProductID;
	    window.open(sURL, null, sFeatures);

	}
												

