<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs directory_title="Volusion Order Notifications" title="Volusion Orders For __UP_domain__" title_url="http://www.brandlabs.us/Volusion-Applications-iGoogle-Gadget.html?utm_source=google&amp;utm_medium=igoogle&amp;utm_campaign=volusion_order_notifications"
		width="365" height="208" scaling="false" scrolling="false" singleton="false"
		author="Brand Labs" author_email="it@brandlabs.us" author_affiliation="Brand Labs" author_link="http://www.brandlabs.us/" author_location="Rochester, MI, USA" 
		description="This Widget updates every 20 minutes to keep up to date information on order count and revenue for your Volution stores.  The most important  business information right at your finger tips.  The Volusion Order Notifications can display multiple Volusion stores on one dashboard, which means no more flipping back and forth between dashboards to get the information you need!"
		screenshot="http://www.brandlabs.us/downloads/widgets/igoogle/images/screenshot.png" thumbnail="http://www.brandlabs.us/downloads/widgets/igoogle/images/thumbnail.png">
		<Require feature="minimessage"/>
		<Require feature="analytics"/>
		<Locale lang="en" country="us"/>
	</ModulePrefs>

	<UserPref name="domain" datatype="string" display_name="Domain Name" required="true"/>
	<UserPref name="user" datatype="string" display_name="User Name" required="true"/>
	<UserPref name="password" datatype="string" display_name="Password" required="true"/>
	<!--
	<UserPref name="currency" datatype="enum" display_name="Currency Type" default_value="$" required="true">
		<EnumValue value="$" display_value="U.S. Dollar"/>
	</UserPref>
	-->
	<UserPref name="ssl" datatype="bool" display_name="SSL" required="true" default_value="true"/>
 
	<Content type="html">
	<![CDATA[   
<script type="text/javascript">
/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/
function BrandLabsWidget__MODULE_ID__() {
	this.moduleId = __MODULE_ID__;
	this.configured = false;
	this.ssl = true;
	this.domain = null;
	this.user = null;
	this.password = null;
	this.currency = '$';
	
	this.updater = null;
	this.updateTime = 1200 /*seconds*/;
	this.lastUpdated = null;
	this.dom = null;
	this.totalOrders = null;
	this.totalRevenue = null;
	
	this.isConfigured = function() {
		if(this.domain == null || this.user == null || this.password == null) {
			return false;
		}
		return true;
	};
	
	this.loadConfiguration = function() {
        var prefs = new _IG_Prefs(this.moduleId);
		this.domain = prefs.getString('domain');
		if(this.domain == '' || this.domain == undefined) {this.domain = null;}
		this.user = prefs.getString('user');
		if(this.user == '' || this.user == undefined) {this.user = null;}
		this.password = prefs.getString('password');
		if(this.password == '' || this.password == undefined) {this.password = null;}
		this.ssl = prefs.getBool('ssl');
	};
	
	this.setup = function() {
		//Load configuration
		this.loadConfiguration();
		this.configured = this.isConfigured();
		
		//Start up widget
        this.widgetOnShow();  
	};
	
	this.createUpdater = function() {
		if(this.updater != null) {
			window.clearInterval(this.updater);
		}
		//Variable is externally named!
		this.updater = window.setInterval('brandLabsWidget' + this.moduleId + '.execute()', (this.updateTime * 1000)); /*msecs*/
	};
		
	this.widgetOnShow = function() {
		//Schedule updating
		this.createUpdater();
		
		//Check the last time we have updated
		if(this.lastUpdated != null) {
			var currentDate = new Date();			
			var difference = (currentDate - this.lastUpdated)
			if(difference >= (this.updateTime * 1000 /*msecs/sec*/)) {				
				this.execute();
			}
		}
		else {			
			//No last update date, do it now
			this.execute();
		}		
	};
	
    this.showError = function(errorText) {		
    	var msg = new _IG_MiniMessage(this.moduleId);	
        msg.createDismissibleMessage(errorText);                
    };
						
	this.receive = function(response) {
		var localThis = brandLabsWidget__MODULE_ID__;

		try {
			//Check for data back
			if(response == null || typeof(response) != 'object' || response.firstChild == null) {
				localThis.showError('Invalid response from server, check configuration.');
				return;
	        }
			
			localThis.dom = response;
			
			//Check for the main element, this is required
			if(localThis.dom.getElementsByTagName('xmldata') == null 
				|| localThis.dom.getElementsByTagName('xmldata').length < 1) {
				localThis.showError('Invalid response from server, check configuration.');
				return;
			}
		}
		catch(e) {
			localThis.showError('Invalid response from server, check configuration: ' + e.message);
			return;
		}
		
		//Filter the results
		try {
			localThis.filter();						
		}
		catch(e) {
			localThis.showError('Could not filter orders, check configuration: ' + e.message);
			return;
		}
		
		//Calculate the totals
		try {
			localThis.calculate();
		}
		catch(e) {
			localThis.showError('Could not calculate totals, check configuration: ' + e.message);
			return;
		}
		
		//Update the calculated display
		try {				
			localThis.updateDisplay();											
		}
		catch(e) {
			localThis.showError('Unable to display data: ' + e.message);
			return;
		}
	};
	
	this.filter = function() {
		var index;
		var node = null;
		var removeOrders = new Array();
		var orderChildren = null;
		var orderDate = null;
		var root = this.dom.getElementsByTagName('xmldata');
		
		orders = this.dom.getElementsByTagName('Orders');

		for(index = 0; index < orders.length; index++) {
			orderChildren = orders[index].childNodes;

			for(index2 = 0; index2 < orderChildren.length; index2++) {
				if(orderChildren[index2].nodeName == 'OrderDate') {
					orderDate = new Date();
					orderDate.setTime(Date.parse(orderChildren[index2].childNodes[0].nodeValue));
	
					if(!this.isToday(orderDate)) {									
						//Put into an array
						removeOrders.push(orders[index]);									
					}
				}							
			}
		}
		
		//Remove the queued		
		for(index = 0; index < removeOrders.length; index++) {
			node = removeOrders[index];
			root[0].removeChild(node);
		}
	};
	
	this.isToday = function(date) {
		var currentDate = new Date();

		if(currentDate.getDate() == date.getDate() 
			&& currentDate.getMonth() == date.getMonth()
			&& currentDate.getFullYear() == date.getFullYear()) {
			return true;
		}
		
		return false;
	};
	
	this.calculate = function() {
		this.totalOrders = 0;
		this.totalRevenue = 0;
		
		orders = this.dom.getElementsByTagName('Orders');
		this.totalOrders = orders.length;

		//Total orders
		for(index = 0; index < orders.length; index++) {
			orderChildren = orders[index].childNodes;

			for(index2 = 0; index2 < orderChildren.length; index2++) {
				if(orderChildren[index2].nodeName == 'PaymentAmount') {
					this.totalRevenue = this.totalRevenue 
						+ new Number(orderChildren[index2].childNodes[0].nodeValue).valueOf();
				}							
			}
		}					
	};
	
	this.updateDisplay = function() {
		var localTotalOrders = '-';
		var localTotalRevenue = '-';
		var dateForDisplay = new Date();
		if(this.totalOrders != null) {
			localTotalOrders = this.addCommas(new String(Math.ceil(this.totalOrders)));
		}
		if(this.totalRevenue != null) {
			localTotalRevenue = this.addCommas(new String(Math.ceil(this.totalRevenue)));
		}
		
		_gel('total_orders' + this.moduleId).innerHTML = localTotalOrders;
		_gel('total_revenue' + this.moduleId).innerHTML = localTotalRevenue;
		
		//Current Date
		if(this.lastUpdated != null) {
			dateForDisplay = this.lastUpdated;
		}
		_gel('date' + this.moduleId).innerHTML = this.getMonthName(dateForDisplay.getMonth()+1) 
			+ '&nbsp;' + dateForDisplay.getDate() + ',&nbsp;'
			+ '<span class="date_year' + this.moduleId + '" style="color: #283E53;">' + dateForDisplay.getFullYear() + '</span>';
	};
	
	this.getMonthName = function(month) {
		switch(month) {
			case 1: return 'January'; break;
			case 2: return 'February'; break;
			case 3: return 'March'; break;
			case 4: return 'April'; break;
			case 5: return 'May'; break;
			case 6: return 'June'; break;
			case 7: return 'July'; break;
			case 8: return 'August'; break;
			case 9: return 'September'; break;
			case 10: return 'October'; break;
			case 11: return 'November'; break;
			case 12: return 'December'; break;
			default: return '';
		}
	};
	
	this.execute = function() {
		var url;
		
		//Update the time executed
		this.lastUpdated = new Date();
		
		//Clear display
		this.totalOrders = null;
		this.totalRevenue = null;
		this.updateDisplay();

		//Create URL
		if(this.ssl) {
			url = "https://";
		}
		else {
			url = "http://";
		}
		url = url + this.domain;
		url = url + "/net/WebService.aspx?Login=" + escape(this.user);
		url = url + "&EncryptedPassword=" + escape(this.password);
		url = url + "&API_Name=Generic\Orders&SELECT_Columns=o.OrderID,o.OrderDate,o.PaymentAmount&WHERE_Column=o.Locked&WHERE_Value=Y";

		//Send request for data		
        _IG_FetchXmlContent(url, this.receive, {refreshInterval: 1200}); 	
	};
	
	/*--------------------------------------------------------------------------*
	 * Disclaimers
	 * The content of mredkj.com (information, code, etc.) is provided as is, and is without warranty of any kind.
	 *
	 * Copyright
	 * mredkj.com articles are copyrighted, and may not be reproduced without permission. Code marked as public domain is without copyright, and can be used without restriction.
	 *	
	 *--------------------------------------------------------------------------*/
	this.addCommas = function(nStr)	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	};
	/*--------------------------------------------------------------------------*/
};			

//Create an instance
var brandLabsWidget__MODULE_ID__ = new BrandLabsWidget__MODULE_ID__();

//Handle on load
_IG_RegisterOnloadHandler(function() {
	brandLabsWidget__MODULE_ID__.setup();
});

//Track analytics
_IG_Analytics("UA-3031858-1", "/igoogle");
</script>
<style type="text/css">
/*Front*/		
#front__MODULE_ID__ {
	width: 365px;
	height: 208px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;

	color: #FFFFFF;
	font-family: "Helvetica";	
}

