/*
 * copyright 2009 Fossit Solutions LLC
 * all rights reserved
 */
$(document).ready(function(){
	
	// browser specific fixes
	jQuery.each(jQuery.browser, function(i, val) {
  		if (i == "msie" && jQuery.browser.version == "6.0") {
			$("#main_content").css('margin-left','10px');
		}
		if (i == "mozilla") {
			$("#main_header_navi").css('-moz-border-radius-topleft','10px')
		}
		if (i == "safari") {
			$("#main_header_navi").css('-webkit-border-top-left-radius','10px');
		}
	});
	
	// preload images
	$.preloadImages(
		"images/login_over.png",
		"images/webdesign.jpg",
		"images/webapps.jpg",
		"images/latest_background.jpg",
		"images/infotech.jpg"
	);
	
	// highlight current page
	var pathname = window.location.pathname;
	pathname = pathname.split('/');
	pathname =  pathname[pathname.length -1];
	$("a[href='"+pathname+"']").addClass("highlight");
	
	// rollover images
	$(".submit").hover(
		function(){
			$(this).attr("src","images/login_over.png");
		},
		function(){
			$(this).attr("src","images/login.png");
		}
	);
	
	// fade between front page images
	$('.fade').innerfade({ 
		speed: 2000, 
		timeout: 15000, 
		type: 'sequence',
		containerheight: '149px' 
	});
	
	// highlight login input boxes when selected
	$(".textinput,.textareainput").focus(function(){
		$(this).addClass('texthighlight');
	});
	$(".textinput,.textareainput").blur(function(){
		$(this).removeClass('texthighlight');
	});
	
	// lightbox for latest projects
	$(".latest_lightbox").colorbox({transition:"elastic"});
	
	//Submit Quote Request on Enter
	$("#quoteform input").keyup(function(event){
  		if(event.keyCode == 13){
    		$("#quoterequestsubmit").trigger("click");
  		}
	});
	
 	// validate name input
    $(".inputname").blur(function(){
        nameregex = /^.{2,}$/;
        if (nameregex.test(this.value) == true || this.value == '') {
            $(this).removeClass("inputerror");
        }
        else {
            $(this).addClass("inputerror");
        }
    });
	
	// validate email address
    $(".inputemail").blur(function(){
        emailregex = /^.+@[^\.].*\.[a-z]{2,}$/;
        if (emailregex.test(this.value) == true || this.value == '') {
            $(this).removeClass("inputerror");
        }
        else {
            $(this).addClass("inputerror");
        }
    });
	
	// validate phone number
    $(".inputphone").blur(function(){
		phoneregex = /^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[eExXtT\.]+ ?\d+)?$/;
        if (phoneregex.test(this.value) == true || this.value == '') {
            $(this).removeClass("inputerror");
        }
        else {
            $(this).addClass("inputerror");
        }
    });
	
	// validate required fields
	$(".required").error(function(){
		if (this.value == '') {
			$(this).addClass("inputerror");
        }
	});
	
	// validate required fields, count total errors and submit if no errors
	$("#quoterequestsubmit").click(function(){
		$("input").trigger("blur");
		$(".required").trigger("error");
		if (parseInt($(".inputerror").size()) == 0) {
            $("#quoterequestsubmit").hide();
			$("#quoteerror").html("");
            var formdata = $("#quoteform").serialize();
            $.post("includes/process.php", formdata, function(data){
                $("#debug").html(data);
                if (data == "success") {
                    $("#quoterequestbody").slideUp("slow",function(){
						$("#quoterequestbody").html("<h3 class=\"inv\">Thank you</h3><p>A Fossit Solutions Representative will contact you shortly</p>");
						$("#quoterequestbody").slideDown("slow");
					});
                }
                else {
                    $("#quoterequestsubmit").show();
					$("#quoteerror").html("Error, Please Try to Submit Again");
                }
            });
        } else {
			$("#quoteerror").html("Please Fix All Highlighted Errors and Submit Again.");
		}
	});
});

