var W_DBG = false;

// Get value of string variable even though it may not exist
function wValue(sVar)
{
	return eval("(typeof(" + sVar + ") == \"string\") ? " + sVar + " : \"\"");
}

function wDebug(strFile)
{
	if (typeof(strFile) == "string" && strFile.length > 0)
	{
		// need to adjust IE security for this to work
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var f = fso.CreateTextFile(strFile, true);
		f.Close();
		W_DBG = strFile;
	}
	else if (W_DBG)
	{
		W_DBG = false; 
	}
}

wDebug(wValue("W_DBGFILE"));  // automatic debug

function wWrite(strVal)
{
	if (typeof(document) == "object") document.write(strVal);
	if (W_DBG)
	{
		// need to adjust IE security for this to work
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var f = fso.OpenTextFile(W_DBG,8);
		f.WriteLine(strVal);
		f.Close();
	}
}


// HTML encoding for JavaScript text value
function wEncode(strVal,bNoBreak,bNoLines)
{
	var result = "";
	var sVal = (typeof(strVal) != "undefined") ? strVal.toString() : "";
	var sLen = sVal.length;
	for (var i=0; i < sLen; i++)
	{
		var chCode = sVal.charCodeAt(i);
		if (chCode == 160 || (chCode == 32 && bNoBreak))
		{
			result += "&nbsp;";
		}
		else if (chCode == 34)
		{
			result += "&quot;";
		}
		else if (chCode == 38)
		{
			result += "&amp;";
		}
		else if (chCode == 60)
		{
			result += "&lt;";
		}
		else if (chCode == 62)
		{
			result += "&gt;";
		}
		else if (chCode == 0x2028)
		{  // Line Separator
			result += (bNoLines) ? " " : "<BR>";
		}
		else if (chCode >= 32 && chCode < 127)
		{
			result += String.fromCharCode(chCode); 
		}
		else
		{
//			result += "&#x" + chCode.toString(16) + ";";
			result += "&#" + chCode + ";";
		}
	}
	return result;
}

function wLeftPad(strVal,strPad,nLength)
{
	var sVal = (typeof(strVal) != "undefined") ? strVal.toString() : "";
	var sPad = (typeof(strPad) != "undefined") ? strPad.toString() : "";
	if (sPad.length == 0) sPad = " ";
	while (sVal.length < nLength) sVal = sPad + sVal;
	return sVal;
}

// JavaScript encoding for a text constant
function wQuote(strVal)
{
	var result = "";
	var sVal = (typeof(strVal) != "undefined") ? strVal.toString() : "";
	var nLen = sVal.length;
	for (var i=0; i < nLen; i++)
	{
		var chCode = sVal.charCodeAt(i);
		if (chCode == 34 || chCode == 92 || chCode == 47 || chCode == 39)
		{
			result += "\\" + String.fromCharCode(chCode);
		}
		else if (chCode >= 32 && chCode < 127)
		{
			result += String.fromCharCode(chCode); 
		}
		else
		{
			result += "\\u" + wLeftPad(chCode.toString(16),"0",4);
		}
	}
	return result;
}

// Embed strings into base string
function wEmbed(strBase)
{
	var sEmbed = "%";
	var sBase = strBase.toString();
	var args = wEmbed.arguments;
	var nArgs = args.length;
	var result = "";
	for (;;)
	{
		var nPos = sBase.indexOf(sEmbed);
		if (nPos < 0) break;
		result += sBase.substr(0,nPos);
		sBase = sBase.substr(nPos+1);
		var chIdx = sBase.substr(0,1);
		var idx = parseInt(chIdx,10);
		if (idx)
		{
			result += (idx < nArgs) ? args[idx] : "";
			sBase = sBase.substr(1);
		}
		else
		{
			result += sEmbed;
			if (chIdx == sEmbed) sBase = sBase.substr(1);
		}
	}
	return result + sBase;
}

// Parse string into an array of strings
function wParse(strParse,nMin,strDelim)
{
	var sDelim = (typeof(strDelim) == "string"
                   && strDelim.length > 0) ? strDelim : "::";
	var result = new Array(), idx = 0;
	for (var i = 0; i < nMin; i++) result[i] = "";
	var sParse = typeof(strParse) == "string" ? strParse : "";
	for (;;)
	{
		var nPos = sParse.indexOf(sDelim);
		if (nPos < 0) break;
		result[idx++] = sParse.substr(0,nPos);
		sParse = sParse.substr(nPos+sDelim.length);
	}
	result[idx] = sParse;
	return result;
}

// Create HTML options from ID and optional selected option
function wOptions(strID,strSelectedOption, strOpts)
{
	var sOpts = (typeof(strOpts) == "string" && strOpts.length > 0) ? strOpts
			: wValue("OPTS_"+strID);
	var aOptions = wParse(sOpts);
	var sPrefix  = "OPT_" + strID + "_";
	var sResult = "";
	var idx;
	for (idx = 0; idx < aOptions.length; idx++)
	{
		var sOpt = aOptions[idx];
		var sTxt = wValue(sPrefix + sOpt); // "" if not found
		sResult += "<option ";
		if (sOpt == strSelectedOption) sResult += "selected ";
		sResult += "value=\"" + sOpt + "\">" + wEncode(sTxt) + "<\/option>";
	}
	return sResult;
}

// Add title to HTML header
function wHeadTitle(sTitle)
{
	var sResult = (typeof(sTitle) == "string" && sTitle.length > 0) ?
		"<title>" + wEncode(sTitle,false,true) + "<\/title>" : "";
	return sResult;
}

