//quote.js
window.addEvent('domready', function() {
	//Check to see if there is an article number in the querystring
	if(getQuerystringVariable('article') != null){
		$('article1').value = getQuerystringVariable('article');
				
		new Ajax('/product/detail?id=' + $('article1').value, {
			method: 'get',
			onComplete: function(){
				var productObj = Json.evaluate(this.response.text);
				var currentProd = productObj.product;
				if(currentProd != null){
					$('prodImg1').src = '/' + currentProd.imgPath + '_s.jpg';
					$('prodImgPath1').value = 'http://www.italycahardware.com/' + currentProd.imgPath + '_s.jpg';
				}else{
					$('prodImg1').src = '/images/noarticle.jpg';
					$('prodImgPath1').value = 'http://www.italycahardware.com/images/noarticle.jpg';
				}
			}
		}).request();
	}
	
	//Add the click event for the Email label
	$('type_email').addEvent('click', function() {
		$('prefCommLabel').setHTML('Email Address:');
	});

	//Add the click event for the Phone Number label
	$('type_phone').addEvent('click', function() {
		$('prefCommLabel').setHTML('Phone Number:');
	});

	//Add the click event for the Fax Number label
	$('type_fax').addEvent('click', function() {
		$('prefCommLabel').setHTML('Fax Number:');
	});
	
	//Add the blur event to every article textbox to do the lookup for the image match
	$$('.article').each(function(article){
		article.addEvent('blur', function(){
			if(article.value != ''){
				new Ajax('/product/detail?id=' + article.value, {
					method: 'get',
					onComplete: function(){
						 var productObj = Json.evaluate(this.response.text);
						 var currentProd = productObj.product;
						 if(currentProd != null){
							$('prodImg' + article.id.substring(article.id.length-1, article.id.length)).src = '/' + currentProd.imgPath + '_s.jpg';
							$('prodImgPath' + article.id.substring(article.id.length-1, article.id.length)).value = 'http://www.italycahardware.com/' + currentProd.imgPath + '_s.jpg';
						 }else{
							$('prodImg' + article.id.substring(article.id.length-1, article.id.length)).src = '/images/noarticle.jpg';
							$('prodImgPath' + article.id.substring(article.id.length-1, article.id.length)).value = 'http://www.italycahardware.com/images/noarticle.jpg';
						 }
					}
				}).request();
			}else{
				$('prodImg' + article.id.substring(article.id.length-1, article.id.length)).src = '';
				$('prodImgPath' + article.id.substring(article.id.length-1, article.id.length)).value = '';
			}
		});
	});
});

function getQuerystringVariable(name) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == name) {
			return pair[1];
		}
	}
} 


