/**
 * Untz Framework
 *
 * @category   Untz
 * @package    JavaScript
 * @version $Id: Model.php 254 2010-06-20 01:46:02Z jeremy $
 */

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

Helpers

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

/**
 * isset
 *
 */
function isset(varname){
    if (typeof(varname) == "undefined")
    {
        return false;
    }
    else {
        return true;
    }
}

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

Validate Elements

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

function validateElementArtistName(options) {
    $().ready(function() {

        var memberId = '';
        if (options.memberId != '') {
            memberId = options.memberId;
        }

        var artistId = '';
        if (options.artistId != '') {
            artistId = options.artistId;
        }

        var location = '';
        if (options.location != '') {
            location = options.location;
        }
        $("#art_name").rules("add", 
            {
				required: true,
                remote: "/validate.php?action=validate_unregistered_artist&memberId=" + memberId + "&artistId=" + artistId + "&location=" + location,
                messages: {
                    remote: "The artist name already exists.",
                    required: "Please enter your artist name",
                }
            }
        );
    });
}

function validateElementArtistRelationship(options) {
    $().ready(function() {

        $("#relationship").rules("add", 
            {
				required: true,
            }
        );
    });
}

function validateElementBehalfCheck(options) {
    $().ready(function() {

        $("#behalf_chk").rules("add", 
            {
                required: true,
                messages: {
                    required: " Required: ",
                }
            }
        );
    });
}

function validateElementBio(options) {
    $().ready(function() {

        $("#bio").rules("add", 
            {
                required: true,
                messages: {
                    required: " Please enter a bio.",
                }
            }
        );
    });
}

