
function new_image_row(table_id) {
    var table = document.getElementById(table_id);
    var root=table.getElementsByTagName('tr')[0].parentNode;//the TBODY
    var clone=table.getElementsByTagName('tr')[0].cloneNode(true);//the clone of the first row
    var inputs=clone.getElementsByTagName('input');
    var new_row=true;
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].value = inputs[i].defaultValue;
        if (inputs[i].type == 'radio') {
            var counter = document.getElementById('image_count');
            if(counter) {
                if (new_row) {
                    counter.value = parseInt(counter.value) + 1;
                    new_row = false;
                }
                inputs[i].value = 'x'+counter.value;
            }
            inputs[i].checked = false;
        }
    }
    root.appendChild(clone);//appends the clone
}

function upload_zip(table_id) {
    var table = $(table_id);
    table.toggle();
}

/***************************************************************************************
This function controls that the object has a value.
If it does not have a value, it alerts the user.
@object        The HTML object that needs to be controlled.
               Normally the object should be passed using the "this" object-reference,
               from where it is called.
@field_name    The human-readable name of the field beign controlled.
               This is used for the alert message returned to the user if necessary.

@return        Returns 1 if the value is a number, 0 otherwise
****************************************************************************************/
function control_empty(object,field_name)
{
        var value=object.value;
        if(value.length == 0)
        {
                cmd = '';
                if (object.id)
                    cmd = "setTimeout('document.getElementById(\\'"+object.id+"\\').focus();',1);";
                display_notice('The value for "'+field_name+'" could not be empty.','','',cmd);
                return 0;
        }
         return 1;
}

function validate_contact_form() {
    var ret = true;
    var msg = '';
    var EMAIL_REGEX = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    var PHONE_REGEX = /^[0-9 \-()]{10,}$/;

    if (document.getElementById('contact_first_name').value.length < 2) {
        msg += '- Please enter your First Name\n';
    }

    if (document.getElementById('contact_last_name').value.length < 2) {
        msg += '- Please enter your Last Name\n';
    }

    if (!EMAIL_REGEX.test(document.getElementById('contact_email').value)) {
        msg += '- Please enter a valid Email address\n';
    }

    if (document.getElementById('contact_phone_number').value.length > 0 && !PHONE_REGEX.test(document.getElementById('contact_phone_number').value)) {
        msg += '- Please enter a valid Phone Number\n';
    }

    if (document.getElementById('contact_location').options[document.getElementById('contact_location').selectedIndex].value == '') {
        msg += '- Please select the Location\n';
    }

    if (document.getElementById('contact_comments').value == '') {
        msg += '- Please enter your Comments\n';
    }
    
    if (msg.length > 0) {
        alert(msg);
        ret = false;
    }

    return ret;
}

if (typeof(Ajax) != 'undefined' && typeof(Ajax.Responders) != 'undefined') {
    Ajax.Responders.register({
        onCreate: function(request) {
            if (request && request.container) {
                receiver = $(request.container.success);
                if (receiver) {
                  receiver.innerHTML="<div align='center' class='AjaxLoadingContainer'><div class='AjaxLoadingContent AjaxLoadingContentNormal'><img width='25' height='25' alt='' src='/images/loading_circle.gif' /><br /><b>Loading...</b></div></div>";
                }
            }
        },
        onFailure: function(request) {
            if (request && request.container) {
                receiver = $(request.container.success);
                if (receiver) {
                  receiver.innerHTML="<div align='center' class='AjaxLoadingContainer'><div class='AjaxLoadingContent AjaxLoadingContentError'><b>Error loading the page, please try again.(FAILURE)</b></div></div>";
                }
            }
        },
        onError: function(request) {
            if (request && request.container) {
                receiver = $(request.container.success);
                if (receiver) {
                  receiver.innerHTML="<div align='center' class='AjaxLoadingContainer'><div class='AjaxLoadingContent AjaxLoadingContentError'><b>Error loading the page, please try again.(ERROR)</b></div></div>";
                }
            }
        },
        onComplete: function(request) {
            if (request.transport.status == 0) {
              if (request && request.container) {
                receiver = $(request.container.success);
                if (receiver) {
                  receiver.innerHTML="<div align='center' class='AjaxLoadingContainer'><div class='AjaxLoadingContent AjaxLoadingContentError'><b>Error loading the page, please try again.(COMPLETE)</b></div></div>";
                }
              }
            }
        }
     });
}

if (typeof(window.addEvent) == "function") {
    window.addEvent("domready",function(){
        $$("input[type='text']").each(function(el){
            el.addEvent('focus', function(){
                var thisVal = $(this).getValue();
                if ($(this).getValue() == $(this).defaultValue)
                    $(this).setProperty('value','');
                $(this).addEvent('blur', function(){
                    if( $(this).getValue() == '')
                        $(this).setProperty('value', thisVal);
                });
            });
        });
    });
} else if (typeof($(document).ready) == "function") {
    $(document).ready(function(){
        $("input[type='text']").focus( function(){
            var defaultVal = $(this).val();
            if ($(this).val() == this.defaultValue)
                $(this).val('');
            $(this).blur( function(){
                if($(this).val() == '')
                    $(this).val( defaultVal );
            });
        });
    });
}

