var divSet = false;
var curId;
var ie = false;
var nocolor = 'none';
if (document.all) { ie = true; nocolor = ''; }

function setDirty()
{
	document.frmCustomWizard.form_dirty.value = 1;
}

function setELPDirty(iDirty)
{
	if (iDirty == -1)
	{
		if (document.frmCustomWizard.gt.value != "" || document.frmCustomWizard.gl.value != "")
			iDirty = 1;
		else
			iDirty = 0;
	}
	
	document.frmCustomWizard.elp_active.value = iDirty;
}

function setSelectColor(el)
{
	if(el.selectedIndex != -1)
	{
		el.style.color = el.options[el.selectedIndex].style.color;
		el.style.backgroundColor = el.options[el.selectedIndex].style.backgroundColor;
	}
}

function allLinesCombo_onchange(sFieldName, sApplyFrom, bUpdateHidden)
{
	var sTemp;
	var sValue;
	var sFieldBase;
	var sSelectedValue;
	var sSelectedText;
	var iSelectIndex;
	var iNdx;
	var iLineType;
	var iLineCount;
	var iLineCountUpper;
	var iLineCountLower;
	var iLineCountFlat;
	var elSelect;
	var elLine;

	sFieldBase = "document.frmCustomWizard.";
	
	iLineCountUpper = eval(sFieldBase + "TRO_CURVED_LINES_UPPER.value");
	iLineCountFlat = eval(sFieldBase + "TRO_HORIZONTAL_TEXT_LINES.value");
	iLineCountLower = eval(sFieldBase + "TRO_CURVED_LINES_LOWER.value");

	iLineCount = iLineCountUpper + iLineCountFlat + iLineCountLower;
	
	if (bUpdateHidden === undefined)
		bUpdateHidden = false;

	//get lines of text
	if (iLineCount > 1)
	{
		for (var i = 0; i <= 2; i++)
		{
			//upper curved lines
			if (i == 0)
			{
				iLineCount = iLineCountUpper;
				iLineType = 2;
			}
			//flat lines
			if (i == 1)
			{
				iLineCount = iLineCountFlat;
				iLineType = 4;
			}
			//lower curved lines
			if (i == 2)
			{
				iLineCount = iLineCountLower;
				iLineType = 8;
			}
			//skip rule distance
			if (sFieldName == 'rd' || sFieldName == 'ri') iLineType = "";
				
			// Get value of the "apply from" control
			sTemp = sFieldBase + sApplyFrom;
			elSelect = eval(sTemp);
			iSelectIndex = elSelect.selectedIndex;
			sSelectedValue = elSelect.options[elSelect.selectedIndex].value;
			sSelectedText = elSelect.options[elSelect.selectedIndex].text;
			
			if (sSelectedValue != "")
			{
				// Set values for each of the valid lines
				for (iNdx = 1; iNdx <= iLineCount; iNdx++)
				{
					//if the field exists
					if (eval(sFieldBase + sFieldName + iLineType + iNdx))
					{
						// If the field is not hidden
						if (eval(sFieldBase + sFieldName + iLineType + iNdx + ".type") != "hidden" )
						{
							elLine = eval(sFieldBase + sFieldName + iLineType + iNdx)
							for(var iOptCount = 0; iOptCount < elLine.options.length; iOptCount++)
							{
								if (elLine.options[iOptCount].value == sSelectedValue)
								{
									elLine.options[iOptCount].selected = true;
									break;
								}
							}
							eval(sFieldBase + sFieldName + iLineType + iNdx).style.color = elSelect.options[elSelect.selectedIndex].style.color;
							eval(sFieldBase + sFieldName + iLineType + iNdx).style.backgroundColor = elSelect.options[elSelect.selectedIndex].style.backgroundColor;
						} else {
							if (bUpdateHidden)
							{
								elLine = eval(sFieldBase + sFieldName + iLineType + iNdx)
								if (elLine)
									elLine.value = sSelectedValue;

								if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
								{
									if (elLine.parentElement.children.length == 2)
										if (elLine.parentElement.children(1).tagName == "SPAN")
											elLine.parentElement.children(1).innerText = sSelectedText
								} else {
									if (elLine.parentNode.childNodes.length == 4)
										if (elLine.parentNode.childNodes[2].tagName == "SPAN")
											elLine.parentNode.childNodes[2].innerHTML = sSelectedText;
								}
							}
						}
					}
				}
			}
		}
		// Set the value for this "All" line to be invalid
		sTemp = sFieldBase + sFieldName + ".selectedIndex = 0";
		eval(sTemp);
	}
	
	setDirty();
	
	return;
}

