<!--
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
ie4 = true; //Accenture Modification
selectedCol = 0;
selectedSortCol = 0;
selectedValue = "";
window.name = "declaration";
fontface = "\"Arial, Helvetica, sans-serif\"";
selectProfile ="";

function init() { 
	       
	var strHTML = "";     	
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	ie4 = true; //Accenture Modification
	                                          
    if (ns4) {TableArea = document.TableBody; 
              SelectArea = document.ColSelect;}
    if (ie4) {TableArea = TableBody; 
              SelectArea = ColSelectBody;}
    if (ie4) {
		window.status = "Generating table... Please wait...";
	}
		
	initVisible();
	initColPos();
	buildSelect();
    	buildTable();

    if (ie4) {
	window.status = "";
	}        		

}

function initColPos(strColPos) {
	var ColPosCookie, len, i;
	var tempArr;
	
	if (!strColPos) {		
		ColPosCookie = GetCookie(PageName + "ColPos"); }
	else{		
		ColPosCookie = "";}
	
	if ( ColPosCookie != "" ) {
		tempArr = ColPosCookie.split("&");
		len = tempArr.length;
		for (i = 0; i < TableCol.length; i++) {
			if (i < len) {
				ColPos[i] = tempArr[i];
			} else {				
				ColPos[i] = i;
			}
		}	
	} else {
		for (i = 0; i < TableCol.length; i++) {			
			ColPos[i] =  i;			
		}
	}
}

function initVisible( strVisible ) {
	var visibleCookie;
	var i, flag;
	var len;
	
	if (!strVisible) 
		visibleCookie = GetCookie(PageName);
	else
		visibleCookie = strVisible;
		
	writeCookieWithDomain(PageName, visibleCookie, 356);
	
	/*if (visibleCookie.indexOf("0") > 0)
	{
		initColPos("default");
		writeColPosCookie();
		strVisible = Default_Base;
		writeColSelectCookie();
		location.reload();
	}*/
	
	if (PageName == "declaration") {	
		switch (visibleCookie) {
			case Stat_Base :
				selectProfile = "Statistical";
				break;
			case Jockey_Base :
				selectProfile = "Jockey-Focus";
				break;
			case Horse_Base :
				selectProfile = "Horse-Based";
				break;
			case "", Default_Base :
				selectProfile = "Default";
				break;
			default :
				selectProfile = "Customise";
				break;
		}
	} else {
		switch (visibleCookie) {			
			case "", Default_Base :
				selectProfile = "Default";
				break;
			default :
				selectProfile = "Customise";
				break;
		}
	
	}
			
	if ( visibleCookie != "") {
		len = visibleCookie.length;
		for (i = 0 ; i < TableCol.length; i++) {
			if (i < len) {
				flag = visibleCookie.substr(i,1)				
				switch (flag) {
					case "0" :
						TableCol[i][1] = "notshow";
						break;
					case "1" :
						TableCol[i][1] = "hide";
						break;
					case "2" :
						TableCol[i][1] = "show";
						break;
				}				
			}
		}
	}
}

