///CSCForm JajaScript
function getSelect( name )
{
	if( name == '' || !name ) return null;
	
	var item = document.getElementsByName( name );
	if( item != null && item[ 0 ] != null )
	{
		return item[ 0 ];
	}
	else
	{
		return null;
	}
}

//for IE
if(!Array.indexOf)
{
	Array.prototype.indexOf = function(obj)
	{
		for(var i=0; i<this.length; i++)
		{
			if(this[i]==obj)
			{
				return i;
			}
		}
		return -1;
	}
}
if( !Array.chunk )
{//divides array into several arrays by chunk length
	Array.prototype.chunk = function( len )
	{
		for(var x, c = -1, i = 0, l = this.length, n = []; i < l; i++)
		{
			(x = i % len) ? n[c][x] = this[i] : n[++c] = [this[i]];
		}
		return n;
	}
}

if( !String.reverse )
{
	String.prototype.reverse = function()
	{
		splitext = this.split("");
		revertext = splitext.reverse();
		reversed = revertext.join("");
		return reversed;
	}
}

function getNaslVysl( krok )
{
	var nasl = new Array();
	var vysl = new Array();
	if( krok >= resc_map.length )
	{
		if( debug )alert( 'getNaslVysl: step is beyond map limits. step:' + krok );
		return [];
	}
	var tmp = resc_map[ krok ];
	var i = 0;
	for( var a = 0; a < tmp.length; )
	{
		nasl[ i ] = tmp[ a ]; a++;
		vysl[ i ] = tmp[ a ]; a++;
		i++;
	}
	return [ nasl, vysl ];
}

function isInOptions( item, value )
{
	if( item == null || value == null ) return -1;
	for( var i = 0; i < item.options.length; i++ )
	{
		if( item.options[ i ].value == value )
		{
			return i;
		}
	}
	return -1;
}

function removeOption( item, index )
{
	if ( item.options[ index ] != null )
	{
		item.remove( index );
	}
}

function insertOption( item, option )
{
	var index = item.options.length;
	var elOptOld = item.options[index];
	//option.index = index;
	if( AgentName != 'Opera' )
	{
		try{
			item.add(option, elOptOld); //standards compliant; doesn't work in IE
		}
		catch(ex)
		{
			item.add(option, index); //IE only
		}
	}
	else
	{
		item.options[ index ] = option;
	}
}

step_nasl = Array();

function getTextByVal( val, krok )
{
	//if( krok >= resc_map.length ) return '';
	var tmp = resc_vals[ krok ];
	for( var i = 0; i < tmp.length; i++ )
	{
		if( tmp[ i ][ 0 ] == val )
			return tmp[ i ][ 1 ];
	}
	return '';
}

