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

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=''; 
	}
}
