function setStatus(s){
    document.getElementById('status').style.display = 'block';
    document.getElementById('statusM').innerHTML = s;
}
function clearStatus(s){
   document.getElementById('status').style.display = 'none';
}
function loadSample(wsid,context,objName,objtype){
    var xhr = null;    
    setStatus('Fetching sample');
    document.getElementById('previewPane').innerHTML = '';
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = function(){showSample(xhr);};
    }else{
       xhr = new XMLHttpRequest();
       xhr.onload = function(){showSample(xhr);};
    }
    xhr.open( "POST", 'mydbcontenthelper.aspx', true );
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    var params = 'wsid='+wsid+'&objname='+objName+'&context='+context+'&objtype='+objtype+'&action=sample';
    xhr.send(params);
    return xhr;
}
var sData,sTab;
function showSample(xhr){ 
    setStatus('Initializing sample');
    if (xhr.readyState == 4) {
        if(xhr.responseXML.getElementsByTagName('message')[0].firstChild){
            document.getElementById('message').innerHTML = URLDecode(xhr.responseXML.getElementsByTagName('message')[0].firstChild.data);
            var mtype = xhr.responseXML.getElementsByTagName('message')[0].attributes.getNamedItem("mtype").value;
            if(mtype == 'happy'){
                document.getElementById('message').className = 'happy';
            }else{
                document.getElementById('message').className = 'message';
                clearStatus();
                return;
            }
        }
        sData = xhr.responseXML.getElementsByTagName('sampleData')[0].getElementsByTagName('tr');
        sTab = document.createElement("table");
        sTab.setAttribute('cellpadding',0);
        sTab.setAttribute('cellspacing',0);
        sTab.className = 'msampTab';
        document.getElementById('previewPane').appendChild(sTab);
        renderRow(0);
        return;
    }
}
function renderRow(rNum){
    setStatus('Drawing rows '+parseInt((rNum/sData.length)*100)+"%");
    if(rNum >= sData.length || 
        (rNum != 0 && document.getElementById('previewPane').style.display == 'none')){
        clearStatus();
        return;
    }
    var tr = sTab.insertRow(rNum);
    if(rNum % 2 == 0){
        tr.className = 'alt';
    }
    var tds = rNum == 0?sData[rNum].getElementsByTagName('th'):sData[rNum].getElementsByTagName('td');
    var td;
    for(i=0;i<tds.length;i++){
        td = tr.insertCell(i);
        td.innerHTML = URLDecode(tds[i].firstChild.data);
        if(rNum == 0){
            td.style.fontWeight = 'bold';
        }
    }
    window.setTimeout('renderRow('+(rNum+1)+')', 2);
}
function loadSchema(wsid, context, objName, objtype){   
    var xhr = null;    
    setStatus('Fetching schema');
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = function(){showSchema(xhr);};
    }else{
       xhr = new XMLHttpRequest();
       xhr.onload = function(){showSchema(xhr);};
    }
    xhr.open( "POST", 'mydbcontenthelper.aspx', true );
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    var params = 'wsid='+wsid+'&objname='+objName+'&context='+context+'&objtype='+objtype+'&action=schema';
    xhr.send(params);
    return xhr;
}
var cnames, ctypes, tr;
function showSchema(xhr){ 
    setStatus('Processing schema');
    if (xhr.readyState == 4) {
        if(xhr.responseXML.getElementsByTagName('message')[0].firstChild){
            document.getElementById('message').innerHTML = URLDecode(xhr.responseXML.getElementsByTagName('message')[0].firstChild.data);
            var mtype = xhr.responseXML.getElementsByTagName('message')[0].attributes.getNamedItem("mtype").value;
            if(mtype == 'happy'){
                document.getElementById('message').className = 'happy';
            }else{
                document.getElementById('message').className = 'message';
                clearStatus();
                return;
            }
        }
        var scnames, sctypes;
        cnames = allText(xhr.responseXML.getElementsByTagName('columnNames')[0]).split('\n');
        ctypes = allText(xhr.responseXML.getElementsByTagName('columnTypes')[0]).split('\n');
        tr = document.getElementById('schemaRow');
        document.getElementById('schemaDiv').style.width = document.getElementById('schemaDiv').offsetWidth;    
        document.getElementById('schemaDiv').style.height = 52;
        document.getElementById('schemaDiv').style.overflow = 'auto';    
        renderSchemaCell(0);
    }
}
//for handling mozilla 4096 text node char limit
function allText(node){
    var rst = '';
    for(i=0;i<node.childNodes.length;i++)
        rst = rst+node.childNodes[i].data;
    return rst;
}
var ra = false, dec = false;
function renderSchemaCell(i){
    setStatus('Drawing schema '+parseInt((i/cnames.length)*100)+'%');
    if(cnames == null || cnames.length == 0 || i >= cnames.length){
        clearStatus();
        if(ra && dec){
            document.getElementById('neighborsYes').style.display = 'block';
            document.getElementById('neighborsNo').style.display = 'none';
        }
        return;
    }
    if(cnames[i] == 'ra')ra = true;
    if(cnames[i] == 'dec')dec = true;
    
    var x=tr.insertCell(tr.cells.length)
    x.className = 'rw';

    var obj = document.createElement("input");
    obj.setAttribute('id', 'COL'+i);
    obj.setAttribute('name', 'COL'+i);
    obj.setAttribute('type', 'text');
    obj.setAttribute('value', cnames[i]);
    obj.className = 'rw';
    x.appendChild(obj);

    obj = document.createElement('input');
    obj.setAttribute('id', 'COL'+i+'ORIG');
    obj.setAttribute('name', 'COL'+i+'ORIG');
    obj.setAttribute('type', 'hidden');
    obj.setAttribute('value', cnames[i]);
    x.appendChild(obj);

    obj = document.createElement('br');
    x.appendChild(obj);

    obj = document.createElement('font'); 
    try{
    obj.className = ctypes[i].replace(']','').replace('[','');
    obj.innerHTML = ctypes[i];       
    }catch(err){}
    x.appendChild(obj);

    obj = document.createElement('div'); 
    obj.setAttribute('id','ccblock_'+i);
    obj.className = 'hideMe';

    var o1 = document.createElement('p');
    o1.innerHTML = 'Upper Limit';
    obj.appendChild(o1);

    o1 = document.createElement('input');
    o1.setAttribute('type', 'text');
    o1.setAttribute('id','upper_'+i);
    o1.className = 'rw';
    obj.appendChild(o1);

    var o1 = document.createElement('p');
    o1.innerHTML = 'Lower Limit';
    obj.appendChild(o1);

    o1 = document.createElement('input');
    o1.setAttribute('type', 'text');
    o1.setAttribute('id','lower_'+i);
    o1.className = 'rw';
    obj.appendChild(o1);

    o1 = document.createElement('input');
    o1.setAttribute('class', 'tabth');
    o1.setAttribute('type', 'radio');
    o1.setAttribute('id','xcolr_'+i);
    o1.setAttribute('name','xcolr');
    obj.appendChild(o1);

    o1 = document.createElement('label');
    o1.setAttribute('for','xcolr_'+i);
    o1.innerHTML = '&nbsp;X';
    obj.appendChild(o1);

    o1 = document.createElement('br');
    obj.appendChild(o1);

    o1 = document.createElement('input');
    o1.setAttribute('class', 'tabth');
    o1.setAttribute('type', 'radio');
    o1.setAttribute('id','ycolr_'+i);
    o1.setAttribute('name','ycolr');
    obj.appendChild(o1);

    o1 = document.createElement('label');
    o1.setAttribute('for','ycolr_'+i);
    o1.innerHTML = '&nbsp;Y';
    obj.appendChild(o1);

    x.appendChild(obj);               
    window.setTimeout('renderSchemaCell('+(i+1)+')', 5);
}
var gCols, gstat=0;//0:nothing, 1 open, -1 close
//-1 hide all, 0 plot, 1 index
function tabToolsSet(t){
    var tab = document.getElementById('schemaDiv');
    gCols = tab.getElementsByTagName('div');
	switch(t){
		case 0:
            gstat = 1;
            document.getElementById('schemaDiv').style.height = 153;
            exCol(0);
		break;
		default:
		    gstat = -1;
		    document.getElementById('schemaDiv').style.height = 52;
            cpCol(0);
		break;
	}
}
function exCol(i){
    setStatus('Drawing plot interface');
    if(!gCols[i] || gstat != 1 || gCols[i].className =='showMe'){
        clearStatus();
        return;
    }
    gCols[i].className = 'showMe';
    if(i < gCols.length){
        i++;
        window.setTimeout('exCol('+i+')', 50);
    }else gstat = 0;
}
function cpCol(i){
    setStatus('Removing plot interface');
    if(!gCols[i] || gstat != -1 || gCols[i].className =='hideMe'){
        clearStatus();
        return;
    }
    gCols[i].className = 'hideMe';
    if(i < gCols.length){
        i++;
        window.setTimeout('cpCol('+i+')', 5);
    }else gstat = 0;
}