var firstSortParam = 0;
var secondSortParam = 0;
var thirdSortParam = 0;
var unSortedArray = new Array();

function getMergedArray(strTab, strSearchString){

	firstSortParam = 1;
	var sortedArray = new Array();

	if(strTab != 'market'){
		checkAndAppendArray(strTab, strSearchString, "ny");
		checkAndAppendArray(strTab, strSearchString, "ar");
	}
	checkAndAppendArray(strTab, strSearchString, "eu");
	checkAndAppendArray(strTab, strSearchString, "al");
	
	sortedArray = unSortedArray.sort(oneLevelSortAscending);
	//alert("sortedArray array length : "+sortedArray.length);
	return sortedArray;
}

function checkAndAppendArray(strTab, strSearchString, strExchange){	
	
	var tempArray = new Array();
	if(eval("window.lc_"+strExchange+"_"+strTab+"_"+strSearchString+"_companies") != undefined){
		tempArray = eval("window.lc_"+strExchange+"_"+strTab+"_"+strSearchString+"_companies");
		//alert(strExchange+" array length : "+tempArray.length);
		if (tempArray.length > 0)
			unSortedArray = unSortedArray.concat(tempArray);
		//document.write(strExchange +" : "+ unSortedArray);
	}
	else{
		//document.write("// "+strExchange.toUpperCase()+" COMPANIES DATA NOT AVAILABLE //");
	}
}

function oneLevelSortAscending(a,b){ // For Company name alone use removeThe() function

	if (removeThe(a[firstSortParam]) < removeThe(b[firstSortParam])) return -1; 
	if (removeThe(a[firstSortParam]) > removeThe(b[firstSortParam])) return 1;
	return 0; // Both the values are equal
}

function twoLevelSortAscending(a,b) { 

	if ((removeThe(a[firstSortParam]) + removeThe(a[secondSortParam])) < (removeThe(b[firstSortParam]) + removeThe(b[secondSortParam]))) return -1; 
	if ((removeThe(a[firstSortParam]) + removeThe(a[secondSortParam])) > (removeThe(b[firstSortParam]) + removeThe(b[secondSortParam]))) return 1; 
	return 0; 
}

function threeLevelSortAscending(a,b) {

	if ((removeThe(a[firstSortParam]) + removeThe(a[secondSortParam]) + removeThe(a[thirdSortParam])) < (removeThe(b[firstSortParam]) + removeThe(b[secondSortParam]) + removeThe(b[thirdSortParam]))) return -1; 
	if ((removeThe(a[firstSortParam]) + removeThe(a[secondSortParam]) + removeThe(a[thirdSortParam])) > (removeThe(b[firstSortParam]) + removeThe(b[secondSortParam]) + removeThe(b[thirdSortParam]))) return 1; 
	return 0; 
}

function removeThe(strCompanyName){
	
	if(strCompanyName != null){
		if(strCompanyName.substring(0,4).toLowerCase()=="the ")
		{
			strCompanyName = strCompanyName.substring(4, strCompanyName.length);
		}
	}
	return strCompanyName.toLowerCase();
}

function getAllSymbols (iSymbolStartPos, iRecordsPerPage , ArrayToGetSym) {

	var sSymbols = "";
	var iSymbolEndPos = 0;
	var strExch = "";

	iSymbolEndPos = getEndPos (iStartPos, iRecordsPerPage, ArrayToGetSym.length);
	//alert("iSymbolEndPos : "+iSymbolEndPos);
	for(var iCounter = eval(iSymbolStartPos) - 1 ; iCounter < iSymbolEndPos; iCounter++){

		sSymbols = ArrayToGetSym[iCounter][0]; 
		strExch = ArrayToGetSym[iCounter][4].toUpperCase();		 
		//alert("strExch : "+strExch);
		if(strExch != null){
			if(strExch == 'NYSE' || strExch == 'NYSE ARCA')
				strCMSSymbols = strCMSSymbols +","+ sSymbols;
			if((strExch.indexOf('EURONEXT') != -1) || (strExch.indexOf('ALTERNEXT') != -1))
				strEuroSymbols = strEuroSymbols +","+ sSymbols;
		}
		strTotalSymbols = strTotalSymbols +","+ sSymbols;
	}
	if (strCMSSymbols.length > 1) {
	  strCMSSymbols = strCMSSymbols.substring(1,strCMSSymbols.length);
	}
	if (strEuroSymbols.length > 1) {
	  strEuroSymbols = strEuroSymbols.substring(1,strEuroSymbols.length);
	}
	if (strTotalSymbols.length > 1) {
	  strTotalSymbols = strTotalSymbols.substring(1,strTotalSymbols.length);
	}
	strCMSSymbols = strCMSSymbols.toUpperCase();
    strCMSSymbols = fnReplace(strCMSSymbols,'.','_');

	strEuroSymbols = strEuroSymbols.toUpperCase();
    strEuroSymbols = fnReplace(strEuroSymbols,'.','_');	

	strTotalSymbols = strTotalSymbols.toUpperCase();
    strTotalSymbols = fnReplace(strTotalSymbols,'.','_');
	//document.write("strTotalSymbols : "+strTotalSymbols);
}

