// REGULAR EXPRESSION DECLARATIONS
var reWhitespace         = /^\s+$/;
var reLetter             = /^[a-zA-Z]$/;
var reAlphabetic         = /^[a-zA-Z]+$/;
var reAlphanumeric       = /^[a-zA-Z0-9]+$/;
var reDigit              = /^\d/;
var reLetterOrDigit      = /^([a-zA-Z]|\d)$/;
var reInteger            = /^\d+$/;
var reSignedInteger      = /^(\+|-)?\d+$/;
var reFloat              = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
var reSignedFloat        = /^(((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))$/;

var reEmail              = /^.+@.+\..+$/;
var reMailbox            = /^[\w-]+(\.[\w-]+)*$/;
var reHitRunEmail        = /^.+@hitandrunbluegrass\.com$/;
var reURL                = /^http:\/\/.*$/;

var reImageFile          = /^.*(\.jpg)$/;
var reAudioFile          = /^.*(\.mp3)$/;
var reHtmlFile           = /^.*(\.html)$/;
var reTextFile           = /^.*(\.txt)$/;

function formValidator()
{
    this.errorList   = new Array;
    this.warningList = new Array;
    this.actionList  = new Array;

    this.isEmpty               = isEmpty;       
    this.isWhitespace          = isWhitespace;  
    this.isNumber              = isNumber;      
    this.isAlphabetic          = isAlphabetic;  
    this.isAlphaNumeric        = isAlphaNumeric;        
    this.isWithinValueRange    = isWithinValueRange;    
    this.isWithinLengthRange   = isWithinLengthRange;   
    this.isChecked             = isChecked;     
                               
    this.isPhoneNumber         = isPhoneNumber; 
    this.isFullZipCode         = isFullZipCode; 
    this.isPartialZipCode      = isPartialZipCode;      
    this.isFullRadioCall       = isFullRadioCall;       
    this.isPartialRadioCall    = isPartialRadioCall;    
    this.isUSRadioCall         = isUSRadioCall; 

    this.isEmailAddress        = isEmailAddress;        
    this.isMailbox             = isMailbox;     
    this.isHitRunEmailAddress  = isHitRunEmailAddress;  
    this.isURL                 = isURL;
    
    this.isImageFile           = isImageFile;
    this.isAudioFile           = isAudioFile;
    this.isHtmlFile            = isHtmlFile;
    this.isTextFile            = isTextFile;

    this.raiseError            = raiseError;    
    this.raiseWarning          = raiseWarning;  
    this.raiseAction           = raiseAction;
    
    this.numErrors             = numErrors;     
    this.numWarnings           = numWarnings;   
    this.numActions            = numActions;
    
    this.displayFirstError     = displayFirstError;     
    this.displayFirstWarning   = displayFirstWarning;   
    this.displayAllWarnings    = displayAllWarnings;    
    this.performFirstAction    = performFirstAction;    
    this.performAllActions     = performAllActions;
    
    this.finish                = finish;        
}

function fvAction(type,elt,val) {this.type = type; this.elt = elt; this.val = val;}

// fv regexp functions         
function isEmpty(s)                       {return ((s == null) || (s.length == 0));}
function isWhitespace(s)                  {return (isEmpty(s) || reWhitespace.test(s));}
function isNumber(s)                      {return (!isNaN(s));}
function isAlphabetic(s)                  {return (reAlphabetic.test(s));}
function isAlphaNumeric(s)                {return (reAlphanumeric.test(s));}
function isWithinValueRange(s, min, max)  {return (s >= min && s <= max);}
function isWithinLengthRange(s, min, max) {return (s.length >= min && s.length <= max);}
function isChecked(obj)                   {return (obj.checked);}
                                          
function isPhoneNumber(s)                 {return reInteger.test(s) && s.length == 10;}
function isFullZipCode(s)                 {return reInteger.test(s) && s.length == 5;}
function isPartialZipCode(s)              {return reInteger.test(s) && s.length <= 5;}
function isFullRadioCall(s)               {return reAlphabetic.test(s) && s.length == 4;}
function isPartialRadioCall(s)            {return reAlphabetic.test(s) && s.length <= 4;}
function isUSRadioCall(s)                 {return s.charAt(0) == 'K' || s.charAt(0) == 'W';}
                                          
function isURL(s)                         {return reURL.test(s);}
function isEmailAddress(s)                {return reEmail.test(s);}
function isMailbox(s)                     {return reMailbox.test(s);}
function isHitRunEmailAddress(s)          {return reHitRunEmail.test(s);}

function isImageFile(s)                   {return reImageFile.test(s);}
function isAudioFile(s)                   {return reAudioFile.test(s);}
function isHtmlFile(s)                    {return reHtmlFile.test(s);}
function isTextFile(s)                    {return reTextFile.test(s);}
                                          
// fv error,warning,action functions         
function numErrors()                      {return this.errorList.length;}
function numWarnings()                    {return this.warningList.length;}
function numActions()                     {return this.actionList.length;}
                                          
function raiseError(msg)                  {this.errorList.push(msg);}
function raiseWarning(msg)                {this.warningList.push(msg);}
function raiseAction(type,elt,val)        {var action = new fvAction(type,elt,val); this.actionList.push(action);}
                                          
//function displayAllErrors()               {while(this.errorList.length>0) return this.displayFirstError(); return}
function displayAllWarnings()             {while(this.warningList.length>0) if (!this.displayFirstWarning()) return false; return true;}
function performAllActions()              {while(this.actionList.length>0) this.performFirstAction();}

function displayFirstError()              {alert("Error: " + this.errorList.shift());  return false;}
function displayFirstWarning()            {return confirm("Warning: " + this.warningList.shift());}
function performFirstAction()
{
    var action = this.actionList.shift();
    switch(action.type)
    {
    case 'format_url': action.elt.value = 'http://'+action.val; break;
    case 'format_phone': action.elt.value = action.val.substring(0,3)+'.'+action.val.substring(3,6)+'.'+action.val.substring(6,10); break;
    case 'replace_value': action.elt.value = action.val; break;
    }
}

function finish()
{
    if (this.numErrors() > 0) return this.displayFirstError();
    if (this.numActions() > 0) this.performAllActions();
    if (this.numWarnings() > 0) return this.displayAllWarnings();
    return true;
}

// other stuff
function getRadioButtonValue(radio)
{
    for (var i = 0; i < radio.length; i++) if (radio[i].checked) return radio[i].value;
    return null;
}

function getSelectedValues(select)
{
    var values = new Array;
    //if ((select.type != "select-one") && (select.type != "select-multiple")) return null;
    switch (select.type)
    {
    case 'select-one':
        for (var i=0; i<select.options.length; i++) if (select.options[i].selected) return select.options[i].value;
        return null;
    case 'select-multiple':
        for (i=0; i<select.options.length; i++) if (select.options[i].selected) values.push(select.options[i].value);
        return values;
    }
    return null;
}

// function getSelectedValue(select)
// {
//     var tmp = getSelectedValues(select);
//     return tmp[0];
// }

/****************************************************************/

function ValidateObjectForm(form)
{
    var fv = new formValidator();

    var validation_function;

    var object_type   = form.object_type.value;
    var object_action = form.object_action.value;
    
    switch(object_type)
    {
    case 'Album':            validation_function = ValidateAlbumForm;       break;
    case 'Article':          validation_function = ValidateArticleForm;     break;
    case 'BandMember':       validation_function = ValidateBandForm;        break;
    case 'BinaryFile':       validation_function = ValidateBinaryFileForm;  break;
    case 'Cart':             validation_function = ValidateCartForm;        break;
    case 'EmailAddress':     validation_function = ValidateEmailForm;       break;
    case 'Howdy':            validation_function = ValidateHowdyForm;       break;
    case 'Image':            validation_function = ValidateImageForm;       break;
    case 'Link':             validation_function = ValidateLinkForm;        break;
    case 'Merchandise':      validation_function = ValidateMerchandiseForm; break;
    case 'Newsletter':       validation_function = ValidateNewsletterForm;  break;
    case 'Photo':            validation_function = ValidatePhotoForm;       break;
    case 'Picker':           validation_function = ValidatePickerForm;      break;
    case 'Promo':            validation_function = ValidatePromoForm;       break;
    case 'Quote':            validation_function = ValidateQuoteForm;       break;
    case 'RadioSearchAgent': validation_function = ValidateRSAForm;         break;
    case 'Review':           validation_function = ValidateReviewForm;      break;
    case 'Signature':        validation_function = ValidateGuestbookForm;   break;
    case 'Station':          validation_function = ValidateStationForm;     break;
    case 'Subscriber':       validation_function = ValidateSubscriberForm;  break;
    case 'ScheduleDate':     validation_function = ValidateScheduleForm;    break;
    case 'Tune':             validation_function = ValidateTuneForm;        break;
    case 'Venue':            validation_function = ValidateVenueForm;       break;
    default: alert('NO JS HANDLER FOR OBJ TYPE ('+object_type+')'); return false;
    }

    validation_function(form,object_action,fv);
    //alert('OBJ TYPE ('+object_type+')'); return false;

    if (!fv.finish()) return false;
    
    if (form.target!='_self') switch (object_action)
    {
        //case 'Submit My Signature': form.target = '_self'; break;
    case 'submit': form.target = CreateQueryWindow(); break;
    case 'update': form.target = CreateQueryWindow(); break;
    case 'delete': form.target = CreateQueryWindow(); break;
    default:
        if (form.target!='') break;
        else alert('WEIRD ACTION ('+object_action+')'); return false;
    }
    //confirm('FORM TARGET: '+form.target);
    
    return true;
}

function ValidateAlbumForm(form,action,fv)
{
    var x = form.x.value;
    
    switch (action)
    {
    case 'submit':
    case 'update':
        if (fv.isWhitespace(x)) fv.raiseError("You have to enter an x.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete this project?');
        break;
    }
}

function ValidateArticleForm(form,action,fv)
{
    var headline = form.headline.value;
    var article = form.article.value;
    
    switch (action)
    {
    case 'submit':
    case 'update':
        if (fv.isWhitespace(headline)) fv.raiseError("You have to enter a headline.");
        if (fv.isWhitespace(article)) fv.raiseError("You have to enter an article body.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the article?\nNews Articles are currently configured to expire automatically after one month.');
        break;
    }
}

function ValidateBandForm(form,action,fv)
{
    switch (action)
    {
    case 'update':
        break;
    }
}

function ValidateBinaryFileForm(form,action,fv)
{
    switch (action)
    {
    case 'update':
        var userfile = form.userfile.value;
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose a file to upload.");
        //else if (!fv.isImageFile(userfile)) fv.raiseError("The chosen file does not appear to be a valid image.\nThe Image Manager application currently accepts only jpeg images (file extension 'jpg').");
        break;
    case 'delete':
        fv.raiseError('To delete a file, you can just delete the object with which the file is associated (eg. a promo item)?');
        break;
    }

    form.target = '_self';
}

function ValidateCartForm(form,action,fv)
{
    switch (action)
    {
    case 'empty':
        fv.raiseWarning('Are You Sure?\nAll items in your cart will be deleted.');
        break;
    case 'update':
        break;
    case 'checkout':
        break;
    }

    form.target = '_self';
}

function ValidateEmailForm(form,action,fv)
{
    var address = form.address.value;
    var description = form.description.value;
    
    switch (action)
    {
    case 'update':
//         if (fv.isWhitespace(description)) fv.raiseError("You have to enter a description.");
//         break;
    case 'submit':
        //fv.raiseError('Planetmind software does not yet support dynamic image creation.\nSend an email to Emory to add an email address.');
        //break;
        
        if (fv.isWhitespace(address)) fv.raiseError("You have to enter a mailbox name (the '@hitandrunbluegrass.com' will be added automatically).");
        else if (fv.isEmailAddress(address)) fv.raiseWarning("The mailbox name you entered appears to be a full email address rather than a local mailbox.\nIn this case, '@hitandrunbluegrass.com' will not be appended.");
        else if (!fv.isMailbox(address)) fv.raiseError("The mailbox is invalid.\nYou should enter only the mailbox name without the '@' or the domain.\nMailbox names can consist of any letters or numbers as well as some other characters like '.' and '_'.\nAs an example, if you want the address 'badass@hitandrunbluegrass.com', you would enter 'badass' in the address field.");
        if (fv.isWhitespace(description)) fv.raiseError("You have to enter a description.");
        //if (!fv.isHitRunEmailAddress(address)) fv.raiseError("The email address must end with '@hitandrunbluegrass.com'.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the email address '+address+' from the contact page?');
        break;
    }
}

function ValidateGuestbookForm(form,action,fv)
{
    var name = form.name;
    var url = form.link_url;
    var email = form.email.value;
    var comments = form.comments.value;
    
    switch (action)
    {
    case 'submit':
        if (fv.isWhitespace(comments)) fv.raiseError("You have to enter a comment to sign the guestbook. (duh!)");
        if ((!fv.isWhitespace(email)) && (!fv.isEmailAddress(email))) fv.raiseError("That doesn't look like a valid email address.\n(You can just leave the email box blank if you prefer not to enter it.)");
        if (fv.isWhitespace(name.value)) fv.raiseAction('replace_value',name,'Anonymous');
        if (!fv.isWhitespace(url.value) && !fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        form.target = '_self';
        break;
    case 'update':
        fv.raiseWarning('Are you sure you want to alter the signature information?');
        if ((!fv.isWhitespace(email)) && (!fv.isEmailAddress(email))) fv.raiseError("That doesn't look like a valid email address.");
        if (fv.isWhitespace(name.value)) fv.raiseAction('replace_value',name,'Anonymous');
        if (!fv.isWhitespace(url.value) && !fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete this signature from the guestbook?');
        break;
    }
}

function ValidateHowdyForm(form,action,fv)
{
    var headline = form.headline.value;
    var article = form.article.value;
    
    switch (action)
    {
    case 'submit':
    case 'update':
        //if (fv.isWhitespace(headline)) fv.raiseError("You have to enter a greeting.");
        if (fv.isWhitespace(article)) fv.raiseError("You have to enter an article body.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the howdy?\nIt is recommended that you configure old howdies to expire automatically rather than deleting them.');
        break;
    }
}

function ValidateImageForm(form,action,fv)
{
    switch (action)
    {
    case 'update':
        var userfile = form.userfile.value;
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose an image file to upload.");
        else if (!fv.isImageFile(userfile)) fv.raiseError("The chosen file does not appear to be a valid image.\nThe Image Manager application currently accepts only jpeg images (file extension 'jpg').");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete this image?');
        break;
    }

    form.target = '_self';
}

function ValidateLinkForm(form,action,fv)
{
    var title = form.link_title.value;
    var url = form.link_url;
    
    switch (action)
    {
    case 'update':
    case 'submit':
        if (fv.isWhitespace(title)) fv.raiseError("You have to enter a link title.");
        if (fv.isWhitespace(url.value)) fv.raiseError("You have to enter a link url.");
        else if (!fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the link '+title+'?');
        break;
    }
}

function ValidateMerchandiseForm(form,action,fv)
{
    var name = form.item_name.value;
    var description = form.description.value;
    
    switch (action)
    {
    case 'update':
    case 'submit':
        if (fv.isWhitespace(name)) fv.raiseError("You have to enter an item name.");
        if (fv.isWhitespace(description)) fv.raiseError("You have to enter an item description.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the item '+name+' from the store?');
        break;
    }
}

function ValidatePhotoForm(form,action,fv)
{
    switch (action)
    {
    case 'submit':
        var userfile = form.userfile.value;
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose a photograph to upload.");
        else if (!fv.isImageFile(userfile)) fv.raiseError("The chosen file does not appear to be a valid image.\nThe Photo Manager application currently accepts only jpeg images (file extension 'jpg').");
        break;
    case 'update':
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete this photo from the photos page?');
        break;
    }
}

function ValidatePickerForm(form,action,fv)
{
    var first_name = form.first_name.value;
    var last_name = form.last_name.value;
    var email = form.email.value;
    var url = form.url;
    
    switch (action)
    {
    case 'submit':
        var userfile = form.userfile.value;
        if ((!fv.isWhitespace(userfile)) && (!fv.isImageFile(userfile))) fv.raiseError("The chosen file does not appear to be a valid image.\nThe Image Manager application currently accepts only jpeg images (file extension 'jpg').");
    case 'update':
        if ((fv.isWhitespace(first_name)) && (fv.isWhitespace(last_name))) fv.raiseError("You have to enter either a first or last name (or both).");
        if (!fv.isWhitespace(test) && !fv.isEmailAddress(test)) fv.raiseError("That doesn't look like a valid email address.");
        if (!fv.isWhitespace(url.value) && !fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete '+first_name+' from the pickers page?');
        break;
    }
}

function ValidatePromoForm(form,action,fv)
{
    var name = form.name.value;
    var description = form.description.value;
    
    switch (action)
    {
    case 'submit':
        var userfile = form.userfile.value;
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose a file to upload.");
    case 'update':
        if (fv.isWhitespace(name)) fv.raiseError("You have to enter an item name.");
        if (fv.isWhitespace(description)) fv.raiseError("You have to enter an item description.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the item '+name+'?');
        break;
    }
}

function ValidateQuoteForm(form,action,fv)
{
    var content = form.content.value;
    var source = form.source.value;
    
    switch (action)
    {
    case 'submit':
    case 'update':
        if (fv.isWhitespace(content)) fv.raiseError("You have to enter a quotation.");
        if (fv.isWhitespace(source)) fv.raiseError("You have to enter a quotation source.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the article?\nNews Articles are currently configured to expire automatically after one month.');
        break;
    }
}

function ValidateReviewForm(form,action,fv)
{
    var source = form.source.value;
    var userfile = form.userfile.value;
    var author = form.author.value;
    
    switch (action)
    {
    case 'submit':
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose a file to upload.");
    case 'update':
        if (fv.isWhitespace(source)) fv.raiseError("You have to enter a review source.");
        if (fv.isWhitespace(author)) fv.raiseError("You have to enter an author.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the review?');
        break;
    }

    fv.raiseError('redo validatereviewform in form_validator.js');
}

function ValidateRSAForm(form,action,fv)
{
    var type = getSelectedValues(form.search_type);
    var call = form.search_call.value;
    var zip = form.search_zip.value;
    var states = getSelectedValues(document.getElementById('search_states'));
    
    switch (type)
    {
    case 'call':
        call = form.search_call.value = call.toUpperCase();
        if (fv.isWhitespace(call)) fv.raiseError("You have to enter 1 or more call letters\n(For example: 'K', 'KG', or 'KGNU').");
        else if (!fv.isPartialRadioCall(call)) fv.raiseError("Station call letter code can contain only the letters A-Z.\n");
        else if (!fv.isUSRadioCall(call)) fv.raiseError("Station call letters must begin with either 'K' or 'W' (US Radio) at the present time.\nFunctionality for international station searching is on the way...");
        break;
    case 'state':
        if (fv.isEmpty(states)) fv.raiseError("You have to choose at least one state.");
        break;
    case 'zip':
        if (fv.isWhitespace(zip)) fv.raiseError("You have to enter a partial zipcode.\n(For example: 8, 803, or 80302)");
        else if (!fv.isPartialZipCode(zip)) fv.raiseError("That doesn't look like a partial zipcode.");
        break;
    }

    form.target = '_self';
}

function ValidateScheduleForm(form,action,fv)
{
    var month = form.month.value;
    var day = form.day.value;
    var year = form.year.value;
    var date = month+'.'+day+'.'+year;
    
    var venue_id = form.venue_id.value;
    
    switch (action)
    {
    case 'submit':
        if (venue_id==0) ValidateVenueForm(form,action,fv);
        break;
    case 'update':
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the date '+date+' from the schedule?');
        break;
    }
}

function ValidateStationForm(form,action,fv)
{
    var call = form.call.value;
    var band = form.band.value;
    var frequency = form.frequency.value;

    var program = form.program.value;
    var url = form.url;
    var comments = form.comments.value;
    
    var first_name = form.first_name.value;
    var last_name = form.last_name.value;
    
    var address = form.address.value;
    var city = form.city.value;
    var state = form.state.value;
    var zip = form.zip.value;
    var email = form.email.value;
    var phone = form.phone;
    
    switch (action)
    {
    case 'update':
    case 'submit':
        if (fv.isWhitespace(call)) fv.raiseError("You have to enter the station call letters.");
        else if (!fv.isFullRadioCall(call)) fv.raiseError("Call letters should be alphabetic (A-Z) and consist of 4 characters.");
        else if (!fv.isUSRadioCall(call)) fv.raiseWarning("Did you really mean to enter non-US station call letters?");
        if (!fv.isWhitespace(url.value) && !fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        if (!fv.isWhitespace(phone.value))
        {
            var phone_digits = phone.value.replace(/[^0-9]/ig, '');
            if (!fv.isPhoneNumber(phone_digits))  fv.raiseError("The phone number is invalid.\n\nYou can format the number however you like, as long as it has exactly 10 digits.\n\nFor example: (303) 818-3811.");
            else fv.raiseAction('format_phone',phone,phone_digits);
        }
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete '+call+' from the radio page?');
        break;
    }
}

function ValidateTuneForm(form,action,fv)
{
    var title = form.title.value;
    var description = form.author.value;
    
    switch (action)
    {
    case 'submit':
        var userfile = form.userfile.value;
        if (fv.isWhitespace(userfile)) fv.raiseError("You have to choose an audio file to upload.");
        else if (!fv.isAudioFile(userfile)) fv.raiseError("The chosen file does not appear to be a valid audio file.\nThe Tunes Manager application currently accepts only mpeg audio files (file extension 'mp3').");
    case 'update':
        if (fv.isWhitespace(title)) fv.raiseError("You have to enter a title for the song.");
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the song '+title+' from the tunes page?');
        break;
    }
}

function ValidateVenueForm(form,action,fv)
{
    var address = form.address.value;
    var name = form.name.value;
    var city = form.city.value;
    var state = form.state.value;
    var zip = form.zip.value;
    var phone = form.phone;
    var url = form.url;

    switch (action)
    {
    case 'update':
    case 'submit':
        if (fv.isWhitespace(address)) fv.raiseWarning("Sure you dont want to enter a venue address?\n\nUsers will not be able to view a mapquest map.");
        if (fv.isWhitespace(name)) fv.raiseError("You have to enter a venue name.");
        if (fv.isWhitespace(city)) fv.raiseError("You have to enter a venue city.");
        if (fv.isWhitespace(state)) fv.raiseError("You have to enter a venue state.");
        if (!fv.isWhitespace(zip) && !fv.isFullZipCode(zip)) fv.raiseError("The zip code is invalid.\n\nUS zip codes should have 5 digits, for example: 80302.");
        if (!fv.isWhitespace(url.value) && !fv.isURL(url.value)) fv.raiseAction('format_url',url,url.value);
        if (!fv.isWhitespace(phone.value))
        {
            var phone_digits = phone.value.replace(/[^0-9]/ig, '');
            if (!fv.isPhoneNumber(phone_digits))  fv.raiseError("The phone number is invalid.\n\nYou can format the number however you like, as long as it has exactly 10 digits.\n\nFor example: (303) 818-3811.");
            else fv.raiseAction('format_phone',phone,phone_digits);
        }
        break;
    case 'delete':
        fv.raiseWarning('Are you sure you want to permanently delete the venue '+name+' from the venue list?');
        break;
    }
}

/*************************** SUBSCRIBER FORMS ********************************/

function ValidateSubscriberForm(form,action,fv)
{
    var formname = form.name;
    var validation_function;
    
    switch(formname)
    {
    case 'signupform':        validation_function = ValidateSignupForm;         break;
    case 'loginform':         validation_function = ValidateLoginForm;          break;
    case 'lostpasswordform':  validation_function = ValidateLostPasswordForm;   break;
    case 'updateinfoform':    validation_function = ValidateUpdateInfoForm;     break;
    case 'updatelistsform':   validation_function = ValidateUpdateListsForm;    break;
    case 'updateemailform':   validation_function = ValidateUpdateEmailForm;    break;
    case 'updatepasswordform':validation_function = ValidateUpdatePasswordForm; break;
    case 'add-subscriber':    validation_function = ValidateAddSubscriberForm;  break;
    default: fv.raiseError('xxx NO JS HANDLER FOR SUBSCRIBER FORM ('+formname+')'); return;
        //default: fv.raiseError('NO JS HANDLER FOR SUBSCRIBER FORM ('+formname+')'); return;
    }

    validation_function(form,fv);
}

function ValidateSignupForm(form,fv)
{
    var email    = form.email.value;
    var password = form.password.value;
    
    if (fv.isWhitespace(email)) fv.raiseError("Please enter an email address.");
    if (!fv.isWhitespace(email) && !fv.isEmailAddress(email)) fv.raiseError("That doesn't look like a valid email address.");
    if (fv.isWhitespace(password)) fv.raiseError("You need to enter a password too.");
}

function ValidateAddSubscriberForm(form,fv)
{
    var email    = form.email.value;
    var first_name = form.first_name.value;
    var last_name = form.last_name.value;
    
    if (fv.isWhitespace(email)) fv.raiseError("Please enter an email address.");
    if (!fv.isWhitespace(email) && !fv.isEmailAddress(email)) fv.raiseError("That doesn't look like a valid email address.");
}

function ValidateLoginForm(form,fv)
{
    var email    = form.email.value;
    var password = form.password.value;
    
    if (fv.isWhitespace(email)) fv.raiseError("Please enter an email address.");
    if (!fv.isWhitespace(email) && !fv.isEmailAddress(email)) fv.raiseError("That doesn't look like a valid email address.");
    if (fv.isWhitespace(password)) fv.raiseError("You need to enter a password too.");
}

function ValidateUpdateInfoForm(form,fv)
{
    //DisplayFormElements(form);
}

function ValidateUpdateListsForm(form,fv)
{
    //DisplayFormElements(form);
}

function ValidateUpdateEmailForm(form,fv)
{
    var password   = form.password.value;
    var old_email  = form.old_email.value;
    var new_email1 = form.new_email.value;
    var new_email2 = form.new_email2.value;
    
    if (fv.isWhitespace(password)) fv.raiseError("Sorry, but you have to enter your password to change your email address.");
    if (fv.isWhitespace(new_email1)) fv.raiseError("Please enter your new email address.");
    if (fv.isWhitespace(new_email2)) fv.raiseError("Sorry, but you have to enter your new email address twice.");
    if (!(new_email1 == new_email2)) fv.raiseError("Your Emails Don't Match!");
    if (!fv.isWhitespace(new_email1) && !fv.isEmailAddress(new_email1)) fv.raiseError("That doesn't look like a valid email address.");
    if (old_email == new_email1) fv.raiseError("Silly, why would you want to change your email address to what it already is?");
}

function ValidateUpdatePasswordForm(form,fv)
{
    var old_password  = form.old_password.value;
    var new_password1 = form.new_password.value;
    var new_password2 = form.new_password2.value;
    
    if (fv.isWhitespace(old_password)) fv.raiseError("Sorry, but you have to enter your old password to change it.");
    if (fv.isWhitespace(new_password1)) fv.raiseError("You have to have a password. You can make it as simple as you like...");
    if (fv.isWhitespace(new_password2)) fv.raiseError("Sorry, but you have to enter your new password twice.");
    if (!(new_password1 == new_password2)) fv.raiseError("Uh-Oh ... Your Passwords Don't Match!");
}

function ValidateLostPasswordForm(form,fv)
{
    var email = form.email.value;
    
    if (fv.isWhitespace(email)) fv.raiseError("Please enter an email address.");
    if (!fv.isWhitespace(email) && !fv.isEmailAddress(email)) fv.raiseError("That doesn't look like a valid email address.");
}

/*************************** NEWSLETTER FORMS ********************************/

function ValidateNewsletterForm(form,action,fv)
{
    var formname = form.name;
    var validation_function;
    
    switch(form.name)
    {
    case 'composeform':   validation_function = ValidateNewsletterComposeForm;   break;
    case 'recipientform': validation_function = ValidateNewsletterRecipientForm; break;
    case 'scheduleform':  validation_function = ValidateNewsletterScheduleForm;  break;
    case 'previewform':   validation_function = ValidateNewsletterPreviewForm;   break;
    default:              fv.raiseError('NO JS HANDLER FOR NEWSLETTER FORM: '+form.name); return;
    }

    form.target = '_self';

    validation_function(form,fv);
}
    
function ValidateNewsletterComposeForm(form,fv)
{
    var type = getSelectedValues(form.compose_type);
    
    var subject  = form.subject.value;
    var body     = form.text_body.value;

    var htmlfile = document.getElementById('file1').value;
    var textfile = document.getElementById('file2').value;

//     var htmlfile = form.userfile[0].value;
//     var textfile = form.userfile[1].value;

//     var htmlfile = form.elements[3].value;
//     var textfile = form.elements[4].value;
        
    switch (type)
    {
    case 'compose':
        if (fv.isWhitespace(subject)) fv.raiseError("You have to enter a subject.");
        if (fv.isWhitespace(body))    fv.raiseError("You have to enter a message body.");
        break;
        
    case 'upload':
        if (fv.isWhitespace(subject)) fv.raiseError("You have to enter a subject.");
        if (fv.isWhitespace(htmlfile)&&fv.isWhitespace(textfile)) fv.raiseError("You have to choose at least one file to upload.");
        if (fv.isWhitespace(htmlfile))     fv.raiseWarning("You have selected only a text file for the message body.\nAre you sure you dont want to add an html body too?");
        else if (!fv.isHtmlFile(htmlfile)) fv.raiseError("The chosen file does not appear to be a valid HTML file.\nThe file extension should be 'html'.");
        if (fv.isWhitespace(textfile))     fv.raiseWarning("You have selected only an HTML file for the message body.\nAre you sure you dont want to add a text body too?");
        else if (!fv.isTextFile(textfile)) fv.raiseError("The chosen file does not appear to be a valid text file.\nThe file extension should be 'txt'.");
        break;

    default:
        fv.raiseError("Invalid composition type ("+type+")");
        break;
    }
}

function ValidateNewsletterRecipientForm(form,fv)
{
    //DisplayFormElements(form);
    var type   = getSelectedValues(form.recipient_type);
    var states = getSelectedValues(form.elements[3]);
    var test   = form.recipient_test.value;
    var zip    = form.recipient_zip.value;

    switch (type)
    {
    case 'test':
        if (fv.isWhitespace(test)) fv.raiseError("You have to enter an email address to send a test newsletter.");
        if (!fv.isWhitespace(test) && !fv.isEmailAddress(test)) fv.raiseError("That doesn't look like a valid email address.");
        break;
    case 'zip':
        if (fv.isWhitespace(zip)) fv.raiseError("You have to enter a partial zip to select recipients by zip.");
        if (!fv.isWhitespace(zip) && !fv.isPartialZipCode(zip)) fv.raiseError("That doesn't look like a valid partial zip code.");
        break;
    case 'state':
        if (fv.isEmpty(states)) fv.raiseError("You have to choose at least one state to select recipients by state.");
        break;
    }
}

function ValidateNewsletterScheduleForm(form,fv)
{
    //DisplayFormElements(form);
    var type   = getSelectedValues(form.schedule_type);
    var states = getSelectedValues(form.elements[8]);
    var zip    = form.schedule_zip.value;

    switch (type)
    {
    case 'none': break;
    case 'upcoming': break;
    case 'date': break;
        
    case 'zip':
        if (fv.isWhitespace(zip)) fv.raiseError("You have to enter a partial zip to select recipients by zip.");
        if (!fv.isWhitespace(zip) && !fv.isPartialZipCode(zip)) fv.raiseError("That doesn't look like a valid partial zip code.");
        break;
        
    case 'state':
        if (fv.isEmpty(states)) fv.raiseError("You have to choose at least one state to select recipients by state.");
        break;

    default: fv.raiseError("Invalid composition type");
    }
}

function ValidateNewsletterPreviewForm(form,fv)
{
    //DisplayFormElements(form);
}