// Add an underline to indicate an access key
function wUnderline(sKey,sText)
{
	var kLen = sKey.length;
	if (kLen == 0) return sText;
	var nPos = sText.toLowerCase().indexOf(sKey.toLowerCase());
	if (nPos < 0) return sText;
	return sText.substr(0,nPos) + "<u>" + sText.substr(nPos,kLen) +
		 "<\/u>" + sText.substr(nPos+kLen);
}

// Tab text is encoded with non-breaking spaces
function wEncodeTab(sText)
{
	return wEncode(sText,true);
}

// Convert location.search format string into 'dictionary' object.
function wQueryDict(sSearch)
{
	var oResult = new Object();
	var strSearch = typeof(sSearch) == "string" ? sSearch : "";
	if (strSearch.substr(0,1) == "?") strSearch = strSearch.substr(1);
	while (strSearch.length > 0)
	{
		var nPos = strSearch.indexOf("=");
		if (nPos < 0) break;
		var strName = unescape(strSearch.substr(0,nPos));
		strSearch = strSearch.substr(nPos+1);
		nPos = strSearch.indexOf("&");
		var strValue = "";
		if (nPos >= 0)
		{
			strValue = strSearch.substr(0,nPos);
			strSearch = strSearch.substr(nPos+1);
		}
		else
		{
			strValue = strSearch;
			strSearch = "";
		}
		// remove superfluous quotes around value 
		if (strValue.substr(0,1) == "'")
		{
			strValue = strValue.substr(1);
		}
		nPos = strValue.length-1;
		if (strValue.substr(nPos,1) == "'")
		{
			strValue = strValue.substr(0,nPos);
		}
		strValue = unescape(strValue);
//DIAG		alert ("wQueryDict: oResult."+strName+": "+strValue);
		oResult[strName] = strValue;
	}
	return oResult;
}

function wCookieDict(sCookie)
{
	var oResult = new Object();
	var aCookie = wParse(typeof(sCookie) == "string" ? sCookie : "",0,"; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var sItem = aCookie[i];
		var nPos = sItem.indexOf("=");
		if (nPos < 0) break;
		var sName = unescape(sItem.substr(0,nPos));
		sItem = sItem.substr(nPos+1);
		nPos = sItem.indexOf("=");
		if (nPos < 0)
		{
			oResult[sName] = unescape(sItem);
//DIAG			alert ("wCookieDict: oResult."+sName+": "+oResult[sName]);
		}
		else
		{
			var oItem = new Object();
			var aItem = wParse(sItem,0,"&");
			for (var j=0; j < aItem.length; j++)
			{
				sItem = aItem[j];
				nPos = sItem.indexOf("=");
				if (nPos < 0) break;
				var sName2 = unescape(sItem.substr(0,nPos));
				oItem[sName2] = unescape(sItem.substr(nPos+1));
//DIAG				alert ("wCookieDict: oResult."+sName+"."+sName2+": "+oItem[sName2]);
			}
			oResult[sName] = oItem;
		}
	}
	return oResult;
}

// Get named property string from dictionary object
function wQueryValue(oDict,sName)
{
	var sResult = "";
	if (typeof(oDict) == "object" && typeof(sName) == "string" && typeof(oDict[sName]) == "string")
	{
		sResult = oDict[sName];
	}
	return sResult;
}

// Get named property string from dictionary object
function wQueryValue2(oDict,sName, sName2)
{
	var sResult = "";
	if (typeof(oDict) == "object" && typeof(sName) == "string" && typeof(sName2) == "string" &&
		typeof(oDict[sName]) == "object" && typeof((oDict[sName])[sName2]) == "string")
	{
		sResult = (oDict[sName])[sName2];
	}
	return sResult;
}

// Convert coded value to its text equivalent
function wCodedValue(strID,strOption)
{
	var result;
	if (strOption == "no_value" || strOption == "" || strOption == "0") 
	{
		result = wValue("TXT_NO_VALUE");
	} 
	else if (strOption == "value_unknown") 
	{
		result = wValue("TXT_UNKNOWN");
	}
	else 
	{
		result = wValue("OPT_" + strID + "_" + strOption);
		if (result.length == 0) result = wEmbed(wValue("ERR_" + strID),strOption);
	}
	return result;
}

function wApplicantType(sCode)
{
	return wCodedValue("APPLICANT_TYPE",sCode);
}

///***************************************************************************************************************
/// It was handy to return a space if nothing has been found.  This is so that when the entry goes into a table
/// the cell's border still gets drawn.
///***************************************************************************************************************
function wPaymentStatus(sCode)
{
    var ret = wCodedValue("PAYMENT_STATUS",sCode);
    if (ret == "")
    {
        ret = " ";
    }
    return ret;
}

function wSpecialStatus(sCode)
{
	return wCodedValue("SPECIAL_STATUS",sCode);
}

function wCurrPrevAddress(sCode)
{
	return wCodedValue("CURR_PREV_ADDR",sCode);
}

function wAddressType(sCode)
{
	return wCodedValue("ADDRESS_TYPE",sCode);
}

function wCompanyType(sCode)
{
	return wCodedValue("COMPANY_TYPE",sCode);
}

function wConsent(sCode)
{
	return wCodedValue("CONSENT",sCode);
}


function wCreditFacilityStatus(sCode)
{
	return wCodedValue("CREDIT_FAC_STATUS",sCode);
}

