/**
 * EventLookupUSA
 *
 * By Kramer of SolutionSet.com
 * 070209
 *
 * See LookupBase documentation for more detail.
 *
 **/
var ResellerLookupUSA = LookupBase.extend({
	// Static properties

	settings: function(settings) {
		if (typeof(this.s) == 'undefined') {
			this.base();

			Object.extendProperties(this.s, {
				resellerAttributes: [
					'psp',
					'company',
					'url',
					'city',
					'state',
					'areacode',
					'phone'
				]
			});
			// Import cookie values
			Object.extendProperties(this.URLTokens, {
				cookie_zip: getCookie('ADSKVISZIP')
			});
			if (this.URLTokens.cookie_zip) {
				this.s.formElementValues.zip = this.URLTokens.cookie_zip;
			}
			Object.extendProperties(this.s, settings);
		}
	},

	constructor: function(settings) {
		this.settings(settings);
		this.base();
	},

	ajaxIn: function(req) {
		Element.removeClassName(this.s.n.container, 'resellerLoading');
		this.base(req);
		this.displayItems.clear();
		
		// Process incoming XML
		var xml = req.responseXML;
		var resellers = xml.getElementsByTagName('listing');
		var hasPSP = 0;

		for (var y=0; y<resellers.length&&y<this.displayItemsQtyLimit; y++) {
			
			var reseller = {};
			
			for (var x=0; x<this.s.resellerAttributes.length; x++) {
				
				var nodes = resellers[y].getElementsByTagName(this.s.resellerAttributes[x]);
				reseller[this.s.resellerAttributes[x]] = (nodes.length && this.DOM_getElementText(nodes[0])) ? this.DOM_getElementText(nodes[0]) : '';
				
				if(this.s.resellerAttributes[x] == 'psp' && (this.DOM_getElementText(nodes[0]) != '0') ){
					reseller['psp'] = '<a href="' + this.s.pspLink + '"><img border="0" src="' + this.s.pspImg + '"></a> ';
					hasPSP = 1;
				} else if(this.s.resellerAttributes[x] == 'psp'){
					reseller['psp'] = '';
				}

			}
			this.displayItems.push(reseller);
			
		}

		this.displayItemsQtyOverflow((resellers.length > this.displayItemsQtyLimit));
		this.display(hasPSP);
	},
	
	ajaxOut: function(args) {
		Element.hide(this.s.n.seeMoreResults);
		
		$("resellerPSPfound").style.display = 'none';
			
		if($F("prod") == ""){ 
			this.s.n.results.innerHTML = this.s.noProductsHTML;
			Effect.BlindDown(this.s.n.resultsContainer);
		} else if(!validateZIP2(document.getElementsBySelector('#resellers form')[0].zip.value)){
			this.s.n.results.innerHTML = this.s.zipErrorHTML;
			Effect.BlindDown(this.s.n.resultsContainer);
		} else{
			// record hit with SiteCatalyst
			waLinkClick("right sash","reseller lookup");
			
			Element.addClassName(this.s.n.container, 'resellerLoading');
			new Ajax.Request(this.s.ajaxUrl+'?'+args, {                      // AJAX out to the server using the given args
				method: 'get',
				//parameters: args,
				onSuccess: this.ajaxIn.bind(this)
			});
		}
	},
	
	display: function(hasPSP) {
		
		this.s.n.results.innerHTML = '';
		
		if (this.displayItems.length) {
			for (var x=0; x<this.displayItems.length; x++) {
				this.s.n.results.innerHTML += this.parseTemplate(this.s.displayItemTemplate, this.displayItems[x]);
			}
		} else {
			this.s.n.results.innerHTML = this.s.noResultsHTML;
		}
		
		if(hasPSP){
			$("resellerPSPfound").innerHTML  = '<div style="margin-top: 6px; padding-left:6px; height:23px;">' +
				'<img src="' + this.s.pspImg + '" width="8" height="8"/> ' +
				'<a href="' + this.s.pspLink + '">Premier Solutions Providers</a></div>';
				$("resellerPSPfound").style.display = '';
		}
		
		Effect.BlindDown(this.s.n.resultsContainer);
	},

	DOM_getElementText: function(el) {
		// Seeks out the first text element node contained in el and returns it
		if (typeof(el) != 'undefined' && typeof(el.childNodes) != 'undefined') {
			var nodeTypes = [3,4];                                         // Node types to check for data
			var nodes = {};
			for (var x=0; x<nodeTypes.length; x++) {
				nodes[nodeTypes[x]] = [];
			}
			for (var x=0; x<el.childNodes.length; x++) {
				if (nodeTypes.inArray(el.childNodes[x].nodeType) != -1) {
					nodes[el.childNodes[x].nodeType].push(el.childNodes[x]);
				}
			}
			if (nodes[4].length > 0) return nodes[4][0].nodeValue;
			if (nodes[3].length > 0) return nodes[3][0].nodeValue;
		}
		return false;
	}
});


