var ie  = document.all ? 1 : 0;
var ns4 = document.layers ? 1 : 0;
var dom = document.getElementById ? 1 : 0;

function getDiv(d,str){
    return dom ? d.getElementById(str) : ie ? d.all[str] : d.layers[str];
}



var db1Temp = null;
var db2Temp = null;
var tempColor1 = [];

var pageCount = 1;




var loadTime = 0;

var pages = {};
var pagesTime = [];
for(var i = 1;i <= pageCount ; i++){
    pages[i] = 0;
}


var intervalId1 = 0;
var intervalId2 = 0;
var intervalId3 = 0;

var flash = false;


var activePage = 0;
var milkPathTime = 0;



var enMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var trMonths = ["Oca", "Sbt", "Mar", "Nis", "May", "Haz","Tem", "Aug", "Eyl", "Eki", "Kas", "Ara"];

var months = enMonths;


var tzFark = 0;


var loadStat = 0;



function fnAjax(){

	var xmlHttp = null;
	try {    // Firefox, Opera 8.0+, Safari    
		xmlHttp = new XMLHttpRequest();    
	}
	catch (e) {    // Internet Explorer    
		try {      
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
    	catch (e) {      
    		try {        
    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");        
    		}
      		catch (e) {        
      			alert("Your browser does not support AJAX!");        
      			return null;        
      		}      
      	}    
	}

	return xmlHttp;
}

function getDataPost(page,sendStr){
	
	var xmlHttp = fnAjax();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 302){
				var newLocation = xmlHttp.getResponseHeader("Location");
				xmlHttp.open("POST", newLocation, true);
				xmlHttp.send(null);	
			}				
			else if(xmlHttp.status == 200){
				var str = xmlHttp.responseText;
				eval(str);
			}
		}
	}	
	xmlHttp.open("POST", page, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.setRequestHeader("Cache-Control", "no-cache");
	xmlHttp.setRequestHeader("If-Modified-Since", "Wed, 31 Dec 1980 00:00:00 GMT");
	xmlHttp.setRequestHeader("Expires", "Wed, 31 Dec 1980 00:00:00 GMT");
	xmlHttp.send(sendStr);	

}

function getDataGet(page){
	var xmlHttp = fnAjax();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4) {
			
			if(xmlHttp.status == 302){
				var newLocation = xmlHttp.getResponseHeader("Location");
				xmlHttp.open("GET", newLocation, true);
				xmlHttp.send(null);	
			}			
			else if(xmlHttp.status == 200){
				var str = xmlHttp.responseText;
				
				//alert(page+"\n"+str);
				eval(str);
			}

			
		}
	}	
	xmlHttp.open("GET", page, true);
	xmlHttp.send(null);	
}

function fnLatinToUcs2(str){
	var Latin1ToUcs2 = {0xC7:0x00C7,0xD0:0x011E,0xD6:0x00D6,0xDC:0x00DC,0xDD:0x0130,0xDE:0x015E,0xE7:0x00E7,0xF0:0x011F,0xFC:0x00FC,0xFD:0x0131,0xFE:0x015F,0xF6:0x00F6};
	var ret = "";
	var ch = "";
	for(i = 0;i < str.length;i++){
		var chCode = str.charCodeAt(i);
		
		if(Latin1ToUcs2[chCode] != null){
			ch = String.fromCharCode(Latin1ToUcs2[chCode]);
			
		}
		else {
			ch = str.charAt(i);	
		}
		ret += ch;
		
	}
	
	return ret;
}

/*#####################################################################################################*/

function onBegin(pMilkPath){
    milkPath = pMilkPath;
    
}
function fnOnLoad(){
		
	var obj = getDiv(document,"page0"); 
	obj.style.visibility = 'visible';//sonra sil	

	dataLoad();
  
    
}

function resetColor1(){

    if(tempColor1 == null) return;

    var val = tempColor1;
    var time1 = (new Date()).getTime();

    for(var i in val){

        if(val[i] != null){


            if(time1 - val[i] > 3000){
                val[i] = null;

                var div = getDiv(document,i);
                if(div != null){
                    div.style.color = '#FFFFFF';
                    div.style.backgroundColor = '#000000';



                }

            }
        }
    }

}

function onMilkPath(f){
    milkPath = f;
    window.frames[0].location = milkRoot + milkPath;
    
}
function fnIntervalRealTime(){
    var time1 = (new Date()).getTime();
    
    if((time1 - loadTime) > 30000) {
                
        milkServer();
    }
}
function pb(f){
    flash = f;
};

function onSendTime(){
    var time1 = (new Date()).getTime();
    loadTime = time1;

};
function milkServer(){
	
	
	
    var time1 = (new Date()).getTime();
    if(((milkPath.length == 0)) || ((time1 - milkPathTime) > 864E5)){//864E5
        milkPathTime = time1;
        milkPath = "";
        getDataGet(root+"index.php?ms=2&ln="+lang+"&sid="+sid+"&rand="+(new Date).getTime());
    }
    else {
    	
        window.frames[0].location = milkRoot + milkPath;
       
    }
}

