
var g_selectionDef = new Array()
var selectedRowID = null
var m_selectedColor = null
var m_selectedColorChannelName = null

function changeImageTitle(obj)
{
	if (typeof(obj)=="undefined" || obj==null) return
	var name = obj.name.substring(3)
	var imgObj = document.getElementById("image_"+name)
	if (imgObj) {
		imgObj.alt = clientGetFileName(obj.options[obj.selectedIndex].value,true)
	}
}
function selectboxchange(obj)
{
	var name = obj.name.substring(3)
	if( obj.getAttribute("fieldType") == "image" )
	{
		var imgObj = document.getElementById("image_"+name)
		if (imgObj) 
		{
			imgObj.src = "/newsway/versions/250/site/iway/general/lr.asp?lowresmode=thumb&file="+obj.options[obj.selectedIndex].value
			imgObj.alt = clientGetFileName(obj.options[obj.selectedIndex].value,true)
		}	
	}
	if( typeof(g_selectionDef[name]) != "object" )
	{
		return;
	}	
	var recordID = obj.options[obj.selectedIndex].getAttribute("recordID")
	var id, actions = g_selectionDef[name][recordID]
	var foundFlag = false
	for( id in actions )
	{
		if( id != "" )
		{
			var uobj = document.getElementById("id_"+id)
			if (uobj!=null) 
			{
				var objValue = actions[id]
				if (uobj.getAttribute("fieldType") == "image")
				{
					 if (uobj.tagName.toLowerCase() == "select") 
					 {
							for (i = 0; i < uobj.options.length; i++) 
							{
								var opt = uobj.options[i]
								var imageName = clientGetFileName(opt.value)
								if (imageName.toLowerCase() == objValue.toLowerCase()) 
								{
									opt.selected = true
									imageSelectModified(id)
									foundFlag = true
									break
								}
							}
							if (!foundFlag) 
							{
								// Failed to find image,adding to select box
								var addOption = document.createElement("OPTION")
								addOption.text = objValue
								addOption.value = clientGetFilePath(opt.value) + "/" + objValue
								addOption.selected = true
								selectAddLastOption(uobj, addOption)
								imageSelectModified(id)
							}
					 }
					 else if (uobj.tagName.toLowerCase() == "input")
					 {
						 uobj.value = uobj.getAttribute("imageFromFolder") + "/" + objValue 
					 }	
					 else 
					 {
						uobj.value = objValue
					 }
				 } 
				 else 
				 {
					if (uobj != obj)
					{	
						uobj.value = objValue

					}
				 }
				if( uobj.type == "hidden" )
				{
					var iobj = document.getElementById("image_"+id)
					if (iobj)
					{
						iobj.src = "/newsway/versions/250/site/iway/general/lr.asp?lowresmode=thumb&file="+actions[id];
					}	
				}
				
				if (typeof(uobj.onblur)=="function")
				{
					uobj.onblur()
				} 
			}	
		}
	}
}

function selectRow(rowObj)
{
	saveFieldGroups()
	selectedRowID = makeTableRowSelected(rowObj.id, selectedRowID )
	hideAllGroupsToolBars()
	hideAllGroupsIcons()
	showDummyGroupsSpans()
	groupsArr = rowObj.getAttribute("groupsIds").split(";")
	for (var i=0; i < groupsArr.length; i++)
	{
		showGroup(groupsArr[i])
		setMSelectedColor(groupsArr[i])
		saveGroupIfHasWrongDefaults(groupsArr[i])
	}
}

// This function save the group detail (set to default) in case the original default value is not in the group constrains 
function saveGroupIfHasWrongDefaults(groupId)
{
	var groupObj = document.getElementById(groupId)
	if (groupObj != null)
	{
		var fontSlctObj = findChildElementById(groupObj,"fontSlct")
		if (fontSlctObj != null && fontSlctObj.selectedIndex == -1)
		{
			fontSlctObj.selectedIndex = 0
		}
		var fontSizesObj = findChildElementById(groupObj,"fontSizesObj")	
		if (fontSizesObj != null && fontSizesObj.selectedIndex == -1)
		{
			fontSizesObj.selectedIndex = 0
		}	
		var fontModeObj = findChildElementById(groupObj,"fontMode")	
		if (fontModeObj != null && fontModeObj.selectedIndex == -1)
		{
			fontModeObj.selectedIndex = 0
		}	
		saveGroup(groupObj)
	}
}