function buildTable() {	
	var tdBgColor = "";
	var strHTML = "";
	var j;
	var count = 0;
	
	PageFullName = "Race Card";
	if (ns4) {
		TableArea.document.open();
		TableArea.document.write("Generating table... Please wait...");
		TableArea.document.close();
	}

	var totalWidth = 0;
	for ( i = 0; i< ColPos.length; i++) {
		Col = TableCol[ColPos[i]];
		if (Col[1] == "show") {
			totalWidth = totalWidth + parseInt(Col[5]);
			count++;
		}
	}
	
	if (selectedValue != "") {
		strHTML = "<a href=\"javascript:doFiltering('', '')\"><font face=" + fontface + " size=\"2\">Show All Records</font></a><br>" ;
	}
	strHTML = strHTML + "<table border='0' width='" + totalWidth + "' class=\"subheader\"><tr><td>";
	strHTML = strHTML + "<table border='0' cellpadding='1' cellspacing='1'> <tr class=\"subheader\">";	
	strHTML = strHTML + "<td colspan = \"" + count + "\"><b>" + "MY " + PageFullName + " LIST </b></td><tr class=\"subsubheader\">";			
	
	for (i = 0; i < ColPos.length; i++ ) {
		Col = TableCol[ColPos[i]];
		if (Col[1]=="show"){
			strHTML = strHTML + "<td valign=\"bottom\" align=\"center\" width=\"" + Col[5] + "\" title=\""+Col[6]+"\" onDragEnter=\"fOnDragOver();\" onDragOver=\"fOnDragOver();\" onDrop=\"fOnDrop();\" ColPosition=\"" + i + "\">" ;
			if (Col[8]=="sort") {
				strHTML = strHTML + "<a href=\"javascript:sortCol('"+Col[0]+"');\" onDragStart=\"fOnDragStart();\" title=\"" + Col[6] + "\"><font face=" + fontface + " size=\"2\" color=\"#000000\"><b><u>" + Col[0] + "</u></b></font></a></td>";		
			} else {
				strHTML = strHTML + "<a href=\"javascript:void(null);\" onDragStart=\"fOnDragStart();\" title=\"" + Col[6] + "\"><font face=" + fontface + " size=\"2\" color=\"#000000\"><b>" + Col[0] + "</b></font></a></td>";
			}
		}
	}
	strHTML = strHTML + "</tr>";
	for ( i = 0; i < SortCol.length; i++ ) {
		Record= Rec[SortCol[i][0]];
		if ( selectedValue == "" || (selectedValue != "" && Record[selectedSortCol] == selectedValue)) {		    		    		
			if ( tdBgColor == "" ) {
				tdBgColor = "#ffffff";
			} else if ( tdBgColor == "#ffffff" ) {
				tdBgColor = "#eeeeee";
			} else if ( tdBgColor == "#eeeeee" ) {
				tdBgColor = "#ffffff";
			}
		
			strHTML = strHTML + "<tr bgcolor=\"" + tdBgColor + "\">";
						
			for ( k = 0; k < Record.length; k++ ) {			
				j = ColPos[k];
				Col = TableCol[j];
				if (PageName == "horse") {
					if ( j == 14 || j==15 || j==16 ) {
						fontColor = "#FF0000";
					} else { fontColor="#000000";}
				} else {
					fontColor = "#000000" ;
				}
				if (Col[1]=="show") {
				    tempval = ( Record[j] != "")?Record[j]:"&nbsp;" ;
				    if ( Col[2] == "filter" ) {			
						strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><a href=\"javascript:doFiltering('" + Col[0] + "','" + Record[j] + "')\"><font face=" + fontface + " size=\"2\" color=\""+ fontColor +"\"><u>" + tempval + "</u></font></a></td>";
				    } else if (Col[4] != "noimage") {
						if ((j == 27) || (j == 28)) 
						{	strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><img src=\"" + getLink(Col[4], Record, j, 4) +"\" border=0 width=32 height=40></td>"; }
						else
						{	strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><img src=\"" + getLink(Col[4], Record, j, 4) +"\" border=0 width=32 height=40></td>"; }
				    } else if (Col[3] != "nolink") {										        
						if ((j == 26) || (j == 27) || (j == 28)) 
						{	strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><a href=\"" + getLink(Col[3], Record, j, 3) +"\"><font face=" + fontface + " size=\"2\">View</font></a></td>"; }
						else
						{
							if (j == 3)
							{
								var strLink = getLink(Col[3], Record, j, 3)
								
								//  2011 PSR : Starter horse point back to JCEW Horse.asp
								//strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><a href=\"" + strLink.replace("Horse.asp?Horseno=" + Record[4], "http://racing.hkjc.com/racing/info/horse/smartform/english/" + Record[4]) +"\"><font face=" + fontface + " size=\"2\">" + Record[j] + "</font></a></td>"; 
								strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><a href=\"" + strLink + "\"><font face=" + fontface + " size=\"2\">" + Record[j] + "</font></a></td>"; 
							}
							else
							{
								strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><a href=\"" + getLink(Col[3], Record, j, 3) +"\"><font face=" + fontface + " size=\"2\">" + Record[j] + "</font></a></td>"; 
							}
						}
				    } else {
						strHTML = strHTML + "<td align=center width=\"" + Col[5] + "\"><font face=" + fontface + " size=\"2\" color=\"" + fontColor +"\">" + tempval + "</font></td>";
					}					
				}
			}			
			strHTML = strHTML + "</tr>";	
		}
	}
	
	strHTML = strHTML + "</table></td></tr></table>";
	
	if (ie4) {
		TableArea.innerHTML = strHTML;
	}

	if (ns4) {
		TableArea.document.open();
		TableArea.document.write(strHTML);
		TableArea.document.close();
	}
}

