// JavaScript Document

function fixPNG(){
	$$('a, div').each(function(el){		   
		var imgURL = el.getStyle('background-image');
		var imgURLLength = imgURL.length;
		if(imgURL != 'none' && imgURL.substring(imgURLLength - 5, imgURLLength - 2)=='png'){
			if(el.getStyle('backgroundRepeat')=='no-repeat'){
				var setimg = 'crop';
			}else{
				var setimg = 'scale';
			}
			el.setStyles({
				background: '',
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='"+setimg+"', src='"+imgURL.substring(5,imgURLLength - 2)+"')"
			});
		}
	});
}

window.addEvent('domready', function() {
	$('errorComment').setStyle('display','none');
	$$('#shortForm .submitButton')[0].addEvent('click', function(e){
		e.preventDefault;
		email_fields('shortForm');
		if (this.console && typeof console.log != "undefined"){
			console.log('pressed');
		}
	});
	var formVis = false;
	$('question').addEvent('keydown', function(e){
		if(!formVis){
			$('shortFormBottom').setStyle('display','block');
			formVis = true;
		}
	});
});

function email_fields(form, aler){
	if($('req_id').value.length){
		var hasError = false;
		var req_fields = $('req_id').value.split(';');
        var nbr_fields = req_fields.length;
        var message = $('question').value;
		var formFailed = false;
		var showError = false;
		$('errorComment').setStyle('display','none');
		req_fields.each(function(el) {
			if (this.console && typeof console.log != "undefined"){
				console.log($(el).value);
			}
			thisVal=$(el).value;
			if(thisVal<1){
				formFailed = true;
			}
				if(thisVal.match(/^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i)||message.match(/^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i)){
					$('errorComment').set('html',"Please do not enter links.");
					showError=true;
				}
				if(el=='email'){
					if(!thisVal.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/)){
						$('errorComment').set('html',"Please enter a valid email address.");
						showError=true;
					}
				}
			});
				
		if(showError){
			$('errorComment').setStyle('display','block');
		}else{
			$('errorComment').setStyle('display','none');
		}
		if(!formFailed){
			document.shortForm.checksubmit.value = '1';
			var req = new Request.HTML({
				method: 'post',
				url: 'includes/mailer.php',
				data: $('shortForm').toQueryString(),
				onRequest: function(){
					var fx1 = new Fx.Slide('shortForm');
					fx1.slideOut();
				},
				onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
					$('thankYouDiv').set('html', responseHTML);
				}
			}).send();
		}else{
			alert('All fields on the Ask the Expert form are required');
		}
	}else{
		alert('This form is temporarily unavailable, please try again later.');	
	}
}

