﻿var CurrentStep = 0;
var StepsCount = 0;
function steps_setEvents(div){
	div.onclick = function(){if(steps_checkStep(this.stepIndex)) CurrentStep = this.stepIndex;};
	div.OnTransformEnd = function(sender){steps_stateChange(sender);};
	div.OnTransformStep = function(sender){steps_onChanging(sender);};
	//Inputs
	var els = div.getElementsByTagName('INPUT');
	for(var i=0; i<els.length; i++){
		els[i].setAttribute('autocomplete', 'off');
		if(els[i].getAttribute('vstatus')) validator_setStatus(els[i], els[i].getAttribute('vstatus'), true);
		if(els[i].type=="checkbox" || els[i].type=="radio") els[i].style.width="auto";
		
		els[i].onfocus = function(){CurrentStep = div.stepIndex; tooltip_hide(this); validator_setStatus(this, this.vstatus, false, true);};
		els[i].onblur = function(){tooltip_show(this); hint_create(this, 'none');}
		els[i].onchange = function(){steps_checkStep(CurrentStep)};
		els[i].autoComplete = false;
		if(document.all){
			els[i].attachEvent('onkeyup', function(){steps_checkStep(div.stepIndex);});
			els[i].attachEvent('onclick', function(){steps_checkStep(div.stepIndex);});
		}else{
			els[i].addEventListener('keyup', function(){steps_checkStep(div.stepIndex);}, false);
			els[i].addEventListener('click', function(){steps_checkStep(div.stepIndex);}, false);
		}
		tooltip_show(els[i]);
	}
	//Selects
	els = div.getElementsByTagName('SELECT');
	for(i=0; i<els.length; i++){
	//	els[i].setAttribute('autocomplete', 'off');
		if(els[i].getAttribute('vstatus')) validator_setStatus(els[i], els[i].getAttribute('vstatus'), true)
		els[i].onfocus = function(){CurrentStep = div.stepIndex; validator_setStatus(this, this.vstatus, false, true);};
		els[i].onblur = function(){hint_create(this, 'none');}
		if(document.all){
			els[i].attachEvent('onchange', function(){steps_checkStep(div.stepIndex);});
		}else{
			els[i].addEventListener('change', function(){steps_checkStep(div.stepIndex);}, false);
		}
	}
	//Text areas
	els = div.getElementsByTagName('TEXTAREA');
	for(i=0; i<els.length; i++){
		els[i].setAttribute('autocomplete', 'off');
		if(els[i].getAttribute('vstatus')) validator_setStatus(els[i], els[i].getAttribute('vstatus'), true);
		els[i].onfocus = function(){CurrentStep = div.stepIndex; validator_setStatus(this, this.vstatus, false, true);};
		els[i].onblur = function(){hint_create(this, 'none');}
		if(document.all){
			els[i].attachEvent('onkeyup', function(){steps_checkStep(div.stepIndex);});
		}else{
			els[i].addEventListener('keyup', function(){steps_checkStep(div.stepIndex);}, false);
		}
	}
	
}
function steps_Init(expanded){
	var holder = $('steps_holder');
	var section = $('section_content');
	//deal with tables
	var tables = holder.getElementsByTagName('TABLE');
	var trs, tds;
	var tr = $('template_row');
	var newtr, tbody, td, cols, isInternal;
	var dw = 0;
	//deal with tables
	for(var i=0; i<tables.length; i++){
		if(tables[i].className.indexOf('fields-table') == -1) continue;
		tables[i].cellSpacing = "0";
		isInternal = tables[i].parentNode.tagName=='TD';
		
		cols = tables[i].getAttribute('cols')*1;
		if(!cols || cols==0) cols = 8;
		tables[i].style.width= cols*58+'px';
		if(document.all) tables[i].style.width= 1*(cols*58)+'px';
		tbody = tables[i].tBodies[0];
		newtr = tbody.insertRow(0);
		newtr.className = 'config-row';
		for(var j=1; j<=cols; j++){
			td = newtr.insertCell(-1);
		}
		for(j=0;j<tables[i].rows.length; j++){
			for(var k=0; k<tables[i].rows[j].cells.length; k++){
				tables[i].rows[j].cells[k].vAlign = "bottom";
				
				if(document.all) dw = 7;
				else dw = 0; 
					
				tables[i].rows[j].cells[k].style.width = 1*(tables[i].rows[j].cells[k].colSpan*58-dw)+'px';
				if(isInternal){
					tables[i].rows[j].cells[k].style.paddingBottom = "0px";
					if(k==0) tables[i].rows[j].cells[k].style.paddingLeft = "0px";
				}
			}
		}
	}
	
	//deal with step divs
	var divs = holder.getElementsByTagName('DIV');
	var maxsteps = 0;
	var h=0;
	var div, clone, a;
	var foother = $('section_foother');
	for(i=0; i<divs.length; i++){
		div = divs[i];
		if(div.className == 'step'){
			clone = div.cloneNode(true);
			clone.id = 'step'+1*(maxsteps+1);
			clone.stepIndex = maxsteps+1;
			
			
			clone.H = div.offsetHeight;				
			clone.className = "step";
			if(!expanded){
				clone.style.height = "1px";
				clone.style.display = "none";
			}
			
			//clone.insertBefore(a, clone.firstChild);
			$('steps_form').insertBefore(clone,  foother);
			steps_setEvents(clone);
			//section.appendChild(clone);
			maxsteps++;
		}
	}
	holder.parentNode.removeChild(holder);
	StepsCount = maxsteps;
	
	if(typeof(steps_afterInit)=='function') steps_afterInit();
//	steps_disableFrom(2);
	section.expanded = expanded;
	$('step1').isEnabled = true;
	if(expanded){
		CurrentStep = 1;
		$('btn_back').style.display="none";
		$('btn_next').style.display="none";
		$('btn_submit').style.display="inline";
		$('img_expand_collapsef').style.display = "none";
		steps_checkStep(1);
		
		if(typeof(steps_afterExpand)=='function') steps_afterExpand();
	}else{
		steps_checkStep(1);
		steps_showStep(1, true);
	}
	
}
function steps_showStep(num, no_animation){
	if($('section_content').expanded) return;
	
	var valFuncName = 'steps_validateStep'+CurrentStep;
	var failure;
	eval("if(typeof("+valFuncName+")=='function') failure = "+valFuncName+"();");
	if(failure)	return;
	var sid = 'step'+num;
	var div = $(sid);
	if(!div) return;
	div.style.display = "block";
	steps_hideStep(CurrentStep);
	if(no_animation){
		div.style.height = (div.H-1)+'px';
		transGeometry(sid, '!', '!', div.H-1, div.H,1);
		if(typeof(steps_onShow) == 'function') steps_onShow(num);
	}else{
		transGeometry(sid, '!', '!', 0, div.H);
		if(typeof(steps_onShow) == 'function') setTimeout("steps_onShow("+num+")", 300);
	}
	if(num > 1) getElementsByTagNames('input,select,textarea', div)[0].focus();
		
	CurrentStep = num;
	if(num == StepsCount){	
		$('btn_submit').style.display = 'inline';
		$('btn_next').style.display = 'none';
	}
	steps_checkStep(num);
}
function steps_hideStep(num){
	var delay = 0;
	if(typeof(steps_onHide) == 'function') delay = steps_onHide(num);
	var sid = 'step'+num;
	var div = $(sid);
	if(!div) return;
	setTimeout("transGeometry('"+sid+"', '!', '!', "+div.H+", 1,600)", delay);
}
function steps_stateChange(div){
	if(div.state == 1){
		div.state = 0;
		div.style.display = "none";
	}else{
		div.state = 1;
	}
}
function steps_onChanging(div){
	div.scrollTop = Math.round((div.H - div.offsetHeight)/2);
}
function steps_expand(){
	if($('section_content').expanded) return;
	if(typeof(steps_beforeExpand)=='function'){
		steps_beforeExpand();
	}
	var div;
	for(var i=1; i<=StepsCount; i++){
		div = $('step'+i);
		div.state = 0;
		if(document.all){
			div.style.height = 1*(div.H-1)+'px';
			transGeometry(div.id, '!', '!', div.H-1, div.H, 1);
		}else{
			transGeometry(div.id, '!', '!', '!', div.H, 400);
		}
	}
	if(typeof(steps_afterExpand)=='function'){
		setTimeout('steps_afterExpand()', 800);
	}
	$('btn_back').style.display = 'none';
	$('btn_next').style.display = 'none';
	$('btn_submit').style.display = 'inline';
	$('section_content').expanded = true;
}
function steps_collapse(){
	if(!$('section_content').expanded) return;
	$('btn_back').style.display = 'inline';
	$('btn_next').style.display = 'inline';
	$('btn_submit').style.display = 'none';
	if(typeof(steps_beforeCollapse)=='function') steps_beforeCollapse();
	var div;
	for(var i=1; i<=StepsCount; i++){
		if(i!=CurrentStep){
			div = $('step'+i);
			if(document.all){
				div.style.height = '2px';
				transGeometry(div.id, '!', '!', '!', 1, 1);
			}else{
				transGeometry(div.id, '!', '!', '!', 1, 400);
			}
		}
	}
	if(typeof(steps_afterCollapse)=='function'){
		setTimeout('steps_afterCollapse()', 800);
	}
	$('section_content').expanded = false;
	steps_checkStep(CurrentStep);
}
function steps_changeState(){
	var imgf = $('img_expand_collapsef');
	if($('section_content').expanded){
		window.scrollBy(0, -100000);
		steps_collapse()
		imgf.src = '/img/scroll_down.gif';
		imgf.alt = 'Разгръщане на формата';
		imgf.title = 'Разгръщане на формата';
	}else{
		steps_expand()
		imgf.src = '/img/scroll_up.gif';
		imgf.alt = 'Свиване на формата';
		imgf.title = 'Свиване на формата';
	}
}