function wCurrency(sCode)
{
	return wCodedValue("CURRENCY",sCode);
}


function wCurrPrevEmployer(sCode)
{
	return wCodedValue("CURR_PREV_EMPL",sCode);
}

function wFinanceType(sCode)
{
	return wCodedValue("FINANCE_TYPE",sCode);
}

function wIDType(sCode)
{
	return wCodedValue("IDENTITY_TYPE",sCode);
}

function wIncomeFrequency(sCode)
{
	return wCodedValue("INCOME_FREQUENCY",sCode);
}

function wNetGross(sCode)
{
	return wCodedValue("NET_GROSS",sCode);
}

function wInterestRate(sCode)
{
	return wCodedValue("INTEREST_RATE",sCode);
}

function wMaritalStatus(sCode)
{
	return wCodedValue("MARITAL_STATUS",sCode);
}

function wPaymentMethod(sCode)
{
	return wCodedValue("PAYMENT_METHOD",sCode);
}

function wNationality(sCode)
{
	return wCodedValue("NATIONALITY",sCode);
}

function wOccupation(sCode)
{
	return wCodedValue("OCCUPATION",sCode);
}

function wPaymentFrequency(sCode)
{
	return wCodedValue("PAYMENT_FREQUENCY",sCode);
}

function wFinancePurpose(sCode)
{
	return wCodedValue("FINANCE_PURPOSE",sCode);
}

function wEnquiryReason(sCode)
{
	return wCodedValue("ENQUIRY_REASON",sCode);
}

function wClosureReason(sCode)
{
	return wCodedValue("CLOSURE_REASON",sCode);
}

function wBlockDispute(sCode)
{
	return wCodedValue("BLOCK_DISPUTE",sCode);
}

function wDelete(sCode)
{
	return wCodedValue("DELETE",sCode);
}

function wResidentialStatus(sCode)
{
	return wCodedValue("RESIDENTIAL_STATUS",sCode);
}

function wSex(sCode)
{
	return wCodedValue("SEX",sCode);
}

function wSpecialInstruction(sCode)
{
	return wCodedValue("SPECIAL_INSTRUCTION",sCode);
}

function wAddressFormat(sCode)
{
	return wCodedValue("ADDRESS_FORMAT",sCode);
}

function wNameFormat(sCode)
{
	return wCodedValue("NAME_FORMAT",sCode);
}

// get (upper-case) filename from location.pathname
function wFile()
{
	var sResult = "";
	if (typeof(window) == "object")
	{
	    sResult = location.pathname;
	}
	var nPos = sResult.lastIndexOf("/");
	if (nPos >= 0) sResult = sResult.substr(nPos+1);
	nPos = sResult.lastIndexOf("\\");
	if (nPos >= 0) sResult = sResult.substr(nPos+1);
	nPos = sResult.indexOf(".");
	if (nPos >= 0) sResult = sResult.substr(0,nPos);
	if (sResult.length == 0) sResult = "default";
	sResult = sResult.toUpperCase();
	return sResult;
}

// get Language from name
function wLanguage(sFile)
{
	var sResult = "";
	if (typeof(window) == "object"
	 && typeof(window.parent) == "object"
	 && typeof(window.parent.gLanguage) == "string")
	{
		// language comes from parent window (frame)
		sResult = window.parent.gLanguage;
	}
	else if (sFile.substr(0,8) == "WELCOME_")
	{
		// language comes from remaining part of file
		sResult = sFile.substr(8);
	}
	else
	{
		sResult = wQuote(wQueryValue(wQueryDict(location.search),"language").toUpperCase());
	}
	// Use English as a default
	if (sResult == "") sResult = "EN";
	return sResult;
}

// Include for configuration and language-specific JavaScript files 
function wIncludes()
{
	var sFile = wFile();
	var sLang = wLanguage(sFile);
	var lLang = sLang.toLowerCase();
	if (sFile.substr(0,8) == "WELCOME_") sFile = "WELCOME";
	var sResult =
		"<script language=\"javascript\" src=\"include/config.js\"><\/script>\r\n" + 
		"<script language=\"javascript\" src=\""+sLang+"/text_"+lLang+".js\"><\/script>\r\n" + 
		"<script language=\"javascript\" src=\""+sLang+"/config_"+lLang+".js\"><\/script>\r\n" +
		"<script language=\"javascript\">var gLanguage=\"" + sLang + "\"; var gFile=\"" +
			 sFile+"\";<\/script>";
	return sResult;
}
// Automatically-include config and language-specific JavaScript files
wWrite(wIncludes());

function wAttribute(sAttribute,sValue)
{
	return ((typeof(sValue) == "string" && sValue.length > 0) ?
		 " " + sAttribute + "=\"" + sValue + "\"" : "");
}

function wHeader(sLang,sFile)
{
	var sCharset = typeof(LNG_CHARSET) == "string" ? LNG_CHARSET : "iso-8895-1";
	var sCssFile = typeof(CSS_FILE)    == "string" ? CSS_FILE : "textformat";
	var sTitle   = wEncode(wValue("HEAD_"+sFile),false,true);
	var sResult  =
		"<html" + wAttribute("dir",wValue("LNG_DIR")) + ">\r\n" +
		"<head>\r\n" +
		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" +
			sCharset + "\">\r\n" +
		"<link rel=\"stylesheet\" href=\"include/" + sCssFile +
			".css\" type=\"text/css\">\r\n" + 
		"<link rel=\"stylesheet\" href=\"" + sLang + "/" + sCssFile + "_" +
			sLang.toLowerCase() + ".css\" type=\"text/css\">\r\n" + 
		"<title>" + sTitle + "<\/title>" +
		"<script language=\"javascript\">document.charset=\"" +
			sCharset + "\";</script>";
	// This last line is a cludge to get the document charset set correctly
	// It should have been set by the meta Content-Type above but it hasn't.
	// We can try setting it directly as follows
	// 	if (typeof(document) == "object") document.charset = sCharset;
	// but that doesn't work either.
	// This delays the set until the document has started and seems to work.
	return sResult;
}

