/**
* Function reads every checked station's id-value from the table inside second page's iframe and writes
* the id-values to a hidden element.
*/
function gatherSelectedStations(){


	var frm=window.frames;
		
	var boxes=frm[0].document.getElementsByName('ss');
	var selectedStations="";
	for(i=0;i<boxes.length;i++){
	
		if(boxes[i].checked)
			selectedStations=selectedStations+boxes[i].value+",";
	
	}
	selectedStations=selectedStations.substr(0,selectedStations.length-1);
	
//	alert(selectedStations);
	document.getElementById('selectedStation').value=selectedStations;
	
}

/**
* Function sets every pages hidden action-inputfield's value and submits the page 
*/
function updatePage(action,formId){

	var form;
	//alert('dd'+action);
	if(document.getElementById('action')==null){
		
		form=parent.document.getElementById(formId);
		parent.document.getElementById('action').value=action;
	}
	else{
		
		form=document.getElementById(formId);
		document.getElementById('action').value=action;		
	}
	
	var scrollXValue=document.createElement("input");scrollXValue.type="hidden";scrollXValue.name="CM_SCROLL_X";
	var scrollYValue=document.createElement("input");scrollYValue.type="hidden";scrollYValue.name="CM_SCROLL_Y";
	
	if(typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
	    scrollYValue.value = window.pageYOffset;
	    scrollXValue.value = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrollYValue.value = document.body.scrollTop;
	    scrollXValue.value = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrollYValue.value = document.documentElement.scrollTop;
	    scrollXValue.value = document.documentElement.scrollLeft;
	}
	
	form.appendChild(scrollXValue);
	form.appendChild(scrollYValue);
	form.submit();

}

/**
* Sets web-application's second page's hidden selectedStation-inputfield's value
*/
/*function selectStation(statId){

	parent.document.getElementById('selectedStation').value=statId;
	
	
}*/

/**
* Checks or unchecks all the station checkboxes inside the second page's iframe
*/
function selectAllStations(){

	var box=document.getElementById('all');
	var c;
	if(box.checked)
		c=true;
	else
		c=false;
	
	var boxes=window.frames[0].document.getElementsByName('ss');
	
	for(i=0;i<boxes.length;i++){
	
		boxes[i].checked=c;
	}
	//alert('toimiko');
	
	/*if(box.checked)
		selectStation('addall');
	else
		selectStation('clearall');*/

}

var SORT_COLUMN_INDEX;
var DOWN=new Array(false,false,false,false,false,false);

/**
* Used to sort table columns with string-data. Contains few exception like when columns contain 
* string-data and input-elements.
* @param a First compared item
* @param b Second compared item
*/
function mySortString(a,b) {
    aa = a.cells[SORT_COLUMN_INDEX].innerHTML;
    bb = b.cells[SORT_COLUMN_INDEX].innerHTML;
    
   // alert(aa.indexOf("<br>"));
    if(aa.indexOf("<br>")!=-1)
    	aa=aa.replace("<br>","");
    if(bb.indexOf("<br>")!=-1)
    	bb=bb.replace("<br>","");
    	
    //alert(aa);
    if(aa.indexOf(">")!=-1)
		aa=aa.split(">")[1];
	if(bb.indexOf(">")!=-1)
		bb=bb.split(">")[1];
		
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

/**
* Used to sort table columns with numeric data.
* @param a First compared item
* @param b Second compared item
*/
function mySortNumeric(a,b) { 
    aa = parseFloat(a.cells[SORT_COLUMN_INDEX].innerHTML);
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(b.cells[SORT_COLUMN_INDEX].innerHTML); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

/**
* Used to sort table columns with date-data.
* @param a First compared item
* @param b Second compared item
*/
function mySortDate(a,b) {
    aString = a.cells[SORT_COLUMN_INDEX].innerHTML;
    bString = b.cells[SORT_COLUMN_INDEX].innerHTML;
   
    aString=aString.replace("&nbsp;"," "); 
    bString=bString.replace("&nbsp;"," "); 
    
    aa=new Date();

    aa.setFullYear(aString.substr(6,4),aString.substr(3,2),aString.substr(0,2));
    
    if(aString.length>10)
	    aa.setHours(aString.substr(11,2));
    
    bb=new Date();
    
    bb.setFullYear(bString.substr(6,4),bString.substr(3,2),bString.substr(0,2));
   
    if(bString.length>10)
	    bb.setHours(bString.substr(11,2));
    
    //aa=aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2)+aa.substr(11,2);
    //bb=bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2)+bb.substr(11,2);    
    
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

/**
* Sorts html table according to one of it's columns
* @param id The id-value of the table
* @param index Which column is used to sort the table
* @param begin From which row the sort should start
* @param forceUp This variable forces the sort direction to up
*/
function sortTable(id, index,begin,forceUp){

	
//	alert('toimiiko');
	var down=DOWN[index];
	
	
	if(forceUp.length>0)
		down=false;
	
	if(down)
		DOWN[index]=false;
	else
		DOWN[index]=true;
	
	SORT_COLUMN_INDEX=index;
	
	var table=document.getElementById(id);
	
	if(table==null)
		table=window.frames[0].document.getElementById(id);
	
	//alert(table);
	//alert(table.rows[6].className);
	
	var newRows = new Array();
	for (j=begin;j<table.rows.length;j++) { newRows[j-begin] = table.rows[j];}
	
	var value=newRows[0].cells[SORT_COLUMN_INDEX].innerHTML;
	
	//alert(value+" "+value.match(/^\d\d[\/.]\d\d[\/.]\d\d\d\d/));
	
	//Find out the type of the column...
	if (value.match(/^\d\d[\/.]\d\d[\/.]\d\d\d\d/)){
//		alert('date'+' '+value.match(/^\d\d[\/.]\d\d[\/.]\d\d\d\d/));
		newRows.sort(mySortDate);
	}else if(!isNaN(parseFloat(value))){
	//	alert('numeric'+parseFloat(value));
		newRows.sort(mySortNumeric);
	}else{
		//alert('string');
		newRows.sort(mySortString);
	}
	
	
	if(!down)
		newRows.reverse();
		
	var i=0;
	for (a=0;a<newRows.length;a++){
		
		if(i%2==0)
			newRows[a].className="";
		else
			newRows[a].className="harmaatausta";
			
		i++;
	}	
	
	for (i=0;i<newRows.length;i++) { table.tBodies[0].appendChild(newRows[i]);}	
	
	
}

function showPlot(id,force){

	var frame=parent.measurementFrame;
	var plot=frame.document.getElementById(id);
	
	if(force.length>0)
		plot.style.visibility=force;
	else{
		if(plot.style.visibility!='hidden')
			plot.style.visibility='hidden';
		else
			plot.style.visibility='visible';
	}
}

function showAllPlots(){


	var mainBox=document.getElementById('plot-1');
	var check=false;
	
	check=mainBox.checked;
	
	var force="hidden";
	if(check)
		force="visible";
		
	var boxes=document.getElementsByName('plotBox');

	for(i=0;i<boxes.length;i++){
	
		boxes[i].checked=check;
		showPlot('plot'+(i),force);
	}
}
