﻿//*************************************************************************
// 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 ;
		}
		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);
	}
	
	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(soldat)
	{
	
		var moveon = true;
		if (soldat == "U"){
			moveon = confirm('This product is only available  for our US customers. It will be taken out of your cart during checkout if you are not shipping it to the US. Do you want to continue?');
		}else if (soldat == "C"){
			moveon = confirm('This product is only available  for our Canadian customers. It will be taken out of your cart during checkout if you are not shipping it to the Canadian. Do you want to continue?');
		}
		if(moveon){
			document.getElementById("cartwishlist").value = 0;
			document.additem.submit();
		}

		
	}
	