﻿//*************************************************************************
// DO NOT MODIFY THIS SCRIPT IF YOU WANT UPDATES TO WORK!
// Function : AJAX Handlers
// Product  : Candypress Store Administration
// Version  : 6.2.0.0
// Modified : Feburary 2007
// Copyright: Copyright (C) 2010 Cavallo Communications, LLC.
//            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 ...
// 08/04/2009 Removed extraous alert statements
//*************************************************************************
// 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)
				{
			    	document.getElementById("idbrand").innerHTML = xmlHttp.responseText;
			    } else {
			    	var strResponse = '<select name="idbrand" id="idbrand" size="1" >' + xmlHttp.responseText + '</select>';
				    document.getElementById("idbrand").outerHTML = 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 );
			}
			
		}
	}		 
	
 /*

/*
quickTree 0.4 - Simple jQuery plugin to create tree-structure navigation from an unordered list
http://scottdarby.com/

Copyright (c) 2009 Scott Darby

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/
jQuery.fn.quickTree=function(){return this.each(function(){var a=$(this);var b=a.find("li");a.find("li:last-child").addClass("last");a.addClass("tree");a.find("ul").hide();b.each(function(){if($(this).children("ul").length>0){$(this).addClass("root").prepend('<span class="expand" />')}});$("span.expand").toggle(function(){$(this).toggleClass("contract").nextAll("ul").slideDown()},function(){$(this).toggleClass("contract").nextAll("ul").slideUp()})})};


/*
 * tweetable 1.6 - jQuery twitter feed generator plugin
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * With modifications from Philipp Robbel (http://www.robbel.com/) and Patrick DW (stackoverflow)
 * for IE compatibility.
 *
 * Revision: $Id: jquery.tweetable.js 2011-01-06 $ 
 *
 */
(function ($) {
    //define the tweetable plugin
    $.fn.tweetable = function (options) {
        //specify the plugins defauls
        var defaults = {
            limit: 5, 						//number of tweets to show
            username: 'allnewcomics', 	//@username tweets to display
            time: false, 					//display date
            replies: false,				//filter out @replys
            position: 'append'			//append position
        };
        //overwrite the defaults
        var options = $.extend(defaults, options);
		//loop through each instance
        return this.each(function (options) {
			//assign our initial vars
            var act = $(this);
            var $tweetList;
            var tweetMonth = '';
            var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
            var api = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
            var count = "&count=";
            //do a JSON request to twitters API
            $.getJSON(api + defaults.username + count + defaults.limit + "&callback=?", act, function (data) {
				//loop through twitters response
                $.each(data, function (i, item) {
					//check for the first loop
                    if (i == 0) {
                    	//create an unordered list to store tweets in
                        $tweetList = $('<ul class="tweetList">')[defaults.position.toLowerCase() + 'To'](act);
                    }
                    //handle @reply filtering if required
                    if (defaults.replies === false) {
                        if (item.in_reply_to_status_id === null) {
                            $tweetList.append('<li class="tweet_content_' + i + '"><p class="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2')+'</p></li>');
                        }
                    } else {
                        $tweetList.append('<li class="tweet_content_' + i + '"><p class="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2') + '</p></li>');
                    }
                    //display the tiem of tweet if required
                    if (defaults.time == true) {
                    	for(var iterate=0; iterate<=12; iterate++) {
                    		if(shortMonths[iterate] == item.created_at.substr(4, 3)) {
								tweetMonth = iterate + 1;
								if(tweetMonth < 10) {
									tweetMonth = '0' + tweetMonth;
								}
	                   		} 	
                    	}
                        $('.tweet_link_' + i).append('<small> ' + item.created_at.substr(8, 2) + '/' + tweetMonth + '/' + item.created_at.substr(26,4) + ' ' + item.created_at.substr(11,8) + '</small>');
                    }
                });
                //close the unordered list
               });
        });
    }
})(jQuery);
