// JavaScript Document

$(init);

function init(){  
  init_dialogs();
  // Remove anchor from btn_contact.
  $('.contact_btn').html('<img src="images/contact_btn.gif" width="90" height="18" alt="Contact Us" style="margin:5px  0 0 30px;" class="contact" />');
  $('.contact_toggle').html("<span class='contact'>Contact Us</span>");
  // Bind the contact us button.
  $('.contact').bind('click',function(){
    $('#dlg_contact_us').dialog('open');
  });
}
  
function init_dialogs(){
  var data_pkt = [];
  // Client Add dialog.
  $("#dlg_contact_us").dialog({ 
    buttons: { 
        "Ok": function() {
			// Get he department.
			var dept = $("input[name='dept']:checked").val();
			data_pkt.push("depart=" + escape(dept)); 
            // Collect the client data
           $('#dlg_contact_us :input').each(function() {
             data_pkt.push(this.name + '=' + escape(this.value));
            });
            // Send via ajax.
            send_ajax("single_page_demo/single_page_contact_us.php", data_pkt); 
        }, 
        "Cancel": function() {
            // Reset form.
            $('#dlg_contact_us :input').each(function() {
             $(this).val('');
            });
            $(this).dialog("close");
			// Reset the valid value to 1.
			$('#valid_contact').val('1');
			$('#form_type_contact').val('frm_contact');
        } 
    },
    autoOpen: false,
    modal: true,
	width:380,
    overlay: { 
        opacity: 0.7, 
        background: "black" 
    }
   });
   // Info Dialog.
  $("#dlg_info").dialog({
    buttons:{"Thank You": function(){$(this).dialog('close');}},
    autoOpen: false,
    modal: true,
    overlay: {opacity: 0.7, background: "black" }
    });
  // Error Dialog
  $("#dlg_error").dialog({
    buttons:{"OK": function(){$(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-webdesign.co.uk/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
		$('#dlg_contact_us').dialog("close");
		// Clear Contact Us dialog contents
		$('#dlg_contact_us :input').each(function() {
			$(this).val('');
		 });
		// Reset the valid value to 1.
		$('#valid_contact').val('1');
		$('#form_type_contact').val('frm_contact');
		$('#dlg_info').html("<p>Your message has validated and has been sent.</p>");
		$('#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>";
		$('#dlg_error').html(err);
		$('#dlg_error').dialog("open");
		}
	}
}




