﻿
function FormFocus(obj, defaultText) {

    if (obj.value == defaultText) {
        obj.value = "";
    }
}

function FormChanged(obj, defaultText) {

    if (obj.value == "") {
        obj.value = defaultText;
    }
}

// JavaScript Document

function submitRequest() {

    if (formValid()) {
        //DO THE POST
        FormSubmit();
        window.location = "/submited/default.aspx";
    }
}

function formValid() {

    errorCount = 0;

    var txt_title = $('#txt_title').attr('value');
    var txt_Fname = $('#txt_fname').attr('value');
    var txt_Sname = $('#txt_sname').attr('value');
    var txt_postcode = $('#txt_postcode').attr('value');
    var txt_tel = $('#txt_tel').attr('value');

    if (txt_title.length == 0 || txt_title == 'Title') {
        errorCount++;
    }

    if (txt_Fname.length == 0 || txt_Fname == 'First Name') {
        errorCount++;
    }

    if (txt_Sname.length == 0 || txt_Sname == 'Surname') {
        errorCount++;
    }

    if (txt_postcode.length == 0 || txt_postcode == 'Post Code') {
        errorCount++;
    }

    if (txt_tel.length == 0 || txt_tel == 'Telephone No') {
        errorCount++;
    }

    if (errorCount == 0) {
        return true;
    } else {

        alert("please ensure you have completed all fields.");
        return false;

    }

}

function FormSubmit() {
    $.ajax({
        type: "POST",
        url: "/AJAX/default.aspx",
        data: ({ p: 'submitForm',
        txt_title: $('#txt_title').attr('value'),
        txt_fname: $('#txt_fname').attr('value'),
        txt_sname: $('#txt_sname').attr('value'),
        txt_postcode: $('#txt_postcode').attr('value'),
        txt_tel: $('#txt_tel').attr('value') 
        }),
        success: function(msg) {

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });  
}
