/**************************
 *
 * Copyright Mindinsight.com
 * author: prithvi
 * 
 **************************/


var waiting =true;
var done = false;
var responseTxt;
var testId;
var clockID  = null;
var clockRunning  = false;
var clockCounter = 5;
var verifyCallback =
{
	cache:false,
	success: function(o) {
	//alert("Inside callback...")
	waiting = false;
	responseTxt = o.responseText;
	if(responseTxt == 1)
	{
		done = true;
	}else
	{
		done = false;
	}
	
}
}
function init()
{
	wating = true;
	//alert("Inside init")
	var transaction = YAHOO.util.Connect.asyncRequest("GET", "/mi_verifycatregistration.action", verifyCallback, null);
}
function getWidth(){
	 var myWidth = 0;
	 if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	   } else if( document.documentElement && document.documentElement.clientWidth) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	   } else if( document.body && document.body.clientWidth ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	   }
	   return myWidth;
	 }
function launchTest(id)
{
	testId =id;
//alert("testID: "+testId+" status: "+done+" waiting: "+waiting);
	if(waiting)
	{
		showMsg('Verifying your submission of CAT 2009 registration number');
		periodicCheck();
	}else
	{
		if(done)
		{
			var W=getWidth();
			var H=screen.availHeight;
			newwindow=window.open("/testcenter/mi_launchtest.action?testId="+id,"TestCenter",'toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=1,fullscreen=no');
			if (window.focus) {newwindow.focus()}
			var msg = " Test in Progress <br> Please wait...";
			showMsg(msg);
			return;
		}else
		{
			verifyCATRegistration(2);
		}
		}
}
function periodicCheck()
{
	if(clockCounter)
	{
		clockID = setTimeout("launchTest("+testId+")",1000);
		clockRunning = true;
	}
	else
	{
		stopClock();
		timeOver();
	}
	clockCounter  = clockCounter -1;
}
function stopClock()
{

	if(clockRunning)
	{
		clearTimeout(clockID);
	}
	clockRunning = false;
}
function launchAnssheet(id)
{
	testId =id;
	alert("testID: "+testId+" status: "+done+" waiting: "+waiting);
	if(waiting)
	{
		//showMsg('Verifying your submission of CAT 2009 registration number');
		verifyCATRegistration(1);
	}else
	{
		if(done)
		{
		var W=getWidth();
		var H=screen.availHeight;
		newwindow=window.open("/testcenter/loadanssheet.action?testId="+id,"TestCenter",'toolbar=1,scrollbars=yes,location=0,statusbar=1,menubar=1,resizable=1,fullscreen=no');
		if (window.focus) {newwindow.focus()}
		var msg = " Test in Progress <br> Please wait...";
		showMsg(msg);
		}else
		{
			verifyCATRegistration(2);
		}
	}
}	

function showAlert()
{
	alert('AITS Capsule will be available after AITS-MCAT is over');
}
function verifyCATRegistration(id)
{

	jQuery.blockUI({message:jQuery('#catregpopup'+id),css: { 
		border: '2px solid #fff',
		padding: '15px',
		backgroundColor: '#7a0000',
		'-webkit-border-radius': '10px',
		'-moz-border-radius': '10px',
		opacity: '1.0',
		color: '#fff'
	},
	overlayCSS:{  
		backgroundColor:'#000',  
		opacity:'0.8'  
	}});
	
}
function takeMeThere()
{
	 this.location.href= "http://www.mindinsight.com/pages/reg/reg.jsp";
	// this.location.href = "http://localhost:8090/pages/cat2009/cat2009.jsp";
	
}
var verifyCallback1 =
{
	cache:false,
	success: function(o) {
	alert("Inside callback...")
	waiting = false;
	var rspTxt = o.responseText;
	alert(rspTxt);
	if(rspTxt == 1)
	{
		done = true;
	}else
	{
		done = false;
	}
	restore();
	launchTest(testId);
}
}
function verifyAsync()
{
	showMsg('Verifying your submission of CAT 2009 registration number.<br> Please wait...');
	
	var transaction = YAHOO.util.Connect.asyncRequest("GET", "/mi_verifycatregistration.action", verifyCallback1, null);
}

function showMsg(msg)
{
	jQuery.blockUI({message:msg,css: { 
		border: '2px solid #fff',
		padding: '15px',
		backgroundColor: '#7a0000',
		'-webkit-border-radius': '10px',
		'-moz-border-radius': '10px',
		opacity: '1.0',
		color: '#fff'
	},
	overlayCSS:{  
		backgroundColor:'#000',  
		opacity:'0.8'  
	}
	});
}
function restore()
{
	jQuery.unblockUI();
}
function download(id,type)
{
	testId =id;
	if(waiting)
	{
		//showMsg('Verifying your submission of CAT 2009 registration number');
		verifyCATRegistration(1);
	}else
	{
		if(done)
		{
			 this.location.href= "http://www.mindinsight.com/dnldpaper.action?downloadName="+type;
		}else
		{
			verifyCATRegistration(2);
		}
	}
}