function saveAllGroupsIfHasWrongDefaults()
{
	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		var groupSpanObj = spansCollection[i]
		if (groupSpanObj.tagName != null && 
			groupSpanObj.tagName.toLowerCase() == "span" && 
			groupSpanObj.getAttribute("fieldsStr") != null)
		{
			setMSelectedColor(groupSpanObj.id)	
			saveGroupIfHasWrongDefaults(groupSpanObj.id)
		}
	}
}
function setMSelectedColor(groupId) // If it's font color group, then set the m_selectColor variable
{
	groupSpanObj = document.getElementById(groupId)
	if (groupSpanObj != null)
	{
		if (groupSpanObj.getAttribute("groupType").toLowerCase() == "fontcolor")
		{
			fontColor = groupSpanObj.getAttribute("fontColor")
			if (isRgbStr(fontColor))
			{
				m_selectedColor = fontColor
			}	
			else
			{
				if (isChannelStr(fontColor))
				{
					m_selectedColor = fontColor
				}	
				else 
				{
					m_selectedColor = chanCMYKStrToChannelStr(fontColor)
				}	
			}
			m_selectedColorChannelName = groupSpanObj.getAttribute("colorChannelName")
		}
	}
}

function initStyleGroups()
{
	var attrList = "id;font;fontSize;fontColor;fontMode;underline;fieldsStr;groupType;colorChannelName;colorType"
	var attrArr = attrList.split(";")
	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		var groupSpanObj = spansCollection[i]
		if (groupSpanObj.tagName != null && 
			groupSpanObj.tagName.toLowerCase() == "span" && 
			groupSpanObj.getAttribute("fieldsStr") != null)
		{
			fieldsArr = groupSpanObj.getAttribute("fieldsStr").split(";")
			for (var j=0; j < fieldsArr.length; j++)
			{
				fieldObj = document.getElementById(fieldsArr[j])
				if (fieldObj != null)
				{
					for (var k=0; k<attrArr.length; k++)
					{
						var attrName = attrArr[k]
						if (fieldObj.getAttribute(attrName) != null && 
							fieldObj.getAttribute(attrName) != "" &&
							groupSpanObj.getAttribute(attrName) == null
							)
							{
								groupSpanObj.setAttribute(attrName, fieldObj.getAttribute(attrName))
							}	
					}		
				}	
			}
			initGroup(groupSpanObj.id)
		}
	}
}

function initGroup(groupId)
{
	groupSpanObj = document.getElementById(groupId)
	if (groupSpanObj != null)
	{
		groupType = groupSpanObj.getAttribute("groupType")
		switch (groupType.toLowerCase())
		{
			case "font":
				groupFont = groupSpanObj.getAttribute("font")
				var fontSlctObj = findChildElementById(groupSpanObj,"fontSlct")
				if (groupFont != "" && groupFont != null)
				{
					fontSlctObj.value = groupFont
				}	
				if (fontSlctObj.selectedIndex == "-1")
				{
					for (var i=0;i < fontSlctObj.options.length; i++)
					{
						if (fontSlctObj.options[i].value.toLowerCase() == groupFont.toLowerCase())
						{
							fontSlctObj.options[i].selected = true	
						}	
					}		
				}					
				set_fonCapabilities(groupSpanObj)
				fieldFontMode = groupSpanObj.getAttribute("fontMode")
				var fontModeObj = findChildElementById(groupSpanObj,"fontMode")	
				if (fieldFontMode != "" && fieldFontMode != null)
				{
					fontModeObj.value = fieldFontMode
				}
					
				fieldUnderline = groupSpanObj.getAttribute("underline")
				var spanForUnderlineObj = findChildElementById(groupSpanObj,"spanForUnderline")	
				if (fieldUnderline == "1")
				{
					highlightButton(spanForUnderlineObj,"on")
				}	
				else
				{
					highlightButton(spanForUnderlineObj,"off")
				}	
				break
			case "fontsize":
				fieldFontSize = groupSpanObj.getAttribute("fontSize")
				var fontSizesObj = findChildElementById(groupSpanObj,"fontSizesObj")	
				if (fieldFontSize != "" && fieldFontSize != null)
				{
					fontSizesObj.value = fieldFontSize
				}	
				break
				
			case "fontcolor":
				fontColor = groupSpanObj.getAttribute("fontColor")
				if (isRgbStr(fontColor))
				{
					rgbColor = fontColor
				}	
				else
				{
					if (isChannelStr(fontColor))
					{
						rgbColor = chanStrToRGBstr(fontColor)
					}	
					else 
					{
						rgbColor = colCMYK2RGBStr(fontColor)
					}	
				}
				var colorRectangleObj = findChildElementById(groupSpanObj,"colorRectangle")
				colorRectangleObj.style.backgroundColor = rgbColor
				break
		}
	}
}

