var remote=null; 
var remotex = 100; var remotey = 100;
function display(url, w, h, winname) {
  if (w == null){ w = "640"; }
  if (h == null){ h = "480"; }
  if (winname == null){ winname = "ReportWindow"; }
  if (navigator.appName.indexOf("Microsoft")>=0) {
	args = "width="+w+",height="+h+",resizable=yes,scrollbars=yes,menubar=no,status=0,left="+remotex+",top="+remotey;
  } else {
	args = "width="+w+",height="+h+",resizable=yes,scrollbars=yes,menubar=no,status=0,screenX="+100+",screenY="+100;
  }
  if (remote && !remote.closed) { remote.focus(); } else { remote=window.open(url,winname,args); remote.focus(); }
  if (remote && !remote.opener) remote.opener = self;
}
function closeWin() {
	if (navigator.appName.indexOf("Microsoft")>=0) {
		opener.remotex = self.screenLeft; opener.remotey = self.screenTop;
	} else {
		opener.remotex = self.screenX; opener.remotey = self.screenY;
	}
 	self.close();
} 

function toggle(id){
	if (document.getElementById(id).style.visibility != 'visible') {
		imgstr = new String(document.getElementById('fullimg').src);
		imgstr = imgstr.replace(/full./,"pop.");
		document.getElementById('popimg').src = imgstr;
		document.getElementById('selectq').style.visibility = 'hidden';
		document.getElementById(id).style.visibility='visible';
	} else {
		document.getElementById('selectq').style.visibility = 'visible';
		document.getElementById(id).style.visibility='hidden';
	}
}

function show(id){
	if (document.getElementById(id).style.display != 'block') {
		document.getElementById(id).style.display = 'block';	
	} 
}

function hide(id){
	if (document.getElementById(id).style.display != 'none') {
		document.getElementById(id).style.display = 'none';	
	} 
}

function swap(img){
	imgstr = new String(img.src);
	imgstr = imgstr.replace(/tmb./,"full.");
	document.getElementById('fullimg').src = imgstr;
}

var str = new Object;
str['email'] = "Form incomplete: Please enter your e-mail address.";
str['email_format'] = "Invalid e-mail address: Please enter a valid e-mail address."
str['firstname'] = "Form incomplete: Please enter your first name.";
str['lastname'] = "Form incomplete: Please enter your last name.";
str['password1'] = "Form incomplete: Please enter a password.";
str['password_format'] = "Invalid password: Please enter a password that is at least 5 characters long."
str['password2'] = "Form incomplete: Please confirm your password.";
str['password_mismatch'] = "Your passwords do not match. Please try again.";
str['password'] = "Form incomplete: Please enter your password.";
str['paymethod'] = "Please select a payment method.";
str['bookplates'] = "Please enter a name to appear on the bookplates (up to 2 lines).";
str['selectedsize'] = "Please enter your desired size.";
str['selectedcolor'] = "Please enter your desired color.";
str['quantity'] = "Please enter your desired quantity.";
str['validquantity'] = "Please enter a valid quantity between 1 and 99.";

str['bill_firstname'] = "Incomplete billing address: Please enter the first name.";
str['bill_lastname'] = "Incomplete billing address: Please enter the last name.";
str['bill_address'] = "Incomplete billing address: Please enter the street address.";
str['bill_city'] = "Incomplete billing address: Please enter the city.";
str['bill_state'] = "Incomplete billing address: Please enter the state.";
str['bill_zipcode'] = "Incomplete billing address: Please enter the zipcode.";
str['ship_firstname'] = "Incomplete shipping address: Please enter the first name.";
str['ship_lastname'] = "Incomplete shipping address: Please enter the last name.";
str['ship_address'] = "Incomplete shipping address: Please enter the street address.";
str['ship_city'] = "Incomplete shipping address: Please enter the city.";
str['ship_state'] = "Incomplete shipping address: Please enter the state.";
str['ship_zipcode'] = "Incomplete shipping address: Please enter the zipcode.";
str['cardtype'] = "Please select the type of credit card you are using.";
str['cardname'] = "Please enter the name on the credit card.";
str['ccfirstname'] = "Please enter the first name on the credit card.";
str['cclastname'] = "Please enter the last name on the credit card.";

str['cardnumber'] = "Please enter the credit card number.";
str['securitycode'] = "Please enter the security code from the back of the credit card.";

