// JScript File
 // ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};


function FormatDateString(dateStr) {
    var dateEd = new Date();
    //var longdow = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday"];
  //  var longdow = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
    //order of day
    //          0    1     2    3     4     5      6    7      8    9     10    11    12    13    14    15    16    17    18    19    20    21    22    23    24    25    26    27    28    29    30    31
//    var ood = ["", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];
    
    var shortMon = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    
    dateEd.setFullYear( parseInt(dateStr.substr(0,4),10));
    dateEd.setMonth( parseInt(dateStr.substr(5,2), 10)-1);
    dateEd.setDate( parseInt(dateStr.substr(8,2),10));
    
    var result = new String();
    result = shortMon[dateEd.getMonth()] + ' ' + dateEd.getFullYear();
      
    return result;
}

function renderFormGuideStatsTable(data, stat1, stat2) {
//alert('generate guide');
    var strtable ='';
    strtable  = '<table class="guideresult" cellpadding="0" cellspacing="0"><tr class="table_header"><td width="30%">&nbsp;</td>'
    strtable += '<td width="35%"><b>' + stat1.type + '</b></td>'
    strtable += '<td width="35%"><b>' + stat2.type + '</b></td></tr>'
    strtable += '<tr class="bold_row alt_row"><td>Played</td>'
    strtable += '<td>' + stat1.Played + '</td>'
    strtable += '<td>' + stat2.Played + '</td></tr>'
    strtable += '<tr><td><b>' + data.FormGuide.Header.HomeTeam.ShortName + ' won</b></td>'
    strtable += '<td><b>' + stat1.HomeWon + '</b> (' + stat1.HomwWinRatio  + '%)</td>'
    strtable += '<td><b>' + stat2.HomeWon + '</b> (' + stat2.HomwWinRatio  + '%)</td></tr>'
    strtable += '<tr class="alt_row"><td><b>' + data.FormGuide.Header.AwayTeam.ShortName + ' won</b></td>'
    strtable += '<td><b>' + stat1.AwayWon + '</b> (' + stat1.AwayWinRatio  + '%)</td>'
    strtable += '<td><b>' + stat2.AwayWon + '</b> (' + stat2.AwayWinRatio  + '%)</td></tr>'
    strtable += '<tr><td><b>Drawn tied</b></td>'
    strtable += '<td><b>' + stat1.Drawn + '</b> (' + stat1.DrawnRatio + '%)</td>'
    strtable += '<td><b>' + stat2.Drawn + '</b> (' + stat2.DrawnRatio + '%)</td></tr>'
    strtable += '<tr class="alt_row"><td colspan="3"><b>' + data.FormGuide.Header.HomeTeam.ShortName + '</b></td></tr>'
    strtable += '<tr class="alt_row"><td>Highest score</td>'
    strtable += '<td><b>' + stat1.Scores.Score[0].Team[0].Score + '</b> ' + stat1.Scores.Score[0].Team[0].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[0].Team[0].Date) + '</td>'    
    strtable += '<td><b>' + stat2.Scores.Score[0].Team[0].Score + '</b> ' + stat2.Scores.Score[0].Team[0].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[0].Team[0].Date) + '</td><tr>'
    strtable += '<tr class="hl_row"><td>Lowest score</td>'
    strtable += '<td><b>' + stat1.Scores.Score[1].Team[0].Score + '</b> ' + stat1.Scores.Score[1].Team[0].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[1].Team[0].Date) + '</td>'
    strtable += '<td><b>' + stat2.Scores.Score[1].Team[0].Score + '</b> ' + stat2.Scores.Score[1].Team[0].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[1].Team[0].Date) + '</td></tr>'
    strtable += '<tr><td colspan="3"><b>' + data.FormGuide.Header.AwayTeam.ShortName + '</b></td></tr>'
    strtable += '<tr><td>Highest score</td>'
    strtable += '<td><b>' + stat1.Scores.Score[0].Team[1].Score + '</b> ' + stat1.Scores.Score[0].Team[1].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[0].Team[1].Date) + '</td>'    
    strtable += '<td><b>' + stat2.Scores.Score[0].Team[1].Score + '</b> ' + stat2.Scores.Score[0].Team[1].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[0].Team[1].Date) + '</td><tr>'
    strtable += '<tr class="grey_row"><td>Lowest score</td>'
    strtable += '<td><b>' + stat1.Scores.Score[1].Team[1].Score + '</b> ' + stat1.Scores.Score[1].Team[1].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[1].Team[1].Date) + '</td>'
    strtable += '<td><b>' + stat2.Scores.Score[1].Team[1].Score + '</b> ' + stat2.Scores.Score[1].Team[1].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[1].Team[1].Date) + '</td></tr></table>'
    return strtable;
}
 
