//
// SmartForm Rev 2 v0.2
// Author: Tommy Lesmeister
// Company: Goes & Roos
//

var SmartForm = function( parent ) {
//	this.SF_Parent = "loli";
	return this._init( parent );
}

//
// SmartForm Class
//**************************************************************
SmartForm.prototype = {
	// Initialiseren
	//**************************************************************
	_init : function( parent ) {
		this.isIE = document.all||navigator.userAgent.indexOf('MSIE')>=0;
		this.devMode = false;
		var bodyElement = document.getElementsByTagName( "body" ).item( 0 );
		
		// Het verschil...moet er zijn?
		if( this.isIE ) {
			var mainForm = document.createElement( '<form id="SmartForm" name="SmartForm" action="http://websites.goesenroos.nl/GoesenRoos/CRM/RegForm/crmregform.asp" method="post" onsubmit="return ValidateForm();">' );
			mainForm.setAttribute( 'action', "http://websites.goesenroos.nl/GoesenRoos/CRM/RegForm/crmregform.asp" );
		} else {
			var mainForm = document.createElement( "form" );
			mainForm.setAttribute( 'id', 'SmartForm' );
			mainForm.setAttribute( 'name', 'SmartForm' );
			mainForm.setAttribute( 'action', "http://websites.goesenroos.nl/GoesenRoos/CRM/RegForm/crmregform.asp" );
			mainForm.setAttribute( 'onSubmit', 'return ValidateForm();');
			mainForm.setAttribute( 'method', 'post' );
		}

		// Developers mode
		if( this.devMode )
			alert("DevMode activated. Let op er kunnen nu fouten optreden, maak je niet druk de module word geupdate.");
			
		// Toevoegen van de SmartFormLogger
		this.addLogbox( mainForm );

		if( parent )
			document.getElementById( parent ).insertBefore( mainForm,  document.getElementById( "Klantinfo" ) );
		else
			bodyElement.appendChild( mainForm );
		
		return this;		
	},
	
	// addLogbox
	//**************************************************************
	addLogbox : function( parent ) {
		var logBox = document.createElement( "div" );
		
		logBox.setAttribute( 'id', 'SmartFormLog' );
		
		logBox.style.display = "none";
		
		parent.appendChild( logBox );
	},
	
	// setValue
	setValue : function( id, val ) {
		document.getElementById( id ).innerHTML = val;
	},
	// getValue
	//**************************************************************
	getValue : function( id ) {
		return document.getElementById( id ).value;
	},
	
	// getParent
	//**************************************************************
	getParent : function() {
		return document.getElementById( "SmartForm" );
	},
	
	// getRow
	//**************************************************************
	getRow : function( id ) {
		return document.getElementById( id + "row" );
	},
	
	// existsRow
	//**************************************************************
	existsRow : function( id ) {
		return ( document.getElementById( id + "row" ) == null||!document.getElementById( id + "row" ) ) ? true : false;
	},
	
	// removeRow
	//**************************************************************
	removeRow : function( id ) {
		var curRow = document.getElementById( id + "row" );
		curRow.parentNode.removeChild( curRow );
	},

	// parse_label voor het aanmaken van spans.
	// 08/04/2010
	//**************************************************************
	parse_labels : function( label ) {
		if( !label.match(/\*/) )
			return label;
	
		return label.replace( /(\*)/g, "<span class=\"smart_star\">$1</span>" );
	},
	// addLabel
	//**************************************************************
	addLabel : function( parent, name, attr ) {
		var label = document.createElement( "div" );
		// 18/03/10 - Label naar div
		label.className = "smartlabel";
		label.innerHTML = this.parse_labels(name);
			
		if( attr ) {
			for( var prop in attr ) {
			//	if( prop == "id" )
			//		continue;
				label.setAttribute( prop, attr[ prop ] );
			}
		}
		
		parent.appendChild( label );
		
		return this;
	},
	
	// addElements
	// Ge-avancheerde methode voor het toevoegen van elementen
	//**************************************************************
	addElement : function( type, parent, name, attr, evts ) {
		if( this.isIE )
			var field = document.createElement('<input name="' + name + '" type="' + type + '">');
		else {
			var field = document.createElement('input');
			field.setAttribute( "name", name );
			field.setAttribute( "type", type );
		}
		
		// 19-05-2010 Tommy - radio button fix..
		if(type=="radio") {
			if(!document.getElementById(name)||document.getElementById(name)==null||typeof document.getElementById(name)=="undefined")
				field.count = 0;
			else {
				document.getElementById(name).count++;
				
			name = name + document.getElementById(name).count;
			}
		}
		
		field.setAttribute( "id", name );
		
		if( attr ) {
			for( var prop in attr ) {
				field.setAttribute( prop, attr[ prop ] );
				
				// Toevoeging voor tooltips
/* 				if( prop == "rel" ) {
					// Voordat we een koppeling maken, even kijken of het object wel bestaat
					if( typeof GR_Tooltip.prototype.toggle == 'function' ) // IE / FF Proof...
						field[ "onfocus" ] = function() { myGR_Tooltip.toggle( this ); };
				} */
			}
		}
		
		if( evts ) {
			for( var evt in evts ) {
				field[ evt ] = evts[ evt ];
			}
		}
		
		parent.appendChild( field );
	},
	
	// addRelink
	// Voegt een redirect toe na het verzenden van het formulier
	//**************************************************************
	addRelink : function( link ) {
		// Voeg toe als er http:// voor staat ;)
		if( link.length > 5 )
			this.addElement( "hidden", this.getParent(), "sfc_relink", { value:link } );
	},
	
	// addKlant
	// Geef het klantid voor verdere afhandeling
	//**************************************************************
	addKlant : function( klant ) {
		this.addElement( "hidden", this.getParent(), "sfc_klant", { value:klant } );
	},
	
	// addRel
	addRel : function( rel ) {
		this.addElement( "hidden", this.getParent(), "sfc_relatie", { value:rel } );
	},
	
	// addCRM
	addCRM : function( url ) {
		this.getParent().setAttribute( 'action', url+"CRM/RegForm/crmregform.asp" );
	},
	
	addCmd : function( cmd ) {
		this.addElement( "hidden", this.getParent(), "sfc_cmd", { value:cmd } );
	},
	
	addEmail : function( mail ) {
		this.addElement( "hidden", this.getParent(), "sfc_subject", { value:mail } );
	},
	
	addEmailTo : function( mail ) {
		this.addElement( "hidden", this.getParent(), "sfc_mailto", { value:mail } );
	},
	
	addEmailContent : function( content ) {
		this.addElement( "hidden", this.getParent(), "sfc_mailcont", { value:content } );
	},
	
	addDiv : function( parent, id, attr, evts, val ) { // Ik wou het niet...Maar moet maar toch..
		var div = document.createElement( "span" ); // 18/11 : Changed from div?
		
		if( id != "" )
			div.setAttribute( "id", id );
		
		// Niet moooi, maar... ik moest?
		if( val ) {
		//	if( this.isIE ) div.innerText = val;
		//	else div.textContent = val;
			div.innerHTML = val;
		}
		
		if( attr ) {
			for( var prop in attr ) {
				if( prop == "className" ) {
					// Firefox fix, span blijft width 0 anders...
					/* if( !this.isIE ) {  */div.setAttribute( "class", attr[ prop ] ); //continue; } // Aangepast voor vinnie ivm ie8:08-02-2010
					div.innerHTML = "&nbsp;";
				}
				
				div.setAttribute( prop, attr[ prop ] );
			}
		}
		
		if( evts ) {
			for( var evt in evts ) {
				div[ evt ] = evts[ evt ];
			}
		}
		
		parent.appendChild( div );
		
		return this;
	},
	
	// addText
	// Voegt een input text veld toe
	//**************************************************************
	addText : function( validatie, parent, name, attr, evts ) {
		if( typeof window.isValMethod == 'function') {
			if( isValMethod( SFV_ONTHEFLY ) && validatie > 0 ) {
				if( !evts ) var evts = [];
				evts[ "onblur" ] = function() { ValidateMe( this ); };
			}
		}
		
		this.addElement( "text", parent, name, attr, evts );
		
		if( validatie > 0 )
			this.addElement( "hidden", parent, "valtype_"+name, { value:validatie } );
			
		// Test test
		this.addElement( "hidden", parent, "zzzsort_"+name );
			
		return this;
	},
	
	// addCheck
	// Voegt een checkbox toe aan de opgegeven parent
	//**************************************************************
	addCheck : function( validatie, parent, name, attr, evts ) {
		this.addElement( "checkbox", parent, name, attr, evts );
		
		if( validatie > 0 ) {
			if( !document.getElementById( "valtype_"+name )||document.getElementById( "valtype_"+name )==null||typeof document.getElementById( "valtype_"+name ) == "undefined" )
				this.addElement( "hidden", parent, "valtype_"+name, { value:validatie } );
		}
			
		return this;
	},
	
	// addRadio
	// Voegt een radioknop toe aan de opgegeven parent
	//**************************************************************
	addRadio : function( validatie, parent, name, attr, evts ) {
		this.addElement( "radio", parent, name, attr, evts );
		
		/* Tommy +- 28-04-2010 toevoeging validatie radio`ssss op parent?
			toevoeging kan in csvalidator.js getElementsByTagName("input") if input.attribute("type")=="radio"
		*/
		if( validatie > 0 ) {
			if( !document.getElementById( "valtype_"+name )||document.getElementById( "valtype_"+name )==null||typeof document.getElementById( "valtype_"+name ) == "undefined" )
				this.addElement( "hidden", parent, "valtype_"+name, { value:validatie } );
		}
			
		return this;
	},
	
	// addTextarea
	// Voegt een textarea toe aan de parent
	//**************************************************************
	addTextarea : function( validatie, parent, name, attr, evts ) {
		// Genereer het element
		if( document.all )
			var textarea = document.createElement( '<textarea id="' + name + '" name="' + name + '">' );
		else {
			var textarea = document.createElement( 'textarea' );
			textarea.setAttribute( "id", name );
			textarea.setAttribute( "name", name );
		}
		
		if( typeof window.isValMethod == 'function') {
			if( isValMethod( SFV_ONTHEFLY ) && validatie > 0 ) {
				if( !evts ) var evts = [];
				evts[ "onblur" ] = function() { ValidateMe( this ); };
			}
		}
		
		if( attr ) {
			for( var prop in attr ) {
				textarea.setAttribute( prop, attr[ prop ] );
			}
		}
		
		if( evts ) {
			for( var evt in evts ) {
				textarea[ evt ] = evts[ evt ];
			}
		}
		
		parent.appendChild( textarea );
		
		if( validatie > 0 )
			this.addElement( "hidden", parent, "valtype_"+name, { value:validatie } );
			
		// Test test
		this.addElement( "hidden", parent, "zzzsort_"+name );
		
		return this;
	},
	
	// addSelect
	// Voor het toevoegen van een textarea veld
	//**************************************************************
	addSelect : function( parent, name, childs, evts ) {
		if( document.all )
			var select = document.createElement( '<select id="'+name+'" name="'+name+'">' );
		else {
			var select = document.createElement( 'select' );
			select.setAttribute( "id", name );
			select.setAttribute( "name", name );
		}
		
		if( childs ) {
			for( var child in childs ) {
				if( document.all ) 
					var option = document.createElement('<option name="'+name+'" value="' + childs[ child ] + '">');
				else {
					var option = document.createElement('option');
					option.setAttribute( "name", name );
					option.setAttribute( "value", childs[ child ] );
				}
				
				if( this.isIE )
					option.innerText = child;
				else
					option.textContent = child;
					
				select.appendChild( option );
			}
		}
		
		if( evts ) {
			for( var evt in evts ) {
				select[ evt ] = evts[ evt ];
			}
		}
		
		parent.appendChild( select );
		
		return this;
	},
	
	// addButton
	// Voor het toevoegen van een knop aan de parent
	//**************************************************************
	addButton : function( parent, name, attr, evts ) {
		var button = document.createElement( 'button' );
		
		if( this.isIE ) {
			var button = document.createElement( '<button type="submit">' );
			button.innerText = name;
		} else {
			var button = document.createElement( 'button' );
			button.textContent = name;
		}
		
		if( attr ) {
			for( var prop in attr ) {
				button.setAttribute( prop, attr[ prop ] );
			}
		}
		
		if( evts ) {
			for( var evt in evts ) {
				button[ evt ] = evts[ evt ];
			}
		}
		
		parent.appendChild( button );
		
		return this;
	},
	
	// addRow
	// Voegt 1 parent toe waarin elementen geplaatst kunnen worden
	//**************************************************************
	addRow : function( id, name, attr ) {
		var row = document.createElement( "div" );
		
		// Add id
		row.setAttribute( "id", id+"row" );
		
		if( name.length>=1 ) {
			this.addLabel( row, name, attr );
		} else {
			this.addDiv( row, "", { className:"label_spacer" } );
		}
		
		this.getParent().appendChild( row );
		
		return this;
	}
};

var SmartFormInit = function() { initSmartForm(); };

if (window.addEventListener) {
	window.addEventListener( "load", SmartFormInit, false );
} else if( window.attachEvent ) {
	window.attachEvent( "onload", SmartFormInit );
} else {
	window.onload = function() { SmartFormInit(); }
}