//find start position
function getStartPos(sForm, sField, iDimension) {
	bFound=false;
	i=0;
	tFormField = eval('document.' + sForm + '.' + sField);
  
  
  if ( tFormField.type == 'hidden' ) {
    
  	tempNum = tFormField.value;
  } else {
    
  	tempNum = tFormField[tFormField.selectedIndex].value;
  }
  do {
		if (q[i]&&q[i][iDimension]) {
			if (q[i][iDimension] == tempNum) {
				bFound=true;
				iStartPos=i;
			}
		}
		i++;
	} while((bFound==false)&&(i<q.length));
	return iStartPos;
}

//find end position
function getEndPos(iDimension,iStartPos) {
	bFound=false;
	i=iStartPos;
	do {
		if (q[i]&&q[i][iDimension]) {
			if (q[i][iDimension] != '') {
				bFound=true;
				iEndPos=i;
			}
		}
		i++;
	} while((bFound==false)&&(i<q.length));
	if (bFound == false) {
		iEndPos = q.length;
	}
	return iEndPos-1;
}


//populate makes
function loadMakes(sFormName, sFieldName, sSeries, sModel, sSiteMake, sSelectedSeries, sSelectedModel) {
	tempDocument = eval('document.' + sFormName);
	with (tempDocument) {
		tempFieldName = eval(sFieldName);
		//empty makes box
		for (i=0;i<tempFieldName.length;i++) {
			tempFieldName.options[i]=null
		}
		tempFieldName.options.length = 0;
		
		j=0;
		for (i=0;i<q.length-1;i++) {
			if (q[i]&&q[i][1]) {
				tempVal = q[i][1];
				var tempOption = new Option(tempVal, tempVal)
				tempFieldName.options[j] = tempOption;
				if ( sSiteMake == tempFieldName.options[j].value ) {
					tempFieldName.options[j].selected = true;
				}
				j++;
			}
		}
	}
	loadSeries(sFormName, sSeries, sFieldName, sModel, sSelectedSeries, sSelectedModel);
}


//populate series
function loadSeries(sFormName, sFieldName, sParentField, sModel, sSelectedSeries, sSelectedModel) {
	tempDocument = eval('document.' + sFormName);
	with (tempDocument) {
		tempFieldName = eval(sFieldName);
		//empty makes box
		for (i=0;i<tempFieldName.length;i++) {
			tempFieldName.options[i]=null
		}
		tempFieldName.options.length = 0;
		
		//determine range
		iStartPosition = getStartPos(sFormName, sParentField, 1);
		iEndPosition = getEndPos(1,iStartPosition+1);
				
		j=0;
		for (i=iStartPosition;i<iEndPosition+1;i++) {
			if (q[i]&&q[i][2]!='') {
				tempVal = q[i][2];
				var tempOption = new Option(tempVal, tempVal)
				tempFieldName.options[j] = tempOption;
				if ( sSelectedSeries == tempFieldName.options[j].value ) {
					tempFieldName.options[j].selected = true;
				}
				j++;
			}
		}
	}
	loadModel(sFormName, sModel, sFieldName, sSelectedModel);
}

//populate model
function loadModel(sFormName, sFieldName, sParentField, sSelectedModel) {
	tempDocument = eval('document.' + sFormName);
	with (tempDocument) {
		
		//empty makes box
		try{
      tempFieldName = eval(sFieldName);
      if (tempFieldName != undefined) {
        for (i=0;i<tempFieldName.length;i++) {
    			tempFieldName.options[i]=null
    		}
    		tempFieldName.options.length = 0;
    		
    		//first option
    		var tempOption = new Option('Select Model', '')
    		tempFieldName.options[0] = tempOption;
    		
    		//determine range
    		iStartPosition = getStartPos(sFormName, sParentField, 2);
    		iEndPosition = getEndPos(2,iStartPosition+1);
    		
    		j=1;
    		for (i=iStartPosition;i<iEndPosition+1;i++) {
    			if (q[i]&&q[i][3]!='') {
    				tempVal = q[i][3];
    				tempTrendLine = q[i][0];
    				var tempOption = new Option(tempVal, tempTrendLine)
    				tempFieldName.options[j] = tempOption;
    				if ( sSelectedModel == tempFieldName.options[j].value ) {
    					tempFieldName.options[j].selected = true;
    				}
    				j++;
    			}
        }
      }
		}catch(ex){return null;}
	}
}
