﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

function ContactRequest() {

    var ContactRequestImpl = {
        Name: null,
        Email: null,
        Subject: null,
        Message: null,
        HasBeenValidated: false,

        Populate: function() {
            this.Name = $('#uxTxtName').val();
            this.Email = $('#uxTxtEmail').val();
            this.Subject = $('#uxTxtSubject').val();
            this.Message = $('#uxTxtMessage').val();
        },

        IsValid: function(){
            this.Populate();

            var nameSupplied = this.IsSupplied(this.Name);
            var emailSupplied = this.IsSupplied(this.Email);
            var subjectSupplied = this.IsSupplied(this.Subject);
            var messageSupplied = this.IsSupplied(this.Message);
            var emailValid = this.IsValidEmail(this.Email);
				
            var isValid = nameSupplied && emailSupplied && 
                            subjectSupplied && messageSupplied && emailValid;

			if(!emailSupplied)
			    this.DisplayMsg(emailSupplied, 'uxTxtEmailValMsg','Please make sure you supply an email');
			else    
			    this.DisplayMsg(emailValid, 'uxTxtEmailValMsg', 'Please make sure you supply a valid email');
			
			this.DisplayMsg(nameSupplied , 'uxTxtNameValMsg', 'Please tell me your name.'); 					
            this.DisplayMsg(subjectSupplied, 'uxTxtSubjectValMsg', 'Please give your email a subject.'); 
            this.DisplayMsg(messageSupplied, 'uxTxtMessageValMsg', 'Cat got your tongue?'); 
			    			    									
            this.HasBeenValidated = true;

            return isValid;
        },

        IsValidEmail: function(addr) {
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            return reg.test(addr);
        },

        IsSupplied: function(s) {
            if (s == undefined)
                return false;
            return s.trim().length > 0;
        },
    
		Reset: function(){
			this.HasBeenValidated = false;
		},
		
		DisplayMsg: function(showMsg, elmId, msg){
			$('#'+elmId).html(null);
			
			if(!showMsg) 
			    $('#'+elmId).html(msg);
		}
	};

    ContactRequestImpl.Populate();
   
    return ContactRequestImpl;
}