function wSpacer()
{
	var sHTML = "<table width='100%' cellpadding='0' cellspacing='0'>" + 
		"<tr><td class='clsSpacer'>&nbsp;</td></tr></table>";
	wWrite(sHTML);
}

function tableStart(sID,sClass,bCenter,bNoSpace)
{
	var sWidth = (bCenter) ? " align='center'" : " width='100%'";
	var sSpace = (bNoSpace) ? " cellpadding='0' cellspacing='0'" : "";
	wWrite("<table border='0'" + sWidth + wAttribute("id",sID) +
		 wAttribute("class",sClass) + sSpace + "><tbody>");
}

function tableEnd()
{
	wWrite("</tbody></table>");
}

function trStart(sID,sStyle,sRowType)
{
	wWrite("<tr" + wAttribute("id",sID) + wAttribute("style",sStyle) +
		wAttribute("rowType",sRowType) + ">");
}

function wLabelResultRow(label, id)
{
	var sHTML = "<tr>\r\n  <td class='smlLabel' width='25%'>" + wEncode(label) +
 		 "</td>\r\n  <td width='75%' colspan='3' class='smlResult'" + wAttribute("id",id) +
		 "></td>\r\n</tr>";
	wWrite(sHTML);
}

function writeLabelAndResult(label, id, width)
{
	var sHTML = "  <td class='smlLabel' width='" + width + "%'>" +
		 wEncode(label) + "</td>\r\n  <td width='" + width +
		 "%' class='smlResult'" + wAttribute("id",id) + "></td>";
	wWrite(sHTML);
}

function writeIDLabelAndResult(id, label, result, width)
{
	var sHTML = "  <td class='smlLabel' width='" + width + "%'" +
		 wAttribute("id",id) + ">" + wEncode(label) + "</td>\r\n  <td width='" +
		 width + "%' class='smlResult'" + wAttribute("id",result) + "></td>";
	wWrite(sHTML);
}

function wTHeader(sLabel,nCols,sClass,sID,sRowType,sLabelID,sStyle)
{
	if (typeof(nCols)  != "number") nCols  = 4;
	if (typeof(sClass) != "string") sClass = "medLabel";
	var sHTML = "<tr" + wAttribute("id",sID) + wAttribute("rowType",sRowType) +
		 wAttribute("style",sStyle) + ">\r\n  <td colspan='" + nCols + "'" +
		 wAttribute("id",sLabelID) + wAttribute("class",sClass) + ">" +
		 wEncode(sLabel) + "</td>\r\n</tr>";
	wWrite(sHTML);
}

function writeMediumLabel(header,nCols)
{
	if (typeof(nCols) != "number") nCols = 4;
	var sHTML = "<tr>\r\n  <td class='medItalicLabel' colspan='" + nCols + "'>" +
		wEncode(header) + "</td>\r\n</tr>";
	wWrite(sHTML);
}

function writeMediumLabel1(header)
{
	var sHTML = "<tr>\r\n  <td class='medItalicLabel1' colspan='4'>" +
		wEncode(header) + "</td>\r\n</tr>";
	wWrite(sHTML);
}

function trEnd()
{
	wWrite("</tr>");
}

function wPRHeader(nLevel, sHeader, bNewPage, sID)
{
	if (typeof(nLevel) != "number") nLevel = 2;
	var sNewPage = (bNewPage) ? " style='page-break-before: always;'" : "";
	var sHTML = "\r\n<h" + nLevel + " class='clsPRH" + nLevel + "'" + 
		 wAttribute("id",sID) + sNewPage + ">" +
		 wEncode(sHeader) + "</h" + nLevel + ">";
	wWrite(sHTML);
}

function wPRHeaderCenter(nLevel, sHeader, bNewPage, sID)
{
	if (typeof(nLevel) != "number") nLevel = 2;
	var sNewPage = (bNewPage) ? " style='page-break-before: always;'" : "";
	var sHTML = "\r\n<h" + nLevel + " class='clsPRH" + nLevel + "C'" + 
		 wAttribute("id",sID) + sNewPage + ">" +
		 wEncode(sHeader) + "</h" + nLevel + ">";
	wWrite(sHTML);
}

function wPRHeaderUnderline(nLevel, sHeader, bNewPage, sID)
{
	if (typeof(nLevel) != "number") nLevel = 2;
	var sNewPage = (bNewPage) ? " style='page-break-before: always;'" : "";
	var sHTML = "\r\n<h" + nLevel + " class='clsPRH" + nLevel + "U'" + 
		 wAttribute("id",sID) + sNewPage + ">" +
		 wEncode(sHeader) + "</h" + nLevel + ">";
	wWrite(sHTML);
}

function wLabel(sLabel,sLabelID,sClass,sWidth,sHeight)
{
	var sHTML = "  <td" + wAttribute("class",sClass) + wAttribute("id",sLabelID) +
		 wAttribute("width",sWidth) + wAttribute("height",sHeight) + ">" + wEncode(sLabel) + "</td>";
	wWrite(sHTML);
}

