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