function updateContactFormStep( iStep, aRemainingConditions )
{
	if( !iStep || iStep < 0 || !parseInt( iStep ) )
		iStep = 0;
	if( iStep > selects.length - 1 )
	{
		return aRemainingConditions;
	}
	iterator++;
	
	/// set the options for next step
	var item = getSelect( selects[ iStep ] );
	if (item == null) return aRemainingConditions;
		//if( j + value_next_shift >= options[ uCFs + 1 ].length ) break;
		//alert( item.type != 'hidden' );
	var type = item.type != 'hidden';
	if( type == true )
	{
		clear_this( item );
			//var step = finals[ 0 ][ i ];
		for( var j = 0; j < aRemainingConditions.length; j++ )
		{
			var vals = aRemainingConditions[ j ][ iStep ];
			for( var k = 0; k < vals.length; k++ )
			{
				var text = getTextByVal( vals[ k ], iStep );
				var tmp = isInOptions( item, vals[ k ] );
				if( tmp < 0 && text != '' )
				{
					var oOption = document.createElement( 'OPTION' );
					oOption.value = vals[ k ];
					tmp = 0;

					for (var c = 0; c < resc_vals[ iStep ].length; c++)
						tmp += resc_vals[ iStep ][ c ][ 0 ] == '' || resc_vals[ iStep ][ c ][ 0 ] == '0' ? 1 : 0;

					oOption.text = text;
					if( krok_vals[ iStep ] == oOption.value )
					{
						oOption.defaultSelected = oOption.selected = true;
					}
					else
					{
						oOption.defaultSelected = oOption.selected = false;
					}
					insertOption( item, oOption );
				}
			}
		}
	}

	if( iStep >= selects.length - 1 ) return aRemainingConditions;
	//var tmp_options = resc_vals[ iStep ];//rescued vals & texts
	//var next_item = getSelect( selects[ iStep + 1 ] );//INPUT for next step

	var aNewRemaining = new Array();
	var j = 0;
	for( var i = 0; i < aRemainingConditions.length; i++ )
	{
		var actualCond = aRemainingConditions[ i ][ iStep ];
		//alert( actualCond.indexOf( item.value ) + "\n" + item.value );
		if( item && (item.value == '0' || item.value == '' ))
		{
			aNewRemaining[ j ] = aRemainingConditions[ i ];
			j++;
		}
		else
		{
			if( actualCond && (actualCond.indexOf( item.value ) >=0 || actualCond.indexOf( '*' ) >=0) )
			{
				aNewRemaining[ j ] = aRemainingConditions[ i ];
				j++;
			}
		}
	}

	var finals = updateContactFormStep( iStep + 1, aNewRemaining );
	return finals;
}

function selectOption( selname, num )
{
	var options = document.getElementsByName(selname)[0].options;
	for (a = 0; a < options.length; a++ )
		if ( a == num )
			options[a].selected = true;
		else
			options[a].selected = false;
}

function replaceSelect( ktery )
{
	var sel = getSelect( ktery );
	var itms = sel.options.length;
	var all;
	var children;
	var selchi

	if ( itms <= 2)
	{
		var opt = sel.options[1];
		
		// check if strong is present
		all = document.getElementById('r'+ktery);
		if (all == null) return;
		children = all.childNodes;
		selchi = -1;
		for ( a = 0; a < children.length; a++ )
		{
			if ( children.item( a ).nodeName.toLowerCase() == "strong" )
				selchi = a;
		}


		if ( selchi == -1)
		{
			var inntext = document.getElementById( 'r' + ktery ).innerHTML;
			
			var inntexta = inntext.split( '</label>' );
			// IE fix :(
			if ( inntexta.length == 1 ) inntexta =  inntext.split( '</LABEL>' );
			document.getElementById( 'r' + ktery ).innerHTML =
				inntexta[0] + "</label>" +
				"\n<strong>" + opt.text + "</strong>" +
				inntexta[1];
		}

		selectOption( ktery, 1 );

		document.getElementsByName(ktery)[0].style.display = 'none';
	}
	else
	{
		document.getElementsByName(ktery)[0].style.display = 'inline';

		all = document.getElementById('r'+ktery);
		if (all == null) return;
		children = all.childNodes;
		selchi = -1;
		for ( a = 0; a < children.length; a++ )
		{
			if ( children.item( a ).nodeName.toLowerCase() == "strong" && selchi == -1 )
				selchi = a;
		}
		if ( selchi != -1 )
			all.removeChild( children.item( selchi ) );
	}
}