function validateEmail(str) {
	if (str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) { 
            return true; 
	} 
	return false; 
} 
function validateLogin(form) {
	if (form.email.value == '') {
		alert(str['email']);
		form.email.focus();
		return false;
	}
	if (validateEmail(form.email.value) == false) {
		alert(str['email_format']);
		form.email.focus();
		return false;
	}
	if (form.password.value == '') {
		alert(str['password']);
		form.password.focus();
		return false;
	}
	return true;
}
function validateGuest(form) {
	if (form.email.value == '') {
		alert(str['email']);
		form.email.focus();
		return false;
	}
	if (validateEmail(form.email.value) == false) {
		alert(str['email_format']);
		form.email.focus();
		return false;
	}
}
function validateSignup(form) {
	if (form.email.value == '') {
		alert(str['email']);
		form.email.focus();
		return false;
	}
	if (validateEmail(form.email.value) == false) {
		alert(str['email_format']);
		form.email.focus();
		return false;
	}
	if (form.fname.value == '') {
		alert(str['firstname']);
		form.fname.focus();
		return false;
	}
	if (form.lname.value == '') {
		alert(str['lastname']);
		form.lname.focus();
		return false;
	}
	if (form.password1.value == '') {
		alert(str['password1']);
		form.password1.focus();
		return false;
	}
	if (form.password1.value.length<5) {
		alert(str['password_format']);
		form.password1.focus();
		return false;
	}
	if (form.password2.value == '') {
		alert(str['password2']);
		form.password2.focus();
		return false;
	}
	if (form.password1.value != form.password2.value) {
		alert(str['password_mismatch']);
		form.password1.focus();
		return false;
	}	
	return true;
}

function validateChoices(choices) {
	for (var j=0; j < choices.length; j++){
		if (choices[j].checked) return true;
	}
	return false;
}

function validateChoicesVal(choices, val) {
	for (var j=0; j < choices.length; j++){
		if (choices[j].checked) {
			if (choices[j].value == val) return true;
		}
	}
	return false;
}

function validateChoicesNotVal(choices, val) {
	for (var j=0; j < choices.length; j++){
		if (choices[j].checked) {
			if (choices[j].value == val) return false;
		}
	}
	return true;
}

function validatePayment(form) {
	var choices = document.getElementsByName('paymentoption');
	if (validateChoices(choices) == false) {
		alert(str['paymethod']);
		form.paymentoptions.focus();
		return false;
	}
	if (validateChoicesVal(choices, 'paypal') == true){
		form.action = 'https://www.papergem.com/pages/ppexpress.php';
	}
	return true;
}

function validateAddresses(form) {
	var choices = document.getElementsByName('billid');
	if (validateChoicesNotVal(choices, '0') == false) {
		return validateBilling(form);
	}
	choices = document.getElementsByName('shipid');
	if (validateChoicesNotVal(choices, '0') == false) {
		return validateShipping(form);
	}
	return true;
}

function validateAddressesShip(form) {
	var choices = document.getElementsByName('shipid');
	if (validateChoicesNotVal(choices, '0') == false) {
		return validateShipping(form);
	}
	return true;
}

function validateBilling(form) {
	if (form.bill_firstname.value == '') {
		alert(str['bill_firstname']);
		form.bill_firstname.focus();
		return false;
	}
	if (form.bill_lastname.value == '') {
		alert(str['bill_lastname']);
		form.bill_lastname.focus();
		return false;
	}
	if (form.bill_address.value == '') {
		alert(str['bill_address']);
		form.bill_address.focus();
		return false;
	}
	if (form.bill_city.value == '') {
		alert(str['bill_city']);
		form.bill_city.focus();
		return false;
	}
	if (form.bill_state.value == '') {
		alert(str['bill_state']);
		form.bill_state.focus();
		return false;
	}
	if (form.bill_zipcode.value == '') {
		alert(str['bill_zipcode']);
		form.bill_zipcode.focus();
		return false;
	}
	return true;
}
function validateShipping(form) {
	if (form.ship_firstname.value == '') {
		alert(str['ship_firstname']);
		form.ship_firstname.focus();
		return false;
	}
	if (form.ship_lastname.value == '') {
		alert(str['ship_lastname']);
		form.ship_lastname.focus();
		return false;
	}
	if (form.ship_address.value == '') {
		alert(str['ship_address']);
		form.ship_address.focus();
		return false;
	}
	if (form.ship_city.value == '') {
		alert(str['ship_city']);
		form.ship_city.focus();
		return false;
	}
	if (form.ship_state.value == '') {
		alert(str['ship_state']);
		form.ship_state.focus();
		return false;
	}
	if (form.ship_zipcode.value == '') {
		alert(str['ship_zipcode']);
		form.ship_zipcode.focus();
		return false;
	}
	return true;
}


function validateQuantity(str) {
	if (str.match(/^\d+$/) ) {
		return true; 
	} 
	return false; 
}