function sortCol( colname ) {

	if (ie4) {
		window.status = "Generating table... Please wait...";
	}

	for ( i= 0; i < TableCol.length; i++) {
		Col = TableCol[i];
		if ( Col[0] == colname ) {
			break;
		}
	}
	
	selectedCol = i;			
	switch (TableCol[i][7])
	{
	  case "date" :		
	    for (i=0; i < SortCol.length; i++) {
			var strDate = Rec[i][selectedCol];
			if (strDate.substr(6,2) >= 50) {
				strDate = "19" + strDate.substr(6,2) + strDate.substr(3,2) + strDate.substr(0,2);
			} else {
				strDate = "20" + strDate.substr(6,2) + strDate.substr(3,2) + strDate.substr(0,2);
			}
			SortCol[i][0] = i;
			SortCol[i][1] = strDate;
		}
		break;
	  case "number" :
	  	for (i=0; i < SortCol.length; i++) {
			var strNumber = Rec[i][selectedCol];			
			SortCol[i][0] = i;
			SortCol[i][1] = parseInt(strNumber);
		}
		break;
	  default :	
		for (i=0; i < SortCol.length; i++) {	
		    SortCol[i][0] = i;		
			SortCol[i][1] = Rec[i][selectedCol];			
		}
	}
		
	SortCol = SortCol.sort(doCompare);		
	buildTable();	
	if (ie4) {
		window.status = "";
	}
}

function doCompare( a, b) {

	var aPos = 0;
	var bPos = 0;
					
	aKey = a[1];
	bKey = b[1];
	
	if ( aKey < bKey) {
		return -1;
	} else if ( aKey == bKey) {
		return 0;
	} else if ( aKey > bKey) {
		return 1;
	}

}

function colRefresh (theform) {

	if (ie4) {
		window.status = "Generating table... Please wait...";
	}	
	for ( i= 0; i < theform.length; i++) {
		if ( theform.elements[i].type == "checkbox" ) {
			if (theform.elements[i].checked) {				
				TableCol[theform.elements[i].value][1] = "show"	;			
			} else {
				TableCol[theform.elements[i].value][1] = "hide";
			}							
		}
	}	
	writeColSelectCookie();	
	if (ie4) {
		window.status = "";
	}	
}

function writeColSelectCookie() {
	var strCookie = "";
	for ( i=0; i< TableCol.length; i++) {
	   switch (TableCol[i][1]) 
	   {
		 case "show" :
			strCookie = strCookie + "2";
			break;
		 case "hide" :
		    strCookie = strCookie + "1";
			break;
		 case "notshow" :
		    strCookie = strCookie + "0";
			break; 	   
	   }
	}
	writeCookie(PageName, strCookie, 356);
	// fix SETUP MY STARTER LIST doesnt work properly in Firefox (IR275187)
	writeCookieWithDomain(PageName, strCookie, 356);
}



function writeCookie( strKey, strValue, intExpire) { 
    var exp = new Date();
    var nowPlusExpireTime = exp.getTime() + (intExpire*24*60*60*1000);
    exp.setTime(nowPlusExpireTime);	
    document.cookie = strKey+"="+strValue+";expires="+exp.toGMTString();	
}

function writeCookieWithDomain( strKey, strValue, intExpire) { 
    var exp = new Date();
    var nowPlusExpireTime = exp.getTime() + (intExpire*24*60*60*1000);
    exp.setTime(nowPlusExpireTime);	
    document.cookie = strKey+"="+strValue+";expires="+exp.toGMTString()+";domain=hkjc.com";
}

