var downloadButtons	= [];

function toggleField(object,fieldName) {

	var status = null;

	if (object.checked == 1) {
		status = '';
	} else {
		status = 'none';
	}

	$(fieldName).style.display = status;

}

function showEmail(user, domain, tld, title){

	var emailId = user + "\u0040" + domain + "." + tld;
	var url = "mailto:" + emailId;

	if(!title){
		title = emailId;
	}

	document.write("<a href='" + url + "'>" + title + "</a>");
}


function initAJAX() {
	try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
};

function populatePulldown(section,field,value) {
	var req = new Ajax.Updater(field,'/ajax-requests.php?section=' + section + '&field=' + field + '&value=' + value);
}

function showDealer(id) {
	var req = new Ajax.Updater('dealerInfo','/ajax-requests.php?ax=showDealer&id=' + id);
}

function onDomLoaded(){
	$$('a[rel=external]').each(function(a){Element.writeAttribute(a,'target','_blanc')});
	var DownloadForm = $('DownloadForm');
	var replaceButton;
	if(DownloadForm){
		downloadButtons = Element.select(DownloadForm,'input[type="submit"]');
		downloadButtons.each(function(b,n){
			replaceButton = document.createElement('input');
			replaceButton.type = 'button';
			replaceButton.name = b.name;
			replaceButton.title = b.title;
			replaceButton.value = b.value;
			replaceButton.className = b.className;
			Element.replace(b,replaceButton);
			Event.observe(replaceButton,'click',downloadBrochure.bind(DownloadForm));
			downloadButtons[n] = replaceButton;
		});
	}
	if($('formDealer')){
		Event.observe($('formDealer'),'change',setSelectedDealerID.bindAsEventListener());
	}
}

function setSelectedDealerID(){
	var formDealer = $('formDealer');
	$('DealerID').value = Element.readAttribute(formDealer[formDealer.selectedIndex],"id");
}

function downloadBrochure(e){
	var button = Event.element(e);
	var mailCheck = $('mailCheck');
	var formEmail = $F('formEmail');
	Element.hide('downloadError');
	/*
	if(mailCheck && mailCheck.checked){
		if(formEmail == ''){
			$('downloadError').innerHTML = 'U heeft geen email adres ingevuld';
			$('formEmail').focus();
			Element.show('downloadError');
			return false;
		}
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(formEmail)) {
			$('downloadError').innerHTML = 'Het ingevoerd email adres is niet correct';
			$('formEmail').focus();
			Element.show('downloadError');
			return false;
		}
	}
	*/
	downloadButtons.each(function(b,n){
		if(b != button){
			Element.writeAttribute(b,'disabled');
		}
	});
	new Ajax.Request(this.action,{'postBody':Form.serialize(this)});
	downloadButtons.each(function(b,n){
		if(b != button){
			Element.writeAttribute(b,'disabled',false);
		}
	});
	window.open(
		button.title
	);
	var frame = '<iframe src="/maxlead.php?google_conversion=download" style="display:none"> </iframe>';
	Element.insert(document.body,frame);

}


Event.observe(document,'dom:loaded',onDomLoaded)


function printCoupon() {

	var content = $('coupon').innerHTML;
	var coupon = window.open('','coupon','width=100,height=100');

	coupon.document.open();
	coupon.document.write('<html><head><link rel="stylesheet" href="/maxlead/css/style.css" type="text/css" /></head><body onload="window.print()"><div id="coupon">'+content+'</div></body></html>');
	coupon.document.close();

	setTimeout(function(){coupon.close();},1000);
}
