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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function openP(popupid) {
	var popup = document.getElementById(popupid);
	if(popup) {
		newy = popup.style.top;
		if(document.all) {
			newy = document.body.scrollTop;
		}
		else {
			newy = window.pageYOffset;
		}
		popup.style.top = newy + 'px';
    popup.className = 'popupshow';
	}
	if(popupid == 'popupfaq') {
		var faqselect = document.getElementById('faqselect');

		//bugfix for opera, it needs a 0ms timeout to change a dynamic select box value
		window.setTimeout('faqselect.options[0].selected = true;',0);

		faqselect.value = 0;

		getFaq(faqselect);
	}
	if ( popupid == 'popupcutoffsolo' ) {
		mustread = true;
	}
}

function closeP(popupid) {
	var popup = document.getElementById(popupid);
  if(popup) {
    popup.className = 'popuphide';
  }
}

function nextStep() {
	setStep(step+1);
}

function setStep(nextstep) {
	if(nextstep < 1 || nextstep > numsteps) {
    return;
	}
	
  stepobj =   document.getElementById('step' + step);
	stepobjtitle = document.getElementById('howtoorder_steptitle' + step);
	nextstepobj =  document.getElementById('step' + nextstep);
  nextstepobjtitle = document.getElementById('howtoorder_steptitle' + nextstep);
	if(stepobj && stepobjtitle && nextstepobj && nextstepobjtitle) {
    stepobj.className = 'step';
    stepobjtitle.src = 'images/howtoorder_step' + step + 'title.gif';
		nextstepobj.className = 'curstep';
    nextstepobjtitle.src = 'images/howtoorder_step' + nextstep + 'title_s.gif';
		if(step == numsteps) {
      MM_showHideLayers('faqtext','','hide');
		}
		if(nextstep == numsteps) {
			MM_showHideLayers('faqtext','','show');
		}
    step = nextstep;
	}
}

var step = 1;
var numsteps = 4;
var mustread = false;

function enableImage1(on) {
  var imageform1 = document.getElementById('attrib-6-0');
  if(!imageform1) return;
  imageform1.disabled = !on;
}

function enableImage2(on) {
  var imageform2 = document.getElementById('attrib-7-0');
  if(!imageform2) return;
  imageform2.disabled = !on;
}

function enableColor(on) {
  var colorform = document.getElementById('attrib-9-11');
  var bwform = document.getElementById('attrib-9-10');
    
  if(!colorform && !bwform) return;
  colorform.disabled = !on;

  if(!on) {
    bwform.checked = "checked";
  }
}

function lockColor(on) {
	var colorform = document.getElementById('attrib-9-11');
	var bwform = document.getElementById('attrib-9-10');
  if(!colorform && !bwform) return;
	bwform.disabled = on;
    bwform.checked = true;
}

function enableLines(on) {
  var lines = document.getElementById('attrib-2-4');
  var nolines = document.getElementById('attrib-2-3');
  if(!lines && !nolines) return;
  lines.disabled = !on;
  nolines.disabled = !on;
}

function disableLines(on) {
  var lines = document.getElementById('attrib-2-4');
  var nolines = document.getElementById('attrib-2-3');
  if(!lines && !nolines) return;
    lines.checked = false;
    nolines.checked = false;
if(!on){
	lines.checked = true;
}
}

function enableBkgrd(on) {
  var showbkgrd = document.getElementById('attrib-3-5');
  var nobkgrd = document.getElementById('attrib-3-6');
  if(!showbkgrd && !nobkgrd) return;
  showbkgrd.disabled = !on;
  nobkgrd.disabled = !on;
}

function enableCutoff(on) {
  var cutoff = document.getElementById('attrib-4-7');
  var notcutoff = document.getElementById('attrib-4-8');
  if(!cutoff && !notcutoff) return;
  cutoff.disabled = !on;
  notcutoff.disabled = !on;
}

function enableText(on) {
  var text = document.getElementById('attrib-5-0');
  if(!text) return;
  text.disabled = !on;
}

function extracheck(obj)
{
  return !obj.disabled;
}

function characterCount(field, maxchars) {
  if (field.value.length > maxchars) {
    field.value = field.value.substring(0, maxchars);
    alert("Error:\n\nThere is a limit of "+maxchars+" characters.");
  }
}

function testAgreed() {
  var scenic1 = document.getElementById('attrib-3-6');
  var scenic2 = document.getElementById('attrib-3-5');
  var pads2 = document.getElementById('funpads2');
  var pads4 = document.getElementById('funpads4');
  var envelopes = document.getElementById('envelope');
  
  if(scenic1 && scenic2 && !scenic1.checked && !scenic2.checked && !pads2.checked && !pads4.checked && !envelopes.checked) {
    alert('You must select a Scenic Background Option');
    return false;
  }	

  var cut1 = document.getElementById('attrib-4-7');
  var cut2 = document.getElementById('attrib-4-8');

  if(cut1 && cut2 && !cut1.checked && !cut2.checked && !pads2.checked && !pads4.checked && !envelopes.checked) {
    alert('You must select a Border Option');
    return false;
  }

  if (scenic2.checked && cut1.checked ) {
	  alert("You have selected both options \"Print entire photo\" and\n\"Please cut out and frame my image\".\nWe cannot do both to your photo.\nPlease select one choice that best describes your photo,\nor choose \"Not applicable\" in order to proceed.\nThank you.");
	  return false;
  }

  var picpadplus = document.getElementById('picpadplus');
  var image1 = document.getElementById('attrib-6-0');
  var image2 = document.getElementById('attrib-7-0');
  
  if(picpadplus && picpadplus.checked) {
    if( (image1 && image1.value !== '' && (!image2 || image2.value === '')) ||
        (image2 && image2.value !== '' && (!image1 || image1.value === '')) ) {
      alert("You must upload two images for a PicPadPlus! order.");
      return false;
    }
  }

  /*if ( ! mustread ) {
	  alert("You need to view the \"must read\" information before continuing.");
	  return false;
  }*/
  var agree1 = document.getElementById('order_agree1');
  var agree2 = document.getElementById('order_agree2');
	
  if(agree1.checked && agree2.checked) {
    return true;
  }
  else {
    alert('You must agree by clicking both checkboxes at the bottom of the form.');
    return false;
  }
  
}

function setProductsId(idnum) {
	pid = document.getElementById('products_id');
	if(pid) {
    pid.value = idnum;	
	}
}

function openPImg(imgloc) {
	var fullsize = document.getElementById('fullsize');

	if(fullsize) {
		fullsize.src = imgloc;

		openP('popupimgfullsize');
	}
}

function previewImage(fileelem,imgid) {
	BrowserDetect.init();
	if(fileelem && (
	 (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) || 
	 (BrowserDetect.browser == "Safari")
	)) {
		localSource = "file://"+fileelem.value;
		previewelem = document.getElementById(fileelem.id + "-preview");
		if(previewelem) {
			previewelem.innerHTML = '<a href="' + localSource + '" onclick="javascript:showLightbox(this);">Preview this image</a>';
		}
	}
}

var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++) {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
    {   string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      identity: "OmniWeb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      identity: "Safari"
    },
    {
      prop: window.opera,
      identity: "Opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      identity: "iCab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      identity: "Konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      identity: "Camino"
    },
    {   // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      identity: "Netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      identity: "Mozilla",
      versionSearch: "rv"
    },
    {     // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      identity: "Netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      identity: "Windows"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      identity: "Mac"
    },
    {
      string: navigator.platform,
      subString: "Linux",
      identity: "Linux"
    }
  ]

};