1if (typeof window.Forms == 'undefined') window.Forms = {}; 2 3;(function(global, $){ 4 //es5 strict mode 5 "use strict"; 6 7 if (typeof window.jQuery == 'undefined') { 8 alert("The Forms Module requires jQuery 1.3 or higher. \n(jQuery not found)"); 9 return; 10 } 11 12 if (typeof global.Forms == 'undefined') global.Forms = {}; 13 global.Forms.Conditionals = {}; 14 15 // ---------------------------------------------------------------------- 16 17 $(document).ready(function(){ 18 19 if (typeof $.fn.closest == 'undefined') { 20 alert("The Forms Module requires jQuery 1.3 or higher. \n(closest method not found)"); 21 } 22 23 var FormField, Input; 24 25 // Find all Forms 26 Forms.dform = $('div.dform'); 27 28 // Did are there any conditionals stored? 29 for (var Field in Forms.Conditionals){ 30 var FIELD_ID = Field.substr(3); 31 // Does is Exists? 32 if (document.getElementById('forms_field_'+FIELD_ID) === null) continue; 33 34 // just in case 35 if(typeof Forms.Conditionals[Field].conditionals == 'undefined') continue; 36 37 for (var i = 0; i < Forms.Conditionals[Field].conditionals.length; i++) { 38 var Cond = Forms.Conditionals[Field].conditionals[i]; 39 FormField = $('#forms_field_'+Cond.field); 40 Input = FormField.find(':input[name^="fields\\['+Cond.field+'\\]"]'); // Matches field names with array 41 //if (Input.data('form_event_added') == true) continue; 42 if (Input.length === 0) { 43 // Maybe paginated? 44 Input = Forms.dform.closest('form').find(':input[name^="fields\\['+Cond.field+'\\]"]'); 45 if (Input.length === 0) continue; 46 else { 47 FormField = Input.parent(); 48 } 49 } 50 51 Input.data('form_event_added', true); 52 if (Input[0].nodeName == 'SELECT') Input.bind('change.forms_'+Cond.field, {'condkey':i, 'formfield':FormField, 'senderfield':Field},Forms.ExecuteConditional).trigger('change'); 53 else if (Input[0].type == 'radio') Input.bind('click.forms_'+Cond.field, {'condkey':i, 'formfield':FormField, 'senderfield':Field},Forms.ExecuteConditional).filter(':checked').trigger('click'); 54 else if (Input[0].type == 'checkbox') Input.bind('click.forms_'+Cond.field, {'condkey':i, 'formfield':FormField, 'senderfield':Field},Forms.ExecuteConditional).filter(':checked').trigger('chnage'); 55 else Input.bind('keyup.forms_'+Cond.field, {'condkey':i, 'formfield':FormField, 'senderfield':Field},Forms.ExecuteConditional).trigger('keyup'); 56 57 } 58 } 59 60 // Disable commas etc 61 Forms.dform.find('div.dform_cart_product').find('.user_defined').bind('keydown', function(e){ 62 63 var decimal_sep = 1.1; 64 decimal_sep = decimal_sep.toLocaleString().substring(1, 2); 65 66 // Comma 67 if (e.which == 188) { 68 e.preventDefault(); 69 return false; 70 } 71 72 // Dot 73 if (e.which == 190 || e.which == 110) { 74 if ($(this).val().indexOf('.') != -1) { 75 e.preventDefault(); 76 return false; 77 } 78 else return true; 79 } 80 81 if (e.which == 8 || e.which == 46) return true; // Backspace & Delete 82 83 // Arrows 84 if (e.which >= 37 && e.which <= 40) return true; 85 86 if ((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105)) { 87 return; 88 } 89 90 e.preventDefault(); 91 return false; 92 }); 93 94 if (Forms.dform.find('span.forms_cart_total').length > 0) { 95 Forms.dform.find('div.dform_cart_product').bind('click keyup change', Forms.CalculateCartTotal); 96 Forms.dform.find('div.dform_cart_coupons').bind('click keyup change', Forms.CalculateCartTotal); 97 Forms.dform.find('div.dform_cart_quantity').bind('click keyup change', Forms.CalculateCartTotal); 98 Forms.dform.find('div.dform_cart_product_options').bind('click keyup change', Forms.CalculateCartTotal); 99 Forms.dform.find('div.dform_cart_product_options_multi').bind('change', Forms.CalculateCartTotal); 100 Forms.dform.find('div.dform_cart_shipping').bind('click keyup change', Forms.CalculateCartTotal); 101 Forms.CalculateCartTotal(); 102 } 103 104 // Products Multi! 105 Forms.dform.find('.dform_cart_product_options_multi').delegate('td.del a', 'click', removeTrRow); 106 Forms.dform.find('.dform_cart_product_options_multi').delegate('.addrow', 'click', addTrRowOptionMulti); 107 Forms.dform.find('.dform_cart_product_options_multi').each(function(i, elem){ 108 syncOrderNumbersOptionMulti($(elem)); 109 }); 110 111 }); 112 113 // ---------------------------------------------------------------------- 114 115 function removeTrRow(e) { 116 e.preventDefault(); 117 var parent = $(e.target).closest('.dform_element'); 118 $(e.target).closest('tr').remove(); 119 120 syncOrderNumbersOptionMulti(parent); 121 } 122 123 // ---------------------------------------------------------------------- 124 125 function addTrRowOptionMulti(e) { 126 e.preventDefault(); 127 var parent = $(e.target).closest('.dform_element'); 128 var html = parent.find('script.rowtmpl').html(); 129 parent.find('table tbody').append(html); 130 131 syncOrderNumbersOptionMulti(parent); 132 } 133 134 // ---------------------------------------------------------------------- 135 136 function syncOrderNumbersOptionMulti(parent) { 137 //console.log(parent); 138 parent.find('.optionrow').each(function(index, row){ 139 row = $(row); 140 141 row.find(':input').each(function(findex,felem){ 142 if (!felem.getAttribute('name')) return false;
143 144 // Get it's attribute and change it 145 var attr = $(this).attr('name').replace(/\[options\]\[.*?\]/, '[options][' + (index+1) + ']'); 146 $(this).attr('name', attr); 147 }); 148 }); 149 } 150 151 // ---------------------------------------------------------------------- 152 153 154}(window, (window.jQuery ? window.jQuery : {}) )); 155 156 157// ********************************************************************************* // 158 159 160// ********************************************************************************* // 161 162 163 164 165 166Forms.AddConditional = function(FieldID, Cond){ 167 Forms.Conditionals['ID_'+FieldID] = Cond; 168}; 169 170//********************************************************************************* // 171 172Forms.ExecuteConditional = function(Event){ 173 if (typeof(Event.data) == 'undefined') return; 174 if (typeof(Event.data.condkey) == 'undefined') return; 175 if (typeof(Forms.Conditionals[Event.data.senderfield].conditionals[Event.data.condkey]) == 'undefined') return; 176 var Cond = Forms.Conditionals[Event.data.senderfield].conditionals[Event.data.condkey]; 177 var Values = []; 178 var Passed = false; 179 180 // Is it a radio button? 181 if (Event.target.type == 'radio' && Event.target.nodeName == 'INPUT') { 182 Values.push(Event.data.formfield.find(':input[name^="fields\\['+Cond.field+'\\]"]').filter(':checked').val()); 183 } else if (Event.target.type == 'checkbox' && Event.target.nodeName == 'INPUT') { 184 Event.data.formfield.find(':input[name^="fields\\['+Cond.field+'\\]"]').filter(':checked').each(function(ii, ee){ 185 Values.push($(ee).val()); 186 }); 187 } else { 188 Values.push(Event.data.formfield.find(':input[name^="fields\\['+Cond.field+'\\]"]').val()); 189 } 190 191 Cond.value = $.trim(Cond.value).toLowerCase(); 192 193 // Trim all values 194 for (var i = Values.length - 1; i >= 0; i--) { 195 Values[i] = $.trim(Values[i]).toLowerCase(); 196 197 // If it's empty, kill it.. 198 if (Values[i] == false) { 199 //Values.splice(i, 1); 200 } 201 } 202 203 if (Values.length > 0) { 204 switch(Cond.operator) { 205 case 'is': 206 for (var i = Values.length - 1; i >= 0; i--) { 207 if (Values[i] == Cond.value) Passed = true; 208 } 209 break; 210 case 'isnot': 211 for (var i = Values.length - 1; i >= 0; i--) { 212 if (Values[i] != Cond.value) Passed = true; 213 } 214 break; 215 case 'greater_then': 216 for (var i = Values.length - 1; i >= 0; i--) { 217 if (parseFloat(Values[i]) > parseFloat(Cond.value)) Passed = true; 218 } 219 break; 220 case 'less_then': 221 for (var i = Values.length - 1; i >= 0; i--) { 222 if (parseFloat(Values[i]) < parseFloat(Cond.value)) Passed = true; 223 } 224 break; 225 case 'contains': 226 for (var i = Values.length - 1; i >= 0; i--) { 227 if (Values[i].toLowerCase().indexOf(Cond.value.toLowerCase()) != -1) Passed = true; 228 } 229 break; 230 case 'starts_with': 231 for (var i = Values.length - 1; i >= 0; i--) { 232 if (Values[i].toLowerCase().lastIndexOf(Cond.value.toLowerCase(), 0) === 0) Passed = true; 233 } 234 break; 235 case 'ends_with': 236 for (var i = Values.length - 1; i >= 0; i--) { 237 if (Values[i].toLowerCase().slice(-Cond.value.toLowerCase().length) == Cond.value.toLowerCase() ) Passed = true; 238 } 239 break; 240 } 241 } 242 243 Cond.passed = Passed; 244 Forms.FinalizeConditional(Event.data.senderfield); 245}; 246 247//********************************************************************************* // 248 249Forms.FinalizeConditional = function(FIELD){ 250 var FIELD_ID = FIELD.substr(3); 251 var Field = $('#forms_field_'+FIELD_ID); 252 var Cond = Forms.Conditionals[FIELD]; 253 254 if (Cond.options.match == 'all'){ 255 var PassedAll = true; 256 for (var i = Cond.conditionals.length - 1; i >= 0; i--) {
257 if (typeof(Cond.conditionals[i].passed) == 'undefined') PassedAll = false; 258 if (Cond.conditionals[i].passed == false) PassedAll = false; 259 } 260 261 if (PassedAll){ 262 if (Cond.options.display == 'show') Field.show(); 263 if (Cond.options.display == 'hide') Field.hide(); 264 } else { 265 if (Cond.options.display == 'show') Field.hide(); 266 if (Cond.options.display == 'hide') Field.show(); 267 } 268 } else { 269 var PassedAny = false; 270 271 for (var i = Cond.conditionals.length - 1; i >= 0; i--) { 272 if (typeof(Cond.conditionals[i].passed) == 'undefined') PassedAny = false; 273 if (Cond.conditionals[i].passed == true) PassedAny = true; 274 } 275 276 if (PassedAny == true){ 277 if (Cond.options.display == 'show') Field.show(); 278 if (Cond.options.display == 'hide') Field.hide(); 279 } else { 280 if (Cond.options.display == 'show') Field.hide(); 281 if (Cond.options.display == 'hide') Field.show(); 282 } 283 } 284 285 Forms.CalculateCartTotal(); 286}; 287 288//********************************************************************************* // 289 290Forms.CalculateCartTotal = function(e){ 291 292 Forms.form_elements = Forms.dform.find('div.dform_element:visible'); 293 294 Forms.Products = []; 295 Forms.Products_Shipping = 0; 296 Forms.Products_FeesMarkup = []; 297 298 // Loop over all fields 299 Forms.form_elements.each(function(i, elem){ 300 elem = $(elem); 301 302 // Product? 303 if (elem.hasClass('dform_cart_product')) { 304 Forms.ParseCart_Products(elem); return; 305 } 306 307 // Product Options 308 if (elem.hasClass('dform_cart_product_options')) { 309 Forms.ParseCart_Options(elem); return; 310 } 311 312 // Product Options Multi 313 if (elem.hasClass('dform_cart_product_options_multi')) { 314 Forms.ParseCart_OptionsMulti(elem); return; 315 } 316 317 // Quantity? 318 if (elem.hasClass('dform_cart_quantity')) { 319 Forms.ParseCart_Quantity(elem); return; 320 } 321 322 // Shipping 323 if (elem.hasClass('dform_cart_shipping')) { 324 Forms.ParseCart_Shipping(elem); return; 325 } 326 327 // Fees & Markup 328 if (elem.hasClass('dform_cart_fees_markup')) { 329 Forms.ParseCart_FeesMarkup(elem); return; 330 } 331 332 // Coupons 333 if (elem.hasClass('dform_cart_coupons')) { 334 Forms.ParseCart_Coupons(elem); return; 335 } 336 337 }); 338 339 var TotalCart = 0; 340 var startTotal = Forms.dform.find('span.forms_cart_total').data('start_total'); 341 342 if (startTotal) { 343 TotalCart = parseFloat(Forms.dform.find('span.forms_cart_total').data('start_total')); 344 } 345 346 for (var i = Forms.Products.length - 1; i >= 0; i--) { 347 var OptionsTotal = 0, ProductTotal = 0, LineTotal = 0; 348 349 if (!Forms.Products[i].price) continue; 350 if (!Forms.Products[i].qty) continue; 351 352 if (Forms.Products[i].option) { 353 for (var ii = Forms.Products[i].option.length - 1; ii >= 0; ii--) { 354 OptionsTotal += parseFloat(Forms.Products[i].option[ii]); 355 } 356 } 357 358 ProductTotal = (parseFloat(OptionsTotal) + parseFloat(Forms.Products[i].price)); 359 360 if (Forms.Products[i].qty) LineTotal += (parseFloat(Forms.Products[i].qty) * parseFloat(ProductTotal)); 361 else LineTotal += parseFloat(ProductTotal); 362 363 if (Forms.Products[i].extraPrice) { 364 LineTotal += Forms.Products[i].extraPrice; 365 } 366 367 TotalCart += parseFloat(LineTotal); 368 } 369 370 TotalCart += parseFloat(Forms.Products_Shipping); 371 372 if (Forms.Products_Coupons) { 373 if (Forms.Products_Coupons.substr(-1) == '%') { 374 TotalCart -= (TotalCart / 100) * Forms.Products_Coupons.slice(0,-1); 375 } else { 376 TotalCart -= parseFloat(Forms.Products_Coupons); 377 } 378 } 379 380 if (Forms.Products_FeesMarkup) { 381 for (var i = 0; i < Forms.Products_FeesMarkup.length; i++) { 382 383 if (Forms.Products_FeesMarkup[i].substr(-1) == '%') { 384 TotalCart += (TotalCart / 100) * Forms.Products_FeesMarkup[i].slice(0,-1); 385 } else { 386 TotalCart += parseFloat(Forms.Products_FeesMarkup[i]); 387 } 388 } 389 } 390 391 if (TotalCart < 0) { 392 TotalCart = 0; 393 } 394 395 var totalCart = parseFloat(TotalCart).toFixed(2); 396 397 Forms.dform.closest('form').data('cart_total', totalCart); 398 Forms.dform.find('span.forms_cart_total').html(totalCart); 399 400 if (e) { 401 Forms.dform.find('.dform_cart_total').find('input').attr('value', totalCart).trigger('keyup'); 402 } 403}; 404 405//********************************************************************************* // 406 407Forms.ParseCart_Products = function(elem){ 408 var Product = {}; 409 var Selected; 410 411 // Single Product? 412 if (elem.find('input.single_product').val() == 'single' || elem.find('input.single_product').val() == 'entry_product' ) { 413 Product.price = elem.find('input.single_product').attr('data-price'); 414 415 if (elem.find('input.cart_quantity').length > 0) { 416 Product.qty = $.trim(elem.find('input.cart_quantity').val()); 417 Product.qty_field = true; 418 } else { 419 Product.qty = 1; 420 Product.qty_field = false;
421 } 422 423 Product.fieldtype = 'single_product'; 424 Forms.Products.push(Product); 425 return; 426 } 427 428 // Radio? 429 if (elem.find('ul.radios').length > 0) { 430 Selected = elem.find('input[type=radio]:checked'); 431 if (Selected.length === 0) return; 432 Product.price = Selected.closest('li').attr('data-price'); 433 434 if (elem.find('input.cart_quantity').length > 0) { 435 Product.qty = $.trim(elem.find('input.cart_quantity').val()); 436 Product.qty_field = true; 437 } else { 438 Product.qty = 1; 439 Product.qty_field = false; 440 } 441 442 Product.fieldtype = 'radio'; 443 Forms.Products.push(Product); 444 return; 445 } 446 447 // Checkbox 448 if (elem.find('ul.checkboxes').length > 0) { 449 Selected = elem.find('input[type=checkbox]:checked'); 450 Selected.each(function(i, el){ 451 el = $(el).closest('li'); 452 Product = {}; 453 Product.price = el.attr('data-price'); 454 if (el.find('input.cart_quantity').length > 0) { 455 Product.qty = $.trim(el.find('input.cart_quantity').val()); 456 Product.qty_field = true; 457 } else { 458 Product.qty = 1; 459 Product.qty_field = false; 460 } 461 Product.fieldtype = 'checkbox'; 462 Forms.Products.push(Product); 463 }); 464 return; 465 } 466 467 // Dropdown 468 if (elem.find('select').length > 0) { 469 Selected = elem.find('select option:selected'); 470 if (Selected.length === 0) return; 471 Product.price = Selected.attr('data-price'); 472 Product.qty = 1; 473 Product.qty_field = false; 474 Product.fieldtype = 'dropdown'; 475 Forms.Products.push(Product); 476 return; 477 } 478 479 // User Defined 480 if (elem.find('input.user_defined').length > 0) { 481 Product.price = elem.find('input.user_defined').val(); 482 Product.qty = 1; 483 Product.qty_field = false; 484 Product.fieldtype = 'user_defined'; 485 Forms.Products.push(Product); 486 return; 487 } 488}; 489 490//********************************************************************************* // 491 492Forms.ParseCart_Quantity = function(elem){ 493 var Qty = elem.find('input,select').val(); 494 495 for (var i = 0; i < Forms.Products.length; i++) { 496 if (!Forms.Products[i].qty || !Forms.Products[i].qty_field) { 497 Forms.Products[i].qty = Qty; 498 } 499 } 500}; 501 502//********************************************************************************* // 503 504Forms.ParseCart_Options = function(elem){ 505 506 var Options = []; 507 var Selected; 508 509 // Radio? 510 if (elem.find('ul.radios').length > 0) { 511 Selected = elem.find('input[type=radio]:checked'); 512 if (Selected.length === 0) return; 513 Options.push(Selected.parent().attr('data-price')); 514 } 515 516 // Checkbox 517 else if (elem.find('ul.checkboxes').length > 0) { 518 Selected = elem.find('input[type=checkbox]:checked'); 519 Selected.each(function(i, el){ 520 el = $(el).parent(); 521 Options.push(el.attr('data-price')); 522 }); 523 } 524 525 // Dropdown 526 else if (elem.find('select').length > 0) { 527 Selected = elem.find('select option:selected'); 528 if (Selected.length === 0) return; 529 Options.push(Selected.attr('data-price')); 530 } 531 532 if (Options.length === 0) return; 533 534 for (var i = 0; i < Forms.Products.length; i++) { 535 if (!Forms.Products[i].option) Forms.Products[i].option = Options; 536 } 537}; 538 539//********************************************************************************* // 540 541Forms.ParseCart_OptionsMulti = function(elem){ 542 543 var product = Forms.Products[Forms.Products.length -1]; 544 var totalExtra=0, totalQty=0; 545 var price, qty; 546 547 if (!product.extraPrice) { 548 product.extraPrice = 0; 549 } 550 551 elem.find('.optionrow').each(function(i, row){ 552 row = $(row); 553 product.price = parseFloat(product.price); 554 product.qty = parseFloat(product.qty); 555 price = parseFloat(row.find('.option select option').filter(':selected').data('price')); 556 qty = parseFloat(row.find('.qty select').val()); 557 558 price = qty * price; 559 560 totalExtra += price; 561 totalQty += qty; 562 563 }); 564 565 product.extraPrice += totalExtra; 566 product.qty = totalQty; // Override main qty 567 product.qty_field = true; 568}; 569 570//********************************************************************************* // 571 572Forms.ParseCart_Shipping = function(elem) { 573 var Selected; 574 575 // Single Ship? 576 if (elem.find('input.single_ship').val() == 'single') { 577 Forms.Products_Shipping = elem.find('input.single_ship').attr('data-price'); 578 return; 579 } 580 581 // Radio? 582 if (elem.find('ul.radios').length > 0) { 583 Selected = elem.find('input[type=radio]:checked'); 584 if (Selected.length === 0) return; 585 Forms.Products_Shipping = Selected.parent().attr('data-price'); 586 return; 587 } 588 589 // Dropdown 590 if (elem.find('select').length > 0) { 591 Selected = elem.find('select option:selected'); 592 if (Selected.length === 0) return; 593 Forms.Products_Shipping = Selected.attr('data-price'); 594 return; 595 } 596 597}; 598 599//********************************************************************************* // 600 601Forms.ParseCart_FeesMarkup = function(elem) { 602 603 Forms.Products_FeesMarkup.push(elem.find('.fee_markup').data('price').toString() ); 604}; 605 606//********************************************************************************* // 607 608Forms.ParseCart_Coupons = function(elem) { 609 610 // If it's disabled continue on! 611 var attr = elem.find('input[type=text]').attr('disabled'); 612 if (typeof attr !== 'undefined' && attr !== false) return; 613 614 Forms.Products_Coupons = null; 615 var code = elem.find('input[type=text]').val(); 616 var coupons = jQuery.parseJSON(elem.find('.dd-coupons').html()); 617 var final_coupon; 618 619 if (typeof(coupons[code]) != 'undefined') { 620 coupons[code].code = jQuery.trim( coupons[code].code ); 621 if (!coupons[code].code) return; 622 623 Forms.Products_Coupons = coupons[code].discount; 624 625 elem.find('input[type=text]').attr('disabled', 'disabled').attr('value', coupons[code].label + ' (-'+coupons[code].discount+')'); 626 elem.find('input[type=hidden]').attr('value', code); 627 } 628 629}; 630 631//********************************************************************************* // 632
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.