function saveFieldGroups()
{
	if (selectedRowID != null)
	{
		rowObj = document.getElementById(selectedRowID)
		if (rowObj != null)
		{
			groupsArr = rowObj.getAttribute("groupsIds").split(";")
			for (var i=0; i < groupsArr.length; i++)
			{
				groupObj = document.getElementById(groupsArr[i])
				if (groupObj != null)
				{
					saveGroup(groupObj)
				}	
			}
		}
	}
}

function saveGroup(groupObj)
{
	switch (groupObj.getAttribute("groupType").toLowerCase())
	{
		case "font":
			var fontSlctObj = findChildElementById(groupObj,"fontSlct")
			groupObj.setAttribute("font", fontSlctObj.value)
			var fontModeObj = findChildElementById(groupObj,"fontMode")
			groupObj.setAttribute("fontMode",  fontModeObj.value)
			break;
		case "fontsize":
			var fontSizesObj = findChildElementById(groupObj,"fontSizesObj")
			groupObj.setAttribute("fontSize", fontSizesObj.value)
			break;
		case "fontcolor":
			groupObj.setAttribute("fontColor", m_selectedColor)
			groupObj.setAttribute("colorChannelName", m_selectedColorChannelName)
			break;
	}
}

function showGroup(groupId)
{
	if (groupId != "" && groupId != null)
	{
		var groupSpanObj = document.getElementById(groupId)
		groupSpanObj.style.display = ''
		var groupType = groupSpanObj.getAttribute("groupType")
		document.getElementById('dummy' + groupType).style.display = 'none'
		var groupFieldsStr = groupSpanObj.getAttribute("fieldsStr")
		var groupFieldsArr = groupFieldsStr.split(";")
		showGroupIcon(groupType, groupFieldsArr)
	}
}

function hideAllGroupsToolBars()
{
	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		var elTagName = spansCollection[i].tagName
		if ((elTagName != null) && 
			((elTagName.toLowerCase() == "span")||(elTagName.toLowerCase() == "label")))
		{	
			spansCollection[i].style.display = 'none';
		}	
	}		
}

function hideAllGroupsIcons()
{
	var typesNames = new Array("FontIconTd", "FontSizeIconTd", "FontColorIconTd")
	for (var typeName in typesNames)
	{
		iconTdArr = document.getElementById(typesNames[typeName])
		for (var i=0; i < iconTdArr.length ; i++)
		{
			iconTdArr[i].innerHTML = ""
		}	
	}
}

function showDummyGroupsSpans()
{
	document.getElementById('dummyFont').style.display = ''
	document.getElementById('dummyFontSize').style.display = ''
	document.getElementById('dummyFontColor').style.display = ''
}

function makeTableRowSelected( rowID, selectedRowID )
{
	var selectedRowObj = document.getElementById( String(selectedRowID) )
	if ( selectedRowID != null )
	{
		if (selectedRowObj != null)
		{
			selectedRowObj.className = "tableRow"
		}	
	}		
	
	var rowIDObj = document.getElementById( String(rowID) )
	if ( rowID != null && rowIDObj != null)
	{
		rowIDObj.className = "tableSelectedRow"
		return rowID
	}
	return null
}

function alertNonEditable()
{
	iwayMessageBox("Field", "This is non-editable field.")
}

////////////////////////////////
/* Style Selection Functions */
function checkButtonPress(spanObj, name)
{
	var parentSpan = spanObj.parentNode
	var currentUnderline = parentSpan.getAttribute("underline")
	parentSpan.setAttribute("underline", (currentUnderline=="1") ? "0" : "1")
	if (currentUnderline == '1')
	{
		highlightButton(spanObj, "off")
	}	
	else
	{
		highlightButton(spanObj, "on")
	}	
}

function highlightButton(spanObj, state)
{
	if (spanObj != null)
	{
		if (state == "on")
		{
			spanObj.style.borderStyle='inset'
			spanObj.style.backgroundColor = '#bebebe'
		}
		else
		{
			spanObj.style.borderStyle=''
			spanObj.style.backgroundColor = 'transparent'
		}
	}
}

