/*
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│		UI FUNCTIONS                                                                                 │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
*/
// DIV ___________________________________________________________________________________________
// Show one div, hide rest ID must be "txt_ plus a number (i)
function Show(a, n) {
	//alert(UI - Show "+a);
	//n = 2; // number of hidden layers
	for(i=0;i<=n;i++) {
		document.getElementById('txt_'+i).style.display = 'none';
	}
	document.getElementById('txt_'+a).style.display = 'inline';
}

// Shows a section of the page by it's Div ID
function ShowDiv(div_id) {
			//alert("UI - ShowDiv = "+div_id);
	document.getElementById(div_id).style.display = 'inline';
}

// Hides a section of the page by it's Div ID
function HideDiv(div_id) {
	//alert("UI - hideDiv = "+div_id);
	document.getElementById(div_id).style.display = 'none';
}

// toggle an element by it's Div ID
function toggleDiv(x) {
	if (document.getElementById(x).style.display == 'none') {
		document.getElementById(x).style.display = 'inline';
	} else {
		document.getElementById(x).style.display = 'none';
	}
}

// Set focus on a div by it's Div ID
function SetFocus(div_id) {
	window.focus(div_id);
}

// Write text to a div tag (id)
function Write(id, txt) {
	document.getElementById(id).innerHTML = txt;
}

// Position a div tag (id, top, left, right)
function PositionDiv(id, tp, lft, rt) {
	//alert(id)
	if(tp = "0") {
		tp = document.Show.MouseY.value;
	}
	if(lft = "0"){
		document.getElementById(id).style.left = lft;
	}else{
		document.getElementById(id).style.right = rt;
	}
	document.getElementById(id).style.top  = tp;
}

// FORMS _________________________________________________________________________________________
// Get the text in a form field by it's ID
function FieldValue(id) {
	oForm = document.forms[0]; 
	$s = oForm.elements[id].value;
}
// Selects all text in a form element by it's ID
function SelectAll(id) {
	document.getElementById(id).focus();
	document.getElementById(id).select();
}

// Change text in a form element by it's ID new text = txt
function WriteFormValue(id, txt) {
	document.getElementById(id).value = txt;
}

// Set focus to the first available form field no matter how many forms are on the page (ignores hidden fields)
function SetToFirstControl() {
  var bFound = false; 
  //for each form
  for (f=0; f < document.forms.length; f++) {
    //for each element in each form
    for(i=0; i < document.forms[f].length; i++) {
      //if it's not a hidden element
      if (document.forms[f][i].type != "hidden") { 
        //and it's not disabled
        if (document.forms[f][i].disabled != true) {
          try {
             //set the focus to it
             document.forms[f][i].focus();
             var bFound = true;
          }
          catch(er) {
          }
        }
      }
      //if found in this element, stop looking
      if (bFound == true)
        break;
    }
    //if found in this form, stop looking
    if (bFound == true)
      break;
  }
}

// TABLES ________________________________________________________________________________________
// Show a row in a table by its ID
function show_row(trID) {											
	var my_row = document.getElementById(trID);
	try {
		my_row.style.display = 'table-row';
	}
	catch (e) {
		my_row.style.display = 'inline';
	}
}

// Hide a row in a table by its ID
function hide_row(trID){											
	var my_row = document.getElementById(trID);
	my_row.style.display = 'none';
}

// Change row background color in a table by its ID
function change_bg_color(trID, BGcolor) {	
	var my_row = document.getElementById(trID).style.backgroundColor=BGcolor;
}

// Write a value to a table cell
function write2cell(id, txt){
	document.getElementById(id).innerText=txt;
}

// Get the number of columns in a table
// METHOD: var x = getNumOfColumns('tblGrid');
function getNumOfColumns(table_id) {
	var num_cols = document.getElementById(table_id).getElementsByTagName('tr')[0].getElementsByTagName('td').length
	return num_cols;
}

/* Use this to get the properties of a selected table row
	METHOD: var srcElem = getEventRow ();
		Row_Number    = srcElem.rowIndex;
		Column_Number = srcElem.colIndex;
		Tag_Name      = srcElem.tagName
*/
function getEventRow () {
	var srcElem = window.event.srcElement;
	//crawl up to find the row
	while (srcElem.tagName != "TR" && srcElem.tagName != "TABLE") {
		srcElem = srcElem.parentNode;
	}
	return srcElem;
}

