// Script Dependancies
// popup_image_script.js

var product_image_switch_elem, product_image_switch_parent = product_image_switch_div = null;

function ProductImageSwitch(elem, id, width, height, image_url) {
	product_image_switch_elem = document.getElementById(id);
	if (product_image_switch_elem != null) {
		product_image_switch_elem.setAttribute('src', '../images/misc/spacer.gif');		
		image_url = (image_url != undefined) ? image_url : elem.attributes.getNamedItem('src').value;
		if (image_url != product_image_switch_elem.attributes.getNamedItem('src').value) {
			product_image_switch_parent = product_image_switch_elem.parentNode;
			
			try {
				product_image_switch_div = document.createElement('<div class="dgray_txt" style="font-weight:bold" align="center">');	
			} catch (e) {
				product_image_switch_div = document.createElement('div');
				product_image_switch_div.setAttribute('class', 'dgray_txt');
				product_image_switch_div.setAttribute('style', 'font-weight:bold');
				product_image_switch_div.setAttribute('align', 'center');
			}
			
			product_image_switch_div.innerHTML = 'Loading ...';
			product_image_switch_parent.removeChild(product_image_switch_elem);
			product_image_switch_parent.appendChild(product_image_switch_div);
			
			product_image_switch_elem.setAttribute('src', image_url);
			product_image_switch_elem.setAttribute('width', width);
			product_image_switch_elem.setAttribute('height', height);
			
			setTimeout("_product_image_switch()", 500);
		}
	}
}

function _product_image_switch() {
	product_image_switch_parent.removeChild(product_image_switch_div);
	product_image_switch_parent.appendChild(product_image_switch_elem);
}

function ProductAskAQuestionOnClick(elem) {
	elem.parentNode.submit();	
}

function ProductRequestACallbackOnClick(elem) {
	elem.parentNode.submit();	
}

function ProductAddShoppingCartOnClick(form_elem) {
	if (ProductAddShoppingCartOnSubmit(form_elem))
		form_elem.submit();
}

function ProductAddShoppingCartOnSubmit(form_elem) {
	var value = parseInt(form_elem.quantity.value);
	if (value > 0) {
		add_product_upgrade_inputs(form_elem);
	} else {
		alert('Please enter positive integer values into the quantity field.\n\nTo view your shoppingcart click on the blue "MyShoppingCart" button located at the top right of this page.\n\nTo delete an item from your shoppingcart, goto "MyShoppingCart" and click on the red "Remove" button.');
		form_elem.quantity.value = 1;
		return false;
	}

	return true;
}

function ProductAddWaitingListOnClick(form_elem, dir_nesting) {
	if (ProductAddShoppingCartOnSubmit(form_elem)) {
		form_elem.attributes.getNamedItem('action').value = dir_nesting + 'pages/waitinglist_information.php';
		form_elem.submit();
	}
}

function ProductUpgradeOnClick(elem) {
	do_product_upgrade_radio_select(elem);
	var price = do_product_upgrade_set_total_price();
	//alert(elem.value);		// debug
	
	show_product_upgrade_price_element(elem, price);
}

function ProductUpgradeInit(upgrades) {
	if (upgrades != '') {
		var ids = upgrades.split(':');
		for(i in ids) {
			var elem = document.getElementById('upgrade_' + ids[i]);
			if (elem != null) {
				do_product_upgrade_radio_select(elem);
				elem.checked = true;
			}
		}
		
		do_product_upgrade_set_total_price();
	}
}

function ProductUpgradeShippingCalculateOnSubmit(form_elem) {
	var upgrade_elems = get_product_upgrade_inputs();
	var postage_multiplier = 1;
	var return_link = '';
	var title = '';
	for(i in upgrade_elems) {
		var elem = upgrade_elems[i];
		if (elem.checked) {
			var values = elem.value.split('::');
			postage_multiplier *= parseFloat(values[2]);
			return_link += ':' + escape(elem.id.match(/[a-z]{1}\d+$/));
			title += '::' + escape(values[0]);
		}
	}
	form_elem.u.value = postage_multiplier;
	form_elem.r.value = return_link.substr(1);
	form_elem.t.value = title.substr(2);
	return true;
}