//------------- basic funcs ----------
function updateContactForm()
{
	resc_map = new Array( map.length );
	iterator = 0;
	for( var i = 0; i < map.length; i++ )
	{
		if( map[ i ].indexOf( '-' ) >= 0 )
		{
			var tmp = map[ i ].split( '-' );
			for( var j = 0; j < tmp.length; j++ )
			{
				tmp[ j ] = tmp[ j ].substr( 1 );
				if( tmp[ j ].substr( 0, 1 ) == '[' )
				{
					tmp[ j ] = tmp[ j ].substr( 1, tmp[ j ].length-2 );
				}
				tmp[ j ] = tmp[ j ].split( ',' );
			}

			resc_map[ i ] = tmp;
		}
		else
		{
			resc_map[ i ] = map[ i ].substr( 1 );
		}
	}
//alert( resc_map );
	krok = 0;
	if( updateContactForm.arguments.length > 0 )
	{
		var tmp = updateContactForm.arguments[ 0 ];
		var i = selects.indexOf( tmp.name );
		if( tmp != null && i >= 0 )
		{
			krok_vals[ i ] = updateContactForm.arguments[ 0 ].value;
		}
	}

	var finals = updateContactFormStep( 0, resc_map );
	// take finals and insert items into form elements

	/*for( var i = 0; i < selects.length; i++ )
	{
		var item = getSelect( selects[ i ] );
		//if( j + value_next_shift >= options[ uCFs + 1 ].length ) break;
		//alert( item.type != 'hidden' );
		var type = item.type != 'hidden';
		if( type == true )
		{
			clear_this( item );
			//var step = finals[ 0 ][ i ];
			for( var j = 0; j < finals.length; j++ )
			{
				var vals = finals[ j ][ i ];
				for( var k = 0; k < vals.length; k++ )
				{
					var text = getTextByVal( vals[ k ], i );
					var tmp = isInOptions( item, vals[ k ] );
					if( tmp < 0 && text != '' )
					{
						var oOption = document.createElement( 'OPTION' );
						oOption.value = vals[ k ];
						var tmp = 0;
						tmp += resc_vals[ i ][ 0 ][ 0 ] == '' || resc_vals[ i ][ 0 ][ 0 ] == '0' ? 1 : 0;
						tmp += resc_vals[ i ][ 1 ][ 0 ] == '' || resc_vals[ i ][ 1 ][ 0 ] == '0' ? 1 : 0;
						oOption.text = text;
						if( krok_vals[ i ] == oOption.value )
						{
							oOption.defaultSelected = oOption.selected = true;
						}
						else
						{
							oOption.defaultSelected = oOption.selected = false;
						}
						insertOption( item, oOption );
						
					}
				}
			}
		}
		else
		{
			//alert( item.type );
		}
		//mame polozky pro dany krok, pridame je
		//alert( step );
	}*/
	firstRun = false;

	replaceSelect( "issue" );
	replaceSelect( "subissue" );
	replaceSelect( "os" );
}

if( !Array.merge )
{
	Array.prototype.merge = function()
	{
		if( this.merge.arguments.length > 0 )
		{
			var arrays = this.merge.arguments;
			for( var i = 0; i < this.merge.arguments.length; i++ )
			{
				arrays.concat( this.merge.agruments[ i ] );
			}
			for( var i = 0; i < this.length; i++ )
			{
				if( this.indexOf( arrays[ i ] ) < 0 )
				{
					this[ this.length ] = arrays[ i ];
				}
			}
		}
		//else
		//{
			return this;
		//}
	}
}

function showHideAll( action )
{
	var sets = document.getElementsByTagName( 'fieldset' );
	if( action == false )//provode se schovani polozek
	{
		for( var i = 1; i < sets.length; i++ )
		{
			sets[ i ].style.display = 'none';
		}
	}
	else
	{
		for( var i = 1; i < sets.length; i++ )
		{
			sets[ i ].style.display = '';
		}
	}
}



