function sm_pop (page, arg1, arg2) {
   var sp = window.open('/en/popups/default_pop.sys?page=' + escape(page) + '&arg1=' + escape(arg1) + '&arg2=' + escape(arg2),  
   'preview_pop',
   'width=330,height=330,left=300,top=200,directories=no,status=no,scrollbars=no,resizable=no,menubar=no');
    sp.focus();
}

function med_pop (page, arg1, arg2) {
   var mp = window.open('/en/popups/default_pop.sys?page=' + page + '&arg1=' + escape(arg1) + '&arg2=' + escape(arg2), 
   'preview_pop',
   'width=500,height=350,left=200,top=100,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
    mp.focus();
}

function lg_pop (page, arg1, arg2) {
   var lp = window.open('/en/popups/default_pop.sys?page=' + page + '&arg1=' + escape(arg1) + '&arg2=' + escape(arg2), 
   'preview_pop',
   'width=650,height=500,left=200,top=100,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
    lp.focus();
}

function greatfor() {var gf = window.open ("/en/popups/great_for.mc",'pop','width=700,height=300,left=0,top=0,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no');
   gf.focus();}

function workswith() {var ww = window.open ("/en/popups/works_with.mc",'pop','width=700,height=280,left=0,top=0,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no');
   ww.focus();}

function addto (m,r) {
   var cp = window.open('/en/popups/addtocart.mc?mid=' + m + '&rld=' + r, 
   'cart_pop',
   'width=350,height=250,left=200,top=100,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
    cp.focus();
}

_sp_loc = null;
_sp_s   = 1;
_sp_p   = 1;
_sp_t   = 0;

function _sls () {window.status = 'View Image Specs'; return true}
function _dls () {window.status = 'View Details'; return true}
function _pls () {window.status = 'Processing...'; return true}
function _cls () {window.status = ''; return true}

function setRows (rows) {
    var r = parseInt(rows);
    if (r < 2 || isNaN(r)) r = 2;

    var ret = setParam('pref_rows',r);
    setParam('t',_sp_t);
    setParam('s',_sp_s);

    ret = stripParam('ppage');
    if (ret != null) _sp_p = ret;

    stripParam('opage');

    setLoc();
}

function setParam (name,val) {
    if (_sp_loc == null) _sp_loc = window.location.href;

    var ns1 = '?' + name + '=';
    var ns2 = '&' + name + '=';

    var old = null;

    if (_sp_loc.indexOf('?') > -1) {
       var ndx = _sp_loc.indexOf(ns1);
       if (ndx == -1) ndx = _sp_loc.indexOf(ns2);

       if (ndx > -1) {
          var b = _sp_loc.indexOf('=', ndx+1);
          var e = _sp_loc.indexOf('&',b) > -1 ? _sp_loc.indexOf('&',b) : _sp_loc.length - 1;

          old = _sp_loc.substring(b+1,e);
          _sp_loc = _sp_loc.substring(0,b+1) + escape(val) + _sp_loc.substring(e);

       } else {
          var b = _sp_loc.indexOf('?');
          _sp_loc = _sp_loc.substring(0,b+1) + name + '=' + escape(val) + '&' + _sp_loc.substring(b+1);
       }

    } else {
       _sp_loc = _sp_loc + '?' + escape(name) + '=' + val;
    }

    return old;
}

function stripParam (name) {
    if (_sp_loc == null) _sp_loc = window.location.href;

    var ns1 = '?' + name + '=';
    var ns2 = '&' + name + '=';

    var old = null;

    if (_sp_loc.indexOf('?') > -1) {
       var ndx = _sp_loc.indexOf(ns1);
       if (ndx == -1) ndx = _sp_loc.indexOf(ns2);

       if (ndx > -1) {
          var e = _sp_loc.indexOf('&',ndx+1) > -1 ? _sp_loc.indexOf('&',ndx+1) + 1 : _sp_loc.length - 1;
          old = _sp_loc.substring(ndx + ns1.length, e-1);
          _sp_loc = _sp_loc.substring(0,ndx+1) + _sp_loc.substring(e);
       }
    }

    return old;
}

function setLoc () {
    if (_sp_loc != null) window.location.href = _sp_loc;
}

function cacheForm (fn,cn) {
	if (!document.forms[fn]) return;
	var f = document.forms[fn];
	var fcache = new Array();
	if (cn == null) cn = fn;

	for (var i=0; i<f.elements.length; i++) {
		var p = "";
		var n = "";
		var et = new String(f.elements[i].type);

		if (f.elements[i].length && (et.indexOf("select-") != 0)) {
			for (var j=0; j<f.elements[i].length; j++) {
				p = handleElement(f.elements[i][j]);
				n = f.elements[i][j].name;
				addValue(fcache,n,p);
			}
		} else {
			p = handleElement(f.elements[i]);
			n = f.elements[i].name;
			addValue(fcache,n,p);
		}
	}

	var len = 0;
	for (var k in fcache) len++;

	if (len > 0) {
		var val = "";
		for (var n in fcache) {
			//var v = "";
			for (var j=0; j<fcache[n].vals.length; j++) {
				//if (v.length) v += ",";
				//v += fcache[n].vals[j];
				if (val.length) val += "&";
				val += escape(n) + "&" + fcache[n].vals[j];
			}
			//if (v.length) {
			//	if (val.length) val += "&";
			//	val += escape(n) + "&" + v;
			//}
		}
		//var now = new Date();
		//var expiry = new Date(now.getFullYear() + 1, now.getMonth(), now.getDate());
		var c = cn + "=" + val + "; path=/"; //; expires=" + expiry.toGMTString();
		document.cookie = c;
	}

	return true;
}

function handleElement (ele) {
	var v = "";
	if (!ele.name || (ele.name == null) || (ele.name == "")) return "";
	var n = new String(ele.name);
	var t = new String(ele.type);

	switch (t.toLowerCase()) {
		case "text":
			var tv = ele.value;
			if ((tv == null) || (tv == "")) break;
			tv = tv.replace(/^\s*|\s*$/g, "");
			if (tv.length == 0) break;
			ele.value = tv;
			v = escape(tv);
			break;
		case "radio":
		case "checkbox":
			if (!ele.checked) break;
			v = escape(ele.value);
			break;
		case "select-one":
			v = escape(optValue(ele.options[ele.selectedIndex],!optsHaveVals(ele)));
			break;
		case "select-multiple":
			var fo = !optsHaveVals(ele);
			var av = new Array();
			for (var i=0; i<ele.options.length; i++) {
				var opt = ele.options[i]; 
				if (!opt.selected) continue;
				var ov = optValue(opt,fo);
				if (ov == "") continue;
				//if (v.length) v += ",";
				//v += escape(ov);
				av[av.length] = escape(ov);
			}
			return av;
			break;
		default:
			v = escape(ele.value);
	}
	if ((v == null) || (v == "")) return "";
	return v;
}

function optsHaveVals (sel) {
	if (!sel || !sel.options) return false;
	for (var i=0; i<sel.options.length; i++) {
		if ((sel.options[i].value != null) && (sel.options[i].value != "")) return true;
	}
	return false;
}

function optValue (opt, failover) {
	var v = "";
	if (failover && ((opt.value == null) || (opt.value == ""))) {
		v = opt.text;
	} else if (opt.value != null) {
		v = opt.value;
	}
	return v;
}

function addValue (ar,n,v) {
	if (v.length && n.length) {
		if (ar[n] == null) {
			ar[n] = new Array();
			ar[n].name = n;
			ar[n].vals = new Array();
		}
		if ((typeof(v) == "array") || (typeof(v) == "object")) {
			for (var i=0; i<v.length; i++) {
				ar[n].vals[ar[n].vals.length] = v[i];
			}
		} else {
			ar[n].vals[ar[n].vals.length] = v;
		}
	}
}

formCacheLoaded = true;
