/*
+----------------------------------------------------------------------+
| Product Packaging Mod                                                |
+----------------------------------------------------------------------+
| Copyright (c) 2009 BCSE LLC. dba BCS Engineering                     |
+----------------------------------------------------------------------+
|                                                                      |
| Product Packaging Mod is subject for version 2.0 of the BCSE         |
| proprietary license. That license file can be found bundled with     |
| this package in the file BCSE_LICENSE. A copy of this license can    |
| also be found at                                                     |
| http://www.bcsengineering.com/license/BCSE_LICENSE_2.0.txt           |
|                                                                      |
+----------------------------------------------------------------------+
*/

function change_price_ajax(pid){

	if(!document.getElementById('options_'+pid) || !document.getElementById('price_'+pid))
		return;

	optionsObj = document.getElementById('options_'+pid);
	if(optionsObj.childNodes.length < 1)
		return;

	var options_text = '';
	for(var x=0; x<optionsObj.childNodes.length; x++){
		if(optionsObj.childNodes[x].className == 'opt_right'){
			valueObj = optionsObj.childNodes[x];
			if(valueObj.childNodes.length < 1)
				continue;

			for(var y=0; y<valueObj.childNodes.length; y++){
				if(valueObj.childNodes[y].id && valueObj.childNodes[y].id.match(/^po\_/i)){
					var input_id = valueObj.childNodes[y].id;
					var class_id = input_id.replace(/po_[\d]*_/i,"");
					var class_value = valueObj.childNodes[y].value;
					options_text = options_text + (options_text != ''?'@@bcse@@':'') + class_id + ':' + class_value;
					continue;
				}
			}
		}
	}

	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return;
	}
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4){
			var return_price = xmlHttp.responseXML.getElementsByTagName('price')[0].firstChild.nodeValue;
			child_prices[pid] = return_price;
			document.getElementById('price_'+pid).innerHTML = currency_symbol + price_format(return_price);

			if(xmlHttp.responseXML.getElementsByTagName('tmbn')[0].hasChildNodes()){
				var return_tmbn = xmlHttp.responseXML.getElementsByTagName('tmbn')[0].firstChild.nodeValue;
				if(return_tmbn != '')
					document.getElementById('t_'+pid).src = return_tmbn;
			}

			if(document.getElementById('oos' + pid)){

				var oosObj = document.getElementById('oos' + pid);

				while(oosObj.hasChildNodes())
					oosObj.removeChild(oosObj.lastChild);

				var outofstock = xmlHttp.responseXML.getElementsByTagName('outofstock')[0].firstChild.nodeValue;
				if(outofstock == 'Y'){
					oosObj.appendChild(document.createTextNode(package_out_of_stock_item));
					oosObj.style.display = 'block';
					child_out_of_stock[pid] = 'Y';
				}else{
					oosObj.style.display = 'none';
					child_out_of_stock[pid] = 'N';
				}
			}

			reset_package_price();
		}
	}
	xmlHttp.open("GET",xcart_web_dir+'/modules/Product_Packaging/product_price_ajax.php?parentid='+parentid+'&productid='+pid+'&options='+options_text,true);
	xmlHttp.send(null);

	return;
}

function reset_package_price(){
	if(!document.getElementById('main_price') || !child_productids || child_productids.length < 1 || package_offset_type == 'C')
		return;

	var final_price = 0;
	for(var x in child_productids){
		if(document.getElementById('price_'+child_productids[x]) && document.getElementById('product_amount_'+child_productids[x])){
			var child_price = child_prices[child_productids[x]];
			var child_amount = document.getElementById('product_amount_'+child_productids[x]).value;
			final_price = +final_price + (+child_price * +child_amount);
		}
	}

	var final_offset = (package_offset_type == 'A'?+package_offset:(+final_price * +package_offset / 100));

	if(document.getElementById('reg_price'))
		document.getElementById('reg_price').innerHTML = currency_symbol + price_format(final_price);

	if(final_offset != 0)
		final_price = +final_price + +final_offset;

	document.getElementById('main_price').innerHTML = currency_symbol + price_format(final_price);
}

function submit_package_form(){
	for(var x in child_out_of_stock)
		if(child_out_of_stock[x] == 'Y'){
			alert(package_out_of_stock);
			return false;
		}

	document.forms['packageform'].submit();
}

function GetXmlHttpObject(){
	var xmlHttp
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

