function setEditorMid(){
    document.getElementById('queryCell').height= '35%';
    document.getElementById('resultsCell').height= '65%';
    document.getElementById('queryRow').style.display = '';
    document.getElementById('resultsRow').style.display = '';
}
function maxQuery(){
    document.getElementById('queryCell').height = '100%';   
    document.getElementById('queryRow').style.display = '';
    document.getElementById('resultsRow').style.display = 'none';
}
function maxResults(){
    document.getElementById('resultsCell').height = '100%';   
    document.getElementById('queryRow').style.display = 'none';
    document.getElementById('resultsRow').style.display = '';
}
var time;
var doTime;
var messageLength;
var messagePos;
var noiseStart = '<span style="font-weight: bold;">';
var noiseEnd = '</span>';
var noiseIndex;
var load_msg;
function startTimer(load_msg_){
    time = 0;
    load_msg = load_msg_;
    noiseIndex = 0;
    doTime = true;
    doTick();
    st = document.getElementById('message')
    dissipateb(1);
    document.getElementById('message').innerHTML = load_msg;
}
function stopTimer(){
  doTime = false;
  if(st){
      st.style.opacity = 1;
      st.style.filter = 'alphab(opacity=100)';
  }
}
function get_random(cap){
    var ranNum= Math.floor(Math.random()*cap);
    return ranNum;
}
var st;
function solidifyb(s){
    if(doTime){
      s = parseFloat(s);
      st.style.opacity = s;
      st.style.filter = 'alpha(opacity='+(s*100)+')';
      if(s >= 1){dissipateb(1); return;}
      var z = 'solidifyb(\''+(s+.1)+'\')';
      window.setTimeout(z, 100);
    }
}
function dissipateb(s){
    if(doTime){
      s = parseFloat(s);
      st.style.opacity = s;
      st.style.filter = 'alpha(opacity='+(s*100)+')';
      if(s <= 0){solidifyb(.1); return;}
      var z = 'dissipateb(\''+(s-.1)+'\')';
      window.setTimeout(z, 100);
    }
}
function doTick(){
    document.getElementById('timer').innerHTML = '['+time+' s]';
    if(doTime)window.setTimeout('doTick()', 1000);
    time++;
}
function submitQuery(){
   document.getElementById('queue').value = 500;
   document.getElementById('syntax').value = 'false';
   sendQuery(document.forms["SubmitJob"], 'submitjobhelper.aspx');
}
function submitQuickQuery(){
   queryIsRunning = true;
   hideExecButtons();
   startTimer('Executing query, please wait...');
   document.getElementById('queue').value = 1;
   document.getElementById('syntax').value = 'false';
   sendQuery(document.forms["SubmitJob"], 'submitjobhelper.aspx');
}
function hideExecButtons(){
    if(document.getElementById('syntaxButton'))document.getElementById('syntaxButton').disabled = true;
    if(document.getElementById('planButton'))document.getElementById('planButton').disabled = true;
    if(document.getElementById('quickButton'))document.getElementById('quickButton').disabled = true;
    if(document.getElementById('submitButton'))document.getElementById('submitButton').disabled = true;   
}
function showExecButtons(){
    if(document.getElementById('syntaxButton'))document.getElementById('syntaxButton').disabled = false;
    if(document.getElementById('planButton'))document.getElementById('planButton').disabled = false;
    if(document.getElementById('quickButton'))document.getElementById('quickButton').disabled = false;
    if(document.getElementById('submitButton'))document.getElementById('submitButton').disabled = false; 
}
function checksyntax(){
    document.getElementById('syntax').value = 'true';
    startTimer('Checking syntax, please wait...');
    sendSyntax(document.forms["SubmitJob"],'submitjobhelper.aspx');
}
function getplan(){
    saveSubmitPage();
	window.open('qplan/viewplan.aspx?wsid='+document.getElementById('wsid').value+'&target='+getTarget()+'&sql='+getqry(),'_top','');
	//'http://localhost/casjobs/qplan/viewplan.aspx?wsid=1759332931&target=dr5&sql=select 0
}
function getTarget(){
	var ddl = document.getElementById("targetDDL");
	return ddl[ddl.selectedIndex].text;
}
function getqry(){
	var sql = window.document.getElementById("sql").value;
	while(sql.indexOf('+') > -1)
		sql = sql.replace('+','P_L_U_S');
	return escape(sql);
}
function setPosition() {
	ohIE();
	var ta = document.getElementById('sql');
	var txt = ta.value.substring(0,ta.selectionStart);
	row=1;col=1;
	var current = -1;
	var oldtxt = null;
	for(;;){
		current = txt.indexOf('\n');
		col=txt.length;		
		if(current == -1)break;
		txt=txt.substring(current+1,txt.length);
		++row;
	}
	col+=1;
	document.getElementById('rowLabel').childNodes[0].nodeValue = row;
	document.getElementById('colLabel').childNodes[0].nodeValue = col;
}
function setQuery(id){
    hideFloats();
    var qry = document.getElementById(id).value;
    document.getElementById('sql').value = qry ;
}
function hideFloats(){
    document.getElementById('samples').style.display = 'none';
    document.getElementById('hist').style.display = 'none';
    cjhideqs();
    cjhideqsM();
}
function toggleSamples(){
   var samp = document.getElementById('samples');
   if(samp.style.display == 'none'){
        samp.style.display = 'block'
        document.getElementById('hist').style.display = 'none';
        positionSamples();
    }else{
      samp.style.display= 'none';
    }
}
function toggleHist(){
   var hist = document.getElementById('hist');
   if(hist.style.display == 'none'){
        hist.style.display = 'block'
        document.getElementById('samples').style.display = 'none';
        positionHist();
    }else{
      hist.style.display= 'none';
    }
}
function positionSamples(){
   var samp = document.getElementById('samples');
   tmp = findPos(document.getElementById('sql'));
   samp.style.top = tmp[1];
   tmp = findPos(document.getElementById('sampCheck'));
   samp.style.left = tmp[0];
}
function positionHist(){
   var hist = document.getElementById('hist');
   tmp = findPos(document.getElementById('sql'));
   hist.style.top = tmp[1];
   tmp = findPos(document.getElementById('histCheck'));
   hist.style.left = tmp[0];
}
/*
 * findPos
 * from http://www.quirksmode.org/js/findpos.html
 */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
