var suboption = new Array();
var option = new Array();
var optionpics = new Array();
var upgrade_selects = new Array();
var last_suboption = new Array();

var newwin ='';

function newWd(page,width, height) {
    newwin = window.open(page, '', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
	newwin.focus();
}

function checkLength(_event, _this, limit)
{
	if (_event.which) {
	  key = _event.which;
	} else if (_event.keyCode) {
		key = _event.keyCode;
	}

	//alert(key);

	if (_this.value.length == limit && key != 8 && key != 46) {
		alert("There are no more characters left on this line.");
	}
}

function showLarge(prod_id)
{
	var newwin = window.open('/show_large.php?id='+prod_id, 'BigPic', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=350,height=450');
	newwin.focus();
}


function showEZShop(url)
{
	var newwin = window.open(url, 'EZShop', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=750,height=550');
	newwin.focus();
}

function showLargeOption(option_id)
{
	var newwin = window.open('/show_large_option.php?option_id='+option_id, 'BigPic', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=350,height=450');
	newwin.focus();
}

function showSizeChart()
{
	var newwin = window.open('/sizechart/', 'SizeChart', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=540,height=380');
	newwin.focus();
}

function showOption()
{
	if (showOption.arguments.length == 0)
		var filename = document.optionpic.src;
	else
		var filename = document['optionpic'+showOption.arguments[0]].src;

	var newwin = window.open('/show_large_option.php?filename='+filename, 'BigPic', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=350,height=450');
	newwin.focus();
}

function setPackageImage(img_name,to_src)
{
	if (document.images) {
		document[img_name].src = to_src;
	}
}

function getSelectedPackageChoice(fld)
{
	var x;

	if (!fld.length)
		return fld.value;

	for (x=0;x<fld.length;x++) {
		if (fld[x].checked) {
			return fld[x].value;
		}
	}

	return fld[0].value;
}

function showOptionsForSelectedPackageChoice(cat_id,prod_id,old_prod_id)
{
	var div;
	var divold;

	if (document.all) {
		div = document.all['option_'+cat_id+'_'+prod_id];
		divold = document.all['option_'+cat_id+'_'+old_prod_id];
	}
	else if (document.getElementById) {
		div = document.getElementById('option_'+cat_id+'_'+prod_id);
		divold = document.getElementById('option_'+cat_id+'_'+old_prod_id);
	}

	divold.style.visibility = 'hidden';
	div.style.visibility = 'visible';
}

function hasImage(id)
{
	for (var x=0;x<optionpics.length;x++) {
		if (optionpics[x] == id)
			return true;
	}

	return false;
}

function isUpgrade(fld_name)
{
	for (var x=0;x<upgrade_selects.length;x++) {
		if (upgrade_selects[x] == fld_name)
			return true;
	}

	return false;
}

function showSuboptions(id,pulldown_id,prod_index,prod_id)
{
	if (hasImage(id) && document.images) {
		var picname = 'optionpic'+(prod_index > -1 ? prod_index : '');
		var pic = document.images[picname];
		if (pic)
			pic.src = '/optionpics/'+id+'_thumb.jpg';
	}

	if (last_suboption[pulldown_id]) {
		setSubOptionVisibility(last_suboption[pulldown_id],'hidden',prod_id);
	}

	if (!suboption[id])
		return false;

	setSubOptionVisibility(suboption[id],'visible',prod_id);

	last_suboption[pulldown_id] = suboption[id];
}

function setSubOptionVisibility(suboption,visibility,prod_id)
{
	var field_name;
	var field_options;
	var field_options_select;

	if (document.all) {
		field_name = document.all['suboption_name_' + suboption];
		field_options = document.all['suboption_options_' + suboption];
		field_options_select = document.all['suboption_options_select_' + suboption];
	}
	else if (document.getElementById) {
		field_name = document.getElementById('suboption_name_' + suboption);
		field_options = document.getElementById('suboption_options_' + suboption);
		field_options_select = document.getElementById('suboption_options_select_' + suboption);
	}

	field_name.style.visibility = visibility;
	field_options.style.visibility = visibility;
	field_options_select.style.visibility = visibility;

	if (prod_id > 0) {
		suboption = prod_id + '][' + suboption;
	}

	if (visibility == 'hidden') {
		document.theFrm['options['+suboption+']'].selectedIndex = 0;
		document.theFrm['options['+suboption+']'].disabled = true;
	}
	else {
		document.theFrm['options['+suboption+']'].disabled = false;
	}
}

function populateShipping(fld)
{
	var thefrm = fld.form;
	var field = fld.name.substring('billing['.length,fld.name.length-1);

	if (fld.type == 'select') {
		if (thefrm['shipping['+field+']'].selectedIndex == 0) {
			thefrm['shipping['+field+']'].selectedIndex = fld.selectedIndex;
		}
	}
	else if (thefrm['shipping['+field+']'].value == '') {
		thefrm['shipping['+field+']'].value = fld.value;
	}

	// populate credit card name when done with last name

	if (field == 'last_name' && thefrm['billing[first_name]'].value != '' && thefrm['billing[last_name]'].value != '' && thefrm['payment[name_on_card]'].value == '') {
		thefrm['payment[name_on_card]'].value = thefrm['billing[first_name]'].value + ' ' + thefrm['billing[last_name]'].value;
	}
}

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_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_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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// form must be called the_form ... otherwise you need to pass 'this' as well.
function clear_text(name)
{
	document.the_form[name].value = "";
	document.the_form['text_' + name].value = "";
}

function validateProductForm(validate_recipient_name)
{
	quantity_valid = document.the_form['_new_qty'].value > 0;
	name_valid = document.the_form['_new_name'].value != '';
	//date_valid = document.the_form['_new_future_date'].value != "";
	//future_date_checked = document.the_form['_new_shipping'][document.the_form['_new_shipping'].length - 1].checked;

	if (!quantity_valid) {
		alert("Please enter a valid quantity.");
		return false;
	}

	//alert(validate_recipient_name);

	if (!name_valid && validate_recipient_name) {
		alert("Please enter a recipient name.");
		return false;
	}

	/*
	if (future_date_checked && !date_valid) {
		alert("Please select a date or a shipping method.");
		return false;
	}
	*/

	return true;

}

function validateCartForm()
{
	//alert(recip_fields);

	for (x in recip_fields) {
	  if (document.the_form[recip_fields[x]].value.length < 1) {
	  	//alert(document.the_form[recip_fields[x]].value);
	    alert("Please fill in names for all recipients.");
	    return false;
	  }
	}

	return true;

}

function prefill(index_dest, index_src)
{
	//alert(index);



	if (index_src) {

	 // alert(0);

		var indices = new Array();
		indices['[first_name]'] = '[first_name]';
		indices['[last_name]'] = '[last_name]';
		indices['[addr_1]'] = '[addr_1]';
		indices['[addr_2]'] = '[addr_2]';
		indices['[company]'] = '[company]';
		indices['[phone]'] = '[phone]';
		indices['[city]'] = '[city]';
		indices['[state]'] = '[state]';
		indices['[zip]'] = '[zip';
		indices['[message][0]'] = '[message][0]';
		indices['[message][1]'] = '[message][1]';
		indices['[message][2]'] = '[message][2]';
		indices['[message][3]'] = '[message][3]';
	  for (var index in indices) {
	  	//alert(index);
	  	//alert(index_src);
		  document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + index) ].value = document.forms.theFrm[ ('recipient_addresses' + '[' + index_src + ']' + index) ].value;
	  }
	} else {

	 // alert(1);

		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'first_name' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'first_name' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'last_name' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'last_name' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'addr_1' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'address1' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'addr_2' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'address2' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'company' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'company' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'phone' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'phone' + ']')].value;
		document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'city' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'city' + ']')].value;
    document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'state' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'state' + ']')].value;
    document.forms.theFrm[ ('recipient_addresses' + '[' + index_dest + ']' + '[' + 'zip' + ']') ].value = document.forms.theFrm[ ('billing' + '[' + 'zip' + ']')].value;

	}
}

