// Sets all of a form's checkboxes of the same name to checked or unchedked
//   form_name: string
//   check_name: string
//   checked: 1 for checked, 0 for unchecked
function setCheckboxes(form_name, check_name, checked)
{
	var elts;
	if (typeof(document.forms[form_name].elements[check_name])=='undefined') {
		return false;
	}
	elts = document.forms[form_name].elements[check_name];
	var elts_cnt  = (typeof(elts.length) != 'undefined')
		? elts.length
		: 0;

	if (elts_cnt) {
		for (var i = 0; i < elts_cnt; i++) {
			elts[i].checked = checked;
		}
	} else {
		elts.checked = checked;
	}

	return true;
}

// Return the markup+text for a 'toggle' link
// form_name: string
// check_name: string
// checked: 1 for check, 0 for uncheck
// link text: text to include in <a> tags

function toggleCheckLink(form_name, check_name, checked, link_text)
{
	retstr = "<a href=\"#\" onclick=\"javascript:setCheckboxes(\'"+form_name+"\',\'"+check_name+"\',"+checked+"); return false;\">";
	if(!link_text) {
		if(checked == 1) { link_text = "check all"; }
		else { link_text = "uncheck all"; }
	}
	retstr += link_text + '</a>';
	return retstr;
}


// Set the background color of all the cells in a table row
//    elm: a table row element object or any object nested in a table row
//    color: string (ex '#aabbcc') background color to set or '' to use default
function setRowBackground(elm, color)
{
	var tr_elm = elm;
	// navigate up the tree until we find the tr
	while (tr_elm && tr_elm.tagName != 'TR') {
		tr_elm = tr_elm.parentNode;
	}
	if (tr_elm) {
		// set background color of all th an td child elements
		for (var elm_child = tr_elm.firstChild; elm_child; elm_child = elm_child.nextSibling) {
			if (elm_child.tagName == 'TD' || elm_child.tagName == 'TH') {
				elm_child.style.backgroundColor = color;
			}
		}
	}
	return true;
}

function makeDeselect(select_id,link_text)
{
	if(!link_text) {
		link_text = "deselect all";
	}
	return '<a onclick=\"javascript:sc=document.getElementById(\''+select_id+'\'); for(i=0;i<sc.length;i++) sc.options[i].selected=false; return false;\" href=\"#\">'+link_text+'</a>';
}

// Generates script for setting a value to a text input form element via a link onclick
// Return string only suitable for passing to document.write()
function makeSetInput(input_id,text_value,link_text)
{
	return '<a onclick=\"javascript:sc=document.getElementById(\''+input_id+'\'); sc.value=\''+text_value+'\'; return false;\" href=\"#\">'+link_text+'</a>';
}

function changePage(newLoc)
{
    nextPage = newLoc.options[newLoc.selectedIndex].value;
    if (nextPage != "") {
        document.location.href = nextPage;
    }
}

function getElementTextNS(prefix, local, parentElem, index) {
	var result = '';
	if (prefix && isIE) {
		// IE/Windows way of handling namespaces
		result = parentElem.getElementsByTagName(prefix + ':' + local)[index];
	} else {
		// the namespace versions of this method 
		// (getElementsByTagNameNS()) operate
		// differently in Safari and Mozilla, but both
		// return value with just local name, provided 
		// there aren't conflicts with non-namespace element
		// names
		result = parentElem.getElementsByTagName(local)[index];
	}
	if (result && result.childNodes.length > 0) {
		// get text, accounting for possible
		// whitespace (carriage return) text nodes 
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;
		} else {
			return result.firstChild.nodeValue;    		
		}
	} else {
		return '';
	}
}

function validate_description(description)
{
	var xmlhttp=false;

	// Native XMLHttpRequest
	if (window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
      if (xmlhttp.overrideMimeType) {
        xmlhttp.overrideMimeType('text/xml');
        // See note below about this line
      }
		} catch(e) {
			xmlhttp = false;
		}

	// IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (E) {
				xmlhttp = false;
			}
		}
	}

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			// If stuff has returned, and it was an OK response
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var el;
				if (el = description) {
          new_desc = getElementTextNS('', 'tidystring', xmlhttp.responseXML, 0);
					//alert(new_desc);
          //new_desc = xmlhttp.responseText;
					if(new_desc) {
						el.value = new_desc;
					}
				}
			}
		}
		// @todo: find a way to pull this url from config?
		// or simply change it to production install at some point
		xmlhttp.open('GET', 'http://calendar.oregonstate.edu/tidy_ajax.php?tidy_string='+escape(description.value), true);
		xmlhttp.send('');
	}
}

function switchPreviewColors(select)
{
    var previewDiv = select.parentNode.parentNode.cells[2].firstChild;
    var new_value = '0';
    if (select.value != '') {
        new_value = select.value
    }
    previewDiv.firstChild.className = "header"+new_value;
    previewDiv.lastChild.className = "footer"+new_value;
}