var m_spanObj = null;
function openColorSelection(spanObj)
{
	m_spanObj = spanObj;
	var arr	= new Array();
	arr ["selectedColor"] = m_selectedColor;
	arr ["colorChannelName"] = m_selectedColorChannelName
	if ((arr ["colorChannelName"] == null) || (arr ["colorChannelName"] == 'null'))
		arr ["colorChannelName"] = ""
	arr ["colorType"] = m_spanObj.getAttribute("colorType");
	arr ["otherColorChannelNamesStr"] = concatOtherChannelNamesStr()
	if (typeof(channelsArray) != "undefined" && channelsArray != null) {
		arr ["channelsArray"] = channelsArray
	}
	var colorModal	= clientOpenDialog("/newsway/Versions/250/site/iway/app/iFormGuiless/colorSelection.asp?" +
		"selectedColor="+ arr["selectedColor"] + "&colorChannelName=" + arr["colorChannelName"] +
		"&colorType=" + arr["colorType"] + "&otherColorChannelNamesStr=" + arr["otherColorChannelNamesStr"] +
		"&channelsArray=" + arr["channelsArray"],
		"", 380, 370, "colorSelectionCallback")
	if (colorModal)		
	{
		colorSelectionCallback(colorModal)
	}
}

function colorSelectionCallback(colorModal)
{
	channelStr = chanCMYKStrToChannelStr(colorModal[0])
	rgbStr = colCMYK2RGBStr(colorModal[0])
	var colorRectangleObj = findChildElementById(m_spanObj,"colorRectangle")
	colorRectangleObj.style.backgroundColor = rgbStr

	m_selectedColor = channelStr
	if (m_spanObj.getAttribute("colorType") == "spot")
	{
			m_selectedColorChannelName = colorModal[1]
	}		
	saveFieldGroups()
}

function concatOtherChannelNamesStr()
{
	str =""
	if (typeof(channelsArray) != "undefined" && channelsArray != null)
		for (var i=0; i < channelsArray.length; i++)
		{
			var channel = channelsArray[i]
			if (channel != null)
			{
				channelName = channel[2]
				if (channelName != "" & channelName != null && channelName != m_selectedColorChannelName)
				{
						str = str + channelName + ";"
				}		
			}
		}

	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		var spanObj = spansCollection[i]
		if (spanObj.tagName != null && spanObj.tagName.toLowerCase() == "span")
		{
			if (spanObj.getAttribute("groupType") == "FontColor" && spanObj.getAttribute("colorType") == "spot")
			{
				if (spanObj.colorChannelName != "" & spanObj.colorChannelName != null &&
					spanObj.colorChannelName  != m_selectedColorChannelName)
					{
						str = str + spanObj.getAttribute("colorChannelName") + ";"
					}	
			}
		}
	}
	return str
}

function sendStyleParams()
{
	var styleStr = "<styles>"
	var attrList = "id;font;fontSize;fontColor;fontMode;underline;fieldsStr;groupType;colorChannelName;colorType"
	var attrArr = attrList.split(";")
	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		if (spansCollection[i].tagName != null && spansCollection[i].tagName.toLowerCase() == "span")
		{
			styleStr += "<styleGroup "
			for (var j=0; j<attrArr.length; j++)
			{
				var attrName = attrArr[j]
				var attrVal = spansCollection[i].getAttribute(attrName)
				if (attrVal)
				{
					styleStr += attrName + "='" + clientXMLEncode(attrVal) + "' "
				}	
			}		
			styleStr += "/>"

		}
	}	
	styleStr += "</styles>"
	document.getElementById('styleSelectionInfo').value = styleStr
}

function getNodeAttributesStr(attrList)
{
	
}

function checkAllStyleParamsExists()
{
	var retVal = true
	var attrToCheck = new Array()
	stylesToolBar = getStylesToolBar()
	spansCollection = stylesToolBar.childNodes
	for (var i=0; i < spansCollection.length; i++)
	{
		if (spansCollection[i].tagName != null && spansCollection[i].tagName.toLowerCase() == "span")
		{
			switch (spansCollection[i].getAttribute("groupType"))
			{
				case "Font":
					attrToCheck = new Array ("font", "underline", "fontMode")
				break
				case "FontSize":
					attrToCheck = new Array ("fontSize")
				break
				case "FontColor":
					attrToCheck = new Array ("fontColor")
				break
				case "null":
				case null:
					attrToCheck = new Array ()
				break
			}
			for (var j=0; j < attrToCheck.length; j++)
			{
				attrValue = spansCollection[i].getAttribute(attrToCheck[j])
				groupName = spansCollection[i].getAttribute("name")
				if (attrValue == null || attrValue == "null" || attrValue == "")
				{
					iwayMessageBoxWithParams("Parameters Missing","The <span class=bigFont>'_nw_value:attrValue_'</span> attribute of the group <span class=bigFont>'_nw_value:theGroupName_'</span> must contain a value._nw_nl_Assign a value.",new Array(attrToCheck[j], groupName ))
					return false
				}
			}
		}
	}
	return true
}

