$(function() {
	$("#question1").slider({
		range: "min",
		value: 5,
		min: 1,
		max: 20,
		slide: function(event, ui) {
			$("#amount1").val(ui.value);
			calculate();
		}
	});
	$("#amount1").val($("#question1").slider("value"));
	
	$("#question2").slider({
		range: "min",
		value: 2,
		min: 1,
		max: 3,
		slide: function(event, ui) {
			$("#amount2").val(ui.value);
			calculate();
		}
	});
	$("#amount2").val($("#question2").slider("value"));
});

function calculate(){
	baseprice = 300;
	q1 = parseInt(document.getElementById('amount1').value);
	q2 = parseInt(document.getElementById('amount2').value);

	var q3m = [];
	var q3m_const = [];
	q3m_const[1] = 1.2;
	q3m_const[2] = 1.2;
	q3m_const[3] = 1.2;
	q3m_const[4] = 1.5;
	q3m_const[5] = 1.2;
	
	var q3 = "";
	for (i=1; i <= 6; i++) {
		if(document.getElementById('q3a'+i).checked){ 
			if(q3m_const[i]) m=q3m_const[i];
			else m=0;
			q3m[q3m.length] = m;
			q3 = document.getElementById('q3a'+i+'_label').innerHTML+", "+q3;
		}
	}
	
	if(q1<5) q1m=1;
	else if(q1>=5 && q1<10) q1m=2;
	else if(q1>=10 && q1<=20) q1m=3;
	else q1m=0;
	
	q2m=q2;
	
	finalsavings = q1m*q2m*baseprice;
	for (i in q3m) { 
		finalsavings=finalsavings*q3m[i];
	}
	finalsavings = Math.round(finalsavings);
	if(finalsavings==0){
		document.getElementById('savings_value').style.display = 'none';
		document.getElementById('contact_savings').style.display = 'block';
	}else{
		document.getElementById('contact_savings').style.display = 'none';
		document.getElementById('savings_value').style.display = 'block';
		document.getElementById('savings_value').innerHTML = "&pound;"+finalsavings;
	}
	if(document.getElementById('contactform_calcq1_id') && document.getElementById('contactform_calcq2_id') && document.getElementById('contactform_calcq3_id') && document.getElementById('contactform_savings_id')){
		document.getElementById('contactform_calcq1_id').value=q1;
		document.getElementById('contactform_calcq2_id').value=q2;
		document.getElementById('contactform_calcq3_id').value=q3;
		document.getElementById('contactform_savings_id').value=finalsavings;
	}
}
$(document).ready(function() { 
	$("a.forms").fancybox({
		'hideOnContentClick': false,
		'frameWidth':         450,
		'frameHeight':        500
	});
}); 
	
function sendform(){
	//fields
	//notempty - field should have a value
	//atleastone:GROUP - at least one checked in group
	var fields = new Object();
	fields['contactform_name'] = 'notempty';
	fields['contactform_companyname'] = 'notempty';
	fields['contactform_email'] = 'notempty';
	fields['contactform_telephone'] = 'notempty';
	fields['contactform_officenumber'] = 'notempty';
	fields['contactform_primarylocationcity'] = 'notempty';
	fields['contactform_primarylocationpostcode'] = 'notempty';
	fields['contactform_pcnumber'] = 'notempty';
	fields['contactform_serversnumber'] = 'notempty';
	fields['contactform_macorpc'] = 'notempty';
	fields['contactform_services1'] = 'atleastone:contactform_services';
	fields['contactform_services2'] = 'atleastone:contactform_services';
	fields['contactform_services3'] = 'atleastone:contactform_services';
	fields['contactform_services4'] = 'atleastone:contactform_services';
	fields['contactform_services5'] = 'atleastone:contactform_services';
	fields['contactform_services6'] = 'atleastone:contactform_services';
	fields['contactform_services7'] = 'atleastone:contactform_services';
	fields['contactform_services8'] = 'atleastone:contactform_services';
	fields['contactform_services9'] = 'atleastone:contactform_services';
	fields['contactform_services10'] = 'atleastone:contactform_services';
	fields['contactform_services11'] = 'atleastone:contactform_services';
	fields['contactform_services12'] = 'atleastone:contactform_services';
	fields['contactform_costmodel'] = 'notempty';
	fields['contactform_monthlybudget'] = 'notempty';
	fields['contactform_issueschanges'] = '';
	fields['contactform_contactmethod'] = 'notempty';
	//
	var atleastoneCounters = [];
	var errors=0;
	for (var i in fields){
		var atleastone_re = new RegExp('^atleastone:[a-z_]*$');
		if(fields[i].match(atleastone_re)){
			group = fields[i].replace("atleastone:", "");
			if(!atleastoneCounters[group] && atleastoneCounters[group]!=0){
				if(document.getElementById(i+'_id').checked) atleastoneCounters[group]=1;
				else atleastoneCounters[group]=0;
			}else{
				if(document.getElementById(i+'_id').checked) atleastoneCounters[group]++;
			}
		}else if(fields[i]=="notempty"){
			if(document.getElementById(i+'_id').value=="" || document.getElementById(i+'_id').value=="---" ){
				document.getElementById(i+'_error').innerHTML="This field should have value.";
				++errors;
				if(errors==1) document.getElementById(i+'_error').innerHTML+='<a name="hereiserror"></a>';
			}else{
				document.getElementById(i+'_error').innerHTML="";
			}
		}
	}
	for (var counter in atleastoneCounters){
		if(atleastoneCounters[counter]==0){
			document.getElementById(counter+"_error").innerHTML="You should check at least one.";
			++errors;
			if(errors==1) document.getElementById(i+'_error').innerHTML+='<a name="hereiserror"></a>';
		}else{
			document.getElementById(counter+"_error").innerHTML="";
		}
	}
	calculate();
	
	if(errors) document.location = "#hereiserror";
	else document.contactform.submit();
}

