/**
 * Request form Page
 * 
 * This is the javascript file for request.php
 * @author David Vélez <david.velez@entersoftweb.com>
 * @version 1.0
 * @package javascript
 */

/**
 * Unsaved changed pending system
 */
var canExit = true;
window.onbeforeunload = function(e) {
     if(!canExit) {
          return msgExit;
     }
};

/**
 * Form inicialization 
 */
$(document).ready(function() {

	$("#loadingBlock").hide();
	$("#requestSendedBlock").hide();
	
	/**
	 * Dialog startup
	 */
	$('#dialogMissingField').dialog({
		autoOpen: false,
		resizable: false,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});	
	$('#dialogAcceptConditions').dialog({
		autoOpen: false,
		resizable: false,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	$('#dialogLegalInfo').dialog({
		autoOpen: false,
		resizable: false,
		width: 500,
		height: 500,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		}
	});
	
	/**
	 * Legal info link funcion
	 */
	$("#legalInfoLink").bind("click", function(){ 
		$('#dialogLegalInfo').load("/legal.php #legalAdviceBlock", function(){
			$('#dialogLegalInfo').dialog("open");
		});
		return false;
	});
	
	/**
	 * Specific source function
	 */
	$("#sourceBlock").hide();
	$("#source").bind("change", function(){
		if($(this).val() == 5)	$("#sourceBlock").slideDown("fast");
		else					$("#sourceBlock").slideUp("fast");		
	});
	
	/**
	 * Masks
	 */ 
	mask("phone", "numeric", 0);
    mask("email", "email", 0);

    /**
     * Check if any have changed in the form 
     */
    $($("#f").elements).bind("change", function(){ canExit = false; })

    /**
     * Form validations 
     */
    $("#f").bind("submit", function() {
	
	    // Disable submit button
		$("#submitFormButton").attr("disabled", "disabled").fadeTo("fast", 0.25);

		// First check the requerid field is not empty
        var missingFields = new Array(); var error = null;
        if(error = checkField("company", "exist"))		missingFields.push(error);
        if(error = checkField("contact", "exist"))		missingFields.push(error);
        if(error = checkField("state", "exist"))		missingFields.push(error);
        if(error = checkField("city", "exist"))   		missingFields.push(error);
        if(error = checkField("phone", "exist"))   		missingFields.push(error);
        if(error = checkField("email", "email"))     	missingFields.push(error);
        if(error = checkField("source", "selector"))	missingFields.push(error);        
        if($("#source").val() == 5){
        	if(error = checkField("sourceSpecify", "exist"))	missingFields.push(error);
        }
                
        // If any error cancel form submit
        if(missingFields.length){
        	// Enable submit button
        	$("#submitFormButton").attr("disabled", "").fadeTo("fast", 1);
        	
        	$('#dialogMissingField').dialog("open");
            
        	return false;
        }else if(!$("#confirmLegal").attr("checked")){
        	// Enable submit button
        	$("#submitFormButton").attr("disabled", "").fadeTo("fast", 1);
        	
        	$('#dialogAcceptConditions').dialog("open");
        	
        	return false;
        }else{
        	canExit = true;
        	
        	$("#formBlock").fadeOut("fast", function(){
        		$("#loadingBlock").fadeIn("fast", function(){
        			$.post("/request.php", { 
						// POST Parameters
						action: 2,
						company: $('#company').val(),
						contact: $('#contact').val(),
						state: $('#state').val(),
						city: $('#city').val(),
						phone: $('#phone').val(),
						email: $('#email').val(),
						source: $('#source').val(),
						sourceSpecify: $('#sourceSpecify').val()
					},
					// Return control
					function(data){
						if(!data["error"]){   // All Ok														
							$("#loadingBlock").fadeOut("fast", function(){
								$("#requestSendedBlock").fadeIn("fast");
							});
						}
					}
					,"json");
        		});
        	});
        	
        	
            return false;
        }
    });
});