﻿//*************************************************************************
// 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
// 12/15/2008 Fixed getTableFields to work with FF Post
// 04/21/2009 Fixed getTableFields to work with IE 8 ...
//
//*************************************************************************
// 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) {
				if(navigator.userAgent.indexOf('MSIE') == -1)
				{
					alert('not IE');
			    	document.getElementById("idbrand").innerHTML = xmlHttp.responseText;
			    	alert(xmlHttp.responseText);
			    } else {
			    	alert('Is IE');
			    	var strResponse = '<select name="idbrand" id="idbrand" size="1" >' + xmlHttp.responseText + '</select>';
				    alert(document.getElementById("idbrand").outerHTML);
				    document.getElementById("idbrand").outerHTML = strResponse;
				    alert(strResponse);
				}				    				
			} 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 (document.getElementById("REQidOption"+formElem.name.substr( 11)).value == 'Y') {
					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) {
				if(navigator.userAgent.indexOf('MSIE') == -1)
				{
			    	document.getElementById("tableFields").innerHTML = xmlHttp.responseText;
			    	//alert(xmlHttp.responseText);
			    } else {
			    	var strResponse = '<select name="tableFields" id="tableFields" size="1" >' + xmlHttp.responseText + '</select>';
				    //alert(document.getElementById("tableFields").outerHTML);
				    document.getElementById("tableFields").outerHTML = strResponse;
				    //alert(strResponse);
				}				    	
			} else { 
				alert("Error: " + xmlHttp.status + " " + xmlHttp.responseText );
			}
			
		}
	}		 
