﻿var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 

var Counter = 0;
var Interval = 1000;
var ShowStatus = false;	
	
window.setTimeout("FirstRefresh()", 1000);

function FirstRefresh()
{ 

	if (__AJAXObjectList == undefined)
		return;
	for (var i=0;i<__AJAXObjectList.length;i++)
	{
		if (__AJAXObjectList[i].NoOfPeriods == 0)
		{
			__AJAXObjectList[i].xmlHttpObj = GetXmlHttpObject(eval('CallBackHandler' + i));             
			//Send the xmlHttp get to the specified url 
			xmlHttp_Get(__AJAXObjectList[i].xmlHttpObj, __AJAXObjectList[i].URL); 
		}
		else
			ShowStatus = true;			
	}
	window.setTimeout("RefreshMe()", 1000);
}

function RefreshMe()
{ 
	if (__AJAXObjectList == undefined)
		return;
	for (var i=0;i<__AJAXObjectList.length;i++)
	{
		if ((__AJAXObjectList[i].NoOfPeriods>0) && (Counter%__AJAXObjectList[i].NoOfPeriods == 0))
		{
			__AJAXObjectList[i].xmlHttpObj = GetXmlHttpObject(eval('CallBackHandler' + i));             
			//Send the xmlHttp get to the specified url 
			xmlHttp_Get(__AJAXObjectList[i].xmlHttpObj, __AJAXObjectList[i].URL); 
		}
    }
	Counter = (Counter+1)%Periods;    
//	if (ShowStatus)
//		window.status="Bảng giá trực tuyến  * Tự động cập nhật sau "+(Periods - Counter)+" giây"
	window.setTimeout("RefreshMe()", Interval);    
}

//============ AJAXObject class =======================
function AJAXObject(ElementID, URL, NoOfPeriods) 
{
	this.ElementID = ElementID;
	this.URL = URL;	
	this.NoOfPeriods = NoOfPeriods;
	
}

// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url) 
{ 
    xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject(handler) 
{ 
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 
    //Depending on the browser, try to create the xmlHttp object 
    if (is_ie)
    { 
        //The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
            
        //Attempt to create the object 
        try
        { 
            objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = handler; 
        } 
        catch(e)
        { 
        //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
        } 
    } 
    else if (is_opera)
    { 
        //Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
    } 
    else
    { 
        // Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
    } 
        
    //Return the instantiated object 
    return objXmlHttp; 
} 

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
		}
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}

function SubmitForm(submitID) {
  if (submitID == 1)
  {	
	//search submit
	var obj = document.getElementById("SearchParam");
    var search = obj.value;
	if ( search == '')
	{
		alert('Hãy điền tên đội bóng bạn cần tìm!');
		return 0;
	}
	else if (!isProper(search))
	{
		alert('Không sử dụng các ký tự không cho phép khi tìm kiếm!');
		return 0;
	}
  }
  else if (submitID == 2)
  {
	//vote submit
	var vote = '';
	for(var i = 0; i<10; i++) 
	{
		if (document.myForm.VoteParam[i] == undefined)
		{
			break;
		}
		if (document.myForm.VoteParam[i].checked)
		{
			vote = document.myForm.VoteParam[i].value;
			break;
		}
	}
	if (vote == '')
	{
		alert('Hãy chọn option!');
		return 0;
	}
  }  
  var obj = document.getElementById("SubmitID");
  obj.value = submitID;
  obj = document.getElementById("CompleteRequest");
  document.myForm.action = obj.value;
  document.myForm.submit();  
}

function ShowPollResult(RegularVoteID)
{
	vWW = 560;
	vWH = 220;

	vWN = 'ShowPollResult';
	winDef = 'status=no,resizable=no,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(vWH).concat(',').concat('width=').concat(vWW).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - vWH)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - vWW)/2);
	newwin = open('', vWN, winDef);

	var action = RegularVoteID + "";
	action = "Chart/ShowPollResult.aspx?ID=" + action;
	document.myForm.action = action;
	document.myForm.target = vWN;
	document.myForm.submit();
	return;
}
         

function OnKeyPress(tb)
{
	// If it is the Enter key -> simulating Click on the default button
	if (event.keyCode == 13)
	{
		// cancel the default submit
		event.returnValue=false;
		event.cancel = true;
		// submit the form by programmatically clicking the specified button
		var obj = document.getElementById("SearchSubmit"); 
		if (obj != null) 
			obj.click();
	}
}

// Function to disallow certain characters in search string
function isProper(string) {
  if (!string) return false;
  var iChars = "*|,\":<>[]{}`;@#%^+?\\";
  for (var i = 0; i < string.length; i++) {
   if (iChars.indexOf(string.charAt(i)) != -1)
     return false;
  }
  return true;
}