function wResult(sID,nWidthR,sClass)
{
	if (typeof(nWidthR) != "number") nWidthR = 25;
	var sWResult  = " width='" + nWidthR + "%'";
	if (nWidthR >= 80) sWResult += " colspan='5'";
	else if (nWidthR >= 50) sWResult += " colspan='3'";
	var sHTML = "  <td" + wAttribute("class",sClass) + wAttribute("id",sID) +
		 sWResult + "></td>";
	wWrite(sHTML);
}

function wPRLabel(sLabel,sLabelID,nWidthL)
{
	if (typeof(nWidthL) != "number") nWidthL = 25;
	var sWLabel  = " width='" + nWidthL + "%'";
	var sHTML = "  <td class='clsPRLabel'" + wAttribute("id",sLabelID) +
		 sWLabel + ">" + wEncode(sLabel) + "</td>";
	wWrite(sHTML);
}

function wPRResult(sID,nWidthR)
{
	if (typeof(nWidthR) != "number") nWidthR = 25;
	var sWResult  = " width='" + nWidthR + "%'";
	if (nWidthR >= 50) sWResult += " colspan='3'";
	var sHTML = "  <td class='clsPRResult'" + wAttribute("id",sID) +
		 sWResult + "></td>";
	wWrite(sHTML);
}

function wPRCell(sLabel,sID,sLabelID,nWidthR,nWidthL)
{
	if (typeof(nWidthR) != "number") nWidthR = 25;
	if (typeof(nWidthL) != "number") nWidthL = (nWidthR < 50) ? 50 - nWidthR : 100 - nWidthR;
	var sWLabel  = " width='" + nWidthL + "%'";
	var sWResult  = " width='" + nWidthR + "%'";
	if (nWidthR >= 80) sWResult += " colspan='5'";
	else if (nWidthR >= 50) sWResult += " colspan='3'";
	var sHTML = "  <td class='clsPRLabel'" + wAttribute("id",sLabelID) +
		 sWLabel + ">" + wEncode(sLabel) +
		 "</td>\r\n  <td class='clsPRResult'" + wAttribute("id",sID) +
		 sWResult + "></td>";
	wWrite(sHTML);
}

function wTCell(sLabel,sID,sLabelID,nWidthR,nWidthL)
{
	if (typeof(nWidthR) != "number") nWidthR = 25;
	if (typeof(nWidthL) != "number") nWidthL = (nWidthR < 50) ? 50 - nWidthR : 100 - nWidthR;
	wLabel(sLabel,sLabelID,"smlLabel",nWidthL + "%");
	wResult(sID,nWidthR,"smlResult");
}

function wTLabel(sLabel,sLabelID,nWidthL)
{
	if (typeof(nWidthL) != "number") nWidthL = 25;
	wLabel(sLabel,sLabelID,"smlLabel",nWidthL + "%");
}

function wTResult(sID,nWidthR)
{
	wResult(sID,nWidthR,"smlResult");
}

var W_MENU_SPACER = false;

function wMenuStart()
{
	var sHTML = "<table class='clsMenuText' cellspacing='0' cellpadding='0' valign='MIDDLE'><tbody><tr>";
	wWrite(sHTML);
	W_MENU_SPACER = false;
}

function wMenuFill(sLabel)
{
	var sHTML  = "<td class='clsMenuFill' nowrap>" + wEncodeTab(sLabel) + "<\/td>";
	wWrite(sHTML);
	W_MENU_SPACER = false;
}

function wMenu(sID,sLabel,bNoDim,bNoHide,sOver)
{
	var sSpacer = wValue("MENU_SPACER");
	if (W_MENU_SPACER && sSpacer.length > 0)
	{
		var sDisplay = (bNoHide) ? "" : " style='display:none;'";
		var sSpacerHTML = "<td class='clsMenuSpacer'" + sDisplay + ">" + wEncodeTab(sSpacer) + "<\/td>";
		wWrite(sSpacerHTML);
	}
	var sClass = (bNoHide) ? "clsMenu" : "clsMenuHide";
	var sTab = wEncodeTab(sLabel);
	var sDim = (bNoDim) ? "" : "\r\n<span class='clsMenuDim'>" + sTab + "<\/span>";

	var sHTML = "<td class='" + sClass + "'" + wAttribute("id",sID) +
		 " onclick='menuClick(this)' onmouseover='menuOver(this)'" +
		 " onmouseout='menuOut(this)'" + wAttribute("XOver",sOver) + ">" +
		 "<a class='clsMenuLink' href='javascript:null'" +
		 "onclick='this.blur(); return false;'>" + sTab + "<\a>" + sDim + "<\/td>";
        if(sID=="Survey")
{
        var sHTML = "<td class='" + sClass + "'" + wAttribute("id",sID) +
		 " onmouseover='menuOver(this)'" +
		 " onmouseout='menuOut(this)'" + wAttribute("XOver",sOver) + ">" +
		 "<a class='clsMenuLink' href='survey/survey.aspx?surveyid=6&ln=ar-sa'" +
		 ";'>" + sTab + "<\a>" + sDim + "<\/td>";
}
        if(sID=="Survey" && sLabel=="Survey")
{
        var sHTML = "<td class='" + sClass + "'" + wAttribute("id",sID) +
		 " onmouseover='menuOver(this)'" +
		 " onmouseout='menuOut(this)'" + wAttribute("XOver",sOver) + ">" +
		 "<a class='clsMenuLink' href='/surveyEN/survey.aspx?surveyid=6'" +
		 ";'>" + sTab + "<\a>" + sDim + "<\/td>";
}
	wWrite(sHTML);
	W_MENU_SPACER = true;
}

