function submitContactForm()
{
    var name = $('#name').val();
    var email = $('#email').val();
    var phone = $('#phone').val();
    var code = $('#code').val();
    var err = false;
    
    if (name == '')
    {
        $('#err_name').show();
        $('#name').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_name').hide();
    }
    if (email == '')
    {
        $('#err_email').show();
        $('#email').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_email').hide();
    }
    if (phone == '')
    {
        $('#err_phone').show();
        $('#phone').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_phone').hide();
    }
    if (code == '')
    {
        $('#err_code').html('Please enter the code above');
        $('#err_code').show();
        $('#code').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: 'content/ajax/captcha.php',
            data: 'code=' + code,
            async: false,
            success: function(data) {
                if (data != 'ok')
                {
                    err = true;
                    $('#err_code').html('Captcha wrong, please try again');
                    $('#err_code').show();
                    $('#code').effect("highlight", {
                        'color': '#CC6666'
                    }, 2000);
                }
                else
                {
                    $('#err_code').hide();
                }
            }
        })
    }
    
    if (!err)
    {
        $('#contact_form').submit()
    }
}

function goToByScroll(id)
{
    $('html,body').animate({
        scrollTop: $("#"+id).offset().top
        },'slow');
}

function loadForm(id)
{
		$.ajax({
  		url: 'content/ajax/form-' + id + '.php',
  		success: function(data) {
    		$('#layover').html(data);
  	  	$( "#layover" ).dialog({
		      resizable: false,
    		  modal: true,
          width: 375,
          draggable: false,
		      title: 'Free website ' + id,
		      buttons: {
		        "Submit": function() {
		        	var check = false;
		        	switch (id)
		        	{
		        		case 'audit':
		        			check = checkAuditForm();
		        			break;
		        		case 'quote':
		        			check = checkQuoteForm();
		        			break;
		        	}
		        	if (check)
		        	{
		        		$('#' + id + '-form').submit();
			          $( this ).dialog( "close" );
		        	}		        	
        		}
      		}
    		});
      }
		});
    
}

function checkAuditForm()
{
	var name = $('#audit_name').val();
    var email = $('#audit_email').val();
    var url = $('#audit_url').val();
    var code = $('#audit_code').val();
    var err = false;
    
    if (name == '')
    {
        $('#err_audit_name').show();
        $('#audit_name').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_audit_name').hide();
    }
    if (email == '')
    {
        $('#err_audit_email').show();
        $('#audit_email').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_audit_email').hide();
    }
    if (url == '')
    {
        $('#err_audit_url').show();
        $('#audit_url').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_audit_url').hide();
    }
    if (code == '')
    {
        $('#err_audit_code').html('Please enter the code above');
        $('#err_audit_code').show();
        $('#audit_code').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: 'content/ajax/captcha.php',
            data: 'code=' + code,
            async: false,
            success: function(data) {
                if (data != 'ok')
                {
                    err = true;
                    $('#err_audit_code').html('Captcha wrong, please try again');
                    $('#err_audit_code').show();
                    $('#audit_code').effect("highlight", {
                        'color': '#CC6666'
                    }, 2000);
                }
                else
                {
                    $('#err_audit_code').hide();
                }
            }
        })
    }
    
    return !err;
}

function checkQuoteForm()
{
	var name = $('#quote_name').val();
    var email = $('#quote_email').val();
    var company = $('#quote_company').val();
    var code = $('#quote_code').val();
    var err = false;
    
    if (name == '')
    {
        $('#err_quote_name').show();
        $('#quote_name').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_quote_name').hide();
    }
    if (email == '')
    {
        $('#err_quote_email').show();
        $('#quote_email').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_quote_email').hide();
    }
    if (company == '')
    {
        $('#err_quote_company').show();
        $('#quote_company').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $('#err_quote_company').hide();
    }
    if (code == '')
    {
        $('#err_quote_code').html('Please enter the code above');
        $('#err_quote_code').show();
        $('#audit_code').effect("highlight", {
            'color': '#CC6666'
        }, 2000);
        err = true;
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: 'content/ajax/captcha.php',
            data: 'code=' + code,
            async: false,
            success: function(data) {
                if (data != 'ok')
                {
                    err = true;
                    $('#err_quote_code').html('Captcha wrong, please try again');
                    $('#err_quote_code').show();
                    $('#audit_code').effect("highlight", {
                        'color': '#CC6666'
                    }, 2000);
                }
                else
                {
                    $('#err_quote_code').hide();
                }
            }
        })
    }
    
    return !err;
}


$(document).ready(function() {
    $('.dialog').hide();
});