function textCounter(field, countfield, maxlimit)
{

  //alert(field.value);

	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else {
		countfield.value = maxlimit - field.value.length;
	}
}

function toggleCompany(isOn, field)
{
	//alert(isOn);
	//alert(field);
  document.forms.theFrm[field].disabled = !isOn;
  if (!isOn)
  	document.forms.theFrm[field].value = "";
}

function selectShipping(index)
{

	/*
	index = '';

	alert(document.getElementById('_shipping').checked);

	if (document.getElementById('_shipping').checked)
	  setting = 'shipping';
	else
	  setting = '';

	if (setting == 'shipping') {
		var x = 0;
    while (info = document.getElementById(index + "future_controls_" + x)) {
      info.disabled = true;
      info.value = '';
      x++;
    }
		var y = 0;
    while (info = document.getElementById(index + "shipping_controls_" + y)) {
      info.disabled = false;
      y++;
    }
    document.getElementById("_calendar").src = '/images/calendar_off.gif';
	}
	else {
		var x = 0;
    while (info = document.getElementById(index + "shipping_controls_" + x)) {
      info.disabled = true;
      info.value = '';
      x++;
    }
		var y = 0;
    while (info = document.getElementById(index + "future_controls_" + y)) {
      info.disabled = false;
      y++;
    }
    document.getElementById("_calendar").src = '/images/calendar.gif';
	}
	*/
}

var displayType = (document.all) ? 'block' : 'table-row-group';

