// Fix the .toFixed bug for rounding
function Stretch(Q,L,c){var S=Q;if(c.length>0)while(S.length<L){S=c+S}return S}
function StrU(X,M,N){var T,S=new String(Math.round(X*Number("1e"+N)));if(S.search && S.search(/\D/)!=-1){return ''+X};with(new String(Stretch(S,M+N,'0')))return substring(0,T=(length-N))+'.'+substring(T);}
function Sign(X){return X<0 ? '-':'';}
function StrS(X,M,N){return Sign(X)+StrU(Math.abs(X),M,N)}
Number.prototype.toFixed=new Function('n','return StrS(this,1,n)')


var pageisloaded = false;
var openbox      = null;
var timer        = null;

function initwindow(){checkJS();externalLinks();pageisloaded=true;}
function resizewindow(){checkZoom();}
function checkZoom() {
	if(document.getElementById('zoom')){
		document.getElementById('zoom').style.top=(getTop(document.getElementById('row2'))-416)+'px';
		document.getElementById('zoom').style.left=(getLeft(document.getElementById('row2'))-403)+'px';
		document.getElementById('zoom_shadow').style.top=(getTop(document.getElementById('row2'))-410)+'px';
		document.getElementById('zoom_shadow').style.left=(getLeft(document.getElementById('row2'))-397)+'px';
	}
}
// Change the hidden field's value (will only work if js is enabled)
function checkJS(){
	if (document.form1){
		if (document.form1.jsenabled){
			document.form1.jsenabled.value = 'yes';
		}
	}
	if(document.getElementById('priceyoupay')){
		yourprice(1);
	}
}
function tlen(t,l){if(t.value.length>l){t.value=t.value.substring(0,l);}}


function rotate(len,dir){
	test = 0;
	for(i=1;i<=len;i++){
		if(document.getElementById('img_catalog'+i).style.display == 'block'){
			document.getElementById('img_catalog'+i).style.display = 'none';
			document.getElementById('img_large'+i).style.display = 'none';
			if(i==len && dir == 1){
				document.getElementById('img_catalog1').style.display='block';
				document.getElementById('img_large1').style.display='block';
				document.getElementById('pd_imagecaption').lastChild.nodeValue='';
				document.getElementById('pd_imagecaption').appendChild(document.createTextNode(document.getElementById('img_caption1').value));
				document.getElementById('zoom_caption').lastChild.nodeValue='';
				document.getElementById('zoom_caption').appendChild(document.createTextNode(document.getElementById('img_caption1').value));
			}
			else if(i==1 && dir == -1){
				document.getElementById('img_catalog'+len).style.display='block';
				document.getElementById('img_large'+len).style.display='block';
				document.getElementById('pd_imagecaption').lastChild.nodeValue='';
				document.getElementById('pd_imagecaption').appendChild(document.createTextNode(document.getElementById('img_caption'+(len)).value));
				document.getElementById('zoom_caption').lastChild.nodeValue='';
				document.getElementById('zoom_caption').appendChild(document.createTextNode(document.getElementById('img_caption'+(len)).value));
			}
			else{
				document.getElementById('img_catalog'+(i+dir)).style.display='block';
				document.getElementById('img_large'+(i+dir)).style.display='block';
				document.getElementById('pd_imagecaption').lastChild.nodeValue='';
				document.getElementById('pd_imagecaption').appendChild(document.createTextNode(document.getElementById('img_caption'+(i+dir)).value));
				document.getElementById('zoom_caption').lastChild.nodeValue='';
				document.getElementById('zoom_caption').appendChild(document.createTextNode(document.getElementById('img_caption'+(i+dir)).value));
			}
			test=1;
			break;
		}
	}
	if(test==0){
		for(i=1;i<=len;i++){
			document.getElementById('img_catalog'+i).style.display = 'none';
			document.getElementById('img_large'+i).style.display = 'none';
		}
		if(dir == 1){
			document.getElementById('img_catalog'+2).style.display = 'block';
			document.getElementById('img_large'+2).style.display = 'block';
			document.getElementById('pd_imagecaption').lastChild.nodeValue='';
			document.getElementById('pd_imagecaption').appendChild(document.createTextNode(document.getElementById('img_caption'+2).value));
			document.getElementById('zoom_caption').lastChild.nodeValue='';
			document.getElementById('zoom_caption').appendChild(document.createTextNode(document.getElementById('img_caption'+2).value));
		}else{
			document.getElementById('img_catalog'+len).style.display = 'block';
			document.getElementById('img_large'+len).style.display = 'block';
			document.getElementById('pd_imagecaption').lastChild.nodeValue='';
			document.getElementById('pd_imagecaption').appendChild(document.createTextNode(document.getElementById('img_caption'+len).value));
			document.getElementById('zoom_caption').lastChild.nodeValue='';
			document.getElementById('zoom_caption').appendChild(document.createTextNode(document.getElementById('img_caption'+len).value));
		}
	}
}
function getTop(el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}
function getLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function roll(obj,sta,lay){
  iSrc=document[obj];
  if(document.layers && lay){iSrc=document.layers[lay].document[obj];}
  if(sta.length>1){iSrc.src="img"+sta;return;}
  iSrc.src=iSrc.src.substring(0,iSrc.src.lastIndexOf('_')+1)+sta+".gif";
}
function showme(alink){
	if(pageisloaded){
		if (timer != null){clearTimeout(timer);}
		if(alink.substring(0,4) == 'link'){
			linkbox = "cat" + alink.substring(4);
		}else{
			linkbox = 'con' + alink.substring(4);
		}
		hidelast(linkbox);
		document.getElementById(linkbox).style.top=getAnchorPosition(alink).y + 20 + "px";
		document.getElementById(linkbox).style.left=getAnchorPosition(alink).x - 5 + "px";
		openbox = linkbox;
	}else{
		return false;
	}
}
function hidelast(linkbox){
	if(openbox != null & openbox != linkbox){
		document.getElementById(openbox).style.left= -500 + "px";
		document.getElementById(openbox).style.top=-1000 + "px";
	}
}
function hideme(linkbox){
	document.getElementById(linkbox).style.left= -500 + "px";
	document.getElementById(linkbox).style.top=-1000 + "px";
}
function hidemeslowly(alink){
	if(pageisloaded){
		if(alink.substring(0,4) == 'link'){
			linkbox = "cat" + alink.substring(4);
		}else{
			linkbox = 'con' + alink.substring(4);
		}
		timer = setTimeout('hideme(\''+linkbox+'\')',300);
	}
	else{
		return false;
	}
}

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

	
var duplicatesubmit = false;

