var bsendform 	= true;

function required(inputId){
	if( $('#'+inputId).val() == '' || $('#'+inputId).val() == $('#'+inputId).attr('title') ){
		$('#e' + inputId).addClass('error');
		bsendform 	= false;
	}else{
		$('#e' + inputId).removeClass('error');
	}
}
		
function email(inputId){
	var objRegExp = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,6})$/;
	 if(!objRegExp.test($('#'+inputId).val())){
		$('#e' + inputId).addClass('error');
		bsendform = false;
	}
	else{
		$('#e' + inputId).removeClass('error');
	}
}

function integer(inputId){
	var objRegExp = /^\d*$/;
	if($('#'+inputId).val() != ''){
		 if(!objRegExp.test($('#'+inputId).val())){
			return false;
		}
		else{
			return true;
		}
	}else{return false;}
}

function currency(inputId){
	var objRegExp = /^\d|\d+\.\d{0,2}$/;
	if($('#'+inputId).val() != ''){
		
		if(!objRegExp.test($('#'+inputId).val())){
			return false;
		}
		else{
			//alert($('#'+inputId).val());
			return true;
		}
	}else{return false;}
}

function postcode(inputId){
	var objRegExp = /^([A-Za-z]{1,2}[0-9]{1,2} \d[A-Za-z]{2})|([A-Za-z]{1,2}[0-9]{1,2}\d[A-Za-z]{2})|([A-Za-z]{2}[0-9]{1}[A-Za-z]{1} \d[A-Za-z]{2})$/;
	if($('#'+inputId).val() != ''){ //WC1A 7XR
		if(!objRegExp.test($('#'+inputId).val())){
			return false;
		}
		else{
			return true;
		}
	}else{return false;}
}

function supply(){
	if($('#how_you_heard_about_us').val() == 'Other' && $('#pleasespecify').val() == ''){
		$('#epleasespecify').addClass('error');
		bsendform = false;
	}else{
		$('#epleasespecify').addClass('');
	}
}

function pleasesupply(){
	if($('#how_you_heard_about_us').val() == 'Other'){
		$('#pleasespecify').val('');
		$('#ps').show();
	}else{
		$('#ps').hide();
	}
}

//************************************ CONTACT FORM *******************************
function validate_contact(){
	bsendform 	= true;
	required('name');
	required('phone');
	email('email');	
	required('how_you_heard_about_us');
	//supply();
	if(bsendform){
		$('#action').val('send');
	}
	return bsendform;
}
//************************************ REVIEW FORM *******************************
function validate_review(){
	bsendform 	= true;
	required('review_rating');
	required('review_from');
	required('review_area');	
	required('review_comments');
	if(bsendform){
		$('#action').val('send');
	}
	return bsendform;
}
//****************************************TABS ************************************/
$(document).ready(function(){
		jQuery(".tab:not(:first)").hide();
		 jQuery(".tab:first").show();
		jQuery("#tabnav a").click(function(){
			jQuery('#tabnav li').removeClass('selected');
			jQuery(this).parent().addClass('selected');
			 stringref = jQuery(this).attr("href").split('#')[1];
			 jQuery('.tab:not(#'+stringref+')').hide();
			 if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
				jQuery('.tab#' + stringref).show();
				return false;
			 }
			 else{
				 jQuery('.tab#' + stringref).fadeIn();
				 return false;
			 }
		});
});
/****************************** QUICK QUOTE *************************************/

//INITIALISE
$(document).ready(function(){
	requestQuote();
	reviewLink();
	initQuickquote();		
});

function initQuickquote(){
	if($('#sizeRandom').length == 1){
			$('#sizeRandom').attr('checked','checked');
			$('.rfixed input[type=text]').val('');
			$('.rfixed input[type=text]').attr('disabled', true);
			$('.rfixed input[type=text]').addClass('disab');
			$('.rsizes input[type=text]').removeAttr('disabled');
			$('.rsizes input[type=text]').removeClass('disab');
			$('.rsizes input[type=text]').parent().parent().removeClass('no-p');
			$('.rfixed input[type=text]').parent().parent().addClass('no-p');
		}
		
		$('.rb').click(function () {
			if ($('#sizeRandom').is(':checked')) {
				$('.rfixed input[type=text]').val('');
				$('.rfixed input[type=text]').attr('disabled', true);
				$('.rfixed input[type=text]').addClass('disab');
				$('.rsizes input[type=text]').removeAttr('disabled');
				$('.rsizes input[type=text]').removeClass('disab');
				$('.rsizes input[type=text]').parent().parent().removeClass('no-p');
				$('.rfixed input[type=text]').parent().parent().addClass('no-p');
				if($('#productsize_0slabs_span').length == 1){
					$('#productsize_0slabs_span').html('');
				}
				
			} else {
				$('.rsizes input[type=text]').val('');
				$('.rsizes input[type=text]').attr('disabled', true);
				$('.rsizes input[type=text]').addClass('disab');
				$('.rfixed input[type=text]').removeAttr('disabled');
				$('.rfixed input[type=text]').removeClass('disab');
				$('.rsizes input[type=text]').parent().parent().addClass('no-p');
				$('.rfixed input[type=text]').parent().parent().removeClass('no-p');
				if($('#productsize_0slabs_span').length == 1){
					$('#productsize_0slabs_span').html('');
				}
			}   
		});	
}

