var default_stroke_text  = "enter value or select from list...";
var stroke_isIE56 = document.all && ((navigator.userAgent.search(/MSIE 5/i) != -1)
	|| (navigator.userAgent.search(/MSIE 6/i) != -1));

function focusCombo(formname) {
	oText= document.getElementById(formname);
	if (oText.value == default_stroke_text) {
		oText.value = ""
		oText.style.color = "";
	}
}

function blurCombo(formname) {
	oText= document.getElementById(formname);
	var numId = oText.id.replace(/[^\d]/g,'');
	if (oText.value == "") {
		oText.value = default_stroke_text;
		oText.style.color = "silver";
	} 
	combo_check(formname);
	// close the combo
	var optionDiv = document.getElementById('selectBoxOptions' + numId);
	if (optionDiv && optionDiv.style.display=='block') {
		selectBox_hideOptions();
	}
}

function combo_check(formname) {
	var oUnit = document.forms['form'][formname+'_unit'];
	var unit = (oUnit[0].checked)?"inch":"mm";
	var oCombo = document.getElementById(formname);
	var dom_id = -1;
	try {
		dom_id = oCombo.getAttribute('domid');
	} catch(err) { }
	var state = "inch";
	try {
		state = oCombo.getAttribute('state');
	} catch(err) { }

	// convert the text value
	var text_val = parseFloat(oCombo.value);
	if (isNaN(text_val)) {
		retval = oCombo.value;
		if (retval == default_stroke_text) retval = "";
	} else {
		var initval = parseFloat(oCombo.value);
		// see if it has a unit string
		if (-1 != oCombo.value.search(/[(in)|\"]/gi)) {
		//alert("found inch")
			// found inch units
			if (state == "mm" || state == "init") {
				// flip units
				oUnit[1].checked = false;
				oUnit[0].checked = true;
				unit_change(formname);
				unit = "inch";
				retval = text_val;
			}
		} else if (-1 != oCombo.value.search(/mm/gi)) {
		//alert("found mm state="+state)
			// found MM units
			if (state == "inch" || state == "init") {
				// flip units
				oUnit[0].checked = false;
				oUnit[1].checked = true;
				unit_change(formname);
				unit = "mm";
			}
		} else {
			unit_change(formname);
		}

		if (unit == "mm") {
			oCombo.value = formatStroke(initval,1) + " mm";
			retval = Math.round(initval/25.4*100);
		} else {
			if (initval > 99) {
				// assume the entry is in 100's of an inch.
				initval /= 100;
			}
			oCombo.value = formatStroke(initval,3) + " inch";
			retval = Math.round(initval*100);
		}
	}
	checkComboChange(formname, retval);
	return retval;
}

function checkComboValue(formname,numId) {
	var oDom = oDomains.getDomainObject(numId);
	var oDomForm = document.getElementById('dom_' + numId);
	if (!setIntegerDomainValue(oDom, oDomForm, false)) {
		oRange = oDom.getConsolidatedValueRange();
		var oUnit = document.forms['form'][formname+'_unit'];
		var unit = (oUnit[0].checked)?"inch":"mm";
		var minv = oRange.minVal;// in 100th of inch
		var maxv = oRange.maxVal;// in 100th of inch
		if (unit == "mm") {
			minv = formatStroke(minv/100*25.4,4) + " mm";
			maxv = formatStroke(maxv/100*25.4,4) + " mm";
		} else {
			minv = formatStroke(minv/100,8) + " inch";
			maxv = formatStroke(maxv/100,4) + " inch";
		}
		alert("Stroke should be a numeric value between "+minv+ " and "+maxv);
		document.getElementById(formname).focus();
		document.getElementById(formname).select();
		oDomForm.value = "";
		return false;
	}
	return true;
}

function checkComboChange(formname, newvalue) {
	// set the hidden value and submit
	var oCombo = document.getElementById(formname);
	var numId = oCombo.id.replace(/[^\d]/g,'');
	var oldval = document.getElementById('dom_' + numId).value;
	if (newvalue != oldval) {
		//alert("submiting("+oldval+"):"+retval)
		var oDomForm = document.getElementById('dom_' + numId);
		oDomForm.value = newvalue;
		if (checkComboValue(formname,numId)) {
			setDomainValue(numId, oDomForm);
			formChange(numId);
		}
	} 
}


function formatStroke( num, fix ) {
	// truncate to 3 .xxx digits
	strlen = parseFloat(num).toFixed(fix);
	var dp = strlen.search(/\./gi);
	// add a leading 0 if missing
	if (dp == 0) {
		strlen = "0" + strlen;
		dp++;
	}
	//remove trailing "0"
	while(strlen.length > 1 && strlen.substring(strlen.length, strlen.length - 1) == "0") {
		strlen = strlen.substring(0, strlen.length-1);
	}
	//remove trailing "."
	if (strlen.substring(strlen.length, strlen.length - 1) == ".") {
		strlen = strlen.substring(0, strlen.length-1);
	}
	return strlen;
}

function unit_change(formname) {
	var oUnit = document.forms['form'][formname+'_unit'];
	//alert(formname+'_unit')
	var unit = (oUnit[0].checked)?"inch":"mm";
	var oCombo = document.getElementById(formname);
	var dom_id = -1;
	try {
		dom_id = oCombo.getAttribute('domid');
	} catch(err) { }
	var state = "XX";
	try {
		state = oCombo.getAttribute('state');
	} catch(err) { }
	//alert(state+"="+unit);
	if (state != unit) {
		// convert the text value
		var text_val = parseFloat(oCombo.value);
		
		if (isNaN(text_val)) {
		} else {
			if (unit == "mm") {
				oCombo.value = formatStroke(parseFloat(oCombo.value) * 25.4, 1) + " mm";
			} else {
				oCombo.value = formatStroke(parseFloat(oCombo.value) / 25.4, 3) + " inch";
			}
		}
		// convert the standard values
		formatStandardValues(formname);
		/*var no = 0;
		var options = oCombo.getAttribute('selectBoxOptions').split(',');
		for (var no=0; no<options.length; no++) {
			oOpt = document.getElementById('selectBoxOption_' + dom_id + '_' + no);
			if (oOpt) {
				// convert number
				if (unit == "mm") {
					newval = formatStroke(options[no] * 25.4, 1) + " mm";
				} else {
					newval = formatStroke(options[no], 3) + " inch";
				}
				oOpt.innerHTML = newval;
			} else {
				//alert('could not find: selectBoxOption_' + dom_id + '_' + no);
				break;
			}
		}*/
	}
	oCombo.state = unit;
	//alert("unit="+unit+" state="+oCombo.getAttribute('state'));
}

function formatStandardValues(formname) {
	var oCombo = document.getElementById(formname);
	var dom_id = oCombo.getAttribute('domid');
	var oUnit = document.forms['form'][formname+'_unit'];
	//alert(formname+'_unit')
	var unit = (oUnit[0].checked)?"inch":"mm";
	var options = oCombo.getAttribute('selectBoxOptions').split(',');
	for (var no=0; no<options.length; no++) {
		oOpt = document.getElementById('selectBoxOption_' + dom_id + '_' + no);
		if (oOpt) {
			// convert number
			if (unit == "mm") {
				newval = formatStroke(options[no] * 25.4, 1) + " mm";
			} else {
				newval = formatStroke(options[no], 3) + " inch";
			}
			oOpt.innerHTML = newval;
		} else {
			//alert('could not find: selectBoxOption_' + dom_id + '_' + no);
			break;
		}
	}
}

function checkEnter(e) { //e is event object passed from function invocation
	if (!e) var e = window.event;
	var characterCode;
	if (e.which) { //if which property of event object is supported (NN4)
		characterCode = e.which //character code is contained in NN4's which property
	} else {
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	} 

	if (characterCode == 13) { 
		//if generated character code is equal to ascii 13 (if enter key)
		var targ = e.target || e.srcElement;
		if (targ.nodeType == 3) targ = targ.parentNode // defeat Safari bug
		setTimeout("blurCombo('"+targ.id+"')");
		return false
	}
}

// original from: http://www.dhtmlgoodies.com
var arrowImage = './images/select_arrow.gif'; // Regular arrow
var arrowImageOver = './images/select_arrow_over.gif'; // Mouse over
var arrowImageDown = './images/select_arrow_down.gif'; // Mouse down 

// Path to arrow images
var arrowImage = './images/select_arrow.gif';	// Regular arrow
var arrowImageOver = './images/select_arrow_over.gif';	// Mouse over
var arrowImageDown = './images/select_arrow_down.gif';	// Mouse down

var selectBoxIds = 0;
var currentlyOpenedOptionBox = false;
var editableSelect_activeArrow = false;

function selectBox_switchImageUrl(){
	if(this.src.indexOf(arrowImage)>=0){
		this.src = this.src.replace(arrowImage,arrowImageOver);	
	} else {
		this.src = this.src.replace(arrowImageOver,arrowImage);
	}
}

function selectBox_hideOptions(e) {
	// walk up the tree and see if we are in the drop down div
	if (!e) {
		var e = window.event;
	} else {
	}
	var targ;
	
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}

	if (editableSelect_activeArrow) {
		while (targ.id.indexOf("selectBox") == -1) {
	        targ = targ.parentNode;
			if ("undefined" == typeof targ ||
				null == targ ||
				"undefined" == typeof targ.id ||
				null == targ.id ||
				"string" != typeof targ.id) {
				if (editableSelect_activeArrow) {
					var numId = editableSelect_activeArrow.id.replace(/[^\d]/g,'');
					var optionDiv = document.getElementById('selectBoxOptions' + numId);
					if (optionDiv.style.display=='block') {
						// if visible close
						editableSelect_activeArrow.onclick();
					}
				}
				break; 
			}
	    }
	}
}

function selectBox_toggleOptions() {
	if(editableSelect_activeArrow && editableSelect_activeArrow!=this) {
		editableSelect_activeArrow.src = arrowImage;
	}
	editableSelect_activeArrow = this;
	
	var numId = this.id.replace(/[^\d]/g,'');
	var optionDiv = document.getElementById('selectBoxOptions' + numId);
	if (optionDiv.style.display=='block') {
		optionDiv.style.display='none';
		if (stroke_isIE56) { 
			document.getElementById('selectBoxIframe' + numId).style.display='none';
		}
		this.src = arrowImageOver;
	} else {			
		optionDiv.style.display='block';
		selectBox_openDiv = optionDiv
		if(stroke_isIE56) {
			document.getElementById('selectBoxIframe' + numId).style.display='block';
		}
		this.src = arrowImageDown;	
		if(currentlyOpenedOptionBox && currentlyOpenedOptionBox!=optionDiv)currentlyOpenedOptionBox.style.display='none';	
		currentlyOpenedOptionBox= optionDiv;			
	}
}

function selectOptionValue(){
	var parentNode = this.parentNode.parentNode;
	var textInput = parentNode.getElementsByTagName('INPUT')[0];
	textInput.value = this.innerHTML;	
	this.parentNode.style.display='none';
	document.getElementById('arrowSelectBox' + parentNode.id.replace(/[^\d]/g,'')).src = arrowImageOver;
	textInput.style.color = "";
	if (stroke_isIE56) {
		document.getElementById('selectBoxIframe' + parentNode.id.replace(/[^\d]/g,'')).style.display='none';
	}
	combo_check(textInput.id)
}
	
var activeOption;
function highlightSelectBoxOption(){
	if (this.style.backgroundColor=='#316AC5') {
		this.style.backgroundColor='';
		this.style.color='';
	} else {
		this.style.backgroundColor='#316AC5';
		this.style.color='#FFF';			
	}	
	
	if(activeOption) {
		activeOption.style.backgroundColor='';
		activeOption.style.color='';			
	}
	activeOption = this;
}

function createEditableSelect(dest) {
	dest.className='selectBoxInput';		
	var div = document.createElement('DIV');
	div.style.styleFloat = 'left';
	div.style.width = dest.offsetWidth + 16 + 'px';
	div.style.position = 'relative';
	div.id = 'selectBox' + selectBoxIds;
	var parent = dest.parentNode;
	parent.insertBefore(div,dest);
	div.appendChild(dest);	
	div.className='selectBox';
	div.style.zIndex = 10000 - selectBoxIds;

	var img = document.createElement('IMG');
	img.src = arrowImage;
	img.className = 'selectBoxArrow';
	
	img.onmouseover = selectBox_switchImageUrl;
	img.onmouseout = selectBox_switchImageUrl;
	img.onclick = selectBox_toggleOptions;
	img.id = 'arrowSelectBox' + selectBoxIds;

	div.appendChild(img);
	
	var optionDiv = document.createElement('DIV');
	optionDiv.id = 'selectBoxOptions' + selectBoxIds;
	optionDiv.className='selectBoxOptionContainer';
	if (div.offsetWidth < 1) {
		optionDiv.style.width = (dest.offsetWidth + 16)-2 + 'px';
	} else {
		optionDiv.style.width = div.offsetWidth-2 + 'px';
	}
	div.appendChild(optionDiv);
	
	if(stroke_isIE56){
		var iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');
		iframe.style.width = optionDiv.style.width;
		iframe.style.height = optionDiv.offsetHeight + 'px';
		iframe.style.display='none';
		iframe.id = 'selectBoxIframe' + selectBoxIds;
		div.appendChild(iframe);
	}
	
	if (dest.getAttribute('selectBoxOptions')) {
		var options = dest.getAttribute('selectBoxOptions').split(',');
		var dom_id = -1;
		try {
			dom_id = dest.getAttribute('domid');
		} catch(err) { } 
		var optionsTotalHeight = 0;
		var optionArray = new Array();
		for(var no=0;no<options.length;no++){
			var anOption = document.createElement('DIV');
			anOption.innerHTML = options[no];
			if (dom_id >= 0) {
				anOption.id = 'selectBoxOption_' + dom_id + '_' + no;
			}
			anOption.className = 'selectBoxAnOption';
			anOption.onclick = selectOptionValue;
			anOption.style.width = optionDiv.style.width.replace('px','') - 2 + 'px'; 
			anOption.onmouseover = highlightSelectBoxOption;
			optionDiv.appendChild(anOption);	
			optionsTotalHeight = optionsTotalHeight + anOption.offsetHeight + 1;
			optionArray.push(anOption);
		}
		if (optionsTotalHeight > optionDiv.offsetHeight) {
			// line below added to show all options - SMC request
			optionDiv.style.height = (optionsTotalHeight+2) + 'px';
			if(stroke_isIE56){
				iframe.style.height = (optionsTotalHeight+2) + 'px';
			}
			for(var no=0;no<optionArray.length;no++) {
				optionArray[no].style.width = optionDiv.style.width.replace('px','') - 22 + 'px'; 	
			}	
		}		
		optionDiv.style.display='none';
		optionDiv.style.visibility='visible';
	}
	selectBoxIds = selectBoxIds + 1;
}	