#date_wrapper__MODULE_ID__ {
	width: 365px;
	height: 38px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;
	
	background-image: url(http://www.brandlabs.us/downloads/widgets/igoogle/images/background_date.gif);
	background-repeat: no-repeat;
	background-position: top left;
	background-color: transparent;	
}

#date__MODULE_ID__ {
	margin: 0px 0px 0px 0px;
	padding-bottom: 0px;
	padding-right: 0px;
	padding-top: 16px;
	padding-left: 32px;
	
	color: #FFFFFF;
	font: bold 16px "Helvetica";
	line-height: 1.0;
}

.date_year__MODULE_ID__ {
	color: #283E53;
}

#total_revenue_currency_wrapper__MODULE_ID__ {
	width: 365px;
	height: 61px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;
	
	background-image: url(http://www.brandlabs.us/downloads/widgets/igoogle/images/background_currency.gif);
	background-repeat: no-repeat;
	background-position: top left;
	background-color: transparent;	
}

#total_revenue_currency__MODULE_ID__ {
	margin: 0px 0px 0px 0px;
	padding-bottom: 0px;
	padding-right: 0px;
	padding-left: 301px;
	padding-top: 15px;

	color: #FFFFFF;	
	font: bold 25px "Helvetica";
	line-height: 1.0;
}

