var baseurl = "/e/tools/performance.cgi";
var proxyurl = "/www2proxy.php";
var def_url = "(Example: http://www.webmetrics.com)";

$(document).ready(function() {
	var inputField 	= $('input#form_websiteurl');

	inputField.blur();

	$('#the_form').validate({ 
		rules: { 
			email: { 
					email: true, 
					required: true
					}, 
			url: {
					url: true,
					required: true,
					maxlength: 500
				},
			name: {
					minlength: 2,
					maxlength: 100,
					required: true
				},
			zip: {
					minlength: 2,
					maxlength: 50,
					required: true
				},
			company: {
					minlength: 2,
					maxlength: 100,
					required: true
				}
			},
		errorClass: 'errorField',
		errorPlacement: function (error, element){
			$('#errorMessage').html('');
			$('#errorMessage').append(error).show();
		},
		submitHandler: function(){
			logConversion();
			addLeadToSalesforce();
			submitForm();
		},
		messages: {
			name: {
				minlength: jQuery.format("Please enter at least {0} characters for the name field."),
				maxlength: jQuery.format("Please limit the name you enter to {0} characters.")
			},
			zip: {
				minlength: jQuery.format("Please enter your 5-digit zip code or at least {0} characters for your country."),
				maxlength: jQuery.format("Please limit your entry in the zip code field to {0} characters.")
			},
			company: {
				minlength: jQuery.format("Please enter at least {0} characters for your company name."),
				maxlength: jQuery.format("Please limit your entry in the company field to {0} characters.")
			}
		},
		success: function(label) {
		}
	});

	inputField.focus(function() {
		if(inputField.val() == def_url){
			inputField.val('http://');
			inputField.focus().val(inputField.val());
		}
	});
	
	inputField.blur(function() {
		if(inputField.val() == 'http://' || inputField.val() == ''){
			inputField.val(def_url);
		}
	});
});

function submitForm(){
    inputSubmit = $('input#form_submit');
    inputField  = $('input#form_websiteurl');
    nameField   = $('input#form_name');
    emailField  = $('input#form_email');
    zipField    = $('input#form_zip');
    companyField = $('input#form_company');

	inputSubmit.attr('disabled', 'disabled');
	
	// Show loading image before we make the request
	$('div#loading').show();

	$.get(proxyurl, {	url: baseurl, 
						timestamp: Math.random(),
						testurl: inputField.val(),
						name: nameField.val(),
						email: emailField.val(),
						zip: zipField.val(),
						company: companyField.val()
					},
	function(xml){updateHTML(xml);}, 'xml');
		
	inputSubmit.removeAttr('disabled');
}

function updateHTML(xml) {
    $(xml).find('results').each(function(){
        var requestStatus = parseInt($(this).attr('status'));

		$('div#loading').hide();

        if(requestStatus){
            $(this).find('result').each(function(){
			
				// Display the URL that the user entered
				$('span#target-site').text($('input#form_websiteurl').val());

				// Show and hide the components from before test
				$('div#try-webmetrics').show();
				$('div#middle').addClass('report');
        		$('div#hotspot').remove();

				// Add the html to the page
				$('div#results').append($(this).text());	
			
				// Run the javascript to make the tabs
				var tabber1 = new Yetii({
									id: 'interface'
				});

				// Show the results
				$('div#results').show();

				// Hide the elements of the index page and show sell text
				$('div#left').remove();
				$('div#left-replacement').attr('id', 'left');

				$('div#right').remove();
				$('div#right-replacement').attr('id', 'right');
            });
        }
        else{
            $(this).find('result').each(function(){
				var errorMessage = $(this).text();
				
				if(errorMessage.match('URL')){
					$('input#form_websiteurl').addClass('errorURL');
				}	
				
				$('div#errorMessage').html('');
                $('div#errorMessage').append($(this).text());
				$('div#errorMessage').show();
            });
        }
    });

    $('#form_submit').removeAttr('disabled');
}

function addLeadToSalesforce()
{

	var sfURL="salesforce.php?first_name="+escape($('input#form_name').val())+"&company="+escape($('input#form_company').val())+
				"&email="+escape($('input#form_email').val())+"&zip="+escape($('input#form_zip').val());

	var sfIframe = "<iframe src='"+sfURL+"' style='display:none'>";

	$('#body').append(sfIframe);
}

