var err_slider = null;

window.addEvent('domready', function() {
	try{
		if($('error-msg')){
			err_slider = new Fx.Slide('error-msg').slideOut();
		}
		if($('budget')){
			$('budget').onchange = CalculateFee;
			CalculateFee();
		}
		$$('FORM').each( function(el){ el.onsubmit = SubmitForm; } );
	} catch(e){}
});

function HideError(){
	if(err_slider){
		err_slider.slideOut();
	}
}

function DisplayError(msg){
	$('error-msg').setHTML(msg);
	if(err_slider){
		err_slider.slideIn();
	}
}

function SubmitForm(){
	var id = this.id;
	var fx = new Fx.Styles('fm-element', {duration: 1000, wait: false});
	fx.start({'opacity': 0.3}).addEvent('onComplete', function(){
		$(id).send({update: null, onComplete: function (request) {FormCallback(request); }});
	});
	return false;
}

function FormCallback(request){
	var fx = new Fx.Styles('fm-element', {duration: 1000, wait: false});
		if((error = ProcessError(request)) != ''){
			DisplayError(error);
			fx.start({'opacity': 1});
		} else {
			SayThankYou();
			window.location="thanks.html";

		}
}

function ProcessError(src){
	if(src.search(':') != -1){
		var list = src.split(':');
		return (list[0] != 'DONE' ? list[1] : '');
	} else {
		return src;
	}
}

function SayThankYou(){
	HideError();
	var fx = new Fx.Styles('fm-element', {duration: 1000, wait: false});
	fx.start({'opacity': 0}).addEvent('onComplete', function(){
		$('fm-element').style.display = 'none';
		$('thank-you').style.opacity = 0;
		$('thank-you').style.display = 'block';

		var fxx = new Fx.Styles('thank-you', {duration: 1000, wait: false});
		fxx.start({'opacity': 1});
	});
};

var can_calculate = false;

function  ToggleEngines(obj){
	$$('#engines INPUT').each( function(el){ el.disabled = (obj.value == 'No'); });
	$('one-time').style.display = ((obj.value == 'No') ?  'block' : 'none');
	$('fee-value').setHTML('$800');
	CalculateFee();
}

function CalculateFee(){
	if (!$('one-time') || !$('fee')){ return; }
	$('congratulations').style.display = 'none';
		$('fee').style.display = 'block';
    if($('budget').options[$('budget').selectedIndex].value > 2000 && $('budget').options[$('budget').selectedIndex].value < 7500) {
		$('fee-value').setHTML('$' + ($('budget').options[$('budget').selectedIndex].value * 16) / 100);
    } else if($('budget').options[$('budget').selectedIndex].value >= 7500 && $('budget').options[$('budget').selectedIndex].value <= 25000) {
        $('fee').style.display = 'none';
        $('congratulations').setHTML('<strong>Congratulations</strong>, you qualify for our Gold account program which entitles you to a <u>30 Day\'s Performance Based Money Back Guarantee</u>. An account manager will call you with the details.');
        $('congratulations').style.display = 'block';
    } else if($('budget').options[$('budget').selectedIndex].value > 25000) {
		$('fee').style.display = 'none';
        $('congratulations').setHTML('<strong>Congratulations</strong>, you qualify for our Platinum account program which entitles you to a <u>30 Day\'s Performance Based Money Back Guarantee</u>. An account manager will call you with the details.');
		$('congratulations').style.display = 'block';
	} else if($('budget').options[$('budget').selectedIndex].value == 2000) {
		$('fee-value').setHTML('$800');
	}
}

var timeRefresh = 7000;
var timeRefreshDistance = 500;

function StartSlider(){
	$$('#images A').each( function(el){
		el.id = $random(1, 1000);
		timeRefresh += timeRefreshDistance;
		el.rev = timeRefresh; 
		
		/*el.rel = 0;*/
		setTimeout(function(){ startBanner(el) }, el.rev);
	});	
}

function startBanner(obj){
	var fx = new Fx.Styles(obj.id, {duration: 500, wait: false});
	fx.start({'opacity': 0}).addEvent('onComplete', function(e){
		$(obj.id).removeEvents();
		$(obj.id).rel = GetRandElem();
		
		child = obj.childNodes[0];
		
		child.src = './public/images/clients/'  + ImageList[obj.rel][0];
		child.alt = ImageList[obj.rel][1];
		obj.href = ImageList[obj.rel][2];
		
		obj.setStyle('opacity', 1);	
		var fy = new Fx.Styles(obj.id, {duration: 500, wait: false});
		fy.start({'opacity': 1}).addEvent('onComplete', function(e){
			$(obj.id).removeEvents();
			setTimeout(function(){ startBanner(obj) }, obj.rev);
		});
	});
}

function GetRandElem(){
	var imageHolders = $$('#images A');	
	for(var i = 0; i < ImageList.length; i++){
		var isVisible = false;
		for(var j = 0; j < imageHolders.length; j++){
			if(i == imageHolders[j].rel){
				isVisible = true;
				break;
			}
		}
		if(!isVisible){ return i; }
	}	
	return $random(0, ImageList.length - 1);
}