#totals__MODULE_ID__ {
	width: 365px;
	height: 59px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;
		
	background-image: url(http://www.brandlabs.us/downloads/widgets/igoogle/images/background_totals.gif);
	background-repeat: no-repeat;
	background-position: top left;
	background-color: transparent;
}

#total_orders_wrapper__MODULE_ID__ {
	float: left;
	width: 159px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;	
}

#total_orders__MODULE_ID__ {
	margin: 0px 0px 0px 0px;
	padding-bottom: 0px;
	padding-right: 0px;	
	padding-left: 23px;		
	
	text-align: center;	
	color: #FFFFFF;
	font: normal 48px "Helvetica";
	line-height: 1.0;
}

#total_revenue_wrapper__MODULE_ID__ {
	float: left;
	width: 190px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;	
}

#total_revenue__MODULE_ID__ {
	margin: 0px 0px 0px 0px;
	padding-bottom: 0px;
	padding-right: 0px;	
	padding-left: 9px; 
	
	text-align: center;	
	color: #FFFFFF;
	font: lighter 48px "Helvetica";
	line-height: 1.0;
}

.cancel_float__MODULE_ID__ {
	clear: both;
	font-size: 1px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;
}

#brand_labs_wrapper__MODULE_ID__ {
	width: 365px;
	height: 50px;

	background-image: url(http://www.brandlabs.us/downloads/widgets/igoogle/images/background_brand_labs.gif);
	background-repeat: no-repeat;
	background-position: top left;
	background-color: transparent;	
}

#brand_labs__MODULE_ID__ {
	margin: 0px 0px 0px 0px;
	padding: 7px 0px 0px 26px;
}

#brand_labs__MODULE_ID__ img {
	background-color: transparent;
	border-color: #FFFFFF #FFFFFF #FFFFFF #FFFFFF;
	border-style: none none none none;
	border-width: 0px 0px 0px 0px;
	margin: 0px 0px 0px 0px;
	padding: 0px 0px 0px 0px;
	text-align: left;
	vertical-align: top;
	z-index: 0;
}
/**/
</style>
<div id="front__MODULE_ID__">
	<div id="date_wrapper__MODULE_ID__"><div id="date__MODULE_ID__">January 1, <span class="date_year__MODULE_ID__">1970</span></div></div>
	<div id="total_revenue_currency_wrapper__MODULE_ID__"><div id="total_revenue_currency__MODULE_ID__">($)</div></div>
	<div id="totals__MODULE_ID__">
		<div id="total_orders_wrapper__MODULE_ID__"><div id="total_orders__MODULE_ID__">-</div></div>
		<div id="total_revenue_wrapper__MODULE_ID__"><div id="total_revenue__MODULE_ID__">-</div></div>
		<div class="cancel_float__MODULE_ID__">&nbsp;</div>
	</div>
	<div id="brand_labs_wrapper__MODULE_ID__"><div id="brand_labs__MODULE_ID__"><a href="http://www.brandlabs.us/?utm_source=google&utm_medium=igoogle&utm_campaign=volusion_order_notifications" target="_blank" title="Powered by Brand Labs"><img src="http://www.brandlabs.us/downloads/widgets/igoogle/images/brand_labs.gif" alt="Powered by Brand Labs" title="Powered by Brand Labs"/></a></div></div>
</div>           
	]]>   
	</Content>
</Module>

