// UNISHOP.JS - Javascript support for unishop websites

function loadStock(sku,review_order_page) {
	idCGI('stock', 'REPORT_NAME=displayStock&MATCH=' + sku + '&review_order_page=' + review_order_page);
	//jQuery('#stock').load ('/cgi-bin/ubpost', {REPORT_NAME: 'displayStock', MATCH: sku, review_order_page: review_order_page});
}

function displayProduct(sku) {
	idCGI('pageContent', 'REPORT_NAME=displayProduct&MATCH=' + sku);
}

function displayImage(img) {
	jQuery("#image").fadeIn(500);
	frameURL("photo", img);
	}

function zoomImage(sku) {
        jQuery('#imageZoom' + sku).addimagezoom({
		imageId: 'imageZoom' + sku
	})
}

function selectOption(id, num) {
	var selObj = document.getElementById(id);
	selObj.selectedIndex = num;
}

function cartButton(sku, size, sizeSelected, review_order_page) {
	jQuery('div[name="sizeSelect"]').each(function(){jQuery(this).css('background-color', '');});
	document.getElementById(sizeSelected).style.backgroundColor = '#99ff99';
	if (size == 'none') {
		// document.getElementById('cartButton').innerHTML = 'Select a valid size';
		jQuery('#cartButton').html('Select a valid size');
	} else {
		productId=dataCGI('PROC_NAME=productId&ID=' + sku + size);
		jQuery('#cartButton').html('<form class="product_form" enctype="multipart/form-data" method="post" name="product_' + productId + '" id="product_' + productId + '"> <input type="hidden" value="add_to_cart" name="wpsc_ajax_action"/> <input type="hidden" value="' + productId + '" name="product_id"/> <input type="hidden" value="' + review_order_page + '" name="next_page"/> <input style="width: 320px; height: 30px;" class="checkout"  type="submit" value="ADD TO SHOPPING CART" name="Buy" class="wpsc_buy_button" id="product_' + productId + '_button_submit"/> <div class="wpsc_loading_animation"> <img title="Loading" alt="Loading" src="http://www.rmkshoes.com/wp-content/plugins/wp-e-commerce/wpsc-theme/wpsc-images/indicator.gif" /> Updating cart...  </div> </form>');
	}
}

function checkStock(productId, currentQty, requestedQty, form, textId) {
	remaining = parseInt(dataCGI('PROC_NAME=remainingQty&ID=' + productId)) + currentQty;
	if (requestedQty <= remaining) {
		document.getElementById(textId).innerHTML = '';
		return true;
	} else {
		form.value = currentQty;
		document.getElementById(textId).innerHTML = 'Sorry you have exceeded the available stock.';
		return false;
	}
}

function copyDetails() {
	billingFields=new Array("billingfirstname","billinglastname","billingaddress1","billingaddress2","billingcity","billingpostcode","billingstate");
	shippingFields=new Array("shippingfirstname","shippinglastname","shippingaddress1","shippingaddress2","shippingcity","shippingpostcode","shippingstate");
	for (i=0; i<billingFields.length; i++) {
		if (document.getElementById(billingFields[i]).value) {
			document.getElementById(shippingFields[i]).value = document.getElementById(billingFields[i]).value;
			document.getElementById(billingFields[i]).style.color="";
			document.getElementById(shippingFields[i]).style.color="";
			} else {
			document.getElementById(billingFields[i]).style.color="#ffaaaa";
			document.getElementById(shippingFields[i]).style.color="#ffaaaa";
		}
	}
	if (!document.getElementById('billingemail').value) {
			document.getElementById('billingemail').style.color="#ffaaaa";
	}
	if (!document.getElementById('billingphone').value) {
			document.getElementById('billingphone').style.color="#ffaaaa";
	}
}

function copyDetailsAll() {
	billingFields=new Array("billingfirstname","billinglastname","billingaddress1","billingaddress2","billingSelectCity","billingcity","billingpostcode","billingstate");
	shippingFields=new Array("shippingfirstname","shippinglastname","shippingaddress1","shippingaddress2","shippingSelectCity","shippingcity","shippingpostcode","shippingstate");
	for (i=0; i<billingFields.length; i++) {
		if (document.getElementById(billingFields[i]).value) {
			document.getElementById(shippingFields[i]).value = document.getElementById(billingFields[i]).value;
			document.getElementById(billingFields[i]).style.color="";
			document.getElementById(shippingFields[i]).style.color="";
			} else {
			document.getElementById(billingFields[i]).style.color="#ffaaaa";
			document.getElementById(shippingFields[i]).style.color="#ffaaaa";
		}
	}
	if (!document.getElementById('billingemail').value) {
			document.getElementById('billingemail').style.color="#ffaaaa";
	}
	if (!document.getElementById('billingphone').value) {
			document.getElementById('billingphone').style.color="#ffaaaa";
	}
}

