var fttw2idmap=Array();
function submitIframeLoaderForm(idname,fname)
{
	$(idname).firstChild.contentDocument.forms[fname].submit();
}

function closeTab(idname)
{
	var cidname=$(idname).getAttribute("fttwid")+"_caption";	

	$(idname).parentNode.removeChild($(idname));
	$(cidname).parentNode.removeChild($(cidname));
}

function fttw_setActive(arg)
{
	var tabel=$(arg).parentNode.firstChild;
	var isred=String();
	var curr=String();

	while(tabel)
	{
		if(tabel.getAttribute("errors") > 0)
			isred='_red';
		else
			isred='';

		if(tabel.id==arg)
		{
			if($(arg).parentNode.getAttribute("lasttabel")!=null)
			{
				if($($(arg).parentNode.getAttribute("lasttabel")).getAttribute("onChange"))
					eval($($(arg).parentNode.getAttribute("lasttabel")).getAttribute("onChange"));
			
				if($($(arg).parentNode.getAttribute("lasttabel")).getAttribute("onFirstChange"))
				{
					eval($($(arg).parentNode.getAttribute("lasttabel")).getAttribute("onFirstChange"));
					$($(arg).parentNode.getAttribute("lasttabel")).removeAttribute("onFirstChange");
				}
			}


			if(tabel.getAttribute("onFirstLoad"))
			{
				eval(tabel.getAttribute("onFirstLoad"));
				tabel.removeAttribute("onFirstLoad");
			}

			if(tabel.getAttribute("onLoad"))
			{
				eval(tabel.getAttribute("onLoad"));
			}

			$(arg).parentNode.setAttribute("lasttabel",arg);
//				tabel.style.display = "block";
			tabel.style.position = "static";
//			tabel.style.top = "0px";
//			tabel.style.left = "0px";
			curr='curr';
		}
		else
		{
//			tabel.style.display = "none";
			tabel.style.position = "absolute";
			tabel.style.top = "-10000px";
			tabel.style.left = "-10000px";
			curr='';
		}
		
		tabelobj = new Element.ClassNames($(tabel.getAttribute("fttwid")+"_caption"));
		tabelobj.set("fttw_tabcaption"+curr+isred);
		tabelobj = new Element.ClassNames($(tabel.getAttribute("fttwid")+"_caption").firstChild);
		tabelobj.set("fttw_tabcaption"+curr+"span"+isred);
		tabel=tabel.nextSibling;
	}
}

