function validate_value(field,name,alerttxt,regex) {

	with (field){
	
		reg = new RegExp(regex)
		
		if ((!reg.test(field.val()))|| field.val()=="") {
		
			$('.error').each( function() {
				var element = this;
				
				if ($(element).attr("class").indexOf(name) != -1) {
						$(element).removeClass("hide");
						$(element).text(alerttxt);
				}
			});
		
			return false;
		
		} else {
		  
			return true;
		
		}
	  
	}
}

//Popup dialog
function popup(message) {
		
	// get the screen height and width  
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();
	
	// calculate the values for center alignment
	var dialogTop =  (maskHeight/3) - ($('#dialog-box').height());  
	var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2); 
	
	// assign values to the overlay and dialog box
	$('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();
	$('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();
	
	// display the message
	$('#dialog-message').html(message);
			
}


$(document).ready(function(){
						   
						   
	$('#slides').bxSlider({
		prev_image: 'images/btn_arrow_left.png',
		next_image: 'images/btn_arrow_right.png',
		wrapper_class: 'slides_wrap',
		margin: 0,
		auto: true,
		auto_controls: false,
		speed: 1000,
		pause: 10000   
	});
	
	$('#slides').css('visibility','visible');
	
	if($('#dialog-message').html() != ""){	
		popup($('#dialog-message').html());
	}
	
	// if user clicked on button, the overlay layer or the dialogbox, close the dialog	
	$('a.btn-ok, #dialog-overlay, #dialog-box').click(function () {		
		$('#dialog-overlay, #dialog-box').hide();		
		return false;
	});
	
	// if user resize the window, call the same function again
	// to make sure the overlay fills the screen and dialogbox aligned to center	
	$(window).resize(function () {
		
		//only do it if the dialog box is not hidden
		if (!$('#dialog-box').is(':hidden')) popup();		
	});	
	
	
	$('#our-services #body #content #main #thumbnails a').click(function(){
		
        $("#our-services #body #content #main #services div").hide();
		$("#our-services #body #content #main #thumbnails a").removeClass("active");
        
        $("#our-services #body #content #main #services div"+$(this).attr('href')).show();
        $(this).addClass("active");
        
        return false;       
    });
	
	
	$('#our-services #body #content #main #thumbnails a[title]').qtip({													  
		style: { 
			width: 130,
			padding: 5,
			background: '#dd1960',
			color: '#ffffff',
			textAlign: 'center',
			border: {
				width: 7,
				radius: 5,
				color: '#dd1960'
			},
			tip: true,
			name: 'dark' // Inherit the rest of the attributes from the preset dark style
		},
				
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftMiddle'
			}
		}
		
	})


	$('#our-clients #body #sidebar ul').innerfade({ 
		speed: 2000, 
		timeout: 12000, 
		type: 'random_start', 
		containerheight: '400px'
	});
	
	$('#our-clients #body #sidebar ul').show();
	
	$("#get-a-quote #body #content form").submit(function () { 
			with ($(this)) {
			
			$(".error").text();
			$(".error").addClass("hide");
			
			$("#errorMsg").text();
			$("#errorMsg").addClass("hide");
			
			var error;
			error = false;
			
			
			if (validate_value($("#details"),"details","Invalid value entered","(^[A-Za-z0-9- ']{1,800}$)")==false) {error = true;}
			if (validate_value($("#name"),"name","Invalid value entered","(^[A-Za-z0-9- ']{1,40}$)")==false) {error = true;}
			if (validate_value($("#company"),"company","Invalid value entered","(^[A-Za-z0-9- ']{1,40}$)")==false) {error = true;}
			if (validate_value($("#phone"),"phone","Invalid value entered","(^[0-9- '()+]{1,40}$)")==false) {error = true;}
			if (validate_value($("#email"),"email","Invalid value entered","(^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$)")==false) {error = true;}
			
			if(error == true) {
				$("#errorMsg").removeClass("hide");
				$("#errorMsg").text("There are some errors with your submission.  Please try again.")
				$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
				return false;
				
			}
			
		}
	});
});
	 