function trim(string){return(string.replace(/^\s*|\s*$/g,""));}

function CheckEmail(obj){
	var pos,atpos,lastatpos,lastperiodpos;
	var emailAddr = obj.value;
	var temp = trim(emailAddr);
	if(temp=='')return true;
	atpos=emailAddr.indexOf("@");	
  lastatpos=emailAddr.lastIndexOf("@");
	if(atpos==-1){alert("E-mail address must contain @");return false;}
	else if(atpos==0){alert("@ cannot be in first position of e-mail address");return false;}
	else if(atpos==emailAddr.length-1){alert("@ cannot be in last position of e-mail address");return false;}
	if(atpos!=lastatpos){alert("E-mail address cannot contain more than one @");return false;}
	pos=emailAddr.indexOf(".");
	lastperiodpos=emailAddr.lastIndexOf(".");
	if(atpos>lastperiodpos){alert("E-mail address must contain a period somewhere after the @");return false;}
	else if(pos==0){alert("Period cannot be in first position of e-mail address");return false;}
	else if(lastperiodpos==emailAddr.length-1){alert("Period cannot be in last position of e-mail address");return false;}
	else pos = emailAddr.indexOf("..");
	if(pos>=0){alert("E-mail address cannot contain two periods in a row");return false;}
	else pos = emailAddr.indexOf("@.");
	if(pos>=0){alert("Period cannot immediately follow @ in e-mail address");return false;}
	else pos = emailAddr.indexOf(".@");
	if(pos>=0){alert("@ cannot immediately follow period in e-mail address");return false;}
	else pos = temp.indexOf(" ");
	if(pos>=0){alert("E-mail address cannot contain spaces");return false;}
	return true;
}
function checkPostalCode(label,string){
	postalcodeok=false;
	if(string.search(/^([A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]$)/) != -1){postalcodeok=true;}
	if(string.search(/^([A-Za-z][0-9][A-Za-z]-[0-9][A-Za-z][0-9]$)/) != -1){postalcodeok=true;}	
	if(string.search(/^([A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9]$)/) != -1){postalcodeok = true;}	
	if(!postalcodeok){alert("For "+label+", please enter a valid Canadian Postal Code of the form M5M5M5, or M5M-5M5 or M5M 5M5. Be careful not to mix up the numbers 0 (zero) and 1 (one) with the letters O (oh) and l (el).");return false;}
	return true;
}
function checkZIP(label,string){
	if(string.search(/^(\d{5}$)/) != -1){return true;}
	if(string.search(/^(\d{5}-\d{4}$)/) != -1){return true;}
	alert("For "+label+", please enter a valid US Zip Code of the form #####, or #####-####. Be careful not to mix up the numbers 0 (zero) and 1 (one) with the letters O (oh) and l (el).");
	return false;
} 

