// Roleover effects for side menu navigation, changes class and image
function role(el, mode, doImg) {
	// Store old src/class
	if (el.oldClass == null) el.oldClass = el.className

	// Get image and store old src
	if(doImg) {
		var	imgNode = document.getElementById(el.id + '_img');
		if(imgNode === null) {
			doImg = false;
		} else {
			// Break name into base and extension
			if(imgNode.oldSrc == null) {
				imgNode.oldSrc = imgNode.src.substring(0, imgNode.src.lastIndexOf('.'));
				imgNode.oldExt = imgNode.src.substring(imgNode.src.lastIndexOf('.'));
			}
		}
	}

	// Mouse over
	if (mode == "in") {
		el.className = el.oldClass + "Over";
		if(doImg)
			imgNode.src = imgNode.oldSrc + "Over" + imgNode.oldExt;
	}
	
	// Mouse out
	if (mode == "out") {
		el.className = el.oldClass;
		if(doImg)
			imgNode.src = imgNode.oldSrc + imgNode.oldExt;
	}
}


function go(url){
	window.location = url
}



/*
 * Show payment method fields only applicable to the current payment method
 */
function showPaymentFields(curMethod) {
	var i;	
	var curEl;
	for(i=0;i<GenericForm.elements.length;i++) {
		curEl = GenericForm.elements[i];
		if(curEl.PaymentMethod != null && curEl.PaymentMethod != "") {
			if(curEl.PaymentMethod.indexOf(curMethod) == -1) {
				curEl.parentNode.parentNode.style.display = "none";
			} else {
				curEl.parentNode.parentNode.style.display = "";
			}
		}
	}
}

/*
 * Returns true if the given tag is visible
 */
function isVisible(el) {
	while(el.tagName != "BODY") {
		if(el.style.display == "none") return false;
		el = el.parentNode;
	}
	return true;	
}

function updateRequiredFields() {
	var i, curEl;
	for(i=0;i<GenericForm.elements.length;i++) {
		curEl = GenericForm.elements[i];
		if(curEl.required && curEl.value == "" && isVisible(curEl)) {
			curEl.className = "required";
		} else {
			curEl.className = "";
		}
	}
}

function checkRequiredFields() {
	for(i=0;i<GenericForm.elements.length;i++) {
		curEl = GenericForm.elements[i];
		if(curEl.required && curEl.value == "" && isVisible(curEl)) {
			alert("Please fill out all the highlighted fields before submitting");
			event.returnValue = false;
			return;
		}
	}
}

function ccValidate(el) {
	if(el.value == "") return;
	if(el.value.length != 13 && el.value.length != 15 && el.value.length != 16) {
		alert("That credit card number does not appear to be a valid length!");
		return;
	}
	
	var i, char1, char2, total = 0;
	for(i=0;i<el.value.length;i+=2) {		
		char1 = parseInt(el.value.substr(el.value.length - i-1 ,1));
		char2 =  parseInt(el.value.substr(el.value.length - i-2 ,1));

		if(isNaN(char2)) char2 = 0;
		char2 *= 2;		
		if(char2 > 10) char2 -= 9;

		total += char1 + char2;
	}
	if(total % 10 != 0)
		alert("That credit card number does not appear to be valid!");
}


function searchStart(el){
	El.value = ""
}

function searchEnd(el){
	if (el.value == "") { el.value = "<search>"}
}

/*
 * Checkout functions
 */
