| 1 |
|
| 2 |
function smartFormGenerator(options){ |
| 3 |
this.client_form_options=options.client_form_options; |
| 4 |
try{ |
| 5 |
this.JavaScriptCode=eval(this.client_form_options.JavascriptCode)(); |
| 6 |
|
| 7 |
}catch(exception) |
| 8 |
{ |
| 9 |
|
| 10 |
} |
| 11 |
this.form_id=options.form_id; |
| 12 |
this.options=options; |
| 13 |
this.RedNaoFormElements=[]; |
| 14 |
this.FormElements=[]; |
| 15 |
var elementOptions=options.elements; |
| 16 |
for(var i=0;i<elementOptions.length;i++) |
| 17 |
{ |
| 18 |
var element=RedNaoCreateFormElementByName(elementOptions[i].ClassName,elementOptions[i]); |
| 19 |
this.RedNaoFormElements.push(element); |
| 20 |
this.FormElements.push(element); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
this.JQueryForm=rnJQuery('<form ></form>'); |
| 25 |
|
| 26 |
this.containerName=options.container; |
| 27 |
var container=this.GetRootContainer(); |
| 28 |
container.empty(); |
| 29 |
container.append(this.JQueryForm); |
| 30 |
for(var i=0;i<this.RedNaoFormElements.length;i++) |
| 31 |
{ |
| 32 |
var formElement=this.RedNaoFormElements[i]; |
| 33 |
formElement.AppendElementToContainer(this.JQueryForm); |
| 34 |
|
| 35 |
if(formElement.StoresInformation()) |
| 36 |
RedNaoFormulaManagerVar.Data[formElement.Id]=formElement.GetValueString(); |
| 37 |
} |
| 38 |
|
| 39 |
var self=this; |
| 40 |
this.JQueryForm.submit(function(e){e.preventDefault();e.stopPropagation();self.SaveForm();}) |
| 41 |
this.AdjustLayout(); |
| 42 |
RedNaoFormulaManagerVar.RefreshAllFormulas(); |
| 43 |
|
| 44 |
try{ |
| 45 |
this.JavaScriptCode.AfterFormLoaded(); |
| 46 |
}catch(exception) |
| 47 |
{ |
| 48 |
|
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
smartFormGenerator.prototype.AdjustLayout=function() |
| 54 |
{ |
| 55 |
var labelArray=[]; |
| 56 |
var controlsArray=[]; |
| 57 |
var maxWidth=0; |
| 58 |
var maxControlWidth=0; |
| 59 |
for(var i=0;i<this.FormElements.length;i++) |
| 60 |
{ |
| 61 |
var element=this.FormElements[i].JQueryElement; |
| 62 |
|
| 63 |
var label=element.find('.rednao_label_container'); |
| 64 |
maxWidth=Math.max(maxWidth,label.width()); |
| 65 |
labelArray.push(label); |
| 66 |
|
| 67 |
var control=element.find('.redNaoControls'); |
| 68 |
maxControlWidth=Math.max(maxControlWidth,control.width()); |
| 69 |
controlsArray.push(control); |
| 70 |
} |
| 71 |
|
| 72 |
for(var i=0;i<labelArray.length;i++) |
| 73 |
labelArray[i].width(maxWidth); |
| 74 |
|
| 75 |
for(var i=0;i<controlsArray.length;i++) |
| 76 |
controlsArray[i].width(maxControlWidth); |
| 77 |
|
| 78 |
this.maxWidth=maxWidth+maxControlWidth; |
| 79 |
/* |
| 80 |
this.maxWidth=0; |
| 81 |
for(var i=0;i<this.FormElements.length;i++) |
| 82 |
{ |
| 83 |
var width=0; |
| 84 |
this.FormElements[i].JQueryElement.find('.rednao_label_container,.redNaoControls').each(function() |
| 85 |
{ |
| 86 |
width+=rnJQuery(this).width(); |
| 87 |
}); |
| 88 |
this.maxWidth=Math.max(this.maxWidth,width); |
| 89 |
}*/ |
| 90 |
|
| 91 |
if(this.JQueryForm.width()<this.maxWidth) |
| 92 |
this.JQueryForm.parent().addClass('redNaoCompactForm'); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
smartFormGenerator.prototype.GenerateFormElements=function(formElementsOptions) |
| 97 |
{ |
| 98 |
this.FormElements=new Array(); |
| 99 |
|
| 100 |
for(var i=0;i<formElementsOptions.length;i++) |
| 101 |
{ |
| 102 |
this.FormElements.push(RedNaoCreateFormElementByOptions(formElementsOptions[i])); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
smartFormGenerator.prototype.DonationGeneratedCode=function() |
| 108 |
{ |
| 109 |
return this.GetStartOfDonationForm()+this.GetEndOfDonationForm(); //'<form id="redNaoElementlist" class="formelements" style="width:600px;"></form>'; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
smartFormGenerator.prototype.GenerationCompleted=function() |
| 114 |
{ |
| 115 |
var form=this.GetRootContainer().find('form'); |
| 116 |
form.addClass('formelements').attr('id','redNaoElementlist'); |
| 117 |
|
| 118 |
for(var i=0;i<this.FormElements.length;i++) |
| 119 |
{ |
| 120 |
this.FormElements[i].AppendElementToContainer(form); |
| 121 |
} |
| 122 |
|
| 123 |
var self=this; |
| 124 |
form.find('.redNaoDonationButton').click(function() |
| 125 |
{ |
| 126 |
|
| 127 |
try{ |
| 128 |
|
| 129 |
self.SaveForm(); |
| 130 |
|
| 131 |
}catch(error) |
| 132 |
{ |
| 133 |
|
| 134 |
|
| 135 |
}finally{ |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
smartFormGenerator.prototype.GenerateDefaultStyle=function() |
| 144 |
{ |
| 145 |
this.styles.formelements="width:600px;padding:10px;margin:0px;"; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
smartFormGenerator.prototype.SaveForm=function() |
| 150 |
{ |
| 151 |
|
| 152 |
var formValues={}; |
| 153 |
var formIsValid=true; |
| 154 |
var amount=0; |
| 155 |
|
| 156 |
this.GetRootContainer().find('.redNaoValidationMessage').remove(); |
| 157 |
this.GetRootContainer().find('.redNaoInputText,.redNaoRealCheckBox,.redNaoInputRadio,.redNaoInputCheckBox,.redNaoSelect,.redNaoTextArea').removeClass('redNaoInvalid'); |
| 158 |
for(var i=0;i<this.FormElements.length;i++) |
| 159 |
{ |
| 160 |
|
| 161 |
if((this.FormElements[i].Options.IsRequired==1||this.FormElements[i].Options.IsRequired=='y')&&!this.FormElements[i].IsValid()) |
| 162 |
{ |
| 163 |
formIsValid=false; |
| 164 |
rnJQuery('#'+this.FormElements[i].Id).find('.redNaoInputText,.redNaoRealCheckBox,.redNaoInputRadio,.redNaoInputCheckBox,.redNaoSelect,.redNaoTextArea').addClass('redNaoInvalid'); |
| 165 |
continue; |
| 166 |
} |
| 167 |
if(this.FormElements[i].StoresInformation()) |
| 168 |
{ |
| 169 |
var value=this.FormElements[i].GetValueString(); |
| 170 |
amount+=this.FormElements[i].amount; |
| 171 |
formValues[this.FormElements[i].Id]=value; |
| 172 |
} |
| 173 |
} |
| 174 |
if(!formIsValid) |
| 175 |
{ |
| 176 |
this.GetRootContainer().prepend('<p class="redNaoValidationMessage" style="margin:0;padding: 0; font-style: italic; color:red;font-family:Arial;font-size:12px;">*Please fill all the required fields</p>') |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
if(this.IsRecurrentPayment(this.GetRootContainer().find('form'))) |
| 181 |
{ |
| 182 |
if(amount<=0) |
| 183 |
{ |
| 184 |
alert('Please set a donation amount before proceeding'); |
| 185 |
return; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if(formValues.length>0) |
| 191 |
formValues=formValues.substr(1); |
| 192 |
|
| 193 |
var data={ |
| 194 |
form_id:this.form_id, |
| 195 |
action:"rednao_smart_forms_save_form_values", |
| 196 |
formString:JSON.stringify(formValues) |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
try{ |
| 201 |
this.JavaScriptCode.BeforeFormSubmit(); |
| 202 |
}catch(exception) |
| 203 |
{ |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
var self=this; |
| 208 |
rnJQuery.ajax({ |
| 209 |
type:'POST', |
| 210 |
url:ajaxurl, |
| 211 |
dataType:"json", |
| 212 |
data:data, |
| 213 |
success:function(result){self.SaveCompleted(result)}, |
| 214 |
error:function(result){ |
| 215 |
alert('An error occurred, please try again later');} |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
smartFormGenerator.prototype.SaveCompleted=function(result){ |
| 220 |
alert(result.message); |
| 221 |
} |
| 222 |
|
| 223 |
smartFormGenerator.prototype.SubmitForm=function(data,amount) |
| 224 |
{ |
| 225 |
if(data.status=="success") |
| 226 |
{ |
| 227 |
var form=this.GetRootContainer().find('form'); |
| 228 |
form.attr('target','_self'); |
| 229 |
form.find('input[name=custom]').val(encodeURI('campaign_id='+this.campaign_id+"&formId="+data.randomString)) |
| 230 |
if(amount>0) |
| 231 |
form.append('<input type="hidden" name="amount" class="amountToDonate" value="'+amount+'">') |
| 232 |
|
| 233 |
if(this.IsRecurrentPayment(form)) |
| 234 |
{ |
| 235 |
if(amount<=0) |
| 236 |
{ |
| 237 |
alert('Please set a donation amount before proceeding'); |
| 238 |
return; |
| 239 |
} |
| 240 |
this.TurnFormIntoRecurrentPayment(form); |
| 241 |
} |
| 242 |
|
| 243 |
form.submit(); |
| 244 |
|
| 245 |
|
| 246 |
}else |
| 247 |
{ |
| 248 |
alert("An error occured, please try again"); |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
smartFormGenerator.prototype.TurnFormIntoRecurrentPayment=function(form) |
| 254 |
{ |
| 255 |
form.find('.amountToDonate').attr('name','a3'); |
| 256 |
form.find('.smartDonationsPaypalCommand').val('_xclick-subscriptions'); |
| 257 |
form.append('<input type="hidden" name="src" value="1"><input type="hidden" name="p3" value="1"><input type="hidden" name="t3" value="'+form.find('.redNaoRecurrence').find(':selected').val()+'">'); |
| 258 |
} |
| 259 |
|
| 260 |
smartFormGenerator.prototype.IsRecurrentPayment=function(form) |
| 261 |
{ |
| 262 |
return form.find('.redNaoRecurrence').length>0&&form.find('.redNaoRecurrence').find(':selected').val()!='OT'; |
| 263 |
} |
| 264 |
|
| 265 |
smartFormGenerator.prototype.GetOptions=function() |
| 266 |
{ |
| 267 |
var options=new Object(); |
| 268 |
options.FormElementsOptions=new Array(); |
| 269 |
|
| 270 |
for(var i=0;i<this.FormElements.length;i++) |
| 271 |
{ |
| 272 |
options.FormElementsOptions.push(this.FormElements[i].Options); |
| 273 |
} |
| 274 |
|
| 275 |
return options; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
smartFormGenerator.prototype.GetRootContainer=function() |
| 280 |
{ |
| 281 |
return rnJQuery('#'+this.containerName); |
| 282 |
} |
| 283 |
|
| 284 |
rnJQuery(function(){ |
| 285 |
if( window.smartFormsItemsToLoad) |
| 286 |
for(var i=0;i< window.smartFormsItemsToLoad.length;i++) |
| 287 |
smartFormsLoadForm(window.smartFormsItemsToLoad[i]); |
| 288 |
}); |
| 289 |
|
| 290 |
|
| 291 |
var smartFormsLoadedItems=[]; |
| 292 |
|
| 293 |
function smartFormsLoadForm(options) |
| 294 |
{ |
| 295 |
var form=new smartFormGenerator(options) |
| 296 |
smartFormsLoadedItems.push(form); |
| 297 |
|
| 298 |
|
| 299 |
} |