function formatAllData (strAllSymbol) {

	var arrAllSymbols = strAllSymbol.split(",");
	var strAllSymbol = "";
	var strExchSpecSymbol = "";	
	var bFlag = false;
	var iExactPosition = 0;
	var strExch = "";
	for(iTotCnt = 0; iTotCnt < arrAllSymbols.length; iTotCnt ++) {

		strAllSymbol = arrAllSymbols[iTotCnt];
		strAllSymbol = strAllSymbol.replace(".", "_");
		iExactPosition = iTotCnt + eval(iStartPos) - 1;						
		// alert("iExactPosition : "+iExactPosition);
		strExch = aTemp[iExactPosition][4].toUpperCase();	
		//alert("strAllSymbol : "+strAllSymbol);
		//document.write("SYMLIST : "+SYMLIST);
		
		if (strCMSSymbols.length > 1) {
			for(iSungardCnt = 0; iSungardCnt < SYMLIST.length; iSungardCnt ++){
				strExchSpecSymbol = SYMLIST[iSungardCnt];
				
				if((strAllSymbol == strExchSpecSymbol) && (strExch == 'NYSE' || strExch == 'NYSE ARCA')) {
					eval("tickFinal"+iTotCnt+" = eval(tick"+iSungardCnt+")");
					//alert("CMS SYMbol : "+strExchSpecSymbol);
					//SYMLIST[iSungardCnt] = '';
					//eval("tick"+iSungardCnt+" = ''");
					bFlag = true;
					break;
				}
			}
		}
		//document.write("SYMLIST 2 : "+SYMLIST);
		//document.write("Euro_SYMLIST : "+Euro_SYMLIST);
		if(!bFlag){
			if (strEuroSymbols.length > 1) {
				for(iEuroCnt = 0; iEuroCnt < Euro_SYMLIST.length; iEuroCnt ++){
					strExchSpecSymbol = Euro_SYMLIST[iEuroCnt];

					if(strAllSymbol == strExchSpecSymbol){
						eval("tickFinal"+iTotCnt+" = formatEuroData(eval(Euro_tick"+iEuroCnt+"),"+iExactPosition+")");
						//alert("Euro symbol : "+strExchSpecSymbol);
						//document.write("euro dat : "+eval("Euro_tick"+iEuroCnt));
						break;
					}
				}
			}
		}
		bFlag = false;
	}
	SYMLIST = arrAllSymbols;
}

function buildEuroData (strAllSymbol) {

	var arrAllSymbols = strAllSymbol.split(",");
	var strAllSymbol = "";
	var strExchSpecSymbol = "";
	var iExactPosition = 0;
	for(iTotCnt = 0; iTotCnt < arrAllSymbols.length; iTotCnt ++){

		strAllSymbol = arrAllSymbols[iTotCnt];
		strAllSymbol = strAllSymbol.replace(".", "_");
		strAllSymbol = strAllSymbol.toUpperCase();
		for(iEuroCnt = 0; iEuroCnt < Euro_SYMLIST.length; iEuroCnt ++){
			strExchSpecSymbol = Euro_SYMLIST[iEuroCnt].toUpperCase();
			if(strAllSymbol == strExchSpecSymbol){

				iExactPosition = iTotCnt + eval(iStartPos) - 1;
				//alert("iExactPosition : "+iExactPosition);
				eval("tickFinal"+iTotCnt+" = formatEuroData(eval(Euro_tick"+iEuroCnt+"),"+iExactPosition+")");
				break;
			}
		}
	}
	SYMLIST = arrAllSymbols;
}