function updatePricing(idx) {
	if(document.getElementById) {
		var qField = document.getElementById('Quantity_' + idx);
		var pField = document.getElementById('ItemPrice_' + idx);

		qField.value = parseInt(qField.value);	
		
		var oldVal = decodeCurrency(pField.innerHTML);
		var unitPrice = parseFloat(compat_getAttribute(qField, 'unitprice'));
		var newVal = (qField.value * unitPrice);
		var oldQ = oldVal / unitPrice;
				
		pField.innerHTML = currency(newVal);

		// Update quantity
		var numItemsField = document.getElementById('totalNumItems');
		var oldNumItems = decodeCurrency(numItemsField.innerHTML);
		numItemsField.innerHTML = parseInt(oldNumItems - oldQ + parseInt(qField.value));


		// Update item total 
		var totalField = document.getElementById('itemTotal');
		if(totalField != null) {
			var oldTotal = decodeCurrency(totalField.innerHTML);
			var itemtotal = oldTotal - oldVal + newVal;
			totalField.innerHTML = currency(itemtotal);
		}


		// Get subtotal field
		var subTotalField = document.getElementById('subTotal');
		if(subTotalField == null) subTotalField = document.getElementById('totalPrice');
			

		if(subTotalField != null) {
			var shipping = parseFloat(compat_getAttribute(subTotalField, 'shipping'));
			var subtotal = decodeCurrency(subTotalField.innerHTML) - oldVal + newVal;
			subTotalField.innerHTML = currency(subtotal);
		}
		
		// Update tax, if it exists
		var tax = 0;
		var taxField = document.getElementById('tax');
		if(taxField != null) {
			tax = subtotal * parseFloat(compat_getAttribute(taxField, 'taxrate'));
			taxField.innerHTML = currency(tax);
		}
		
		// Update full total, if it exists
		var fullTotal = document.getElementById('fullTotal');
		if(fullTotal != null) fullTotal.innerHTML = currency(subtotal + tax);
	}
}
function currency(val) {
	if(val == 0) return "$0.00";
	val = Math.round(val * 100).toString();
	return '$' + val.substr(0, val.length - 2) + '.' + val.substr(val.length - 2);
}
function decodeCurrency(val) {
	val = val.replace(/[^0-9.]/g, '');
	if(val == "") return 0;
	else return parseFloat(val);	
}

function removeCartItem(idx) {
	var qField = document.CheckoutForm.elements['Quantity_' + idx];
	qField.value = 0;
	updatePricing(idx);
}

/*
 * Quick order
 */
function updatePricing2(idx) {
	if(document.all) {
		var qField = document.all('Quantity_' + idx);
		var pField = document.all('ItemPrice_' + idx);
		
		var sizes = document.all('ProductSize_' + idx);
		var newVal = qField.value * decodeCurrency(sizes.options[sizes.selectedIndex].unitprice);

		
		pField.innerHTML = (newVal == 0) ? '' : currency(newVal);
	}
}
 
 
/* Payment type dropdown */
function filterToPaymentType(formObj, paymentField) {
	if(!(formObj = getFormObj(formObj))) return;

	// Deal with radios better
	var curPaymentMethod = null;
	if(!paymentField.value && paymentField.length) {
		for(var i=0;i<paymentField.length;i++) {
			if(paymentField[i].checked) {
				curPaymentMethod = paymentField[i].value;
				break;
			}
		}
	} else {
		curPaymentMethod = paymentField.value;
	}
	
	var i, el, displayMe, record;
	for(i=0;i<formObj.elements.length;i++) {
		el = formObj.elements[i];
		if(el.parentNode != null) {
			if(el.parentNode.tagName.toLowerCase() == "td") record = el.parentNode.parentNode;
			else record = el.parentNode;
			record.style.display = appliesToPaymentType(el, curPaymentMethod) ? "" : "none";
			
		}
	}
	
	if(paymentField.value == "PayPal") {
		if(formObj.oldAction == null) formObj.oldAction = formObj.action;
		formObj.action = "https://www.paypal.com/cgi-bin/webscr";		
	} else {
		if(formObj.oldAction != null) formObj.action = formObj.oldAction;
	}
	
	
	
		if( document.getElementById("action_ProcessPayment") != null){
			
			if(paymentField.value == "Invoice"){		
				document.getElementById("action_ProcessPayment").value = "Continue"
				}
				
			else{
				document.getElementById("action_ProcessPayment").value = "Make Payment"	
			}
		
	}
	
}
function appliesToPaymentType(el, paymentMethod) {
	var pT = compat_getAttribute(el,'paymentMethods');
	if(pT) {
		pT = ' ' + pT + ' ';
		return pT.indexOf(' ' + paymentMethod + ' ') != -1;
	} else {
		return true;
	}
}

function compat_getAttribute(el, attribute) {
	var attNode = el.getAttributeNode(attribute);
	if(attNode != null) return attNode.value;
	else return null;
}