var screenHeight = screen.height;
var screenWidth = screen.width;

var platform = navigator.platform;

var browser = new browser();

//Drag Layer global variable
var dragPosSeq = new Array(-1,-1,-1,-1);
var dragPosImg = new Array(-1,-1,-1,-1);
var dragPosAln = new Array(-1,-1,-1,-1);
var dragPosDpl = new Array(-1,-1,-1,-1);
var dragObj = new Object();
dragObj.zIndex = 100;

var resultType = 0;

var sec = 0;
var min = 0;
var d = 0;
var timer = 0;
var optionOpen = false;
var optionAdvOpen = false;
var dirHome = '/plotrep/';
var dirImage = dirHome + 'images/';
var dirCgiHome = '/cgi-bin/';
var dirWork = '/plotrep_res/';
headerMenuNameArray = new Array("headerMenuHome","headerMenuSearch","headerMenuTutorial","headerMenuReferences","headerMenuContact");
headerMenuStageArray = new Array(false,true,true,true,true);
var headerMenuPrevClick = 0;
var resultMenuPrevClick = 0;

var imageId = 0;
var imageType = 0;
var imageZoom = 100;
var imageSummedZoom = 850;
var imageDotplotZoom = 600;

var partSeqBeg = 0;
var partSeqEnd = 0;

