/* =====================================================================	
	my_natives.js
========================================================================
   	Author: James Dacosta
   	Date:	23/08/07
------------------------------------------------------------------------
	Comments
------------------------------------------------------------------------
   	
   	Javascript functions used within My Natives
   	
   	
========================================================================*/ 




// addLoadEvent function as seen at http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// allows you to stack functions and apply them to the onload event and also means you 
// can abstract onload functions from the html
// I've added an argument 'arg' for onload functions with an argument
// Could probably be better implemented with 'arguments[]'

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


/* =hideActions (Hides save or cancel actions e.g. for profile pic tab)
------------------------------------------------------------------------*/

function hideActions(){
	if (!document.getElementById("form_action")) return false;
	document.getElementById("form_action").style.display = "none";
}




/* =Easy tabs script
------------------------------------------------------------------------*/

/*
EASY TABS 1.1 Produced and Copyright by Koller Juergen
www.kollermedia.at | www.austria-media.at
You can use this Script for private and commercial Projects, 
but just leave the two credit lines, thank you.
*/


//EASY TABS 1.1 - MENU SETTINGS
//Set the id names of your tablink (without a number at the end)
var tablink_idname = new Array("edit_profile","register");
//Set the id name of your tabcontentarea (without a number at the end)
var tabcontent_idname = new Array("edit_profile_section","register_section");
//Set the number of your tabs
var tabcount = new Array("4","4");
//Set the Tab wich should load at start (In this Example:Tab 2 is visible on load)
var loadtabs = new Array("1","1");


/*Swich EasyTabs Functions - no need to edit something here*/
function easytabs(menunr, active) {
menunr = menunr-1;
for (i=1; i <= tabcount[menunr]; i++)
{
if (!document.getElementById(tablink_idname[menunr]+i)) return false;

document.getElementById(tablink_idname[menunr]+i).className='tab'+i;
document.getElementById(tabcontent_idname[menunr]+i).style.display = 'none';
}
document.getElementById(tablink_idname[menunr]+active).className='tab'+active+' tabactive';
document.getElementById(tabcontent_idname[menunr]+active).style.display = 'block';
}


function initTabs(){

if (url_param('tab') == 'picture'){
	loadtabs = new Array("4","1");
}	

var menucount=loadtabs.length; var a = 0; var b = 1; do {easytabs(b, loadtabs[a]); a++; b++;}while (b<=menucount);


}




/* =Season picker
------------------------------------------------------------------------*/

 
function addRowToTable(new_season_id, new_year)
{
  var tbl = document.getElementById('seasonTable');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // resort cell
  var cellResortSel = row.insertCell(0);
  var selResort = document.createElement('select');
  selResort.name = 'cds.seasons.'+iteration+'.resort_id';
  var i = 0;
  for( i = 0; i< resort.length; i++)
  {
  		selResort.options[i] = new Option(resort[i].resortName,resort[i].resortId);
  		if(new_season_id == resort[i].resortId)
  		{
  			selResort.selectedIndex = i;
  		}
  }
  cellResortSel.appendChild(selResort);
  
  // year cell
  var cellYearSel = row.insertCell(1);
  var selYear = document.createElement('select');
  selYear.name = 'cds.seasons.'+iteration+'.season_year';
  for(i = 0; i<year.length; i++)
  {
  		selYear.options[i] = new Option(year[i], year[i]);
  		if(new_year == year[i])
  		{
  			selYear.selectedIndex = i;
  		}
  }
  cellYearSel.appendChild(selYear);
  
  //remove button
  var cellRemove = row.insertCell(2);
  var remove = document.createElement('input');
  remove.type = 'button';
  remove.value = '-' ;

  remove.onclick = function(){ removeRowFromTable(iteration);};
  cellRemove.appendChild(remove);
  
  
}


function removeRowFromTable(row_index)
{
	row_index = parseInt(row_index);
  var tbl = document.getElementById('seasonTable');
  var allRows = tbl.rows;
  var i;
  for (i=row_index; i<allRows.length; i++) 
  {
  		var new_index = parseInt(i-1);
 		allRows[i].cells[0].firstChild.name = 'cds.seasons.'+new_index+'.resort_id';
 		allRows[i].cells[1].firstChild.name = 'cds.seasons.'+new_index+'.season_year';
 		setOnClick(i,new_index);	
  }
  tbl.deleteRow(row_index);
}

function setOnClick(old_index,new_index)
{
	var local_new_index = new_index;
	var tbl = document.getElementById('seasonTable');
	tbl.rows[old_index].cells[2].firstChild.onclick = function(){ removeRowFromTable(local_new_index);};
}

// ---
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 highlight_subnav() {
	// Highlight the right subnav item by comparing the URL with
	// the href of each <a> elt in the <div id="subnav">, or by
	// using the hint if supplied as arguments[0]. The hint
	// specifies a part of the URL, useful for actions with
	// lots of paramters
	
	var this_page = document.location.href;
	
	// Remove anchor if there is one -- anything following #, including #
	this_page = this_page.replace( /\#.*/, '' );
	
	
	
	var hint = "";
	if (arguments[0]) {
		hint = arguments[0];
	}
	
	
	// Try both subnav and subnavIndent for our item to highlight
	
	var divs = [ "main_navigation","mynatives_menu" ];
	for (var d = 0; d < divs.length; d++) {
	
		if (document.getElementById(divs[d])) {
			var subnav_elts = document.getElementById(divs[d]);
			var aTags = subnav_elts.getElementsByTagName("A");
 			var i = 0;
			for (i = 0; i < aTags.length; i++) {
				// Is this a link?
				var elt = aTags[i]
  					if (elt.href == this_page || (hint && elt.href.indexOf(hint) != -1) ) {
						elt.className = elt.className + " currentsection";
					 
						
						
					}
 			}
		}
	}
		

} 

function list_value(l) 
{
	return l.options[l.selectedIndex].value;
}

function add_markup_tag(select, textElement,open_tag,close_tag) 
{
	var ti = document.getElementById(textElement);
	ti.value += open_tag+list_value(select)+close_tag;	
}


