//displayProductDetails.js
function fOnload() {
 	if(hasOptions) {
 		fireOnChangeEvents();
 		updatePrices();
 		updatePictures();
 	}
}

function fireOnChangeEvents() {
	var stillProcessing = true;
	var index = 1;
	var temp = '';
	while(stillProcessing) {
		var elementOption = document.getElementById('cmbOption' + index);
		if(elementOption) {
			elementOption.onchange();				
		} else {
			stillProcessing = false;
		}
		index++;
	}
}

function findPosition(arr, match) {
	notFound = true;
	position = 0;
	while(notFound) {
		if(arr[position] == match) {
			notFound = false;
		} else {
			position++;
		}			
	}
	return position;
}
		
function fValidQty (rows) {
    if (rows==0) {
        //standalone item
        if (document.frmProduct.dfQty.value=='') {
            alert('Please specify a valid positive integer value for the quantity');
            return false;
        }
    } else {
        //variants list
        var validQty=false;
        for (var i=0; i<rows; i++) {
            if (eval('document.frmProduct.dfQty'+i).value!='') {
                validQty=true;
                break;
            }
        }
        if (!validQty) {
            alert('Please specify a valid positive integer value for the quantity');
            return false;
        }
    }
    return true;
}

function fAddToShoppingCart(rows, returnToProductPage) {
    
    document.frmProduct.formAction.value='INSERT';
    document.frmProduct.rows.value=rows;
    document.frmProduct.returnToProductPage.value=returnToProductPage;
    
    if (rows == 0) {
    	//standalone item
    } else {
    	//variants list
    }
    if(hasOptions) {
	    createOptionString();
	    createOptionIdString();
    }
    document.frmProduct.submit();
}

function fChangeOption(index, optionElement) {
	updatePrices();
	updatePartNo();
}

function createOptionString() {
	var stillProcessing = true;
	var firstEntry = true;
	var index = 1;
	var temp = '';
	while(stillProcessing) {
		var elementCatagory = document.getElementById('dfCatagory' + index);
		var elementOption = document.getElementById('cmbOption' + index);
		if(elementCatagory) {
			var catagoryName = elementCatagory.childNodes[0].nodeValue;
			var optionName = elementOption.options[elementOption.selectedIndex].childNodes[0].nodeValue;
			if(firstEntry) {			
				temp += catagoryName + optionName;
				firstEntry = false;
			} else {
				temp += ',' + catagoryName + optionName;
			}
		} else {
			stillProcessing = false;
		}
		index++;
	}
	document.frmProduct.Options.value=temp;
}

function createOptionIdString() {
	var stillProcessing = true;
	var firstEntry = true;
	var index = 1;
	var temp = '';
	while(stillProcessing) {
		var elementOption = document.getElementById('cmbOption' + index);
		if(elementOption) {
			var optionId = elementOption.options[elementOption.selectedIndex].value;
			if(firstEntry) {			
				temp += optionId;
				firstEntry = false;
			} else {
				temp += ',' + optionId;
			}
		} else {
			stillProcessing = false;
		}
		index++;
	}
	document.frmProduct.OptionIds.value=temp;
}
		
function getOptionTotalPrice() {
	var stillProcessing = true;
	var index = 1;
	var temp = '';
	var totalOptionPrice=0;
	while(stillProcessing) {
		var elementOption = document.getElementById('cmbOption' + index);
		if(elementOption) {
			var optionId = elementOption.options[elementOption.selectedIndex].value;
			var price = optionPriceList[findPosition(optionIdList, optionId)];
			if(price == 'null') {
			} else {
				totalOptionPrice += parseFloat(price);
			}		
		} else {
			stillProcessing = false;
		}
		index++;
	} 
	return totalOptionPrice;
}
		
function fOpenProduct(productGroupId, productDetails) {
	document.frmProduct.Item.value=productGroupId;
	document.frmProduct.formAction.value='DISPLAY_' + productDetails;
	document.frmProduct.submit();
}

