
/* make sure to use: 
    jsmin <shared.js.txt >shared.js "(c)2007 Knob4Less"
*/


/* ------------------ DREAMWEAVER FUNCTIONS ------------------------------ */

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length;
i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}
}

/* ----------------------------------------------------------------------- */

function set_selected_sort() {
    var m = window.location.href.match(/[?&]sort=(-?(?:sales|rank_name|price))/);
    if (!m) { m = ['','sales_rank']; } 
    var f = document.forms.sortBy;
    if (!(f && f.sort)) return;
    for (var i = 0; i < f.sort.options.length; i++) {
        if (f.sort.options[i].value == m[1]) {
            f.sort.selectedIndex = i;
            return;
        }
    }
}

function add_load_event(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() { oldonload(); func(); }
    }
}

add_load_event(set_selected_sort);

function showmore(id) {
    var e;
    if (e = document.getElementById('hidden-' + id))
        e.style.display = 'block';
    if (e = document.getElementById('show-' + id))
        e.style.display = 'none';
}

function disable_form(form) {
    for (i = 0; i < form.length; i++) {
        e = form.elements[i];
        if (e.type.toLowerCase() == "submit") {
            e.disabled = true;
        }
    }
}

function tell_a_friend(id) {
    url = '/pages/tell-a-friend.html?id=' + id;
    window.open(url,'tellafriend','scrollbars=no,resizable=yes,width=500,height=550');
}

var ZoomWin;

function view_larger(im) {
   url =/* im.substring(0,4) == 'http' ? im 
       : */ '/pages/view-larger.html?im=' + escape(im);
    ZoomWin=window.open(url,'viewlarger','scrollbars=yes,resizable=yes,width=500,height=500');
    ZoomWin.focus();
}

error_msg = "You must select an option!";
error_nothing = "You must enter a quantity to continue!";

saved_price = new Array(); 
saved_price[0] = saved_price[1] = 0;

_update_price = function(price) {                                                   ids = new Array('text_full', 'text_sale');
    for (i = 0; i < ids.length; i++) {
        if (e = document.getElementById(ids[i])) {
            replace_price(e, price, i);
        }
    }
}

function update_price(opt) {
    re_opt = 
      new RegExp('\\(\\s*\\+\\s*\\$?\\s*(\\d+(?:\\.\\d+)?|\\.\\d+)\\s*\\)');
    price = 0;
    for (i = 0; i < opt.form.elements.length; i++) {
        e = opt.form.elements[i];
        if (e.name.substring(0, 6) == 'vwattr') {
            if (r_opt = re_opt.exec(e[e.selectedIndex].text)) {
                price += r_opt ? r_opt[1] * 1 : 0;
            }
        }
    }
    _update_price(price);
}

function replace_price(elem, price, p_idx) {
    re = new RegExp('(\\d+(?:\\.\\d+)?|\\.\\d+)');
    if (saved_price[p_idx] == 0) {
       if (r = re.exec(elem.data)) 
           saved_price[p_idx] = r[0] * 1;
    }
    price += saved_price[p_idx];
    elem.data = '$' + price.toFixed(2);
}

function select_changed(select) {
    r = select.name.match(/^vwattr(\d+)_/);
    if (r != null) {
        q = select.form.elements['vwquantity' + r[1]];
        if (q && (n = parseInt(q.value)) != NaN && n == 0) {
            q.value = 1;
        }
    }
}

function check_options(form, idx) {
    re = new RegExp('^vwattr' + idx);
    for (var i = 0; i < form.elements.length; i++) {
        e = form.elements[i];
        if (re.test(e.name) && e.tagName == 'SELECT') {
            if (e.selectedIndex == 0) {
                return false;
            }
        }
    }
    return true;
}

function scroll_to_element(e) {
    var x = 0; var y = 0;
    while (e != null) {
        x += e.offsetLeft;
        y += e.offsetTop;
        e = e.offsetParent;
    }
    if (x > 0 && y > 0) {
        window.scrollTo(x,y);
    }
}

function form_submit_alt(form) {
    if (!check_options(form, 1)) {
        scroll_to_element(document.getElementById('configs'));
        alert(error_msg);
        return false;
    }
    var valid_qty = 0;
    var re = /^qty\[\d+\]$/;
    for (i = 0; i < form.elements.length; i++) {
        e = form.elements[i];
        if (re.test(e.name) && (n = parseInt(e.value)) != NaN && n > 0) {
            valid_qty++;
        }
    }
    if (valid_qty == 0) {
        alert(error_nothing);
        return false;
    }
    return true;
}

function form_submit(form) {
    ok = false;
    for (var i = 0; i < form.elements.length; i++) {
        r = form.elements[i].name.match(/^vwitem(\d+)/);
        if (r != null) {
            q = form.elements['vwquantity' + r[1]];
            if (q && (n = parseInt(q.value)) != NaN && n > 0) {
                if (!check_options(form, r[1])) {
                    alert(error_msg);
                    return false;
                } else {
                    ok = true;
                }
            }
        }
    } 
    if (!ok) alert(error_nothing);
    return ok;
}

function update_image(opt) {
	remove_a = false;
	text = opt.options[opt.selectedIndex].text;
	pos1 = text.indexOf('-');
	if (pos1 != -1) {
		pos2 = text.indexOf(' ', pos1 + 1);
		code = text.substring(
            pos1 + 1, pos2 == -1 ? text.length : pos2)
            .replace(')','')
            .toLowerCase();
        name = opt.name.replace(/^vwattr\d+_/, '')
            .replace(/[^A-Za-z0-9\-]+/g, '-')
            .toLowerCase();
        if (!(img = document.getElementById('opt-' + name))) return;
        if (image_map[code]) {
			img.src = PATH + image_map[code];
		} else {
			img.src = PATH + 'transpix.gif';
			remove_a = true;
		}
		if ((p = img.parentNode).tagName.toUpperCase() == 'A') {
			if (remove_a) p.parentNode.replaceChild(img, p);
		} else {
			a = document.createElement('a');
			a.setAttribute('href', '#');
            if (img_finish != '') {
                a.onclick = function () {
                    view_larger(img_finish.match(/^http/) ?
                       img_finish : PATH+img_finish);
                    return false;
                }
            }
			a.appendChild(p.removeChild(img));
			p.appendChild(a);
		}
	}
}