function steps_disableFrom(stepIndex){
	var i, j, fields, step;
	for(i=stepIndex; i<=StepsCount; i++){
		step = $('step'+i);
		fields = step.getElementsByTagName('input');
		for(j=0; j<fields.length; j++){
			if(fields[j].type == 'submit') continue;
			fields[j].disabled=true;
			fields[j].style.background = '#EFEFEF';
		}
		fields = step.getElementsByTagName('textarea');
		for(j=0; j<fields.length; j++){
			fields[j].disabled=true;
			fields[j].style.background = '#EFEFEF';
		}
		fields = step.getElementsByTagName('select');
		for(j=0; j<fields.length; j++){
			fields[j].disabled=true;
			fields[j].style.background = '#EFEFEF';
		}
		step.isEnabled = false;
	}
	/*
	$('btn_submit').disabled = true;
	$('btn_submit').style.color = '#AEAEAE';
	*/
}
function steps_enableStep(stepIndex){
	var step = $('step'+stepIndex);
	if(!step) return;
	var fields = step.getElementsByTagName('input');
	for(var i=0; i<fields.length; i++){
		if(fields[i].type=="submit") continue;
		if(fields[i].disabled) validator_setStatus(fields[i], fields[i].vstatus, true);
		fields[i].disabled = false;
		tooltip_show(fields[i]);
	}
	fields = step.getElementsByTagName('textarea');
	for(var i=0; i<fields.length; i++){
		if(fields[i].disabled) validator_setStatus(fields[i], fields[i].vstatus, true);
		fields[i].disabled = false;
		
	}
	fields = step.getElementsByTagName('select');
	for(var i=0; i<fields.length; i++){
		if(fields[i].disabled) validator_setStatus(fields[i], fields[i].vstatus, true);
		fields[i].disabled = false;
	}
	step.isEnabled = true;
	steps_checkStep(stepIndex);
}
function steps_checkStep(stepIndex, debug){
	var step = $('step'+stepIndex);
	if(!step) return;
	var OK = true;
	var debugstr ='';
	var fields = step.getElementsByTagName('input');
	for(var i=0; i<fields.length; i++){
		OK = OK && (fields[i].vstatus == 'OK');
		debugstr += 'input '+i+'('+fields[i].type+'): '+fields[i].vstatus+"\n";
	}
	fields = step.getElementsByTagName('textarea');
	for(var i=0; i<fields.length; i++){
		OK = OK && (fields[i].vstatus == 'OK');
		debugstr += 'textarea '+i+': '+fields[i].vstatus+"\n";
	}
	fields = step.getElementsByTagName('select');
	for(var i=0; i<fields.length; i++){
		OK = OK && (fields[i].vstatus == 'OK');
		debugstr += 'select '+i+': '+fields[i].vstatus+"\n";
	}
	if(OK){
		if(!$('section_content').expanded){
			if(stepIndex == StepsCount){
				$('btn_submit').disabled = false;
				$('btn_submit').style.display = 'inline';
				$('btn_submit').style.color = '#FFF';
			}else{
				$('btn_next').style.display = 'inline';
				$('btn_submit').style.color = '#FFF';
			}
		}else{
			$('btn_submit').style.display = 'inline';
			$('btn_submit').style.color = '#FFF';
			$('btn_submit').disabled = false;
		}
		steps_enableStep(stepIndex+1);
		if(debug) alert('Step is OK \n'+debugstr);
	} else {
		if(stepIndex == CurrentStep && !$('section_content').expanded){			
			$('btn_next').style.display = 'none';
			$('btn_submit').style.display = 'none';
		}
		if(debug) alert('Step is FAIL \n'+debugstr);
		steps_disableFrom(stepIndex+1);
	}
}
function steps_highlightUnfinished(caller){
	for(var i=1; i<StepsCount; i++){
		
		if($('step'+i).isEnabled) steps_highlightStep(i);
	}
}
function steps_highlightStep(stepIndex){
	var step = $('step'+stepIndex);
	var msg = 'Попълнете тук';
	if(!step) return;

	var fields = step.getElementsByTagName('input');
	for(var i=0; i<fields.length; i++){
		if('FAIL_OK'.indexOf(fields[i].vstatus)==-1){
			hint_create(fields[i], 'hint', msg)
		}
	}
	
	fields = step.getElementsByTagName('select');
	for(i=0; i<fields.length; i++){
		if('FAIL_OK'.indexOf(fields[i].vstatus)==-1){
		 hint_create(fields[i], 'hint', msg)
		}
	}
	fields = step.getElementsByTagName('textarea');
	for(i=0; i<fields.length; i++){
		if('FAIL_OK'.indexOf(fields[i].vstatus)==-1){
		 hint_create(fields[i], 'hint', msg)
		}
	}
	return true;
}