function validateElementCompany(options) {

    $().ready(function() {

        var memberId = '';
        if (options.memberId != '') {
            memberId = options.memberId;
        }

        var userType = '';
        if (options.userType != '') {
            userType = options.userType;
        }

        var location = '';
        if (options.location != '') {
            location = options.location;
        }
        //alert('userType: ' + userType);

        //v = $("span.name").html().replace(/,/g,'');
        //$("span.name").html(v);
        //*
        $('#company').blur( function() {
                //alert('$("#' + sefuParseField + '").val(): ' + $("#" + sefuParseField).val());
            if($("#sefu").val() == "") {
                //alert('$("#' + sefuParseField + '").val(): ' + $("#" + sefuParseField).val());
                //alert('$(company).val(): ' + $(company).val());
                var valueCompany = $('#company').val().replace(/[^a-zA-Z0-9'\-\_\.]/g,'');
                //alert('valueCompany' + valueCompany);
                $("#sefu").val(valueCompany);
            }
        });
        //*/
        $("#company").rules("add", 
            {
                required: true,
                remote: "/validate.php?action=validate_unregistered_company&memberId=" + memberId + "&userType=" + userType + "&location=" + location,
                //minlength: 2,
                messages: {
                    remote: "The company name is not available.",
                    required: "Please enter your company name",
                    //minlength: jQuery.format("Please, at least {0} characters are necessary")
                }
            }
        );
    });
}

function validateElementDescription(options) {
    $().ready(function() {

        var useTextEditor = false;
        if (isset(options.useTextEditor)) {
            useTextEditor = !!options.useTextEditor;
        }

        var toolbar = 'UntzSimple';
        if (isset(options.toolbar)) {
            toolbar = options.toolbar;
        }

        if (useTextEditor) {

            var optionsTextEditor = {
                toolbar: toolbar,
            }
            textEditor('description', optionsTextEditor);	
        }
        
        var required = false;
        if (isset(options.required)) {
            required = !!options.required;
        }
            
        $("#description").rules("add", 
            {
                required: required,
                messages: {
                    required: "You must enter a description.",
                }
            }
        );
    });
}

function validateElementEmail(options) {
    $().ready(function() {

        $("#email").rules("add", 
            {
				required: true,
				email: true,
                messages: {
                    required: "Please enter your email address",
                }
            }
        );
    });
}

function validateElementGenres(options) {
    $().ready(function() {

        $("#genres").rules("add", 
            {
				required: true,
                messages: {
                    required: "Please select at least one genre.",
                }
            }
        );
    });
}

function validateElementLocation(options) {

    $().ready(function() {

        var userType = '';
        if (options.userType != '') {
            userType = options.userType;
        }
        
        $("#city").rules("add", 
            {
                required: true,
                messages: {
                    required: "Please enter your city.",
                }
            }
        );
        
        $("#country").rules("add", 
            {
                required: true,
                messages: {
                    required: "Please select a country.",
                }
            }
        );
        
        //$("#not_local").click();
        //$('#not_local').removeAttr('checked')
        if ($('#not_local').is(':checked')) {
            
        }
        else {
            
        }
        
        //alert('userType: ' + userType);
        if (userType != 'promoter' && userType != 'fan') {
            $("#country").change(function () {
                if ($("#country").val() == 'US' || $("#country").val() == 'CA') {
                    //alert('local: ' + $("#country").val());
                    if ($('#not_local').is(':checked')) {
                        $('#not_local').click();
                    }
                    //alert('not_local: unchecking - Add: \nState\nZip\nMiles');
                    $("#state").rules("remove");
                    
                    if ($("#miles").length) {
                        $("#miles").rules("remove");
                    }
                    
                    if ($("#zip").length) {
                        $("#zip").rules("remove");
                    }
                    
                    $("#state").rules("add", 
                        {
                            required: true,
                            messages: {
                                required: "Please select your state/province.",
                            }
                        }
                    );
                    
                    if ($("#zip").length) {
                        $("#zip").rules("add", 
                            {
                                required: true,
                                messages: {
                                    required: "Please enter your postal/zip code.",
                                }
                            }
                        );
                    }
                    
                    if ($("#miles").length) {
                        $("#miles").rules("add", 
                            {
                                required: true,
                                messages: {
                                    required: "Please enter a search radius for your zip code.",
                                }
                            }
                        );
                    }
                }
                else {
                    //alert('not local: ' + $("#country").val());
                    $("#state").rules("remove");

                    if ($("#miles").length) {
                        $("#miles").rules("remove");
                    }
                    
                    if ($("#zip").length) {
                        $("#zip").rules("remove");
                    }

                    if (!$('#not_local').is(':checked')) {
                        //alert('not_local: clicking');
                        $('#not_local').click();
                    }
                }
            });
            //$('#not_local').attr('checked', false);
        }
        else {
             $("#state").rules("add", 
                {
                    required: true,
                    messages: {
                        required: "Please select your state/province.",
                    }
                }
            );
            
            if ($("#zip").length) {
                $("#zip").rules("add", 
                    {
                        required: true,
                        messages: {
                            required: "Please enter your postal/zip code.",
                        }
                    }
                );
            }
            
            if ($("#miles").length) {
                $("#miles").rules("add", 
                    {
                        required: true,
                        messages: {
                            required: "Please enter a search radius for your zip code.",
                        }
                    }
                );
            }
       }

    });
}

function validateElementPassword(options) {
    $().ready(function() {

        var location = '';
        if (options.location != '') {
            location = options.location;
        }

        if (location == 'sign-up') {
            $("#password").rules("add", 
                {
                    required: true,
                    minlength: 6,
                    messages: {
                        required: "Please provide a password"
                    }
                }
            );
    
            $("#password1").rules("add", 
                {
                    required: true,
                    minlength: 6,
                    equalTo: "#password",
                    messages: {
                        required: "Please provide a password",
                        equalTo: "Please enter the same password as above"
                    }
                }
            );
        }
    });
}

function validateElementPerson(options) {
    $().ready(function() {

        $("#firstname").rules("add", 
            {
				required: true,
                messages: {
                    required: "Please enter your first name",
                }
            }
        );

        $("#lastname").rules("add", 
            {
				required: true,
                messages: {
                    required: "Please enter your last name",
                }
            }
        );
    });
}

function validateElementTicketPrice(options) {
    $().ready(function() {

        $('.money').rules("add", 
            {
                number: true,
                messages: {
                    number: "You must enter a valid number without the $. For example: 19.95",
                }
            }
        );
    });
}

function validateElementTicketPriceRule(options) {
    var id = options.id;

    $('#' + id).rules("add", 
        {
            number: true,
            messages: {
                number: "You must enter a valid number without the $. For example: 19.95",
            }
        }
    );
}

function validateElementRsvpUntz(options) {
    $().ready(function() {

        var action = '';
        if (options.action != '') {
            action = options.action;
        }

        if ($('#rsvp_untz').is(':checked')) {
            $('#rsvp_guests').show();
            $('#elementRsvpOffer').show();
            if (action == 'edit') {
                $('#elementRsvpEnd').show();
                $('#rsvpGuestExport').show();
            }
        }
        else {
            $('#rsvp_guests').hide();
            $('#elementRsvpOffer').hide();
            if (action == 'edit') {
                $('#elementRsvpEnd').hide();
                $('#rsvpGuestExport').hide();
            }
        }

        $("#rsvp_untz").change(function () {
            if ($('#rsvp_untz').is(':checked')) {
                $('#rsvp_guests').show();
                $('#elementRsvpOffer').show();
                if (action == 'edit') {
                    $('#elementRsvpEnd').show();
                    $('#rsvpGuestExport').show();
                }
            }
            else {
                $('#rsvp_guests').hide();
                $('#elementRsvpOffer').hide();
                if (action == 'edit') {
                    $('#elementRsvpEnd').hide();
                    $('#rsvpGuestExport').hide();
                }
            }
        });

    });
}

function validateElementRsvpUrl(options) {
    $().ready(function() {

        $("#rsvp_url").rules("add", 
            {
                url: true,
                messages: {
                    url: "You must enter a full URL like http://theuntz.com",
                }
            }
        );
    });
}

function validateElementSecurityQuestion(options) {
    $().ready(function() {

        $("#sec_question").rules("add", 
            {
                required: true,
                messages: {
                    required: "Please select a security question.",
                }
            }
        );

        $("#sec_answer").rules("add", 
            {
                required: true,
                messages: {
                    required: "Please enter an answer to the security question.",
                }
            }
        );
    });
}

function validateElementSefu(options) {
    $().ready(function() {

        var location = '';
        if (options.location != '') {
            location = options.location;
        }
        
        if (location != 'sign-up') {
            $.validator.addMethod("sefu", function(value, element) { 
                //var re = new RegExp("/^[a-zA-Z'\-\_\.\s]+$/");
                //alert('re.test(value): ' + re.test(value));
                return this.optional(element) || /^[a-zA-Z0-9'\-\_\.]+$/.test(value); 
            }, "You must use alphanumeric characters"); 
                
            var sefuId = '';
            if (options.sefuId != '') {
                sefuId = options.sefuId;
            }    
            
            var url = "/validate.php?action=validate_sefu&code=" + sefuId;
            //alert('url: ' + url);
            
            $("#sefu").rules("add", 
                {
                    required: true,
                    remote: url,
                    sefu: true,
                    //minlength: 2,
                    messages: {
                        remote: "That custom url is already taken. Please try another.",
                        required: "You must have a Custom The Untz URL",
                        //minlength: jQuery.format("Please, at least {0} characters are necessary")
                    }
                }
            );
        }
            
    });
}

function validateElementTicketUrl(options) {
    $().ready(function() {

        $("#ticket_url").rules("add", 
            {
                url: true,
                messages: {
                    url: "You must enter a full URL like http://theuntz.com",
                }
            }
        );
    });
}

function validateElementUrl(options) {
    $().ready(function() {

        $("#url").rules("add", 
            {
                required: true,
                messages: {
                    required: "You must enter a relative or full URL.",
                }
            }
        );
    });
}

function validateElementUsername(options) {
    $().ready(function() {

        var memberId = '';
        if (options.memberId != '') {
            memberId = options.memberId;
        }

        $.validator.addMethod("userName", function(value, element) { 
            return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); 
        }, "Invalid username."); 
        
        $("#username").rules("add", 
            {
				required: true,
				userName: true,
				remote: "/validate.php?action=validate_username&memberId=" + memberId,
                messages: {
                    required: "Please enter a user name.",
                    remote: "User name already exist.",
                }
            }
        );
    });
}

function validateElementValidateCode(options) {
    $().ready(function() {


        var location = '';
        if (options.location != '') {
            location = options.location;
        }
        
        if (location == 'sign-up') {
            $("#validate_code").rules("add", 
                {
                    required: true,
                    messages: {
                        required: "Please enter the validation code",
                    }
                }
            );
        }
    });
}

function validateElementVenueName(options) {
    $().ready(function() {

        $("#name").rules("add", 
            {
                required: true,
                messages: {
                    required: "You must enter a name for the Venue.",
                }
            }
        );
    });
}

function validateElementWebsite(options) {
    $().ready(function() {

        $("#website").rules("add", 
            {
                url: true,
                messages: {
                    url: "You must enter a full URL like http://theuntz.com",
                }
            }
        );
    });
}

function validateElementWhatsNew(options) {
    $().ready(function() {

        $("#whats_new").rules("add", 
            {
                required: true,
                messages: {
                    required: "You must enter text for What's New",
                }
            }
        );
    });
}

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

Validate Element Groups

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

function validateArtist(options) {

    options.userType = 'artist';

    var unregistered = '';
    if (options.unregistered !== '' && options.unregistered !== null) {
        unregistered = options.unregistered;
    }

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    //alert('userType: ' + userType);
    if (location == 'backend' && unregistered) {
        validateElementArtistName(options);    
        validateElementSefu(options);
    }
    else if (location == 'backend') {
        validateElementArtistName(options);    
        
        //validateElementArtistRelationship(options);    
        //validateElementEmail(options);    
        //validateElementGenres(options);
        //validateElementLocation(options);
        //validateElementPerson(options);    
        validateElementSefu(options);
    }
    else if (location == 'update-add') {
        //validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
    else if (location == 'update') {
        //validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        //validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
    }
    else if (location == 'artist-add') {
        validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        //validateElementLocation(options);
        //validateElementPerson(options);    
        validateElementBehalfCheck(options);
        validateElementBio(options);
    }
    else if (location == 'artist-edit') {
        //validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        //validateElementLocation(options);
        //validateElementPerson(options);    
        //validateElementBehalfCheck(options);
        validateElementBio(options);
        validateElementSefu(options);
    }
    else if (location == 'sign-up-add') {
        validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementBehalfCheck(options);
        validateElementBio(options);
    }
    else if (location == 'sign-up') {
        validateElementArtistName(options);    
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
    }
    else {
        validateElementArtistName(options);
        //return;
        //validateElementArtistRelationship(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
    }
}

function validateArtistRepresentive(options) {

    options.userType = 'artist_rep';

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend') {
        validateElementUsername(options);    
        
        //validateElementEmail(options);    
        //validateElementGenres(options);
        //validateElementLocation(options);
        //validateElementPerson(options);    
    }
    else if (location == 'update') {
        validateElementEmail(options);    
        validateElementArtistRelationship(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
    }
    else {
        validateElementEmail(options);    
        validateElementArtistRelationship(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
    }
}

function validateContest(options) {

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend-edit') {
        //validateElementLocation(options);
        validateElementSefu(options);
        options.location = 'edit';
        validateElementRsvpUntz(options)
    }
    else if (location == 'backend-add') {
        //validateElementLocation(options);
        //validateElementSefu(options);
    }
    else if (location == 'update') {
        //validateElementLocation(options);
        validateElementSefu(options);
    }
    else {
        //validateElementLocation(options);
    }
}

function validateEvent(options) {

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location);
    if (location == 'backend-edit') {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        validateElementSefu(options);
        validateElementTicketPrice(options);

        //validateElementDescription(options);
        //validateElementLocation(options);
    }
    else if (location == 'backend-add') {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        validateElementTicketPrice(options);
        //validateElementLocation(options);
        //validateElementSefu(options);
        //validateElementDescription(options);
    }
    else if (location == 'event-edit') {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        //validateElementLocation(options);
        validateElementSefu(options);
        validateElementDescription(options);
        validateElementTicketPrice(options);
    }
    else if (location == 'event-add') {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        validateElementDescription(options);
        //validateElementLocation(options);
        validateElementTicketPrice(options);
    }
    else if (location == 'update') {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        //validateElementLocation(options);
        validateElementDescription(options);
        validateElementSefu(options);
        validateElementTicketPrice(options);
    }
    else {
        validateElementWebsite(options);
        validateElementTicketUrl(options);
        validateElementTicketPrice(options);
        validateElementRsvpUrl(options);
        validateElementRsvpUntz(options);
        validateElementDescription(options);
        //validateElementLocation(options);
    }
}

function validateFan(options) {

    options.userType = 'fan';

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend') {
        //validateElementEmail(options);    
        //validateElementGenres(options);
        //validateElementLocation(options);
        //validateElementPerson(options);    
    }
    else if (location == 'update') {
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
    }
    else {
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
    }
}

function validateLabel(options) {

    options.userType = 'label';

    var unregistered = '';
    if (options.unregistered !== '' && options.unregistered !== null) {
        unregistered = options.unregistered;
    }

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend' && unregistered) {
        validateElementSefu(options);
        validateElementCompany(options);    
    }
    else if (location == 'backend') {
        validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementSefu(options);
    }
    else if (location == 'update') {
        //validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
    else if (location == 'label-edit') {
        //validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        //validateElementPerson(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
    else if (location == 'label-add') {
        //validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementBio(options);
    }
    else {
        validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
}

function validateNews(options) {

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend-edit') {
        //validateElementLocation(options);
        validateElementSefu(options);
    }
    else if (location == 'backend-add') {
        //validateElementLocation(options);
        //validateElementSefu(options);
    }
    else if (location == 'update') {
        //validateElementLocation(options);
        validateElementSefu(options);
    }
    else {
        //validateElementLocation(options);
    }
}

function validatePromoter(options) {

    options.userType = 'promoter';

    var unregistered = '';
    if (options.unregistered !== '' && options.unregistered !== null) {
        unregistered = options.unregistered;
    }

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend' && unregistered) {
        validateElementCompany(options);    
        validateElementSefu(options);
    }
    else if (location == 'backend') {
        validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementSefu(options);
    }
    else if (location == 'update') {
        validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPerson(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
    else {
        validateElementCompany(options);    
        validateElementEmail(options);    
        validateElementGenres(options);
        validateElementLocation(options);
        validateElementPassword(options);    
        validateElementPerson(options);    
        validateElementSecurityQuestion(options);    
        validateElementValidateCode(options);    
        validateElementUsername(options);    
        validateElementSefu(options);
        validateElementBio(options);
    }
}

function validateSefu(options) {
    
    validateElementSefu(options);    
    
    //validateElementUrl(options);    
}

function validateVenues(options) {

    var location = '';
    if (options.location != '') {
        location = options.location;
    }
    //alert('location: ' + location + ' - unregistered: ' + unregistered);
    if (location == 'backend-edit') {
        validateElementVenueName(options);
        validateElementLocation(options);
        validateElementSefu(options);
    }
    else if (location == 'backend-add') {
        validateElementVenueName(options);
        validateElementLocation(options);
        //validateElementSefu(options);
    }
    else if (location == 'update') {
        validateElementVenueName(options);
        validateElementLocation(options);
        validateElementSefu(options);
    }
    else {
        validateElementVenueName(options);
        validateElementLocation(options);
        //validateElementSefu(options);
    }
}

function validateWhatsNew(options) {
    
    validateElementUrl(options);    

    validateElementWhatsNew(options);    
}

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

Validate Element Groups

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

function validateFormUntz(options) {

    var formId = '';
    if (options.formId != '') {
        $("#" + options.formId).validate();
    }

    var location = '';
    if (options.location != '') {
        location = options.location;
    }

    var userType = '';
    if (options.userType != '') {
        userType = options.userType;
    }
    //alert('location: ' + location + ' - userType: ' + userType);
    //return;

    if (userType == 'artist_add' || userType == 'artist') {
        validateArtist(options);
    }
    else if (userType == 'artist_rep') {
        validateArtistRepresentive(options);    
    }
    else if (userType == 'fan') {
        validateFan(options);    
    }
    else if (userType == 'label') {
        validateLabel(options);    
    }
    else if (userType == 'promoter') {
        validatePromoter(options);    
    }
   
}

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

Autocomplete

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

function untzAutocomplete(id, options) {
    $().ready(function() {

        var untzType = '';
        if (options.untzType != '') {
            untzType = options.untzType;
        }

        var untzTypeKey = '';
        if (options.untzTypeKey != '') {
            untzTypeKey = options.untzTypeKey;
        }

        var untzTypeText = '';
        if (options.untzTypeText != '') {
            untzTypeText = options.untzTypeText;
        }

        var url = "/json.php?action=" + untzType;

        if (untzType == 'labels' || untzType == 'promoters') {

            if (options.unregistered) {
                url += '&unregistered=true'
            }
        }
        
        var mySelector = "#" + id;
        //alert('url: ' + url + ' - mySelector: ' + mySelector);
        if (isset(options.mySelector)) {
            mySelector = options.mySelector;
        }
        
        //alert('url: ' + url);
        //alert('url: ' + url + ' - mySelector: ' + mySelector);
        $(mySelector).autocomplete({
            source: url,
            json: true,
            width: 260,
            //*
            select: function(event, ui) {
                if (isset(ui.item)) {
                    if (untzTypeKey != '') {
                        $('#' + untzTypeKey).val(ui.item.id);
                    }

                    if (untzTypeText != '') {
                        $('#' + untzTypeText).html(ui.item.id);
                   }
                }
                //log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
            }//*/
            //mustMatch: true,
            //formatItem: formatItem,
            //formatResult: formatResult
        });
        //.data( "autocomplete" )._renderItem = function( ul, item ) {
        //    return $( "<li></li>" )
        //        .data( "item.autocomplete", item )
        //        .append( item.label + " (" + item.id + ")" )
        //        .appendTo( ul );
        //};
    });

}

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

Text Editor

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

/**
 * Drop in a text editor into forms
 *
 */
function textEditor(id, options) {
    $(function()
        {

            var toolbar = '';
            if (options.toolbar == '') {
                options.toolbar = 'UntzSimple';
            }
            
            /*
            var options = {
                //skin: 'v2',
                //skin: 'kama',
                //skin: 'office2003',
                //width: '50%',
            };
            */
            $('#' + id).ckeditor(function() { /* callback code */ }, options);   
        }
    );
}

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

Date & Time Functions

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

/**
 * Drop in a datetime picker
 *
 */
function dateTimePicker() {
    $(function()
        {
            var options = {
                //format: "%Y-%m-%d %H:%i",
                //formatUtcOffset: "%: (%@)",
                //hideInput: true,
                //dateFormat: 'yyyy-mm-dd HH:MM:ss',
                dateFormat: "isoDateTime",
                //dateFormat: "fullDate",
                changeMonth: true,
                changeYear: true,
                numberOfMonths: 2,
                showButtonPanel: true,
            }

            $('.datetimepicker').datetimepicker(options);
        }
    );
}

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

Banners

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

/**
 * Load Banner
 *
 */
function untzLoadBanner(myFlashUpload, options) {
    //alert('options.id: ' + options.id);
    //alert('options.width: ' + options.width);
    if (!isset(options)) {
        var options = {};
    }
    
    if (!isset(options.id)) {
        options.id = 'flashPlayer';
    }
    
    if (!isset(options.width)) {
        options.width = 200;
    }
    
    if (!isset(options.height)) {
        options.height = 200;
    }

    $(function()
        {
            var html = '';

            if (myFlashUpload == '' || myFlashUpload == null) {
                html = 'NO FLASH FILE';
                return html;
            }

            var so = new SWFObject(myFlashUpload, "mymovie", options.width, options.height, "7", "#FFFFFF");
            //so.addVariable("autoPlay", "no");
            so.write(options.id);
        }
    );
}

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

Tickets

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

function untzAddTicketLevel(options) {
/*
<tr>
  <td ><label for="price_<{$myEventTicketNextLevel}>">Ticket Level <{$myEventTicketNextLevel}> Price</label></td>
  <td colspan="2"><input name="price_<{$myEventTicketNextLevel}>" type="text" id="price_<{$myEventTicketNextLevel}>" size="45" value="" /></td>
</tr>
<tr>
  <td><label for="events_tickets_<{$ticket.level}>">Ticket Level <{$myEventTicketNextLevel}> Description</label></td>
  <td colspan="2"><input name="events_tickets_<{$myEventTicketNextLevel}>" type="text" id="events_tickets_<{$myEventTicketNextLevel}>" size="45" value="" /></td>
</tr>

*/
    var levels = 10;
    var level = Math.ceil($("#ticketTable").attr('rows').length / 2) + 1;
    //alert('level: ' + level);
    if (level <= levels) {
        $("#ticketTable").find('tbody')
            .append($('<tr>')
                .append($('<td>')
                    .append($('<label>')
                        .attr('for', 'price_' + level)
                        .text('Ticket Level ' + level +  ' Price')
                    )
                )
                .append($('<td>')
                    .append($('<input>')
                        .attr('id', 'price_' + level)
                        .attr('name', 'price[' + level + ']')
                        .attr('size', 45)
                        .attr('class', 'money')
                    )
                )
            )
            .append($('<tr>')
                .append($('<td>')
                    .append($('<label>')
                        .attr('for', 'events_tickets_' + level)
                        .text('Ticket Level ' + level +  ' Description')
                    )
                )
                .append($('<td>')
                    .append($('<input>')
                        .attr('id', 'events_tickets_' + level)
                        .attr('name', 'events_tickets[' + level + ']')
                        .attr('size', 45)
                    )
                )
            )
        ;
        
        validateElementTicketPriceRule({id: 'price_' + level});
    }
    else {
    alert('Sorry, you cannot add more than ' + levels + ' levels');
    }
    // Return false to prevent the form from being submitted.
    return false;
    /*
    $("#ticketTable").find('tbody')
    .append($('<tr'>)
        .append($('<td>')
            .append($('<label>')
                .attr('for', 'img.png')
                .text('Image cell')
            )
        )
    );
    //*/
}

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

RSVP

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

function untzRsvp(options) {
    
    if (options.id == '' || options.id == null) {
        alert('Unable to RSVP, no event was selected.');
        return false;
    }

    $(function() {
        $("#rsvpNow").click(function () { 
           
            var guests = $("#rsvp_guests").val();      
            var dataString = '&rsvp=yes&guests=' + guests;
            //alert('dataString: ' + dataString);return false;
            $.ajax({
                type: "GET",
                url: "/event-detail?id=" + options.id,
                data: dataString,
                dataType: 'json',
                success: function(data) {
                    $('#rsvp').html("<div id='message'></div>");
                    if (data.status == false) {
                        $('#message').html("<img style='float: left;' src='/images/warning.png' />")
                        .append(data.message);
                    }
                    else {
                    $('#message').html("<img style='float: left;' src='/images/ok.png' />")
                        .append(data.message);
                    }
                }
            });
        });
    });
  
}
/*******************************************************************************

Deprecated - still in production

*******************************************************************************/
/*
function FormCheck() {
    return true;
	if($("#not_local").attr('checked')) {
		$("#zip").rules("remove");
		$("#city").rules("remove");
		$("#state").rules("remove");
		$("#country").rules("remove");
		$("#miles").rules("remove");
	} else {
		if($("#zip").val() != "") {
			$("#zip").rules("add", {
				required: true
			});
			$("#city").rules("remove");
			$("#state").rules("remove");
			$("#country").rules("remove");
		} else {
			$("#city").rules("add", {
				required: true
			});
			$("#state").rules("add", {
				required: true
			});
			$("#country").rules("add", {
				required: true
			});
		}
		$("#miles").rules("add", {
			required: true
		});
	}
	return true;
}
*/
function login_chk() {	
    $.ajax({ 
        url:       "login_chk.php",
        data:{uname:$('#r_username').val(),pwd:$('#r_password').val(),remb:$('#remb:checked').val()},
        type:      "post",
        success:   function(msg){
                  if(msg=='1'){
                  location.reload();
                  }else{
                  $("#login_msg").html("Incorrect User Name or Password!");
                  $("#login_msg").css('display','');
                  }
               }
    }); 
}

/* - Old {
$("#city").blur( function() {
    if($("#city").val() != "" && $("#state").val() != "" && $("#country").val() != "") {
        $("#zip").rules("remove");
    } else {
        $("#zip").rules("add", {
            required: true
        });
    }
});
$("#state").blur( function() {
    if($("#city").val() != "" && $("#state").val() != "" && $("#country").val() != "") {
        $("#zip").rules("remove");
    } else {
        $("#zip").rules("add", {
            required: true
        });
    }
});
$("#country").blur( function() {
    if($("#city").val() != "" && $("#state").val() != "" && $("#country").val() != "") {
        $("#zip").rules("remove");
    } else {
        $("#zip").rules("add", {
            required: true
        });
    }
});

$("#zip").blur( function() {
    if($("#zip").val() != "") {
        $("#city").rules("remove");
        $("#state").rules("remove");
        $("#country").rules("remove");
    } else {
        $("#city").rules("add", {
            required: true
        });
        $("#state").rules("add", {
            required: true
        });
        $("#country").rules("add", {
            required: true
        });
    }
} ); 
}*/

/*
	// validate signup form on keyup and submit
	$.validator.addMethod("sefu", function(value, element) { 
        var re = new RegExp("/^[a-zA-Z'.\s]{1,40}$/");
        alert('re.test(value): ' + re.test(value));
		return this.optional(element) || re.test(value); 
	}, "You must use alphanumeric characters"); 

	$.validator.addMethod("userName", function(value, element) { 
		return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); 
	}, "Invalid username."); 

	$("#signupForm").validate({
		rules: {
			username: {
				required: true,
				userName: true,
				remote: "/validate.php?action=validate_username"
			},
			password: {
				required: true,
				minlength: 6
			},
			password1: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			sec_question: "required",
			sec_answer: "required",
			firstname: "required",
			lastname: "required",
			relationship: "required",
			multiple: "required",
			//gender: "required",
			email: {
				required: true,
				email: true
			},

			art_name: {
				required: true,
				//required: "#art_type_1:checked",
				remote: "/validate.php?action=validate_unregistered_artist",
			},
			
			//multiple: {
			//	required: "#art_type_2:checked",
			//},
			
			city: {required: "#not_local:unchecked",},
			state: {required: "#not_local:unchecked",},
			country: {required: "#not_local:unchecked",},
			zip: "required",
			miles: {required: "#not_local:unchecked",},
			
			validate_code: {
				required: true,
				remote: "/validate.php?action=validate_code"
			}
		},
		messages: {
			username: {
				remote: "User name already exist"
			},
			password: {
				required: "Please provide a password"
			},
			password1: {
				required: "Please provide a password",
				equalTo: "Please enter the same password as above"
			},
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
            art_name: {
                remote: "The artist name already exists.",
                required: "Please enter your artist name",
                //minlength: jQuery.format("Please, at least {0} characters are necessary")
            }
		}
	});
*/