function wMenuEnd()
{
	var sHTML = "<\/tr><\/tbody><\/table>";
	wWrite(sHTML);
	W_MENU_SPACER = false;
}

function menuPrevItem(oMenuItem)
{
	for (var oPrev = oMenuItem.previousSibling; oPrev != null; oPrev = oPrev.previousSibling)
	{
		var sPrevClass = oPrev.className;
		if (sPrevClass == "clsMenu" || sPrevClass == "clsMenuDimTD")
		{
			return oPrev;
		}
		if (sPrevClass == "clsMenuFill")
		{
			return null;
		}
	}
	return null;
}

function menuNextItem(oMenuItem)
{
	for (var oNext = oMenuItem.nextSibling; oNext != null; oNext = oNext.nextSibling)
	{
		var sNextClass = oNext.className;
		if (sNextClass == "clsMenu" || sNextClass == "clsMenuDimTD")
		{
			return oNext;
		}
		if (sNextClass == "clsMenuFill")
		{
			return null;
		}
	}
	return null;
}

function hideMenuSpacer (bHidden,oMenuItem)
{
	var oSpacer = (oMenuItem != null) ? oMenuItem.previousSibling : null;
	if (oSpacer != null && oSpacer.className == "clsMenuSpacer")
	{
		oSpacer.style.display = (bHidden) ? "none" : "block";
	}
}

function hideMenuItem (oMenuItem)
{
	var sMenuClass = oMenuItem.className;
	oMenuItem.className = "clsMenuHide";
	if (sMenuClass == "clsMenu" || sMenuClass == "clsMenuDimTD")
	{
		hideMenuSpacer(true,oMenuItem);
		if (menuPrevItem(oMenuItem) == null)
		{
			hideMenuSpacer(true,menuNextItem(oMenuItem));
		}
	}	
}

function dimMenuItem (oMenuItem)
{
	var sMenuClass = oMenuItem.className;
	var oLink = oMenuItem.firstChild;
	var oSpan = oLink.nextSibling;
	oMenuItem.className = "clsMenuDimTD";
	oLink.style.display = "none";
	oSpan.style.display = "block";
	if (sMenuClass == "clsMenuHide")
	{
		hideMenuSpacer(false,menuNextItem(oMenuItem));
		if (menuPrevItem(oMenuItem) != null)
		{
			hideMenuSpacer(false,oMenuItem);
		}
	}
}

function showMenuItem (oMenuItem)
{
	var sMenuClass = oMenuItem.className;
	var sSpacer = wValue("MENU_SPACER");
	var oLink = oMenuItem.firstChild;
	var oSpan = oLink.nextSibling;
	oMenuItem.className = "clsMenu";
	oLink.className = "clsMenuLink";
	oLink.style.display = "block";
	if (oSpan != null)
	{
		oSpan.style.display = "none";
	}
	if (sMenuClass == "clsMenuHide")
	{
		hideMenuSpacer(false,menuNextItem(oMenuItem));
		if (menuPrevItem(oMenuItem) != null)
		{
			hideMenuSpacer(false,oMenuItem);
		}
	}
}

function menuClick(oMenuItem)
{
 	event.cancelBubble = true;
 	if (oMenuItem.className != "clsMenuDimTD")
		eval("do" + oMenuItem.id + "()");
	event.returnValue = false;
}

function menuOver(oMenuItem)
{
	var oLink = oMenuItem.firstChild;
	oLink.className = "clsMenuLinkSelected";
	if (typeof(oMenuItem.XOver) == "string" && oMenuItem.XOver.length > 0)
	{
		eval(oMenuItem.XOver);
	}
}

function menuOut(oMenuItem)
{
	var oLink = oMenuItem.firstChild;
	oLink.className = "clsMenuLink";
}

function wTabStart(sQualifier,sID)
{
	var sHTML = "<table border='0' cellspacing='0' cellpadding='0' class='clsTabTable' " +
		wAttribute("id",sID) + "><tbody><tr id='tabRow" + sQualifier + "'>";
	wWrite(sHTML);
}

function wTabItem(sID,sLabel,bNoDim,bHilite)
{
	var sTab = wEncodeTab(sLabel);
	var sClass = (bHilite) ? "clsTabHilite" : "clsTab";
	var sDim = (bNoDim) ? "" : "<span class='clsDimTabLink'>" + sTab + "<\/span>";
	var sSpacer = (typeof(TAB_SPACER) != "undefined" && TAB_SPACER) ?
		"<td class='clsTabSpacer' id='" + sID + "Spacer'>&nbsp;<\/td>" : "";
	var sHTML = sSpacer + "<td" + wAttribute("class",sClass) + wAttribute("xClass",sClass) +
		" id='" + sID +	"' onclick='fnTabClick(this)' onmouseover='fnTabOver(this)' " +
		"onmouseout='fnTabOut(this)'><a class='clsTabLink' href='javascript:null'" +
		"onclick='this.blur(); return false;'>" + sTab + "<\/a>" + sDim + "<\/td>";
	wWrite(sHTML);
}

function wTabFill(sID)
{
	var sHTML = "<td class='clsTabFill'" + wAttribute("id",sID) + ">&nbsp;<\/td>";
	wWrite(sHTML);
}

