﻿
/// <reference name="MicrosoftAjax.js"/>
///for ContactForm.aspx

$(document).ready(function() {

    var xx01 = $('#thisForm').text(); //document.getElementById('thisForm').innerText;
    //alert(xx01);
    var found = xx01.indexOf("Updates");
    //var warraty = xx01.indexOf("Warranty");
    
    if (found > 0) {
        $('#HideMe1').css({ 'display': 'none' });
        $('#HideMe2').css({ 'display': 'none' });
    }
    if (xx01 != "Service Request") {
        $('#HideMe3').css({ 'display': 'none' });
        $('#HideMe4').css({ 'display': 'none' });
    }

    $('#Email').blur(function() {
        ValidateEmail();
    });
    //    $('#dayPhone').blur(function() {
    //        var phoneRegEx = /^(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}(\s*ext(ension)?[0-9]{5})?$/;
    //        phoneRegEx = document.getElementById(this.id).value;
    //        if (!phoneRegEx.match(phoneRegEx)) {
    //            document.getElementById('notification').innerHTML = 'Pls. have a valid phone number';
    //            document.getElementById(this.id).value = "";
    //            $('#dayPhone').focus();
    //        }
    //        else
    //        { document.getElementById('notification').innerHTML = ""; }
    //    });

});

//for validating file upload
function ValidateFile() {
    //    var oas = new ActiveXObject("Scripting.FileSystemObject");
    //    var d = document.getElementById('attach').value;
    //    var e = oas.getFile(d);
    //    var f = e.size;   
        $.ajax({
            type: "POST",
            async: false,
            url: "/Home/CheckFileSize",
            success: function(result) {
                if (result == "true") {
                    document.getElementById('notification').innerHTML = 'Files size must below 2MB';
                    $('#attach').val("");
                    return false;
                }
                else {
                    return true;
                }
            }
        })
    }

    function isOk(captchaInput) {
        //var captchaInput = $('#recaptcha_response_field').val();
        var strErrCaptcha = '<span class="twitTextMsgError"></span>';
        var correct = false;

        $.ajax({
            type: "POST",
            url: "/Home/isCaptchaValid",
            async: false,
            data: { "challenge": Recaptcha.get_challenge(), "response": Recaptcha.get_response() },
            success: function(data) {
                if (data == 'True') {
                    correct = true;
                } else {
                    ///$("#recaptcha_incorrect").html('Please try to match the words shown above.');
                    correct = false;
                }
            },
            error: function() {
                ///$("#recaptcha_incorrect").html('Please try to match the words shown above.');
                correct = false;
            }
        });   return correct;
    }

//function to validate email
function ValidateEmail() {
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    ismail = document.getElementById('Email').value;

    if (NotNull(ismail)) {
        if (!ismail.match(emailRegEx)) {
            document.getElementById('notification').innerHTML = 'Invalid Email Address.';
            //document.getElementById(this.id).value = "";
            $('#Email').select();
            $('#Email').focus();
            return false;
        }
        else {
            document.getElementById('notification').innerHTML = "";
            return true;
        }
    }
}

function SubmitForm() {
    if (!stopThis()) {
        $('#frmTheForm').submit();
    }
}

function stopThis() {
    var xx01 = $('#thisForm').text();
    var name = "";
    var ztop = false;

    if (xx01 == "Service Request") {
        name = "abc1";
    }
    else if (xx01 == "Sales Information Request" || xx01 == "Contact Form") {
        name = "abc2";
    }
    else {
        name = "abc3";
    }

    //    if (!Mandatory(name)) {
    //        $('#sendingGIF').css('display', 'block');
    //    }
    if (Mandatory(name)) {
        ztop = true;
    }
    else if (xx01 == "Service Request") {
        if (!isOk($('#recaptcha_response_field').val())) {
            $("#errorCaptchazz").css("display", "block");
            Recaptcha.reload();
            $('#recaptcha_response_field').val("");
            $('#recaptcha_response_field').css('border', '1px solid #FF1313');
            $('#recaptcha_response_field').focus();
            ztop = true;
        }
    }
    else {
        ztop = false;
    }

    if (!ztop) {
        $('#sendingGIF').css('display', 'block');
        $("#errorCaptchazz").css("display", "none");
    }
    
    return ztop;
}

function Mandatory(thename) {
    var stop = false;
    var chap = false;

    ResetBorder();
    if (thename != "abc2") {
    
        //street Address
        if (!NotNull($('#StreetAdd').val())) {
            stop = true;
            $('#StreetAdd').css('border', '1px solid #FF1313');
            $('#StreetAdd').focus();
        }
        
        // City
        if (!NotNull($('#City').val())) {
            stop = true;
            $('#City').css('border', '1px solid #FF1313');
            $('#City').focus();
        }
        
        // State
        if (!NotNull($('#home_state').val())) {
            stop = true;
            $('#home_state').css('border', '1px solid #FF1313');
            $('#home_state').focus();
        }
        
        // ZipCode
        if (!NotNull($('#ZipCode').val())) {
            stop = true;
            $('#ZipCode').css('border', '1px solid #FF1313');
            $('#ZipCode').focus();
        }
    }

    if (thename == "abc1") {
    //warranty
        //var d = document.getElementById('attach').value;
        //var recapt = document.getElementById('captchaInputed').value;
        
        //DayPhone
        if (!NotNull($('#dayPhone').val())) {
            stop = true;
            $('#dayPhone').css('border', '1px solid #FF1313');
            $('#dayPhone').focus();
        }
        
        //Question needs to hold few characters
        if ($('#textComments').length > 200) {
            stop = true;
            $('#textComments').css('border', '1px solid #FF1313');
            $('#textComments').focus();
        }
        ////for the file size validation
        //if (d != null) {
        //   ValidateFile();
        // }
    }
    
    //email
    if (!NotNull($('#Email').val())) {
        stop = true;
        $('#Email').css('border', '1px solid #FF1313');
        $('#Email').focus();
    }
    
    //commetnsss
    if (!NotNull($('#textComments').val())) {
        stop = true;
        $('#textComments').css('border', '1px solid #FF1313');
        $('#textComments').focus();
    }
    
    //last name
    if (!NotNull($('#LName').val())) {
        stop = true;
        $('#LName').css('border', '1px solid #FF1313');
        $('#LName').focus();
    }
    
    //first name
    if (!NotNull($('#FName').val())) {
        stop = true;
        $('#FName').css('border', '1px solid #FF1313');
        $('#FName').focus();
    }

    if (stop) {
        document.getElementById('notification').innerHTML = 'Pls. Fill up all the Required Fields';
    }
    else {

        //isEmail
        if (!ValidateEmail()) {
            stop = true;
        }
    }

    return stop;
}

function NotNull(obj) {
    return !(obj == null || obj == "undefined" || obj == "" || obj == undefined);
}

function ResetBorder() {
    $('#FName').css('border', '1px solid #BBBBBB');
    $('#LName').css('border', '1px solid #BBBBBB');
    $('#Email').css('border', '1px solid #BBBBBB');
    $('#textComments').css('border', '1px solid #BBBBBB');
    $('#StreetAdd').css('border', '1px solid #BBBBBB');
    $('#City').css('border', '1px solid #BBBBBB');
    $('#ZipCode').css('border', '1px solid #BBBBBB');
    $('#home_state').css('border', '1px solid #BBBBBB');
    $('#dayPhone').css('border', '1px solid #BBBBBB');
}
  