var at_submitType;
var at_formId;
var at_linkId;
var at_checkboxIdList;

var at_sleepTime = 9;		
var at_inputSleepTime;	
var at_progressColor = 'blue';	
var at_progressInterval = 1000;	
var at_progressAt = -1;
var at_progressTimer;
var at_isPaused;
var at_isSubmitted = false;
var at_toolBar;
var at_showProgress;

var at_arrayUpdateUrlSpanId;
var at_action;
var at_arrayRowIndex;
var at_arrayColSize;
var at_arrayAddRowSize;

function AT_progressUpdate() {
	if ( at_showProgress == false ) {
		window.clearTimeout(at_progressTimer);	
		return;
	}
	at_progressAt++;	
	if ( at_progressAt > at_sleepTime ) {
		AT_doSubmit();
		window.clearTimeout(at_progressTimer);			
		//AT_progressClear();
	} else {
		var aProg = findObject('at_toolbarProg' + at_progressAt);
		if ( aProg ) {
			aProg.style.backgroundColor = at_progressColor;
		}
	}
	at_progressTimer = window.setTimeout('AT_progressUpdate()', at_progressInterval);
}

function AT_checkTransaction(formId, checkBoxId) {
	var form = GL_findForm(formId);
	if ( !form ) {
		return;
	}
	var field = GL_findField(form, checkBoxId);
	if ( field ) { 
		field.checked = true;
	}
}

function AT_setArraySubmitData (sleepTime, arrayUpdateUrlSpanId, arrayRowIndex, arrayColSize, addSize) {
	at_submitType = 'array';
	at_sleepTime = sleepTime;
	at_inputSleepTime = sleepTime;
	at_arrayUpdateUrlSpanId = arrayUpdateUrlSpanId;
	at_action = 'addArrayElements';
	at_arrayRowIndex = arrayRowIndex;
	at_arrayColSize = arrayColSize
	at_arrayAddRowSize = addSize;
}

function AT_setSubmitData(submitType, id, sleepTime) {
	at_submitType = submitType;
	at_sleepTime = sleepTime;
	at_inputSleepTime = sleepTime;
	if ( at_submitType == 'link' ) {
		at_linkId = id;
	} else {
		at_formId = id;
	} 
	at_sleepTime = sleepTime - 1;
}

function AT_findLink(linkId) {
	var link = findObject(linkId);
	if ( !link ) {
		return;
	}	
	if ( link.length && link.length > 0 ) {
		link = link[0];
	}
	return link;
}

function AT_highlightLink(linkId) {
	var link = AT_findLink(linkId);
	if ( link ) {
		link.style.backgroundColor='blue';
		link.style.color='white';
		link.style.fontWeight='bold';	
	}
}

function GL_fixToolBar() {
	if ( !GL_isIE() ) {
		return;
	}
	var toolbar = AT_getATToolBar();
	toolbar.fix_bottom = toolbar.currentStyle.bottom;	
	toolbar.style.position = 'absolute';
	toolbar.style.visibility = 'visible';
	GL_fixEvent();
	window.attachEvent('onscroll',GL_fixEvent);
	window.attachEvent('onresize',GL_fixEvent);	
}

function GL_checkActionExist() {
	var action;
	if ( at_submitType == 'link' ) {
		action = AT_findLink(at_linkId);		
	} else {	
		if ( at_submitType == 'array' ) {
			action = findObject( at_arrayUpdateUrlSpanId);
		} else {
			action = GL_findForm(at_formId);		
		}
	}	
	
	if ( action ) {
		at_showProgress = true;
	} else {
		at_showProgress = false;
	}
	
	if ( !at_showProgress ) {
		findObject('at_progressControl').innerHTML = ' Auto Testing Available ';
	}
}
function GL_fixEvent() {
	var toolbar = AT_getATToolBar();
	var body = document.getElementsByTagName('body')[0];
	if ( GL_isIE() ) {
		var html = document.getElementsByTagName('html')[0];
		if (html.scrollTop == 0 && html.clientHeight == 0) {
			toolbar.style.top = (body.scrollTop + body.clientHeight - toolbar.offsetHeight) + 'px';
		} else {
			toolbar.style.top = (html.scrollTop + html.clientHeight - toolbar.offsetHeight) + 'px';		
		}
		var left = (body.clientWidth - toolbar.offsetWidth)/2;
		toolbar.style.left = left + 'px';
	} else {
		toolbar.style.top = body.scrollTop + body.clientHeight - toolbar.offsetHeight;
	}
}


function AT_doSubmit() {
	if ( at_isSubmitted ) {
		return;
	}	
	at_isSubmitted = true;
	findObject('at_progressControl').innerHTML = ' Please wait ... ';
	if ( at_submitType == 'link' ) {
		var link = AT_findLink(at_linkId);		
		if ( link ) {
			link.href += '&sleepTime=' + at_inputSleepTime;
			document.location.href=	link.href;	
		}
	} else {	
		if ( at_submitType == 'array' ) {
			AT_setInputSleepTime('transactionForm');
			GL_submitUpdateArrayForm(at_arrayUpdateUrlSpanId, at_action,
				 at_arrayRowIndex, at_arrayColSize, at_arrayAddRowSize);
		} else {
			var aForm = GL_findForm(at_formId);		
			if ( aForm ) {
				AT_setInputSleepTime(at_formId);
				aForm.submit();
			}
		}
	}
}

function AT_setInputSleepTime(formId) {
	var aForm = GL_findForm(formId);	
	if ( !aForm ) {
		return;
	}
	var sleepTimeField = GL_findField(aForm, 'sleepTime');
	if ( !sleepTimeField ) {
		aForm.innerHTML += '<input type="hidden" name="sleepTime" value=\"\"/>';
		sleepTimeField = GL_findField(aForm, 'sleepTime');
	}	
	sleepTimeField.value = at_inputSleepTime;	
}

function AT_setSleepTime(evt, input) {
	if ( input.value < 1 ) {
		return;
	}
	at_inputSleepTime = input.value;
}

function AT_progressClear() {
	for (var i = 1; i <= at_sleepTime; i++) {
		var aProg = findObject('at_toolbarProg' + i);
		aProg.style.backgroundColor = 'transparent';
	}
	at_progressAt = 0;
}


function AT_getATToolBar() {
	if ( at_toolBar == null ) {
		at_toolBar = findObject('at_toolbar');	
	}
	return at_toolBar;
}

function AT_progressStop() {	
	var pauseImage = findObject('at_toolbarPause');	
	if ( at_isPaused ) {
		pauseImage.src = 'image/pause.gif';		
		at_isPaused = false;
		AT_progressUpdate();
	} else {
		pauseImage.src = 'image/play.gif';	
		at_isPaused = true;
		window.clearTimeout(at_progressTimer);	
	}
}