function wTabEnd()
{
	var sHTML = "<\/tr><\/tbody><\/table>";
	wWrite(sHTML);
}

function fnTabDim (bDim,oTab)
{
	var oLink = oTab.firstChild;
	var oSpan = oLink.nextSibling;
	if (bDim)
	{
		oTab.className = "clsDimTab";
		oLink.style.display = "none";
		oSpan.style.display = "block";
	}
	else
	{
		oTab.className = oTab.xClass;
		oLink.className = "clsTabLink";
		oLink.style.display = "block";
		oSpan.style.display = "none";
	}
}

function fnTabText(sLabel,oTab)
{
	var sTab = wEncodeTab(sLabel);
	var oLink = oTab.firstChild;
	if (oLink != null)
	{
		oLink.innerText = sTab;
		var oSpan = oLink.nextSibling;
		if (oSpan != null)
		{
			oSpan.innerText = sTab;
		}
	}
}

function fnTabHide(bHidden,oTab)
{
	oTab.style.display = (bHidden) ? "none" : "block";
	if (typeof(TAB_SPACER) != "undefined" && TAB_SPACER)
	{
		oTab.previousSibling.style.display = (bHidden) ? "none" : "block";
	}
}

function fnTabOver(oTab)
{
	var oLink = oTab.firstChild;
	if (oLink.className == "clsTabLink")
		oLink.className = "clsTabLinkOver";
}

function fnTabOut(oTab)
{
	var oLink = oTab.firstChild;
	if (oLink.className == "clsTabLinkOver")
		oLink.className = "clsTabLink";
}

function fnTabClick(oTab)
{
 	event.cancelBubble = true;
	doTabClick (oTab,0,-1);
	event.returnValue = false;
}

function doTabClick(oTab,nIdx,nExtra)
{
	if (oTab.className != "clsDimTab")
	{
		// deselect all the tabs (both the TD and the A tags)
		var oRow = oTab.parentNode;
	 	for (var i = 0; i < oRow.cells.length-1; i++)
 		{
 			var oCell = oRow.cells(i);
			var sClass = oCell.className;
			if (sClass != "clsDimTab" && sClass != "clsTabSpacer")
			{
		 		oCell.className = oCell.xClass;
				oCell.firstChild.className = "clsTabLink";
			}
 		}
		// perform tab-specific functions
		var sQual = oRow.id.substr(6);
		eval("show" + sQual + "TabOutput(oTab.id,nIdx,nExtra)");
		// Set the clicked tab to be selected (both TD and A tags)
	 	oTab.className = "cls" + sQual + "TabSelected";
		oTab.firstChild.className = "cls" + sQual + "TabLinkSelected";
	}
}

function wDivStart(sID,sClass,sStyle)
{
	var sHTML = "<div" + wAttribute("class",sClass) + wAttribute("id",sID) +
		wAttribute("style",sStyle) + ">";
	wWrite(sHTML);
}

function wDivEnd()
{
	var sHTML = "<\/div>";
	wWrite(sHTML);
}

function wFormStart(sID,sOnKeyPress,sAction,sMethod)
{
	var sHTML = "<form" + wAttribute("id",sID) + wAttribute("name",sID) + 
		wAttribute("onkeypress",wQuote(sOnKeyPress)) +
		wAttribute("action",wQuote(sAction)) + wAttribute("method",sMethod) + ">";
	wWrite(sHTML);
}

function wFormEnd()
{
	var sHTML = "<\/form>";
	wWrite(sHTML);
}

function tdStart(sClass,sWidth,sColspan,sAlign,sID,sRowSpan)
{
	var sHTML = "<td" + wAttribute("class",sClass) + wAttribute("colspan",sColspan) +
		wAttribute("width",sWidth) + wAttribute("align",sAlign) + 
		wAttribute("id",sID) + wAttribute("rowspan",sRowSpan) + ">";
	wWrite(sHTML);
}

function tdEnd(sLabel)
{
	var sHTML = "<\/td>";
	if (typeof(sLabel) == "string" && sLabel.length > 0)
	{
		sHTML = "\r\n" + wEncode(sLabel) + sHTML; 
	}
	wWrite(sHTML);
}

function wFHidden(sName,sValue)
{
	var sHTML = "<input type='hidden'" + wAttribute("name",sName) +
		wAttribute("value",wQuote(sValue)) + " \/>";
	wWrite(sHTML);
}

function wFInput(sName,sValidation,sSize,sMaxLength,sOnChange,sOnKeyPress,bDisabled,sValue,bPassword)
{
	var sDisabled	= (bDisabled) ? " disabled='true'" : "";
	var sClass	= (bDisabled) ? "grayInput" : "normalInput";
	var sType	= (bPassword) ? "password" : "text";
	var sHTML = "<input type='" + sType + "' class='" + sClass + "'" + wAttribute("name",sName) +
		wAttribute("validation",sValidation) + wAttribute("size",sSize) +
		wAttribute("maxlength",sMaxLength) + wAttribute("onchange",wQuote(sOnChange)) +
		wAttribute("onkeypress",wQuote(sOnKeyPress)) + sDisabled +
		wAttribute("value",sValue) + " \/>";
	wWrite(sHTML);
}