function formatEuroData(arrSymbolDetail, iTotCnt){
	//alert("formatEuroData() :"+arrSymbolDetail.length);	
	var strURL = buildEuronextLinkURL(arrSymbolDetail);
	if(iTotCnt < aTemp.length)
		aTemp[iTotCnt][3] = strURL;
	//alert("iTotCnt : "+iTotCnt);
	//alert("aTemp[iTotCnt][3] : "+aTemp[iTotCnt][3]);
	var formattedArray = new Array();
	formattedArray.push("WRONG0");
	formattedArray.push("WRONG1");
	formattedArray.push("WRONG2");
	formattedArray.push("WRONG3");
	formattedArray.push("WRONG4");
	formattedArray.push("WRONG5");
	formattedArray.push(arrSymbolDetail[1]); // Symbol Value
	formattedArray.push(arrSymbolDetail[2]); // Company Name
	formattedArray.push("WRONG8");
	formattedArray.push("WRONG9");
	formattedArray.push("WRONG10");
	formattedArray.push(arrSymbolDetail[17]); // Last Traded Time
	formattedArray.push("WRONG12");
	formattedArray.push(arrSymbolDetail[16]); // Last Traded Date 
	formattedArray.push(checkEmptyValue(arrSymbolDetail[9])); // Last Traded Price
	formattedArray.push(getCurrency(arrSymbolDetail[19])); // Euro Currency convertion
	formattedArray.push("WRONG16");
	formattedArray.push("WRONG17");
	formattedArray.push(removeDecimalValue(checkEmptyValue(arrSymbolDetail[10]))); // Volume Value
	formattedArray.push(roundValue(checkEmptyValue(arrSymbolDetail[13]))); // Change Value
	formattedArray.push(arrSymbolDetail[15]); //  Change Sign
	formattedArray.push("WRONG21");
	formattedArray.push("WRONG22");
	formattedArray.push("WRONG23");
	formattedArray.push("WRONG24");
	formattedArray.push("WRONG25");
	formattedArray.push("WRONG26");
	formattedArray.push("WRONG27");
	formattedArray.push("WRONG28");
	formattedArray.push("WRONG29");
	formattedArray.push("WRONG30");
	formattedArray.push("WRONG31");
	formattedArray.push("WRONG32");
	formattedArray.push("WRONG33");
	formattedArray.push("WRONG34");
	formattedArray.push("WRONG35");
	formattedArray.push(roundValue(checkEmptyValue(arrSymbolDetail[14]))); //  Percentage(%) Change
	formattedArray.push("WRONG37");
	formattedArray.push("WRONG38");
	formattedArray.push("WRONG39");
	formattedArray.push("WRONG40");
	formattedArray.push("WRONG41");
	formattedArray.push("WRONG42");
	formattedArray.push("WRONG43");
	formattedArray.push("WRONG44");
	formattedArray.push("WRONG45");
	formattedArray.push("WRONG46");
	formattedArray.push("WRONG47");
	//alert("formattedArray length :"+formattedArray.length);	
	//alert(arrSymbolDetail[1]+" - Currency : "+arrSymbolDetail[19]);	
	return formattedArray;
}

function checkEmptyValue(colValue){

	if(colValue == '')
		colValue = '0';
	return colValue;
}

function roundValue(colValue){

	colValue = fnRound(colValue,2);
	return colValue;
}

function removeDecimalValue(colValue){

	if (colValue.indexOf('.') != -1) {
		colValue = colValue.substring(0,colValue.indexOf('.'));
	}
	return colValue;
}