function validateaccount(){
	var fel=document.getElementById('form1');
	var alertstr='';
	if(trim(fel.firstname.value)==''){var alertstr=alertstr+'First Name is a required field.\n';}
	if(trim(fel.lastname.value)==''){var alertstr=alertstr+'Last Name is a required field.\n';}
	if(trim(fel.emailaddr.value)==''){var alertstr=alertstr+'Email Address is a required field.\n';}
	if(trim(fel.username.value)==''){var alertstr=alertstr+'Username is a required field.\n';}
	if(trim(fel.emailaddrorg.value)=='' || trim(fel.password.value)!=''){
		var digitpattern = /(\d)/g;
		var alphapattern = /([a-zA-Z])/g;
		if(trim(fel.password.value)==''){var alertstr=alertstr+'Password is a required field.\n';}
		else if(trim(fel.password.value).length < 6){var alertstr=alertstr+'Your Password must be at least 6 characters long.\n';}
		else if(trim(fel.password.value).match(digitpattern)==null
		     || trim(fel.password.value).match(alphapattern)==null){var alertstr=alertstr+'Your Password must contain at least one digit and one letter.\n';}
		else if(trim(fel.password.value) != trim(fel.confirmpassword.value)){var alertstr=alertstr+'Your password and Confirm Password do not match.\n';}
	}
	if(duplicatesubmit){var alertstr='Your transaction is being processed.\n\nPlease wait.';}
	if(!CheckEmail(fel.emailaddr)){return false;}
	if(alertstr.length > 0){alert(alertstr);return false;}
	duplicatesubmit=true;
	return true;
}

function validateaddress(){
	var fel=document.getElementById('form1');
	var alertstr='';
	if(trim(fel.shipnickname.value)==''){var alertstr=alertstr+'Address Nickname is a required field.\n';}
	if(trim(fel.shipfirstname.value)==''){var alertstr=alertstr+'First Name is a required field.\n';}
	if(trim(fel.shiplastname.value)==''){var alertstr=alertstr+'Last Name is a required field.\n';}
	if(trim(fel.shipaddress1.value)==''){var alertstr=alertstr+'Address is a required field.\n';}
	if(trim(fel.shipcity.value)==''){var alertstr=alertstr+'City is a required field.\n';}
	if(trim(fel.shipphone.value).length < 10){var alertstr=alertstr+'Phone is a required field and must contain at least 10 characters.\n';}
	if(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value==''){var alertstr=alertstr+'Country is a required field.\n';}
	else if(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value=='US' &&
	       ( "AB,BC,MB,NB,NF,NT,NS,NT,NU,ON,PE,QC,SK,YT".indexOf(fel.shipstatecode.options[fel.shipstatecode.selectedIndex].value)>=0 ||
					fel.shipstatecode.options[fel.shipstatecode.selectedIndex].value=='')){var alertstr=alertstr+'Since you selected a Country of US, please select a US state for State or Province.\n';}
	else if(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value=='CA' &&
	       "AB,BC,MB,NB,NF,NT,NS,NT,NU,ON,PE,QC,SK,YT".indexOf(fel.shipstatecode.options[fel.shipstatecode.selectedIndex].value)<0){var alertstr=alertstr+'Since you selected a Country of Canada, please select a Canadian province for State or Province.\n';}
	else if("US,CA".indexOf(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value)<0){var alertstr=alertstr+'Please choose "No Selection" for State or Province (the first choice in the list), since you did not select a Country of US or Canada.\n';}
	if(duplicatesubmit){var alertstr='Your transaction is being processed.\n\nPlease wait.';}
	if(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value=='US' && !checkZIP('ZIP or Postal Code',trim(fel.shipzip.value))){return false;}
	if(fel.shipcountrycode.options[fel.shipcountrycode.selectedIndex].value=='CA' && !checkPostalCode('ZIP or Postal Code',trim(fel.shipzip.value))){return false;}
	if(alertstr.length > 0){alert(alertstr);return false;}
	duplicatesubmit=true;
	return true;
}

function searchEvents(){
	var fel=document.getElementById('eventsearch');
	var alertstr='';
	if(trim(fel.q.value)==''){var alertstr=alertstr+'Please enter a search Keyword.\n';}
	if(alertstr.length > 0){alert(alertstr);return false;}
	else{document.location.href='/public.events.cfm/q/'+fel.q.value;return false;}
}

function yourprice(formtype,formel) {
	var startprice=parseFloat(document.getElementById('startprice').value);
	var form=document.getElementById('pd_form');
	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].tagName.toLowerCase()=='select' && form.elements[i].value!=0){
			startprice=startprice+parseFloat(document.getElementById("optionprices"+form.elements[i].value).value);
		}
	}
	document.getElementById('priceyoupay').removeChild(document.getElementById('priceyoupay').childNodes[1]);
	document.getElementById('priceyoupay').appendChild(document.createElement("span").appendChild(document.createTextNode(startprice.toFixed(2))));
}
function validateoptions() {
	var form=document.getElementById('pd_form');
	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].name.indexOf("optionschoiceid_")>=0){
			if(form.elements[i].options[form.elements[i].selectedIndex].value==0){alert("Please select a choice for each Product Option");return false;}
		}
	}
	return true;
}
function externalLinks() {
 if(!document.getElementsByTagName) return;
 var anchors=document.getElementsByTagName("a");
 for(var i=0;i<anchors.length;i++){var anchor=anchors[i];if(anchor.getAttribute("href") && anchor.getAttribute("rel")=="external"){anchor.target="_blank";}}
}
window.onload = initwindow;
window.onresize = resizewindow;
