// JavaScript Document
var myAccordion;
var stretchers;
function initTabs(classnme){
	classnme = (classnme == undefined) ? "container" : classnme;
	stretchers = document.getElementsByClassName(classnme);
	var toggleHolder = document.getElementById('tabs');
	var toggles = getElementsByAttribute(toggleHolder, 'li', 'title');
	//getElementsByAttribute(document.body, "*", "id"); 
	//getElementsByAttribute(document.getElementById("the-form"), "input", "type", "text");
	myAccordion = new fx.Accordion(
		toggles, stretchers, '', true
	);
	var debugMsg = "";
	debugMsg += "toglleHolder = "+toggleHolder.localName+" ";
	//hash functions
	var found = false;
	var urlText = window.location.href;
	var hasLink = urlText.split("#");
	var titleLink = hasLink[1];
	toggles.each(function(li, i, arry){
		//var div = Element.find(li, "nextSibling");
		
		//alert("i = "+i+" div = "+div.getAttribute('title')+" and cur link title = "+titleLink);
		if (window.location.href.indexOf(toggles[i].getAttribute('title')) > 0) {
			//alert("going to show div ="+toggles[i]+" title : "+toggles[i].getAttribute('title')+" because title is in href "+window.location.href+" = "+window.location.href.indexOf(toggles[i].getAttribute('title')));
			myAccordion.showThisHideOpen(stretchers[i], true);
			setCurrentTab(toggles[i].getAttribute('title'))
			found = true;
		} else {
			//alert('2');
			//alert("going to show div ="+toggles[0]+" title : "+toggles[0].getAttribute('title')+" because it's the first time we open the page ");
			//myAccordion.showThisHideOpen(stretchers[0], true);
			//myAccordion.showThisHideOpen(stretchers[0], true);
		}
		
		//alert("window.location.href.indexOf(toggles[i].getAttribute('title')) = "+toggles[i].getAttribute('title')+"\nfound = "+found);
	});
	
	//alert("found = "+found);
	if (!found) myAccordion.showThisHideOpen(stretchers[0]);
	checkTabTitle();
		
}

function setCurrentTab(titleValue) {
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	
	allTabs.each( function(ele, i, arry){
		  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
		  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
		  if (ele.id == "current") {
			  if(ele.title != titleValue) ele.id = "";
			  vCurrentId = i;
			  checkTabTitle();
		  } else if(ele.title == titleValue) {
			 // alert("we are setting "+titleValue+" to be the current tab");
			  ele.id = "current";
			  checkTabTitle();
		  }
		  });
}


function setNextTab() {
	
	var urlText = window.location.href;
	var hasLink = urlText.split("#");
	var titleLink = (hasLink[1]!= undefined) ? hasLink[1] : "personal";
	
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	
	allTabs.each( function(ele, i, arry){
		  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
		  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
		  if (ele.id == "current") {
			  //if(ele.title != titleValue) ele.id = "";
			  ele.id = "";
			  vCurrentId =  i;
			  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\n");
		  } 
		  if (i == vCurrentId + 1) {
			  //if(ele.title != titleValue) ele.id = "";
			 // ele.id = "current";
			 setCurrentTab(ele.title);
			 myAccordion.showThisHideOpen(stretchers[i], true);
			 checkTabTitle();
			 //GetCurrentTabTitle();
			 // alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\n");
		  } 
		  });
}

function setPreviousTab() {
	
	var urlText = window.location.href;
	var hasLink = urlText.split("#");
	var titleLink = (hasLink[1]!= undefined) ? hasLink[1] : "personal";
	
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	
	allTabs.each( function(ele, i, arry){
		  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
		  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
		  if (ele.id == "current") {
			  //if(ele.title != titleValue) ele.id = "";
			  ele.id = "";
			  vCurrentId =  i;
			  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\n");
		  } 
		  
		  });
		  
	allTabs.each( function(ele, i, arry){
		  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
		  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
		  if ((i == vCurrentId - 1) && (vCurrentId > 0)) {
			  //if(ele.title != titleValue) ele.id = "";
			 // ele.id = "current";
			 setCurrentTab(ele.title);
			 myAccordion.showThisHideOpen(stretchers[i], true);
			 checkTabTitle();
			 // alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\n");
		  } 
		  });	  
}


function GetCurrentTabTitle() {
	
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	
	allTabs.each( function(ele, i, arry){
		  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
		  
		  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
		  if (ele.id == "current") {
			 // alert(ele.title);	
		  } 
		  
		  });  
}


function checkTabTitle() {
	
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	if (document.getElementById("next")) {
		allTabs.each( function(ele, i, arry){
			  //alert("element "+i+"  is "+ele+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+titleValue);
			  //alert("element "+i+" =  "+ele.id+"\narry =  allTabs ? : "+(arry == allTabs)+"\ntitle = "+allTabs[i].title+"\ntitleValue = "+titleValue);
			  if (ele.id == "current") {
				 //alert(ele.title);		
				 if(i== allTabs.length-1){
				 	// alert('LAST TAB, switch button');
				 	//ocument.form.registrationform.Registrate.disabled=true;
				 	document.getElementById('next').style.display="none";
				 	document.getElementById('previous').style.display="";
				 	document.getElementById('Registrate').style.display="";
			 	 }else{
				 	 if(i == 0){
					 	// alert('FIRST TAB, switch button');
					 	//ocument.form.registrationform.Registrate.disabled=true;
					 	document.getElementById('next').style.display="";
					 	document.getElementById('previous').style.display="none";
				 	document.getElementById('Registrate').style.display="none";
				 	 }
				 	 else{
					 	// alert('MIDDLE TABs, switch button');
					 	document.getElementById('next').style.display="";
					 	document.getElementById('previous').style.display="";
				 	document.getElementById('Registrate').style.display="none";
				 	 }
				}
			 	 
			  } 
			  });  
	  }
}

function findStretcher(formRef) {
	var container = formRef.parentNode.parentNode.parentNode;
	var allTabs = getElementsByAttribute(document.getElementById('tabs'), 'li', 'title');
	var curTab = document.getElementById("current");
	var curTab;
	
	allTabs.each( function(ele, i, arry){
		  if (stretchers[i] == container) {
			 setCurrentTab(ele.title);
			 myAccordion.showThisHideOpen(stretchers[i], true);
			 checkTabTitle();  
		  } 
	  });
}