function calculate_quickquote(productId){
	m2 		= 0;
	dVAT	= 0.2;
	pavingPrice = 0;
	if ($('#productsize_1quantity').length == 0){
		for (i=0;i<=$('#productsize_count').val();i++){
			if (currency('productsize_'+i+'m2') ){
				m2 = parseFloat(m2) + parseFloat($('#productsize_'+i+'m2').val());
				pavingPrice = pavingPrice + (parseFloat($('#productsize_'+i+'m2').val())*parseFloat($('#productsize_'+i+'price').val()));
			}
		}
	}else{
		for (i=0;i<=$('#productsize_count').val();i++){
			if (integer('productsize_'+i+'quantity') ){
				m2 = parseFloat(m2) + (parseFloat($('#productsize_'+i+'quantity').val()) * parseFloat($('#productsize_'+i+'factor').val()));
				pavingPrice = pavingPrice + (parseFloat($('#productsize_'+i+'quantity').val())*parseFloat($('#productsize_'+i+'price').val()));
				//alert(parseFloat(m2)+ ' * '+parseFloat($('#productsize_'+i+'price').val()) +' = '+parseFloat(m2)*parseFloat($('#productsize_'+i+'price').val()));
			}
		}
	}
	m2 = toPrice(m2);
	if (m2 != 0){
		pavingPrice = toPrice(pavingPrice);
		if ($('#productsize_1quantity').length == 0){
			$('#sumPaving').html('<span>Paving ('+m2+'m<sup>2</sup>)</span><em id="dPaving">&pound;'+pavingPrice+'</em>');
		}else{
			$('#sumPaving').html('<span>Paving</span><em id="dPaving">&pound;'+pavingPrice+'</em>');
		}
		
		//check postcode
		var deliveryPrice = 0;
		var totalPrice = 0;
		if (postcode('delivery_postcode')){
			$('#sumDelivery').show();
			$.ajax({
			  url: '/_resources/ajax/calculate-delivery.asp?deliverypostcode_name='+$('#delivery_postcode').val()+'&delivery_m2='+m2+'&productId='+productId,
			  success: function(data) {
				  var objRegExp = /^\d*$/;
				  if (objRegExp.test(data)){
					data = toPrice(data);
					deliveryPrice=data;
					data = '&pound;'+data;
					$('#sumDelivery').html('<span>Delivery (3 working days)</span><em id="dDelivery">'+data+'</em>');
					deliveryPrice = parseFloat(deliveryPrice);
					pavingPrice = parseFloat(pavingPrice);
					vatPrice = parseFloat(deliveryPrice) + parseFloat(pavingPrice);
					vatPrice = parseFloat(vatPrice) * parseFloat(dVAT);
					vatPrice = toPrice(vatPrice);
					$('#sumVAT').html('<span>VAT</span><em id="dVat">&pound;'+vatPrice+'</em>');
					totalPrice = parseFloat(deliveryPrice) + parseFloat(vatPrice) + parseFloat(pavingPrice);
					totalPrice = toPrice(totalPrice);
					$('#sumTotal').html('<span class="str">Total</span><em class="str" id="dTotal">&pound;'+totalPrice+'</em>');/**/
				  }
				else{
					$('#sumDelivery').html('<span>Delivery (3 working days)</span><em id="dDelivery">'+data+'</em>');
					vatPrice = parseFloat(pavingPrice)*parseFloat(dVAT);
					vatPrice = toPrice(vatPrice);
					$('#sumVAT').html('<span>VAT</span><em id="dVat">&pound;'+vatPrice+'</em>');
					deliveryPrice = 0;
					totalPrice = parseFloat(deliveryPrice) + parseFloat(vatPrice) + parseFloat(pavingPrice);
					totalPrice = toPrice(totalPrice);
					$('#sumTotal').html('<span class="str">Total</span><em class="str" id="dTotal">&pound;'+totalPrice+'</em>');
				}
			  }
			});
		}
		else{
			//$('#sumDelivery').hide();
			$('#sumDelivery').html('<span>Delivery (3 working days)</span><em id="dDelivery">N/A</em>');
			deliveryPrice = 0;
			pavingPrice = parseFloat(pavingPrice);
			//vatPrice = parseFloat(dVAT);
			vatPrice = parseFloat(pavingPrice)*parseFloat(dVAT);
			vatPrice = toPrice(vatPrice);
			$('#sumVAT').html('<span>VAT</span><em id="dVat">&pound;'+vatPrice+'</em>');
			totalPrice = parseFloat(deliveryPrice) + parseFloat(vatPrice) + parseFloat(pavingPrice);
			totalPrice = toPrice(totalPrice);
			$('#sumTotal').html('<span class="str">Total</span><em class="str" id="dTotal">&pound;'+totalPrice+'</em>');
		}
		$('.Poptions').slideDown('slow');
		$('.Poptions').animate({opacity: 1});
	}
	else{
		$('.Poptions').hide();
		$('.Poptions').animate({opacity: 0});
		$('#sumPaving').html('');
		$('#sumVAT').html('');
		$('#sumDelivery').html('');
		$('#sumTotal').html('');
	}
	setTimeout('sendOrder(productId,3);',1000);
	return false;
}

