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(htmlString(getMessage(lid, arg1, arg2, arg3)))
}

function getLocalizedMessage(lid, arg1, arg2, arg3) {
	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, "%")			
	if (arg1) {
		text = text.replace(/%1/gi, arg1)	
	}
	if (arg2) {
		text = text.replace(/%2/gi, arg2)  
	}
	
	if (arg3) {
		text = text.replace(/%3/gi, arg3)
	}
	
	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 = ""
		

	if (lllist.familyName == loc_englishAbreviation && defaultValue ) {	
		html = defaultValue
	}else {		
		if (lid) {
			html = getLocStrByLID(lid,defaultValue) + parseAndLocalizeStr(extraInfo)
		} else if (!extraInfo) {
			html = defaultValue 
		}else {
			html = defaultValue + parseAndLocalizeStr(extraInfo)
		}
	}
	
	if (labelID) {
		html = "<label for=\"" + htmlString(labelID) + "\">" + htmlString(html) + "</label>"
	}
	else
	{
		html = htmlString(html)
	}
	return html
}

function writeString(lid, extraInfo, defaultValue, labelID){
	document.write(getString(lid, extraInfo, defaultValue, labelID))
}
				
function getLocalizedString(str) {
	var result = ""

	var strInfo = new getStringInfo(str)
		
	if (lllist.familyName == loc_englishAbreviation && strInfo.value ) {
		result = strInfo.value
	}else {														
		if (strInfo.lid) {								
			result = getLocStrByLID(strInfo.lid,strInfo.value)
		}else if (strInfo.value) {				
			result = strInfo.value
		}
	}

	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 {		
		var targetNode = lllist.elements[id]
		if (targetNode) {
			result =targetNode.text		
		}else {
			result = defaultValue
		}
	}
	return result
}

function parseAndLocalizeStr(str) {
	if ( ! str )
	{
		return "";
	}
	
	if (str.charAt(0) != loc_encodeOpen) 
	{				
		return str
	}
	
	var startPos = str.indexOf (loc_encodeOpen)	
	var result = ""
	var tempStr = str.substr(startPos+1)	
	result += str.substring(0,startPos)			
	var nextPos = tempStr.indexOf (loc_encodeClose)	
		
	if (nextPos < 0 ) {								
		result += loc_encodeOpen + tempStr
	} else {											
		var paramStr = tempStr.substring(0,nextPos)		
		var locedStr = ""
		var paramInfo = new getStringInfo(paramStr)
			
		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										
		tempStr = tempStr.substr(nextPos + 1)	
		result += parseAndLocalizeStr(tempStr)
	}	
	return result;	
}

function locAlert(str, wnd) 
{
	if ( wnd )
	{
		wnd.alert(str)
	}
	else
	{
		alert(str)
	}
}

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 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 = lid ? getLocStrByLID(lid, str) : new getStringInfo(str).hasDelimitor ? parseAndLocalizeStr(str): str;
	var html
	html = '<a class="' + htmlString(cls ? cls : 'worksite-item-body') + '" href=\'' + htmlString(href ? href : '#') + '\' ' +
			(click ? 'onClick=\'' + htmlString(click) + '\' ' : '') +
			(target ? 'target="' + htmlString(target) + '" ' : '') +
			( !g_browser.isNS4 && style ? 'style="' + htmlString(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="' + htmlString(image) + '" border="0" align="middle" alt="' + title + '"/>' : '') +
			(bold ? '<b>' + htmlString(text) + '</b>' : htmlString(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))
}

window.lllist = 
{
	lcid : '1033', 
	alertMode : '0', 
	abbreviation : 'en-us', 
	name : 'English',
	charset : 'iso-8859-1',
	familyName : 'en',
	htmlEncode : false,
	localizedName : 'English (USA)',
	elements : []
};

