var aUser = new Array();

/*
 	_A must have a class that ends with hide
	_A and _B must both exist with the same Identifier x in x_*
	A Identifier must not be dupplicated.
*/
window.onload = function(){
	var eTags = document.getElementsByTagName( 'input' );
	for( i = 0; i < eTags.length; i++ ){
		var eTag = eTags[i];
		if( eTag.id.indexOf( '_A' ) > -1 ){
			/*
				For textfield, reweal by removing className 'hide' and set onFocus
			*/
			eTag.className = eTag.className.substr( 0, eTag.className.length-5 );	
			
			eTag.onfocus = function(){
				toggleInput( this, true );	
			};
			
		}else
		if( eTag.id.indexOf( '_B' ) > -1 ){
			/*
				For passwordfield, hide by adding className 'hide' and set onBlur
			*/
			eTag.className = ' hide';	
			
			eTag.onblur = function(){
				toggleInput( this, false );
			};	
			
		}else
		if( eTag.type == 'text' && eTag.className.indexOf( '_txt' ) > -1  ){
			/*
				For the rest textfields, inserts value into array and sets the index of the value as className onFocus.
				className is used to get the right value onBlur.
			*/
			eTag.className += ' __' + setArray( eTag.value );
			
			eTag.onfocus = function(){
				var aTmp = this.className.split( '__' );					
				if( this.value == getArray()[aTmp[aTmp.length-1]] ){
					this.value = '';	
				}
			};
			
			eTag.onblur = function(){
				if( this.value == '' ){
					var aTmp = this.className.split( '__' );
					this.value = getArray()[aTmp[aTmp.length-1]];	
				}
			};
			
		}
	}
}

function inArray( aHaystack, sNeedle ){
	for( j = 0; j < aHaystack.length; j++ ){
		if( sNeedle == aHaystack[j] ){
			return j;	
		}
	}
	return -1;
}

function getArray(){
	return aUser;
}
function setArray( sValue ){
	if( !( sValue in aUser ) ){
		aUser[aUser.length] = sValue;
	}
	return inArray( aUser, sValue );
}

function toggleInput( eTag, bFocus ){
	if( eTag.value == '' || bFocus ){
		var iIndex = eTag.id.indexOf( '_' );
		var sPar = eTag.id.substr( 0, iIndex );
		var sType = eTag.id.substr( iIndex );		
		var eTmp = document.getElementById( sPar + ( sType == '_A' ? '_B': '_A' ) );		
		
		eTag.className += ' hide';

		eTmp.className = eTmp.className.substr( 0, eTmp.className.length-5 );
		sType == '_A' ? eTmp.focus(): null;
	}
}

function moveEmail(){
	oFrom = document.getElementById( 'email' );
	oTo = document.getElementById( 'students' );
	
	oTo.value +=  oTo.value == '' ? oFrom.value :', ' + oFrom.value;
	oFrom.value = '';
	oFrom.focus();
}

function openExc( id ){
	iH = 700;
	iW = 638;
	oNew = window.open( 'index.php?id=99&taskid=' + id, 'exc', 'height=' + iH + ', width=' + iW + ', resizable=no, status=no, toolbar=no, menubar=no, location=no, scrollbars=no, top=' + ( ( screen.height - iH ) / 2 ) + ', left=' + ( ( screen.width - iW ) / 2 ) );
	if( !oNew ){
		return confirm( 'Övningarna är anpassade för popup.' + "\n" + 'Vill du visa övningen utan popup?' + "\n\n" + '(Vill du visa sidan i en popup välj avbryt, tillåt popups i din webbläsare och tryck sedan på länken igen.)' );	
	}else{
		if( !oNew.opener ){
			oNew.opener = self;
		}		
		oNew.focus();	
	}
	return false;
}




