
$(init);

function init(){
	init_menu();
  
  init_dialogs();
  // Enable display elements that are js only.
  jQuery(".js_only").css({display:"inline"});
  // Remove anchor from btn_contact.
  jQuery('#contact_toggle').html("<span id='contact_us'>Message ...</span>");
  // Remove href from enquire btn
  jQuery('#enquire_toggle').html('<img src="images/enquire.gif" width="146" height="33" alt="Get A fixed price website design" style="float:right" id="btn_enquire" />');
  // Bind the contact us button.
  jQuery('#contact_us').bind('click',function(){
    jQuery('#dlg_contact_us').dialog('open');
  });
  // Bind enquiry button.
  jQuery('#btn_enquire').bind('click', function(){
	jQuery('#dlg_enquiry').dialog('open');								  
  });
  //jQuery(".nav .menu_content").css("display","inline");
}

function init_menu(){
	jQuery('#nav_group').accordion({
			autoheight: false,
			header:'a.menu_hdr',
			active: false, 
    		alwaysOpen: false 
		});
}
  
function init_dialogs(){
  var data_pkt = [];
  // Client Add dialog.
  jQuery("#dlg_contact_us").dialog({ 
    buttons: { 
        "Ok": function() { 
            // Collect the client data
           jQuery('#dlg_contact_us :input').each(function() {
             data_pkt.push(this.name + '=' + escape(this.value));
            });
            // Send via ajax.
            send_ajax("contact_us.php", data_pkt);
			// Clear data packet.
			dat_pk = [];
        }, 
        "Cancel": function() {
            // Reset form.
            jQuery('#dlg_contact_us :input').each(function() {
             jQuery(this).val('');
            });
            jQuery(this).dialog("close");
			// Reset the valid value to 1.
			jQuery('#valid_contact').val('1');
			jQuery('#form_type_contact').val('frm_contact');
        } 
    },
    autoOpen: false,
    modal: true,
	width:380,
    overlay: { 
        opacity: 0.7, 
        background: "black" 
    }
   });
  jQuery("#dlg_enquiry").dialog({ 
    buttons: { 
        "Ok": function() { 
            // Collect the client data
           jQuery('#dlg_enquiry :input').each(function() {
             data_pkt.push(this.name + '=' + escape(this.value));
            });
            // Send via ajax.
            send_ajax("enquiry.php", data_pkt);
			// Clear the data packet array.
			data_pkt = [];
        }, 
        "Cancel": function() {
            // Reset form.
            jQuery('#dlg_enquiry :input').each(function() {
             jQuery(this).val('');
            });
            jQuery(this).dialog("close");
			// Reset the valid value to 1.
			jQuery('#valid_enquiry').val('1');
			jQuery('#form_type_enquiry').val('frm_enquiry');
        } 
    },
    autoOpen: false,
    modal: true,
	width:600,
    overlay: { 
        opacity: 0.7, 
        background: "black" 
    }
   });
   // Info Dialog.
  jQuery("#dlg_info").dialog({
    buttons:{"Thank You": function(){jQuery(this).dialog('close');}},
    autoOpen: false,
    modal: true,
    overlay: {opacity: 0.7, background: "black" }
    });
  // Error Dialog
  jQuery("#dlg_error").dialog({
    buttons:{"OK": function(){jQuery(this).dialog('close');}},
    autoOpen: false,
    modal: true,
    overlay: {opacity: 0.7, background: "black" }
    });

}

function send_ajax(page, data_pkt){
  ajax_url = "http://www.dabnis.com/"+page;
  //ajax_url = "http://192.168.1.102:1956/dabnis.com/public_html/"+page;
  $.ajax({
		 type:'POST',
		 url: ajax_url,
		 data: data_pkt.join('&'),
		 dataType: 'json',
		 success: process_form_response
		 });
		 
 // Post command data via ajax to server control script.
 // $.post(ajax_url, data_pkt.join('&'), {success: process_form_response},'json');
}

