/**
 * Azure Cotnact Form
 */
$(document).ready(function(){
	$('.errorBox, .errIcon').hide();
	$('#contactForm').submit(function() { 
		var options = { 
	        //target:			'#testOutput',   // target element(s) to be updated with server response 
	        //beforeSubmit:	formValidation,  // pre-submit callback 
	        success:		processResponse , // post-submit callback 
	        dataType:		'json',       // 'xml', 'script', or 'json' (expected server response type) 
	        type:			'post'        // 'get' or 'post', override for form's 'method' attribute 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
    	}; 
 
 		$('.errorBox, .successBox').fadeOut('fast', function() {
			$('.errorBox').remove();
		});
 		$('.inputLine, #cMessage').removeClass('hasError');
		$(this).ajaxSubmit(options); 
		return false;//Don't submit form
	});	
});

/**
 * Pre-Send Form Validation
 * @param {Object} formData
 * @param {Object} jqForm
 * @param {Object} options
 */
function formValidation(formData, jqForm, options)
{
	// formData is an array of objects representing the name and value of each field 
    // that will be sent to the server;  it takes the following form: 
    // 
    // [ 
    //     { name:  username, value: valueOfUsernameInput }, 
    //     { name:  password, value: valueOfPasswordInput } 
    // ] 
    // 
    // To validate, we can examine the contents of this array to see if the 
    // username and password fields have values.  If either value evaluates 
    // to false then we return false from this method. 
 	/*
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('Please enter a value for both Username and Password'); 
            return false; 
        } 
    } 
    alert('Both fields contain values.');
    */ 
}

/**
 * Handle response from AJAX call
 * @param JSON responseText
 * @param Object statusText
 */
function processResponse(json)
{
	/** Errors **/
	if(json.status == '0') {
		$('#nlCheck').after('<div class="errorBox pngfix"><ul></ul></div>');
		var $blankFields = 0;
		for (var $i in json.errors) {
			if(json.errors[$i] == 'email') {
				$('.errorBox > ul').append('<li>Please enter a valid email.</li>');
			}
			else {
				$blankFields++;
			}
			//alert('[' + $i+ '] = ' + json.errors[$i]);
			$('#' + $i).addClass('hasError');
		}
		$('.hasError').parent().find('.errIcon').fadeIn();
		$('#contactForm .inputLine:not(.hasError), #cMessage:not(.hasError)').parent().find('.errIcon').fadeOut();
		if($blankFields > 0 ) {
			$('.errorBox > ul').append('<li>Please complete all fields.</li>');
		}
		$('.errorBox').fadeIn();
	}
	/** All ok **/
	else {
		$('.errIcon').fadeOut(); // Hide errors
		$('#nlCheck').after('<div class="successBox pngfix"><ul><li>Thank you! Your message has been sent.</li></ul></div>');
		$('.successBox').fadeIn('slow', function(){
			setTimeout(function() {
				$('.successBox').fadeOut('slow', function(){ $('.successBox').remove() });
            },7000); 
		});
		$('#contactForm').find('.blankOut').each(function(){
			$(this).val(this.defaultValue);
		});
		//alert(json.message);
	}
	
}