function fDisplayProducts(sDiv, sCls, nStart, nRows, nCountPage, sSearchText, sOrderBy) {
	//document.frmProducts.DIVISION_ID.value=sDiv;
	//document.frmProducts.CLASS_ID.value=sCls;
	if (nStart<1) nStart=1;
	document.frmProduct.formAction.value="DISPLAY_CATALOG";
	document.frmProduct.S.value=nStart;
	document.frmProduct.R.value=nRows;
	document.frmProduct.C.value=nCountPage;
	document.frmProduct.dfSearch.value=sSearchText;
	document.frmProduct.OrderBy.value=sOrderBy;
	document.frmProduct.submit();
}

function fOnNext()
{
	document.frmPrevNext.formAction.value='DISPLAY_NEXT_PRODUCT_DETAILS';
	document.frmPrevNext.submit();
}

function fOnPrev()
{
	document.frmPrevNext.formAction.value='DISPLAY_PREVIOUS_PRODUCT_DETAILS';
	document.frmPrevNext.submit();
}

	function fUpdate() {
		updatePictures();
		updatePrices();
		updatePartNo();
	}
	
	function updatePartNo(){
		var stillProcessing = true;
		var index = 1;
		var temp = '';
		var optionPartNo="";
		while(stillProcessing) {
			var elementOption = document.getElementById('cmbOption' + index);
			if(elementOption) {
				var optionId = elementOption.options[elementOption.selectedIndex].value;
				var partNo = optionPartNoList[findPosition(optionIdList, optionId)];
				if(partNo == 'null') {
				} else {
					optionPartNo += partNo;
				}		
			} else {
				stillProcessing = false;
			}
			index++;
		} 
		document.getElementById('totalOptionPartNoDisplayArea').innerHTML=itemPartNoPrefix + optionPartNo;	
		document.frmProduct.OptionPartNo.value= optionPartNo;
		
	}
	
	
	function fChangeDivLayerContent(ilayerID, divlayerID, sContent){
		//alert('ilayerID=' + ilayerID + ', divlayerID=' + divlayerID + ', sContent=' + sContent);
	    if (ie || ns6) {
	        document.getElementById(divlayerID).innerHTML=sContent;
	    } else {
	        eval('document.'+ilayerID+'.document.'+divlayerID+'.document').write(sContent);
	        eval('document.'+ilayerID+'.document.'+divlayerID+'.document').close();
	    }
	};
	

	
	function getOptionTotalPrice() {
		var stillProcessing = true;
		var index = 1;
		var temp = '';
		var totalOptionPrice=0;
		while(stillProcessing) {
			var elementOption = document.getElementById('cmbOption' + index);
			if(elementOption) {
				var optionId = elementOption.options[elementOption.selectedIndex].value;
				var price = optionPriceList[findPosition(optionIdList, optionId)];
				if(price == 'null') {
				} else {
					totalOptionPrice += parseFloat(price);
				}		
			} else {
				stillProcessing = false;
			}
			index++;
		} 
		return totalOptionPrice;
	}
	
	
		function formatNumber(nNumber) {
	    var numberStr = '' + nNumber;
	    var lastIndexOfDot = numberStr.lastIndexOf('.');
	    if ( lastIndexOfDot == -1 ){
	        return numberStr+'.00';
	    } else {
	
	        if ( lastIndexOfDot + 2 == numberStr.length ){
	            return numberStr+'0';
	        } else {
	            return numberStr;
	        }
	    }
	}
	
	function fPopulateChildrenCombo(nOptionIndex, nSelectedOption) {
		//alert('fPopulateChildrenCombo');
	    var childCID;
	    var childComboIndex='';
	    var childDIDArray;
	
	    // populate each child combo
	    for ( var i = 0; i < eval('asChildCID'+nOptionIndex).length; i++ ){
	         // get child CID
	        childCID = eval('asChildCID' + nOptionIndex)[i];
	        // get child combo index
	        childComboIndex = fGetIndex(asCID,childCID);
	        childComboIndex++;
	        // get new options of the child combo
	        childDIDArray = fGetChildDIDArray( eval('asChildDID'+nOptionIndex)[nSelectedOption], childCID);
	
	        if (childDIDArray.length==0){
	            fPopulateChildCombo(eval('asDID'+childComboIndex),childComboIndex);
	        } else {
	            fPopulateChildCombo(childDIDArray,childComboIndex);
	        }
	    }
	}
	

	

	
	function fPopulateChildCombo(childDIDArray,childComboIndex){
	    var childDIDIndex='';
	    var selIndex=0;
	
	   // populate the child combo
	    eval('document.frmProduct.cmbOption'+childComboIndex).length=childDIDArray.length;
	
	    for (var i=0;i<childDIDArray.length;i++) {
	        childDIDIndex = fGetIndex(eval('asDID'+childComboIndex),childDIDArray[i]);
	        eval('document.frmProduct.cmbOption'+childComboIndex).options[i].text= eval('asOptionName'+childComboIndex)[childDIDIndex];
	
	//        if (asCIDType[childComboIndex-1]=='2') {
	            eval('document.frmProduct.cmbOption'+childComboIndex).options[i].value= eval('asOptionValue'+childComboIndex)[childDIDIndex];
	//        }
	
	        if (eval('asSelected'+childComboIndex)[childDIDIndex]=='1'){
	            selIndex=i;
	        }
	    }
	
	    eval('document.frmProduct.cmbOption'+childComboIndex).options.selectedIndex=selIndex;
	

	}
	
	function fGetIndex(array,value) {
	       for (var i=0;i<array.length;i++){
	
	             if (array[i] == value) {
	                return i;
	            }
	        }
	}
	
	function fGetChildDIDArray(childrenDIDArray, childCID) {
	    var childDIDArray=new Array();
	    var j=0;
	    for (var i=0;i<childrenDIDArray.length-1;i=i+2){
	        if (childrenDIDArray[i] == childCID){
	            childDIDArray[j] = childrenDIDArray[i+1];
	            j++;
	        }
	    }
	    return childDIDArray;
	}
	
	function ImgObjArrayInit(nLength) {
	    this.length = nLength;
	}
	
	function fOnOptionChange(nOptionIndex, nSelectedOption){
	     //get correct index in the img array

	    var index = fGetIndex( eval('asOptionName'+nOptionIndex),
	                            eval('document.frmProduct.cmbOption'+nOptionIndex).options[nSelectedOption].text );
	    nSelectedOption=index;
	
	    if (((navigator.appName=='Netscape')
	                    && (eval('imgObj'+nOptionIndex)[nSelectedOption]==undefined))
	                    || (eval('imgObj'+nOptionIndex)[nSelectedOption]!='[object]')) {
	            eval('imgObj'+nOptionIndex)[nSelectedOption]=new Image();
	            eval('imgObj'+nOptionIndex)[nSelectedOption].src=eval('asImg'+nOptionIndex)[nSelectedOption];
	    }
	    eval('i'+nOptionIndex).src=eval('imgObj'+nOptionIndex)[nSelectedOption].src;    
	}
	
	function addLargePic(pic) {
	    var imgSrc = '';
	    if (window.location.search=='') {
	        imgSrc = '../graphics/orderportal' + pic;
	    } else {
	        var asToken = window.location.search.split("addl_pic=");
	        re=new RegExp('%2F','gi');
	        imgSrc = '../graphics/orderportal' + asToken[1].replace(re,'/');
	    }
	    document.write('<img src="' + imgSrc + '">');
	}
	
	function openAddlPic(pic) {
	    wProps = '';
	    if (navigator.appName!='Netspace') {
	        wProps = 'width=300,height=150,left=0,top=0,screenX=0,screenY=0,toolbar=no,menubar=no, resizable=yes';
	    }
	    wAddl=window.open('addlPic.htm?addl_pic=' + pic ,'wAddl',wProps);
	    wAddl.opener=self; wAddl.focus();
	}
	
var currentView = 'productInfor';

function switchView(inName)
{   // only switch if different view
    if (currentView != inName)
    {   // local variables
        var oNow = null;
        var oTarget = null;
        
        oNow = document.getElementById(currentView);
        oTarget = document.getElementById(inName);
        this.className = "selected";
        // check for valid target                
        if (oTarget != null) 
        {   // check for valid current information
            if (oNow != null) 
                oNow.className = "hideComment";
                
            oTarget.className = "showComment";
            currentView = inName;
            document.getElementById("product_detail").className = inName;
        }
    }
}
     
function zoomPopupPicture(){
	var zoomPopupWindow = window.open("","wndZoom","width=800,height=800,scrollbars=yes,resizable=yes"); 		    
  	document.frmProductPopup.target="wndZoom";
  	document.frmProductPopup.submit();
 	zoomPopupWindow.opener = self;
 	zoomPopupWindow.focus();
}				