function process_form_response(response){
	if(response['form'] !== undefined && response['form'] == 'frm_contact'){
		if(response['result'] == true){
		// Message gone OK
		jQuery('#dlg_contact_us').dialog("close");
		// Clear Contact Us dialog contents
		jQuery('#dlg_contact_us :input').each(function() {
			jQuery(this).val('');
		 });
		// Reset the valid value to 1.
		jQuery('#valid_contact').val('1');
		jQuery('#form_type_contact').val('frm_contact');
		jQuery('#dlg_info').html("<p>Your message has validated and has been sent.</p>");
		jQuery('#dlg_info').dialog("open");
		}else{
		// There are errors.
		var err = "<p>You have these errors in your submission.</p>";
		err += "<ul>";
		if(response['contact_name']['valid'] == false) err += "<li>Contact Name: "+response['contact_name']['msg']+"</li>";
		if(response['contact_via']['valid'] == false) err += "<li>Tel or E-Mail: "+response['contact_via']['msg']+"</li>";
		if(response['message']['valid'] == false) err += "<li>Message: "+response['message']['msg']+"</li>";
		err += "</ul>";
		jQuery('#dlg_error').html(err);
		jQuery('#dlg_error').dialog("open");
		}
	}
	// Enquiry form processing.
	if(response['form'] !== undefined && response['form'] == 'frm_enquiry'){
		if(response['result'] == true){
		// Message gone OK
		jQuery('#dlg_enquiry').dialog("close");
		// Clear Contact Us dialog contents
		jQuery('#dlg_enquiry :input').each(function() {
			jQuery(this).val('');
		 });
		// Reset the valid value to 1.
		jQuery('#valid_enquiry').val('1');
		jQuery('#form_type_enquiry').val('frm_enquiry');
		jQuery('#dlg_info').html("<p>Your enquiry has validated and has been sent.</p>");
		jQuery('#dlg_info').dialog("open");
		}else{
		// There are errors.
		var err = "<p>You have these errors in your submission.</p>";
		err += "<ul>";
		if(response['contact_name']['valid'] == false) err += "<li>Contact Name: "+response['contact_name']['msg']+"</li>";
		if(response['contact_via']['valid'] == false) err += "<li>Tel or E-Mail: "+response['contact_via']['msg']+"</li>";
		if(response['comments']['valid'] == false) err += "<li>Comments: "+response['comments']['msg']+"</li>";
		if(response['business_idea']['valid'] == false) err += "<li>Description or idea: "+response['business_idea']['msg']+"</li>";
		err += "</ul>";
		jQuery('#dlg_error').html(err);
		jQuery('#dlg_error').dialog("open");
		}
	}
}

/*
$.post(ajax_url, data_pkt.join('&'), function(response){
    if(response['result'] == true){
    // Message gone OK
    jQuery('#dlg_contact_us').dialog("close");
	// Clear Contact Us dialog contents
	jQuery('#dlg_contact_us :input').each(function() {
 		jQuery(this).val('');
     });
	// Reset the valid value to 1.
	jQuery('#valid_msg').val('1'); 
    jQuery('#dlg_info').html("<p>Your message has validated and has been sent.</p>");
    jQuery('#dlg_info').dialog("open");
    }else{
    // There are errors.
    var err = "<p>You had these errors in your submission.</p>";
    err += "<ul>";
    if(response['contact_name']['valid'] == false) err += "<li>Contact Name: "+response['contact_name']['msg']+"</li>";
    if(response['contact_via']['valid'] == false) err += "<li>Tel or E-Mail: "+response['contact_via']['msg']+"</li>";
    if(response['message']['valid'] == false) err += "<li>Message: "+response['message']['msg']+"</li>";
    err += "</ul>";
    jQuery('#dlg_error').html(err);
    jQuery('#dlg_error').dialog("open");
    }  
  },'json');
*/



