function ajaxRead(file, type){ 		
	var xmlObj = null;  		

 	if(window.XMLHttpRequest){ 
    	xmlObj = new XMLHttpRequest(); 
 	} 
 	else if(window.ActiveXObject){ 
    	xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
 	} 
 	else { 
    	return; 
 	}
 	
 	if( type == 'shipping' ) {
    	document.getElementById( 'shippingLoadingArea' ).innerHTML = '<img src=\'images_new/loading.gif\' style=\"vertical-align: absmiddle;\"> <span class=\"co_h\" style=\"vertical-align: middle;\">Please wait ... searching for best shipping rates</span>';
    }
 	else if( type == 'checkAccount' ) {
	 	document.getElementById( 'checkAccount' ).innerHTML = '<img src=\'images_new/loading.gif\'>';
 	}
 
 	xmlObj.onreadystatechange = function(){ 
  	
  		if(xmlObj.readyState == 4){
  			if( type == 'adBar') {
    			processAdBarXML(xmlObj.responseXML);
  			}
  			else if( type == 'login' ) {
  				processLogin(xmlObj.responseXML);
  			}
  			else if( type == 'shipping' ) {			
	  			document.getElementById( 'shippingLoadingArea' ).innerHTML = '';  	
  				processShipping(xmlObj.responseXML);		  				  						
  			}
  			else if( type == 'logAdBar' ) {      				 
  			}
  			/* November 30, 2007 WSH // ADDED CHECK FOR EXISTING ACCOUNT */
  			else if( type == 'checkAccount' ) {
	  			document.getElementById( 'checkAccount' ).innerHTML = '';  	  				  				
  				existingAccount( xmlObj.responseXML );
  			}
  			// July 16, 2008 WSH // MOVED CONFIGURATOR JS HANDLING TO HERE FOR UNIFICATION
  			else if( type == 'configurator' ) { 
  				var configArray = processListingXMLData( xmlObj.responseXML );
  				updateDisplay( configArray );
  				//changeLoading("");
  				updateConfDesc();
  				updateConfiguratorPricing();
  			}
  			// June 2, 2009 WSH // GREENWICH // MADE OLD STYLE AND NEW DROPDOWN STYLE CONFIGURATOR ABLE TO COEXIST
  			else if( type == 'configurator_dropdown' ) { 

  				var configArray = processListingXMLData( xmlObj.responseXML, true );

  				updateDisplay( configArray );
  				
  				updateConfDesc( configArray );
  				updateConfiguratorPricing();
  			}
  			// July 29, 2008 WSH // ADDED NEW MODE FOR SHIPPING ESTIMATE AT CART
  			else if( type == 'shipping_estimate' ) {  			
  				var shippingArray = processShippingEstimate( xmlObj.responseXML );
  				updateDisplay( shippingArray );
  						
  			}
  			else {
      			processXML(xmlObj.responseXML);      			
      		}
  		}  		 
  		// July 16, 2008 WSH // MOVED CONFIGURATOR JS HANDLING TO HERE FOR UNIFICATION
  		else if( xmlObj.readyState == 1 ) {
  			if( type == 'configurator' ) {
  				//changeLoading("<table width='100%' bgcolor='#099000'><tr><td style='color: #FFFFFF ;'>searching...</td></tr></table>");
  			}
  			else if( type == 'shipping' ) {
  				document.getElementById( 'address_next_step_1' ).style.display = 'none'; 
    			document.getElementById( 'address_next_step_2' ).style.display = 'none';
		    }
  			else if( type == 'shipping_estimate' ) {  			  				
  				var loading = new Array();
  				loading['shipping_options'] = "<img src='images_new/loading.gif' border='0' align='absmiddle'>Please wait... searching for best shipping rates";
  				
  				updateDisplay( loading );			
  			}
  		}
    } 
    	
   	xmlObj.open ('GET', file, true);
	xmlObj.send (''); 		
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function processListingXMLData( obj, isDropdown ) {
	var dataArray = obj.getElementsByTagName('products')[0].childNodes;
	var dataArrayLen = dataArray.length;
	var configureArray = new Array();
	
	if( dataArrayLen > 0 ) {
		var currentConfig;
	
	// May 27, 2009 WSH // GREENWICH // NO OTHER REFERENCE TO CURRENTCONFIG IN THIS FUNCTION, BUT IT IS CAUSING ISSUES WITH THE NEW STYLE OF CONFIGURATOR, SO IT'S COMMENTED OUT NOW
	/*	for( var i = 0; i < fieldsArray.length; i++ ) {
			currentConfig += document.getElementById( fieldsArray[i] ).value;			
		}*/
		
		// December 4, 2008 WSH // EMU // ADDED LIST PRICE FOR SAVINGS CALCULATION
		for( var i = 0; i < dataArrayLen; i++ ) {
			if( dataArray[i].tagName && dataArray[i].getAttribute( 'products_id' ) ) {			
				var dweb_price = unescape(dataArray[i].getAttribute('products_price_no_tags')).replace(/\+/g, " ") + ' ';
				var products_name = unescape(dataArray[i].getAttribute('products_name')).replace(/\+/g, " ") + ' ';
				var products_model = unescape(dataArray[i].getAttribute('products_model')).replace(/\+/g, " ") + ' ';
				var products_description = unescape(dataArray[i].getAttribute('products_description')).replace(/\+/g, " ").replace(/<br>/g, "\n\n");
				var products_weight = unescape(dataArray[i].getAttribute('products_weight')).replace(/\+/g, " ");
				var price_1 = unescape(dataArray[i].getAttribute('usd_price')).replace(/\+/g, " ");
				var price_3 = unescape(dataArray[i].getAttribute('cad_price')).replace(/\+/g, " ");
				var list_1 = unescape(dataArray[i].getAttribute('usd_list')).replace(/\+/g, " ");
				var list_3 = unescape(dataArray[i].getAttribute('cad_list')).replace(/\+/g, " ");
				var cost_1 = unescape(dataArray[i].getAttribute('usd_cost')).replace(/\+/g, " ");
				var cost_3 = unescape(dataArray[i].getAttribute('cad_cost')).replace(/\+/g, " ");
				var currency = unescape(dataArray[i].getAttribute('currency')).replace(/\+/g, " ");
				
				// May 27, 2009 WSH // GREENWICH // ADDED STOCK CHECKING FOR CONFIGURATED PRODUCT
				var foreign_stock = unescape(dataArray[i].getAttribute('foreign_stock')).replace(/\+/g, " ");
			}
		}
		
		var currencies_id = 1;
		if( currency == "CAD" ) {
			currencies_id = 3;
		}
		else {
			currencies_id = 1;
		}
			
		configureArray['conf_sum_desc'] = products_description;						
		
		// November 6, 2009 WSH // HOMO // NOW NEED TO SUPPORT THE CROSS OUT PRICING DISPLAY FOR SALE ITEMS
		//configureArray['conf_sum_pr'] = formatPrice( dweb_price );						
		eval( "configureArray['conf_sum_pr'] = formatPrice( list_" + currencies_id + " )" );				
		eval( "configureArray['sale_price_display'] = formatPrice( price_" + currencies_id + " )" );
		eval( "configureArray['savings_percentage'] = Math.round( ( ( list_" + currencies_id + " - price_" + currencies_id + " ) / list_" + currencies_id + " * 100 ) )" );
		
		configureArray['conf_products_price'] = dweb_price;
		configureArray['conf_price_1'] = price_1;				
		configureArray['conf_price_3'] = price_3;				
		configureArray['conf_list_price_1'] = list_1;				
		configureArray['conf_list_price_3'] = list_3;				

		configureArray['conf_buy_name'] = products_name;				
		configureArray['conf_buy_model'] = products_model;				
		configureArray['conf_buy_description'] = products_model;

		configureArray['conf_buy_weight'] = products_weight;				
		configureArray['conf_buy_price_1'] = price_1;
		configureArray['conf_buy_price_3'] = price_3;
		configureArray['conf_buy_list_1'] = list_1;
		configureArray['conf_buy_list_3'] = list_3;
		configureArray['conf_buy_cost_1'] = cost_1; // April 6, 2009 WSH // GREENWICH // COST NOW SET PROPERLY TO COST
		configureArray['conf_buy_cost_3'] = cost_3;
		
		// April 6, 2009 WSH // GREENWICH // ADDED UPDATE TO CRM'S CONFIG
		configureArray['price_1'] = price_1;
		configureArray['price_3'] = price_3;
		configureArray['list_1'] = list_1;
		configureArray['list_3'] = list_3;
		configureArray['cost_1'] = cost_1;
		configureArray['cost_3'] = cost_3;
		
		try {
			configureArray['conf_buy_configuration'] = document.getElementById( "conf_all_conditions" ).value;
		}
		catch( e ) {
			configureArray['conf_buy_configuration'] = products_model;
		}
		
		if( !( typeof(document.getElementById( "conf_quantity" ).value) === "undefined"  ) ) {		
			configureArray['conf_buy_quantity'] = document.getElementById( "conf_quantity" ).value;			
		}

		// June 2, 2009 WSH // GREENWICH // MADE OLD AND NEW DROPDOWN STYLE CONFIGURATOR ABLE TO COEXIST
		// May 27, 2009 WSH // GREENWICH // NOW UPDATIG THE MODEL ON SCREEN
		if( isDropdown ) {
			configureArray['confBox'] = products_model;				
			configureArray['configurator_model'] = products_model;		
			configureArray['conf_part_number'] = products_model;		
		}
		
		if( foreign_stock > 0 ) {				
			configureArray['conf_stock'] = foreign_stock;
		}
		else {
			configureArray['conf_stock'] = "Call for Availability";		
		}
	}

	return( configureArray );
}

function updateConfiguratorPricing() {
	var currentValue = document.getElementById( 'conf_quantity' ).value;
	var subTotal = currentValue * document.getElementById( 'conf_products_price' ).value;
	var inputArray = new Array();
	
	inputArray['conf_sum_qt'] = currentValue;
	inputArray['conf_sum_sub'] = formatPrice( subTotal );
	
	if( !( typeof(document.getElementById( "conf_quantity" ).value) === "undefined"  ) ) {		
		inputArray['conf_buy_quantity'] = currentValue;
	}
	
	updateDisplay( inputArray );
	updateConfDesc();
}

function updateDisplay( inputArray ) {
	// WSH // INPUTARRAY SHOULD BE UPDATE KEY TO VALUE, DEPENDING IF ELEMENT IS INPUT, OR DIV OR SOMETHING, CHANGE VALUE OR INNERHTML
	for( var key in inputArray ) {
		try {
			var tempElement = document.getElementById( key );

			if( tempElement.tagName.toUpperCase() == "INPUT" ) {
				tempElement.value = inputArray[key];
			}
			else {
				// March 15, 2010 WSH // IF CONFIGURED STOCK, AND IT'S A NUMBER, THEN SHOW SHIP DATES
				if( key == "conf_stock" ) {
					if( IsNumeric( inputArray[key] ) ) {					
						$('#ship_dates').show();
						$('.number_in_stock').html( inputArray[key] );
					}
					else {
						$('#ship_dates').hide();
					}
				}
				tempElement.innerHTML = inputArray[key];				
			}
			//alert( "setting " + key + " to " + inputArray[key] );
		}
		catch( e ) {
			//alert( e );
		}		
	}	
}

function formatPrice( price ) {
	var priceOutput = "";
	priceOutput += "$";
	
	var formattedPrice = new NumberFormat( price );
	formattedPrice.setPlaces( 2 );
	formattedPrice.setSeparators( false );
	
	priceOutput += formattedPrice.toFormatted();
	
	return( priceOutput );
}

function getConfiguration( current_folder ) {
	$( "#temp_configuration" ).val( "" );

	$( ".configurator_option" ).each( function() {
			var tempOption;
			var tempValue;

			tempOption = $( this ).attr( "id" ).substring(7) + "aaa";
			tempValue = $( this ).val();

			tempConfig = $( "#temp_configuration" ).val();

			$( "#temp_configuration" ).val( tempConfig + tempOption + tempValue + "bbb" );
		}
	);

	var productsID = $( "#configurator_products_id" ).val();
	var currenciesID = $( "#configurator_currencies_id" ).val();

	$( "#conf_all_conditions" ).val( $( "#temp_configuration" ).val() );

	var return2 = ajaxRead( current_folder + "/product_suggestor_results.php?website_id=3&class=osc&products_id=" + productsID + "&currencies_id=" + currenciesID + "&conditions=" + $( "#temp_configuration" ).val(), "configurator_dropdown" );	
}

function getShippingEstimate() {

	var postCode = document.getElementById('shipping_postcode').value;				 
	//var zoneId = document.getElementById('shipping_zone_id').value;
	//var state = document.getElementById('shipping_state_us').value;
	var countryId = document.getElementById('shipping_country_id').value;
	//var company = document.getElementById('shipping_company').value;
	var zoneId = '';
	var state = '';
	var company = '';

	if( postCode != '' && countryId != ''  ) {
		ajaxRead('shipping_ajax.php?post_code=' + postCode + '&zone_id=' + zoneId + '&state=' + state + '&country_id=' + countryId + '&company=' + company, 'shipping_estimate');
	}
	else {
		var shippingArray = new Array();
		shippingArray['shipping_options'] = "A country must be selected and zip/postal code must be entered in order to estimate a shipping rate.";
		updateDisplay( shippingArray );
	}
}

function processShippingEstimate( obj ) {
	var dataArray = obj.getElementsByTagName('pets')[0].childNodes;
	var dataArrayLen = dataArray.length;
	var shippingArray = new Array();
	shippingArray['shipping_options'] = "";

	try {
		if( dataArrayLen > 0 ) {		
			shippingArray['shipping_options'] += "<table cellpadding='0' cellspacing='0' class='se'>\n";
			for( var i = 0; i < dataArrayLen; i++ ) {
				if( dataArray[i].tagName ) {
					var id = unescape(dataArray[i].getAttribute('id')).replace(/%20/g, ' ').replace(/\+/g, " ");
					var module = unescape(dataArray[i].getAttribute('name')).replace(/%20/g, ' ').replace(/\+/g, " ");
					var method = unescape(dataArray[i].getAttribute('title')).replace(/%20/g, ' ').replace(/\+/g, " ");
					var cost = unescape(dataArray[i].getAttribute('cost')).replace(/%20/g, ' ').replace(/\+/g, " ");
					var tax_rate = unescape(dataArray[i].getAttribute('tax_rate')).replace(/%20/g, ' ').replace(/\+/g, " ");
					var tax_description = unescape(dataArray[i].getAttribute('tax_description')).replace(/%20/g, ' ').replace(/\+/g, " ");
					
					if( method != "null" )  {
						shippingArray['shipping_options'] += "<tr>\n";
						shippingArray['shipping_options'] += "<td class='se_sm'>\n";
						shippingArray['shipping_options'] += method;
						shippingArray['shipping_options'] += "</td>\n";	
						shippingArray['shipping_options'] += "<td class='se_sc'>\n";
						shippingArray['shipping_options'] += formatPrice( cost );
						shippingArray['shipping_options'] += "</td>\n";					
						shippingArray['shipping_options'] += "</tr>\n";
					}
				}
			}
			shippingArray['shipping_options'] += "</table>\n";
		}
	}
	catch( e ) {
		shippingArray['shipping_options'] = "Sorry, failed to retrieve a rate from UPS. Please call us toll free at 1-877-766-5412.";
	}
	
	return( shippingArray );
}

function updateConfDesc(theArray) {
	
	for(var theTarget in theArray) {
		/* alert('Setting target ' + theTarget + ' to value ' + theArray[theTarget]) */
		try {
			document.getElementById(theTarget).innerHTML = theArray[theTarget];		
		}
		catch( e ) {
			;
		}
	}

	//colorFadeAndBack()
}

function colorFadeAndBack() {
	colorFade('confBox','background','feffc8','fecb00')
	setTimeout("colorFade('confBox','background','fecb00','feffc8')",500)
}

function setElementAttribute( targetID, attributeName, targetValue ) {
	try {
		var command = "document.getElementById( '" + targetID + "' )." + attributeName + " = '" + targetValue + "';";
		eval( command );	
	}
	catch( e ) {
		alert( e );
	}
}