﻿// JScript File



function FormCheck( oInstansName )
{

    this.formName = 0;
    this.instansName = oInstansName;
    
    this.inputTextClassName = 'input';
    this.inputTextReqClassName = 'input';
    this.inputTextErrClassName = 'input';
    
    this.inputTextReqStyle = '1px solid #663300';
    this.inputTextOkStyle  = '1px solid #993300';
    this.inputTextErrStyle = '1px solid #FF3300';
   
    this.generalErrorText = 'Udfyld venligst fremhævede felter';
    
    this.onloadFuncName = this.instansName + '_onload';
    this.submitFuncName = this.instansName + '_onsubmit';
    this.changeFuncName = this.instansName + '_onchange';
    //this.changeFuncName = this.instansName + '.ElementChange';
    this.mandatoryAttributeName = "man";

    
    this.captionAfterFix = "*";
    this.getCaptionHandler = "CaptionParentParentFirstChildGet";
    this.getCaption = eval( this.getCaptionHandler );
   
    
    this.Init = function() 
    {
        this.pageLoadEvtCreate();
    }
    
    this.CheckForm = function(t) {
			// Selbit check script
		    
			var f=this.getForm();
			
			if (! f) return;
		    
			var feltOk;
			var err=false;
			for( a=0; a<f.length; a++ ) 
			{
				feltOk=true;
				o=f[a];
				if (! this.CheckElement(o)) err=true;
				
			}
			return  (! err );
			
    }
    this.SubmitForm = function()
    {
        var f=this.getForm();
        if (! f) return;
        f.submit();
        return true;
    }
    this.CheckElement = function(t)
    {
        var feltOk = false;
        var at = t.getAttribute(this.mandatoryAttributeName);
		if ( at ) 
		{
			if (at=='zip') feltOk =  ( t.value.match(/^[0-9]{4}/) );
			if (at=='str') feltOk =  ( t.value.length>1 );
			if (at=='email') feltOk =  ( t.value.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/) );


			//t.className = (feltOk)?this.inputTextClassName:this.inputTextErrClassName;
			t.style.border = (feltOk)?this.inputTextOkStyle:this.inputTextErrStyle;
			return feltOk;
		}
		return true;
    }
    this.ElementChange = function()
    {
        var t = event.srcElement;
        
        //eventtype=event.type;
        if (t){
            return this.CheckElement(t);
            }
    }
    
    function CaptionParentParentFirstChildGet(t) 
    {
        if ( ! t ) return;
        var oP = t.parentNode;
        var oPP = oP.parentNode;
        if ( oPP.childNodes.length > 0 )
            return oPP.childNodes[0];
    }

	this.PrepForm = function() {
	    
	    var f=this.getForm();
	    
	    
		for( a=0; a<f.length; a++ ) {
			o=f[a];
			 
			at = o.getAttribute('man');
			if ( at ) 
			{
			    o.style.border=this.inputTextReqStyle;
			    var oC = this.getCaption(o);
			    if (oC && oC.innerHTML && this.captionAfterFix) oC.innerHTML = oC.innerHTML + this.captionAfterFix;
			    this.onchangeEvtCreate(o);
		    }
		 }
		 this.subitEvtCreate();
	}

	this.getForm = function()
	{
	    return document.forms[this.formName];
	}

	this.subitEvtCreate = function()
	{
	
	    var F = this.getForm();
	    if (F && eval( this.submitFuncName ) ) F.onsubmit = eval( this.submitFuncName );
	}
	this.onchangeEvtCreate = function(t)
	{
	    if (! t ) return;
	    if (! eval( this.changeFuncName )) return;
	    
	    t.onblur = eval( this.changeFuncName );
	}
	this.pageLoadEvtCreate = function()
	{
	    var s = this.onloadFuncName;
	    window.onload = eval(s);
	    //if ( window.onload && eval(s) ) window.onload += ""+eval(s); else window.onload = eval(s);
	}
	
	this.Init();
}
