function changepage(list) {
	location.href=list.options[list.selectedIndex].value;
}

function setPointer(theRow, thePointerColor)
{
	if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {
			return false;
	}

	var row_cells_cnt = theRow.cells.length;
	for (var c = 0; c < row_cells_cnt; c++) {
			theRow.cells[c].bgColor = thePointerColor;
	}

	return true;
}

// used in connection with the quarter dropdown.
//   when the q-drop is changed, the start/end dates will change also
function setStartAndEndDates(quarter_drop, form_num) {
  setStartDate(quarter_drop, form_num);
  setEndDate(quarter_drop, form_num);
}
function setStartDate(quarter_drop, form_num) {
  myform = document.forms[form_num]

  // kludge to get the fields we need since we can't pull them by name bacause of rails's bracketed names
  for(i=0; i<myform.elements.length; i++) {
    if (myform.elements[i].name == "start_date[month]")
      var month_drop_id = i
    if (myform.elements[i].name == "start_date[day]")
      var day_drop_id = i
    if (myform.elements[i].name == "start_date[year]")
      var year_drop_id = i
  }

  var start_date_month_drop = myform.elements[month_drop_id];
  var start_date_day_drop = myform.elements[day_drop_id];
  var start_date_year_drop = myform.elements[year_drop_id];

  var selected_quarter = quarter_drop.value;
  var splitted_quarter = selected_quarter.split("-");

  var selected_quarter_year = splitted_quarter[1];

  var selected_quarter_month = 1;
  if (splitted_quarter[0] == "Q2") {
    selected_quarter_month = 4;
  } else if (splitted_quarter[0] == "Q3") {
    selected_quarter_month = 7;
  } else if (splitted_quarter[0] == "Q4") {
    selected_quarter_month = 10;
  }
  
  var selected_quarter_day = 1;
  

//  start_date_year_drop.selectedIndex = selected_quarter_year
  start_date_year_drop.value = selected_quarter_year
  start_date_month_drop.value = selected_quarter_month

}
function setEndDate(quarter_drop, form_num) {
  myform = document.forms[form_num]

  // kludge to get the fields we need since we can't pull them by name bacause of rails's bracketed names
  for(i=0; i<myform.elements.length; i++) {
    if (myform.elements[i].name == "end_date[month]")
      var month_drop_id = i
    if (myform.elements[i].name == "end_date[day]")
      var day_drop_id = i
    if (myform.elements[i].name == "end_date[year]")
      var year_drop_id = i
  }

  var end_date_month_drop = myform.elements[month_drop_id];
  var end_date_day_drop = myform.elements[day_drop_id];
  var end_date_year_drop = myform.elements[year_drop_id];

  var selected_quarter = quarter_drop.value;
  var splitted_quarter = selected_quarter.split("-");

  var selected_quarter_year = splitted_quarter[1];

  var selected_quarter_month = 4;
  if (splitted_quarter[0] == "Q2") {
    selected_quarter_month = 7;
  } else if (splitted_quarter[0] == "Q3") {
    selected_quarter_month = 10;
  } else if (splitted_quarter[0] == "Q4") {
    selected_quarter_month = 1;
    selected_quarter_year = parseInt(selected_quarter_year) + 1;
  }
  
  var selected_quarter_day = 1;
  
  end_date_year_drop.value = selected_quarter_year
  end_date_month_drop.value = selected_quarter_month

}


// http://tuckey.org/textareasizer/
function countLines(strtocount, cols) {
	var hard_lines = 1;
	var last = 0;
	while ( true ) {
    last = strtocount.indexOf("\n", last+1);
    hard_lines ++;
    if ( last == -1 ) break;
	}
	var soft_lines = Math.round(strtocount.length / (cols-1));
	var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
	if ( hard ) soft_lines = hard_lines;
		return soft_lines;
}
function cleanForm() {
	var the_form = document.forms[0];
	for ( var x in the_form ) {
    if ( ! the_form[x] ) continue;
    if( typeof the_form[x].rows != "number" ) continue;
    the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
	}
	setTimeout("cleanForm();", 300);
}
function cleanForm2() {
	var the_form = document.forms[1];
	for ( var x in the_form ) {
    if ( ! the_form[x] ) continue;
    if( typeof the_form[x].rows != "number" ) continue;
    the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
	}
	setTimeout("cleanForm2();", 300);
}

// search input
function checkValue(thisForm) {
	if (thisForm.search_input.value==" Search for toys" || thisForm.search_input.value == "")
		return false;
	else
		return true;
}

function focusNClearInput(theField){
	var hasBeenFocused = theField.getAttribute("hasBeenFocused");
	var hasNotBeenFocused = (hasBeenFocused=="" || hasBeenFocused==null);
	if(hasNotBeenFocused)	{

		theField.setAttribute("hasBeenFocused","true");
		theField.style.color = "#000000";
		theField.value = "";

		var theForm = theField.form;
		for(ii=0; ii<theForm.elements.length; ii++)	{
			if(theForm.elements[ii].type=="submit")	{
				theForm.elements[ii].disabled = false;
			}
		}
	}
}

function submitForm() {
	document.forms[0].submit();
}

// Dynamic Help toolkit
var answers = new Array();
var seeMoreLinks = new Array();
var shownAnswer = -1;
var isSafari = navigator.userAgent.indexOf("Safari") > -1 || navigator.userAgent.indexOf("AppleWebKit") > -1;

/*
function showAnswer(whichOne, totalShown, presentationId, faqId) {
	showInlineAnswer(whichOne, totalShown, presentationId, faqId, "ShowHelpAnswer");
}

function showSelfHelpAnswer(whichOne, totalShown, presentationId, faqId) {
	showInlineAnswer(whichOne, totalShown, presentationId, faqId, "ShowSelfHelpAnswer");
}
*/

//function showInlineAnswer(whichOne, totalShown, presentationId, faqId, url) {
function showInlineAnswer(whichOne) {
	if (shownAnswer != -1) {
//		var oldquestion = document.getElementById("faq" + shownAnswer + "q");
		var oldanswer = document.getElementById("faq" + shownAnswer + "a");    
//		oldquestion.style.backgroundColor = "#FFFFEE";
//		oldquestion.style.fontWeight = "normal";
		oldanswer.style.backgroundColor   = "#FFFFEE";
		oldanswer.innerHTML = seeMoreLinks[shownAnswer];
		
	}

//	var question = document.getElementById("faq" + whichOne + "q");
	var answer = document.getElementById("faq" + whichOne + "a");
//	question.style.backgroundColor = "#FEFEC0";
//	question.style.fontWeight = "bold";
	answer.style.backgroundColor   = "#FEFEC0";
	answer.innerHTML = answers[whichOne];

	shownAnswer = whichOne;

}

function setInnerHTML( id, str ) {
  if(!document.getElementById) return; // Not Supported
  if(document.getElementById){
    document.getElementById(id).innerHTML = str;
  }
}