// Determine browser and version.
function browser(){
	var ua, s, i;
	
	this.isIE    = false;
	this.isNS    = false;
	this.version = null;
	
	ua = navigator.userAgent;
	
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0){
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0){
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0){
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

function changeLibrary(){
	var libIndex = document.getElementById('libType').selectedIndex;
	if (libIndex == 0){
		document.getElementById('libReference').style.display = 'block';
		document.getElementById('libRepBase').style.display = 'none';
		document.getElementById('libTigr').style.display = 'none';
	}else if (libIndex == 1){
		document.getElementById('libReference').style.display = 'none';
		document.getElementById('libRepBase').style.display = 'block';
		document.getElementById('libTigr').style.display = 'none';
	}else if (libIndex == 2){
		document.getElementById('libReference').style.display = 'none';
		document.getElementById('libRepBase').style.display = 'none';
		document.getElementById('libTigr').style.display = 'block';
	}
}

function optionOver(){
	if (optionOpen){
		document.getElementById('optionImage').src = dirImage + '16_arrowDownGreen.gif';
	}else{
		document.getElementById('optionImage').src = dirImage + '16_arrowRightGreen.gif';
	}
}

function optionOut(){
	if (optionOpen){
		document.getElementById('optionImage').src = dirImage + '16_arrowDownBlue.gif';
	}else{
		document.getElementById('optionImage').src = dirImage + '16_arrowRightBlue.gif';
	}
}

function optionClick(){
	if (optionOpen){
		document.getElementById('option').style.display = 'none';
		document.getElementById('optionImage').src = dirImage + '16_arrowRightBlue.gif';
		optionOpen = false;
	}else{
		document.getElementById('option').style.display = 'block';
		document.getElementById('optionImage').src = dirImage + '16_arrowDownBlue.gif';
		optionOpen = true;
	}
}

function optionAdvOver(){
	if (optionAdvOpen){
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowDownGreen.gif';
	}else{
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowRightGreen.gif';
	}
}

function optionAdvOut(){
	if (optionAdvOpen){
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowDownBlue.gif';
	}else{
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowRightBlue.gif';
	}
}

function optionAdvClick(){
	if (optionAdvOpen){
		document.getElementById('optionPropAdvProp').style.display = 'none';
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowRightBlue.gif';
		optionAdvOpen = false;
	}else{
		document.getElementById('optionPropAdvProp').style.display = 'block';
		document.getElementById('optionAdvImage').src = dirImage + '16_arrowDownBlue.gif';
		optionAdvOpen = true;
	}
}

function optInfoAction(action,infoId){
	if (action == 'over'){
		document.getElementById(infoId).src = dirImage + '16_infoRed.gif';
	}else if (action == 'out'){
		document.getElementById(infoId).src = dirImage + '16_infoBlue.gif';
	}
}

function optionPropOver(overOut,propName){
	if (overOut == 'over'){
		if (propName == 'ins'){
			document.getElementById('optionPropIns').src = dirImage + '16_infoRed.gif';
		}else if (propName == 'del'){
			document.getElementById('optionPropDel').src = dirImage + '16_infoRed.gif';
		}else if (propName == 'gap'){
			document.getElementById('optionPropGap').src = dirImage + '16_infoRed.gif';
		}else if (propName == 'merg'){
			document.getElementById('optionPropMerg').src = dirImage + '16_infoRed.gif';
		}else if (propName == 'offset'){
			document.getElementById('optionPropOffset').src = dirImage + '16_infoRed.gif';
		}
	}else if (overOut == 'out'){
		if (propName == 'ins'){
			document.getElementById('optionPropIns').src = dirImage + '16_infoBlue.gif';
		}else if (propName == 'del'){
			document.getElementById('optionPropDel').src = dirImage + '16_infoBlue.gif';
		}else if (propName == 'gap'){
			document.getElementById('optionPropGap').src = dirImage + '16_infoBlue.gif';
		}else if (propName == 'merg'){
			document.getElementById('optionPropMerg').src = dirImage + '16_infoBlue.gif';
		}else if (propName == 'offset'){
			document.getElementById('optionPropOffset').src = dirImage + '16_infoBlue.gif';
		}
	}
}

function headerMenuAction(menuPos,menuEvent){
	if (headerMenuStageArray[menuPos]){
		var menuName = headerMenuNameArray[menuPos];
		if (menuEvent == 'over'){
			document.getElementById(menuName).className = 'headerMenuOver';
		}else if (menuEvent == 'out'){
			document.getElementById(menuName).className = 'headerMenuOut';
		}else if (menuEvent == 'click'){
			document.getElementById(menuName).className = 'headerMenuClick';
			var menuNameTmp = headerMenuNameArray[headerMenuPrevClick];
			document.getElementById(menuNameTmp).className = 'headerMenuOut';
			headerMenuStageArray[headerMenuPrevClick] = true;
			headerMenuStageArray[menuPos] = false;
			headerMenuPrevClick = menuPos;
			requestHeaderMenu(1,menuPos);
		}
	}
}

function requestHeaderMenu(requestNbr,menuPos) {
	if (window.XMLHttpRequest) {
		var xmlhttp = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
		xmlhttp.open('POST',dirCgiHome + 'plotrep.pl', true);
		xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (requestNbr == 1){
				document.getElementById('mainPage').innerHTML = xmlhttp.responseText;
				if (menuPos == 1){
					document.getElementById('pageId').value = document.getElementById('pageMainId').value;
				}
			}else if (requestNbr == 2){
				document.getElementById('libTigrClassSelect').innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	if (requestNbr == 1){
		xmlhttp.send('request=1&menu=' + menuPos);
	}else if (requestNbr == 2){
		xmlhttp.send('request=' + requestNbr + '&database=' + menuPos);
	}
}

function tigrDatabaseSelect(){
	var tigrDatabasePos = document.getElementById('libTigrDatabase').selectedIndex;
	var tigrDatabaseName = document.getElementById('libTigrDatabase').options[tigrDatabasePos].value;
	requestHeaderMenu(2,tigrDatabaseName);
}

function requestFormSubmit(){
	var querySeq = 0;
	var libSeq = 0;
	var coolSeq = 0;
	var errMsg = '';
	
	if (document.getElementById('queryFile').value){querySeq ++}
	if (document.getElementById('queryText').value){querySeq ++}
	if (document.getElementById('libFile').value){libSeq ++}
	if (document.getElementById('libText').value){libSeq ++}
	
	if (querySeq == 2){
		errMsg += 'You have given more than one Query source sequence!\n';
	}else if (querySeq < 1){
		errMsg += 'You have given no Query sequence!\n';
	}else{
		coolSeq ++;
	}
	
	if (document.getElementById('libType').selectedIndex == 0){
		if (libSeq == 2){
			errMsg += 'You have given more than one Reference source sequence!\n';
		}else if (libSeq < 1){
			errMsg += 'You have given no Reference sequence!\n';
		}else{
			coolSeq ++;
		}
	}else{
		coolSeq ++;
	}
	
	if (document.getElementById('searchMethod').selectedIndex == 1){
		if (document.getElementById('optBlastWordSize').value < 6){
			errMsg += 'The value given for \'Word size\' is smaller than permissible (6-13)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastWordSize').value > 13){
			errMsg += 'The value given for \'Word size\' is larger than permissible (6-13)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastPenalGap').value < 5){
			errMsg += 'The value given for \'Penalty for a gap\' is smaller than permissible (5-40)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastPenalGap').value > 40){
			errMsg += 'The value given for \'Penalty for a gap\' is larger than permissible (5-40)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastPenalExt').value < 4){
			errMsg += 'The value given for \'Penalty for extending\' is smaller than permissible (4-10)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastPenalExt').value > 10){
			errMsg += 'The value given for \'Penalty for extending\' is larger than permissible (4-10)!\n';
			coolSeq = 1;
		}
	}else if (document.getElementById('searchMethod').selectedIndex == 2){
		if (document.getElementById('optBlastWordSize').value < 2){
			errMsg += 'The value given for \'Word size\' is smaller than permissible (2-4)!\n';
			coolSeq = 1;
		}
		if (document.getElementById('optBlastWordSize').value > 4){
			errMsg += 'The value given for \'Word size\' is larger than permissible (2-4)!\n';
			coolSeq = 1;
		}
	}
	
	if (coolSeq == 2){
		document.plotrepSearchForm.submit();
	}else{
		window.alert(errMsg);
	}
}