function allLinesCheck_onclick(sFieldName, sFieldDesc)
{
	var sTemp;
	var sValue;
	var iNdx;
	var sFieldBase;
	var iLineType;
	var iLineCount;
	var iLineCountUpper;
	var iLineCountLower;
	var iLineCountFlat;

	sFieldBase = "document.frmCustomWizard.";

	iLineCountUpper = eval(sFieldBase + "TRO_CURVED_LINES_UPPER.value");
	iLineCountFlat = eval(sFieldBase + "TRO_HORIZONTAL_TEXT_LINES.value");
	iLineCountLower = eval(sFieldBase + "TRO_CURVED_LINES_LOWER.value");

	iLineCount = iLineCountUpper + iLineCountFlat + iLineCountLower;
	if (iLineCount > 1)
	{
		//get lines of text
		for (var i = 0; i <= 2; i++)
		{
			//upper lines
			if (i == 0)
			{
				iLineCount = iLineCountUpper;
				iLineType = 2;
			}
			//flat lines
			if (i == 1)
			{
				iLineCount = iLineCountFlat;
				iLineType = 4;
			}
			//lower lines
			if (i == 2)
			{
				iLineCount = iLineCountLower;
				iLineType = 8;
			}
			
			//skip lineType for rules
			if (sFieldName == 'hr') iLineType = "";

			// Get value from the "All" line
			sTemp = sFieldBase + sFieldName + ".checked";
			// Get value
			sValue = escape(eval(sTemp));

			// Set values for each of the valid lines
			for (iNdx = 1; iNdx <= iLineCount; iNdx++)
			{
				//If the field exists
				if (eval(sFieldBase + sFieldName + iLineType + iNdx))
				{
					// If the field is not hidden and not disabled
					if (eval(sFieldBase + sFieldName + iLineType + iNdx + ".type") != "hidden" )
					{
						if (eval(sFieldBase + sFieldName + iLineType + iNdx + ".disabled") != true )
						{
							sTemp = sFieldBase + sFieldName + iLineType + iNdx + ".checked=" + sValue;
							eval(sTemp);
						}
					}
				}
			}
		}
		// Clear the "All" value
		sTemp = sFieldBase + sFieldName + ".value=''";
		eval(sTemp);
	}
	
	setDirty();

	return;
}

