﻿function formValidate() {
    $('.field_error_box').fadeOut();
    $('.field_error_box').remove();
    var a = $('.quickQuoteForm input:first').attr('id'); //get the id prefix for this instance
    var c = a; // making a copy so we can do a split
    b = a.split('_');
    var p = c.substring(0, a.indexOf("__")); //p is now the prefix
    f = new Form(p);
    var validated = true;
    jQuery.each(f, function(i, field) {
        if (field.value == '' || field.value == null || field.value.match(/[\s]+/gi)) {
            //alert('You must fill out all required fields.');
            highlight(field);
            fieldError('You must fill out all required fields.');
            validated = false;
        }
    });
    if ((f.email1.value != f.email2.value) && (validated == true)) {
        highlight(f.email1);
        highlight(f.email2);
        fieldError('The email addresses you have entered do not match.');
        validated = false;
    }
    if ((!f.email2.value.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) && (validated == true)) {
        highlight(f.email1);
        highlight(f.email2);
        fieldError('You must provide a valid email address.');
        validated = false;
    }
    if ((!f.phone.value.match(/^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/)) && (validated == true)) {
        highlight(f.phone);
        fieldError('You must provide a valid phone number.');
        validated = false;
    }
    if (!validated) {
        scrollTo('.invalid_field:first');
    }
    return validated;
}
function Form(p) {
    this.email1 = document.getElementById(p + '_emailField_text');
    this.email2 = document.getElementById(p + '_confirmEmail_text');
    this.fname = document.getElementById(p + '_firstNameField_text');
    this.lname = document.getElementById(p + '_lastNameField_text');
    this.phone = document.getElementById(p + '_phoneNumField_text');
}
function highlight(obj) {
    $(obj).addClass('invalid_field');
    $(obj).focus(function() {
        $(this).removeClass('invalid_field');
    });

}
function scrollTo(selector) {
    var targetOffset = $(selector).offset().top - 20;
    $('html,body').animate({ scrollTop: targetOffset }, 500);
}
function fieldError(message) {
    $('.field_error_box').fadeOut();
    $('.quickQuoteForm').after('<div class=\'field_error_box\'>' + message + '<\/div>');
    $('.field_error_box').css('bottom', '10px').css('left', '10px');
    $('.field_error_box').fadeIn();
}

	function LoadModels(item)
	{
//		var modelCombo = <%= _VehicleModel.ClientID %>;
//
//		modelCombo.ClearSelection();
//		modelCombo.SetText("Loading...");
//		modelCombo.RequestItems(item.Text, false);                                
    }
    
	function ItemsLoaded(combo)
	{
//		if (combo.Items.length > 0)
//		{
//			combo.SetText(combo.Items[0].Text);
//			combo.Items[0].Highlight();
			//combo.SelectedIndex = 0;
//		}
		//combo.ShowDropDown();
	}