function validateCustom(form, option) {
	switch (option){
		case 'bookplates': 
			if ((form.line1.value == '') && (form.line2.value == '')){
				alert(str['bookplates']);
				form.line1.focus();
				return false;
			}
			var choices = eval("document.getElementsByName('confirm')");
			if (validateChoices(choices) == false) {
				alert(str['confirm']);
				form.paymentoptions.focus();
				return false;
			}
			return true;
			break; 
		case 'luggagetags':
			if ((form.initial1.value == '') || (form.initial2.value == '') || (form.initial3.value == '')){
				alert(str['luggagetags']);
				form.initial1.focus();
				return false;
			}
			var choices = eval("document.getElementsByName('confirm')");
			if (validateChoices(choices) == false) {
				alert(str['confirm']);
				form.confirm.focus();
				return false;
			}
			return true;
			break; 
		default:
			return true;
			break;
	}
}

function validateCC(form) {
	if (!form.cardtype.selectedIndex) {
		alert(str['cardtype']);
		form.cardtype.focus();
		return false;
	}
	if (form.firstname.value == '') {
		alert(str['ccfirstname']);
		form.firstname.focus();
		return false;
	}
	if (form.lastname.value == '') {
		alert(str['cclastname']);
		form.lastname.focus();
		return false;
	}
	if (form.cardnumber.value == '') {
		alert(str['cardnumber']);
		form.cardnumber.focus();
		return false;
	}
	if (form.securitycode.value == '') {
		alert(str['securitycode']);
		form.securitycode.focus();
		return false;
	}
	/* if (!form.expmonth.selectedIndex) {
		alert(str['expmonth']);
		form.expmonth.focus();
		return false;
	}
	if (!form.expmonth.selectedIndex) {
		alert(str['expyear']);
		form.expyear.focus();
		return false;
	} */
}
var selectedsize;
var selectedcolor
function select(dimension, value, elem){
	if (dimension == 'size') {
		selectedsize = value;
		var container = document.getElementById('mysize');
		elems = getElementsByClassName('selectbox','a', container);
		for (var i = 0; i < elems.length; i++) {
			elems[i].style.borderColor = "#dddddd";
		}
		document.getElementById(dimension+value).style.borderColor = '#ff0000';
		document.getElementById('selectedsize').value = value;
		document.getElementById('displaysize').innerText = value;
	}
	if (dimension == 'color') {
		selectedcolor = value;
		var container = document.getElementById('mycolor');
		elems = getElementsByClassName('selectbox','a', container);
		for (var i = 0; i < elems.length; i++) {
			elems[i].style.borderColor = "#dddddd";
		}
		document.getElementById(dimension+value).style.borderColor = '#ff0000';
		document.getElementById('selectedcolor').value = value;
		document.getElementById('displaycolor').innerText = value;
	}
	return void(0);
}
function validateSelections(form) {
	if (form.selectedsize.value == '') {
		alert(str['selectedsize']);
		return false;
	}
	if (form.selectedcolor.value == '') {
		alert(str['selectedcolor']);
		return false;
	}
	if (form.quantity.value == '') {
		alert(str['quantity']);
		form.quantity.focus();
		return false;
	}
	val = form.quantity.value;
	if (!(val.match(/^\d*$/) && (val >0) && (val < 99))){
		alert(str['validquantity']);
		form.quantity.focus();
		return false;
	}
	return true;
}
//http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}

function ta_embedSWF(container, swf, w, h, bgcolor, id) {
	var content = '<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'">'+
	'<param name="movie" value="'+swf+'" />'+
	'<param name="base" value="." />'+
	'<param name="quality" value="high" />'+
	'<param name="wmode" value="opaque" />'+
	'<param name="bgcolor" value="'+bgcolor+'" />'+"\n"+
	'<embed name="'+id+'" src="'+swf+'" base="." quality="high" wmode="opaque" bgcolor="'+bgcolor+'" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer"></embed></object>'+"\n";
	document.getElementById(container).innerHTML = content;
}
function ta_embedSWF_adj(container, swf, w, h, bgcolor, id) {
	adjw = w-4; adjh=h-4;
	var content = '<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'">'+
	'<param name="movie" value="'+swf+'" />'+
	'<param name="base" value="." />'+
	'<param name="quality" value="high" />'+
	'<param name="wmode" value="opaque" />'+
	'<param name="bgcolor" value="'+bgcolor+'" />'+"\n"+
	'<param name="loop" value="false" />'+"\n"+
	'<embed name="'+id+'" src="'+swf+'" base="." quality="high" wmode="opaque" bgcolor="'+bgcolor+'"  loop="false" width="'+adjw+'" height="'+adjh+'" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer"></embed></object>'+"\n";
	document.getElementById(container).innerHTML = content;
}