function generateMatchDetails(data) {
    //for redirection
    if (data) {
        var teams = '';
        //alert('render match');
        teams = data.FormGuide.Header.HomeTeam.LongName.toUpperCase() + ' v ' + data.FormGuide.Header.AwayTeam.LongName.toUpperCase() + ' - ' + data.FormGuide.Header.Series.toUpperCase() ;
        $('#match_team1').html(teams);
//        $('#formguide_container').html(renderFormGuideStatsTable(data, data.FormGuide.Statistics.Stats[1] , data.FormGuide.Statistics.Stats[2]));
//        $('#formguide_container').append(renderFormGuideStatsTable(data, data.FormGuide.Statistics.Stats[0] , data.FormGuide.Statistics.Stats[3]));
        
        $('#formguide_container').html(generateTeamsFormGuideTable(data, data.FormGuide.Statistics.Stats[1] , data.FormGuide.Statistics.Stats[2]));
        $('#formguide_container').append(generateTeamsFormGuideTable(data, data.FormGuide.Statistics.Stats[0] , data.FormGuide.Statistics.Stats[3]));
        setHeight();
    }
    else {
        $('#formguide_container').html('<span class="no_result">No form guide information found</span>');
    }

}

function generateTeamsFormGuideTable(data, stat1, stat2) {
    //alert('msg');
    var strtable ='';
    strtable  = '<table class="guideresult" cellpadding="0" cellspacing="0"><tr class="table_header"><td width="30%">&nbsp;</td>'
    strtable += '<td width="35%"><b>' + stat1.type + '</b></td>'
    if (stat2==null)
        strtable += '<td width="35%"><b>&nbsp;</b></td></tr>'
    else
        strtable += '<td width="35%"><b>' + stat2.type + '</b></td></tr>'
    strtable += '<tr class="bold_row alt_row"><td>Played</td>'
    strtable += '<td>' + stat1.Played + '</td>'
    if (stat2==null)
        strtable += '<td>&nbsp;</td></tr>'
    else
        strtable += '<td>' + stat2.Played + '</td></tr>'
    
    if (data.FormGuide.Header.Team1 != null)
        strtable += '<tr><td><b>' + data.FormGuide.Header.Team1.ShortName + ' won</b></td>'
    else
        strtable += '<tr><td><b>' + data.FormGuide.Header.HomeTeam.ShortName + ' won</b></td>'
        
    strtable += '<td><b>' + stat1.HomeWon + '</b> (' + stat1.HomwWinRatio  + '%)</td>'
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td></tr>'
    else
        strtable += '<td><b>' + stat2.HomeWon + '</b> (' + stat2.HomwWinRatio  + '%)</td></tr>'
    
    if (data.FormGuide.Header.Team2 != null)
        strtable += '<tr class="alt_row"><td><b>' + data.FormGuide.Header.Team2.ShortName + ' won</b></td>'
    else
        strtable += '<tr class="alt_row"><td><b>' + data.FormGuide.Header.AwayTeam.ShortName + ' won</b></td>'
    
    strtable += '<td><b>' + stat1.AwayWon + '</b> (' + stat1.AwayWinRatio  + '%)</td>'
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td></tr>'
    else
        strtable += '<td><b>' + stat2.AwayWon + '</b> (' + stat2.AwayWinRatio  + '%)</td></tr>'
        
    strtable += '<tr><td><b>Drawn tied</b></td>'
    strtable += '<td><b>' + stat1.Drawn + '</b> (' + stat1.DrawnRatio + '%)</td>'
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td></tr>'
    else
        strtable += '<td><b>' + stat2.Drawn + '</b> (' + stat2.DrawnRatio + '%)</td></tr>'
    
    if (data.FormGuide.Header.Team1 != null)
        strtable += '<tr class="alt_row"><td colspan="3"><b>' + data.FormGuide.Header.Team1.ShortName + '</b></td></tr>'
    else
        strtable += '<tr class="alt_row"><td colspan="3"><b>' + data.FormGuide.Header.HomeTeam.ShortName + '</b></td></tr>'
    
    strtable += '<tr class="alt_row"><td>Highest score</td>'
    if (stat1.Scores.Score[0].Team[0].Match.Name!=null)
        strtable += '<td><b>' + stat1.Scores.Score[0].Team[0].Score + '</b> ' + stat1.Scores.Score[0].Team[0].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[0].Team[0].Date) + '</td>'    
    else if (stat1.Scores.Score[0].Team[0].Score!=null)
        strtable += '<td><b>' + stat1.Scores.Score[0].Team[0].Score + '</b> ' + FormatDateString(stat1.Scores.Score[0].Team[0].Date) + '</td>'    
    else
        strtable += '<td><b> - </b></td>'    
        
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td><tr>'
    else if (stat2.Scores.Score[0].Team[0].Match.Name!=null)
        strtable += '<td><b>' + stat2.Scores.Score[0].Team[0].Score + '</b> ' + stat2.Scores.Score[0].Team[0].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[0].Team[0].Date) + '</td><tr>'
    else if (stat2.Scores.Score[0].Team[0].Score!=null)
        strtable += '<td><b>' + stat2.Scores.Score[0].Team[0].Score + '</b> ' + FormatDateString(stat2.Scores.Score[0].Team[0].Date) + '</td><tr>'        
    else
        strtable += '<td><b> - </b></td><tr>'        
    
    strtable += '<tr class="hl_row"><td>Lowest score</td>'
    if (stat1.Scores.Score[1].Team[0].Match.Name!=null)
        strtable += '<td><b>' + stat1.Scores.Score[1].Team[0].Score + '</b> ' + stat1.Scores.Score[1].Team[0].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[1].Team[0].Date) + '</td>'
    else if (stat1.Scores.Score[1].Team[0].Score!=null)
        strtable += '<td><b>' + stat1.Scores.Score[1].Team[0].Score + '</b> ' + FormatDateString(stat1.Scores.Score[1].Team[0].Date) + '</td>'
    else
        strtable += '<td><b> - </b></td>'
        
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td></tr>'
    else if (stat2.Scores.Score[1].Team[0].Match.Name!=null)
        strtable += '<td><b>' + stat2.Scores.Score[1].Team[0].Score + '</b> ' + stat2.Scores.Score[1].Team[0].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[1].Team[0].Date) + '</td></tr>'
    else if (stat2.Scores.Score[1].Team[0].Score!=null)
        strtable += '<td><b>' + stat2.Scores.Score[1].Team[0].Score + '</b> ' + FormatDateString(stat2.Scores.Score[1].Team[0].Date) + '</td></tr>'
    else
        strtable += '<td><b> - </b></td></tr>'
        
    if (data.FormGuide.Header.Team2 != null)
        strtable += '<tr><td colspan="3"><b>' + data.FormGuide.Header.Team2.ShortName + '</b></td></tr>'
    else
        strtable += '<tr><td colspan="3"><b>' + data.FormGuide.Header.AwayTeam.ShortName + '</b></td></tr>'
    
    strtable += '<tr><td>Highest score</td>'
    if (stat1.Scores.Score[0].Team[1].Match.Name!=null)
        strtable += '<td><b>' + stat1.Scores.Score[0].Team[1].Score + '</b> ' + stat1.Scores.Score[0].Team[1].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[0].Team[1].Date) + '</td>'    
    else if (stat1.Scores.Score[0].Team[1].Score!=null)
        strtable += '<td><b>' + stat1.Scores.Score[0].Team[1].Score + '</b> ' + FormatDateString(stat1.Scores.Score[0].Team[1].Date) + '</td>'    
    else
        strtable += '<td><b> - </b></td>'    
        
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td><tr>'
    else if (stat2.Scores.Score[0].Team[1].Match.Name!=null)
        strtable += '<td><b>' + stat2.Scores.Score[0].Team[1].Score + '</b> ' + stat2.Scores.Score[0].Team[1].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[0].Team[1].Date) + '</td><tr>'
    else if (stat2.Scores.Score[0].Team[1].Score!=null) 
        strtable += '<td><b>' + stat2.Scores.Score[0].Team[1].Score + '</b> ' + FormatDateString(stat2.Scores.Score[0].Team[1].Date) + '</td><tr>'
    else
        strtable += '<td><b> - </b></td><tr>'
        
    strtable += '<tr class="grey_row"><td>Lowest score</td>'
    if (stat1.Scores.Score[1].Team[1].Match.Name!=null)
        strtable += '<td><b>' + stat1.Scores.Score[1].Team[1].Score + '</b> ' + stat1.Scores.Score[1].Team[1].Match.Name + ', ' + FormatDateString(stat1.Scores.Score[1].Team[1].Date) + '</td>'
    else if (stat1.Scores.Score[1].Team[1].Score!=null)
        strtable += '<td><b>' + stat1.Scores.Score[1].Team[1].Score + '</b> ' + FormatDateString(stat1.Scores.Score[1].Team[1].Date) + '</td>'
    else
        strtable += '<td><b> - </b></td>'
    
    if (stat2==null)
        strtable += '<td><b>&nbsp;</b></td></tr></table>'
    else if (stat2.Scores.Score[1].Team[1].Match.Name!=null )
        strtable += '<td><b>' + stat2.Scores.Score[1].Team[1].Score + '</b> ' + stat2.Scores.Score[1].Team[1].Match.Name + ', ' + FormatDateString(stat2.Scores.Score[1].Team[1].Date) + '</td></tr></table>'
    else if (stat2.Scores.Score[1].Team[1].Score!=null)
        strtable += '<td><b>' + stat2.Scores.Score[1].Team[1].Score + '</b> ' + FormatDateString(stat2.Scores.Score[1].Team[1].Date) + '</td></tr></table>'
    else
        strtable += '<td><b> - </b></td></tr></table>'
    return strtable;

}

function generateTeamsDetails(data) {
    //for user request
    if (data) {
    var teams = '';
    teams = data.FormGuide.Header.Team1.LongName.toUpperCase() + ' v ' + data.FormGuide.Header.Team2.LongName.toUpperCase(); // + ' - ' + data.FormGuide.Header.MatchType ;
        $('#match_team1').html(teams);
        $('#formguide_container').html(generateTeamsFormGuideTable(data, data.FormGuide.Statistics.Stats[1] , data.FormGuide.Statistics.Stats[2]));
        $('#formguide_container').append(generateTeamsFormGuideTable(data, data.FormGuide.Statistics.Stats[0] , null));
        setHeight();
    } else {
        $('#formguide_container').html('<span class="no_result">No form guide information found</span>');
    }
}



