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";
 }
}

function checkNumeric(objName, minval, maxval, comma, period, hyphen, label){
	var numberfield = objName;
	if (chkNumeric(objName, minval, maxval, comma, period, hyphen, label) == false){
		numberfield.select();
		numberfield.focus();
		return false;
	}else{
		return true;
	}
}

function chkNumeric(objName, minval, maxval, comma, period, hyphen, label){
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for (i = 0;  i < checkStr.value.length;  i++){
		ch = checkStr.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j)) break;
		if (j == checkOK.length){
			allValid = false;
			break;
		}
		if (ch != ",") allNum += ch;
	}
	if (label=='') label = checkStr.name;
	if (!allValid){	
		alert("The field \"" + label + "\" requires a numeric value.");
		return (false);
	}
	
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval)){
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + label + "\" field."
		alert(alertsay);
		return (false);
	}
}

function qty_down(){
	var el = $('qty');
	var qty = parseInt(el.value);qty--;
	if (qty <= 1) qty=1;
	el.value = qty;
	el.select();
	el.focus();	
}

function qty_up(frm){
	var el = $('qty');
	var qty = parseInt(el.value);qty++;
	if (qty <= 1) qty=1;
	el.value = qty;
	el.select();
	el.focus();	
}

function product_option_set_details(item_id, item_options){
	/**
	 * Use AJAX to get the price and stock 
	 * availability for a product option pair
	 */
	$('p_calc_val').update('<i>Loading ...</i>');
	$('p_stck_val').update('<i>Loading ...</i>');
	new Ajax.Request('/aqshop/?action=product_option_get_details', {
		method: 'get',
		parameters: { item_id: item_id, item_options: item_options },
		onSuccess: product_option_set_details_success,
		onFailure: product_option_set_details_fail
	});
}

function product_option_set_details_fail(transport, data){
	$('p_calc_val').update('<i> Error </i>');
	$('p_stck_val').update('<i> Error </i>');
	alert("There was an error getting the details for this product option");
}

function product_option_set_details_success(transport, data){
	/**
	 * Display the results from the AJAX call
	 */
	if (isNaN(data.item_price)) {
		if (data.item_price_was != '0') 
			$('p_calc_val').update(data.item_price + '<br />Was <s>' + data.item_price_was + '</s> SAVE ' + data.item_price_save);
		else 
			$('p_calc_val').update(data.item_price);
	} else {
		if (data.item_price_was != '0') 
			$('p_calc_val').update('&pound;' + data.item_price + '<br />Was <s>&pound;' + data.item_price_was + '</s> SAVE ' + data.item_price_save);
		else 
			$('p_calc_val').update('&pound;' + data.item_price);
	} 
	$('p_stck_val').update(data.item_stock);
	if (data.item_stock == 'Not available') {
		$('aqhop_add').disabled = true;
	} else {
		$('aqhop_add').disabled = false;
	}
}

function product_option_select_change(){
	/**
	 * change price and stock when an option select is changed
	 */
	var option_selects = $$('select[rel="item_option_select"]').sortBy(function(element){
		return element.name;
	});
	var item_options = '';
	option_selects.each(function(element){
		if (item_options!='') item_options+=',';
		item_options += element.options[element.selectedIndex].value;
	});
	// Normalise option value string by ID ASC
	item_options = item_options.split(',').sort(function(a,b){return a - b}).join(',');
	
	var item_id = $('pid').value;
	if (item_id > 0 && item_options != '') product_option_set_details(item_id, item_options);
}

function product_options_change(item_id, item_options, item_price){
	/**
	 * Receive the options string
	 * Parse the strip and set the selected index of the relevent select boxes
	 * Update details via AJAX
	 */
	var options_selected = item_options.split(',');
	if (options_selected.length) {
		var option_selects = $$('select[rel="item_option_select"]');
		option_selects.each(function(element){
			for(var i=0; i<element.options.length; i++){ 
			    if(options_selected.indexOf(element.options[i].value) >= 0) { 
			        element.selectedIndex = i; 
			    	break;
				}
			} 
		});
	}
	
	if (item_id > 0 && item_options != '') product_option_set_details(item_id, item_options);
}
