//----------------------------------------------------------------
// Copyright (C) 2002-2003 iManage Inc.
// All rights reserved.
//
//									Brian Xian  bxian@imanage.com
//----------------------------------------------------------------

window.loc_delimitor = "|"	
window.loc_englishAbreviation = "en"
window.loc_encodeOpen = "{"	
window.loc_encodeClose = "}"		
	
function node (lid, type, text) {
	//type: 2 = "message"; 3 = "str"
	this.lid = lid
	this.type = type
	this.text = text
}
	
function getLanguageBase() {
	var result = ""
	var langAbv = lllist.abbreviation
	if (langAbv) {
		var pos = langAbv.indexOf("-")
		if (pos >-1) {
			result = langAbv.substring(0,pos)
		}else {
			result = langAbv
		}
	}
	return result
}

function getMessage(lid, arg1, arg2, arg3) {
	var result = ""
	var lidInfo = new getStringInfo(lid)                        
	if (lidInfo.lid) {
		result = getLocalizedMessage(lidInfo.lid,arg1,arg2,arg3)
	}else if (lidInfo.value) {
		result = replaceMsgParams(lidInfo.value,arg1,arg2,arg3)
	}
	return result
}

function writeMessage(lid, arg1, arg2, arg3) {
	document.write(getMessage(lid, arg1, arg2, arg3))
}

function getLocalizedMessage(lid,arg1, arg2, arg3) {
	//currently, we only support three parameters at most
	var msgNode = lllist.elements[lid]
	if (!msgNode) {
		return ""
	}
	var text = replaceMsgParams(msgNode.text,arg1,arg2,arg3)
	return text
}

function replaceMsgParams(str,arg1,arg2,arg3) {
	if (!str) return
	var text = str
	text = text.replace(/%%/gi,"%")			//replace %% with %
	if (arg1) {
		text = text.replace(/%1/gi,arg1)	//replace argument 1
	}
	if (arg2) {
		text = text.replace(/%2/gi,arg2)  //replace argument 2
	}
	
	if (arg3) {
		text = text.replace(/%3/gi,arg3) //replace argument 3
	}
	
	return text
}

function getString(lid, extraInfo, defaultValue, labelID){
	/*	5/23/02 (ns):  labelID has been added as an optional parameter
		for Section 508 purposes.  For label-control association, 
		we must wrap the text in <label>.  The screen readers
		will not properly process our client-side localization
		scripting if the <label> tags are outside of <script> tags.
		Therefore, we must have writeString output the <label> tags
		as well.  labelID is required as the "for" attribute of <label>.
		
		Example output of writeString with this modification:
			<label for="description">Description</label>
	*/
	
	var html= ""
	if (!defaultValue) defaultValue = ""
		
	//English and has default value, use default value
	if (lllist.familyName == loc_englishAbreviation && defaultValue ) {	
		html = defaultValue
	}else {		//Non English or no default value, do look up
		if (lid) {
			html = getLocStrByLID(lid,defaultValue) + parseAndLocalizeStr(extraInfo)
		} else if (!extraInfo) {
			html = defaultValue 
		}else {
			html = defaultValue + parseAndLocalizeStr(extraInfo)
		}
	}
	
	if (labelID) {
		html = "<label for=\"" + labelID + "\">" + html + "</label>"
	}
	return html
}

function writeString(lid, extraInfo, defaultValue, labelID){
	document.write(getString(lid, extraInfo, defaultValue, labelID))
}
				
function getLocalizedString(str) {
	var result = ""
	//get the information about delimitor, lid and original value
	var strInfo = new getStringInfo(str)
		
	//English and has default value, use default value
	if (lllist.familyName == loc_englishAbreviation && strInfo.value ) {
		result = strInfo.value
	}else {															//Non English or no default value, do look up
		if (strInfo.lid) {								//Get localized string based on the lid
			result = getLocStrByLID(strInfo.lid,strInfo.value)
		}else if (strInfo.value) {				
			result = strInfo.value
		}
	}
	//If no localized string found, return the original value with lid info and delimitor.
	if (!result) {					
		result = (strInfo.value ? strInfo.value : str)
	}
	return result;
}

function getStringInfo(str) {
	str = String(str)
	var tempAry = str.split(loc_delimitor)
	var valueFirst = isNaN(Number(tempAry[0]))	
	this.hasDelimitor = (tempAry.length > 1)
	if (valueFirst) {	
		this.value = tempAry[0]
		this.lid = (this.hasDelimitor ? tempAry[1] : "")
	}else{
		this.value = (this.hasDelimitor ? tempAry[1] : "")
		this.lid = tempAry[0]
	}
}

