﻿jQuery(document).ready(function() {


    jQuery("#txtname").focus();
    jQuery("#txtname").keypress(function(e) {
        if (e.which == 13) {
            if (jQuery("#txtname").val() == "") {
                alert("Enter your Name");
                return false;
            }
            jQuery("#txtPhone").focus();
        }
    });

    jQuery("#txtEmail").keypress(function(e) {
        if (e.which == 13) {
            if (jQuery("#txtEmail").val() == "") {
                alert("Enter your Email ID");
                return false;
            }
            if (validator.form()) {
                jQuery("#txtQuory").focus();
            }
            else {
                alert("Enter Valid Email ID");
                return false;
            }
        }
    });
    jQuery("#txtQuory").keypress(function(e) {
        if (e.which == 13) {
            if (jQuery("#txtQuory").val() == "") {
                alert("Enter your Query");
                return false;
            }
            jQuery("#drTopic").focus();
        }
    });
    jQuery("#drTopic").keypress(function(e) {
        if (e.which == 13) {
            if (jQuery("#drTopic").val() == "") {
                alert("Select 'How did you hear about us?'");
                return false;
            }
            jQuery("#btnsubmit").focus();
        }
    });
    ///Variables
    var nameval, phoneval, emailval, topicval, queryval;

    jQuery("div.error").hide();

    //Examples of how to assign the ColorBox event to elements


    jQuery.validator.messages.required = "";
    jQuery("#formhelp").bind("invalid-form.validate", function(e, validator) {
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
				? 'You missed a field. It has been highlighted in red.'
				: 'You missed ' + errors + ' fields.  They have been highlighted.';
            jQuery("div.error").show();

        } else {
            jQuery("div.error").hide();
        }
    });

    var validator = jQuery("#formhelp").validate({
        onkeyup: false,
        debug: true,
        messages: {
            txtEmail: ""
        }
    });




    ///build json for sending mail
    function BuildJsonForsendingMail() {
        var SendMail = BuildHtmlForsendingMail();
        var subjectVal = "Noortakaful.com Web Query";
        var mailRequest = { act: 'sendQueryToEmail',
            From: emailval,
            To: "info@noortakaful.com",
            Subject: subjectVal,
            Body: SendMail,
            Bcc: 'nagashri@sigmasoftwarearchitects.com~sanjer@sigmasoftwarearchitects.com~'
        };
        return mailRequest;
    }

    jQuery("#txtPhone").keypress(function(e) {
        if (!((e.which >= 48) && (e.which <= 57)) || ((e.which == 13))) {
            if ((e.which == 13)) {
                if (jQuery("#txtPhone").val() == "") {
                    alert("Enter your Phone No.");
                    return false;
                }
                jQuery("#txtEmail").focus();
            }
            return false;
        }
    });
    ///forgot quote no
    jQuery("#btnsubmit").click(function() {
        if (jQuery("#txtname").val() == "") {
            alert("Enter your Name");
            return false;
        }
        else if (jQuery("#txtPhone").val() == "") {
            alert("Enter your Phone No.");
            return false;
        }
        else if (jQuery("#txtEmail").val() == "") {
            alert("Enter your Email ID");
            return false;
        }
        else if (jQuery("#txtQuory").val() == "") {
            alert("Enter your Query");
            return false;
        }
        else if (jQuery("#drTopic").val() == "") {
            alert("Select 'How did you hear about us?'");
            return false;
        }
        if (!(validator.form())) {
            alert("Enter Valid Email ID");
            return false;
        }

        if (validator.form()) {
            nameval = jQuery("#txtname").val();
            phoneval = jQuery("#txtPhone").val();
            emailval = jQuery("#txtEmail").val();
            topicval = jQuery("#drTopic").val();
            queryval = jQuery("#txtQuory").val();
            //var mailRequest=BuildJsonForsendingMail();
            var mailRequest = { act: 'savequery',
            name: nameval,
            phone: phoneval,
            email: emailval,
            topic: topicval, 
            query: queryval
            };
            ajaxLoading();
            jQuery.post("/NTQuries.aspx", mailRequest, function(data) {
                if (data == "1") {
                    jQuery("div.error").hide();
                    alert("Thanks for your feedback.");
                }
                else {
                    jQuery("div.error").hide();
                    alert(data);
                }
            });
        }
    });

    function BuildHtmlForsendingMail() {
        var htmlbody = "<body style='font-family:Arial; font-size:10px; color:#000000;'>";
        htmlbody += "<table width='100%' border='0' cellspacing='0' cellpadding='0' style='border:1px solid #CCCCCC;'>";
        htmlbody += "<tr>";
        htmlbody += "<td width='4%'>&nbsp;</td>";
        htmlbody += "<td width='68%'>&nbsp;</td>";da
        htmlbody += "<td width='28%'>&nbsp;</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td height='25' colspan='3' align='left' style='font-size:13px;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Query details:</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td height='25' colspan='2' align='left' style='font-size:11px;'><label><div align='justify'>" + queryval + "</div></label></td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td>&nbsp;</td>";
        htmlbody += "<td align='right'>From</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>Email : " + emailval + "</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td height='24'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>Name : " + nameval + "</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td height='26'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>Phone : " + phoneval + "</td>";
        htmlbody += "</tr>";
        htmlbody += "<tr>";
        htmlbody += "<td height='26'>&nbsp;</td>";
        htmlbody += "<td align='left'>&nbsp;</td>";
        htmlbody += "<td align='left'>From page : " + window.location + "</td>";
        htmlbody += "</tr>";
        htmlbody += "</table>";
        htmlbody += "</body>";
        return htmlbody;
    }

    function ajaxLoading() {
        jQuery().ajaxStart(function() {
        }).ajaxStop(function() {
        }).ajaxError(function(XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 404) {
                alert("Error: Page not found... Try again");
                return false;
            }
            else if (XMLHttpRequest.status == 403) {
                alert("Error: Access Denied/Forbidden... Try again");
                return false;
            }
            else {
                alert("Error:Sorry we could not process your Request... Kindly try again");
                return false;
            }

        });
    }

});