﻿//*************************************************************************
// DO NOT MODIFY THIS SCRIPT IF YOU WANT UPDATES TO WORK!
// Function : AJAX Handlers
// Product  : Shopping Tree Store Administration
// Version  : 3.5
// Modified : Feburary 2007
// Copyright: Copyright (C) 2004 - 2007 Shopping Tree.Com 
//            See "license.txt" for this product for details regarding 
//            licensing, usage, disclaimers, distribution and general 
//            copyright requirements. If you don't have a copy of this 
//            file, you may request one at http://www.candypress.com
//*************************************************************************
//  Date                    Description
// 08/18/2008 Fixed send to work with FF
//
//*************************************************************************
// Global functions
//	
	var xmlHttp;
	
	function createXMLHttpRequest() {
	    if (window.XMLHttpRequest && !(window.ActiveXObject)) {
	        try {
	            xmlHttp = new XMLHttpRequest();
	        }
	        catch (e) {
	            xmlHttp = false;
	        }

	    }
	    else if (window.ActiveXObject) {
	        try {
	            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	        }
	        catch (e) {
	            try {
	                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	            }
	            catch (e) {
	                xmlHttp = false;
	            }
	        }
	    }
	}

// 
// Brand handling functions
//
	function getBrands(id) {
	// AJAX Call to process brands
		if(id.len != 0) {
			createXMLHttpRequest();
			xmlHttp.open('GET', '../AJAX/ajax_getBrands.asp?recid=' + id, true);
			
			xmlHttp.onreadystatechange = displayBrands;
			xmlHttp.send(null);
		}
	}
	
	function displayBrands() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				document.getElementById("showBrands").innerHTML = xmlHttp.responseText;
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}
	

// 
// Affiliate Commission Tier Display
//
	function getTiers(showState,idCust) {
	// AJAX Call to process tiers
		if(showState == '-1') {
			createXMLHttpRequest();
			xmlHttp.onreadystatechange = displayTiers;
			xmlHttp.open("GET","../AJAX/ajax_getTiers.asp?idCust=" + idCust, true);
			xmlHttp.send(null);
		} else {
			document.getElementById("showTiers").innerHTML = '';
		}
			
	}
	
	function displayTiers() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				document.getElementById("showTiers").innerHTML = xmlHttp.responseText;
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}
		

		
		