function getLocStrByLID(id, defaultValue) {
	var result = ""
	if (!defaultValue) defaultValue = ""
	if (!id) {
		result = defaultValue
	} else if (lllist.familyName == loc_englishAbreviation && defaultValue ) {
		result = defaultValue
	} else {		//Non English or no default value, do look up
		//find the node in the resouce file
		var targetNode = lllist.elements[id]
		if (targetNode) {
			result =targetNode.text		//get the localized text.
		}else {
			result = defaultValue
		}
	}
	return result
}

function parseAndLocalizeStr(str) {
	var result =""
	var startPos = str.indexOf (loc_encodeOpen)
	if (startPos < 0 || (!str)) {				//No encoded segment found
		result = str
	}else {										//Found an encoded part
		var tempStr = str.substr(startPos+1)	//get the string after the encode charactor
			
		//Append the string ahead of the encode charactor to the result
		result += str.substring(0,startPos)			
		var nextPos = tempStr.indexOf (loc_encodeClose)	//Find the next encode charactor
			
		//If no closing encode charactor, append the rest string to the result.
		if (nextPos < 0 ) {								
			result += loc_encodeOpen + tempStr
		} else {											//Found the closing encode charactor
			var paramStr = tempStr.substring(0,nextPos)		//get the parameter string need to be localized
			var locedStr = ""
			var paramInfo = new getStringInfo(paramStr)
				
			//English and has default value, use default value
			if (lllist.familyName == loc_englishAbreviation && paramInfo.value) {	
				locedStr = paramInfo.value
			}else {
				if (paramInfo.lid) {
					locedStr = getLocStrByLID(paramInfo.lid)
				}else if ( paramInfo.value) {
					locedStr = paramInfo.value
				}
			}
			
			if (!locedStr) {
				locedStr = (paramInfo.value ? paramInfo.value : paramStr)
			}
			result += locedStr											//Append the localized string to the result
			tempStr = tempStr.substr(nextPos + 1)		//get the string after the closing encode charactor
			result += parseAndLocalizeStr(tempStr)	//Keep finding the rest parts
		}	
	}
	return result;	
}

function locAlert(str) {
	self.alert(str)
}

function writeLocMetaTag() {
	var html = "<META HTTP-EQUIV='Content-Type' content='text/html; charset=" + lllist.charset + "' />"
	document.writeln(html)
}

function writeHtmlTitle(str) {
	document.writeln("<title>" + parseAndLocalizeStr(str) + "</title>")
}

function buildMenuCaption(str) {
	var result = parseAndLocalizeStr(str)
	var strInfo = new getStringInfo(result)
	if (strInfo.hasDelimitor) {
		if (loc_englishAbreviation == getLanguageBase() && strInfo.value ) {
			result = strInfo.value
		} else if (strInfo.lid) {
			result = getLocStrByLID(strInfo.lid)
		}
	}
	return result
}

// Function to write the anchor tag in the XSL.

function getAnchorStr(lid,str,href,target,image,bold,style,cls,click,msgLID,title1,title2,title3){		
	/*
		lid = LID of string for the anchor string and also the image ALT.		
		msgLID = LID of the message string that will come up on the anchor title.
		title1 = First parameter that replaces %1 in the message string.
		title2 = Second parameter that replaces %2 in the message string.
		title3 = Third parameter that replaces %3 in the message string.
		
		If we have a title parameter passed then we write the title attribute for the anchor tag 
		and the alt for the image is not written,otherwise we write the alt on the image
		and there is no title attribute written on the anchor tag.
	*/	
	
	var text = htmlString(lid ? getLocStrByLID(lid, str) : parseAndLocalizeStr(str))
	var html
	html = '<a  familyName="lookupAnchor" class="' + (cls ? cls : 'item-body') + '" href=\'' + (href ? (href) : '#') + '\' ' +
			(click ? 'onClick=\'' + htmlString(click) + '\' ' : '') +
			(target ? 'target="' + target + '" ' : '') +
			( !g_browser.isNS4 && style ? 'style="' + style + '" ' : '')

	var title = ''
	if ( (title1 || title2 || title3) && msgLID != '' ){
		title1 = title1 ? parseAndLocalizeStr(title1) : ''
		title2 = title2 ? parseAndLocalizeStr(title2) : ''
		title3 = title3 ? parseAndLocalizeStr(title3) : ''
		title = getLocalizedMessage(msgLID,title1,title2,title3)
	} else if (title1){			
		title = parseAndLocalizeStr(title1)
	}
	if ( title ) {
		html += 'title="' + htmlString(title) + '"'
	}
	html += '>'	+
			(image ? '<img src="' + image + '" border="0" align="middle" alt="' + title + '"/>' : '') +
			(bold ? text.bold() : text) +
			'</a>'
	return html
}

function writeAnchorStr(lid,str,href,target,image,bold,style,cls,click,msgLID,title1,title2,title3) {		
	document.write(getAnchorStr(lid,str,href,target,image,bold,style,cls,click,msgLID,title1,title2,title3))
}