function requestStart(){
	if (document.getElementById('requesError').value == 1){
		var prRun = 'request=3&run=1&pid=' + document.getElementById('resultPageId').value + '&rid=' + document.getElementById('resultRequestId').value;
		var prChack = 'request=3&run=2&pid=' + document.getElementById('resultPageId').value + '&rid=' + document.getElementById('resultRequestId').value + '&smode=' + document.getElementById('searchMethod').value;
		document.getElementById('result').innerHTML = 'Request in process...<br />Elapsed time: <strong>0:0</strong>';
		startTimer(prRun,prChack);
	}
}

function startTimer(processRun,processChack){
	if (d == 0){
		window.resizeTo(950,800);
		chackResult(processRun,1);
	}else if (d == 4){
		//chackResult(processChack);
	}else if (d < 5){
		//chackResult('processChack');
	}else if (d == 10){
		chackResult(processChack,2);
		d = 1;
	}
	if (sec == 60){min ++;sec = 0;}
	document.getElementById('result').innerHTML = 'Request in process...<br />Elapsed time: <strong>' + min + ':' + sec + '</strong>';
	sec ++;
	d ++;
	timer = window.setTimeout('startTimer(\'' + processRun + '\',\'' + processChack + '\')', 1000);
}

function resultTypeChange(rType){
	if (rType != resultType){
		resultType = rType;
		var pid = document.getElementById('resultPageId').value;
		var rid = document.getElementById('resultRequestId').value;
		var libraryType = document.getElementById('libraryType').value;
		var querySeq = 'request=3&run=6&pid=' + pid + '&rid=' + rid + '&rtype=' + rType + '&ltype=' + libraryType;
		chackResult(querySeq,6);
	}
}