//
// Option Inventory Handling
//
	function getOptionsInventory(idProduct,theForm) {
		/*var options = getOptions(theForm);
		if (options == '') { 
			document.getElementById("showInventoryMsg").innerHTML = '<b>No options selected!</b><br/>';
			return ;
		}
		alert(options);
		var url = "../AJAX/ajax_optInventory.asp";
		var nvp = "action=get&idproduct=" + idProduct + "&options=" + options;
		createXMLHttpRequest();
		xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        xmlHttp.setRequestHeader("Content-length",nvp.length); 
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange = displayGetOptionsInventory;
		xmlHttp.send(nvp);*/
		//first see if we need to continue
		var theurl = "../AJAX/ajax_optInventorynew.asp?action=checkstock&idProduct=" +idProduct;
		//location.href = theurl;
		var req = new Request({url:theurl, 
				onFailure: function() {
					alert('unable to get option inventory');
					location.href = theurl;
				},
				onSuccess:function(results) {
					if(results == 'yes'){
				
						CheckStockLevel(idProduct);
					}
				}
			});
			
			req.send();
	}
	function CheckStockLevel(idProduct){
		var idOptions='';
	//	alert('sel' + idProduct);
		$$('[class=sel' + idProduct +']').each(function(element) {
		//	alert('yep');
			if(idOptions!=''){
				idOptions+=",";
			}
			idOptions+= element.value;
		});
		//alert('2');
		var theurl = "../AJAX/ajax_optInventorynew.asp?action=CheckStockLevel&idProduct=" +idProduct + "&idOptions=" +idOptions;
	//	alert(theurl);
		var req = new Request({url:theurl, 
				onFailure: function() {
					alert('unable to get stock level');
				},
				onSuccess:function(results) {
					//alert(results);
					if(results == 'out'){
						alert('The Selected Product is out of stock');
						//alert('quantity' + idProduct);
						$('quantity' + idProduct).value = '';
					}
				}
			});
			
			req.send();
	}
	function setOptionsInventory(idProduct,inventory,theForm) {
		var options = getOptions(theForm);
		if (options == '') { 
			document.getElementById("showInventoryMsg").innerHTML = '<b>No options selected!</b><br/>';
			return ;
		}
		var url = "../AJAX/ajax_optInventory.asp?action=set&idproduct=" + idProduct + "&options=" + options + "&inventory=" + inventory;
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = displaySetOptionsInventory;
		xmlHttp.open("get",url);
		xmlHttp.send(null);
	}
	

	function displayGetOptionsInventory() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				if (isNaN(parseInt(xmlHttp.responseText))) {
					document.getElementById("showInventoryMsg").innerHTML = xmlHttp.responseText;
					if (document.getElementById("showInventory").tagName == 'INPUT') {
						document.getElementById("showInventory").value = '';
					} else {
						document.getElementById("showInventory").innerHTML = '0';
					}					
				} else {
					document.getElementById("showInventoryMsg").innerHTML = '';
					if (document.getElementById("showInventory").tagName == 'INPUT') {
						document.getElementById("showInventory").value = xmlHttp.responseText;
					} else { 
						document.getElementById("showInventory").innerHTML = xmlHttp.responseText;
					}
				}
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}
	
	function displaySetOptionsInventory() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				document.getElementById("showInventoryMsg").innerHTML = xmlHttp.responseText;
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}
	

	function getOptions(theForm) {
		var formElem;
		var i,j;
		var iLen;
		options = '';
		for (i=0; i < theForm.elements.length;i++) {
			formElem = theForm.elements[i];
			//alert(formElem.name.substr(0,11));
			if (formElem.name.substr(0,11) == 'OPTidOption') {
				if (formElem.type == 'select-one') {
					options += formElem.options[formElem.selectedIndex].value + ",";
				} else {
					/* assume radio */
				    if(formElem.type == 'radio' && formElem.checked) {
				       options += formElem.value + ',';
				    }
				}
					
			}					
		}
		if(options.length > 0) { options = options.substr(0,options.length - 1); }
		return options;
	}
	
	//
	// Customer Info
	//
	function getCust(el,id) {
	// AJAX Call to process customer info
		if(id.len != 0) {
			createXMLHttpRequest();
			xmlHttp.onreadystatechange = displayCust(el);
			xmlHttp.open("GET","../AJAX/ajax_getCust.asp?recid=" + id, true);
			xmlHttp.send(null);
		}
	}
	
	function displayCust(el) {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				el.innerHTML = xmlHttp.responseText;
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}
	
	// 
	// Database table field selection 
	//
	function getTableFields(table) {
		var url = "../AJAX/ajax_getTableFields.asp";
		var nvp = "table=" + table;
		createXMLHttpRequest();
		xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange = displayGetTableFields;
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        xmlHttp.setRequestHeader("Content-length",nvp.length);
		xmlHttp.send(nvp);
	}
	

	function displayGetTableFields() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
			    document.getElementById("showFields").innerHTML = xmlHttp.responseText;
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}		 
	
	
	function addtoWishlist()
	{
		document.getElementById("cartwishlist").value = 1;
		document.additem.submit();
		
	}
	
	function addtocart()
	{
		document.getElementById("cartwishlist").value = 0;
		document.additem.submit();
		
	}
	
	function CheckStockAndMinOrder(){
		var count = 0;
		var count2 = 0;
		var movenext = false;

		$$('[class=sel]').each(function(element) {
			count = count + 1;
	});
	
		$$('[class=sel]').each(function(element) {
			count2 = count2 + 1;
			if(count == count2){
				movenext = true;
			}
			var tempString = element.get('rel');
			var tempArray = tempString.split("|");
			var idProduct = tempArray[0];
			tempString = ("OPTidOption" + tempArray[1] + "|" + idProduct);
			//alert (tempString)
			var idOptions = document.getElementById(tempString).value;
			var quantity = $('quantity' + idProduct).value;
			//alert(idOptions)
			CheckStockAndMinOrderExecute(idOptions,idProduct,quantity,movenext);	
		});
	}
	var submitform = true;
	var resultsArray;
	
	function CheckStockAndMinOrderExecute(idOptions,idProduct,quantity,movenext){
		var truemovenext=false;
		
			var theurl = "../AJAX/ajax_optInventorynew.asp?action=CheckStockAndMinOrderExecute&idProduct=" +idProduct + "&idOptions=" +idOptions+ "&quantity=" +quantity;
					var req = new Request({url:theurl, 
						onFailure: function() {
							location.href = theurl;
							alert('unable to get stock level' + theurl);
						},
						onSuccess:function(results) {
							if (results.length !=''){
								resultsArray = results.split("-");
								submitform = false;
								//alert('quantity' + idProduct);
								$('quantity' + idProduct).value = resultsArray[1];
							}
							if (movenext && submitform){
		
								$('additem').submit();
							}else if(movenext){
								if (resultsArray[0] =="Q" ){
									alert("Please select an option")
								}else if (resultsArray[0] == "out" && resultsArray[1] != "0") {
									alert("Sorry but we currently only have " + resultsArray[1] + " in stock at this time")
								}
								else if (resultsArray[0] == "out" && resultsArray[1] == "0") {
									alert("Sorry but this item is currently out of stock")
								}
								else if(resultsArray[0] == "qty"){
									alert("Sorry but there is a minimum quantity of " + resultsArray[1] + " for this item")
								}
							}
						}
					});
					
					req.send();
		
		
		
	}
	