document.write("<style>/* Reseller Lookup");
document.write("----------------- */");
document.write("#resellers div {");
document.write("	font-weight: normal;");
document.write("}");
document.write("#resellers dt {");
document.write("	font-weight: bold;");
document.write("	padding: 8px 6px;");
document.write("	padding-top: 5px;");
document.write("	padding-bottom: 2px;");
document.write("	margin: 0;");
document.write("}");
document.write("");
document.write("#resellers {");
document.write("	background-color: #ebebeb;");
document.write("	width: 276px;");
document.write("	margin: 0 0 12px 0;");
document.write("	color: #232323;");
document.write("}");
document.write("#resellers * {");
document.write("	font-size: 11px;");
document.write("}");
document.write("#resellers form {");
document.write("	margin: 0;");
document.write("	padding: 0;");
document.write("}");
document.write("#resellers dt {");
document.write("	font-weight: bold;");
document.write("	padding: 8px 6px;");
document.write("	padding-top: 5px;");
document.write("	padding-bottom: 4px;");
document.write("	background-color: #dadada;");
document.write("	margin: 0;");
document.write("}");
document.write("");
document.write("/* Form");
document.write("------------------------------ */");
document.write("#resellers form fieldset {");
document.write("	border: 0;");
document.write("	padding: 3px 6px;");
document.write("	padding-bottom: 1px;");
document.write("	margin-left: 1px;");
document.write("}");
document.write("#resellers form fieldset label {");
document.write("	display: block;");
document.write("	margin-top: 5px;");
document.write("	margin-bottom: 8px;");
document.write("}");
document.write("#resellers form fieldset label.inline {");
document.write("	float: none;");
document.write("	display: inline;");
document.write("	padding-left: 3px;");
document.write("	top: -2px;");
document.write("	position: relative;");
document.write("}");
document.write("#resellers form fieldset select {");
document.write("	width: 263px;");
document.write("	font-size: 100%; /* Force inheritance */");
document.write("	background-color: #f5f5f5;");
document.write("}");
document.write("#resellers form fieldset input {");
document.write("	border: 0;");
document.write("	background-color: #f5f5f5;");
document.write("	padding: 2px 0;");
document.write("}");
document.write("#resellers form fieldset input.submit {");
document.write("	background-color: #979797;");
document.write("	border: 0;");
document.write("	color: #fff;");
document.write("	margin: -19px 1px 5px 0;");
document.write("	height: 18px;");
document.write("	padding: 0 5px 1px 0;");
document.write("}");
document.write("#resellers form fieldset input.submit {");
document.write("	float: right;");
document.write("	width: 60px;");
document.write("	text-align: right;");
document.write("}");
document.write("");
document.write("/* Specific fieldsets");
document.write("----------------------------------- */");
document.write("#resellers form fieldset.product {");
document.write("	margin-bottom: 2px;");
document.write("	margin-left: 0;");
document.write("}");
document.write("#resellers form fieldset.minor {");
document.write("	margin-bottom: 1px;");
document.write("}");
document.write("#resellers form fieldset.minor input,");
document.write("#resellers form fieldset.minor select {");
document.write("	width: 75px;");
document.write("}");
document.write("#resellers form fieldset.Search {");
//document.write("	padding-top: 9px;");
document.write("	background-color: #ebebeb;");
document.write("}");
document.write("#resellers form fieldset p.resellerLoading {");
document.write("	display: none;");
document.write("	float: left;");
document.write("	padding-right: 13px;");

document.write("	background: url(\"http://images.autodesk.com/adsk/s930/images/loadingDots.gif\") bottom right no-repeat;");
document.write("}");
document.write("#resellers.resellerLoading form fieldset p.resellerLoading {");
document.write("	display: block;");
document.write("	margin-top: 6px;");
document.write("}");
document.write("");
document.write("/* Lookup Results");
document.write("----------------------------------- */");
document.write("#resellers dd dl.resellerResultsContainer dt {");
document.write("	background-color: #ebebeb;");
document.write("	padding: 5px 6px;");
document.write("}");
document.write("#resellers dd dl.resellerResultsContainer dd p.resellerSeeMore {");
document.write("	background-color: #dadada;");
document.write("	height: 23px;");
document.write("	line-height: 23px;");
document.write("	padding: 0 0 0 9px;");
document.write("	margin: 0;");
document.write("	xborder: 1px solid blue;");
document.write("}");
document.write("#resellers dd dl.resellerResultsContainer dd ul.resellerResults {");
document.write("	background-color: #ebebeb;");
document.write("	list-style-type: none;");
document.write("	padding: 0 6px;");
document.write("}");
document.write("#resellers dd dl.resellerResultsContainer dd ul.resellerResults li {");
document.write("	border-top: 1px solid #CCCCCC;");
document.write("	margin: 0;");
document.write("	padding: 5px 0 5px 0px;");
document.write("	zoom: 1; /* MSIE bugfix */");
document.write("}");
document.write("</style>");