function buildEuronextLinkURL (arrSymbolDetail) {

	var XPAR_code = 1;
	var XAMS_code = 2;
	var XBRU_code = 3;
	var XLIS_code = 5;
	var XLON_code = 7;
	var DPAR_code = 1;
	var DAMS_code = 2;
	var DBRU_code = 3;
	var DLIS_code = 5;
	var DLON_code = 7;

	var XPAR_desc = "PAR";
	var XAMS_desc = "AMS";
	var XBRU_desc = "BRU";
	var XLIS_desc = "LIS";
	var XLON_desc = "LON";
	var DPAR_desc = "PAR";
	var DAMS_desc = "AMS";
	var DBRU_desc = "BRU";
	var DLIS_desc = "LIS";
	var DLON_desc = "LON";
	
	var typeNumber = '';
	var isin = arrSymbolDetail[0]; // ISIN Value
//	var id_type = arrSymbolDetail[4].toLowerCase(); // Type Value (stocks, index,etc..)
	var id_type = arrSymbolDetail[4]; // Type Value (stocks, index,etc..)
	var id_exch = arrSymbolDetail[3]; // Market / Exchange value (PAR, AMS, etc...)
	
//	alert("id_type : "+id_type);

//	if(id_type=="stock")		// Stocks
	if(id_type=="STOCK")		// Stocks
		typeNumber = "2593";
	else if(id_type=="FUNDS")	// Fund
		typeNumber = "2603";
	else if(id_type=="WARRT")	// Warrants & certificates
		typeNumber = "2616";
	else if(id_type=="TRACK")	// ETF/Trackers
		typeNumber = "2783";
	else if(id_type=="INDEX")	// Index
		typeNumber = "2634";
	else if(id_type=="BOND")	// Bond
		typeNumber = "3022";
	else if(id_type=="etcommodities") // 
		typeNumber = "8505";
	
	var euronextURL = "http://www.euronext.com/trader/summarizedmarket/stocks-" + typeNumber + "-EN-" + isin + ".html?selectedMep=" + eval(id_exch+"_code");
	return euronextURL;
}

function industryType(urlLink)
{
	var subsectorid = getQueryVariable("subsector", urlLink);
	var sectorid = getQueryVariable("sector", urlLink);
	var supersectorid = getQueryVariable("supersector", urlLink);
	var industryIs;
	if(subsectorid!=null)
	{
		industryIs = subsectorid+'||subsector';
	}
	else if(sectorid!=null)
	{
		industryIs = sectorid+'||sector';
	}
	else if(supersectorid!=null)
	{
		industryIs = supersectorid+'||supersector';
	}
	else
	{
		var indexHtml = urlLink.indexOf(".html");
		var indexIndustry = urlLink.indexOf("industry_");
		industryIs = urlLink.substring(indexIndustry+9,indexHtml)+'||industry';	
	}
		
	return industryIs;
}

function regionType(urlLink)
{	
	var regionIs;
	var indexHtml = urlLink.indexOf(".html");
	var indexRegion = urlLink.indexOf("_region_");
	if(indexRegion!=-1)
		regionIs = urlLink.substring(indexRegion+8,indexHtml);
	else 
		regionIs = null;
	return regionIs;
}

function getCurrency(fieldValue) {
	var currency = "&euro;";
	if (fieldValue != "")
	{
		switch(fieldValue)
		{
			case "AUD" : currency = "AU$";
						break;
			case "BEF" : currency = "fr";
						break;
			case "CAD" : currency = "CA$";
						break;
			case "CHF" : currency = "CHF";
						break;
			case "DEM" : currency = "DM";
						break;
			case "DKK" : currency = "kr";
						break;
			case "EUR" : currency = "&euro;";
						break;
			case "GBP" : currency = "&pound;";
						break;
			case "HKD" : currency = "HK$";
						break;
			case "ISK" : currency = "krt";
						break;
			case "ITL" : currency = "&#8356;";
						break;
			case "JPY" : currency = "&yen;";
						break;
			case "LUF" : currency = "fr";
						break;
			case "MXN" : currency = "MXN$";
						break;
			case "NLG" : currency = "&fnof;";
						break;
			case "NOK" : currency = "kr";
						break;
			case "NZD" : currency = "NZ$";
						break;
			case "SEK" : currency = "kr";
						break;
			case "TRY" : currency = "YTL";
						break;
			case "USD" : currency = "$";
						break;
			case "XXX" : currency = "&euro;";
						break;
			case "ZAR" : currency = "R";
						break;
			default : currency = "&euro;";

		}
	}
	else
	{
		currency = "&euro;";
	}
	return currency;
}