// JavaScript Document
// prepare the form when the DOM is ready 
$(document).ready(function() { 
    // bind form using ajaxForm 
    $('#contact_form').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
 
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   processJson,
		beforeSubmit: validate
    }); 
	 $('#iphone_form').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
 
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   processJson,
		beforeSubmit: validate
    }); 
	$('#subscribe_form').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
 
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   processJsonSub,
		beforeSubmit: validate
    }); 
	function validate(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 complete all the fields'); 
				return false; 
			} 
		} 
	}
	function processJson(data) { 
    // 'data' is the json object returned from the server 
    	if(data.message == 'OK')
		{
			alert("Thank you for showing an interest in us. \nWe will contact you as soon as possible.");
			return;
		}
		alert(data.message);
	}
	function processJsonSub(data)
	{
		if(data.message == 'OK')
		{
			alert("Thank you for subscribing to our Newsletter");	
			return;
		}
		alert(data.message);
	}
});