function wFCheck(sName,sValidation,sID,sOnClick,bChecked,bDisabled,sClass)
{
	var sDisabled	= (bDisabled) ? " disabled='true'" : "";
	var sChecked	= (bChecked)  ? " checked='true'" : "";
	var sHTML = "<input type='checkbox'" + wAttribute("name",sName) + 
		wAttribute("validation",sValidation) + wAttribute("id",sID) + 
		wAttribute("onclick",wQuote(sOnClick)) + sChecked + sDisabled + 
		wAttribute("class",sClass) + " \/>";
	wWrite(sHTML);
}

function wFSelect(sName,sValidation,sOption,sSelected,sOptions,sOnChange,bDisabled)
{
	var sDisabled	= (bDisabled) ? " disabled='true'" : "";
	var sClass	= (bDisabled) ? "grayInput" : "normalInput";
	var sHTML = "<select class='" + sClass + "' size='1'" + wAttribute("name",sName) +
		wAttribute("validation",sValidation) + wAttribute("onchange",wQuote(sOnChange)) +
		sDisabled + ">" + wOptions(sOption,sSelected,sOptions) + "<\/select>";
	wWrite(sHTML);
}

function wSpan(sLabel,sID,sClass)
{
	var sHTML = "<span" + wAttribute("id",sID) + wAttribute("class",sClass) + ">" +
		wEncode(sLabel) + "<\/span>";
	wWrite(sHTML);
}

function wButton(sLabel,sID,sOnClick,sColspan)
{
	var sHTML = "<td align='center'" + wAttribute("colspan",sColspan) + 
		"><button" + wAttribute("id",sID) +
		wAttribute("onclick",wQuote(sOnClick)) + ">" + wEncode(sLabel) +
		"<\/button><\/td>";
	wWrite(sHTML);
}

function diffSeconds(dTo,dFrom)
{
	var nFrom = dFrom.getMilliseconds();
	var nTo = dTo.getMilliseconds();
	var bOverflow = nFrom > nTo;
	if (bOverflow) nTo += 1000;
	var nResult = nTo - nFrom;

	nFrom = dFrom.getSeconds();
	nTo = dTo.getSeconds();
	if (bOverflow) nFrom++;
	bOverflow = nFrom > nTo;
	if (bOverflow) nTo += 60;
	nResult += (nTo - nFrom)*1000;

	nFrom = dFrom.getMinutes();
	nTo = dTo.getMinutes();
	if (bOverflow) nFrom++;
	bOverflow = nFrom > nTo;
	if (bOverflow) nTo += 60;
	nResult += (nTo - nFrom)*60000;

	return nResult/1000;	
}

// Returns a new Options string in which the most recently used options appear first.
// The MRU string should end with the delimiter string.
// The MRU string normally comes from a suitably named cookie.
function wMRUoptions(strOptions,strMRU)
{
	if (typeof(strOptions) != "string" || strOptions.length == 0) return "";
	if (    typeof(strMRU) != "string" ||     strMRU.length == 0) return strOptions;
	var sDelim = "::";
	var sOptions = strOptions + sDelim;
	var aMRU = wParse(strMRU,0,sDelim);
	for (var idx = aMRU.length - 2; idx >= 0; idx--)
	{
		var sOption = aMRU[idx];
		var sTemp = sDelim + sOptions;
		var nPos = sTemp.indexOf(sDelim + sOption + sDelim);
		if (nPos >= 0)
		{
			// remove existing option
			sTemp = sTemp.substr(0,nPos) + sTemp.substr(nPos+sDelim.length+sOption.length);
			// reposition option at start of options string
			sOptions = sOption + sTemp;
		}
	}
	// remove additional final delimiter
        return sOptions.substr(0,sOptions.length-sDelim.length);
}

function addName(sLeft,sRight)
{
	if (typeof(sRight) != "string" || sRight.length == 0) return sLeft;
	if (typeof(sLeft) != "string" || sLeft.length == 0) return sRight;
	return (sLeft + " " + sRight);
}

function getIndex(oThis,oArray)
{
	var nLength = oArray.length;
	for (var i = 0; i < nLength; i++)
	{
		if (oThis == oArray[i]) return i;
	}
	return -1;
}

function setDisplayPossibleArray(fieldName, displayStyle, nIdx)
{
    if (typeof(fieldName) == "string" &&
        typeof(displayStyle) == "string")
    {
        if (typeof(nIdx) == "number")
        {
            var obj = window[fieldName];
            if (obj != null)
            {
                if (typeof(obj.length) == "undefined")
                {
                    obj.style.display = displayStyle;
                }
                else
                {
                    if (obj.length > nIdx)
                    {
                        obj[nIdx].style.display = displayStyle;
                    }
                }
            }   
        }
        else
        {
            // Do all
            var possibleArray = window[fieldName];
            if (possibleArray != null)
	    {
                if (typeof(possibleArray.length) != "undefined")
            	{
                	for (var i = 0; i < possibleArray.length; ++i)
                	{
                    		// Recurse
                    		setDisplayPossibleArray(fieldName, displayStyle, i);
                	}
            	}
	    	else
	    	{
			//possibleArray is not null, but I think that if only one
			//is found it doesn't create an array, so the .length will be undefined.
			//So assume that there is one here
			setDisplayPossibleArray(fieldName, displayStyle, 0);
		}
	    }
        }
    }
}

function hideSection(bHidden,oTable,sAttr,sID)
{
	var oRows = oTable.rows;
	for (var i = 0; i < oRows.length; i++)
	{
		var oRow = oRows[i];
		if (oRow.rowType == sAttr && (typeof(sID) != "string" || sID.length == 0 || oRow.id == sID))
		{
			oRow.style.display = (bHidden) ? "none" : "block";
		}
	}
}

