// A simple AJAX application
// Coded By Özgür Kaya 2007
var xmlHttp = createXmlHttpRequestObject();
var code = 0;
function createXmlHttpRequestObject() {
	var xmlHttp;
	if(window.ActiveXObject){
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	else {
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}

	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else 
		return xmlHttp;
}
//---------------------------------
function showFixture(action) {
    rnd = get_random();
    if (action == "default") {
        fixture_no = 1;
        url = "/euro2008/_ajax/fixture.php?rnd=" + rnd;
    }
    else if (action == "prev") {
        fixture_no--;
        url = "/euro2008/_ajax/fixture.php?no=" + fixture_no + "&rnd=" + rnd ;
    }
    else {
        fixture_no++;
        url = "/euro2008/_ajax/fixture.php?no=" + fixture_no + "&rnd=" + rnd;
    }
    //alert(url);
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = getFixtureRecord;
    xmlHttp.send(null);
}
//------------------------------------------------------------------------------
function getFixtureRecord() {
  	if (xmlHttp.readyState == 4) {
    	if (xmlHttp.status == 200) {
            //alert(prefix);
            xmlResponse = xmlHttp.responseXML;
  		    xmlDocumentElement = xmlResponse.documentElement;
            var matches = xmlResponse.getElementsByTagName('match');
            for (var i=0;i<matches.length;i++){
                if (i == 0) {
                    fixture_no = getNodeValue(matches[i],'id');
                }
                type = getNodeValue(matches[i],'finished');
                isLive = getNodeValue(matches[i],'isLive');
                img = getNodeValue(matches[i],'stadium_img');
                date = getNodeValue(matches[i],'date');
                t1ID = getNodeValue(matches[i],'team1id');
                t2ID = getNodeValue(matches[i],'team2id');
                t1 = getNodeValue(matches[i],'team1');
                sid = getNodeValue(matches[i],'stadiumid');
                t2 = getNodeValue(matches[i],'team2');
                t1link = "/euro2008/takimlar.php?takim_id=" + t1ID;
                t2link = "/euro2008/takimlar.php?takim_id=" + t2ID;
                //##
                news_p = getNodeValue(matches[i],'news_p');
                news_a = getNodeValue(matches[i],'news_a');
                video = getNodeValue(matches[i],'video');
                gallery = getNodeValue(matches[i],'gallery');
                live = getNodeValue(matches[i],'live');
                news_p_link = (news_p > 0) ? '/euro2008/haber/' + news_p + '.php' : '#';
                news_a_link = (news_a > 0) ? '/euro2008/haber/' + news_a + '.php' : '#';
                video_link = (video > 0) ? '/euro2008/video.php?videoID=' + video : '#';
                gallery_link = (gallery > 0) ? '/euro2008/galeri.php?galleryID=' + gallery : '#';
                live_link = (live > 0) ? '/detail.php?Type=canli' : '#';
                if (links_info) {
                    document.getElementById('col' + (i + 1) + '_2').href = news_p_link;
                    document.getElementById('col' + (i + 1) + '_5').href = news_a_link;
                    //document.getElementById('col' + (i + 1) + '_4').href = live_link;
                    document.getElementById('col' + (i + 1) + '_6').href = video_link;
                    document.getElementById('col' + (i + 1) + '_7').href = gallery_link;
                }
                //##
                if (isLive == 'true' && type == 'false')
                    live_text = "<a href='/detail.php?Type=canli'><font style='font-weight:bold;color:#D65500;'>CANLI</font></a>";
                else
                    live_text = "";

                if (type == 'true' || isLive == 'true') {
                    score1 = getNodeValue(matches[i],'score1');
                    score2 = getNodeValue(matches[i],'score2');
                    td = "       <td height=\"40\" align=\"center\" colspan=\"3\" valign=\"bottom\" style=\"padding-bottom:5px;background:url(/euro2008/i/skor_bck.gif) bottom center no-repeat;color:#08D700;font-weight:bold;font-size:14px;\">" + score1 + " - " + score2 + "</td>";
                }
                else
                    td = "       <td height=\"40\" align=\"center\" colspan=\"3\"><a href=\"/euro2008/statlar.php?stat_id=" + sid + "\">  <img width=116 height=40 src=\"" + img + "\" style=\"border:1px solid #fff;\"></a></td>";
                html =
                "<table width=\"133\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">" +
                "    <tr>" +
                "       <td height=\"10\" colspan=\"3\" bgcolor=\"#6B6D6B\" style=\"color:#fff;font-size:10px\" align=\"center\">" + date + "</td>" +
                "    </tr>" +
                "    <tr>" +
                "       <td height=\"10\" colspan=\"3\" align=\"center\">" + live_text + "</td>" +
                "    </tr>" +
                "    <tr>" +
                td +
                "    </tr>" +
                "    <tr>" +
                "       <td height=\"10\" colspan=\"3\"></td>" +
                "    </tr>" +
                "    <tr>" +
                "        <td width=\"48%\" align=\"right\" height=\"20\"><a href=\"" + t1link + "\"><img src=\"/i/euro2008/bayraklar/new/" + t1ID + ".gif\" border=\"0\"></a></td>" +
                "        <td width=\"4%\"></td>" +
                "        <td width=\"48%\" align=\"left\"><a href=\"" + t2link + "\"><img src=\"/i/euro2008/bayraklar/new/" + t2ID + ".gif\"  border=\"0\"></a></td>" +
                "    </tr>" +
                "    <tr>" +
                "        <td align=\"right\" height=\"20\" style=\"font-weight:bold;color:#525952;font-size:12px;\">" + t1 + "</td>" +
                "        <td></td>" +
                "        <td align=\"left\" style=\"font-weight:bold;color:#525952;font-size:12px;\">" + t2 + "</td>" +
                "    </tr>" +
                "</table>";
                if (type == '')
                    html = '';
                document.getElementById('col' + (i + 1)).innerHTML = html;
                //
                record_id = getNodeValue(matches[i],'id');
                if (record_id == '') {
                    document.getElementById('tabfixture' + (i+1)).style.display = 'none';
                    document.getElementById('col1').style.width = '98%';
                    document.getElementById('col1').style.textAlign = 'center';
                    document.getElementById('col2').style.width = '1%';

                    document.getElementById('tfix_td1').style.width = '99%';
                    document.getElementById('tfix_td1').style.textAlign = 'center';
                    document.getElementById('tfix_td2').style.width = '1%';

                    continue;
                }
                document.getElementById('tabfixture' + (i+1)).style.display = 'block';
                //
            }
    	}
    	else {
      		alert("There was a problem accessing the server: " + xmlHttp.statusText);
    	}
  	}
}
//------------------------------------------------------------------------------
function getNodeValue(obj,tag){
    if (obj.getElementsByTagName(tag)[0].firstChild == null){
        return "";
    }
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
//------------------------------------------------------------------------------
function getTabContent(box_id,tab_id) {
    url = "ajax/ajax_tabs.php?boxID=" + box_id + "&tabID=" + tab_id;
    document.getElementById("tab_content_" + box_id + "_" + tab_id).innerHTML = getLoadingHTML();
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
}

function handleServerResponse() {
  	if (xmlHttp.readyState == 4) {
    	if (xmlHttp.status == 200) {
            xmlResponse = xmlHttp.responseXML;
  		    xmlDocumentElement = xmlResponse.documentElement;
            box_id = xmlDocumentElement.childNodes[0].firstChild.data;
            tab_id = xmlDocumentElement.childNodes[1].firstChild.data;
      		pageMessage = xmlDocumentElement.childNodes[2].firstChild.data;
			document.getElementById("tab_content_" + box_id + '_' + tab_id ).innerHTML=pageMessage;
    	}
    	else {
      		alert("There was a problem accessing the server: " + xmlHttp.statusText);
    	}
  	}
}

function getLoadingHTML() {
    html = "<div style='position:relative;height:130;background-color:#FAFAFA;text-align:center'>";
    html += "<div style='position:absolute;top:57px;left:198px;'><img src='images/ajax-loader.gif'></div>";
    html += "</div>";

    return html;
}

function get_random() {
    var ranNum= Math.floor(Math.random()*50);
    return ranNum;
}