if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

Array.prototype.shuffle = function(n) {
    if (n > this.length || n == undefined) {
        n = this.length;
    }
    
    var indices = []
    while(indices.length < n) {
        var rand_index = Math.round(Math.random() * (this.length - 1));
        if (indices.indexOf(rand_index) == -1) {
            indices.push(rand_index)
        }
    }
    
    var new_array = []
    for(i = 0; i < indices.length; i++) {
        new_array.push(this[indices[i]]);
    }
    
    return new_array
}





function log(msg) {
  try {
      console.log(msg);
  } catch(e) {}
}

function is_external_url(str) {
  return false;
    var host = find_host(str);
    if (host != location.hostname) {
        return true;
    }

    return false;
}

function strip_protocol(str) {
    var matches = str.match(/^https?:\/\/(.*)/);
    if (matches) {
        return matches[1]
    }
    return str;
}

function clear_error_messages() {
    var els = $('.fieldWithError')
    els.each(function() {
      $(this).removeClass('fieldWithError');
    })
    
    var msgs = $('.fieldError').each(function() {
      $(this).remove();
    })
}

function display_form_error(el, message) {
    Element.addClassName(el, 'fieldWithError');
    msg = document.createElement('div');
    msg.className = 'fieldError';
    msg.appendChild(document.createTextNode(message));   
    el.parentNode.appendChild(msg);     
}
function ajax_submit(btn) {
    frm = Element.up(btn, 'form');
    options = {evalJSON: true, method: 'post', parameters: Form.serialize(frm), onSuccess: function(r) {
        clear_error_messages();
        Element.hide($('formMessageDisplay'));
        if (r.responseJSON.log) {
            log(r.responseText);
        }
        if (r.responseJSON.isError) {
            //el = $(r.responseJSON.data.field)
            
            for(i = 0; i < r.responseJSON.data.length; i++) {      
                el = frm[r.responseJSON.data[i].field];
                if (el.length > 0) { // assume its a radio/cb group
                    el = Element.up(el[0], 'div');
                }                
                display_form_error(el, r.responseJSON.data[i].message);
                if (i == 0) {
                    Element.scrollTo(el);
                }
            } 



        } else {
            $('formMessageDisplay').innerHTML = r.responseJSON.data.message
            Element.removeClassName($('formMessageDisplay'), 'error');            
            Element.addClassName($('formMessageDisplay'), 'success');
            Element.show($('formMessageDisplay'));
            Element.remove(btn);
        }
    }}
    r = new Ajax.Request(frm.action, options);
}

function tag(name, attrs, content) {
    el = document.createElement(name);
    for(k in attrs) {
        if (k == 'className') {
            k = 'class';
        }
        el.setAttribute(k, attrs[k]);
    }
    
    if (content) {
        t = txt(content);
        el.appendChild(t)
    }
    return el
}

function txt(str) {
    return document.createTextNode(str);
}

function FormBuilder(action, method) {
    this._form = null;
    
    this._init = function(action, method) {
        this._form = tag('form', {action: action, method: method});
    }
    
    this.add = function(name, value) {
        f = tag('input', {type: 'hidden', name: name, value: value})
        
        this._form.appendChild(f);
    }
    
    this.attach = function(cnt) {
        if (cnt == undefined) {
            cnt = $('body').get(0);
        }
        this._form.style.display = 'none';
        cnt.appendChild(this._form);
    }
    
    this.setAttribute = function(name, value) {
        this._form.setAttribute(name, value)
    }
    
    this.submit = function() {
        this._form.submit()
    }
    
    this._init(action, method)
}

function submit_data(data, url, method, target) {
    form = create_form(data, url, method, target)
    form.attach();
    form.submit();
}

function create_form(data, url, method, target) {
    if (method == undefined) {
        method = 'post'
    }
    form = new FormBuilder(url, method)
    if (target != undefined) {
        form.setAttribute('target', target);
    }
    for(k in data) {
        form.add(k, data[k])
    }    
    return form;
}

function paypal_show_cart(bid) {
  data = {}
  data['cmd'] = '_cart';
  data['business'] = bid;
  data['display'] = '1';

  submit_data(data, 'https://www.paypal.com/cgi-bin/webscr', 'post', '_self')
}

$(function() {
  $('a').each(function() {
    var mail;
    if (is_external_url($(this).attr('href'))) {
      $(this).attr('target', '_blank');
    }
  })


  $('a.paypal-add-to-cart').each(function() {
    $(this).click(function() {
      $(this).parents('form').submit()
      return false;
    })
  })
})
