﻿$(document).ready(function() {
    highlight_page();
    
    submit_form();
});



function submit_form()
{
    $('#ctl00_sub_content_submit_button').click(function() {
        if (validate_form() == true) {
            return true;
        }

        else {
            return false;
        }
    });
}

function validate_form() {
    var valid;
    clearErrors();
    validateName();
    validateCompany();
    validatePhone();
    validateEmail();
  //  alert('validate');
    
    if ((validateName() == true) && (validateCompany() == true) && (validatePhone() == true) && (validateEmail() == true)) {
        valid = true;
     //   alert('valid');
    }

    else {
        valid = false;
        document.getElementById('form_container').scrollIntoView(true);
    }

    
    return valid;




}

function clearErrors() {
    $('#nameError').text("");
    $('#companyError').text("");
    $('#phoneError').text("");
    $('#emailError').text("");
}

function validateName() {

    var name = document.getElementById('ctl00_sub_content_txtName');
//    alert('validate name');
    if (name.value.length > 2) {
         //   alert('name is valid');
            return true;
        }

    else {
          //  alert('name is invalid');
            $('#nameError').text("please enter your name");
            return false;
        }
}

function validateCompany() {
    var company = document.getElementById('ctl00_sub_content_txtCompany');
 //   alert('validate company');
    if (company.value.length > 2) {
     //   alert('company name is valid');
        return true;
    }

    else {
     //   alert('company name is invalid');
        $('#companyError').text("please enter your company name");
        return false;
    }
}

function validatePhone() {
    var phone = document.getElementById('ctl00_sub_content_txtPhone');
//alert('validate phone');
    if (phone.value.length > 2) {
      //  alert('phone is valid');
        return true;
    }

    else {
     //   alert('phone is invalid');
        $('#phoneError').text("please enter your phone number");
        return false;
    }
}


function validateEmail(){
    var email = document.getElementById('ctl00_sub_content_txtEmail');
    var emailfilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    var result = emailfilter.test(email.value)
   
   if (result==true){
     //   alert("email is valid");
            return true;
        }

   else{
         //   alert("email is invalid");
            $('#emailError')
                            .text("Please enter a valid email");
            return false;
        }
}




function get_current_page() {
  var url = window.location.href;
  url = url.substr(url.lastIndexOf("/") + 1);
  return url;
    //alert(url);
}

function highlight_page() {
    var url = get_current_page();
    
    if (url.length == 0) {
        $("#NavHome").css("background", "#525252");
        $(".nav-list").find("a[href='" + url + "']").css("color", "white").toggleClass('nav_focus');
    }

    else {  
    $(".nav-list").find("a[href='" + url + "']")
        .css("color", "white")

        .css("background-color", "#525252");
 
    
    }
}

