// JavaScript Document
/**
 * JS file created for calculating the price based upon the quantity provided by user.
 * @author Khawaja Mohsin Ali mohsinsecret@googlemail.com
 * @package include
 * @subpackage /js
 * @name price_calculator.js
 * since September 30, 2009
 * @copyright http://www.diy.co.uk
 */
var selectedDivID = "containerDiv";
var quantityRanges = new Array();
function clearCells(){
	var temp;
	for(var x=0;x<9;x++){//alert(deliveredId[x]);
		temp="collection"+quantityRanges[x];
		if(document.getElementById(temp)){
		//alert("inside");
			document.getElementById(temp).style.backgroundColor="#FFFFFF";
			document.getElementById(temp).style.backgroundImage="";
			document.getElementById("delivered"+quantityRanges[x]).style.backgroundImage="";
			document.getElementById("delivered"+quantityRanges[x]).style.backgroundColor="#FFFFFF";
		}	
	}	
}
function selectPriceTD(tep){
	//alert("jkhakshf");//1+	3+	6+	11+	16+	21+	
	clearCells();
	if(document.formSelect.qty.value==""){
		return true;
	}
	var quantity=parseInt(document.formSelect.qty.value);
	var selectedCol=1;
    var priceType;

	/////
	if(quantity<0){
		alert("Please enter valid quantity.");
		return true;
	}/*else if (quantity>=quantityRanges[0]&&quantity<quantityRanges[1]){
		selectedCol=quantityRanges[0];
	}else if(quantity>=quantityRanges[1]&&quantity<quantityRanges[2]){
		selectedCol=quantityRanges[1];
	}else if(quantity>=quantityRanges[2]&&quantity<quantityRanges[3]){
		selectedCol=quantityRanges[2];
	}else if(quantity>=quantityRanges[3]&&quantity<quantityRanges[4]){
		selectedCol=quantityRanges[3];
	}else if(quantity>=quantityRanges[4]&&quantity<quantityRanges[5]){
		selectedCol=quantityRanges[4];
	}else if(quantity>=quantityRanges[5]){
		selectedCol=quantityRanges[5];
	}*/
	for(var counter=0;counter<quantityRanges.length; counter++){
		if(counter == quantityRanges.length-1){
			if(quantity>=quantityRanges[counter]){
				selectedCol=quantityRanges[counter];
				break;
			}
		}
		if(quantity>=quantityRanges[counter]&&quantity<quantityRanges[counter+1]){
			selectedCol=quantityRanges[counter];
			break;
		}	
	}
	if (document.formSelect.delivery_option[0].checked){
		priceType="collection"+selectedCol;
	}else{
		priceType="delivered"+selectedCol;//alert("poiij");
	}
//	console.log(priceType);
	document.getElementById(priceType).style.backgroundImage="url(../../templates/diyinsulationcouk/images/selected_price.gif)";

}//end of func
function getProductPrice(productID){
	url = "/slider/flash_product.php?p="+productID;

	if (window.XMLHttpRequest){
			xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		if(new ActiveXObject("Microsoft.XMLHTTP")){
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){	
			if(xmlhttp.status==200){
				updatePriceTable(xmlhttp);
			}
			else{
				document.getElementById('containerDiv').innerHTML = "There was some error in retrieving price table. Please refresh page and try again.";
			}
		}else{
			document.getElementById('containerDiv').innerHTML = "Loading...";
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send("");
	
	
	
	/*url = "/slider/flash_product.php?p="+productID;
	//url="flash_product.xml";
	new Ajax.Request(url,{
	method:'post',
	onLoading:function(){
		$(selectedDivID).update("Loading...");
		//document.getElementById('containerDiv').innerHTML = "Loading...";
	},
	onSuccess:function(response){
	},
	onFailure:function(){
		$(selectedDivID).update("The Server Did not Respond in Promptly Manner. Please Refresh the Page.");
	}
	});*/
}

function updatePriceTable(response){
		var xmlResponse = response.responseXML;
		var product = xmlResponse.getElementsByTagName("product");
		priceCode = product[0].getAttribute("code");
		//console.log(product.childNodes.length);
		var prices = xmlResponse.documentElement.childNodes;
//		console.log(xmlResponse.documentElement.childNodes.length);
		var collectionTR="<tr height=35>", collectionTD="", deliveredTR="<tr height=35 >", deliveredTD="",topTR="<tr height=35>",topTD="",bottomTR="<tr height=35 >",bottomTD="";

		////////////////////////////////////
		topTD+="<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'></td>";
		/*topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"1+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"3+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"6+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"11+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"16+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"21+" +"</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>&nbsp;</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"Bulk" +"</td>" ;*/
		////////////////////
		collectionTD += "<td id='' class='product_price_td' style='border-left: solid 1px #8F9EB4;'>Collection</td>\n";
		deliveredTD += "<td id='' class='product_price_td' style='border-left: solid 1px #8F9EB4;'>Delivered</td>\n";
		for(var productN=0; productN<prices.length; productN++){
			if(prices[productN].nodeType ==1 ){
				//console.log(prices[productN].nodeName);	
				switch(prices[productN].nodeName){
					case 'prices':
					var price = prices[productN];
					for(var number=0; number<price.childNodes.length;number++){
						if(price.childNodes[number].nodeType== 1){
							if(prices[productN].getAttribute('type')=="collection"){
								quantityRanges.push(price.childNodes[number].getAttribute('minQty'));
								//console.log(price.childNodes[number].getAttribute('amount'));
								topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+price.childNodes[number].getAttribute('minQty')+"+</td>" ;

								collectionTD += "<td id='collection"+price.childNodes[number].getAttribute('minQty')+"' class='product_price_td'>"+price.childNodes[number].getAttribute('amount')+"</td>\n";
							
							}else if(prices[productN].getAttribute('type')=="delivered"){
								deliveredTD += "<td id='delivered"+price.childNodes[number].getAttribute('minQty')+"' class='product_price_td'>"+price.childNodes[number].getAttribute('amount')+"</td>\n";
							
							}
						}
						///////
								
		
													////////
					}
					break;
				}
			}
		}
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>&nbsp;</td>" ;
		topTD+= "<td id='' class='price_table_header_td' style='border-right: solid 1px #8F9EB4;'>"+"Bulk" +"</td>";

		topTR+=topTD;
		collectionTR += collectionTD + "<td id='' class='product_price_td'>&nbsp;</td><td id='' class='product_price_td'>"+" Call  " +"</td></tr>";
		deliveredTR += deliveredTD + "<td id='' class='product_price_td'>&nbsp;</td><td id='' class='product_price_td'>"+" Call"+"</td></tr>";

		//////////////////
		bottomTD+="<td id='' class='price_table_header_td' rowspan='1'>" +" QTY</td>";
		bottomTD+="<td id='' class='price_table_header_td'>"+"       <input name='qty' type='text' value='' size='10'  onkeyup='return selectPriceTD();'/> " +" </td>";
		bottomTD+="<td id='' class='price_table_header_td'>"+"" +" </td>";
		bottomTD+="<td id='' class='price_table_header_td'>"+"Collection" +" </td>";
		bottomTD+="<td id='' class='price_table_header_td'>"+" <input name='delivery_option' type='radio' value='collection' onclick='return selectPriceTD();' />" +" </td>";

		bottomTD+="<td id='' class='price_table_header_td'>"+"Delivered" +" </td>";
		bottomTD+="<td id='' class='price_table_header_td'>"+"<input name='delivery_option' type='radio'  value='delivered' checked='checked' onclick='return selectPriceTD();' />" +" </td>";
		bottomTD+="<td id='' class='price_table_header_td' >&nbsp;</td>";
		bottomTD+="<td id='' class='price_table_header_td' >"+"<input name='cart' class='add_cart' type='button' value='Add To Cart' onclick='return sendToCart(\""+priceCode+"\");' />" +" </td>";
		


		bottomTR+=bottomTD;
		var cartForm="<form name='formSelect' method='post' action='' >";
		var finalTable = "<table width='100%' cellspacing='0' cellpadding='5'>"+
		topTR+collectionTR + deliveredTR+"</table>"+"<br>";
		finalTable += cartForm;		
		finalTable += "<table width='100%' cellspacing='0' cellpadding='5'>";
		finalTable +=bottomTR;
		finalTable += "</table>";
		finalTable += "</form>";
//		$(selectedDivID).update(finalTable);
		document.getElementById('containerDiv').innerHTML = finalTable;

}

function sendToCart(productCode){
	var deliveryOption;
	if (document.formSelect.delivery_option[0].checked){
		deliveryOption="c";
	}else{
		deliveryOption="d";//alert("poiij");
	}
	var qty = document.formSelect.qty.value;
	if(!qty){
		alert('Please insert valid quantity.');
		return false;
	}
	//TODO: Do something to actually send the item to the shopping cart
	var msg=" The javascript function sendToCart() has been ";
	msg+="\n invoked with the following parameters:";
	msg+="\n\n  productCode="+productCode;
	msg+="\n  qty="+qty;
	msg+="\n  deliveryOption="+deliveryOption;
	msg+="\n\n  This function is a placeholder for the actual ";
	msg+="\n  code that will submit this item to the shopping cart. ";
	msg+="\n\n  You can edit this function in \n<?=__FILE__?>";
	//alert(msg);
	//var url ="http://diyinsulation.co.uk/pages/shop.php?s=complete_order&productCode="+productCode+"&qty="+qty+"&deliveryOption="+deliveryOption;
	window.location="http://diyinsulation.co.uk/pages/shop.php?s=complete_order&productCode="+productCode+"&qty="+qty+"&deliveryOption="+deliveryOption;
	//window.open(url, 'newWin');
}