function allLinesText_onchange(sFieldName, sFieldDesc)
{
	var sTemp;
	var sValue;
	var iNdx;
	var sFieldBase;
	var iLineType;
	var iLineCount;
	var iLineCountUpper;
	var iLineCountLower;
	var iLineCountFlat;
	
	sFieldBase = "document.frmCustomWizard.";

	iLineCountUpper = eval(sFieldBase + "TRO_CURVED_LINES_UPPER.value");
	iLineCountFlat = eval(sFieldBase + "TRO_HORIZONTAL_TEXT_LINES.value");
	iLineCountLower = eval(sFieldBase + "TRO_CURVED_LINES_LOWER.value");

	iLineCount = iLineCountUpper + iLineCountFlat + iLineCountLower;

	if (iLineCount > 1)
	{
		// Get value from the "All" line
		sTemp = sFieldBase + sFieldName + ".value";
		// Get value
		sValue = escape(eval(sTemp));
		if (sValue != '')
		{
			//get lines of text
			for (var i = 0; i <= 2; i++)
			{
				//upper lines
				if (i == 0)
				{
					iLineCount = iLineCountUpper;
					iLineType = 2;
				}
				//flat lines
				if (i == 1)
				{
					iLineCount = iLineCountFlat;
					iLineType = 4;
				}
				//lower lines
				if (i == 2)
				{
					iLineCount = iLineCountLower;
					iLineType = 8;
				}
				// Set values for each of the valid lines
				for (iNdx = 1; iNdx <= iLineCount; iNdx++)
				{
					 //couldn't use 'in' because it somehow confuses java parser
					if (sFieldName == 'ind')
					{
						//if the field exists
						if (eval(sFieldBase + 'in' + iLineType + iNdx))
						{
							// If the field is not hidden
							if (eval(sFieldBase + 'in' + iLineType + iNdx + ".type") != "hidden" )
							{
								sTemp = sFieldBase + 'in' + iLineType + iNdx + ".value=" + sValue;
								eval(sTemp);
							}
						}
					}
					else {
						//if the field exists
						if (eval(sFieldBase + sFieldName + iLineType + iNdx))
						{
							// If the field is not hidden
							if (eval(sFieldBase + sFieldName + iLineType + iNdx + ".type") != "hidden" )
							{
								sTemp = sFieldBase + sFieldName + iLineType + iNdx + ".value=" + sValue;
								eval(sTemp);
							}
						}
					}
				}
			}
		}
		// Clear the "All" value
		sTemp = sFieldBase + sFieldName + ".value=''";
		eval(sTemp);
	}
	
	setDirty();

	return;
}

function popupWindow(sURL)
{
	var sFeatures = 'directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,height=400,width=600';
	var m_wndPopup = window.open(sURL, 'Popup', sFeatures, true)
	m_wndPopup.focus()
}

function onSelectCheck(oSelect)
{
	if (document.frmSave)
	{
		if (oSelect.name == "txtOrganization")
		{
			if (! oSelect.checked)
				for (var i = 0; i < document.frmSave.lstOrganization.length; i++)
					document.frmSave.lstOrganization.options[i].selected = false
			else
				document.frmSave.lstOrganization.options[0].selected = true
		}

		if (oSelect.name == "txtUser" || oSelect.id == "txtUser")
		{
			if (document.frmSave.lstUser.length > 0)
			{
				if (! oSelect.checked)
					for (i = 0; i < document.frmSave.lstUser.length; i++)
							document.frmSave.lstUser.options[i].selected = false
				else
					document.frmSave.lstUser.options[0].selected = true
			}
		}
	}
}

function changeOptions()
{
	if (document.frmCustomWizard.action.indexOf('?') >  0)
		document.frmCustomWizard.action += '&';
	else
		document.frmCustomWizard.action += '?';
		
	document.frmCustomWizard.action += 'doop=1';
	document.frmCustomWizard.form_dirty.value = '1';
	
	document.frmCustomWizard.submit();
}

function changeBorderFitText()
{
	var bBorderFitText;
	var bBorderOn;

	bBorderOn = document.frmCustomWizard.bo.checked;
	if (bBorderOn)
	{
		if (document.frmCustomWizard.bf && typeof(document.frmCustomWizard.bf) == "checkbox")
			bBorderFitText = document.frmCustomWizard.bf.checked;
		else
			bBorderFitText = false;

		document.frmCustomWizard.bd.disabled = bBorderFitText;
		if (bBorderFitText)
			document.frmCustomWizard.bs.options.selectedIndex = 0;

		document.frmCustomWizard.bs.disabled = bBorderFitText;
		
		setDirty();
	} else {
		document.frmCustomWizard.bd.disabled = false;
		document.frmCustomWizard.bs.disabled = false;
	}
}

function resetGraphicPosition(iHorAlign, iVerAlign)
{
	if (document.frmCustomWizard.gt)
		document.frmCustomWizard.gt.value = "";
		
	if (document.frmCustomWizard.gl)
		document.frmCustomWizard.gl.value = "";
	
	setDirty();
		
	setELPDirty(0);

	if (iHorAlign != -1)
	{
		document.frmCustomWizard.gh.selectedIndex = -1;
		for (var i = 0; i < document.frmCustomWizard.gh.length; i++)
		{
			if (document.frmCustomWizard.gh.options[i].value == iHorAlign)
			{
				document.frmCustomWizard.gh.options[i].selected = true;
				break;
			}
		}
	}
	
	if (iVerAlign != -1)
	{
		document.frmCustomWizard.gv.selectedIndex = -1;
		for (var i = 0; i < document.frmCustomWizard.gv.length; i++)
		{
			if (document.frmCustomWizard.gv.options[i].value == iVerAlign)
			{
				document.frmCustomWizard.gv.options[i].selected = true;
				break;
			}
		}
	}
}