function dataLoad(){
	
	getDataGet(root+"index.php?ms=1&ln="+lang+"&sid="+sid+"&rand="+(new Date()).getTime());
}




function dataLoaded2(val){
    
}

function dataLoaded(val){
    
    
    if(val == null) return;
    var time1 = (new Date()).getTime();
    var time2 = new Date();
    var tp = 0;


    for(var i in val){
        var v = val[i];
        tp = 0;
        if(tzFark != 0){
            if(v.indexOf(":") >= 0){
                vTime = v.split(":");
                if(vTime.length == 3){
                    var h = vTime[0] * 1;
                    var m = vTime[1] * 1;
                    var s = vTime[2] * 1;

                    time2.setHours((h + tzFark),m,s);
                    v = time2.toLocaleTimeString();
                    tp = 1;


                }
            }
        }

        if(tp == 0){
            if(v.indexOf("/") >= 0){
                var vDate = v.split("/");
                if(vDate.length == 3){

                    v = months[parseInt(vDate[1],10) - 1] + "&nbsp;" + vDate[0];

                }
            }
        }

        val[i] = v;
    }

	dataProcess(val);
	loadStat = loadStat | 0x0001;
	fnShow();
    
    
           	
   	if(loadTime == 0){
    	
    	
    	milkPathTime = time1;
    	
		if(user_level == 0){
			flash = true;
			intervalId1 = setInterval("dataLoad();",(30 * 1000));		
			
		}
		else if(user_level == 1){
	    	milkServer();
	    	intervalId2 = setInterval("fnIntervalRealTime();",30000);  		
			
		}
		setInterval("resetColor1();",1000);	
		
    }
       
    loadTime = time1;
}



function fnShow(){

	if((loadStat == 0x0011)){
    	
    	var obj = getDiv(document,"page0"); 
    	obj.style.visibility = 'visible';
    	loadStat = loadStat | 0x0100;	   	
    	
    }


	
}


function dataProcess(val){


    var time1 = (new Date()).getTime();


	
	for(i in val){
    

        var idName =  '_' + i;
        var div = getDiv(document,idName);
        var v = val[i];
        if(div != null){

            var vo = div.innerHTML;

            if(vo != v){
                var iv = parseFloat(v);
                var ivo = parseFloat(div.innerHTML);
                div.innerHTML = v;

                div.style.fontFamily = "Tahoma";
                div.style.fontSize = 11;       
                
    
                div.style.color = "#FFFFFF";//sonra sil

                if(flash == true){
                	
                    tempColor1[idName] = time1;
                    var re = /([^0-9^\.^\-])+/gi ;
                    if(!(v.match(re) || vo.match(re))){
                        if(iv > ivo){
                            div.style.backgroundColor = '#00FF00';
                            div.style.color = '#000000';
                        }
                        else if(iv < ivo){
                            div.style.backgroundColor = '#FF0000';
                            div.style.color = '#FFFFFF';
                        }
                        else {
                            div.style.backgroundColor = '#FFFF00';
                            div.style.color = '#000000';
                        }
                    }
                    else if(vo != v){
                        div.style.backgroundColor = '#FFFF00';
                        div.style.color = '#000000';
                    }
                    else {
                        div.style.backgroundColor = '#000000';//sonra sil
                        div.style.color = '#FFFFFF';////sonra sil
                    }
                    
                }//if(flash == true){


            }
        }
    

	}

	//flash = false;
    

}

function newsLoaded(val){
    
    if(val.length == 0) {

        return;
    }
    

    var time2 = new Date();
    
    var newsH = "";

    var len = val.length - 1;

    newsId = val[len][0];

    for(i = 0;i <= len;i++){
        
        var news = val[i];
        
        if(news[0] != "" && news[1] != "" && news[2] != "" && news[3] != ""){
        
        
            var vDate = news[1].split("-");
            news[1] = months[parseInt(vDate[1],10) - 1] + "&nbsp;" + vDate[2];//dede
                    
            
            
            if(tzFark != 0){
                var v = news[2];
                if(v.indexOf(":") >= 0){
                    vTime = v.split(":");
                    if(vTime.length == 2){
                        var h = vTime[0] * 1;
                        var m = vTime[1] * 1;
                                                                
                        time2.setHours((h + tzFark),m,0);
                        v = time2.toLocaleTimeString();
                        v = v.substr(0,5);
                        
                                            
                    }
                }
                news[2] = v;
            }       
            
            newsVal.push(news);
        
        
        }
    }
    
    newsLoadPage();
    
    loadStat = loadStat | 0x0010;
    fnShow();


}