function showContactFormFields()
{
	fieldsToShow = hideConds;

	if( fieldsToShow == null ) return false;
	if( fieldsToShow.length == 0 ) return;
	if( typeof( fieldsToShow ) == 'string' )
	{
		fieldsToShow = fieldsToShow.split( ',' );
	}
	var d1 = getSelect( 'qtype' );
	if( d1 == null ) return false;
	// this part handles the request in bug 25804
	if( d1.value == '' )
	{
		showHideAll( false );
		return;//here we end the script, because no further processing is required.
	}
	else
	{
		showHideAll( true );
	}


	d1 = ( d1.value == '' && krok_vals[ 0 ] != '' ) ? krok_vals[ 0 ] : d1.value;
	var d2 = getSelect( 'avgver' );
	if( d2 == null ) return false;
	d2 = ( d2.value == '' && krok_vals[ 1 ] != '' ) ? krok_vals[ 1 ] : d2.value;
	var elems_to_hide = new Array();
	var elems_to_show = new Array();
	for( var i = 0; i < fieldsToShow.length; i++ )
	{
		var cond = fieldsToShow[ i ].c;
		if( cond.length > 1 )
		{
			if( cond[ 0 ] == d1 && cond[ 1 ] == d2 )
			{
				hide = fieldsToShow[ i ].h;
				if( typeof hide == 'string' )
					hide = [ hide ];
				for( var j = 0; j < nums.length; j++ )
				{
					var elem = document.getElementById( 'r' + nums[ j ] );
					if( elem == null ) continue;
					if( hide.indexOf( j ) > -1 )
					{
						elems_to_hide[ elems_to_hide.length ] = nums[ j ];
						elem.style.display = 'none';
					}
					else
					{
						elems_to_show[ elems_to_show.length ] = nums[ j ];
						elem.style.display = '';
					}
				}
				break;
			}
			else
			{
				continue;
			}
		}
		else
		{
			if( cond[ 0 ] == d1 )
			{
				var hide = fieldsToShow[ i ].h;
				for( var j = 0; j < nums.length; j++ )
				{
					var elem = document.getElementById( 'r' + nums[ j ].toString() );
					if( elem == null ) continue;
					if( hide.indexOf( j ) > -1 )
					{
						elem.style.display = 'none';
						elems_to_hide[ elems_to_hide.length ] = nums[ j ];
					}
					else
					{
						elem.style.display = '';
						elems_to_show[ elems_to_show.length ] = nums[ j ];
					}
				}
				break;
			}
			else
			{
				continue;
			}
		}
	}
	if( elems_to_hide.indexOf( 'issue' ) >=0 && elems_to_hide.indexOf( 'subissue' ) >=0 )
	{
		document.getElementById( 'rissue' ).parentNode.style.display = 'none';
	}
	if( elems_to_show.indexOf( 'issue' ) >=0 || elems_to_show.indexOf( 'subissue' ) >=0 )
	{
		document.getElementById( 'rissue' ).parentNode.style.display = '';
	}
}

resc_map = null;
debug = true;
resc_vals = new Array();
krok_vals = new Array();
function clear()
{
	//let's backup actual data

	if( typeof( selects ) == 'string' )
	{
		selects = selects.split( ',' );
	}
	for( var krok = 0; krok < selects.length; krok++ )
	{
		//if( krok > 0 )
		var item = getSelect( selects[ krok ] );
		krok_vals[ krok ] = item.value;
		var tmp = new Array();

		if( item.options != null && resc_vals[ krok ] == null )
		{
			var opt = 0, i = 0;
			while( item.options.length > i )
			{
				tmp[ opt ] = [ 
					new String( item.options[ i ].value ), 
					new String( item.options[ i ].text ), 
					false 
				];
				if( tmp[ opt ][ 0 ] != '' && tmp[ opt ][ 0 ] != 0 )
				{
					item.remove( i );
				}
				else
				{
					i++;
				}
				opt++;
			}
			resc_vals[ krok ] = tmp;
		}

		/*else
		{
			krok_vals[ krok ] = '';
			resc_vals[ krok ] = [];
		}*/
	}
}

function clear_this( item )
{
	if( item == null || item.options == null ) return false;

	for( var o = 0; o < item.options.length; )
	{
		if( item.options[ o ].value != '' && item.options[ o ].value != 0 )
		{
			item.remove( o );
		}
		else
		{
			o++;
		}
	}
	return true;
}

function processPage()
{
	UserAgent = navigator.userAgent
	AgentName = UserAgent.substring(25,30)
	firstRun = true;

	showContactFormFields();
	clear();
	updateContactForm();
}