function get_product_upgrade_inputs() {
	var result = new Array();
	var description_elem = document.getElementById('product_description');
	if (description_elem != null) {
		var inputs = description_elem.getElementsByTagName('input');
		for(i in inputs) {
			var input = inputs[i];
			if (input.type == 'radio' && input.id.match(/^upgrade_[a-z]{1}\d+$/) != null) {
				// systems like opera creates double entries with getElementById, therefore check if input already in result before push
				var found = false;
				for(j in result) {
					if (result[j] == input) {
						found = true;
						break;
					}
				}
				if (!found)
					result.push(input);
			}
		}
	}
	return result;
}

/*function add_product_upgrade_inputs(form_elem) {
	var upgrade_elems = get_product_upgrade_inputs();
	for(i in upgrade_elems) {
		var elem = upgrade_elems[i];
		if (elem.checked) {
			var input;
			try {
				input = document.createElement('<input name="upgrade' + elem.attributes.getNamedItem('index').value + '" type="hidden" value="' + elem.value + '::' + elem.id.match(/[a-z]{1}\d+$/) + '">');
			} catch (e) {
				input = document.createElement('input');
				input.setAttribute('name', 'upgrade' + elem.attributes.getNamedItem('index').value);
				input.setAttribute('type', 'hidden');
				input.setAttribute('value', elem.value + '::' + elem.id.match(/[a-z]{1}\d+$/));
			}
			form_elem.appendChild(input);
		}
	}
}*/

function add_product_upgrade_inputs(form_elem) {
	var upgrade_elems = get_product_upgrade_inputs();
	var parsed_upgrades = '';
	for(i in upgrade_elems) {
		var elem = upgrade_elems[i];
		if (elem.checked) {
			parsed_upgrades += ':::' + elem.value + '::' + elem.id.match(/[a-z]{1}\d+$/);
		}
	}
	if (parsed_upgrades != '') {
		var input;
		parsed_upgrades = parsed_upgrades.substr(3);
		try {
			input = document.createElement('<input name="upgrades" type="hidden" value="' + parsed_upgrades + '">');
		} catch (e) {
			input = document.createElement('input');
			input.setAttribute('name', 'upgrades');
			input.setAttribute('type', 'hidden');
			input.setAttribute('value', parsed_upgrades);
		}
		form_elem.appendChild(input);	
	}
}

function do_product_upgrade_radio_select(elem) {
	var set_values = elem.value.split('::');
	var set_price = parseInt(set_values[1]);
	
	var id = elem.id.match(/^upgrade_[a-z]{1}/);
	if (id != null) {
		var i = 0;
		while(++i) {
			var input = document.getElementById(id + i);
			if (input == null || input == undefined) break;
			
			var price = document.getElementById(id + i + '_price');
			if (price != null) {
				if (input == elem) {
					input.checked = true;
					
					price.setAttribute('class', 'bold_txt');
					price.innerHTML = '[Included in Price]';
				} else {
					input.checked = false;
					
					var values = input.value.split('::');
					var new_price = parseInt(values[1]) - set_price;
					
					price.setAttribute('class', '');
					price.innerHTML = '[' + ((new_price >= 0) ? 'add $' : 'subtract $') + Math.abs(new_price) + '.00]';
				}
			}
		}
	}
}