function graphicNudge(sDir, iIncrement)
{
	switch (sDir)
	{
		case 'u':
			if (document.frmCustomWizard.gt.value - iIncrement >= 0)
				document.frmCustomWizard.gt.value -= iIncrement;
			else
				document.frmCustomWizard.gt.value = "";
			
			break;
		case 'l':
			if (document.frmCustomWizard.gl.value - iIncrement >= 0)
				document.frmCustomWizard.gl.value -= iIncrement;
			else
				document.frmCustomWizard.gl.value = "";

			break;
		case 'r':
			document.frmCustomWizard.gl.value -= iIncrement;
			break;
		case 'd':
			document.frmCustomWizard.gt.value -= iIncrement;
			break;
	}
	
	setDirty();
	
	if (document.frmCustomWizard.gt.value != "" || document.frmCustomWizard.gl.value != "")
		setELPDirty(1);
	else
		setELPDirty(0);
}

function getObj(id)
{
    if (ie) { return document.all[id]; }
    else {	return document.getElementById(id);	}
}

function setColor(color)
{
	var scolor = '#' + color;
    var link = getObj('pick' + curId);
    var field = getObj('pick' + curId + 'field');
    var picker = getObj('colorpicker');
    
    field.value = color;
    if (color == '')
    {
         link.style.background = nocolor;
         link.style.color = nocolor;
         color = nocolor;
    } else {
         link.style.background = scolor;
         link.style.color = scolor;
    }
    link.parentNode.style.backgroundColor = scolor;
    picker.style.display = 'none';
    //eval(getObj('pick' + curId + 'field').title);

    if(curId != 'border' && curId != 'graphic')
    {
		var iLineType, iNdx;
		var iLineCountUpper = getObj('TRO_CURVED_LINES_UPPER').value;
		var iLineCountFlat = getObj('TRO_HORIZONTAL_TEXT_LINES').value;
		var iLineCountLower = getObj('TRO_CURVED_LINES_LOWER').value;

		var iLineCount = iLineCountUpper + iLineCountFlat + iLineCountLower;
		//get lines of text
		if (iLineCount > 1)
		{
			for (var i = 0; i <= 2; i++)
			{
				//upper curved lines
				if (i == 0)
				{
					iLineCount = iLineCountUpper;
					iLineType = 2;
				}
				//flat lines
				if (i == 1)
				{
					iLineCount = iLineCountFlat;
					iLineType = 4;
				}
				//lower curved lines
				if (i == 2)
				{
					iLineCount = iLineCountLower;
					iLineType = 8;
				}
					
				// Set values for each of the valid lines
				for (iNdx = 1; iNdx <= iLineCount; iNdx++)
				{
					if(curId == 'alllines')
					{
						link = getObj('pick' + iLineType + iNdx);
						field = getObj('pick' + iLineType + iNdx + 'field');
						//if both the fields exist
						if (link && field)
						{
							field.value = color;
							link.style.background = scolor;
							link.style.color = scolor;
							link.parentNode.style.backgroundColor = scolor;
						}
					}
					var ddlCW = getObj('ff' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = 'block'; }
					ddlCW = getObj('fs' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = 'block'; }
					ddlCW = getObj('ha' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = 'block'; }
				}
			}
		}
		var ddlApply = getObj('ff');
		if(ddlApply) { ddlApply.style.display = 'block'; }
		ddlApply = getObj('fs');
		if(ddlApply) { ddlApply.style.display = 'block'; }
		ddlApply = getObj('ha');
		if(ddlApply) { ddlApply.style.display = 'block'; }

		if(curId == 'alllines' || curId == 'inset')
		{
			link = getObj('pickinset');
			field = getObj('pickinsetfield');
			//if both the fields exist
			if (link && field)
			{
				field.value = color;
				link.style.background = scolor;
				link.style.color = scolor;
				link.parentNode.style.backgroundColor = scolor;
			}
		}
		
		setDirty();
    }
}

