/*
  Head.js Javascript Loader
  Load scripts like images. Use HTML5 and CSS3 safely. Target CSS for different screens, paths, states and browsers. Make it the only script in your HEAD. A concise solution to universal issues.
  
  head.js("/path/to/jquery.js", "/google/analytics.js", "/js/site.js", function() {
  
     // all done
     
  });
  
  http://headjs.com/
  
 */
	

head.js('/javascripts/jquery-1.5.1.min.js', '/javascripts/jquery.cycle.min.js', '/javascripts/jquery.blockUI.js', function(){

	$(document).ready(function() {
    /* Include  LEADS explorer script*/
    include('http://v9ifuh8n.appmetrx.com/t/v9ifuh8n.js');

    if($("#carousel").length){
	$("#carousel ul.customers").cycle({
	    pager: "#pager",
	    timeout: 10000,
	    speed: 3000
	})
    }



    $("#call_me").click(function(){
	$.blockUI({
	    message: $("#callback"),
	    css: {
		border: 'none',
		width: '455px',
		height: '457px',
		background: 'none',
		textAlign: 'left',
		top: "10%",
		fontSize: "12px",
		cursor: "default"
	    },
	    onUnblock: function(){
		$("#callback").hide();
	    }
	})
	return false
    })

    $("#request_callback").click(function(){
	$.blockUI({
	    message: $("#callback"),
	    css: {
		border: 'none',
		width: '455px',
		height: '457px',
		background: 'none',
		textAlign: 'left',
		top: "10%",
		fontSize: "12px",
		cursor: "default"
	    },
	    onUnblock: function(){
		$("#callback").hide();
	    }
	})
	return false
    })


	

    $("#callback a.close").click(function(){
	$.unblockUI();
    })

    /* Validation of "LOGIN" from */
    var login_form_content  = new Array();
    login_form_content["customer_ref"] = "Please enter your login";
    login_form_content["password_line"] = "Please enter your password";
    $('form#login_form').submit(function(){
	var is_ok = check_form('login_form', login_form_content);
	return is_ok;
    });

    /* Validation of "CONTACT US" from */
    var contact_form_content  = new Array();
    contact_form_content["contact_first_name"] = "Please enter your first name";
    contact_form_content["contact_last_name"] = "Please enter your last name";
    contact_form_content["contact_email"] = "Please enter your email";
    contact_form_content["contact_phone"] = "Please enter your phone number";
    contact_form_content["contact_message"] = "Please enter a message";
    $('form#contact_form').submit(function(submit_event){
	var is_ok = check_form('contact_form', contact_form_content);
	// If correct information is provided, AJAX call
	if(is_ok == true) {
	    // Submit the form via AJAX call
	    var dataString = $('form#contact_form').serialize();
	    $.ajax({
		type: "POST",
		url: "back_end/process_contact_form.php",
		data: dataString,
		dataType: "json",
		success: function(data) {
		    if(data.success == true) {
			alert('Your request has been sent. One of our team will be in touch soon.');
			$('form')[0].reset();
		    } else {
			alert('An error occured submitting your request. Please try again later or contact us by phone/email.');
		    }
		}
	    });
	}
	// Prevent form from being submitted
	submit_event.preventDefault();
	return false;
    });

    /* Validation of "CALL BACK" from */
    var callback_form_content  = new Array();
    callback_form_content["callback_name"] = "Please enter your name";
    callback_form_content["callback_phone"] = "Please enter your phone number";
    callback_form_content["callback_message"] = "Please enter a message";
    $('form#callback_form').submit(function(submit_event){
	var is_ok = check_form('callback_form', callback_form_content);
	// If correct information is provided, AJAX call
	if(is_ok == true) {
	    // Submit the form via AJAX call
	    var dataString = $('form#callback_form').serialize();
	    $.ajax({
		type: "POST",
		url: "back_end/process_call_me_form.php",
		data: dataString,
		dataType: "json",
		success: function(data) {
		    if(data.success == true) {
			// We make the window disappear
			$.unblockUI();
			alert('Your request has been sent. One of our team will be in touch soon.');
			$('form')[0].reset();
		    } else {
			alert('An error occured submitting your request. Please try again later or contact us by phone/email.');
		    }
		}
	    });
	}
	// Prevent form from being submitted
	submit_event.preventDefault();
	return false;
    });

    /* Check if fields of a form are empty and display appropriate error message */
    function check_form(form_name, form_content) {
	var is_ok = true;
	for(var field_name in form_content) {
	    var value = $('form #'+field_name).val();
	    value = $.trim(value);
	    // Check if customer ref field is empty
	    if(value.length == '0') { // zero-length string AFTER a trim
		// Display a warning message
		$('form #'+field_name).removeClass('error');
		// Remove the associated error message
		$('form #'+field_name+'_error').html(form_content[field_name]);
		is_ok = false;
	    } else {
		// Put back the normal class
		$('form #'+field_name).removeClass('error');
		// Remove the associated error message
		$('form #'+field_name+'_error').html('&nbsp;');
	    }
	}
	return is_ok;
    }

    //this function includes all necessary js files for the application
    function include(file) {

	var script  = document.createElement('script');
	script.src  = file;
	script.type = 'text/javascript';
	script.defer = true;

	document.getElementsByTagName('head').item(0).appendChild(script);
    }

	});
});