function getCapabilities(fontName, groupObj)
{
	var TFS = findChildElementById(groupObj, 'fontSlct')
	var oldIndex=TFS.selectedIndex
	if (fontName!=-1)
	{
		oldIndex=TFS.selectedIndex
		TFS.value=fontName
	}
	var selIndex=TFS.selectedIndex
	if (selIndex==-1)
	{
		return null
	}	
	var Capabilities=TFS.options[selIndex].getAttribute("capability")
	TFS.selectedIndex=oldIndex
	if (Capabilities)
	{
		Capabilities=Capabilities.replace(/Oblique/g,"Italic").replace(/Italic Bold/g,"Bold Italic")
	}	
	return Capabilities
}

function set_fonCapabilities(groupObj)
{
	var Capabilities=getCapabilities(-1, groupObj)
	var fontMode = findChildElementById(groupObj, 'fontMode')
	var oldMode = fontMode.value 
	while (fontMode.length)
	{
		fontMode.remove(0)
	}	
	if(Capabilities==null) 
	{
		return
	}	
	Capabilities=Capabilities.split(";")
	for (var cntr in Capabilities)
	{
		var val=Capabilities[cntr]
		var text=-1
		for (var i=0;i<4;i++)
			if (fontModes[i]==val)
			{
				text=i;
				break;
			}
		if (text!=-1)
		{
			selectAddLastOption(fontMode, new Option(fontModeNames[text],val))
		}

	}
	if ((selectedRowID!=null) && (typeof(oldMode) == "undefined" || oldMode == "" ))
	{
		fontMode.value = document.getElementById(selectedRowID).getAttribute("stepFontMode")
	}	
	else
	{
		fontMode.value=oldMode
	}	
	/////////////////////	if no text mode and only one object is selected !!!!!!!!!!
	if ((fontMode.selectedIndex==-1))
	{
		fontMode.selectedIndex = 0
	}	
}

function showGroupIcon(groupType, fieldsArr)
{
	for (var i=0 ; i < fieldsArr.length; i++)
	{
		fieldId = fieldsArr[i]
		fieldRow = document.getElementById(fieldId)
		if (fieldRow != null)
		{
			findChildElementById(fieldRow,groupType + "IconTd").innerHTML = "<IMG title='" + showLocalizeNameClient(groupType.toLowerCase()) + "' src='/newsway/versions/250/site/iway/Resources/icons/" + groupType + ".gif'>"
		}	
	}
}

function doCheckTextFieldsFormatting(elementType)
{
	var iformElements = document.getElementsByTagName(elementType)
		
	for (var i=0;i < iformElements.length;i++)
	{
		if ((iformElements[i]!=null) && 
			(iformElements[i].getAttribute("formatted") == "true") && 
			(typeof(iformElements[i].onblur) != "undefined"))
		{
			try
			{
				iformElements[i].focus();
				iformElements[i].onblur();
			}
			catch (ex)
			{}
		}
	}
}
function checkTextFieldsFormatting()
{
	doCheckTextFieldsFormatting("input")
	doCheckTextFieldsFormatting("textarea")
}

function isMismatchFound()
{
	if (typeof(g_MismatchArray)!="undefined")
	{
		for (var fieldID in g_MismatchArray)
		{
			if (g_MismatchArray[fieldID]==true) 
			{
				return true;
			}	
		}
	}	
	return false;
}

function openFormatRulesDialog()
{
	iwayMessageBox("Text Should Fit Format Rules","Some of the Field Values do not fit the Format Rules.<BR>These fields have been marked with a warning icon.<BR><BR>In order to proceed with the Job, correct the values according to the format rules that appear near each field.")
}

function disableImageFields( bDisable)
{
	var docCombos = document.getElementsByTagName("SELECT")
	for (var i=0;i<docCombos.length;i++)
	{
		var el = docCombos[i]
		if (el.getAttribute("fieldType")=="image" && el.getAttribute("fieldState")=="disabled")
		{
			el.disabled = bDisable
		}
	}
}

function findChildElementById(parentEl, id)
{
	if (clientBrowserType()=="ie")
	{
		return parentEl.all(id)
	}
	else
	{
		var children = parentEl.childNodes
		for (var i=0; i<children.length; i++)
		{
			if (typeof(children[i].id)!="undefined")
			{
				if (children[i].id.toLowerCase() == id.toLowerCase())
				{
					return children[i];
				}
			}	
		}
		return null;
	}	
}

function selectAddLastOption(selObj, opt)
{
	 try {   
			selObj.add(opt);
	  }
	  catch (ex) {
			selObj.add(opt,null);
	  }
}

function getStylesToolBar()
{
	return document.getElementById("stylesToolbar")
}