function _getElement(id)
{
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id);
	}
	else if (document.all && document.all[id]) {
		return document.all[id];
	}
	else {
		return false;
	}
}

function toggleDisplay(item)
{
	if (_getElement(item).style.display == displayType) {
		setDisplay(item, 'none');
	} else {
		setDisplay(item,displayType);
	}
}

function setDisplay(id,dis)
{
	if(_getElement(id)) {
	_getElement(id).style.display = dis;
	}
}

function clearSearch(search)
{
	if(search.value == 'Search')
	{
		search.value='';
	}
}

function submitPager()
{
	document.pagerFrm.submit();
}

function checkCustomFrm()
{
	if(document.customeLabelFrm.qty.value < 2){
		alert("You must purchase a minimum Quantity of 2 for any custom label");
		return false;
	}else
		return true;
}

function hideClass(className)
{
	var items = document.getElementsByClassName(
		className
	);

	items.each(function (item) {
		item.hide();
	});
}

function showClass(className)
{

	var items = document.getElementsByClassName(
		className
	);

	items.each(function (item) {
		item.show();
	});
}

function showOptionsFrm(frmName)
{
	hideClass('options');

	setDisplay(frmName,'block');

}

function setQty(qty)
{
	document.labelFrm.quantity.value = qty;
	$('labelQty').className = 'qtyChanged';
}

function initSliders(min_val, max_val)
{
	if(min_val == null)
		min_val = 0;
	if(max_val == null)
		max_val = 20000;

	var handles = [$('handle1'), $('handle2')];
	var track = $('amountSlider');
	var init_vals = [min_val, max_val]; // First handle at 0, 2nd at 100
	var allowed_vals = new Array(0,50,100,150,200,250,300,350,400,450,500,600,700,800,900,1000,1250,1500,1750,2000,2250,2500,2000,3000,3500,4000,4500,5000,6000,7000,8000,9000,10000,12500,15000,17500,20000)

	var slider = new Control.Slider(handles, track, {
		range:$R(0, 20000),
		increment: 1,
		sliderValue: init_vals,
		values:	allowed_vals,
		spans: ['amountSliderSpan'],
		restricted:true,
		onSlide: function(value) {
		    $('hid_min_amount').value = Math.round(value[0]);
		    $('hid_max_amount').value = Math.round(value[1]);
		    $('min_val').innerHTML = addCommas(Math.round(value[0]));
			$('max_val').innerHTML = addCommas(Math.round(value[1]));
		},
		onChange: function(value) {
			$('hid_min_amount').value = Math.round(value[0]);
			$('hid_max_amount').value = Math.round(value[1]);
			$('min_val').innerHTML = addCommas(Math.round(value[0]));
			$('max_val').innerHTML = addCommas(Math.round(value[1]));
		}
	});
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function showBig(image_id,replace_suffix,new_suffix,filename)
{
	var thumb = $(image_id);
	var big_image = $('big_image');

	var new_src = thumb.src;
	new_src = new_src.replace(replace_suffix,new_suffix);

	big_image.src = new_src;
	$('popLink').href = "javascript:popLargeWindow('"+filename+"')";
}

function nextPicture(){
	try{
		show_id = end_id + 1;
		var show_pic = document.getElementById('image_div_'+show_id);
		show_pic.style.display = "inline";
		var hide_pic = document.getElementById('image_div_'+start_id);
		hide_pic.style.display = "none";
	}catch(err){
		return;
	}
	start_id++;
	end_id++;
	highliteArrows();
}

function prevPicture(){
	try{
		show_id = start_id - 1;
		var show_pic = document.getElementById('image_div_'+show_id);
		show_pic.style.display = "inline";
		var hide_pic = document.getElementById('image_div_'+end_id);
		hide_pic.style.display = "none";
	}catch(err){
		return;
	}
	start_id--;
	end_id--;
	highliteArrows();
}

function highliteArrows(){
	next_id = end_id + 1;
	prev_id = start_id - 1;

	if(document.getElementById('image_div_'+ next_id))
		var prev = document.getElementById('next_arrow').className = 'nextAct';
	else
		var prev = document.getElementById('next_arrow').className = 'next';

	if(document.getElementById('image_div_'+ prev_id))
		var prev = document.getElementById('prev_arrow').className = 'prevAct';
	else
		var prev = document.getElementById('prev_arrow').className = 'prev';
}

function popLargeWindow(prod_id)
{
	var newwin = window.open('/show_large.php?id='+prod_id, 'BigPic', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=575,height=550');
	newwin.focus();
}

function toggleFaqDiv(divid){

    if($(divid).style.display == 'none')
        Effect.SlideDown(divid);
    else
        Effect.SlideUp(divid);
	// $(divid).toggle();
    return false;
}