/** Global Javascript File 
2010-05-13 Added Catalog Request function
*/
window.addEvent('domready', function(){
	/** set the tool tips */
	$$('.tip').each(function(el){
		el.store('tip:text', el.getElement('.tip-text').get('html'));
	});
	new Tips($$('.tip'), {maxTitleChars:100});
	
	/** show the shopping cart */
	if ($('CartItems')) getCart(viewCart);
});

/**
 * check the search form
 */
function checkSearch()
{
	/** get the form */
	var f = $('search');
	
	/** check values */
	if ((f.partNumbers.value == 'Seperate By commas (,). Omit any dashes (-).') || (f.partNumbers.value.length < 4)){
		new Notify({'text': 'An Error Occurred::You Must Enter A Part Number To Search For', 'classes': ['inlineWarn']});
		return false;
	}
}

/**
 * Add items to the shopping cart
 * @param element form
 */
function addToCart(form)
{
	/** send the request */
	new Request.JSON({'url': '/Cart/add/', 'secure': false, 'data': form.toQueryString(), 'onComplete': function(rs){
		/** check for success */
		if (!rs.success) new Notify({'text': 'An Error Occurred::' + rs.message, 'classes': ['inlineWarn']});
		else {
			/** adjust the shopping cart view */
			viewCart(rs);
			
			/** show success message */
			new Notify({'text': 'Items Added Successfully!', 'classes': ['inlineNotice']});
			
			/** reset the form */
			form.reset();
		}
	}}).send();
}

/**
 * Get the shopping cart
 * @param function callback
 */
function getCart(callback)
{
	/** send the request */
	new Request.JSON({'url': '/Cart/get/', 'secure': false, 'data': {}, 'onComplete': function(rs){
		/** check for success */
		if (!rs.success) new Notify({'text': 'An Error Occurred::' + rs.message, 'classes': ['inlineWarn']});
		else callback.run(rs);
	}}).send();
}

/**
 * Adjust the shopping cart view
 * @param object rs
 */
function viewCart(rs)
{
	/** hide the special order area by default */
	$('SpecialOrder').addClass('hidden');
	
	/** add the cart total amount */
	$('CartTotal').set('text', rs.total.b);
	
	/** add the savings message */
	if (rs.savings.a > 0) $('SavingsMsg').removeClass('hidden').set('text', 'You\'re Saving ' + rs.savings.b + ' Off OEM List!');
	else $('SavingsMsg').addClass('hidden');
	
	/** check for cart */
	if ($('CartItems')){
		/** empty the cart */
		$('CartItems').getChildren().each(function(tr){ tr.destroy(); });
		$('NFCartItems').getChildren().each(function(tr){ tr.destroy(); });
		
		/** store the cart parts */
		$('CartItems').store('cart', rs.cart).store('notfound', rs.notfound);
		
		/** show the cart parts */
		if (rs.cart.length > 0) rs.cart.each(function(c, i){
			$('CartItems').adopt(
				new Element('tr', {'class': 'rs' + (i % 2 == 0 ? '1' : '2')}).adopt(
					new Element('td').set('text', c.part_code)
				).adopt(
					new Element('td', {'class': 'atc'}).set('text', c.quantity)
				).adopt(
					new Element('td', {'class': 'atr'}).set('text', c.formatted_total)
				)
			);
		});
		else $('CartItems').adopt(
			new Element('tr', {'class': 'rs1'}).adopt(
				new Element('td', {'colspan': '3', 'class': 'atc'}).set('text', ' - Shopping Cart Empty - ')
			)
		);
		
		/** add the not found items */
		if (rs.notfound.length > 0){
			/** show the special order area */
			$('SpecialOrder').removeClass('hidden');
			
			/** add parts */
			rs.notfound.each(function(n, i){
				$('NFCartItems').adopt(
					new Element('tr', {'class': 'rs' + (i % 2 == 0 ? '1' : '2')}).adopt(
						new Element('td').set('text', n.part_code)
					).adopt(
						new Element('td', {'class': 'atc'}).set('text', n.quantity)
					).adopt(
						new Element('td', {'class': 'atr'}).set('text', n.manufacturer_name)
					)
				);
			});
		} else $('NFCartItems').adopt(
			new Element('tr', {'class': 'rs1'}).adopt(
				new Element('td', {'colspan': '3', 'class': 'atc'}).set('text', ' - Special Order Empty - ')
			)
		);
	}
}

