﻿if (!window.XMLHttpRequest) {
   window.XMLHttpRequest=function (){
    return new ActiveXObject("Microsoft.XMLHTTP");
   }
}

function ajaxLoadPage(url){
    var loader=new XMLHttpRequest;//require Cross-Browser XMLHttpRequest
    loader.open("GET",url,true);
    loader.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    loader.onreadystatechange=function(){
        if (loader.readyState==4){
        	var json = eval('('+loader.responseText+')');
        	htm = procHTML(json.games);
        	document.getElementById("score").innerHTML = htm;
        }
    }
    loader.send(null);
}

function formToRequestString(form_obj){
	var query_string='';
	var and='';
	//alert(form_obj.length);
	for (var i=0;i<form_obj.length ;i++ ){
		e=form_obj[i];
		if (e.name) {
			if (e.type=='select-one') {
				element_value=e.options[e.selectedIndex].value;
			} else if (e.type=='select-multiple') {
				for (var n=0;n<e.length;n++) {
					var op=e.options[n];
					if (op.selected) {
						query_string+=and+e.name+'='+encodeURIComponent(op.value);
						and="&"
					}
				}
				continue;
			} else if (e.type=='checkbox' || e.type=='radio') {
				if (e.checked==false) {   
					continue;   
				}   
				element_value=e.value;
			} else if (typeof e.value != 'undefined') {
				element_value=e.value;
			} else {
				continue;
			}
			query_string+=and+e.name+'='+encodeURIComponent(element_value);
			//alert(query_string);
			and="&"
		}

	}
	return query_string;
}

function procHTML(games){
	txt = '<div>';
	for(var i = 0;i<games.length;i++){
		txt += "<div class='scoreUnit'><span class='tname'>"+games[i].vtm+"</span><span class='score'>"+games[i].vtmScore+"</span><br/>";
		txt += "<span class='tname'>"+games[i].htm+"</span><span class='score'>"+games[i].htmScore+"</span><br/>";
		txt += "<span class='gs'>"+games[i].gs+"</span></div>";
	}
	txt += '</div>';
	//alert(txt);
	return txt;
}

function getDateForcache(){
	var now = new Date();
	var year = now.getYear();
	var month = now.getMonth()+'';
	var day = now.getDate()+'';
	var hour = now.getHours()+'';
	var minute = now.getMinutes()+'';
	var second = now.getSeconds()+'';
	//alert(month+day+hour+minute+second);
	return month+day+hour+minute+second;
}

function intervalFunc(){
	var now = new Date();
	var year = now.getYear();
	var month = now.getMonth()+1;
	var day = now.getDate();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds(); 
	ajaxLoadPage("http://www2.dormforce.net/nba/getScore.php?"+getDateForcache());
}

ajaxLoadPage("http://www2.dormforce.net/nba/getScore.php?"+getDateForcache());

setInterval(intervalFunc,20000);