//page load
$(document).ready(function() {
       
    AddHandlers();
       
    jQuery.validator.addMethod("integer", function(value, element, param) {
        return this.optional(element) || ((value > 0) && (value == parseInt(value, 10)));
    }, "Please enter a valid integer.");
    
    jQuery.validator.addMethod("postalcode", function(postalcode, element) {
    	return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
    }, "Please specify a valid postal/zip code");
    
    $('#frmMain').validate({
      rules: {
        ZIP: {
            postalcode: true,
            required: true
        } 
      }
   });
   
});

function submitForm() 
{
    $('#frmMain').submit();
}

//set up event handlers
function AddHandlers() 
{
    $('#selLoanType').change(function() {
        setURL();
    });
    $('#CREDIT_GRADE').change(function() {
        setURL();
    });    
}

function setURL() 
{
    var sUrl = CreditURL;
    var credVal = $('#CREDIT_GRADE').val();
    if ((credVal != 'POOR') && (credVal != 'FAIR')) 
    {
        sUrl = (($('#selLoanType').val() == 'PP_NEWHOME') ? NewHomeURL : RefiURL );    
    }
    $('#frmMain').attr('action',sUrl);
}