function resultShowDotplotAction(action,btId,e){
	if (action == 'over'){
		document.getElementById(btId).src = dirImage + 'showDotplotRed.gif';
	}else if (action == 'out'){
		document.getElementById(btId).src = dirImage + 'showDotplotBlue.gif';
	}else if (action = 'click'){
		var pageID = document.getElementById('resultPageId').value;
		var requestID = document.getElementById('resultRequestId').value;
		var imageSrc = dirWork + pageID + '_' + requestID + '_1_d' + '.png';
		//imageId = imId;
		//imageType = imType;
		//imageZoom = 100;
		var browser = navigator.appName;
		if (browser == "Netscape"){
			y = e.pageY;
		}else{
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		document.getElementById('resDotplotLayerImageIm').src = imageSrc;
		document.getElementById('resDotplotLayer').style.top = y - 10;
		document.getElementById('resDotplotLayer').style.visibility = 'visible';
	}
}

function resultTableButtonAction(action,btId){
	if (action == 'over'){
		document.getElementById(btId).src = dirImage + '16_tableRed.gif';
	}else if (action == 'out'){
		document.getElementById(btId).src = dirImage + '16_tableBlue.gif';
	}
}

function resultLinkAction(action,btId){
	if (action == 'over'){
		document.getElementById(btId).src = dirImage + '16_linkRed.gif';
	}else if (action == 'out'){
		document.getElementById(btId).src = dirImage + '16_linkBlue.gif';
	}
}

function resultRowAction(action,rowId){
	if (action == 'over'){
		document.getElementById(rowId).className = 'bgStyle1';
	}else if (action == 'out'){
		document.getElementById(rowId).className = 'bgStyle4';
	}
}

function resultNameAction(action,nameId,imId,imType,e){
	if (action == 'over'){
		document.getElementById(nameId).className = 'fontStyle6';
	}else if (action == 'out'){
		document.getElementById(nameId).className = 'fontStyle2';
	}else if (action == 'click'){
		var pageID = document.getElementById('resultPageId').value;
		var requestID = document.getElementById('resultRequestId').value;
		var imageSrc = dirWork + pageID + '_' + requestID + '_' + imId + '_' + imType + '.png';
		imageId = imId;
		imageType = imType;
		imageZoom = 100;
		var browser = navigator.appName;
		if (browser == "Netscape"){
			y = e.pageY;
		}else{
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		document.getElementById('resImageLayerImageIm').src = imageSrc;
		document.getElementById('resImageLayer').style.top = y + 30;
		document.getElementById('resImageLayer').style.visibility = 'visible';
	}
}

function resultLoadRepplotImage(){
	var width = document.getElementById('resImageLayerImageIm').width;
	var height = document.getElementById('resImageLayerImageIm').height;
	if (width > screenWidth){
		width = screenWidth - 35;
		height += 35;
		document.getElementById('resImageLayerImage').style.overflow = 'scroll';
	}else{
		document.getElementById('resImageLayerImage').style.overflow = 'visible';
	}
	document.getElementById('resImageLayerCaption').style.width = width + 4;
	document.getElementById('resImageLayerCaption').style.height = height + 20;
	document.getElementById('resImageLayer').style.width = width + 6;
	document.getElementById('resImageLayer').style.height = height + 22;
	document.getElementById('resImageLayerImage').style.width = width;
	document.getElementById('resImageLayerImage').style.height = height;
	document.getElementById('resImageLayerBtCancel').style.left = width - 17;
	document.getElementById('resImageLayerBtZin').style.left = width - 34;
	document.getElementById('resImageLayerBtZfit').style.left = width - 51;
	document.getElementById('resImageLayerBtZout').style.left = width - 68;
	dragPosInit('img',1,1,width - 70,19);
}

function resultLoadDotplotImage(){
	var width = document.getElementById('resDotplotLayerImageIm').width;
	var height = document.getElementById('resDotplotLayerImageIm').height;
	if (width > screenWidth){
		width = screenWidth - 35;
		height += 35;
		document.getElementById('resDotplotLayerImage').style.overflow = 'scroll';
	}else{
		document.getElementById('resDotplotLayerImage').style.overflow = 'visible';
	}
	document.getElementById('resDotplotLayerCaption').style.width = width + 4;
	document.getElementById('resDotplotLayerCaption').style.height = height + 20;
	document.getElementById('resDotplotLayer').style.width = width + 6;
	document.getElementById('resDotplotLayer').style.height = height + 22;
	document.getElementById('resDotplotLayerImage').style.width = width;
	document.getElementById('resDotplotLayerImage').style.height = height;
	document.getElementById('resDotplotLayerBtCancel').style.left = width - 17;
	document.getElementById('resDotplotLayerBtZin').style.left = width - 34;
	document.getElementById('resDotplotLayerBtZfit').style.left = width - 51;
	document.getElementById('resDotplotLayerBtZout').style.left = width - 68;
	dragPosInit('dotplot',1,1,width - 70,19);
}

function resultLoadSummedImage(){
	var width = document.getElementById('resSummedLayerImageIm').width;
	if (width > 850){
		document.getElementById('resSummedLayerImage').style.overflow = 'scroll';
	}else{
		document.getElementById('resSummedLayerImage').style.overflow = 'visible';
	}
}

function resultTooltipAction(winName,action,button,buttonId){
	var pid = 0;
	var rid = 0;
	if (action == 'click'){
		pid = document.getElementById('resultPageId').value;
		rid = document.getElementById('resultRequestId').value;
	}
	if (button == 'cancel'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_cancelRed_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_cancelRed.gif';
		}else if (action == 'click'){
			if (winName == 'seq'){
				document.getElementById('resSeqLayer').style.visibility = 'hidden';
			}else if (winName == 'image'){
				document.getElementById('resImageLayer').style.visibility = 'hidden';
			}else if (winName == 'aln'){
				document.getElementById('resAlnLayer').style.visibility = 'hidden';
			}else if (winName == 'dotplot'){
				document.getElementById('resDotplotLayer').style.visibility = 'hidden';
			}
		}
	}else if (button == 'copy'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_copyBlue_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_copyBlue.gif';
		}else if (action == 'click'){
			if (winName == 'seq'){
				var textTmp = document.getElementById('resSeqLayerSeqText').innerHTML;
				if (platform == 'Win32' || platform == 'Win64'){
					var replaceStr = '\n\r';
				}else{
					var replaceStr = '\n';
				}
				var regexp = eval(/<br>/gi);
				var text =  textTmp.replace(regexp,replaceStr);
				copyClipboard(text);
			}else if (winName == 'aln'){
				var textTmp1 = document.getElementById('resAlnLayerAlnText').innerHTML;
				var regexp1 = eval(/<pre>/gi);
				var regexp2 = eval(/<\/pre>/gi);
				var textTmp2 =  textTmp1.replace(regexp1,'');
				var text =  textTmp2.replace(regexp2,'');
				copyClipboard(text);
			}
		}
	}else if (button == 'save'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_saveBlue_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_saveBlue.gif';
		}else if (action = 'click'){
			if (winName == 'seq'){
				var querySeq = 'request=3&run=5&pid=' + pid + '&rid=' + rid + '&beg=' + partSeqBeg + '&end=' + partSeqEnd;
				chackResult(querySeq,5);
			}
		}
	}else if (button == 'zoomIn'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_zoomInBlue_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_zoomInBlue.gif';
		}else if (action == 'click'){
			if (winName == 'image'){
				var imid = imageId;
				var imtype = imageType;
				imageZoom -= 10;
				var imzoom = imageZoom;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=' + imid + '&imtype=' + imtype + '&imzoom=' + imzoom;
				chackResult(querySeq,4,imtype);
			}else if (winName == 'summed'){
				/*if (document.getElementById('resSummedLayerImageIm').width == 850){
					imageZoom = 850;
				}*/
				imageSummedZoom += 100;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=1&imtype=s&imzoom=' + imageSummedZoom;
				chackResult(querySeq,4,'s');
			}else if (winName == 'dotplot'){
				imageDotplotZoom += 100;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=1&imtype=d&imzoom=' + imageDotplotZoom;
				chackResult(querySeq,4,'d');
			}
		}
	}else if (button == 'zoomOut'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_zoomOutBlue_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_zoomOutBlue.gif';
		}else if (action == 'click'){
			if (winName == 'image'){
				var imid = imageId;
				var imtype = imageType;
				imageZoom += 10;
				var imzoom = imageZoom;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=' + imid + '&imtype=' + imtype + '&imzoom=' + imzoom;
				chackResult(querySeq,4,imtype);
			}else if (winName == 'summed'){
				imageSummedZoom -= 100;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=1&imtype=s&imzoom=' + imageSummedZoom;
				chackResult(querySeq,4,'s');
			}else if (winName == 'dotplot'){
				imageDotplotZoom -= 100;
				var querySeq = 'request=3&run=4&pid=' + pid + '&rid=' + rid + '&imid=1&imtype=d&imzoom=' + imageDotplotZoom;
				chackResult(querySeq,4,'d');
			}
		}
	}else if (button == 'zoomFit'){
		if (action == 'over'){
			document.getElementById(buttonId).src = dirImage + '16_zoomFitBlue_1.gif';
		}else if (action == 'out'){
			document.getElementById(buttonId).src = dirImage + '16_zoomFitBlue.gif';
		}else if (action == 'click'){
			var imageSrc = '';
			if (winName == 'image'){
				var imid = imageId;
				var imtype = imageType;
				imageZoom = 100;
				var imzoom = imageZoom;
				imageSrc = dirWork + pid + '_' + rid + '_' + imid + '_' + imageType + '.png';
				if (imageType == 'm'){
					document.getElementById('resImageLayerImageIm').src = imageSrc;
				}else if (imageType == 'r'){
					document.getElementById('resImageLayerImageIm').src = imageSrc;
				}
			}else if (winName == 'summed'){
				imageSummedZoom = 850;
				imageSrc = dirWork + pid + '_' + rid + '_1_s.png';
				document.getElementById('resSummedLayerImageIm').src = imageSrc;
			}else if (winName == 'dotplot'){
				imageDotplotZoom = 600;
				imageSrc = dirWork + pid + '_' + rid + '_1_d.png';
				document.getElementById('resDotplotLayerImageIm').src = imageSrc;
			}
		}
	}
}

