// JavaScript Document
var xmlhttp;
var timer;
var timer_flag = false;
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function change_profile(element_id)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
if(document.getElementById(element_id).value == '')
	{
			if(element_id == "user_alias")
				{
					document.getElementById(element_id).value = "Enter your alias";
					document.getElementById(element_id).style.border = "1px solid #584C6F";
				}
			
			 //document.getElementById(element_id).value = "Enter your alias";
		
	}
	else if(document.getElementById(element_id).value == "Enter your alias")
	{
		document.getElementById(element_id).style.border = "1px solid #584C6F";
	}
	
	else
	{
		document.getElementById(element_id).style.border = "0px solid #332B42";	
		element_value = document.getElementById(element_id).value;	
	

		
		var url="index.php?option=ajax_change_user_alias&value="+element_value;
		url=url+"&sid="+Math.random();
		//alert(url);
		xmlhttp.onreadystatechange=function(){
			//if(xmlhttp.readyState==4)
				//alert(xmlhttp.responseText);
		};
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}



function focus_profile(element_id)
{
	if(document.getElementById(element_id).value == "Enter your alias")
	{
		document.getElementById(element_id).value = "";
	}
	else if(document.getElementById(element_id).value == "Enter your Contact No.")
	{
		document.getElementById(element_id).value = "";
	}
}
function mouseover_profile(element_id)
{
	document.getElementById(element_id).style.border = "1px solid #584C6F";
	
}

function mouseout_profile(element_id)
{
	
	if(document.getElementById(element_id).value == '')
	{
			if(element_id == "user_alias")
				{
					document.getElementById(element_id).value = "Enter your alias";
					document.getElementById(element_id).style.border = "1px solid #584C6F";
				}
			else
				 {
					 document.getElementById(element_id).value = "Enter your Contact No.";
					 document.getElementById(element_id).style.border = "1px solid #584C6F";
				 }
			 //document.getElementById(element_id).value = "Enter your alias";
		
	}
	else if(document.getElementById(element_id).value == "Enter your alias")
	{
		document.getElementById(element_id).style.border = "1px solid #584C6F";
	}
	else if(document.getElementById(element_id).value == "Enter your Contact No.")
	{
		document.getElementById(element_id).style.border = "1px solid #584C6F";
	}
	else
		document.getElementById(element_id).style.border = "0px solid #332B42";	
}
function check_exit_arena(){
			var flag = confirm("Are you sure you want to exit the arena???!!! Your score in the practice problems will be reset");
			if(flag)window.location="index.php?option=exit_arena";
		}
function check_enter_arena(){
			var flag = confirm("Welcome to arena. Here you can submit solutions to problems and get a feel of the upcoming competitions. All problems are visible within the arena only. While in the arean, you cannot edit your profile details and cannot form team with someone. Once the competition is midway, you will not be allowed to exit the arena.");
			if(flag)window.location="index.php?option=enter_arena";
		}
function emailValidator(elem){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		elem.focus();
		return false;
	}
}



// SIDEBAR MENU LINKS : AJAX AND LINKS
function tab_select(div_id)
{
	
	if(div_id=='recentAct')
	{
		document.getElementById('recentAct').className = 'current' ;
		document.getElementById('top10').className = '';
		document.getElementById('topCampus').className = '';
		recent(1);
	}
	else if(div_id=='top10')
	{
		document.getElementById('recentAct').className = '' ;
		document.getElementById('top10').className = 'current';
		document.getElementById('topCampus').className = '';
		fetch_top10();
		
	}
	else if(div_id=='topCampus')
	{
		document.getElementById('recentAct').className = '' ;
		document.getElementById('top10').className = '';
		document.getElementById('topCampus').className = 'current';
		fetch_topCampus();
		
	}
}