function validateNumber(evt) {
	var theEvent = evt || window.event;
	var key = theEvent.keyCode || theEvent.which;
	key = String.fromCharCode( key );
	var regex = /[0-9]/;
	if (evt.keyCode == 8 || evt.keyCode == 46 || evt.keyCode == 37 || evt.keyCode == 39 || evt.keyCode == 9) {
		return true;
	}
	if( !regex.test(key) ) {
		theEvent.returnValue = false;
		if(theEvent.preventDefault) theEvent.preventDefault();
	}
}

function confirmOrder() {
	var fail=0;
fields=new Array("billingfirstname","billinglastname","billingemail","billingemail","billingphone","billingaddress1","billingcity","billingpostcode","billingstate","shippingfirstname","shippinglastname","shippingaddress1","shippingcity","shippingpostcode","shippingstate","ccNumber","ccMonth","ccYear");
	for (i=0; i<fields.length; i++) {
		if (!document.getElementById(fields[i]).value) {
			document.getElementById(fields[i]).style.color="#ffaaaa";
			fail=1;
		} else {
			document.getElementById(fields[i]).style.color="";
			}
		}
	if (fail == '1') {
		alert('Please complete the highlighted fields');
		return false;
	}

// Check for a valid email address
	var email = document.getElementById('billingemail');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		return false;
	}

// check for valid Billing Suburb/State/Postcode
//	suburb = document.getElementById('billingcity').value;
//	postcode = document.getElementById('billingpostcode').value;
//	state = document.getElementById('billingstate').value;
//	if (! (dataCGI('REPORT_NAME=postcodeCheck&MATCH=' + suburb + '&POSTCODE=' + postcode + '&STATE=' + state))) {
//		alert('Please choose a valid Billing Suburb / State / Postcode');
//		return false;
//	}

// check for valid Shipping Suburb/State/Postcode
//	suburb = document.getElementById('shippingcity').value;
//	postcode = document.getElementById('shippingpostcode').value;
//	state = document.getElementById('shippingstate').value;
//	if (! (dataCGI('REPORT_NAME=postcodeCheck&MATCH=' + suburb + '&POSTCODE=' + postcode + '&STATE=' + state))) {
//		alert('Please choose a valid Shipping Suburb / State / Postcode');
//		return false;
//	}
}

function confirmOrderAll() {
	var fail=0;
fields=new Array("billingfirstname","billinglastname","billingemail","billingemail","billingphone","billingaddress1","billingSelectCity","shippingfirstname","shippinglastname","shippingaddress1","shippingSelectCity","ccNumber","ccMonth","ccYear");
	for (i=0; i<fields.length; i++) {
		if (!document.getElementById(fields[i]).value) {
			document.getElementById(fields[i]).style.color="#ffaaaa";
			fail=1;
		} else {
			document.getElementById(fields[i]).style.color="";
			}
		}
	if (fail == '1') {
		alert('Please complete the highlighted fields');
		return false;
	}

// Check for a valid email address
	var email = document.getElementById('billingemail');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		return false;
	}

// check for valid Billing Suburb/State/Postcode
	suburb = document.getElementById('billingcity').value;
	postcode = document.getElementById('billingpostcode').value;
	state = document.getElementById('billingstate').value;
	if (! (dataCGI('REPORT_NAME=postcodeCheck&MATCH=' + suburb + '&SUBURB=' + suburb + '&POSTCODE=' + postcode + '&STATE=' + state))) {
		alert('Please choose a valid Billing Suburb / State / Postcode');
		return false;
	}

// check for valid Shipping Suburb/State/Postcode
	suburb = document.getElementById('shippingcity').value;
	postcode = document.getElementById('shippingpostcode').value;
	state = document.getElementById('shippingstate').value;
	if (! (dataCGI('REPORT_NAME=postcodeCheck&MATCH=' + suburb + '&SUBURB=' + suburb + '&POSTCODE=' + postcode + '&STATE=' + state))) {
		alert('Please choose a valid Shipping Suburb / State / Postcode');
		return false;
	}
}

