/**
 * Includes a Form with javascript
 * @param {Object} formId
 * @param {Object} initialHeight
 * @param {Object} iframeCode
 */
function FrameBuilder (formId, initialHeight, iframeCode){
	this.formId = formId;
	this.initialHeight = initialHeight;
	this.iframeCode = iframeCode;
	this.frame = null;
	this.timeInterval= 200;
    
	// initialize function for object
	this.init = function(){
		this.createFrame();
		this.addFrameContent(this.iframeCode);
	};
    
	// Create the frame
	this.createFrame = function(){
		var htmlCode = "<"+"iframe src=\"\" allowtransparency=\"true\" frameborder=\"0\" name=\""+this.formId+"\" id=\""+this.formId+"\" style=\"width:100%; height:"+this.initialHeight+"px; border:none;\" scrolling=\"no\"></if"+"rame>";
		document.write(htmlCode);
        // also get the frame for future use.
        this.frame = document.getElementById(this.formId);
        // set the time on the on load event of the frame
        this.addEvent (this.frame, 'load', this.bindMethod(this.setTimer, this));
	};
    
	// add event function for different browsers
	this.addEvent = function( obj, type, fn ) {
		if ( obj.attachEvent ) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); };
			obj.attachEvent( "on"+type, obj[type+fn] );
		}
		else{
			obj.addEventListener( type, fn, false );   
		}
    };
    
	this.addFrameContent = function (string){
        string = string.replace(new RegExp('src\=\"[^"]*captcha.php\"><\/scr'+'ipt>', 'gim'), 'src="http://api.recaptcha.net/js/recaptcha_ajax.js"></scr'+'ipt><'+'div id="recaptcha_div"><'+'/div>'+
        	    '<'+'style>#recaptcha_logo{ display:none;} #recaptcha_tagline{display:none;} #recaptcha_table{border:none !important;} .recaptchatable .recaptcha_image_cell, #recaptcha_table{ background-color:transparent !important; } <'+'/style>'+
        	    '<'+'script defer="defer"> window.onload = function(){ Recaptcha.create("6Ld9UAgAAAAAAMon8zjt30tEZiGQZ4IIuWXLt1ky", "recaptcha_div", {theme: "clean",tabindex: 0,callback: function (){'+
        	    'if (document.getElementById("uword")) { document.getElementById("uword").parentNode.removeChild(document.getElementById("uword")); } if (window["validate"] !== undefined) { if (document.getElementById("recaptcha_response_field")){ document.getElementById("recaptcha_response_field").onblur = function(){ validate(document.getElementById("recaptcha_response_field"), "Required"); } } } if (document.getElementById("recaptcha_response_field")){ document.getElementsByName("recaptcha_challenge_field")[0].setAttribute("name", "anum"); } if (document.getElementById("recaptcha_response_field")){ document.getElementsByName("recaptcha_response_field")[0].setAttribute("name", "qCap"); }}})'+
        	    ' }<'+'/script>');
        string = string.replace(/(type="text\/javascript">)\s+(validate\(\"[^"]*"\);)/, '$1 jTime = setInterval(function(){if("validate" in window){$2clearTimeout(jTime);}}, 1000);');
    	var frameDocument = (this.frame.contentWindow) ? this.frame.contentWindow : (this.frame.contentDocument.document) ? this.frame.contentDocument.document : this.frame.contentDocument;
		frameDocument.document.open();
		frameDocument.document.write(string);
		setTimeout( function(){frameDocument.document.close();},200);
	};
    
	this.setTimer = function(){
		var self = this;
		this.interval = setTimeout(function(){self.changeHeight();},this.timeInterval);
	};
    
	this.changeHeight = function (){
	    var actualHeight = this.getBodyHeight();
		var currentHeight = this.getViewPortHeight();
        if(actualHeight === undefined){
            this.frame.style.height = "100%";
            this.frame.style.minHeight = "300px";
        }else if  (Math.abs(actualHeight - currentHeight) > 18){
	    	this.frame.style.height = (actualHeight)+"px";
	    }
		this.setTimer();
	};
    
	this.bindMethod = function(method, scope) {
		return function() {
			method.apply(scope,arguments);
		};
	};
    
	this.getBodyHeight = function (){
	    var height;
	    var scrollHeight;
	    var offsetHeight;
        try{  // Prevent IE from throw errors
    	    if (this.frame.contentWindow.document.height){
                
    	        height = this.frame.contentWindow.document.height;
                
    		} else if (this.frame.contentWindow.document.body){
                
    	        if (this.frame.contentWindow.document.body.scrollHeight){
    	            height = scrollHeight = this.frame.contentWindow.document.body.scrollHeight;
    	        }
                
    	        if (this.frame.contentWindow.document.body.offsetHeight){
    	            height = offsetHeight = this.frame.contentWindow.document.body.offsetHeight;
    	        }
                
    	        if (scrollHeight && offsetHeight){
    	            height = Math.max(scrollHeight, offsetHeight);
    	        }
    	    }            
        }catch(e){ }
	    return height;
	};
    
	this.getViewPortHeight = function(){
	    var height = 0;
        try{ // Prevent IE from throw errors
    	    if (this.frame.contentWindow.window.innerHeight)
    	    {
    	        height = this.frame.contentWindow.window.innerHeight - 18;
    	    }
    	    else if ((this.frame.contentWindow.document.documentElement)
    			&& (this.frame.contentWindow.document.documentElement.clientHeight))
    	    {
    	        height = this.frame.contentWindow.document.documentElement.clientHeight;
    	    }
    	    else if ((this.frame.contentWindow.document.body)
    			&& (this.frame.contentWindow.document.body.clientHeight))
    	    {
    	        height = this.frame.contentWindow.document.body.clientHeight;
    	    }            
        }catch(e){ }
	    return height;
	};
    
	this.init();
}
new FrameBuilder("1464605066" ,"", "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\">\n<html><head>\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\n<meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" \/>\n<meta name=\"HandheldFriendly\" content=\"true\" \/>\n<title>Form<\/title>\n<link type=\"text\/css\" rel=\"stylesheet\" href=\"http:\/\/www.jotform.com\/css\/styles\/form.css?v3\"\/>\n<link type=\"text\/css\" rel=\"stylesheet\" href=\"http:\/\/www.jotform.com\/css\/styles\/industrial_dark.css\" \/>\n<link href=\"http:\/\/www.jotform.com\/css\/calendarview.css\" rel=\"stylesheet\" type=\"text\/css\" \/>\n<style type=\"text\/css\">\n    .form-label{\n        width:105px !important;\n    }\n    .form-label-left{\n        width:105px !important;\n    }\n    .form-line{\n        padding:10px;\n    }\n    .form-label-right{\n        width:105px !important;\n    }\n    body, html{\n        margin:0;\n        padding:0;\n        background:#FFFFFF;\n    }\n\n    .form-all{\n        margin:0px auto;\n        padding-top:20px;\n        width:639px;\n        background:#FFFFFF;\n        color:#000000 !important;\n        font-family:\"Gill Sans\";\n        font-size:12px;\n    }\n<\/style>\n\n<script src=\"http:\/\/www.jotform.com\/min\/g=jotform&3.1.1\" type=\"text\/javascript\"><\/script>\n<script type=\"text\/javascript\">\nvar jsTime = setInterval(function(){try{JotForm.jsForm = true;\n   JotForm.init(function(){\n      $('input_6').hint('ex: myname@example.com');\n   });\nclearInterval(jsTime);}catch(e){}}, 1000);<\/script>\n<\/head>\n<body>\n<form class=\"jotform-form\" action=\"http:\/\/www.jotform.com\/submit.php\" method=\"post\" name=\"form_1464605066\" id=\"1464605066\" accept-charset=\"utf-8\">\n    <input type=\"hidden\" name=\"formID\" value=\"1464605066\" \/>\n    <div class=\"form-all\">\n        <ul class=\"form-section\">\n            <li class=\"form-line\" id=\"id_7\">\n                <label class=\"form-label-left\" id=\"label_7\" for=\"input_7\"> Full Name <\/label>\n                <div id=\"id_7\" class=\"form-input\"><span class=\"form-sub-label-container\"><input class=\"form-textbox\" type=\"text\" size=\"10\" name=\"q7_fullName[first]\" id=\"first_7\" \/>\n                        <label class=\"form-sub-label\" for=\"first_7\" id=\"sublabel_first\"> First Name <\/label><\/span><span class=\"form-sub-label-container\"><input class=\"form-textbox\" type=\"text\" size=\"15\" name=\"q7_fullName[last]\" id=\"last_7\" \/>\n                        <label class=\"form-sub-label\" for=\"last_7\" id=\"sublabel_last\"> Last Name <\/label><\/span>\n                <\/div>\n            <\/li>\n            <li class=\"form-line\" id=\"id_6\">\n                <label class=\"form-label-left\" id=\"label_6\" for=\"input_6\"> E-mail <\/label>\n                <div id=\"id_6\" class=\"form-input\">\n                    <input type=\"text\" class=\"form-textbox validate[Email]\" id=\"input_6\" name=\"q6_email6\" size=\"30\" \/>\n                <\/div>\n            <\/li>\n            <li class=\"form-line\" id=\"id_5\">\n                <label class=\"form-label-left\" id=\"label_5\" for=\"input_5\"> Your Message <\/label>\n                <div id=\"id_5\" class=\"form-input\">\n                    <textarea id=\"input_5\" class=\"form-textarea\" name=\"q5_yourMessage\" cols=\"40\" rows=\"6\"><\/textarea>\n                <\/div>\n            <\/li>\n            <li class=\"form-line\" id=\"id_8\">\n                <div id=\"id_8\" class=\"form-input-wide\">\n                    <div style=\"margin-left:111px\" class=\"form-buttons-wrapper\">\n                        <button id=\"input_8\" type=\"submit\" class=\"form-submit-button\">\n                            Submit Form\n                        <\/button>\n                    <\/div>\n                <\/div>\n            <\/li>\n            <li style=\"display:none\">\n                Should be Empty:\n                <input type=\"text\" name=\"website\" value=\"\" \/>\n            <\/li>\n        <\/ul>\n    <\/div>\n    <input type=\"hidden\" id=\"simple_spc\" name=\"simple_spc\" value=\"1464605066\" \/>\n    <script type=\"text\/javascript\">\n        document.getElementById(\"si\" + \"mple\" + \"_spc\").value = \"1464605066-1464605066\";\n    <\/script>\n<\/form><\/body>\n<\/html>\n");