function recent(page)
{
	document.getElementById('tab_content').style.display = 'none';
	document.getElementById('tab_loader').style.display = 'block';
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null){
		alert("Your browser does not support XMLHTTP!!!");
		return;
	}
	var url="index.php?option=recent_activity&page="+page;
	url=url+"&sid="+Math.random();
		//alert(url);
	xmlhttp.onreadystatechange=function(){
	if(xmlhttp.readyState==4)
	{
		document.getElementById('tab_content').innerHTML=xmlhttp.responseText;
		document.getElementById('tab_loader').style.display='none';
		document.getElementById('tab_content').style.display='block';
	}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	if(timer_flag)
	{
		clearTimeout(timer);
		timer = setTimeout("recent(1)",45000);
	}
	else
	{
		timer_flag=true;
		timer = setTimeout("recent(1)",45000);
	}
		
		
}



function fetch_top10()
{
	document.getElementById('tab_content').style.display = 'none';
	document.getElementById('tab_loader').style.display = 'block';
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null){
		alert("Your browser does not support XMLHTTP!!!");
		return;
	}
	var url="index.php?option=ajax_leader_board";
	url=url+"&sid="+Math.random();
		//alert(url);
	xmlhttp.onreadystatechange=function(){
	if(xmlhttp.readyState==4)
	{
		document.getElementById('tab_content').innerHTML=xmlhttp.responseText;
		document.getElementById('tab_loader').style.display='none';
		document.getElementById('tab_content').style.display='block';
	}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	if(timer_flag)
	{
		clearTimeout(timer);
		timer = setTimeout("fetch_top10()",90000);
	}
	else
	{
		timer_flag=true;
		timer = setTimeout("fetch_top10()",90000);
	}
	
}

function fetch_topCampus()
{
	document.getElementById('tab_content').style.display = 'none';
	document.getElementById('tab_loader').style.display = 'block';
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null){
		alert("Your browser does not support XMLHTTP!!!");
		return;
	}
	var url="index.php?option=ajax_leader_board&campus="+true;
		url=url+"&sid="+Math.random();
		//alert(url);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4)
			{
				document.getElementById('tab_content').innerHTML=xmlhttp.responseText;
				document.getElementById('tab_loader').style.display='none';
				document.getElementById('tab_content').style.display='block';
			}
		};
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	if(timer_flag)
	{
		clearTimeout(timer);
		timer = setTimeout("fetch_topCampus()",120000);
	}
	else
	{
		timer_flag=true;
		timer = setTimeout("fetch_topCampus()",120000);
	}
}



//////////////
function countdown_timer()
{
	clearTimeout(clock);
	var h = parseInt(document.getElementById('clock_hr_val').value);
	var m = parseInt(document.getElementById('clock_min_val').value);
    var s = parseInt(document.getElementById('clock_sec_val').value);
	if( (h==0) && (m==0) && (s==0) )
		return;
	
	s = s - 1 ;
	if (s < 0)
	{
		s = 59 ;
		m = m  - 1;
		if ( m < 0)
		{
			m = 59;
			h = h - 1 ;
		}
	}
	//alert(s);
	document.getElementById('clock_hr_val').value = h;
	document.getElementById('clock_min_val').value = m;
	document.getElementById('clock_sec_val').value = s;
	if (h < 10)
		h = '0' + h ;
	if (s < 10)
		s = '0' + s ;
	if (m < 10)
		m = '0' + m ;
	h = h + ':' ;
	m = m + ':' ;
	document.getElementById('clock_hr').innerHTML = h;
	document.getElementById('clock_min').innerHTML = m ;
	document.getElementById('clock_sec').innerHTML = s ;
	clock = setTimeout("countdown_timer()",1000);	
}
////////////


//////////////
function refresh_update()
{
	clearTimeout(update_clock);
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null){
		alert("Your browser does not support XMLHTTP!!!");
		return;
	}
	var url="index.php?option=ajax_update_bar";
	url=url+"&sid="+Math.random();
		//alert(url);
	xmlhttp.onreadystatechange=function(){
	if(xmlhttp.readyState==4)
	{
		document.getElementById('ajax_update_bar').innerHTML=xmlhttp.responseText;
		
	}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	update_clock = setTimeout("refresh_update()",60000);	
}
////////////