function do_product_upgrade_set_total_price() {
	var price_elem_a = document.getElementById('price_a');
	var price_elem_b = document.getElementById('price_b');
	
	var new_price = parseFloat(price_elem_a.attributes.getNamedItem('value').value);
	var upgrade_elems = get_product_upgrade_inputs();
	for(i in upgrade_elems) {
		var input = upgrade_elems[i];
		if (input.checked) {
			var values = input.value.split('::');
			new_price += parseInt(values[1]);
		}
	}
																	
	var new_price_elem_text = 'AU ' + float_to_currency(new_price);
	price_elem_a.innerHTML = new_price_elem_text;
	price_elem_b.innerHTML = new_price_elem_text;
	return new_price;
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = parseFloat(number), c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function str_remove_currency_token(input) {
	return input.toString().replace(/[^0-9.+-]/g, '');	
}

function float_to_currency(value) {
	var value = parseFloat(value);
	
	if (isNaN(value)) return value;  // return NaN value
		
	if (value <= -0.01)
		return '- $' + number_format(Math.abs(value), 2);
	if (value >= 0.01)	
		return '$' + number_format(value, 2);
		
	return '$0.00';	
}

function ProductJumpOnChange(elem){
  //eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  //if (restore) selObj.selectedIndex=0;
  var location = elem.options[elem.selectedIndex].value;
  if (location != '') {
	  window.location = location;
  } else elem.selectedIndex = 0;
}

function ProductPostageCalculateOnKeyDown(e, elem) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	if (keycode == 13) {		// return key
		var form_elem = elem.parentNode;
		while(form_elem.tagName != 'FORM' && form_elem.parentNode != null) form_elem = form_elem.parentNode;
		ProductUpgradeShippingCalculateOnSubmit(form_elem);
		form_elem.submit();
	}
}

function ProductPostageCalculateButtonOnClick(elem) {
	var form_elem = elem.parentNode;
	while(form_elem.tagName != 'FORM' && form_elem.parentNode != null) form_elem = form_elem.parentNode;
	ProductUpgradeShippingCalculateOnSubmit(form_elem);
	form_elem.submit();
}

//--------------------------------------------
var product_upgrade_price_element_index = 0;
function create_product_upgrade_price_element(price) {
	var index = product_upgrade_price_element_index++;
	
	var id = 'product_price_elem';
	var pre_existing_elem = document.getElementById(id);
	if (pre_existing_elem != null && pre_existing_elem != undefined) {
		document.body.removeChild(pre_existing_elem);
		//alert('here');		// debug
	}
	
	var shadow;
	var class_name = 'product_price_opaque_layer';
	try {
		shadow = document.createElement('<div id="' + id + '" class="' + class_name + '" index="' + index + '">');
	} catch (e) {
		shadow = document.createElement('div');
		shadow.setAttribute('id', id);
		shadow.setAttribute('class', class_name);
		shadow.setAttribute('index', index);
	}
	
	shadow.innerHTML = 'Price = AU ' + float_to_currency(price);
	
	document.body.appendChild(shadow);
	return shadow;
}

function show_product_upgrade_price_element(elem, price) {
	var shadow = create_product_upgrade_price_element(price);
	var id = shadow.id;
	var pos = getAbsolutePosition(elem);
	var index = parseInt(shadow.attributes.getNamedItem('index').value);
	
	shadow.style.left = pos.x + 'px';
	shadow.style.top = (parseInt(pos.y) + 20) + 'px';
	shadow.style.display = "block";
	
	for(var i = 15; i >= 0; i--)
		setTimeout(get_product_upgrade_price_element_opacity_function_string(id, index, parseFloat(i) / 20), 2000 + (100 * (18 - i)));
}

function get_product_upgrade_price_element_opacity_function_string(id, index, opacity) {
	return 	'do_product_upgrade_price_element_opacity("' + id + '",' + index + ',' + opacity + ')';
}

function do_product_upgrade_price_element_opacity(id, index, opacity) {
	var elem = document.getElementById(id);
	if (elem != null && elem != undefined) {
		var saved_index = parseInt(elem.attributes.getNamedItem('index').value);
		if (saved_index == index) {
			if (opacity > 0) {
				if (elem.style != undefined && elem.style.opacity != undefined)
					elem.style.opacity = opacity;
				if (elem.filters != undefined)
					elem.MozOpacity = opacity;
				if (elem.KhtmlOpacity != undefined)
					elem.KhtmlOpacity = opacity;
				if (elem.filters != undefined && elem.filters.alpha != undefined && elem.filters.alpha.opacity != undefined)
					elem.filters.alpha.opacity = opacity * 100;
				if (elem.filter != undefined || elem.filter != null)
					elem.filter = "alpha(opacity=" + (opacity * 100) + ")";
			}
			else
				document.body.removeChild(elem);
		}
	}
}