function setDiv()
{
    if (!document.createElement) { return; }
    var elemDiv = document.createElement('div');
    if (typeof(elemDiv.innerHTML) != 'string') { return; }
    elemDiv.id = 'colorpicker';
    elemDiv.style.position = 'absolute';
    elemDiv.style.display = 'none';
    elemDiv.style.border = '#000000 1px solid';
    elemDiv.style.background = '#FFFFFF';
    elemDiv.innerHTML =  getColorTable();
    document.body.appendChild(elemDiv);
    divSet = true;
}

function pickColor(id)
{
    if (!divSet) { setDiv(); }
    var picker = getObj('colorpicker');
    var ddlDisplayStyle = '';
    var bIsIE6 = false;
	var browser_ver = window.navigator.userAgent;
	var reg = new RegExp('ms.e [5-6]','i');
	
	bIsIE6 = reg.test(browser_ver);
    
    if (id == curId && picker.style.display == 'block')
    {
        picker.style.display = 'none';
        ddlDisplayStyle = 'block';
	} else {
		curId = id;
		var thelink = getObj('pick' + id);
		picker.style.top = getAbsoluteOffsetTop(thelink) + 20;
		picker.style.left = getAbsoluteOffsetLeft(thelink);
		picker.style.display = 'block';
        ddlDisplayStyle = 'none';
	}

	if((!isNaN(id) || id == 'inset' || id == 'alllines') && bIsIE6)
	{
		var iLineType, iNdx;
		var iLineCountUpper = getObj('TRO_CURVED_LINES_UPPER').value;
		var iLineCountFlat = getObj('TRO_HORIZONTAL_TEXT_LINES').value;
		var iLineCountLower = getObj('TRO_CURVED_LINES_LOWER').value;

		var iLineCount = iLineCountUpper + iLineCountFlat + iLineCountLower;

		if (iLineCount > 1)
		{
			for (var i = 0; i <= 2; i++)
			{
				if (i == 0)
				{
					iLineCount = iLineCountUpper;
					iLineType = 2;
				}
				if (i == 1)
				{
					iLineCount = iLineCountFlat;
					iLineType = 4;
				}
				if (i == 2)
				{
					iLineCount = iLineCountLower;
					iLineType = 8;
				}
					
				for (iNdx = 1; iNdx <= iLineCount; iNdx++)
				{
					var ddlCW = getObj('ff' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = ddlDisplayStyle; }
					ddlCW = getObj('fs' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = ddlDisplayStyle; }
					ddlCW = getObj('ha' + iLineType + iNdx);
					if(ddlCW) { ddlCW.style.display = ddlDisplayStyle; }
				}
			}
		}
		var ddlApply = getObj('ff');
		if(ddlApply) { ddlApply.style.display = ddlDisplayStyle; }
		ddlApply = getObj('fs');
		if(ddlApply) { ddlApply.style.display = ddlDisplayStyle; }
		ddlApply = getObj('ha');
		if(ddlApply) { ddlApply.style.display = ddlDisplayStyle; }
	}
}

function relateColor(id, color)
{
    var link = getObj('pick' + id);
    if (color == '')
    {
        link.style.background = nocolor;
        link.style.color = nocolor;
        color = nocolor;
    } else {
        link.style.background = '#' + color;
        link.style.color = '#' + color;
    }
    //eval(getObj('pick' + id + 'field').title);
}

function getAbsoluteOffsetTop(obj)
{
    var top = obj.offsetTop;
    var parent = obj.offsetParent;
    while (parent != document.body)
    {
        top += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return top;
}

function getAbsoluteOffsetLeft(obj)
{
    var left = obj.offsetLeft;
    var parent = obj.offsetParent;
    while (parent != document.body)
    {
        left += parent.offsetLeft;
        parent = parent.offsetParent;
    }
    return left;
}