function GetCookie(ps_val)
{
	var li_len=ps_val.length+1;
	var ls_All=document.cookie ;
	var li_Pos=ls_All.indexOf(ps_val + "=");
	var ls_Cookie = "";
	if (li_Pos!=-1)
	{
		var li_Start=li_Pos+li_len;
		var li_End=ls_All.indexOf(";",li_Start);
		if (li_End==-1)
			li_End=ls_All.length;
		var ls_Cookie=ls_All.substring(li_Start,li_End);
		ls_Cookie=unescape(ls_Cookie);		
	} 
	return ls_Cookie;
}



function doFiltering(colname, itemvalue){
	
	if (ie4) {
		window.status = "Generating table... Please wait...";
	}
	
	for ( i = 0; i < TableCol.length; i++) {
		col = TableCol[i];
		if ( col[0] == colname ) { break; }		
	}
	selectedSortCol = i;	
	selectedValue = itemvalue;
	buildTable();
	
	if (ie4) {
		window.status = "";
	}		
}

function getLink ( strtemp, arrayTemp, rowNum, colNum ) {

	var k = 0;
	var str1 = strtemp;
	var intPos1 = 0;
	var intPos2 = 0;
	intPos1 = str1.indexOf("%", intPos1);
	intPos2 = str1.indexOf("%", intPos1+1);	
	

	if (intPos1 != -1) {
		while ( intPos1 != -1 && intPos2 != -1 ) {	
			strVar = str1.substring(intPos1 + 1, intPos2 );			
			for ( k = 0; k < TableCol.length; k++) {
				col = TableCol[k];
				if ( col[10] == strVar ) { break; }
			}			
			str1 = str1.substring(0, intPos1) + chkChar(arrayTemp[k]) + str1.substr(intPos2+1);											
			intPos1 = str1.indexOf("%", intPos2+1);
			intPos2 = str1.indexOf("%", intPos1+1);				
		}
	} else {
		intPos1 = 0;
		intPos2 = 0;
		intPos1 = str1.indexOf("[", intPos1);
		intPos2 = str1.indexOf("]", intPos1+1);
		while ( intPos1 != -1 && intPos2 != -1 ) {	
			strVar = str1.substring(intPos1 + 1, intPos2 );
			//alert("2 " + arrayTemp[strVar]) ;
			str1 = str1.substring(0, intPos1) + chkChar(arrayTemp[strVar]) + str1.substr(intPos2+1);			
			intPos1 = str1.indexOf("%", intPos2+1);
			intPos2 = str1.indexOf("%", intPos1+1);						
		}
	}

	intPos1 = str1.indexOf("%", intPos2);
	intPos2 = str1.indexOf("%", intPos1+1);	
	
	if (intPos1 != -1) {
		while ( intPos1 != -1 && intPos2 != -1 ) {	
			strVar = str1.substring(intPos1 + 1, intPos2 );			
			for ( k = 0; k < TableCol.length; k++) {
				col = TableCol[k];
				if ( col[10] == strVar ) { break; }
			}			
			str1 = str1.substring(0, intPos1) + chkChar(arrayTemp[k]) + str1.substr(intPos2+1);											
			intPos1 = str1.indexOf("%", intPos2+1);
			intPos2 = str1.indexOf("%", intPos1+1);				
		}
	}else {
		intPos1 = 0;
		intPos2 = 0;
		intPos1 = str1.indexOf("[", intPos1);
		intPos2 = str1.indexOf("]", intPos1+1);
		while ( intPos1 != -1 && intPos2 != -1 ) {	
			strVar = str1.substring(intPos1 + 1, intPos2 );
			//alert("4 " + arrayTemp[strVar]) ;
			str1 = str1.substring(0, intPos1) + chkChar(arrayTemp[strVar]) + str1.substr(intPos2+1);			
			intPos1 = str1.indexOf("%", intPos2+1);
			intPos2 = str1.indexOf("%", intPos1+1);						
		}
	} 

	return (str1);

}

function chkChar(strInput)
{
	if ((typeof(strInput) != "undefined") && (strInput != ""))
	{
		strInput = strInput.replace(/\'/g,"\\\'") ;	
		strInput = strInput.replace(/\"/g,"\\\"") ;
	}
	return strInput ;		
}


function chkLinkChar(strInput)
{
	strInput = strInput.replace(/src=\'/g,"\src=\\'") ;	
	strInput = strInput.replace(/\' border/g,"\\' border") ;	
	return strInput ;		
}

//-->

