// JavaScript Document

function imgSwap(oImg)
{
   var strOver  = "_on"    // image to be used with mouse over
   var strOff = "_off"     // normal image
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1) 
      oImg.src = strImg.replace(strOver,strOff)
   else
      oImg.src = strImg.replace(strOff,strOver)
}

function initAddSizeLinks() {
	// alert('updating size links');
	var product_idx = 0;
		
	do {
			
		 var productElem = document.getElementById('prod_'+product_idx);   //cycles through all products
		// alert('testing product '+product_id);
		 if (null != productElem) {
			
			if (updateAddSizeLink(product_idx) == false) {
				//alert('skipping product #'+product_id);
				product_idx++;
				continue;
			}
			
		//	alert('about to update link for prodcut #'+product_id);
			updateAddSizeLink(product_idx);
			product_idx++;
		 } else {
			 product_idx++;
		 }
		   
	} while (null != productElem) 
	
	
	
	
	
}


function ShowParentInfo(radio,index) {
	var pDiv = getElem('parent_fields_' + index);
	if(radio.value == 'happy camper')
		pDiv.style.display = '';
	else
		pDiv.style.display = 'none';
}


function replaceValueInURL(string, name, value) {
	var valIdx = string.indexOf(name+'=');
	var rv = string.substr(0,valIdx+1+name.length);
	
	//alert("old = "+string);
	
	var ampIdx = string.indexOf('&',valIdx);
	var rv2 = '';
	if (ampIdx != -1) {
		rv2 = string.substr(ampIdx, string.length);
	}
	
//	alert("new = "+rv+value+rv2);
	
	return rv+value+rv2;


}



function updateAddSizeLink(product_idx) {
	var sizeElem = document.getElementById('product_'+product_idx+'_size_var');
	var selectElem = document.getElementById('product_'+product_idx+'_size');
	if (null == sizeElem) return false;
	var size = selectElem.options[selectElem.selectedIndex].text;
	sizeElem.value = size;
}

function handlePlanChange(name) {
	var planPurchURL = document.getElementById(name+'_purch_url');
	var targetHref = planPurchURL.href;
	var planSelectId = (document.getElementById(name)).value;
	planPurchURL.href = replaceValueInURL(targetHref,'product_id',planSelectId);
	
	//if id is odd, that means it's a paper copy, so we make sure extra field is displayed
	//if even, we make sure it's not displayed
	//handleExtrasChange and resetExtras are used to update the purchase URL's 'extras' field
	var extraField = document.getElementById(name+'_extras');
	if ((planSelectId % 2) == 1) {
		extraField.style.display = '';
		handleExtrasChange(name);
	} else {
		resetExtras(name);
		extraField.style.display = 'none';
	}

}

/* used when an electronic copy is selected...have to make sure there are no "extras" in the 	
	purchase url
*/
function resetExtras(name) {
	var planPurchURL = document.getElementById(name+'_purch_url');
	var targetHref = planPurchURL.href;
	planPurchURL.href = replaceValueInURL(targetHref,'extras',0);
}

function handleExtrasChange(name) {
	var numExtras = (document.getElementById(name+'_extras_field')).value;
	if (numExtras == '') numExtras = 0;
	var planPurchURL = document.getElementById(name+'_purch_url');
	var targetHref = planPurchURL.href;
	planPurchURL.href = replaceValueInURL(targetHref,'extras',numExtras);
	
	
}

function isNumberKey(evt) {
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		alert('The character you just entered is not a number.  Only numbers are allowed here.');
 }



function InfoPopup(url) {
	url += '?popup=1';
	window.open(url,"newWin","width=400,height=400,toolbar=no,menubar=no");
}


function SizeablePopup(url, w, h) {
	url += '?popup=1';
	window.open(url,"newWin","width="+w+",height="+h+",toolbar=no,menubar=no,scrollbars=1");
}

function ReturnPolicyPopup(url) {
	SizeablePopup(url, 600, 800);
}

function MerchReturnPolicyPopup(url) {
	SizeablePopup(url, 600, 500);
}


function getElem(id) {
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
		return document.all[id];
	else if(document.layers)
		return document.layers[id];
}

function ShowPayInfo(payType) {
	var ccSet = getElem('credit_card_info');
	var ecSet = getElem('e_check_info');
	var sRow = getElem('submit_row');
	var ccStyle = 'none';
	var ecStyle = 'none';
	if(payType == 'credit_card')
		ccStyle = '';
	else
		ecStyle = '';
	ccSet.style.display = ccStyle;
	ecSet.style.display = ecStyle;
	sRow.style.display = '';
}

function CopyBilling(cBox,payType) {
	if(!cBox.checked)
		return;
	var fKeys = new Array('address','city','state','zip');
	for(var i = 0; i < fKeys.length; i++) {
		var fVal = billInfo[fKeys[i]];
		var elem = getElem(payType + '_' + fKeys[i]);
		if(fKeys[i] == 'state')
			SetMenu(elem,fVal);
		else
			elem.value = fVal;
	}
}

function SetMenu(menu,value) {
	for(var j = 0; j < menu.options.length; j++) {
		if(menu.options[j].value == value) {
			menu.options[j].selected = true;
			return;
		}
	}
}