// 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 showFixtureOld(type, action) {
    if (action == "default") {
        fixture_no = 1;
        url = "/_ajax/dinamik_fikstur.php?type=" + type;
    }
    else if (action == "prev") {
        fixture_no--;
        url = "/_ajax/dinamik_fikstur.php?no=" + fixture_no + "&type=" + type ;
    }
    else {
        fixture_no++;
        url = "/_ajax/dinamik_fikstur.php?no=" + fixture_no + "&type=" + type;
    }
    //alert(url);
    fixture_type = type;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = getFixtureRecord;
    xmlHttp.send(null);
}
//------------------------------------------------------------------------------

function showFixture(type, action) {
    if (action == "default") {
        fixture_no = 1;
        url = "/_ajax/dinamik_fikstur.php?type=" + type;
    }
    else if (action == "prev") {
        fixture_no--;
        url = "/_ajax/dinamik_fikstur.php?no=" + fixture_no + "&type=" + type ;
    }
    else {
        fixture_no++;
        url = "/_ajax/dinamik_fikstur.php?no=" + fixture_no + "&type=" + type;
    }
    fixture_type = type;
    var link = url;
    var data_type = 'xml';
    if ($.browser.safari) {
        data_type = 'xml/text';
    }
    $.ajax({
		type: 'GET',
		url: link,
		dataType: data_type,
		success: function(ajaxData) {
		    //alert(ajaxData);
			if (fixture_type == 'F')
                prefix = 'f';
            else if (fixture_type == 'BM')
                prefix = 'b'
            else if (fixture_type == 'BF')
                prefix = 'b3'
            else if (fixture_type == 'VM')
                prefix = 'v'
            else if (fixture_type == 'VF')
                prefix = 'v3'
            else
                prefix = 'o';
            //xmlResponse = ajaxData;
            $(ajaxData).find('match').each(function(){
                //alert($(this).find('id').text());
                if (prefix == 'o') {
                    document.getElementById(prefix + "_tarih").innerHTML = $(this).find('date').text();
                    document.getElementById(prefix + '_bilgi').innerHTML = $(this).find('data').text();
                } else {
                    fixture_no = $(this).find('id').text();
                    o_logo = $(this).find('o_logo').text();
                    g_logo = $(this).find('g_logo').text();
                    document.getElementById(prefix + "_img1").innerHTML = "<img width=38 height=38 vspace='4' border='0' src='/images/fikstur/" + o_logo + "'>";
                    document.getElementById(prefix + '_img2').innerHTML = "<img width=38 height=38 vspace='4' border='0' src='/images/fikstur/" + g_logo + "'>";
                    
                    document.getElementById(prefix + '_hakem').innerHTML = $(this).find('referee').text();
                    document.getElementById(prefix + '_yer').innerHTML = $(this).find('stadium').text();
                    document.getElementById(prefix + '_skor1').innerHTML = '&nbsp;&nbsp;<b>' + $(this).find('score1').text() + '</b>';
                    document.getElementById(prefix + '_skor2').innerHTML = '&nbsp;&nbsp;<b>' + $(this).find('score2').text() + '</b>';
                    document.getElementById(prefix + '_tarih').innerHTML = $(this).find('date').text();
                    document.getElementById(prefix + '_ligadi').innerHTML = $(this).find('league').text();
                    document.getElementById(prefix + '_hafta').innerHTML = $(this).find('week').text();
                    document.getElementById(prefix + '_takim1').innerHTML = $(this).find('owner').text();
                    document.getElementById(prefix + '_takim2').innerHTML = $(this).find('guest').text();
                    
                }
            });
		}
	});
}
//------------------------------------------------------------------------------
function getFixtureRecord() {
  	if (xmlHttp.readyState == 4) {
    	if (xmlHttp.status == 200) {
            if (fixture_type == 'F')
                prefix = 'f';
            else if (fixture_type == 'BM')
                prefix = 'b'
            else if (fixture_type == 'BF')
                prefix = 'b3'
            else if (fixture_type == 'VM')
                prefix = 'v'
            else if (fixture_type == 'VF')
                prefix = 'v3'
            else
                prefix = 'o';
            //alert(prefix);
            xmlResponse = xmlHttp.responseXML;
  		    xmlDocumentElement = xmlResponse.documentElement;
            var matches = xmlResponse.getElementsByTagName('match');
            for (var i=0;i<matches.length;i++){
                if (prefix == 'o') {
                    document.getElementById(prefix + "_tarih").innerHTML = getNodeValue(matches[i],'date');
                    document.getElementById(prefix + '_bilgi').innerHTML = getNodeValue(matches[i],'data');
                } else {
                    fixture_no = getNodeValue(matches[i],'id');
                    o_logo = getNodeValue(matches[i],'o_logo');
                    g_logo = getNodeValue(matches[i],'g_logo');
                    document.getElementById(prefix + "_img1").innerHTML = "<img width=38 height=38 vspace='4' border='0' src='/images/fikstur/" + o_logo + "'>";
                    document.getElementById(prefix + '_img2').innerHTML = "<img width=38 height=38 vspace='4' border='0' src='/images/fikstur/" + g_logo + "'>";
                    document.getElementById(prefix + '_hakem').innerHTML = getNodeValue(matches[i],'referee');
                    document.getElementById(prefix + '_yer').innerHTML = getNodeValue(matches[i],'stadium');
                    document.getElementById(prefix + '_skor1').innerHTML = '&nbsp;&nbsp;<b>' + getNodeValue(matches[i],'score1') + '</b>';
                    document.getElementById(prefix + '_skor2').innerHTML = '&nbsp;&nbsp;<b>' + getNodeValue(matches[i],'score2') + '</b>';
                    document.getElementById(prefix + '_tarih').innerHTML = getNodeValue(matches[i],'date');
                    document.getElementById(prefix + '_ligadi').innerHTML = getNodeValue(matches[i],'league');
                    document.getElementById(prefix + '_hafta').innerHTML = getNodeValue(matches[i],'week');
                    document.getElementById(prefix + '_takim1').innerHTML = getNodeValue(matches[i],'owner');
                    document.getElementById(prefix + '_takim2').innerHTML = getNodeValue(matches[i],'guest');
                }
            }
    	}
    	else {
      		//alert("There was a problem accessing the server: " + xmlHttp.statusText);
    	}
  	}
}
//------------------------------------------------------------------------------
function showMatchProgram(type) {
    url = "/_ajax/dinamik_fikstur_macprogram3.php?type=" + type;

    match_program_type = type;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = getProgramRecord;
    xmlHttp.send(null);
}
//------------------------------------------------------------------------------
function getProgramRecord() {
  	if (xmlHttp.readyState == 4) {
    	if (xmlHttp.status == 200) {
            if (match_program_type == 'F')
                prefix = 'f';
            else if (match_program_type == 'BF' || match_program_type == 'BM')
                prefix = 'b'
            else if (match_program_type == 'VF' || match_program_type == 'VM')
                prefix = 'v'
            else
                prefix = 'o';
            xmlResponse = xmlHttp.responseXML;
  		    xmlDocumentElement = xmlResponse.documentElement;
            var matches = xmlResponse.getElementsByTagName('match');
            var match_program = 'TEST';
            //alert(matches.length);
            for (var i=0;i<matches.length;i++){
                match_program = getNodeValue(matches[i],'data');
                //alert(match_program);
            }
            document.getElementById(prefix + "_program").innerHTML = match_program;
    	}
    	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;
}
