var forms_sErrorCheck = "Vinarliga útfyll feltið";
var forms_sErrorEmail = "Vinarliga skriva eina teldupost adr.";
var forms_sErrorDigits = "Einans tøl eru loyvd"
var forms_sErrorHacker = "html er ikki loyvt!"

function forms_editor_validate ( oForm, sFunction ) { // validate them forms

		window.bSubmit = true;
		
		// collect fields
		var aInputs = new Array ();
		var aInputfields = oForm.getElementsByTagName ( "input" );
		var aTextareas = oForm.getElementsByTagName ( "textarea" );
		for ( var i = 0; i < aInputfields.length; i++ ) aInputs [ aInputs.length ] = aInputfields.item ( i );
		for ( var i = 0; i < aTextareas.length; i++ ) aInputs [ aInputs.length ] = aTextareas.item ( i );
		
		// scan fields
		for ( var i=0; i < aInputs.length; i++ ) {

			var oInput = aInputs [ i ];
			
			// only check displayed fields
			if ( !forms_isRelevant ( oInput )) continue;
			
			// check for any entry
			else if ( oInput.id.indexOf ( "check" ) !=-1 && oInput.value == "" || oInput.value == forms_sErrorCheck ) 
				forms_displayError ( oInput, forms_sErrorCheck );
			
			// check for digits only
			else if ( oInput.id.indexOf ( "digits" ) !=-1 ) {
				var sValueClone = oInput.value;
				while ( sValueClone.indexOf ( "." ) !=-1 ) sValueClone = sValueClone.replace ( ".", "" );
				while ( sValueClone.indexOf ( "," ) !=-1 ) sValueClone = sValueClone.replace ( ",", "" );
				if ( isNaN ( sValueClone ) || oInput.value == forms_sErrorDigits || oInput.value == "" )
					forms_displayError ( oInput, forms_sErrorDigits );
			}
			
			// check for email syntax
			else if ( oInput.id.indexOf ( "email" ) !=-1 && ( oInput.value == "" || oInput.value.indexOf ( "@" ) ==-1 || oInput.value.indexOf ( "." ) ==-1 || oInput.value == forms_sErrorEmail )) 
				forms_displayError ( oInput, forms_sErrorEmail );
			
			// check for markup tags [default check]
//			if ( oInput.value.indexOf ( "<" ) !=-1 && oInput.value.indexOf ( ">" ) !=-1 ) 
//				forms_displayError ( oInput, forms_sErrorHacker );
		}
		form.inpContentTxt.value = oEdit1.getHTMLBody();

		// don't submit forms intended for clientside parsing
		if ( oForm.action == "clientside" && window.bSubmit ) eval ( sFunction );
		if ( oForm.action == "clientside" ) return false;
		else return window.bSubmit;
}

function forms_validate ( oForm, sFunction ) { // validate them forms

		window.bSubmit = true;
		
		// collect fields
		var aInputs = new Array ();
		var aInputfields = oForm.getElementsByTagName ( "input" );
		var aTextareas = oForm.getElementsByTagName ( "textarea" );
		for ( var i = 0; i < aInputfields.length; i++ ) aInputs [ aInputs.length ] = aInputfields.item ( i );
		for ( var i = 0; i < aTextareas.length; i++ ) aInputs [ aInputs.length ] = aTextareas.item ( i );
		
		// scan fields
		for ( var i=0; i < aInputs.length; i++ ) {

			var oInput = aInputs [ i ];
			
			// only check displayed fields
			if ( !forms_isRelevant ( oInput )) continue;
			
			// check for any entry
			else if ( oInput.id.indexOf ( "check" ) !=-1 && oInput.value == "" || oInput.value == forms_sErrorCheck ) 
				forms_displayError ( oInput, forms_sErrorCheck );
			
			// check for digits only
			else if ( oInput.id.indexOf ( "digits" ) !=-1 ) {
				var sValueClone = oInput.value;
				while ( sValueClone.indexOf ( "." ) !=-1 ) sValueClone = sValueClone.replace ( ".", "" );
				while ( sValueClone.indexOf ( "," ) !=-1 ) sValueClone = sValueClone.replace ( ",", "" );
				if ( isNaN ( sValueClone ) || oInput.value == forms_sErrorDigits || oInput.value == "" )
					forms_displayError ( oInput, forms_sErrorDigits );
			}
			
			// check for email syntax
			else if ( oInput.id.indexOf ( "email" ) !=-1 && ( oInput.value == "" || oInput.value.indexOf ( "@" ) ==-1 || oInput.value.indexOf ( "." ) ==-1 || oInput.value == forms_sErrorEmail )) 
				forms_displayError ( oInput, forms_sErrorEmail );
			
			// check for markup tags [default check]
//			if ( oInput.value.indexOf ( "<" ) !=-1 && oInput.value.indexOf ( ">" ) !=-1 ) 
//				forms_displayError ( oInput, forms_sErrorHacker );
		}

		// don't submit forms intended for clientside parsing
		if ( oForm.action == "clientside" && window.bSubmit ) eval ( sFunction );
		if ( oForm.action == "clientside" ) return false;
		else return window.bSubmit;
}

function forms_displayError ( oNode, sString ) { // display warning and add listener to normalize field on interaction

		oNode.setAttribute ( "originalString", oNode.value == sString ? "" : oNode.value );
		oNode.style.color = "#FF4900";
		oNode.value = sString;
		oNode.onmousedown = forms_resetField;
		oNode.onfocus = forms_resetField;
		window.bSubmit = false;
}

function forms_resetField ( e ) { // normalize field onclick/onfocus

		if ( !e ) var e = window.event;
		var node = e.target ? e.target : e.srcElement;
		if ( node.nodeType == 3 ) node = node.parentNode;
		
		node.value = node.getAttribute ( "originalString" ) ? node.getAttribute ( "originalString" ) : "";
		node.style.color = "#000000";
		node.onmousedown = null
		node.onfocus = null
}

function forms_isRelevant ( node ) { // don't validate undisplayed fields

		var bReturnable = true;
		if ( node.type == "hidden" ) bReturnable = false;
		else while ( node.parentNode.tagName.toLowerCase () != "form" ) {
			if ( node.parentNode.style.display == "none" ) bReturnable = false;
			node = node.parentNode;
		}
		return bReturnable;
}
