// JavaScript Document

$(function() {
	$('input#submit_contact').click(function() {
		var name = $('input#Name').val();
		var company = $('input#Company').val();
		var email = $('input#Email').val();
		var telephone = $('input#Telephone').val();
		var comments = $('textarea#Comments').val();
		var newsletter = $('input#Newsletter');
		if (newsletter.is(':checked')) {
			newsletter = "y";}
		else {
			newsletter = "n";}
		var spam = $('input#Spam').val();
		
		if(spam.length >  1) {	
            alert("Go Away!!");
			$('input#Spam').focus();
        	return false;
        }
		else if(name.length <  1) {	
            alert("You have not entered a first name");
			$('input#Name').focus();
        	return false;
        }
		
		else if(company.length <  1) {	
            alert("You have not entered a company name");
			$('input#Company').focus();
        	return false;
        }
        
       
        
        else if(telephone.length <  1) {	
            alert("You have not entered a telephone number");
			$('input#Telephone').focus();
        	return false;
        }
        
        else if(telephone.length >  1) {
        
            telephone = telephone.replace("-","");
            telephone = telephone.replace(" ","");
            telephone = telephone.replace("(","");
            telephone = telephone.replace(")","");
            telephone = telephone.replace(".","");
    
            for (i=0;i<telephone.length;i++) {
                if (telephone.charAt(i) < "0" || telephone.charAt(i) > "9") {
                    alert("Your telephone number has an invalid character");
					$('input#Telephone').focus();
                	return false;
                }
            }
			
			if(email.length <  1) {	
            alert("You have not entered an email address");
			$('input#Email').focus();
        	return false;
        	}
			
			if(email.indexOf("/") > -1 || email.indexOf("/") > -1 || email.indexOf(":") > -1 || email.indexOf(",") > -1 || email.indexOf(";") > -1 || email.indexOf("@") < 0 || email.indexOf("\.") < 0) {	
            alert("Your email address has an invalid character");
			$('input#Email').focus();
        	return false;
        	}
            
            if(comments.length <  1) {	
               alert("You have not entered a comment");
				$('textarea#Comments').focus();
				return false;
            }
          
        } // telephone length
		
		
		$('<div class="overlay"></div>').appendTo('div.cntContactForm');
		
		$.ajax({
		
			url: 'http://www.candeomedia.com/actions/enquiry_ajax.asp',
			data: {Name:name,Company:company,Email:email,Telephone:telephone,Comments:comments,Spam:spam,Newsletter:newsletter},
			type: 'GET',
			
			success: function(results) {
				$('div.overlay').remove();
				$('input#Name').val(''),
				$('input#Company').val(''),
				$('input#Email').val(''),
				$('input#Telephone').val(''),
				$('textarea#Comments').val('');
				$('#default').fadeOut(400).remove();
			    $('#response').html('');
				$('#response').fadeIn(400).html(results);//can use text instead of html
			}
		});
		return false;
		});
});	//document ready
