// $Id: form_scripts.js 5022 2008-03-31 14:09:16Z imac $ // // Extend wrapper // Object.extend = function(source, destination) { //override source object properties with props of new object for (k in destination) { source[k] = destination[k]; } return source; } $A = Array.from = function(iterable) { if (!iterable) { return []; } if (iterable.toArray) { return iterable.toArray(); } else { var results = []; for (var i = 0, length = iterable.length; i < length; i++) { results.push(iterable[i]); } return results; } } // --- Javascript prototypes ---------- // str_replace prototype wrapper String.prototype.str_replace = function(src, dst) { return this.toString().split(src).join(dst); } String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } // object bind prototype Function.prototype.bind = function() { var __method = this; var args = $A(arguments); var object = args.shift(); return function() { return __method.apply(object, args.concat($A(arguments))); } } // load htmlarea // Automatically calculates the editor base path based on the current URL. var html_editor_url = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('/')) + '/classes/fckeditor/'; var document_loaded = false; var fn_inheritance = {}; var html_editor_window; var html_editor_id; // // Global definitions // function fn_add_event(elm, event, func) { if (elm.attachEvent) { elm.attachEvent('on' + event, func); } else if (elm.addEventListener) { elm.addEventListener(event, func, true); } else { elm['on' + event] = func; } } // // Show/hide any tag by its id. // parameters: // id - element id // @status - can be true (expand) or false (collapse) function fn_show_tag(id, status, stop_listen) { if (document.getElementById(id)) { if (status == true || status == false) { document.getElementById(id).style.display = (status == true)?"none":""; } else { document.getElementById(id).style.display = (document.getElementById(id).style.display == "")?"none":""; } } if (stop_listen == true) { window.onscroll = ''; } } function fn_disable_elms(ids, status) { for(var i in ids){ if (document.getElementById(ids[i])){ document.getElementById(ids[i]).disabled = status; } } } // // Change section and // id - section id function fn_show_section(id, scts, separate_form, redraw_all) { for (i in scts) { // Hide empty tabs if (document.getElementById('content_'+i) && document.getElementById('content_'+i).innerHTML.trim() == '') { document.getElementById('content_'+i).style.display = 'none'; document.getElementById('tab_'+i+'_bg').style.display = 'none'; } if (i == id) { if (document.getElementById('product_save_button')) { document.getElementById('product_save_button').style.display = (separate_form == 'Y') ? 'none' : ''; } if (document.getElementById('content_'+i)) { document.getElementById('content_'+i).style.display = ''; } document.getElementById('tab_'+i+'_bg').className = 'section-active-tab-bg'; if (document.getElementById('tab_'+i+'_left')){ document.getElementById('tab_'+i+'_left').src = tab_left_active_image_path; } if (document.getElementById('tab_'+i+'_right')){ document.getElementById('tab_'+i+'_right').src = tab_right_active_image_path; } if (document.getElementsByName('selected_section')) { elms = document.getElementsByName('selected_section'); for (var k=0; k 0) { alert(message + skipped_elms.join(', ')); } return true; } function fn_move_field(id, direction) { elm = document.getElementById(id); var selected_idx = elm.selectedIndex; var total = elm.length; var move = true; var exchange_idx = 0; if (selected_idx != -1) { if ((direction == 'up' && selected_idx == 0) || direction == 'down' && selected_idx == total -1) { return false; } if (direction == 'up') { exchange_idx = selected_idx - 1; } if (direction == 'down') { exchange_idx = selected_idx + 1; } exchange = {text: elm.options[exchange_idx].value, value: elm.options[exchange_idx].value, classname: elm.options[exchange_idx].className}; elm.options[exchange_idx].value = elm.options[selected_idx].value; elm.options[exchange_idx].text = elm.options[selected_idx].text; elm.options[exchange_idx].className = elm.options[selected_idx].className; elm.options[selected_idx].value = exchange.value; elm.options[selected_idx].text = exchange.text; elm.options[selected_idx].className = exchange.classname; elm.selectedIndex = exchange_idx; } } // // Select all elements in selects input // function fn_select_save_elements() { var args = fn_select_save_elements.arguments; for (i = 0; i < args.length; i++) { if (document.getElementById(args[i])) { var select = document.getElementById(args[i]); for(var k = select.length-1; k >= 0; k--) { select.options[k].selected = true; } } } return true; } // // Unselect all elements in selects input // function fn_unselect_save_elements() { var args = fn_unselect_save_elements.arguments; for (i = 0; i < args.length; i++) { if (document.getElementById(args[i])) { var select = document.getElementById(args[i]); for(var k = select.length-1; k >= 0; k--) { select.options[k].selected = false; } } } return true; } // // Select text in text input // function fn_select_input(select) { select.select(); } // Compare 2 strings // @haystack - where search // @needle - what search // @strict - exact compare or partial function fn_compare_strings(haystack, needle, strict) { if (strict == true) { return (haystack == needle); } else { return (haystack.indexOf(needle) == -1) ? false : true; } } // // Check if there are any selected checkboxes in the form // @form_name - form name in which checkboxes should be selected or deselected // @checkbox_id - tag id of checkboxes that should be selected or deselected // @no_alert - do not display notification // @strict - compare ids strictly or partially function fn_check_selected(form_name, checkbox_id, no_alert, strict) { { if (typeof(strict) == 'undefined') { strict = true; } if (!checkbox_id) { checkbox_id = 'delete_checkbox'; } if(!(d_form = document.forms[form_name])) { return false; } for(i=0; i < d_form.length; i++) { if (fn_compare_strings(d_form.elements[i].id, checkbox_id, strict)) { if (d_form.elements[i].checked) { return true; } } } if (!no_alert) { alert(lang.error_no_items_selected); } return false; } } function fn_delete_selected(form_name, mode_value, no_confirmation, checkbox_id, elm_name, strict) { if (typeof(strict) == 'undefined') { strict = true; } if (!fn_check_selected(form_name, checkbox_id, '', strict)) { return false; } if (!no_confirmation) { if (!confirm(lang.delete_confirmation)) { return false; } } if (typeof(elm_name)== 'undefined' || elm_name.length == 0) { elm_name = mode_name; } document.forms[form_name].elements[elm_name].value = mode_value; document.forms[form_name].submit(); return true; } cscart_handlers = { document_loaded: false, handlers: [], set_handler: function() { var args = $A(this.set_handler.arguments); // If document is already loaded - execute function right now if (this.document_loaded) { fn = args.shift(); if (fn) { fn.apply(fn, args); } } else { this.handlers.push({'function': args.shift(), 'args': args}); } }, load: function() { if (this.handlers) { for (i=0; i]*>([\u0001-\uFFFF]*?)', 'img'); var regex_one = new RegExp(']*>([\u0001-\uFFFF]*?)', 'im'); var matches = []; var match = ''; if (req.responseJS.force_redirection) { fn_redirect(req.responseJS.force_redirection); } if (req.responseJS.html) { for (k in req.responseJS.html) { src = document.getElementById(k); if (!src) { continue; } matches = req.responseJS.html[k].match(regex_all); src.innerHTML = matches ? req.responseJS.html[k].replace(regex_all, '') : req.responseJS.html[k]; if (matches) { for (var i=0 ; i< matches.length ; i++ ) { if (matches[i].match(regex_one)[1]) { if (window.execScript) { window.execScript(matches[i].match(regex_one)[1]); } else { window.eval(matches[i].match(regex_one)[1]); } } } } } } else { var new_node = document.createElement('div'); new_node.innerHTML = req.responseText; document.body.appendChild(new_node); } fn_ajax_update_vars(req.responseJS); if (func) { func(req.responseJS, req.responseText); } } } if (data instanceof Object) { data.result_ids = result_ids; } else { for (i = 0; i < result_ids.length; i++ ) { data += '&result_ids[]=' + result_ids[i]; } } if (pending_message) { if (pending_message instanceof Object) { for (k in pending_message) { document.getElementById(k).innerHTML = pending_message[k]; } } else { for (k in result_ids) { document.getElementById(result_ids[k]).innerHTML = pending_message; } } } req.open(method || 'GET', query, true); req.send(data); } // // Submit form using AJAX // function fn_ajax_form_submit(form_name, result_id, callback, pending_message) { var method, action; var form; var data = {}; form = document.forms[form_name]; elms = form.elements; data = ''; for (i = 0; i < elms.length ; i++) { if (elms[i].disabled == true) { continue; } if ( (elms[i].tagName.toLowerCase() == 'input' && ( elms[i].type == 'text' || elms[i].type == 'hidden' || elms[i].type == 'password' || (elms[i].type == 'radio' && elms[i].checked == true) || (elms[i].type == 'checkbox' && elms[i].checked == true) ) ) || ( elms[i].tagName.toLowerCase() == 'select' || elms[i].tagName.toLowerCase() == 'textarea' )){ if (elms[i].tagName.toLowerCase() == 'select' && elms[i].multiple == true) { for (k = 0; k < elms[i].options.length ; k++) { if (elms[i].options[k].selected == true) { data += escape(elms[i].name) + '=' + escape(elms[i].options[k].value) + '&'; } } } else { data += escape(elms[i].name) + '=' + escape(elms[i].value) + '&'; } } } for (i=0; i1){ return false; } var string="1234567890"; if (string.indexOf(num)!=-1){ return true; } return false; } // // Checks if the value is phone number // function fn_is_phone(phone) { return phone.match(/^\(?\d{3}\)?[ ]?[\d-]*$/gi) ? true : false; } function fn_display_trigger() { var elms = document.getElementsByTagName('SPAN'); var args = fn_display_trigger.arguments; for (i=0;i0) { for (var i=0; i= 4 && thousands_separator != '') { tmp = new Array(); for (var i=num.length-3; i>-4 ; i=i-3) { k = 3; if (i<0) { k = 3 + i; i=0; } tmp.push(num.substr(i,k)); if (i==0) { break; } } num = tmp.reverse().join(thousands_separator); } // Add decimals if (decplaces > 0) { if (decimals_separator!= '' && expr.indexOf('.') != -1) { // Fixme , use toFixed() here var decimal_full = expr.substr(expr.indexOf('.') + 1, expr.length); if (decimal_full.length > decplaces) { decimals = Math.round(decimal_full / (Math.pow(10 , (decimal_full.length - decplaces)))); } else { decimals = expr.substr(expr.indexOf('.') + 1, decplaces); } } else { decimals = '0'; } if (decimals.length < decplaces) { tmp = decimals.length; for (i=0; i= 0) { start = query.substr(0, query.indexOf('?')); var search = query.substr(query.indexOf('?')); var srch_array = search.split("&"); var temp_array = new Array(); var concat = true; var amp = ''; for (var i = 0; i < srch_array.length; i++) { temp_array = srch_array[i].split("="); concat = true; for (var j = 0; j < vars.length; j++) { if (vars[j] == temp_array[0] || temp_array[0].indexOf(vars[j]+'[') != -1) { concat = false; break; } } if (concat == true) { start += amp + temp_array[0] + '=' + temp_array[1] } amp = '&'; } } return start; } // // Open pop-up window with detailed image // function fn_open_popup_image(popup_script, image_width, image_height) { if (image_width == 0) { image_width = 400; } if (image_height == 0) { image_height = 400; } image_width += 10; image_height += 10; if ((typeof(handle_popup_image)!='undefined') && (handle_popup_image.closed == false)) { handle_popup_image.close(); } handle_popup_image = window.open(popup_script, 'popup_image', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,left=200,top=100,width=' + image_width + ',height=' + image_height + ',resizable=yes'); } function fn_align_element() { var ids = $A(arguments); var w = fn_get_window_sizes(); for (k in ids) { msg = document.getElementById(ids[k]); if (msg) { msg.style.display = 'block'; msg.style.top = w.offset_y + (w.view_height - msg.offsetHeight) / 2 + 'px'; msg.style.left = w.offset_x + (w.view_width - msg.offsetWidth) / 2 + 'px'; } } } function fn_blink(cycle) { if (cycle > 6) { be.style.visibility = 'visible'; clearTimeout(timeout); return; } cycle++; if (be = document.getElementById('blinking_elm')) { be.style.visibility = (be.style.visibility == 'visible') ? 'hidden' : 'visible'; timeout = window.setTimeout('fn_blink('+cycle+')', 500); } } function fn_format_price(value, decplaces) { if (typeof(decplaces) == 'undefined') { decplaces = 2; } value = parseFloat(value.toString()) + 0.00000000001; var tmp_value = value.toFixed(decplaces); if (tmp_value.charAt(0) == '.') { return ('0' + tmp_value); } else { return tmp_value; } } function fn_check_type(src, expected_type) { expected_type = expected_type.toLowerCase(); src_type = (''+typeof(src)).toLowerCase(); var res; switch (expected_type) { case "object": res = (src instanceof Object) || (src_type.indexOf(expected_type) != -1); break; case "array": res = (src instanceof Array) || (src_type.indexOf(expected_type) != -1); break; default: res = src_type.indexOf(expected_type) != -1; } return res; } function fn_addons_inheritance(caller) { if (fn_inheritance[caller]){ for(var i in fn_inheritance[caller]){ if(typeof(window[fn_inheritance[caller][i]]) == 'function' && typeof(window[caller]) == 'function'){ fn = eval(fn_inheritance[caller][i]); fn.apply(fn_inheritance[caller][i], eval(caller).arguments); } } } } function fn_get_function_name(body) { var body = new String (body); return body.substring(9,body.indexOf('(',9)).replace(/\s/g, ''); } // -------- Cookie functions collection ------------- function fn_get_cookie_val (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) { endstr = document.cookie.length; } return unescape(document.cookie.substring(offset, endstr)); } function fn_get_cookie(name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { return fn_get_cookie_val(j); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) { break; } } return null; } function fn_set_cookie(name, value, expires, path, domain, secure) { document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); } function fn_delete_cookie(name, path, domain) { if (fn_get_cookie(name)) { document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } } // /-------- Cookie functions collection ------------- // // -------- Web-form object ------------------- // // // This object represents maintenance routines for web-form // function cscart_form(name, use_ajax, result_id, callback, bind_keypress) { this.name = name; this.use_ajax = use_ajax || false; this.result_id = result_id || ''; this.required_fields = []; this.callback = callback || null; this.onsubmit = []; this.extra_ids = []; this.css_cache = []; this.failed_message = ''; this.pending_message = ''; // Attach submit event to all form text elements if (bind_keypress) { cscart_handlers.set_handler(this.bind_keys.bind(this)); } } cscart_form.prototype = { // Set onkeypress event for all text elements to allow send form by hitting ENTER key bind_keys: function() { var func = function(evt) { var char_code = (window.event) ? window.event.keyCode : evt.which; var elm = (window.event) ? window.event.srcElement : evt.target; if (char_code == 13) { elm._form_object.submit(); } } form = document.forms[this.name]; if (!form) { return false; } form.onsubmit = function() {return false}; form.setAttribute('autocomplete', 'off'); for (k = 0 ; k < form.elements.length ; k++) { if ((form.elements[k].tagName.toLowerCase() == 'input' && (form.elements[k].type.toLowerCase() == 'text' || form.elements[k].type.toLowerCase() == 'password'))) { form.elements[k]._form_object = this; fn_add_event(form.elements[k], 'keypress', func); } } }, // Put form field(s) to required list set_required_fields: function () { for (i=0;i \"" + value + "\"\n"; } } } else { //Stings/Chars/Numbers etc. dumped_text = arr+" ("+typeof(arr)+")"; } return dumped_text; } function fn_html_entity_decode(str) { var ta = document.createElement("TEXTAREA"); ta.innerHTML = str.replace(//g,">"); return ta.value; }