/**
*   Little JS hack to prefill forms 
*   bosselmann@gmail.com
*/

// list of form ids, just push ids into this list
// like   prefillForms.push('myid');
var prefillForms = [];

$(document).ready(function () {
	// expect variable prefillFormId to be set
	$.each(prefillForms, function() {
		
		if(typeof this != "undefined" || $('#'+this)) {
			var cbname = this;
			
			 $("#"+cbname).submit(function() {
			 	if($('#checkbox-'+cbname).is(':checked')) {
			     	 var data = $("#"+cbname+' :input').serializeArray();
			     	 
			     	 $.cookie(cbname, $.toJSON(data));
		     	 } else {
		     	 	$.cookie(cbname, '');
		     	 }
		        return true;
		    });

			$('#'+this+" input[type=submit]").after('<div style="clear:both; margin-top:10px;"> <input style="float:left;"  type="checkbox" name="checkbox-'+this+'" id="checkbox-'+this+'" value="yes" /> <div style="float:left; margin-left:10px; width:360px;">If you have cookies enabled for Nova Energy in your web browser, by ticking this box your details will be saved on your computer for the next time you order.</div></div>');
			
			prefillForm(this);
		}
	});
});


log = function(obj) {
	if(typeof console != "undefined") {
		console.log(obj);
	}
}

prefillForm = function(formid) {
	// get Cookie
	var cookie = $.cookie(formid);
	if(cookie != '') {
		var data = $.evalJSON(cookie);
		$('#'+formid).deserialize(data);
	} else {
		return false;
	}
};