function toPrice(strPrice){
	strPrice = parseFloat(strPrice).toFixed(2);
	return strPrice;
}

function inputBlur(iInputnumber,sInputtype){
	var bsendform 	= true;
	if (sInputtype == 'm2'){
		//currency('productsize_'+iInputnumber+'m2');
		if (currency('productsize_'+iInputnumber+'m2')){
			$('#equote').html('');
			slabs = parseFloat(Math.round ($('#productsize_'+iInputnumber+'m2').val()/$('#productsize_'+iInputnumber+'factor').val()));
			//$('#calcm2').html($('#productsize_'+iInputnumber+'m2').val()+'/'+$('#productsize_'+iInputnumber+'factor').val()+' = '+($('#productsize_'+iInputnumber+'m2').val()/$('#productsize_'+iInputnumber+'factor').val())+' = '+slabs+' slabs' );
			$('#productsize_'+iInputnumber+'slabs').val(slabs);
			if($('#productsize_0slabs_span').length == 1 && $('#sizeRandom').is(':checked')){
				$('#productsize_0slabs_span').html(slabs+' of each size');
			}
			m2 = ($('#productsize_'+iInputnumber+'slabs').val()*$('#productsize_'+iInputnumber+'factor').val());
			m2 = parseFloat(m2).toFixed(2);
			$('#productsize_'+iInputnumber+'m2').val(m2);
		}else{
			$('#equote').addClass('error');
			$('#equote').html('Please ensure all your values are whole numbers');
			return bsendform;
		}
	}
	else if (sInputtype == 'slabs'){
		integer('productsize_'+iInputnumber+'slabs');
		if(integer('productsize_'+iInputnumber+'slabs')){	//VALIDATION FAILED
			$('#equote').html('');
			m2 = ($('#productsize_'+iInputnumber+'slabs').val()*$('#productsize_'+iInputnumber+'factor').val());
			m2 = parseFloat(m2).toFixed(2);
			//$('#calcslabs').html($('#productsize_'+iInputnumber+'slabs').val()+'*'+$('#productsize_'+iInputnumber+'factor').val()+' = '+($('#productsize_'+iInputnumber+'slabs').val()*$('#productsize_'+iInputnumber+'factor').val())+' = '+m2+' m2' );
			$('#productsize_'+iInputnumber+'m2').val(m2);
			slabs = parseFloat(Math.round ($('#productsize_'+iInputnumber+'m2').val()/$('#productsize_'+iInputnumber+'factor').val()));
			$('#productsize_'+iInputnumber+'slabs').val(slabs);
		}else{
			$('#equote').addClass('error');
			$('#equote').html('Please ensure all your values are whole numbers');
			return bsendform;
		}
	}
}
/**************************************** REQUEST QUOTE BUTTON ********************************************/
function requestQuote(){
	$('#requestQuote').click(function(){
		if($('#c2').length > 0){
			jQuery('#tabnav li').removeClass('selected');
			jQuery('#tabnav li a[name="c2"]').parent().addClass('selected');
			 jQuery('.tab:not(#c2)').hide();
			 if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
				jQuery('.tab#c2').show();
				return false;
			 }
			 else{
				 jQuery('.tab#c2').fadeIn();
				 return false;
			 }		
		}
									  
	});	
}

/**************************************** REVIEW LINK ********************************************/
function reviewLink(){
	$('.reviewlink').click(function(){
		if($('#c3').length > 0){
			jQuery('#tabnav li').removeClass('selected');
			jQuery('#tabnav li a[name="c3"]').parent().addClass('selected');
			 jQuery('.tab:not(#c3)').hide();
			 if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
				jQuery('.tab#c3').show();
				return false;
			 }
			 else{
				 jQuery('.tab#c3').fadeIn();
				 return false;
			 }		
		}
		return false;							  
	});	
}