/**
 * Update the cart items
 * @param element form
 */
function updateCart(form)
{
	/** send the request */
	new Request.JSON({'url': '/Cart/update/', 'secure': false, 'data': form.toQueryString(), 'onComplete': function(rs){
		/** check for success */
		if (!rs.success) new Notify({'text': 'An Error Occurred::' + rs.message, 'classes': ['inlineWarn']});
		else {
			
		}
	}}).send();
}

/** 
 * Empty the shopping cart
 */
function emptyCart()
{
	/** confirm the action */
	if (confirm('Are You Sure You Want To Empty Your Cart?')){
		/** send the request */
		new Request.JSON({'url': '/Cart/purge/', 'secure': false, 'data': {}, 'onComplete': function(rs){
			/** check for success */
			if (!rs.success) new Notify({'text': 'An Error Occurred::' + rs.message, 'classes': ['inlineWarn']});
			else {
				/** show success message */
				new Notify({'text': 'Cart Emptied Successfully!', 'classes': ['inlineNotice']});
				
				/** reload the shopping cart */
				getCart(viewCart);
			}
		}}).send();
	}
}

/**
 * Increment or decrement the ship to address
 * @param element form
 * @param object shipTo
 * @param int i
 */
var key = 0;
function changeShip(form, i)
{
	/** check for object if so do increment */
	if (ship[key + i]){
		/** adjust the key */
		key = key + i;
		
		/** change the fields */
		form.company_ship.value = ship[key].ship_name;
		form.address1_ship.value = ship[key].ship_address1;
		form.address2_ship.value = ship[key].ship_address2;
		form.city_ship.value = ship[key].ship_city;
		form.state_ship.value = ship[key].ship_state;
		form.zipcode_ship.value = ship[key].ship_zip;
	}
}

/**
 * Check the final order page to ensure that all fields are entered
 * @param element form
 */
function checkOrder(form)
{
	if (form.packing_slip.value.length > 0 && form.packing_slip.value.search('.pdf') < 0){
		new Notify({'text': 'An Error Occurred::Packing Slips Must Be In Adobe PDF Format.', 'classes': ['inlineWarn']});
		return false;
	}
}

/**
 * Search through the online manuals for those matching the search criteria
 * @param element form
 */
function searchManuals(form)
{
	/** check that something was entered and is at least 4 characters long */
	if (form.keywords.value.length < 4){
		new Notify({'text': 'An Error Occurred::You Must Enter At Least 4 Characters To Perform A Search.', 'classes': ['inlineWarn']});
		return false;
	}
	
	/** send the search criteria */
	
}

/**
 * Transfer values from one field(s) to another field(s)
 * @param element source
 * @param element destination
 */
function transferValues(source, destination)
{
	/** setup the message variable */
	message = '';
	
	/** cycle through source to get the whole value */
	for (var i = 0; i < source.length; i++) message += source[i] + '\n';
	
	/** set the destination value */
	destination.value += message;
}

/**
 * Check a form for required values
 * @param element form
 */
function checkForm(form)
{
	/** check that required fields are entered */
	valid = true;
	form.getElements('.required').each(function(e){
		/** check for value */
		if (!e.value || e.value.length < 1 || e.value == '-') valid = false;
	});
	
	/** check valid flag */
	if (!valid) new Notify({'text': 'An Error Occurred::You Must Enter All Required Fields.', 'classes': ['inlineWarn']});
	
	/** return valid */
	return valid;
}
 function submitCatalogRequest(customer)
 {
		/** confirm the action */
		if (confirm('Are You Sure You Want To Request A Catalog?')){
			/** send the request */
			new Request.JSON({'url': '/MVCatalog/request/', 'secure': false, 'data': {}, 'onComplete': function(rs){
				/** check for success */
				if (!rs.success) new Notify({'text': 'An Error Occurred::' + rs.message, 'classes': ['inlineWarn']});
				else if (rs.success) {
					new Notify({'text': 'Successful!', 'classes': ['inlineNotice']});
				}
			}}).send();
		}
		else 	{new Notify({'text': 'Request Cancelled', 'classes': ['inlineNotice']});}
		
	}