function showSeqTooltipAction(action,id,start,end,e){
	if (action == 'over'){
		document.getElementById(id).src = dirImage + '16_sequenceRed_1.gif';
	}else if (action == 'out'){
		document.getElementById(id).src = dirImage + '16_sequenceBlie.gif';
	}else if (action == 'click'){
		var browser = navigator.appName;
		if (browser == "Netscape"){
			x = e.pageX;
			y = e.pageY;
		}else{
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		partSeqBeg = start;
		partSeqEnd = end;
		var pid = document.getElementById('resultPageId').value;
		var rid = document.getElementById('resultRequestId').value;
		document.getElementById('resSeqLayerHrefSave').href = dirCgiHome + 'plotrep.pl?request=3&run=5&pid=' + pid + '&rid=' + rid + '&beg=' + partSeqBeg + '&end=' + partSeqEnd;
		document.getElementById('resSeqLayer').style.left = x;
		document.getElementById('resSeqLayer').style.top = y;
		document.getElementById('resSeqLayerSeqText').innerHTML = 'Loading...';
		document.getElementById('resSeqLayer').style.visibility = 'visible';
		dragPosInit('seq',1,1,330,19);
		var querySeq = 'request=3&run=3&pid=' + pid + '&rid=' + rid + '&beg=' + start + '&end=' + end;
		chackResult(querySeq,3);
	}
}

function showAlnTooltipAction(action,id,lid,start,end,e){
	if (action == 'over'){
		document.getElementById(id).src = dirImage + '16_alignmentRed.gif';
	}else if (action == 'out'){
		document.getElementById(id).src = dirImage + '16_alignmentBlue.gif';
	}else if (action == 'click'){
		var browser = navigator.appName;
		if (browser == "Netscape"){
			x = e.pageX;
			y = e.pageY;
		}else{
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		var pid = document.getElementById('resultPageId').value;
		var rid = document.getElementById('resultRequestId').value;
		document.getElementById('resAlnLayerHrefSave').href = dirCgiHome + 'plotrep.pl?request=3&run=9&pid=' + pid + '&rid=' + rid + '&beg=' + start + '&end=' + end + '&lid=' + lid + '&platform=' + platform;
		document.getElementById('resAlnLayer').style.left = x;
		document.getElementById('resAlnLayer').style.top = y;
		document.getElementById('resAlnLayerAlnText').innerHTML = 'Loading...';
		document.getElementById('resAlnLayer').style.visibility = 'visible';
		dragPosInit('aln',1,1,490,18);
		var querySeq = 'request=3&run=8&pid=' + pid + '&rid=' + rid + '&beg=' + start + '&end=' + end + '&lid=' + lid + '&platform=' + platform;
		chackResult(querySeq,8);
	}
}

function chackResult(sendOption,queryNbr,iType){
	if (window.XMLHttpRequest) {
		var xmlhttp = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('POST', dirCgiHome + 'plotrep.pl', true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			var ret = xmlhttp.responseText;
			if (ret != 1){
				if (queryNbr == 2){
					window.clearTimeout(timer);
					document.getElementById('mainPage').innerHTML = ret;
				}else if (queryNbr == 3){
					document.getElementById('resSeqLayerSeqText').innerHTML = ret;
				}else if (queryNbr == 4){
					var pageID = document.getElementById('resultPageId').value;
					var requestID = document.getElementById('resultRequestId').value;
					var imageSrc = dirWork + pageID + '_' + requestID + '_' + imageId + '_' + imageType + '_' + imageZoom + '.png';
					if (iType == 'm'){
						document.getElementById('resImageLayerImageIm').src = imageSrc;
					}else if (iType == 'r'){
						document.getElementById('resImageLayerImageIm').src = imageSrc;
					}else if (iType == 's'){
						var imageSummedSrc = dirWork + pageID + '_' + requestID + '_1_s_' + imageSummedZoom + '.png';
						document.getElementById('resSummedLayerImageIm').src = imageSummedSrc;
					}else if (iType == 'd'){
						var imageDotplotSrc = dirWork + pageID + '_' + requestID + '_1_d_' + imageDotplotZoom + '.png';
						document.getElementById('resDotplotLayerImageIm').src = imageDotplotSrc;
					}
				}else if (queryNbr == 6){
					document.getElementById('result').innerHTML = ret;
				}else if (queryNbr == 8){
					document.getElementById('resAlnLayerAlnText').innerHTML = ret;
				}
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send(sendOption);
}

function copyClipboard(text){
	if (window.clipboardData){
		// Internet Explorer
		window.clipboardData.setData("Text", text);
		return true;
	}else{
		// Mozilla
		var url = [
				   'data:text/html;charset=utf-8;base64,PGJvZHk+PC9ib2',
				   'R5PjxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4KKGZ1',
				   'bmN0aW9uKGVuY29kZWQpe3ZhciBzd2ZfZGF0YSA9IFsKICdkYX',
				   'RhOmFwcGxpY2F0aW9uL3gtc2hvY2t3YXZlLWZsYXNoO2Jhc2U2',
				   'NCxRMWRUQjJ3JywKICdBQUFCNG5EUGdZbGpBd01qSTRNejAlMk',
				   'YlMkY5JTJGZTJaZkJnYUdhV3dNRE1uNUthJywKICdrTU10TjRH',
				   'ZGdaZ1NJTXdaWEZKYW01UUFFJTJCQm9iaTFCTG5uTXlDcFB6RW',
				   '9oU0dJJywKICdQRnAlMkZBeHNEREJRa3BGWkRGUUZGQ2d1eVM4',
				   'QXlqSTRBRVVCaXkwVndBJTNEJTNEJwpdLmpvaW4oIiIpOwpkb2',
				   'N1bWVudC5ib2R5LmlubmVySFRNTCA9IFsKICc8ZW1iZWQgc3Jj',
				   'PSInLHN3Zl9kYXRhLCciICcsCiAnRmxhc2hWYXJzPSJjb2RlPS',
				   'csZW5jb2RlZCwnIj4nLAogJzwvZW1iZWQ+JwpdLmpvaW4oIiIp',
				   'Owp9KSgi',
				   base64encode( encodeURIComponent(text) + '")</'+'script>')
		].join("");
		var tmp = document.createElement("div");
		tmp.innerHTML = [
						 '<iframe src="',url,'"',
						 ' width="0" height="0">',
						 '</iframe>'
						 ].join("");
		with(tmp.style){position ="absolute";left = "-10px";top  = "-10px";visibility = "hidden";};
		
		document.body.appendChild(tmp);
		setTimeout(function(){document.body.removeChild(tmp)},1000);
	}
}
		
function base64encode(str){
	var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
	var c1, c2, c3;
	var buf = [];
	var len = str.length;
	var i = 0;
	while(i < len){
		c1 = str.charCodeAt(i) & 0xff;
		c2 = str.charCodeAt(i+1);
		c3 = str.charCodeAt(i+2);
		buf.push(Chars[(c1 >> 2)]);
		if(i+1 == len){
			buf.push(Chars[(c1 & 0x3) << 4],"==");
			break;
		}
		buf.push(Chars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
		if(i+2 == len){
			buf.push(Chars[(c2 & 0xF) << 2],"=");
			break;
		}
		buf.push(Chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)],Chars[(c3 & 0x3F)]);
		i+=3;
	}
	return buf.join("");
}

function dragPosInit(winType,posX,posY,posW,posH){
	if (winType == 'seq'){
		dragPosSeq = [posX,posY,posW,posH];
	}else if (winType == 'img'){
		dragPosImg = [posX,posY,posW,posH];
	}else if (winType == 'aln'){
		dragPosAln = [posX,posY,posW,posH];
	}else if (winType == 'dotplot'){
		dragPosDpl = [posX,posY,posW,posH];
	}
}

function dragStart(event, id, winType){
	var el;
	var x, y;
	var posX,posY,posW,posH;
	
	// If an element id was given, find it. Otherwise use the element being clicked on.
	if (id){
		dragObj.elNode = document.getElementById(id);
	}else{
		if (browser.isIE)dragObj.elNode = window.event.srcElement;
		if (browser.isNS)dragObj.elNode = event.target;
		// If this is a text node, use its parent element.
		if (dragObj.elNode.nodeType == 3)dragObj.elNode = dragObj.elNode.parentNode;
	}
	
	// Get cursor position with respect to the page.
	if (browser.isIE){
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.isNS){
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	
	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft = parseInt(dragObj.elNode.offsetLeft, 10);
	dragObj.elStartTop = parseInt(dragObj.elNode.offsetTop, 10);
	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;
	
	if (winType == 'seq'){
		posX = dragObj.elStartLeft + dragPosSeq[0];
		posY = dragObj.elStartTop + dragPosSeq[1];
		posW = dragObj.elStartLeft + dragPosSeq[2] + dragPosSeq[0];
		posH = dragObj.elStartTop + dragPosSeq[3] + dragPosSeq[1];
	}else if (winType == 'img'){
		posX = dragObj.elStartLeft + dragPosImg[0];
		posY = dragObj.elStartTop + dragPosImg[1];
		posW = dragObj.elStartLeft + dragPosImg[2] + dragPosImg[0];
		posH = dragObj.elStartTop + dragPosImg[3] + dragPosImg[1];
	}else if (winType == 'aln'){
		posX = dragObj.elStartLeft + dragPosAln[0];
		posY = dragObj.elStartTop + dragPosAln[1];
		posW = dragObj.elStartLeft + dragPosAln[2] + dragPosAln[0];
		posH = dragObj.elStartTop + dragPosAln[3] + dragPosAln[1];
	}else if (winType == 'dotplot'){
		posX = dragObj.elStartLeft + dragPosDpl[0];
		posY = dragObj.elStartTop + dragPosDpl[1];
		posW = dragObj.elStartLeft + dragPosDpl[2] + dragPosDpl[0];
		posH = dragObj.elStartTop + dragPosDpl[3] + dragPosDpl[1];
	}
	
	if ((x >= posX) && (x <= posW) && (y >= posY) && (y <= posH)){
		
		dragObj.elNode.style.cursor = 'move';
		
		// Update element's z-index.
		dragObj.elNode.style.zIndex = ++dragObj.zIndex;
	
		// Capture mousemove and mouseup events on the page.
		if (browser.isIE){
			document.attachEvent("onmousemove", dragGo);
			document.attachEvent("onmouseup", dragStop);
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (browser.isNS){
			document.addEventListener("mousemove", dragGo, true);
			document.addEventListener("mouseup", dragStop, true);
			event.preventDefault();
		}
	}
}

function dragGo(event){
	var x, y;
	
	// Get cursor position with respect to the page.
	if (browser.isIE){
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.isNS){
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	
	// Move drag element by the same amount the cursor has moved.
	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
	
	if (browser.isIE){
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS){
		event.preventDefault();
	}
}

function dragStop(event){
	dragObj.elNode.style.cursor = 'default';
	
	// Stop capturing mousemove and mouseup events.
	if (browser.isIE){
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup", dragStop);
	}
	if (browser.isNS){
		document.removeEventListener("mousemove", dragGo, true);
		document.removeEventListener("mouseup", dragStop, true);
	}
}





function searchButtonAction(action,button,buttonId){
	if (action == 'over'){
		document.getElementById(buttonId).className = 'searchButtonOthetOver';
	}else if (action == 'out'){
		if (button == 'submit'){
			document.getElementById(buttonId).className = 'searchButtonSubmitOut';
		}else{
			document.getElementById(buttonId).className = 'searchButtonOtherOut';
		}
	}else if (action == 'click'){
		if (button == 'submit'){
			requestFormSubmit();
		}else if (button == 'clearQuery'){
			document.plotrepSearchForm.queryFile.value = '';
			document.plotrepSearchForm.queryText.value = '';
			document.plotrepSearchForm.queryText.focus();
		}else if (button == 'clearRef'){
			document.plotrepSearchForm.libFile.value = '';
			document.plotrepSearchForm.libText.value = '';
			document.plotrepSearchForm.libText.focus();
		}else if (button == 'reset'){
			document.getElementById('libReference').style.display = 'block';
			document.getElementById('libRepBase').style.display = 'none';
			document.getElementById('libTigr').style.display = 'none';
			document.plotrepSearchForm.reset();
			document.getElementById('pageId').value = document.getElementById('pageMainId').value;
			document.getElementById('libType').disabled = false;
			document.getElementById('optBlastWordSize').disabled = true;
			document.getElementById('optBlastExpect').disabled = true;
			document.getElementById('optBlastFilter').disabled = true;
			document.getElementById('optBlastPenalGap').disabled = true;
			document.getElementById('optBlastPenalExt').disabled = true;
			document.getElementById('optBlastMatrix').disabled = true;
			document.getElementById('optCensorMode').disabled = false;
		}
	}
	
}

function changeMethod(){
	var methodIndex = document.getElementById('searchMethod').selectedIndex;
	if (methodIndex == 0){
		document.getElementById('libType').disabled = false;
		document.getElementById('optBlastWordSize').disabled = true;
		document.getElementById('optBlastExpect').disabled = true;
		document.getElementById('optBlastFilter').disabled = true;
		document.getElementById('optBlastPenalGap').disabled = true;
		document.getElementById('optBlastPenalExt').disabled = true;
		document.getElementById('optBlastMatrix').disabled = true;
		document.getElementById('optCensorMode').disabled = false;
	}else if (methodIndex == 1){
		document.getElementById('libType').disabled = false;
		document.getElementById('optBlastWordSize').disabled = false;
		document.getElementById('optBlastExpect').disabled = false;
		document.getElementById('optBlastFilter').disabled = false;
		document.getElementById('optBlastPenalGap').disabled = false;
		document.getElementById('optBlastPenalExt').disabled = false;
		document.getElementById('optBlastMatrix').disabled = true;
		document.getElementById('optCensorMode').disabled = true;
		document.getElementById('optBlastWordSize').value = 7;
	}else if (methodIndex == 2){
		document.getElementById('libType').options[0].selected = true;
		document.getElementById('libType').disabled = true;
		document.getElementById('optBlastWordSize').disabled = false;
		document.getElementById('optBlastExpect').disabled = false;
		document.getElementById('optBlastFilter').disabled = false;
		document.getElementById('optBlastPenalGap').disabled = true;
		document.getElementById('optBlastPenalExt').disabled = true;
		document.getElementById('optBlastMatrix').disabled = false;
		document.getElementById('optCensorMode').disabled = true;
		document.getElementById('optBlastWordSize').value = 3;
		document.getElementById('libReference').style.display = 'block';
		document.getElementById('libRepBase').style.display = 'none';
		document.getElementById('libTigr').style.display = 'none';
	}
}