function formToTabWindow(formname,parameters)
{
	
	var fttw_alltabels=Array();
	var fttw_feifid=0;
	var fttw_errors=Array();
	var tabhtml=String();
	var contenthtml=String();
	var contenthtmladd=String();
	var randid=Math.random();
	var fttw_remove=Array();
	var arrows=true;
	var autosubmit=false;
	var contenthtmladds=Array();
	var count;
	var nexttabel;
	
	if(document.getElementsByName(formname)[0])
		var formEL=document.getElementsByName(formname)[0];
	else
		var formEL=$(formname);
		
	if (!formEL) return false;
	Element.addClassName(formEL, 'dd_oldtabs');
		
	var tabel=$(formEL.firstChild);

	var maintabel=formEL;

	if(parameters)
	{
		if(parameters.arrows=='no')
			arrows=false;
	}

	count=0;

	while(tabel)
	{
		nexttabel=$(tabel.nextSibling);
		if(tabel.style)
		{
			if(tabel.getAttribute("caption"))
			{
				count++;

				if(tabel.getAttribute("errors"))
					fttw_feifid=count;

				if(tabel.getAttribute("order"))
				{
					fttw_alltabels[tabel.getAttribute("order")]=tabel;
					fttw_errors[count]=tabel.getAttribute("order");
				}
				else
				{
					fttw_alltabels[count]=tabel;
					fttw_errors[count]=count;
				}

				if(tabel.id)
					fttw2idmap["fttw_"+randid+"_"+count]=tabel.id;
				else
					fttw2idmap["fttw_"+randid+"_"+count]="fttw_"+randid+"_"+count;
				
				
				if (count>1) 
					{
						//new Element.remove(tabel);
//						$(tabel).hide();
							tabel.style.position = "absolute";
							tabel.style.top = "-10000px";
							tabel.style.left = "-10000px";
					}
			}
		}

		tabel=nexttabel;
	}

	
	var tabul = document.createElement('UL');
			tabul.className='tabform_ul';
			
	var subdiv=document.createElement("div");
				subdiv.className='fttw_contentrow';

	for(count=1;fttw_alltabels[count];count++)
	{
		var tabli = document.createElement('LI');
				tabli.id ='fttw_'+randid+'_'+count+'_caption';
				//tabli.setAttribute('onclick',"fttw_setActive(\""+fttw2idmap["fttw_"+randid+"_"+count]+"\");");
				//tabli.onclick="fttw_setActive(\""+fttw2idmap["fttw_"+randid+"_"+count]+"\")";
				tabli.innerHTML ="<span onclick='fttw_setActive(\""+fttw2idmap["fttw_"+randid+"_"+count]+"\")'>"+fttw_alltabels[count].getAttribute("caption")+"</span>";
				
		var contentdiv = $(fttw_alltabels[count]);
			  contentdiv.className='fttw_content yui-content';
			  contentdiv.setAttribute('fttwid','fttw_'+randid+'_'+count);
			  contentdiv.setAttribute('caption',fttw_alltabels[count].getAttribute("caption"));
			  contentdiv.setAttribute('id',fttw2idmap["fttw_"+randid+"_"+count]);
			 // contentdiv.style.position='relative';
			 // contentdiv.innerHTML = fttw_alltabels[count].innerHTML;
			  
		if(fttw_alltabels[count].getAttribute('onChange'))
			contentdiv.setAttribute('onChange',fttw_alltabels[count].getAttribute('onChange'));

		if(fttw_alltabels[count].getAttribute('onFirstChange'))
			contentdiv.setAttribute('onFirstChange',fttw_alltabels[count].getAttribute('onFirstChange'));

		if(fttw_alltabels[count].getAttribute('onLoad'))
			contentdiv.setAttribute('onLoad',fttw_alltabels[count].getAttribute('onLoad'));

		if(fttw_alltabels[count].getAttribute('onFirstLoad'))
			contentdiv.setAttribute('onFirstLoad',fttw_alltabels[count].getAttribute('onFirstLoad'));

		if(arrows)
		{
			if(count>1)
			{
				new Insertion.Bottom(contentdiv, "<div name='arrows'><br><a href='javascript:fttw_setActive(\""+fttw2idmap["fttw_"+randid+"_"+(count-1)]+"\")' style='float:left'>"+L_back+"</a></div>");
			}

			if(count<(fttw_alltabels.length-1))
			{
				new Insertion.Bottom(contentdiv, "<div name='arrows'><a href='javascript:fttw_setActive(\""+fttw2idmap["fttw_"+randid+"_"+(count+1)]+"\")' style='float:right'>"+L_next+"</a></div>");
			}
		}

		subdiv.appendChild(contentdiv);
		tabul.appendChild(tabli);
	}

	
	if(fttw_alltabels.length > 0)
	{
		var maindiv=document.createElement("div");
				maindiv.className='fttw_main';
				maindiv.appendChild(tabul);
				maindiv.appendChild(document.createElement("br"));
				maindiv.appendChild(subdiv);
		
		var cleardiv=document.createElement("div");
				cleardiv.className='fttw_clear';
	
		maintabel.insertBefore(cleardiv,maintabel.firstChild);
		maintabel.insertBefore(maindiv,maintabel.firstChild);
		
		if(fttw_feifid == 0)
		{
			fttw_setActive(fttw2idmap["fttw_"+randid+"_"+1]);
		}
		else
		{
			fttw_setActive(fttw2idmap["fttw_"+randid+"_"+fttw_errors[fttw_feifid]]);
		}
	}
}