function toggleImage(imageView) {
        inputs = document.getElementsByName('imageView');
        for(s = 0; s < inputs.length; s++) {
                inputs[s].style.display = 'none';
        }
        document.getElementById(imageView).style.display = '';
}

function checkFriendFields() {
// Check for a valid sender email address
        var email = document.getElementById('senderEmail');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
                alert('Your email address is not valid');
                return false;
        }
// Check for a valid receipient email address
        var email = document.getElementById('friendEmail');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
                alert('Your friends email address is not valid');
                return false;
        }
	return true;
}

function postCodeLoad(suburb, evt, field) {
	evt.preventDefault();
	evt.stopPropagation();
	elm = field + 'SelectCity';
	suburb = document.getElementById(elm).value;

	if (document.getElementById('select1')) {
		document.getElementById('select1').scrollIntoView(true);
	}

	// enter key - select suburb
	if (evt.keyCode == 13) {
		var inputs = jQuery('[name = "selectItem"]').get();
		for(s = 0; s < inputs.length; s++) {
			if (inputs[s].getAttribute('select') == '1') {
				record = s + 1;
				selectSuburb(document.getElementById('select' + record), field);
				return false;
			}
		}
	}
	
	// down arrow
	if (evt.keyCode == 40) {
		var inputs = jQuery('[name = "selectItem"]').get();
		for(s = 0; s < inputs.length; s++) {
			if (inputs[s].getAttribute('select') == '1') {
				inputs[s].setAttribute('select', '0');
				inputs[s].style.color = '#000';
				if (s < inputs.length-1) {
					inputs[s+1].style.color = '#0a0';
					inputs[s+1].setAttribute('select', '1');
					inputs[s].scrollIntoView(true);
					return false;
				} else {
					inputs[0].style.color = '#0a0';
					inputs[0].setAttribute('select', '1');
					return false;
				}
				
			} 
		}
	}
	
	// up arrow
	if (evt.keyCode == 38) {
		var inputs = jQuery('[name = "selectItem"]').get();
		for(s = 0; s < inputs.length; s++) {
			if (inputs[s].getAttribute('select') == '1') {
				inputs[s].setAttribute('select', '0');
				inputs[s].style.color = '#000';
				if (s > 0) {
					inputs[s-1].style.color = '#0a0';
					inputs[s-1].setAttribute('select', '1');
					if (s > 2) { inputs[s-2].scrollIntoView(true); }
					return false;
				} else {
			//		document.getElementById('billingcity').focus();
					return false;
				}
			} 
		}
	}

	var box = jQuery('#' + field + 'Box');
	if (suburb.length > 2) {
		data = dataCGI('PROC_NAME=postcodes&SUBURB=' + suburb + '&FIELD=' + field);
		if (data) {
//			idCGI(field + 'Box', 'PROC_NAME=postcodes&SUBURB=' + suburb + '&FIELD=' + field);
			document.getElementById(field + 'Box').innerHTML = data;
			s = jQuery('#' + elm);
			box.css('top', s.position().top + s.outerHeight() + 10 + 'px');
			box.show();
		} else {
			jQuery('#' + field + 'Box').val('');
			jQuery('#' + field + 'Box').hide();
		}
	}
}

function selectSuburb(rec, field) {
	display = rec.getAttribute('suburb') + ' (' + rec.getAttribute('postcode') + ', ' + rec.getAttribute('state') + ')';
	jQuery('#' + field + 'SelectCity').val(display);
	document.getElementById(field + 'Box').style.display = 'none';
	document.getElementById(field + 'Box').innerHTML = '';
	document.getElementById(field + 'city').value = rec.getAttribute('suburb');
	document.getElementById(field + 'postcode').value = rec.getAttribute('postcode');
	document.getElementById(field + 'state').value = rec.getAttribute('state');
}
 
function compareAddress(field) {
	address = jQuery('#' + field + 'SelectCity').attr('value');
	addressCity = jQuery('#' + field + 'city').attr('value');
	addressPostcode = jQuery('#' + field + 'postcode').attr('value');
	addressState = jQuery('#' + field + 'state').attr('value');
	addressCompare = addressCity + ' (' + addressPostcode + ', ' + addressState + ')';
	
	if (address != addressCompare) {
		jQuery('#' + field + 'SelectCity').val('');
		jQuery('#' + field + 'city').val('');
		jQuery('#' + field + 'postcode').val('');
		jQuery('#' + field + 'state').val('');
	}
}