var lastNode;
function cjshowqs(node,e){ 
       var samp = document.getElementById('samples');
       tmp = findPos(samp);
       var n = document.getElementById(node);
       n.style.top = findPos(e)[1];
       n.style.left = samp.offsetWidth + tmp[0];
       n.style.visibility = 'visible';
       n.style.display = '';
       lastNode = n;
}
var oldHeight;
function cjshowqsM(){
    var m = document.getElementById('message');
    if(m.innerHTML == null || m.innerHTML == '')return;
    try{
        oldHeight = parseInt(m.style.height.replace('p','').replace('x',''));
    }catch(never){
        return;
    }
    m.style.height = null;
    if(m.offsetHeight <= oldHeight){
        m.style.height = oldHeight;        
        return;
    }
    var pos = findPos(m);  
    m.style.left = pos[0];
    m.style.top = pos[1];
    m.style.width = m.offsetWidth-5;
    m.style.height = null;
    m.style.position = 'absolute';
}
function cjhideqsM(){
    if(oldHeight)
        document.getElementById('message').style.height = oldHeight;
}
function cjhideqs(node){
    if(lastNode)
        lastNode.style.display = 'none';    
}
/**
  sets selectionStart in IE.  From:
		http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/
*/
var row, col;
function ohIE(){
	var element = document.getElementById( 'sql' );
	if( document.selection ){
		// The current selection
		var range = document.selection.createRange();
		// We'll use this as a 'dummy'
		var stored_range = range.duplicate();
		// Select all text
		stored_range.moveToElementText( element );
		// Now move 'dummy' end point to end point of original range
		stored_range.setEndPoint( 'EndToEnd', range );
		// Now we can calculate start and end points
		element.selectionStart = stored_range.text.length - range.text.length;
		element.selectionEnd = element.selectionStart + range.text.length;
	}
}
function clearQry(){
    if(confirm('Really clear query?'))document.getElementById("sql").value = '';
}
function save(){
	var type = document.getElementById('ddl').options[
		document.getElementById('ddl').selectedIndex].text;
	window.frames['resultPane'].location = 'output/vodataclean.aspx?type='+type+'&save=true';
}