/* 
   @function getElementsByClassName()
   To use:

   o  To get all a elements in the document with a info-links class.
       getElementsByClassName(document, "a", "info-links");

   o  To get all div elements within the element named container, with a col class.
       getElementsByClassName(document.getElementById("container"), "div", "col"); 

   o  To get all elements within in the document with a click-me class.
       getElementsByClassName(document, "*", "click-me"); 

*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

/* 
    @function addLoadEvent()
    To use:

    o Named function
	addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); Note: WITHOUT () on function name!

    o Anonymous function
	addLoadEvent(function() { });
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
} // End function addLoadEvent

function display_block(id) {
    obj = document.getElementById(id);
    obj.style.display = 'block';
}

function highlightCell(c) {
    cell = document.getElementById(c);
    cell.style.backgroundColor = '#66c';
    cell.style.color = 'white';
}
function unhighlightCell(c) {
    cell = document.getElementById(c);
    cell.style.backgroundColor = 'transparent';
    cell.style.color = 'black';
}

function get_option_value(select) {
    for(var i=0; i<select.childNodes.length; i++)
    {
	option = select.childNodes[i];
	if (option.selected){
	    return option.value;
	}
    }
}

function get_value(id) {
    var e = document.getElementById(id)
    if ( e != null ) {
	return e.value;
    }
    else {
	return null;
    }
}

function get_selected_datetime() {
    var year = get_option_value(document.getElementById('id_date_year'));
    var month = get_option_value(document.getElementById('id_date_month'));
    var day = get_option_value(document.getElementById('id_date_day'));
    var hour = get_option_value(document.getElementById('id_date_hour'));
    var minute = get_option_value(document.getElementById('id_date_minute'));
    if ( hour == '2359' ) {
	hour = '23'; minute = '59';
    }
    return new Date(year,month-1,day,hour,minute);
}

function disable_observation_date() {
    var f = document.getElementById('observation_date_controls');
    f.style.display = "none";
}

function submit_login() {
    var login_form = document.getElementById('login_form');
    var login_form_username = document.getElementById('login_form_username');
    var login_form_accesscode = document.getElementById('login_form_accesscode');
    var username = document.getElementById('username');
    var password = document.getElementById('password');

    if (username.value == "") {
	alert('Please enter your username.');
	username.focus();
	return false;
    }
    if (password.value == "") {
	password.focus();
	return false;
    }

    var submit_button = document.getElementById('id_login_submit');
    if ( submit_button ) {
	submit_button.value = "Signing in ...";
    }
    login_form_username.value = username.value;
    login_form_accesscode.value = password.value;
    login_form.submit();
    return false;
}



function toggle_form_table_row(row,TorF) {
    if (row && row.cells ) {
	var day = get_day_from_row(row);
	for( var i=0; i< row.cells.length; i++ ) {
	    var cell = row.cells[i];
	    var childLength = cell.childNodes.length;
	    var firsttime = true;
	    for (var j=0; j<childLength; j++) {
		var _node = cell.childNodes[j];
		if (_node.nodeType == 1){
		    var isLocked = TorF;
		    if(isLocked == null) { 
			isLocked = !_node.disabled; 
			if ( firsttime ) {
			    firsttime = false; // only change the lock once
			    var lock_img = document.getElementById('lock_day_'+day);
			    if (isLocked) { 
				lock_img.src='/wxcoder/media/img/lock.gif'; 
			    }
			    else { 
				lock_img.src='/wxcoder/media/img/unlock.gif'; 
			    }
			}
		    }
		    _node.disabled = isLocked;
		}
	    }
	}
    }
}

/* function toggle_form_table_rows
 Disable any form elements found within table's first and last row.
 First defaults to 0. Last defaults to table's last row.
*/
function toggle_form_table_rows(TorF,table,first,last) {
    if(table && table.rows && table.rows.length){
	var first = ( first == null ) ? 0 : first;
	var last = ( last == null ) ? table.rows.length : last;

	for(var i=first; i<last; i++){
	    if (table.rows[i].className != 'error_row'){
		toggle_form_table_row(table.rows[i],TorF);
	    }
	}
    }
}


function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
	windowHeight=window.innerHeight;
    }
    else {
	if (document.documentElement &&
		document.documentElement.clientHeight) {
	    windowHeight = document.documentElement.clientHeight;
	}
	else {
	    if (document.body && document.body.clientHeight) {
		windowHeight = document.body.clientHeight;
	    }
	}
    }
    return windowHeight;
}

function getScrollTop() {
    var scrollTop = 0;
    if ( document.documentElement &&
	    document.documentElement.scrollTop) {
	scrollTop = document.documentElement.scrollTop;
    }
    else if ( document.body && document.body.scrollTop ){
	scrollTop = document.body.scrollTop;
    }

    return scrollTop;
}

function display_help_text(obj) {
    var help_text_box = document.getElementById('help_text');
    var new_help_text = document.getElementById('text_'+obj.id);
    help_text_box.innerHTML = new_help_text.innerHTML;
    setHelpBox();
}

function display_help_text_new_window(obj) {
    var elemID = obj.id.split('_');
    var elemID = elemID[elemID.length - 1];
    var specs = "directories=0, location=0, menubar=0, status=0, scrollbars=1, resizable=1, width=600, height=450";
    var url = "/wxcoder/observations/element_help/?e="+elemID;
    window.open(url,'_wxcoder_help',specs);
}

function setHelpBox() {
    if (document.getElementById) {
	if (getWindowHeight()>0) {
	    var helpbox = document.getElementById('id_helpbox');
	    var headerHeight = document.getElementById('header').offsetHeight;
	    var newtop = headerHeight + getScrollTop();
	    if (newtop>=0) {
		helpbox.style.position = 'relative';
		helpbox.style.top = (newtop-headerHeight)+'px';
	    }
	    else {
		helpbox.style.position='static';
	    }
	}
    }
}

function enableHelperLinks(){
	/* Get all helper links and give them a click event */
	if ( document.getElementsByTagName ) {
	    var helpers = getElementsByClassName(document, "a", "helper_link"); 
	    for ( var i=0; i < helpers.length; i++ ) {
		helpers[i].onclick = function(){ return display_help_text(this); };
	    }

	    var helpers = getElementsByClassName(document, "a", "helper_link_new_window"); 
	    for ( var i=0; i < helpers.length; i++ ) {
		helpers[i].onclick = function(){ return display_help_text_new_window(this); };
	    }
	}
}

function get_selected(select){
    return select.options[select.selectedIndex].value;
}

