$(document).ready(
	function(){
		var value1 = 0, value2 = 0, value3 = 0, value4 = 0;
		
		$("#name").blur(
			function (){
				checkField(this.id);
			}
		);
		/**$("#company").blur(
			function (){
				checkField(this.id);
			}
		);*/
		
		$("#alcoholic").click(function(){	
			value1 = 275.00;
			value2 = 330.00;
			value3 = 412.50;
			value4 = 467.50;
		});
		
		$("#alcoholic2").click(function(){
			value1 = 231.10;
			value2 = 287.10;
			value3 = 334.40;
			value4 = 389.40;
		});
		
		$("#e-mail").blur(
			function (){
				checkField(this.id);
				checkEmail(this.id);
			}
		);
		$("#noOftwin").keyup(
			function(){
				if($("#alcoholic").is(':checked')){
					value1 = 275.00;
					value2 = 330.00;
					value3 = 412.50;
					value4 = 467.50;
				}else{
					value1 = 231.10;
					value2 = 287.10;
					value3 = 334.40;
					value4 = 389.40;
				}
				if($('input[name=twinMachine]').is(':checked')){
					$('span#twinPrice').html("Pick Up: $" + ($('input[name=noOftwin]').val() * value1) + " | Deliver: $" + ($('input[name=noOftwin]').val() * value2));
				}
			}
		);
		$("#noOftrip").keyup(
			function(){
				if($('input[name=tripMachine]').is(':checked')){
					$('span#tripPrice').html("Pick Up: $" + ($('input[name=noOftrip]').val() * value3) + " | Deliver: $" + ($('input[name=noOftrip]').val() * value4));
				}
			}
		);
		$("#mobile").blur(
			function (){
				checkField(this.id);
				checkNumber(this.id);
			}
		);	
		$("#address1").blur(
			function (){
				checkField(this.id);
			}
		);	
		$("#postcode").blur(
			function (){
				checkField(this.id);
				checkNumber(this.id);
			}
		);
		$("#subject").blur(
			function (){
				checkField(this.id);
			}
		);
		$("#date").blur(
			function (){
				checkField(this.id);
			}
		);
		$("#message").blur(
			function (){
				checkField(this.id);
			}
		);
		/**$("#state").blur(
			function (){
				checkField(this.id);
			}
		);(*/
		$("#items").blur(
			function (){
				checkField(this.id);
			}
		);
		$("#veri").blur(
			function (){
				checkField(this.id);
				checkNumber(this.id);
			}
		);
	}
);

function checkField(thisField){
	if ($("#"+thisField).val() == ''){
		$("#" + thisField + "Error").html("Please fill in the " + thisField + " field!");
		return false;
	}else{
		$("#" + thisField + "Error").html("");
		return true
	}
}

function checkNumber(thisField){
	var value = $("#"+thisField).val();
	if(isNaN(value)){
		$("#" + thisField + "Error").html("Invalid input and no space please!");
	}
}

function checkEmail(thisField){	
	var str = document.getElementById('e-mail').value;	
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){		
		$("#" + thisField + "Error").html("Invalid e-mail address!");
		return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		$("#" + thisField + "Error").html("Invalid e-mail address!");
	   return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		$("#" + thisField + "Error").html("Invalid e-mail address!");
		return false;
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		$("#" + thisField + "Error").html("Invalid e-mail address!");
		return false;
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		$("#" + thisField + "Error").html("Invalid e-mail address!");	
		return false;
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		$("#" + thisField + "Error").html("Invalid e-mail address!");	
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		$("#" + thisField + "Error").html("Invalid e-mail address!");
		return false;
	 }
	 
		$("#" + thisField + "Error").html("");
	 return true;	
	 
}

function checkMachineType(){
	var twin = $("#twinC").attr('checked')?1:0;
	var trip = $("#tripC").attr('checked')?1:0;
	
	if(twin || trip){
		if(twin){
			if($("#noOftwin").val() == '0'){
				$("#twinPrice").text("please specify quantity!");
				return false;
			}
		}
		
		if(trip){
			if($("#noOftrip").val() == '0'){
				$("#tripPrice").text("please specify quantity!");	
				return false;
			}			
		}
		return true
	}else{
		$("#twinPrice").text("please select your machine type");
		return false;
	}
//	if($("#twinQ").val){
		
	//}
}

function checkAllFields(){
	if(checkField("name") && checkField("e-mail") && checkField("mobile") && checkField("address1") && checkField("postcode") && checkField("subject") &&checkField("date") && checkField("message") && checkField("items") && checkField("veri") && checkMachineType()){
		return true;
	}else{
		return false;
	}
}