// Get the content of a table cell
function cellContent(table_id, row_index, cell_index) {
	cellVal = document.getElementById(table_id).rows[row_index].cells[cell_index];
	return cellVal.innerHTML;
}

// BROWSER ___________________________________________________________________________________________
// Go to another webpage, transfer variable
function gotoPage(page, variable){											
	var trans_data = document.getElementById(variable).value;
	window.location = "http://www.whelen.com/outdoor/build_a_spec/build-a-spec.php?act="+trans_data;
}

/* Go to another webpage in 'n' seconds
	METHOD: timedRedirect("1500", "http://www.whelen.com")
*/
function timedRedirect(redirectTime, redirectURL) {
	setTimeout("location.href = redirectURL;",redirectTime);
}

// Open popup window
function popup(url) {
	newwindow=window.open(url,'popup','height=200,width=400');
	if (window.focus) {newwindow.focus()}
	return false;
}

/* Auto Expire Page
The JavaScript Source!! http://javascript.internet.com
Created by: Don Demrow | http://www.euro-innova.com */

/* Note that we are setting all the dates and date parts to strings to do our comparisons. */
function autoExpire() {
  var goLiveMonth = "10"  									// Month you want your content to start displaying. Two digits.
  var goLiveDay = "01"      								// Day you want your content to start displaying. Two digits.
  var goLiveYear = "2009"     								// Year you want your content to start displaying. Four digits.

  var expireMonth = "10"   									// Month you want your content to stop displaying. Two digits.
  var expireDay = "07"     									// Day you want your content to stop displaying. Two digits.
  var expireYear = "2009"  									// Year you want your content to stop displaying. Four digits.

  /* This is where you put your content. Make sure you escape any quotation marks with a backslash. Make sure you do not delete the opening and closing quotes. */
  var myContent = "This text will display, <strong>beginning and ending</strong> on the dates you have set."

  /* Don't edit below this line. Don */
  var goLiveDate = goLiveYear + goLiveMonth + goLiveDay;  	// puts START year, month, and day together.
  var expireDate = expireYear + expireMonth + expireDay;  	// puts EXPIRE year, month, and day together.

  var nowDate = new Date();
  var day = nowDate.getUTCDate();
  var month = nowDate.getUTCMonth();
  var correctedMonth = month + 1;  							//month - JavaScript starts at "0" for January, so we add "1"

  if (correctedMonth < 10) {  								// if less than "10", put a "0" in front of the number. 
    correctedMonth = "0" + correctedMonth;
  }
   
  if (day < 10) {  											// if less than "10", put a "0" in front of the number. 
    day = "0" + day;
  }

  var year = nowDate.getYear();  							// Get the year. Firefox and Netscape might use century bit, and two-digit year. 
  if (year < 1900) {
    year = year + 1900;  									// This is to make sure Netscape AND FireFox doesn't show the year as "107" for "2007."
  }

  var GMTdate = year + "" + correctedMonth + "" + day;  	//corrected month GMT date.
	
  if ((GMTdate <= expireDate) && (GMTdate >= goLiveDate)) {
    document.write(myContent)
  }
}


// STRINGS ____________________________________________________________________________________________
function upperCase(x) {
	var y=document.getElementById(x).value;
	var n=document.getElementById('aorder').value;
	if(n==0){
		document.getElementById('aorder').value=y;
	}
}

function removeSubstring(s, t) {
	/*  Remove all occurrences of a token in a string
	**    s  string to be processed
	**    t  token to be removed
	**  returns new string							*/
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + remove(s.substring(i + t.length), t);
	return r;
}

// MISC _______________________________________________________________________________________________
// Change button image on mouseover
function btnMouseover(img_name, img_src) {
	document[img_name].src = img_src;
}

// Which key has been pressed?
// NOTE: the keypress event, and in most browsers the keydown event, fires as long as the user keeps the key pressed
function keystroke(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);
	alert('UI - keystroke - Character was ' + character);
}

/* Right Click
   NOTE: Please note that, although Macs have only one mouse button, 
   Mozilla gives a Ctrl–Click a button value of 2, since Ctrl–Click 
   also brings up the context menu. iCab doesn’t yet support mouse 
   button properties at all and you cannot yet detect a right–click in Opera. */
function rightClick(e) {
	var rightclick;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	alert('UI - Rightclick: ' + rightclick); // true or false
}

// Mouse Position On Document Element
function mousePosition(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}


